golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.94k stars 17.53k forks source link

x/tools/gopls: validate that user build flags are actually flags #58023

Open ChristoWolf opened 1 year ago

ChristoWolf commented 1 year ago

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.19.5 windows/amd64 * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. - golang.org/x/tools/gopls v0.11.0 * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. - 1.74.3 97dec172d3256f8ca4bfb2143f3f76b503ca0534 x64 * Check your installed extensions to get the version of the VS Code Go extension - v0.37.1 * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. - ``` Workspace Folder (devTools): c:\dev\devTools set GO111MODULE= set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\cwolf\AppData\Local\go-build set GOENV=C:\Users\cwolf\AppData\Roaming\go\env set GOEXE=.exe set GOEXPERIMENT= set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=C:\Users\cwolf\go\pkg\mod set GONOPROXY=redacted set GONOSUMDB=redacted set GOOS=windows set GOPATH=C:\Users\cwolf\go set GOPRIVATE=redacted set GOPROXY=https://proxy.golang.org,direct set GOROOT=C:\Program Files\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64 set GOVCS= set GOVERSION=go1.19.5 set GCCGO=gccgo set GOAMD64=v1 set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=NUL set GOWORK=c:\dev\devTools\go.work set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\cwolf\AppData\Local\Temp\go-build1196747904=/tmp/go-build -gno-record-gcc-switches ``` I also had `GOWORK=` before, but that led to the same issue.

Share the Go related settings you have added/edited

"go.lintOnSave": "file",
"go.buildOnSave": "off",
"go.toolsManagement.autoUpdate": true,
"go.formatTool": "goimports",
"go.inlayHints.constantValues": true,
"go.testFlags": [
    "-v",
    "-timeout",
    "5m",
    // "-race"
],
"go.coverageDecorator": {
    "type": "highlight",
    "coveredHighlightColor": "rgba(64,128,128,0.5)",
    "uncoveredHighlightColor": "rgba(128,64,64,0.25)",
    "coveredGutterStyle": "blockgreen",
    "uncoveredGutterStyle": "blockred"
},
"go.coverMode": "atomic",
"go.languageServerExperimentalFeatures": {
    "diagnostics": false
},
"gopls": {
    "build.buildFlags": [ "integration" ]
},

Describe the bug

A clear and concise description of what the bug is:

In my workspace folder I have a go.work file containing my relative module paths in that folder. Still, I get the following persistent error notification:

Error loading workspace: packages.Load error: err: exit status 1: stderr: cannot find package "integration" in any of:  C:\Program Files\Go\src\integration (from $GOROOT)  C:\Users\cwolf\go\src\integration (from $GOPATH) cannot find package "-e" in any of:    C:\Program Files\Go\src\-e (from $GOROOT)   C:\Users\cwolf\go\src\-e (from $GOPATH) cannot find package "-f" in any of:     C:\Program Files\Go\src\-f (from $GOROOT)   C:\Users\cwolf\go\src\-f (from $GOPATH) cannot find package "{{context.ReleaseTags}}" in any of:    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)  C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH) cannot find package "--" in any of:    C:\Program Files\Go\src\-- (from $GOROOT)   C:\Users\cwolf\go\src\-- (from $GOPATH) 

I have no idea why these are being probed, because they do not exist at all and are not referenced anywhere. The ones with hyphens do not even make sense of course. The integration is also not a package but a go:build flag that I have at the top of a test file, which works as expected.

Further, when I set

"go.languageServerExperimentalFeatures": {
    "diagnostics": true
},

all modules inside the workspace folder are marked as red because the error above is shown inside the mod files, and the notification still persists.

[Info  - 9:37:06 AM] 2023/01/26 09:37:05 go env for C:\Users\cwolf\Desktop
(root C:\Users\cwolf\Desktop)
(go version go version go1.19.5 windows/amd64)
(valid build configuration = false)
(build flags: [integration])
GOROOT=C:\Program Files\Go
GOINSECURE=
GOPATH=C:\Users\cwolf\go
GOPROXY=https://proxy.golang.org,direct
GOCACHE=C:\Users\cwolf\AppData\Local\go-build
GOPRIVATE=redacted
GO111MODULE=
GOFLAGS=
GOMODCACHE=C:\Users\cwolf\go\pkg\mod
GOSUMDB=sum.golang.org
GONOSUMDB=redacted
GOMOD=NUL
GOWORK=
GONOPROXY=redacted

[Error - 9:37:06 AM] 2023/01/26 09:37:05 initial workspace load failed: packages.Load error: err: exit status 1: stderr: cannot find package "integration" in any of:
    C:\Program Files\Go\src\integration (from $GOROOT)
    C:\Users\cwolf\go\src\integration (from $GOPATH)
cannot find package "-e" in any of:
    C:\Program Files\Go\src\-e (from $GOROOT)
    C:\Users\cwolf\go\src\-e (from $GOPATH)
