golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.79k stars 728 forks source link

gopls: crash with test variant of command-line-arguments package #3207

Closed amirrmonfared closed 4 months ago

amirrmonfared commented 4 months ago

gopls version: v0.15.1/go1.21.6 gopls flags: update flags: proxy extension version: 0.41.1 environment: Visual Studio Code darwin initialization error: undefined issue timestamp: Thu, 29 Feb 2024 12:47:42 GMT restart history: Thu, 29 Feb 2024 09:27:35 GMT: activation (enabled: true)

ATTENTION: PLEASE PROVIDE THE DETAILS REQUESTED BELOW.

Describe what you observed.

Basically every time I do git pull or checkout this will happen

Please attach the stack trace from the crash. A window with the error message should have popped up in the lower half of your screen. Please copy the stack trace and error messages from that window and paste it in this issue.

Failed to auto-collect gopls trace: incomplete panic trace.
gopls stats -anon { "DirStats": { "Files": 11852, "TestdataFiles": 52, "GoFiles": 6693, "ModFiles": 4, "Dirs": 1641 }, "GOARCH": "arm64", "GOOS": "darwin", "GOPACKAGESDRIVER": "", "GOPLSCACHE": "", "GoVersion": "go1.21.6", "GoplsVersion": "v0.15.1", "InitialWorkspaceLoadDuration": "2.430264625s", "MemStats": { "HeapAlloc": 329135272, "HeapInUse": 401940480, "TotalAlloc": 2201666424 }, "WorkspaceStats": { "Files": { "Total": 6942, "Largest": 7361676, "Errs": 0 }, "Views": [ { "GoCommandVersion": "go1.21.6", "AllPackages": { "Packages": 1421, "LargestPackage": 214, "CompiledGoFiles": 8340, "Modules": 177 }, "WorkspacePackages": { "Packages": 593, "LargestPackage": 214, "CompiledGoFiles": 4090, "Modules": 1 }, "Diagnostics": 0 } ] } }
findleyr commented 4 months ago

Hi, thanks for this report. Can you please share the panicking stack from gopls? You can find it in the gopls (server) output window:

Screen Shot 2021-01-27 at 2 53 49 PM

amirrmonfared commented 4 months ago

Hi @findleyr, thanks for your reply. Here is the panicking stack, please let me know if you need further information.

[Info  - 3:11:50 PM] 2024/02/29 15:11:50 go/packages.Load golang/vscode-go#5: updating metadata for 9 packages

panic: nil metadata for "github.com/mailru/easyjson/easyjson [command-line-arguments.test]"

goroutine 7258 [running]:
golang.org/x/tools/gopls/internal/cache.(*Snapshot).getPackageHandles.func1(0x1400d262320, {0x14002cb9450, 0x41})
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/cache/check.go:872 +0x53c
golang.org/x/tools/gopls/internal/cache.(*Snapshot).getPackageHandles.func1(0x0, {0x14002256380, 0x6b})
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/cache/check.go:889 +0x468
golang.org/x/tools/gopls/internal/cache.(*Snapshot).getPackageHandles(0x140020b4120, {0x101718648, 0x1400c55c480}, {0x1400a6bdb08, 0x1, 0x0?})
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/cache/check.go:899 +0x17c
golang.org/x/tools/gopls/internal/cache.(*Snapshot).forEachPackage(0x140020b4120, {0x101718648, 0x1400c55c300}, {0x1400a6bdb08, 0x1, 0x1}, 0x1400d26a340, 0x1400c2a3410)
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/cache/check.go:343 +0x134
golang.org/x/tools/gopls/internal/cache.(*Snapshot).PackageDiagnostics(0x140020b4120, {0x101718680?, 0x14005765770?}, {0x1400a6bdb08, 0x1, 0x1})
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/cache/snapshot.go:686 +0x19c
golang.org/x/tools/gopls/internal/cache.(*Snapshot).orphanedFileDiagnostics(0x140020b4120, {0x101718680, 0x14005765770}, {0x1400c2a33d0, 0x2, 0x101013ad0?})
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/cache/snapshot.go:1452 +0x1c8
golang.org/x/tools/gopls/internal/cache.(*Session).OrphanedFileDiagnostics(0x140004b2540, {0x101718680, 0x14005765770})
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/cache/session.go:1165 +0x474
golang.org/x/tools/gopls/internal/server.(*server).diagnoseChangedViews(0x14000812000, {0x101718680, 0x14005765770}, 0x2, 0x14008bc3140, 0x0)
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/server/diagnostics.go:157 +0x4f0
golang.org/x/tools/gopls/internal/server.(*server).didModifyFiles.func2()
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/server/text_synchronization.go:269 +0x3c
created by golang.org/x/tools/gopls/internal/server.(*server).didModifyFiles in goroutine 137
    /Users/amirmonfared/go/pkg/mod/golang.org/x/tools/gopls@v0.15.1/internal/server/text_synchronization.go:268 +0x468
