1

I am using Go 1.19 and VSCode 1.70.2 on Mac. My problem is that when I right click on a function name and choose "Go to Definition" or "Fn F12" it says "No definition found " I have gopls installed and here is the snippet for gopls

"go.useLanguageServer": true,
"gopls": {
    "usePlaceholders": true, // add parameter placeholders when completing a function
    "completionDocumentation": true // for documentation in completion items

},

I have restarted the VSCode after adding the above modifications. I have tried other related functions like "ctrl + click" or control + hover etc, and they also do not work.

Note: The code compiles and runs. However, Visual Code shows an error:

Error loading workspace: err: exit status 1: stderr: package unsafe is not in GOROOT

There is no reference to package "unsafe" in this workspace at all. I do use package unsafe in some other workspace.

Here is what I see in the Output window:

    [Info  - 1:13:53 PM] 2022/08/21 13:13:53 go env for /Users/path-to-project
(root /Users/path-to-project)
(go version go version go1.19 darwin/arm64)
(valid build configuration = false)
(build flags: [])
GOWORK=
GONOPROXY=
GOPROXY=https://proxy.golang.org,direct
GO111MODULE=
GOINSECURE=
GOSUMDB=sum.golang.org
GOMOD=/dev/null
GOPRIVATE=
GOMODCACHE=/Users/username/go/pkg/mod
GOFLAGS=
GOPATH=/Users/username/go
GOROOT=/Users/username/go/pkg/mod
GOCACHE=/Users/username/Library/Caches/go-build
GONOSUMDB=


[Info  - 1:13:53 PM] 2022/08/21 13:13:53 7.916792ms for GOROOT=/Users/jusername/go/pkg/mod GOPATH=/Users/username/go GO111MODULE=auto GOPROXY=https://proxy.golang.org,direct PWD=/Users/jpath-to-project go list -f "{{context.GOARCH}} {{context.Compiler}}" -- unsafe

[Info  - 1:13:53 PM] 2022/08/21 13:13:53 6.871ms for GOROOT=/Users/username/go/pkg/mod GOPATH=/Users/username/go GO111MODULE=off GOPROXY=https://proxy.golang.org,direct PWD=/Users/path-to-project go list -e -f {{context.ReleaseTags}} -- unsafe

[Info  - 1:13:53 PM] 2022/08/21 13:13:53 9.467042ms for GOROOT=/Users/username/go/pkg/mod GOPATH=/Users/username/go GO111MODULE=auto GOPROXY=https://proxy.golang.org,direct PWD=/Users/path-to-project go list -e -json=Name,ImportPath,Error,Dir,GoFiles,IgnoredGoFiles,IgnoredOtherFiles,CFiles,CgoFiles,CXXFiles,MFiles,HFiles,FFiles,SFiles,SwigFiles,SwigCXXFiles,SysoFiles,TestGoFiles,XTestGoFiles,CompiledGoFiles,Export,DepOnly,Imports,ImportMap,TestImports,XTestImports,ForTest,DepsErrors,Module -compiled=true -test=true -export=false -deps=true -find=false -- ./ builtin

[Error - 1:13:53 PM] 2022/08/21 13:13:53 go/packages.Load #1: err: exit status 1: stderr: cannot find package "unsafe" in any of:
    /Users/username/go/pkg/mod/src/unsafe (from $GOROOT)
    /Users/username/go/src/unsafe (from $GOPATH)

    snapshot=0
    directory=/Users/path-to-project
    query=[./ builtin]
    packages=0

[Error - 1:13:53 PM] 2022/08/21 13:13:53 initial workspace load failed: err: exit status 1: stderr: cannot find package "unsafe" in any of:
    /Users/username/go/pkg/mod/src/unsafe (from $GOROOT)
    /Users/username/go/src/unsafe (from $GOPATH)
: packages.Load error

[Error - 1:13:53 PM] 2022/08/21 13:13:53 errors loading workspace: err: exit status 1: stderr: cannot find package "unsafe" in any of:
    /Users/username/go/pkg/mod/src/unsafe (from $GOROOT)
    /Users/username/go/src/unsafe (from $GOPATH)