cannot find package "-f" in any of:
    C:\Program Files\Go\src\-f (from $GOROOT)
    C:\Users\cwolf\go\src\-f (from $GOPATH)
cannot find package "{{context.ReleaseTags}}" in any of:
    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)
    C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH)
cannot find package "--" in any of:
    C:\Program Files\Go\src\-- (from $GOROOT)
    C:\Users\cwolf\go\src\-- (from $GOPATH)

[Error - 9:37:06 AM] 2023/01/26 09:37:05 go/packages.Load golang/vscode-go#1: err: exit status 1: stderr: cannot find package "integration" in any of:
    C:\Program Files\Go\src\integration (from $GOROOT)
    C:\Users\cwolf\go\src\integration (from $GOPATH)
cannot find package "-e" in any of:
    C:\Program Files\Go\src\-e (from $GOROOT)
    C:\Users\cwolf\go\src\-e (from $GOPATH)
cannot find package "-f" in any of:
    C:\Program Files\Go\src\-f (from $GOROOT)
    C:\Users\cwolf\go\src\-f (from $GOPATH)
cannot find package "{{context.ReleaseTags}}" in any of:
    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)
    C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH)
cannot find package "--" in any of:
    C:\Program Files\Go\src\-- (from $GOROOT)
    C:\Users\cwolf\go\src\-- (from $GOPATH)

    snapshot=0
    directory=file:///C:/Users/cwolf/Desktop
    query=[./ builtin]
    packages=0

[Info  - 9:37:07 AM] 2023/01/26 09:37:07 go env for C:\dev\devTools
(root C:\dev\devTools)
(go version go version go1.19.5 windows/amd64)
(valid build configuration = true)
(build flags: [integration])
GOPROXY=https://proxy.golang.org,direct
GOROOT=C:\Program Files\Go
GOSUMDB=sum.golang.org
GOFLAGS=
GOPRIVATE=redacted
GOMOD=NUL
GONOPROXY=redacted
GONOSUMDB=redacted
GOPATH=C:\Users\cwolf\go
GOWORK=C:\dev\devTools\go.work
GOCACHE=C:\Users\cwolf\AppData\Local\go-build
GOINSECURE=
GO111MODULE=
GOMODCACHE=C:\Users\cwolf\go\pkg\mod

The gopls (server) OUTPUT shows:

[Error - 9:37:07 AM] 2023/01/26 09:37:07 go/packages.Load golang/vscode-go#2: err: exit status 1: stderr: cannot find package "integration" in any of:
    C:\Program Files\Go\src\integration (from $GOROOT)
    C:\Users\cwolf\go\src\integration (from $GOPATH)
cannot find package "-e" in any of:
    C:\Program Files\Go\src\-e (from $GOROOT)
    C:\Users\cwolf\go\src\-e (from $GOPATH)
cannot find package "-f" in any of:
    C:\Program Files\Go\src\-f (from $GOROOT)
    C:\Users\cwolf\go\src\-f (from $GOPATH)
cannot find package "{{context.ReleaseTags}}" in any of:
    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)
    C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH)
cannot find package "--" in any of:
    C:\Program Files\Go\src\-- (from $GOROOT)
    C:\Users\cwolf\go\src\-- (from $GOPATH)

    snapshot=0
    directory=file:///C:/dev/devTools
    query=[builtin redacted/... redacted/... redacted/...]
    packages=0

[Error - 9:37:07 AM] 2023/01/26 09:37:07 initial workspace load failed: packages.Load error: err: exit status 1: stderr: cannot find package "integration" in any of:
    C:\Program Files\Go\src\integration (from $GOROOT)
    C:\Users\cwolf\go\src\integration (from $GOPATH)
cannot find package "-e" in any of:
    C:\Program Files\Go\src\-e (from $GOROOT)
    C:\Users\cwolf\go\src\-e (from $GOPATH)
cannot find package "-f" in any of:
    C:\Program Files\Go\src\-f (from $GOROOT)
    C:\Users\cwolf\go\src\-f (from $GOPATH)
cannot find package "{{context.ReleaseTags}}" in any of:
    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)
    C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH)
cannot find package "--" in any of:
    C:\Program Files\Go\src\-- (from $GOROOT)
    C:\Users\cwolf\go\src\-- (from $GOPATH)

[Error - 9:37:07 AM] 2023/01/26 09:37:07 workspace packages: diagnosing file:///C:/dev/devTools/redacted/go.mod: packages.Load error: err: exit status 1: stderr: cannot find package "integration" in any of:
    C:\Program Files\Go\src\integration (from $GOROOT)
    C:\Users\cwolf\go\src\integration (from $GOPATH)
cannot find package "-e" in any of:
    C:\Program Files\Go\src\-e (from $GOROOT)
    C:\Users\cwolf\go\src\-e (from $GOPATH)
