Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Auto-completion doesnt when using Go modules, if the pkg hasnt been imported #1944

Closed
ramya-rao-a opened this issue Sep 27, 2018 · 49 comments
Closed
Labels
go-modules Related to Go modules upstream

Comments

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Sep 27, 2018

Using https://github.com/stamblerre/gocode and the 0.6.90 version of this extension

In the below case we dont get any completions.

import (
        "rsc.io/quote"
        "fmt"
)

func main() {
        fmt.Println("hello")
        quote.
}

Update the quote. above to quote.Hello(), save the file. The next time auto-completions work

import (
        "rsc.io/quote"
        "fmt"
)

func main() {
        fmt.Println("hello")
        quote.Hello();
        quote.
}

@stamblerre Logging this issue here for tracking purposes as your fork doesnt support logging issues

@ramya-rao-a ramya-rao-a changed the title Auto-completion doesnt work for unimported packages Auto-completion doesnt when using Go modules, if the pkg hasnt been imported Sep 27, 2018
@ramya-rao-a ramya-rao-a added the go-modules Related to Go modules label Sep 27, 2018
@stamblerre
Copy link
Contributor

@ramya-rao-a thanks for reporting this. I just enabled issues and copied it to stamblerre/gocode#3.

@dreamheld
Copy link

You, guys, are awesome keeping on top of these things the way you do!!!

@ramya-rao-a
Copy link
Contributor Author

@stamblerre Thanks! I'll keep this issue open because invariably, folks will log an issue in this repo.

@JoseFMP
Copy link

JoseFMP commented Oct 1, 2018

To be fair, it also happens with imported packages.

@stamblerre
Copy link
Contributor

