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

"Add Import" and "Go to definition" do not work with builtin packages #1636

Closed
conradoplg opened this issue Apr 19, 2018 · 21 comments
Closed

Comments

@conradoplg
Copy link

"Go: Add Import" does not work with builtin Go packages, and "Go to definition" also doesn't work for builtin packages.

go version go1.10.1 linux/amd64

VS Code 1.22.2
Go plugin 0.6.78

@modyuan
Copy link

modyuan commented Apr 30, 2018

only can not add import for me

@ramya-rao-a
Copy link
Contributor

Both work fine for me.
Any chance your GOROOT hasnt been set properly?

In the integrated terminal, run echo $GOROOT and see if it gives you the location where all your builtin packages are.

@conradoplg
Copy link
Author

Setting GOROOT fixes the "Go to definition" issue, but not the "Add import" issue.

Furthermore, IMO the plugin shouldn't require GOROOT to bet set, it seems better to obtain it with go env GOROOT as described in https://dave.cheney.net/2013/06/14/you-dont-need-to-set-goroot-really

@ramya-rao-a
Copy link
Contributor

Where did you set the GOROOT?
Before you set the GOROOT, what did you see when you ran echo $GOROOT or go env GOROOT in the terminal in VS Code?

@conradoplg
Copy link
Author

conradoplg commented May 3, 2018