[Error - 3:11:50 PM] 
findleyr commented 4 months ago

Thanks very much and... wow. Are you by any chance working in open source, so that we could reproduce this crash in your workspace? This is a very strange failure mode (for a bit of context, somehow gopls is loading a test package for a "script").

This panic does not occur with gopls@v0.14.2, I assume? In that case, can you try adding "gopls": {"zeroConfig": false}" to your settings? Does that resolve the problem in your workspace? (this is not a long-term solution, but it would give us clues as to how the strangely loaded package is appearing)

amirrmonfared commented 4 months ago

Thanks very much and... wow. Are you by any chance working in open source, so that we could reproduce this crash in your workspace? This is a very strange failure mode (for a bit of context, somehow gopls is loading a test package for a "script").

This panic does not occur with gopls@v0.14.2, I assume? In that case, can you try adding "gopls": {"zeroConfig": false}" to your settings? Does that resolve the problem in your workspace? (this is not a long-term solution, but it would give us clues as to how the strangely loaded package is appearing)

I'm using gopls v0.41.1 and as I tested with different workspaces it seems I only have the issue with one repository only and I also set the "gopls": {"zeroConfig" but still it will panic.

I'm contributing to k8s test infra as well but I'm not sure if there are any steps that I can reproduce it on purpose. what is your suggestion to investigate more on the reason of this issue?

amirrmonfared commented 4 months ago

Update: I have tried different cases and seems this issue only happens when I open the go-test file and only in one newly created git branch and other branches are working correctly, I'm not sure where is the source of this issue but I would be happy to help on investigate more and solve it.

findleyr commented 4 months ago

Thanks @amirrmonfared. We will definitely fix this one way or another (we can always avoid the panic), but we'd really like to understand it, as it violates several core assumptions:

  1. "command-line-arguments" packages are special, reserved for one-off files outside of a module. As such, they're not importable.
  2. But the syntax of the package ID github.com/mailru/easyjson/easyjson [command-line-arguments.test] indicates that this is a copy of the easyjson import that exists to import the test variant of a command-line-arguments package. Suggesting that not only is the command-line-arguments package somehow imported, but also it has a test variant.

So this sounds like a bug in go/packages.

Is the repo that reproduces this bug open source? If not, can you say more about the diff on the branch that reproduces the bug? Are there any files with build tags ("//go:build ...")? Is there anything special, in particular, about the go test file that causes the crash? If possible, could you share just the header of that file: the package declaration and imports? (feel free to redact or obfuscate any proprietary import names).

Since this is reproducible I can also put together a patched gopls with more debugging output that can help narrow down the problem. However, today is a Google holiday so I won't be able to do that until Monday.

Interestingly, golang/go#66109 is a dupe with the exact same package ID in the panic, so it may be something special about the easyjson package that exacerbates this bug.

amirrmonfared commented 4 months ago

Thanks a lot @findleyr for your support, as it's a duplicate of https://github.com/golang/go/issues/66109 let's close it and follow up on this issue there