@JoseFMP, autocompletion that works for Go modules is currently in beta for VSCode (https://github.com/Microsoft/vscode-go/wiki/Use-the-beta-version-of-the-latest-Go-extension). If you use that, imported packages should autocomplete.

@joaoferrao
Copy link

@stamblerre I tried installing according to your suggestion, restarting VSCode but auto completion still doesn't happen if the project is outside the $GOPATH.

@stamblerre
Copy link
Contributor

@joaoferrao Did VSCode prompt you to install the gocode-gomod tool, and did you install it? There is a separate version of gocode that works specifically for modules.

@joaoferrao
Copy link

joaoferrao commented Oct 15, 2018

@stamblerre first time around yes, but I didn't know I should also install beta version of the extension. Then I reinstalled VSCode, installed beta version but wasn't asked to install the fork.

Anyway to force installation of the said fork?

@stamblerre
Copy link
Contributor

@joaoferrao: Yep, just execute the following commands:

go get -u -d github.com/stamblerre/gocode
go build -o $GOPATH/bin/gocode-gomod github.com/stamblerre/gocode

You should end up with a tool gocode-gomod in your $GOPATH/bin.

@joaoferrao
Copy link

@stamblerre thank you for getting back so fast, but I still am in no luck. I still only see the "PANIC".

image

I also confirm I have the beta version of the Go extension:
image

Is there anything else I can check? Any setting I should enforce in the settings.json for the go extension?

@stamblerre
Copy link
Contributor

@joaoferrao I'd recommend running gocode close and gocode-gomod close to restart gocode, just to be safe. Then, run gocode-gomod -s -debug and try to complete in VSCode. The debug output should be printed there, and if you paste it in here, I can try to investigate it.

@joaoferrao
Copy link

@stamblerre outstanding support, thanks!! Maybe would be worthwhile

image

So for others who end up here with the same issue:

cd /tmp
curl -O https://github.com/Microsoft/vscode-go/releases/download/latest/Go-0.6.92-beta.1.vsix
code --install-extension Go-latest.vsix
go get -u -d github.com/stamblerre/gocode
go build -o $GOPATH/bin/gocode-gomod github.com/stamblerre/gocode
gocode close
gocode-gomod close

Kill VSCode and relaunch.

Thanks again.

@stamblerre
Copy link
Contributor

@joaoferrao awesome, glad it works!

@zeevallin
Copy link

zeevallin commented Oct 19, 2018

Running package version 0.6.93

I want to confirm that the package is found

Package is found

But once I add a dot to autocomplete the package, nothing is returned

I go back to step one to manually import the package, there is no autocompletion on the package

Not until I save the file with the import added, will I get an autocomplete list for my package

Hope this helps! Keep up the good work.

@stamblerre
Copy link
Contributor

@zeeraw yep, this issue is tracked in stamblerre/gocode#3. It's actually the result of a problem in the go/packages API that still needs to be solved. I will update on the issue once the go/packages API has fixed this problem.

@joaoferrao
Copy link

@stamblerre so the go extension decided to update from beta to the latest release and clearly is not behaving correctly (the beta seemed more stable).

I tried repeating the process for installing the beta versions .vsix files but:

 $  code --install-extension Go-0.6.94-beta.5.vsix
Corrupt ZIP: end of central directory record signature not found

@stamblerre
Copy link
Contributor

@joaoferrao, I think this is probably a question for @ramya-rao-a?

@joaoferrao
Copy link

OK... just found out it's a problem related of downloading it with curl...

@nezorflame
Copy link
Contributor

nezorflame commented Nov 11, 2018

Wanna chime in as well. Version 0.7.0, macOS 10.14.1.
All Go tools updated, using gogetdoc + goimports + golangci-lint.
Also I have "go.toolsEnvVars": {"GO111MODULE": "on"} set, since my package is inside $GOPATH.

Fresh project, clean main.go, go mod init.
Trying to auto-import github.com/google/uuid (present in local cache):
image

After go get github.com/google/uuid:
image

Trying autocomplete:
image

After manually writing a function:
image

Trying out methods on function output:
image

But printing it with fmt.Println() gives out proper type:
image

Overall impression - very hard to operate right now.
Autocomplete just refuses to work, saved or not.

@nezorflame
Copy link
Contributor

nezorflame commented Nov 11, 2018

Another observation: behavior is different being inside $GOPATH with "go.toolsEnvVars": {"GO111MODULE": "on"} and outside $GOPATH without env.
In the second case, autocomplete indeed works after the package was used once:
image
The result of the function is also recognized well:
image

So, it seems that in order for this to work, you have to operate outside the $GOPATH, which is not something I'd like to do being used to it.

@stamblerre
Copy link
Contributor

@nezorflame: in the case where "id" had an invalid type, there appears to be a red underline beneath the import, so I wonder if that's related to the issue.

I'm also not sure why gocode would behave differently depending on the location of the code, but I would guess that it's not switching between the module-unaware and module-aware versions of gocode correctly in these cases. VSCode-Go checks that value of go env GOMOD to decide which tool to use, so if you have GO111MODULE=on set, then it should work correctly. @ramya-rao-a, what do you think?

@ramya-rao-a
Copy link
Contributor Author

@stamblerre Are you able to repro that as well? i.e insider GOPATH with "go.toolsEnvVars": {"GO111MODULE": "on"} setting?

@nezorflame
Copy link
Contributor

nezorflame commented Nov 13, 2018

@ramya-rao-a @stamblerre after some research, it seems that if the file's not saved yet, these invalid type and could not import... errors happen.
And, still, this only happens inside $GOPATH with GO111MODULE=on.

So, basically, you can't have autocomplete, auto-import etc. until your file is saved, which renders the features useless.

Some examples:

  • before saving:
    image
  • after saving:
    image

@ramya-rao-a BTW, is my issue still in the scope of the current one, or do I need to create a new one?

@gencer
Copy link

gencer commented Nov 13, 2018

@ramya-rao-a, save or not. Anything outside of current file does not auto-complete. Only internal (core) is getting autocompleted. (Like ioutil, strings...)

Update:

go-langserver works. I get all possible candidates for auto complete. However, gocode does not work. No matter what I do, it does not auto-complete anything in $GOPATH/src. Even I have a models dir in same app. It does not auto completes too.

VSCode=1.29.0
gocode=latest
go-langserver=latest

gocode does not work and go-langserver consumes too much memory and intensive CPU. What am I going to use?

Update 2:

See: mdempsky/gocode#84

@nezorflame
Copy link
Contributor

nezorflame commented Nov 13, 2018

@gencer VSCode uses @stamblerre's fork of this lib for modules as gocode-gomod binary, so I guess you should create this issue in https://github.com/stamblerre/gocode repo instead.
There's already one that looks alike: stamblerre/gocode#9

@stamblerre
Copy link
Contributor

@ramya-rao-a: I am pretty sure that I can reproduce the issue, though my VSCode-Go settings are very strange at this point so it's possible I'm missing something. When I set GO111MODULE=on through my terminal, it started working, so I expect it must be something to do with propagating the go.ToolsEnvVars.

@nezorflame: your issue is known and tracked by stamblerre/gocode#3.

@gencer: is this related to the bug you already filed here mdempsky/gocode#84? Are you using modules? mdempsky/gocode does not support modules, so if you are experiencing issues with completion using Go modules, then it would be best to file a separate issue under stamblerre/gocode.

@ramya-rao-a
Copy link
Contributor Author

@gencer @nezorflame Can you try running gocode-gomod close, ensure there are no running instances of it and try again?

Once gocode is running in server mode, the same is re-used everytime we ask for suggestions. It may happen the current running instance isnt aware of the GO111MODULE env var and has to be restarted?

@gencer
Copy link

gencer commented Nov 14, 2018

@ramya-rao-a, My issue is about generic autocomplete but not modules (so no *-gomod running). I will continue this issue on other repository. For now, I will follow there.

@nezorflame
Copy link
Contributor

nezorflame commented Nov 14, 2018

@ramya-rao-a just checked.

  • inside $GOPATH with "go.toolsEnvVars": {"GO111MODULE": "on"}:
~ ps ax | grep gocode | grep -v grep
1454   ??  S      0:00.04 /Users/nezorflame/Go/bin/gocode -s -sock unix -addr 127.0.0.1:37373
  • outside $GOPATH with and without "go.toolsEnvVars": {"GO111MODULE": "on"}:
~ ps ax | grep gocode | grep -v grep
5108   ??  S      0:00.08 /Users/nezorflame/Go/bin/gocode-gomod -s -sock unix -addr 127.0.0.1:37373

Seems to me that vscode-go ignores go.toolsEnvVars setting and simply launches gocode after checking the output of go env GOMOD, instead of appending whatever env is inside of the go.toolsEnvVars, like GO111MODULE=on go env GOMOD, thus not launching the gocode-gomod.

@shaodahong
Copy link

when i run

~/go/bin
0² ./gocode close
2018/11/26 21:47:19 dial unix /var/folders/j3/1mmzgr1j51s_kgdg28ksn7b40000gn/T/gocode-daemon.shaodahong: connect: no such file or directory

@stamblerre
Copy link
Contributor

@shaodahong: that just means that your gocode server wasn't running.

@shaodahong
Copy link

@stamblerre I know when I coding in vscode , gocode start run, but autocomplete still bad, I have some question

if run gocode-gomod -s -debug a part autocomplete is good, but when I ctrl+c exit, autocomplete bad

if run gocode -s -debug, no log display

go env

0² go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/shaodahong/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/shaodahong/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/shaodahong/Workspace/demo/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/j3/1mmzgr1j51s_kgdg28ksn7b40000gn/T/go-build051668293=/tmp/go-build -gno-record-gcc-switches -fno-common"

@stamblerre
Copy link
Contributor

@shaodahong: just to clarify, are you having the same problem as @nezorflame? When you open a file in a package that uses modules, which version of gocode gets started? Does gocode-gomod close work?

@shaodahong
Copy link

@stamblerre similar, when use module autocomplete fail, version 0.7.0, mac version 10.14 (18A391)

@shaodahong
Copy link

new log

0² gocode-gomod -s -debug
2018/11/27 12:29:43 Got autocompletion request for '/Users/shaodahong/Workspace/demo/main.go'
2018/11/27 12:29:43 Cursor at: 30
2018/11/27 12:29:43 -------------------------------------------------------
package main

func main() {
	f#
}
2018/11/27 12:29:43 -------------------------------------------------------
2018/11/27 12:29:44 Elapsed duration: 195.027647ms
2018/11/27 12:29:44 Offset: 0
2018/11/27 12:29:44 Number of candidates found: 3
2018/11/27 12:29:44 Candidates are:
2018/11/27 12:29:44   const false untyped bool
2018/11/27 12:29:44   type float32 float32
2018/11/27 12:29:44   type float64 float64
2018/11/27 12:29:44 =======================================================
2018/11/27 12:29:44 Got autocompletion request for '/Users/shaodahong/Workspace/demo/main.go'
2018/11/27 12:29:44 Cursor at: 33
2018/11/27 12:29:44 -------------------------------------------------------
package main

func main() {
	fmt.#
}
2018/11/27 12:29:44 -------------------------------------------------------
2018/11/27 12:29:44 Elapsed duration: 114.33626ms
2018/11/27 12:29:44 Offset: 0
2018/11/27 12:29:44 Number of candidates found: 0
2018/11/27 12:29:44 Candidates are:
2018/11/27 12:29:44 =======================================================
2018/11/27 12:29:44 Got autocompletion request for '/Users/shaodahong/Workspace/demo/main.go'
2018/11/27 12:29:44 Cursor at: 46
2018/11/27 12:29:44 -------------------------------------------------------
package main
import "fmt"

func main() {
	fmt.#
}
2018/11/27 12:29:44 -------------------------------------------------------
2018/11/27 12:29:44 Elapsed duration: 113.933882ms
2018/11/27 12:29:44 Offset: 0
2018/11/27 12:29:44 Number of candidates found: 0
2018/11/27 12:29:44 Candidates are:
2018/11/27 12:29:44 =======================================================

@stamblerre
Copy link
Contributor

@shaodahong: what changed between this and your other comment (#2148 (comment))?

@nezorflame
Copy link
Contributor

nezorflame commented Nov 27, 2018

Gave HEAD version of gocode-gomod another go (updated with vscode-go feature Install/Update Tools).

Piece of code:

package main

type A struct {
	S string
}

func main() {
	a := A{}
}

Saved outside $GOPATH at $HOME/Go/test-project/main.go, so modules should automatically work.

Preparation:

  • gocode close
  • gocode-gomod close
  • go mod init test-project
  • gocode-gomod -s -debug

Tests:

  1. Action: Entering fm.
    Expected: autocompletion for fmt package, on acceptance fmt is imported.
    Result: works:
package main

import (
	"fmt"
)

type A struct {
	S string
}

func main() {
	a := A{}
	fmt
}
  1. Action: Entering . after fmt
    Expected: functions of fmt package
    Result: nothing:
    image
    Output: https://pastebin.com/GVCH02Em
  2. Action: Saving the file, deleting and re-entering . after fmt
    Expected: functions of fmt package should appear now
    Result: works:
    image
    Output: https://pastebin.com/PjpDU2nA
  3. Action: Entering Println(a.) after fmt., cursor at .
    Expected: struct field S should be shown in autocomplete
    Result: works:
    image
    Output: https://pastebin.com/PyRSzKPa

@nezorflame
Copy link
Contributor

nezorflame commented Nov 27, 2018

Still, there's a problem of vscode-go launching the wrong version of gocode while being inside $GOPATH with setting...

"go.toolsEnvVars": {
    "GO111MODULE": "on"
}

...being set inside 'User preferences'.
Instead of launching gocode-gomod, it launches gocode.

This doesn't happen outside of $GOPATH.

@ramya-rao-a is there any way to debug this behavior so I could help with solving this?

@shaodahong
Copy link

@stamblerre new log I delete import "fmt"

@shaodahong
Copy link

GO111MODULE i think no need set, go version 1.11 can auto judge it, when outside GOPATH and have go.mod, go module become effective

@shaodahong
Copy link

Maybe you can refer to the intellij IDEA go plugin, I don't know is the same as VScode.
image

@nezorflame
Copy link
Contributor

nezorflame commented Nov 28, 2018

@shaodahong you have to force GO111MODULE=on if you're inside $GOPATH and want to use modules (which is my current scenario and the preferred one, 'cause I like the idea of $GOPATH).

Regarding the IDEA plugin, vscode-go currently doesn't have such a setting (correct me @ramya-rao-a if I'm wrong).
AFAIK the setting I mentioned earlier is the only way to force it to use modules for Go tools.