Before (both don't work):

  • echo $GOROOT: empty (not set)
  • go env GOROOT: /usr/lib/go-1.10

After ("Go to definition" works; "Add import" does not work)

  • echo $GOROOT: /usr/lib/go-1.10
  • go env GOROOT: /usr/lib/go-1.10

@ramya-rao-a
Copy link
Contributor

And where did you set the GOROOT?
Outside or inside VS Code?

@conradoplg
Copy link
Author

Outside; I opened VS Code with

GOROOT=/usr/lib/go-1.10 code

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented May 7, 2018

@conradoplg I've pushed a fix to read GOROOT from go env if its not already set as env var. With this, you don't have to set it specifically. To get this fix before I release an update, you can

@uudashr The Add Import gets the list of packages from when we run gopkgs. Any idea why it wouldn't fetch the built-in packages?

@uudashr
Copy link
Contributor

uudashr commented May 7, 2018

@ramya-rao-a
Somehow the go builder doesn't respect the GOROOT
https://github.com/golang/go/blob/master/src/go/build/build.go#L283-L287

@conradoplg
Base on https://golang.org/doc/install section "Installing to a custom location" you need to set the PATH also

export GOROOT=$HOME/go1.X
export PATH=$PATH:$GOROOT/bin

Now the question is "can vscode use non-standard go installation?" If it can, then probably that we should do.

I think go compiler automatically use the GOROOT as context of the "go" command, so no need to set it on env for GOROOT

@uudashr
Copy link
Contributor

uudashr commented May 7, 2018

@conradoplg @yuansushow
can you run gopkgs| head -n 10

@uudashr
Copy link
Contributor

uudashr commented May 7, 2018

I try to install golang on non-standard location

$ docker run -it --rm ubuntu

inside the docker container run

cd $HOME && apt-get update && \
    apt-get install -y --no-install-recommends curl git ca-certificates && \
    curl -C - -O -k https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz && \
    tar xzvf go1.10.2.linux-amd64.tar.gz && \
    export GOROOT=$HOME/go; export PATH=$PATH:$GOROOT/bin && \
    mkdir gopath; export GOPATH=$HOME/gopath; export PATH=$PATH; export PATH=$PATH:$GOPATH/bin && \
    go get -u github.com/uudashr/gopkgs/cmd/gopkgs && \
    echo "=== Executing gopkgs... ===" && \
    gopkgs| head -n 10

Output:

=== Executing gopkgs... ===
log
internal/testenv
mime/quotedprintable
encoding/json
net/rpc/jsonrpc
cmd/link/internal/ppc64
vendor/golang_org/x/net/nettest
cmd/vendor/github.com/google/pprof/internal/symbolizer
internal/syscall/unix
internal/syscall/windows

Make sure

  1. To set the GOROOT and expose $GOROOT/bin to the PATH
  2. You have GOPATH and expose the $GOPATH/bin to the PATH

@jlucktay
Copy link

jlucktay commented May 9, 2018

My builtin packages have been behaving strangely as well, recently.

It can see the builtin packages in the autocomplete list, like so:

image

However, when I select one of the builtins to use, it says it can't find it:

image

I have followed this wiki article and tried setting go.inferGopath to true but the behaviour is still odd.

I don't want to have to hardcode the path to my Go install, because I'm using Homebrew on MacOS and that path will shift unpredictably with future versions.

Versions:

  • Go: go version go1.10.2 darwin/amd64
  • VSCode: 1.23.0
  • Extension: 0.6.79

@ramya-rao-a
Copy link
Contributor

@jlucktay

  • That's a build error, can you share the complete error from the Output pane? View -> Output -> Select Go from the drop down on the top right corner of the Output pane.
  • What do you get when you run go env GOROOT in the integrated terminal? Does that match with the hombrwe location?
  • Are the built-in packages appearing in the list when you run the command Go: Add Import?

@jlucktay
Copy link

Hi, @ramya-rao-a and thanks for investigating further!

I removed all of my go.* specific settings from VSCode preferences, and it settled down a little bit, but still seems a little off.

  • The output from View -> Output -> Go looks like this after I open a .go file and edit and (re)save it:
/Users/jameslucktaylor/go/src/github.com/jlucktay/golang-workbench/codewars/playing-with-digits>Finished running tool: /Users/jameslucktaylor/go/bin/golint

/Users/jameslucktaylor/go/src/github.com/jlucktay/golang-workbench/codewars/playing-with-digits>Finished running tool: /usr/local/bin/go test -c -i -o /var/folders/92/8y_s2pz52wj_5byc2cvgv3w00000gp/T/go-code-check github.com/jlucktay/golang-workbench/codewars/playing-with-digits

/Users/jameslucktaylor/go/src/github.com/jlucktay/golang-workbench/codewars/playing-with-digits>Finished running tool: /usr/local/bin/go vet ./...
  • go env GOROOT in the terminal gives this:
/usr/local/Cellar/go/1.10.2/libexec

This path lines up with Homebrew. 👌

  • When I do Go: Add Import the built-in packages do not show up, yet the packages themselves no longer have the red underline, and autocomplete gives all of the methods etc when typing, e.g. math. or big.:
    image

@conradoplg
Copy link
Author

Sorry for the delay.

@ramya-rao-a yes, I confirm your change makes "Go to definition" work. Thanks!

@uudashr Adding $GOROOT/bin to $PATH did not make "Add import" work.

The output of gopkgs| head -n 10 is (some private packages were omitted):

github.com/derekparker/delve/service/rpc1
[omitted]
github.com/davidrjenni/reftools/vendor/golang.org/x/tools/benchmark/parse
github.com/davidrjenni/reftools/vendor/golang.org/x/tools/go/callgraph/rta
github.com/pkg/errors
golang.org/x/tools/godoc/vfs
github.com/davidrjenni/reftools/vendor/golang.org/x/tools/godoc/proxy
github.com/MichaelTJones/walk
golang.org/x/tools/godoc/dl
github.com/derekparker/delve/pkg/config

I think I found the issue. My "unusual" setup is the result of using the golang-backports Ubuntu ppa: https://launchpad.net/~longsleep/+archive/ubuntu/golang-backports
However, it uses symbolic links in order to follow the standard Unix folder structure:

$ ll /usr/lib/go-1.10
total 28
drwxr-xr-x   4 root root  4096 Apr 20 08:15 ./
drwxr-xr-x 184 root root 12288 Apr 20 08:15 ../
drwxr-xr-x   2 root root  4096 Apr 20 08:15 bin/
lrwxrwxrwx   1 root root    36 Feb 19 18:07 doc -> ../../share/doc/golang-1.10-doc/html/
lrwxrwxrwx   1 root root    46 Apr 17 09:56 favicon.ico.gz -> ../../share/doc/golang-1.10-doc/favicon.ico.gz
drwxr-xr-x   8 root root  4096 Apr 17 09:56 pkg/
lrwxrwxrwx   1 root root    23 Feb 19 18:07 src -> ../../share/go-1.10/src/
lrwxrwxrwx   1 root root    24 Feb 19 18:07 test -> ../../share/go-1.10/test/
-rw-r--r--   1 root root     8 Mar 29 01:28 VERSION

However, symlinks in GOROOT break some things like godoc. The Go team decided that "Symlinks in Go file trees should just be disallowed", and an issue was created in the golang-backports in order to change it to not use symlinks anymore.

gopkgs uses a Walk function which does not follow symlinks, resulting in the same issue.

So either:

  • gopkgs should follow symlinks
  • golang-backports should be fixed

While I personally think everything should work with symlinks, it seems that if the Go team gave up on that, so we can't do much about it. So I'll understand if you decide to close this issue, and I'll wait for the golang-backports people to "fix" it.

@conradoplg
Copy link
Author

I've just noticed that this is not limited to golang-backports. All Go packages in Ubuntu are like that, including default packages from LTS versions. So maybe that's an argument for changing gopkgs.

@ramya-rao-a
Copy link
Contributor

@jlucktay Can you follow the steps in #1636 (comment) and see if that fixes the issue of the built in packages not appearing when running the command Go: Add Import. Also, is your setup anyway similar to what @conradoplg describes in #1636 (comment)?

@jlucktay
Copy link

jlucktay commented May 12, 2018

@ramya-rao-a on MacOS and Windows both, installing the Go-latest.vsix file (which reports its version as 0.6.80-beta.3 within VSCode) has straightened out the Go: Add Import problem:
image

@uudashr
Copy link
Contributor

uudashr commented May 13, 2018

@conradoplg try to re-install the Go tools

go get -u github.com/uudashr/gopkgs/cmd/gopkgs

I've made some changes.. hopefully that fix the Ubuntu issues.

@conradoplg
Copy link
Author

@uudashr It works now!

Thank you and @ramya-rao-a for the excellent work 👍

@uudashr
Copy link
Contributor

uudashr commented May 14, 2018

Cool

@vscodebot vscodebot bot locked and limited conversation to collaborators Jun 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants