标准错误:$GOPATH/go.mod 存在,但应该不会无法加载 file:///Users/xxx 的视图

sli*_*wp2 5 go visual-studio-code

我正在使用 vscode 和vscode-go来开发Go应用程序。

当我试图重新启动服务器语言使用command+ shirt+ p,并选择“转到:重启语言服务器”选项。

有错误:

加载工作区文件夹时出错(预期为 4,得到 2)未能加载文件的视图:///Users/ldu020/workspace/github.com/mrdulin/golang:err:退出状态 1:stderr:$GOPATH/go.mod 存在但不应未能加载文件的视图:///Users/ldu020/workspace/github.com/mrdulin/graphql-go-cnode: err: exit status 1: stderr: $GOPATH/go.mod 存在但不应该

环境信息:

  gopls version                                                                          
golang.org/x/tools/gopls v0.3.4
    golang.org/x/tools/gopls@v0.3.4 h1:4GC7q/pXQ/tsxHBGVdsMdlB4gCxVC06m/7rIXg1Px4E=
Run Code Online (Sandbox Code Playgroud)
  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"
Run Code Online (Sandbox Code Playgroud)

当前工作目录:

  pwd
/Users/ldu020/workspace/github.com/mrdulin/graphql-go-cnode
Run Code Online (Sandbox Code Playgroud)

可以看到,graphql-go-cnode是在GOPATH之外新建了一个项目,我用的go mod命令,go.modgraphql-go-cnode项目的根目录下有一个。对于golang项目,也有一个go.mod文件。

?  golang [master] ls
LICENSE   README.md bin       go.mod    go.sum    pkg       src
Run Code Online (Sandbox Code Playgroud)

这是我的vscode-go设置:

"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,
Run Code Online (Sandbox Code Playgroud)

我按照本指南在 GOPATH 之外创建项目。但现在,我完全搞砸了。我怎样才能解决这个问题?谢谢。

P A*_*S H 1

我最终弄清楚了。您需要uncheck the "Infer GOPATH"在 VS 代码中选择 Golang 扩展。默认情况下,VS code 会将工作目录路径推断为 GOPATH,如果您的存储库实现了 Go mod,则会抛出错误。

如果你的仓库实现了 Go mod,你的 GOPATH 需要位于工作目录路径之外,我的通常设置如下: export GOPATH="$HOME/go"

希望有帮助!