@stamblerre
Copy link
Contributor

I found and fixed this issue with c026b8c, so if you follow the instructions to install the latest beta for VSCode-Go, you should be able to see the fix (https://github.com/Microsoft/vscode-go/wiki/Use-the-beta-version-of-the-latest-Go-extension). Otherwise, the fix will be out in the next release.

@nezorflame
Copy link
Contributor

While it definitely works now, I believe that this new release broke Go to definition for the std packages like http, io/ioutil etc.
It's possible to reference functions from them and go to their definition, but once inside, Go to definition does nothing and doesn't work at all.

@shaodahong
Copy link

oh, if you don't import net/http package, you input http. nothing happen

@stamblerre
Copy link
Contributor

@shaodahong: that is the intended behavior. mdempsky/gocode offers completions for the standard library if it's not imported, but we aren't able to do that for stamblerre/gocode.

@nezorflame: I would imagine that the definition breakage is unrelated to this bug. I know there have been some issues with godef, though I think they should be fixed soon. It might be worth checking out the issues on https://github.com/ianthehat/godef.

@shaodahong
Copy link

@stamblerre thank you

@nezorflame
Copy link
Contributor

@stamblerre agreed, thanks!

@ramya-rao-a
Copy link
Contributor Author

The issue of GO111MODULES not being respected and gocode-gomod not being launched is fixed.
But the original issue of completions not working without saving the file with the package used atleast once still exists and is being tracked in #2179 in this repo and https://github.com/stamblerre/gocode#3 in the upstream repo.

Closing this issue.

@vscodebot vscodebot bot locked and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
go-modules Related to Go modules upstream
Projects
None yet
Development

No branches or pull requests

9 participants