vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

Unable to read go.mod when GOPATH is set to a folder inside the workspace.

Open peterldowns opened this issue 3 years ago • 2 comments

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal. go version go1.18 darwin/arm64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
----------
golang.org/x/tools/gopls v0.8.1
    golang.org/x/tools/gopls@(devel)
    github.com/BurntSushi/[email protected]
    github.com/google/[email protected]
    github.com/sergi/[email protected]
    golang.org/x/[email protected]
    golang.org/x/[email protected]
    golang.org/x/[email protected]
    golang.org/x/[email protected]
    golang.org/x/[email protected]
    golang.org/x/[email protected]
    honnef.co/go/[email protected]
    mvdan.cc/[email protected]
    mvdan.cc/xurls/[email protected]
go: go1.18
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
1.65.2
c722ca6c7eed3d7987c0d5c3df5c45f6b15e77d1
arm64
  • Check your installed extensions to get the version of the VS Code Go extension v0.32.0
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
Checking configured tools....
GOBIN: undefined
toolsGopath: 
gopath: /Users/pld/code/gopls-repro-example/.go
GOROOT: /nix/store/adwrscl1v5qxbqv0l5f3wkljb0ha77mx-go-1.18/share/go
PATH: /Users/pld/code/gopls-repro-example/.go/bin:/nix/store/qc4fzcja2bs760vsqcsc6v28imjwy0wi-clang-wrapper-11.1.0/bin:/nix/store/rb559z09mrinv0w8mm1clss8s6p8k2if-clang-11.1.0/bin:/nix/store/8405gyg05szs83h883v84ds9h6x0i2ir-coreutils-9.0/bin:/nix/store/c0kfkdk2kad0dq0h981j8ffb1ngaf3y4-cctools-binutils-darwin-wrapper-949.0.1/bin:/nix/store/hwjqaci7cz4r7sxivsciil0ipfdg1nwf-cctools-binutils-darwin-949.0.1/bin:/nix/store/97mlg5qmdz36rpcz2dlc0rx79v1cbi8w-core-toolchains/bin:/nix/store/8405gyg05szs83h883v84ds9h6x0i2ir-coreutils-9.0/bin:/nix/store/b4f8lzhl4rjmssa557iz0j1l8i0mjyms-findutils-4.9.0/bin:/nix/store/kp0p49hzszmvs1lx2v6rs62aplkdxxa3-diffutils-3.8/bin:/nix/store/l8xw0jqwqrsbpzgbcmwkg58n2p1icc2s-gnused-4.8/bin:/nix/store/zdqvm614pwima18li99xrjyd82cwrq2p-gnugrep-3.7/bin:/nix/store/6w14k8a8arsizrlb8zsn9cyfhc44hh3q-gawk-5.1.1/bin:/nix/store/wkd35pvzjhb73qcv2njf524kf9ks9xki-gnutar-1.34/bin:/nix/store/r74vap0miki0j5y7d4lira1iwrjdyvw9-gzip-1.11/bin:/nix/store/raa1kmp83ry3yb184sqpyl1bk1h7bbjh-bzip2-1.0.6.0.2-bin/bin:/nix/store/1nvk71lbggjfvjh6gppb3kbkq60l88l2-gnumake-4.3/bin:/nix/store/czipazlkvql7n201a9l1jks8kzs0dlqa-bash-5.1-p16/bin:/nix/store/19kh8x7ilsh7539r8y2kc1rx99i37dk2-patch-2.7.6/bin:/nix/store/qqh82nrrhkj7gy3qqmmb2jbb961a0gxm-xz-5.2.5-bin/bin:/Users/pld/.bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/pld/.nix-profile/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin

	go:	/nix/store/97mlg5qmdz36rpcz2dlc0rx79v1cbi8w-core-toolchains/bin/go: go version go1.18 darwin/arm64

	go-outline:	/nix/store/97mlg5qmdz36rpcz2dlc0rx79v1cbi8w-core-toolchains/bin/go-outline	(version: (devel) built with go: go1.17.7)
	gotests:	not installed
	gomodifytags:	not installed
	impl:	not installed
	goplay:	not installed
	staticcheck:	not installed
	gopls:	/nix/store/97mlg5qmdz36rpcz2dlc0rx79v1cbi8w-core-toolchains/bin/gopls	(version: (devel) built with go: go1.18)

