Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stderr: $GOPATH/go.mod exists but should not failed to load view for file:///Users/xxx

I am using vscode and vscode-go to develop the Go application.

When I try to restart language server using command + shirt + p and select "Go: Restart Language Server" option.

Got error:

Error loading workspace folders (expected 4, got 2) failed to load view for file:///Users/ldu020/workspace/github.com/mrdulin/golang: err: exit status 1: stderr: $GOPATH/go.mod exists but should not failed to load view for file:///Users/ldu020/workspace/github.com/mrdulin/graphql-go-cnode: err: exit status 1: stderr: $GOPATH/go.mod exists but should not

Environment information:

  gopls version                                                                          
golang.org/x/tools/gopls v0.3.4
    golang.org/x/tools/[email protected] h1:4GC7q/pXQ/tsxHBGVdsMdlB4gCxVC06m/7rIXg1Px4E=
  go env       
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ldu020/Library/Caches/go-build"
GOENV="/Users/ldu020/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/ldu020/workspace/go:/Users/ldu020/workspace/github.com/mrdulin/golang"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ldu020/workspace/github.com/mrdulin/graphql-go-cnode/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/38/s8g_rsm13yxd26nwyqzdp2shd351xb/T/go-build240831409=/tmp/go-build -gno-record-gcc-switches -fno-common"

Current work directory:

  pwd
/Users/ldu020/workspace/github.com/mrdulin/graphql-go-cnode

As you can see, graphql-go-cnode is a new project outside of GOPATH, and I use go mod command, there is a go.mod in the root directory of graphql-go-cnode project. For golang project, there is a go.mod file as well.

☁  golang [master] ls
LICENSE   README.md bin       go.mod    go.sum    pkg       src

Here is my vscode-go settings:

"go.languageServerFlags": [
    "-rpc.trace",
    "serve",
    "--debug=localhost:6060"
  ],
  "go.toolsGopath": "/Users/ldu020/workspace/go",
  "go.gopath": "/Users/ldu020/workspace/github.com/mrdulin/golang",
  "go.useCodeSnippetsOnFunctionSuggest": true,
  "go.useLanguageServer": true,

I follow this guide to create project outside of the GOPATH. But now, I am totally messed up. How can I fix this? Thanks.

like image 687
slideshowp2 Avatar asked Nov 16 '22 13:11

slideshowp2


1 Answers

I ended up figuring it out. You need to uncheck the "Infer GOPATH" option in the VS code Golang extension. By default, VS code will infer the working directory path as the GOPATH, and that throws an error if your repo implements Go mod.

If your repo implements Go mod, your GOPATH needs to be outside of the working directory path, mine is generally set like this: export GOPATH="$HOME/go"

Hope that helps!

like image 82
P A S H Avatar answered Dec 06 '22 02:12

P A S H