cannot find package "-f" in any of:
    C:\Program Files\Go\src\-f (from $GOROOT)
    C:\Users\cwolf\go\src\-f (from $GOPATH)
cannot find package "{{context.ReleaseTags}}" in any of:
    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)
    C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH)
cannot find package "--" in any of:
    C:\Program Files\Go\src\-- (from $GOROOT)
    C:\Users\cwolf\go\src\-- (from $GOPATH)

[Error - 9:37:07 AM] 2023/01/26 09:37:07 workspace packages: diagnosing file:///C:/dev/devTools/redactedr/go.mod: packages.Load error: err: exit status 1: stderr: cannot find package "integration" in any of:
    C:\Program Files\Go\src\integration (from $GOROOT)
    C:\Users\cwolf\go\src\integration (from $GOPATH)
cannot find package "-e" in any of:
    C:\Program Files\Go\src\-e (from $GOROOT)
    C:\Users\cwolf\go\src\-e (from $GOPATH)
cannot find package "-f" in any of:
    C:\Program Files\Go\src\-f (from $GOROOT)
    C:\Users\cwolf\go\src\-f (from $GOPATH)
cannot find package "{{context.ReleaseTags}}" in any of:
    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)
    C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH)
cannot find package "--" in any of:
    C:\Program Files\Go\src\-- (from $GOROOT)
    C:\Users\cwolf\go\src\-- (from $GOPATH)

[Error - 9:37:07 AM] 2023/01/26 09:37:07 workspace packages: diagnosing file:///C:/dev/devTools/redacted/go.mod: packages.Load error: err: exit status 1: stderr: cannot find package "integration" in any of:
    C:\Program Files\Go\src\integration (from $GOROOT)
    C:\Users\cwolf\go\src\integration (from $GOPATH)
cannot find package "-e" in any of:
    C:\Program Files\Go\src\-e (from $GOROOT)
    C:\Users\cwolf\go\src\-e (from $GOPATH)
cannot find package "-f" in any of:
    C:\Program Files\Go\src\-f (from $GOROOT)
    C:\Users\cwolf\go\src\-f (from $GOPATH)
cannot find package "{{context.ReleaseTags}}" in any of:
    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)
    C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH)
cannot find package "--" in any of:
    C:\Program Files\Go\src\-- (from $GOROOT)
    C:\Users\cwolf\go\src\-- (from $GOPATH)

[Error - 9:37:07 AM] 2023/01/26 09:37:07 errors loading workspace: packages.Load error: err: exit status 1: stderr: cannot find package "integration" in any of:
    C:\Program Files\Go\src\integration (from $GOROOT)
    C:\Users\cwolf\go\src\integration (from $GOPATH)
cannot find package "-e" in any of:
    C:\Program Files\Go\src\-e (from $GOROOT)
    C:\Users\cwolf\go\src\-e (from $GOPATH)
cannot find package "-f" in any of:
    C:\Program Files\Go\src\-f (from $GOROOT)
    C:\Users\cwolf\go\src\-f (from $GOPATH)
cannot find package "{{context.ReleaseTags}}" in any of:
    C:\Program Files\Go\src\{{context.ReleaseTags}} (from $GOROOT)
    C:\Users\cwolf\go\src\{{context.ReleaseTags}} (from $GOPATH)
cannot find package "--" in any of:
    C:\Program Files\Go\src\-- (from $GOROOT)
    C:\Users\cwolf\go\src\-- (from $GOPATH)

    snapshot=0
    directory=file:///C:/dev/devTools

Either way, everything builds and runs as expected despite these issues.

A clear and concise description of what you expected to happen.

That no non-existent packages/modules are tried to be loaded.

Steps to reproduce the behavior:

  1. Open VS Code
  2. The loading notification in the status bar appears and after a short time shows the error notification.
  3. Open the gopls (server) OUTPUT.
  4. Its error output as dumped above is shown.
findleyr commented 1 year ago

I think the problem is your "buildFlags" setting. It looks like you are trying to set the "integration" build tag, but in that case it should be "-tags=integration".

It looks like gopls is passing the build flags unmodified to the go command, and that's why "-e" (which is probably passed after build tags) is interpreted as a non-flag argument.

This is messy, and we should validate build flags to produce a better error.

ChristoWolf commented 1 year ago

@findleyr: Thanks for the quick reply!

That was of course also my first thought, but when I first encountered the issue I tried to disable the buildFlags, but it did not fix the issue. Now with your reply I tried it out again and also restarted VS Code (and by that also gopls) and it worked. So that was just a false positive from my side. Anyway, your answer helped me resolve my issue, thanks a ton!

This is messy, and we should validate build flags to produce a better error.

I think that would be quite helpful! One could also argue that the current error message is already telling (I mean, it was, because we both already knew what the cause could be), but improving it would certainly improve the experience.