: packages.Load error
    snapshot=0
    directory=file:///Users/path-to-project

[Info  - 1:13:53 PM] 2022/08/21 13:13:53 fixImports(filename="/Users/usedrname/path-to-project/somefile.go), abs="/Users/path-to-project/somefile.go”, srcDir="/Users/path-to-project" ...


[Error - 1:13:53 PM] Request textDocument/codeAction failed.
  Message: err: exit status 1: stderr: cannot find package "unsafe" in any of:
    /Users/username/go/pkg/mod/src/unsafe (from $GOROOT)
    /Users/username/go/src/unsafe (from $GOPATH)
: packages.Load error
  Code: 0 
user2995358
  • 977
  • 11
  • 27
  • Have you worked through the [troubleshooting steps](https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md) (up to the "Ask For Help" step)? If not, please do so and let us know the results. – Brits Aug 21 '22 at 04:02
  • @Brits There is an error. I just updated the question with that. – user2995358 Aug 21 '22 at 05:36
  • Please confirm that you have worked through the troubleshooting steps in full. Without context the error is not all that useful; find it in [the logs](https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#collect-extension-logs) and include a few lines either side). My guess is that the extension is installing something that does need `unsafe`. – Brits Aug 21 '22 at 20:10
  • @Brits I added the content of the "output" window also in the questions. I also enabled gopls tracing in the settings.json, but it is essentially the same as what I have above. Thank you @Brits! – user2995358 Aug 21 '22 at 20:56
  • OK - `GOROOT=/Users/jusername/go/pkg/mod` looks wrong to me. Where is `go` installed? ([this answer](https://stackoverflow.com/a/28741724/11810946) might help). The ` Go: Locate Configured Go Tools` step in the troubleshooting will provide a lot of relevant info. – Brits Aug 21 '22 at 21:04
  • @Brits OK. I removed the goroot settings and changed the gopath to look like this: "go.gopath": "/opt/homebrew/Cellar/go/1.19/libexec/" I have installed 1.19 using homebrew. Now it is complaining about Failed to run "go env env,-json,GOMOD": warning: GOPATH set to GOROOT (/opt/homebrew/Cellar/go/1.19/libexec/) has no effect In addition to that, all the imports in my go packages are throwing up an error. – user2995358 Aug 21 '22 at 23:28
  • I believe that with brew by default `GOPATH=$HOME/go` and `GOROOT="$(brew --prefix golang)/libexec"` but I don't have access to a mac currently. – Brits Aug 22 '22 at 02:48
  • @Brits With that, I have some other error. ---------------------- Error loading workspace: You are outside of a module and outside of $GOPATH/src. If you are using modules, please open your editor to a directory in your module. If you believe this warning is incorrect, please file an issue: https://github.com/golang/go/issues/new. ----------- My code is outside of $HOME/go. I tried adding the root of the src folder to the GOPATH with ";" separator. Still the above error. – user2995358 Aug 22 '22 at 05:00
  • You should really be using go modules (see [getting started](https://go.dev/doc/tutorial/getting-started) and [go modules refence](https://go.dev/ref/mod) (if you dont want then change gopath but would not recommend that). Unfortunately this is not really the place for a long discussion; if you are now to go I'd suggest following the getting started guide and then go from there. – Brits Aug 22 '22 at 07:14
  • @Brits I am using modules. I am new to VSCode not to Go. With what I have, both Sublime and Goland (Go to definition, etc) works. There is something in VS Code set up that is not working. Thanks for your help! – user2995358 Aug 23 '22 at 17:19
  • 2
    For the benefit of anyone who face this situation in future: The issue is because the repo I was using had multiple modules and I was trying to open root folder of the repo. VSCode apparently cannot handle that. The fix is to create a workspace with a folder with a mod file and then add other folders with the mod file using the menu file -> add folder to workspace option – user2995358 Aug 29 '22 at 01:40
  • In that case [this workaround](https://github.com/golang/tools/blob/master/gopls/doc/workspace.md#go-workspaces-go-118) might also be of use. – Brits Aug 29 '22 at 03:43

0 Answers0