go env
Workspace Folder (gopls-repro-example): /Users/pld/code/gopls-repro-example
	GO111MODULE=""
	GOARCH="arm64"
	GOBIN=""
	GOCACHE="/Users/pld/Library/Caches/go-build"
	GOENV="/Users/pld/Library/Application Support/go/env"
	GOEXE=""
	GOEXPERIMENT=""
	GOFLAGS=""
	GOHOSTARCH="arm64"
	GOHOSTOS="darwin"
	GOINSECURE=""
	GOMODCACHE="/Users/pld/code/gopls-repro-example/.go/pkg/mod"
	GONOPROXY=""
	GONOSUMDB=""
	GOOS="darwin"
	GOPATH="/Users/pld/code/gopls-repro-example/.go"
	GOPRIVATE=""
	GOPROXY="https://proxy.golang.org,direct"
	GOROOT="/nix/store/adwrscl1v5qxbqv0l5f3wkljb0ha77mx-go-1.18/share/go"
	GOSUMDB="sum.golang.org"
	GOTMPDIR=""
	GOTOOLDIR="/nix/store/adwrscl1v5qxbqv0l5f3wkljb0ha77mx-go-1.18/share/go/pkg/tool/darwin_arm64"
	GOVCS=""
	GOVERSION="go1.18"
	GCCGO="gccgo"
	AR="ar"
	CC="clang"
	CXX="clang++"
	CGO_ENABLED="1"
	GOMOD="/dev/null"
	GOWORK="/Users/pld/code/gopls-repro-example/go.work"
	CGO_CFLAGS="-g -O2"
	CGO_CPPFLAGS=""
	CGO_CXXFLAGS="-g -O2"
	CGO_FFLAGS="-g -O2"
	CGO_LDFLAGS="-g -O2"
	PKG_CONFIG="pkg-config"
	GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/1_/ryk53sgj7jvc7l7pdrpllrqh0000gn/T/go-build2290272961=/tmp/go-build -gno-record-gcc-switches -fno-common"

Share the Go related settings you have added/edited

{
  "go.autocompleteUnimportedPackages": true,
  "go.lintOnSave": "package",
  "go.buildOnSave": "off",
  "go.vetOnSave": "off",
  "go.useLanguageServer": true,
  "go.toolsManagement.autoUpdate": false,
  "go.toolsManagement.checkForUpdates": "off",
  "gopls": {
    "formatting.gofumpt": true,
    "formatting.local": "github.com/peterldowns/gopls-repro-example",
    "build.directoryFilters": ["-", "+backend"],
    "build.experimentalWorkspaceModule": true
  },
  "[go]": {
    "editor.formatOnSave": true
  }
}

Describe the bug

When $GOPATH is set to a folder within the workspace, the Go extension throws a huge number of errors about failing to resolve a .mod file. It also attempts to do a lot of file access which means that it makes the user wait before they can do things like jump-to-definition or see their files autoformat on save. The error messages read like this:

Failed while handling 'onDidOpenTextDocument': EntryNotFound (FileSystemError): Unable to read file '/Users/pld/code/gopls-repro-example/.go/pkg/mod/go.mod' (Error: Unable to resolve nonexistent file '/Users/pld/code/gopls-repro-example/.go/pkg/mod/go.mod')
Failed while handling 'FileSystemWatcher.onDidCreate': EntryNotFound (FileSystemError): Unable to read file '/Users/pld/code/gopls-repro-example/.go/pkg/mod/go.mod' (Error: Unable to resolve nonexistent file '/Users/pld/code/gopls-repro-example/.go/pkg/mod/go.mod')

Instead, I would expect there to be no errors thrown and for all extension functionality to remain unaffected.

Steps to reproduce the behavior:

The key to triggering the issue seems to be setting $GOPATH to a folder inside the repository where VSCode is launched. I have provided an example repository with instructions in the README for how to reproduce the issue:

https://github.com/peterldowns/gopls-repro-example#vscode-go-reporting-unable-to-read-file-gomod-in-gomodcache

Screenshots or recordings

image

peterldowns avatar Mar 30 '22 16:03 peterldowns

Thanks for the thorough description of the repro case. I greatly appreciate your effort! @peterldowns

I think this error message is coming from https://github.com/golang/vscode-go/blob/78cc836ad4fc25aae2318c92249db2275334257c/src/goTest/explore.ts#L148

We should

  • [ ] stop scanning the module cache directory
  • [ ] investigate why this code was repeatedly run and why the code is looking for the non-existent go.mod file.
  • [ ] stop outputting this error message

hyangah avatar Mar 31 '22 16:03 hyangah

@hyangah glad to hear that the repro was worth the effort 🙇 I should say that we were able to work around this bug by simply moving the $GOPATH to a directory outside of our repo, so the urgency / importance is low. I just wanted to report it because the failure behavior was surprising and unexpected. Thank you again for all of your hard work and for the wonderful tools that you provide.

peterldowns avatar Apr 01 '22 16:04 peterldowns

I am running into this issue with a different scenario: We use self-contained devbox environments with Go and all Go modules and tooling in the .devbox/virtenv folder. Frequently, this folder is in a vscode workspace folder, as a result we get these same symptoms described in this issue. Adding here in case it helps inform the priority of fixing this.

stefanba3 avatar Sep 12 '23 21:09 stefanba3