golang / vscode-go

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

gopls: nil deref in analysis (due to package cycle?) #3126

Closed ptodev closed 5 months ago

ptodev commented 6 months ago

gopls version: v0.14.2/go1.21.1 gopls flags: update flags: proxy extension version: 0.40.1 environment: Visual Studio Code darwin initialization error: undefined issue timestamp: Fri, 12 Jan 2024 19:08:52 GMT restart history: Fri, 12 Jan 2024 19:08:36 GMT: activation (enabled: true)

ATTENTION: PLEASE PROVIDE THE DETAILS REQUESTED BELOW.

Describe what you observed.

gopls crashed 3-4 times in a row while I've been working on the Prometheus codebase.

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.

Gopls output ``` [Info - 19:08:36] 2024/01/12 19:08:36 go info for /Users/paulintodev/Documents/GitHub/prometheus-paulin (go dir /Users/paulintodev/Documents/GitHub/prometheus-paulin) (go version go version go1.21.1 darwin/arm64) (valid build configuration = true) (build flags: []) (selected go env: [GO111MODULE=, GOCACHE=/Users/paulintodev/Library/Caches/go-build, GOFLAGS=, GOMODCACHE=/Users/paulintodev/go/pkg/mod, GOPATH=/Users/paulintodev/go, GOPRIVATE=, GOROOT=/opt/homebrew/Cellar/go/1.21.1/libexec, GOWORK=]) [Info - 19:08:40] 2024/01/12 19:08:40 go/packages.Load #1 snapshot=0 directory=file:///Users/paulintodev/Documents/GitHub/prometheus-paulin query=[/Users/paulintodev/Documents/GitHub/prometheus-paulin/... builtin] packages=227 [Info - 19:08:40] 2024/01/12 19:08:40 go/packages.Load #1: updating metadata for 1280 packages [Error - 19:08:40] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:40] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:40] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:40] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10079db34] goroutine 9840 [running]: golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).typeCheck(0x140013738c0, {0x140073e2580, 0x8, 0x8}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:1042 +0x954 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).run(0x140013738c0, {0x100d55f68?, 0x1400b883290}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:794 +0x1a0 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).runCached(0x140013738c0, {0x100d55f68?, 0x1400b883290}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:656 +0x104 golang.org/x/tools/gopls/internal/lsp/cache.(*snapshot).Analyze.func6.1() /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:383 +0xa4 golang.org/x/sync/errgroup.(*Group).Go.func1() /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:75 +0x58 created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 9340 /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:72 +0x98 [Info - 19:08:41] true [Info - 19:08:41] 2024/01/12 19:08:41 go info for /Users/paulintodev/Documents/GitHub/prometheus-paulin (go dir /Users/paulintodev/Documents/GitHub/prometheus-paulin) (go version go version go1.21.1 darwin/arm64) (valid build configuration = true) (build flags: []) (selected go env: [GO111MODULE=, GOCACHE=/Users/paulintodev/Library/Caches/go-build, GOFLAGS=, GOMODCACHE=/Users/paulintodev/go/pkg/mod, GOPATH=/Users/paulintodev/go, GOPRIVATE=, GOROOT=/opt/homebrew/Cellar/go/1.21.1/libexec, GOWORK=]) [Info - 19:08:43] 2024/01/12 19:08:43 go/packages.Load #1 snapshot=0 directory=file:///Users/paulintodev/Documents/GitHub/prometheus-paulin query=[/Users/paulintodev/Documents/GitHub/prometheus-paulin/... builtin] packages=227 [Info - 19:08:43] 2024/01/12 19:08:43 go/packages.Load #1: updating metadata for 1280 packages [Error - 19:08:43] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:43] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:43] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:43] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x102799b34] goroutine 10112 [running]: golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).typeCheck(0x140014b2a50, {0x140041eb240, 0x8, 0x8}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:1042 +0x954 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).run(0x140014b2a50, {0x102d51f68?, 0x1400b863800}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:794 +0x1a0 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).runCached(0x140014b2a50, {0x102d51f68?, 0x1400b863800}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:656 +0x104 golang.org/x/tools/gopls/internal/lsp/cache.(*snapshot).Analyze.func6.1() /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:383 +0xa4 golang.org/x/sync/errgroup.(*Group).Go.func1() /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:75 +0x58 created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 9639 /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:72 +0x98 [Info - 19:08:44] true [Info - 19:08:44] 2024/01/12 19:08:44 go info for /Users/paulintodev/Documents/GitHub/prometheus-paulin (go dir /Users/paulintodev/Documents/GitHub/prometheus-paulin) (go version go version go1.21.1 darwin/arm64) (valid build configuration = true) (build flags: []) (selected go env: [GO111MODULE=, GOCACHE=/Users/paulintodev/Library/Caches/go-build, GOFLAGS=, GOMODCACHE=/Users/paulintodev/go/pkg/mod, GOPATH=/Users/paulintodev/go, GOPRIVATE=, GOROOT=/opt/homebrew/Cellar/go/1.21.1/libexec, GOWORK=]) [Info - 19:08:45] 2024/01/12 19:08:45 go/packages.Load #1 snapshot=0 directory=file:///Users/paulintodev/Documents/GitHub/prometheus-paulin query=[/Users/paulintodev/Documents/GitHub/prometheus-paulin/... builtin] packages=227 [Info - 19:08:45] 2024/01/12 19:08:45 go/packages.Load #1: updating metadata for 1280 packages [Error - 19:08:46] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:46] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:46] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:46] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x103785b34] goroutine 9703 [running]: golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).typeCheck(0x1400754a630, {0x14007f02280, 0x8, 0x8}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:1042 +0x954 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).run(0x1400754a630, {0x103d3df68?, 0x1400adb5380}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:794 +0x1a0 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).runCached(0x1400754a630, {0x103d3df68?, 0x1400adb5380}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:656 +0x104 golang.org/x/tools/gopls/internal/lsp/cache.(*snapshot).Analyze.func6.1() /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:383 +0xa4 golang.org/x/sync/errgroup.(*Group).Go.func1() /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:75 +0x58 created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 9582 /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:72 +0x98 [Info - 19:08:47] true [Info - 19:08:47] 2024/01/12 19:08:47 go info for /Users/paulintodev/Documents/GitHub/prometheus-paulin (go dir /Users/paulintodev/Documents/GitHub/prometheus-paulin) (go version go version go1.21.1 darwin/arm64) (valid build configuration = true) (build flags: []) (selected go env: [GO111MODULE=, GOCACHE=/Users/paulintodev/Library/Caches/go-build, GOFLAGS=, GOMODCACHE=/Users/paulintodev/go/pkg/mod, GOPATH=/Users/paulintodev/go, GOPRIVATE=, GOROOT=/opt/homebrew/Cellar/go/1.21.1/libexec, GOWORK=]) [Info - 19:08:48] 2024/01/12 19:08:48 go/packages.Load #1 snapshot=0 directory=file:///Users/paulintodev/Documents/GitHub/prometheus-paulin query=[/Users/paulintodev/Documents/GitHub/prometheus-paulin/... builtin] packages=227 [Info - 19:08:48] 2024/01/12 19:08:48 go/packages.Load #1: updating metadata for 1280 packages [Error - 19:08:49] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:49] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:49] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:49] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:49] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x1034c1b34] goroutine 9610 [running]: golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).typeCheck(0x140007c06e0, {0x1400d8f3d80, 0x8, 0x8}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:1042 +0x954 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).run(0x140007c06e0, {0x103a79f68?, 0x1400c058de0}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:794 +0x1a0 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).runCached(0x140007c06e0, {0x103a79f68?, 0x1400c058de0}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:656 +0x104 golang.org/x/tools/gopls/internal/lsp/cache.(*snapshot).Analyze.func6.1() /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:383 +0xa4 golang.org/x/sync/errgroup.(*Group).Go.func1() /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:75 +0x58 created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 9609 /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:72 +0x98 [Info - 19:08:50] true [Info - 19:08:50] 2024/01/12 19:08:50 go info for /Users/paulintodev/Documents/GitHub/prometheus-paulin (go dir /Users/paulintodev/Documents/GitHub/prometheus-paulin) (go version go version go1.21.1 darwin/arm64) (valid build configuration = true) (build flags: []) (selected go env: [GO111MODULE=, GOCACHE=/Users/paulintodev/Library/Caches/go-build, GOFLAGS=, GOMODCACHE=/Users/paulintodev/go/pkg/mod, GOPATH=/Users/paulintodev/go, GOPRIVATE=, GOROOT=/opt/homebrew/Cellar/go/1.21.1/libexec, GOWORK=]) [Info - 19:08:51] 2024/01/12 19:08:51 go/packages.Load #1 snapshot=0 directory=file:///Users/paulintodev/Documents/GitHub/prometheus-paulin query=[/Users/paulintodev/Documents/GitHub/prometheus-paulin/... builtin] packages=227 [Info - 19:08:51] 2024/01/12 19:08:51 go/packages.Load #1: updating metadata for 1280 packages [Error - 19:08:51] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:51] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:51] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:51] Request textDocument/semanticTokens/full failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:51] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:51] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 [Error - 19:08:51] Request textDocument/semanticTokens/range failed. Message: semantictokens are disabled Code: 0 panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x105001b34] goroutine 9597 [running]: golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).typeCheck(0x1400271a000, {0x1400d28fbc0, 0x8, 0x8}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:1042 +0x954 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).run(0x1400271a000, {0x1055b9f68?, 0x1400a714e10}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:794 +0x1a0 golang.org/x/tools/gopls/internal/lsp/cache.(*analysisNode).runCached(0x1400271a000, {0x1055b9f68?, 0x1400a714e10}) /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:656 +0x104 golang.org/x/tools/gopls/internal/lsp/cache.(*snapshot).Analyze.func6.1() /Users/paulintodev/go/pkg/mod/golang.org/x/tools/gopls@v0.14.2/internal/lsp/cache/analysis.go:383 +0xa4 golang.org/x/sync/errgroup.(*Group).Go.func1() /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:75 +0x58 created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 9535 /Users/paulintodev/go/pkg/mod/golang.org/x/sync@v0.4.0/errgroup/errgroup.go:72 +0x98 [Error - 19:08:52] ```

Failed to auto-collect gopls trace: incomplete panic trace.

gopls stats -anon { "DirStats": { "Files": 48021, "TestdataFiles": 262, "GoFiles": 507, "ModFiles": 2, "Dirs": 6042 }, "GOARCH": "arm64", "GOOS": "darwin", "GoVersion": "go1.21.1", "GoplsVersion": "v0.14.2", "InitialWorkspaceLoadDuration": "2.184998625s", "MemStats": { "HeapAlloc": 180347224, "HeapInUse": 238641152, "TotalAlloc": 1021183800 }, "WorkspaceStats": { "Files": { "Total": 7064, "Largest": 9194290, "Errs": 0 }, "Views": [ { "GoCommandVersion": "go1.21.1", "AllPackages": { "Packages": 1280, "LargestPackage": 218, "CompiledGoFiles": 7342, "Modules": 173 }, "WorkspacePackages": { "Packages": 160, "LargestPackage": 35, "CompiledGoFiles": 708, "Modules": 1 }, "Diagnostics": 149 } ] } }
hyangah commented 6 months ago

Thanks for the report and providing the missing trace! @ptodev Can you try gopls@v0.15.0-pre.1 to check if the problem still exists?

go install golang.org/x/tools/gopls@v0.15.0-pre.1

Looks very similar to https://github.com/golang/go/issues/64227 which is under investigation. cc @adonovan

Is this one what you mean by Prometheus repo? https://github.com/prometheus/prometheus

adonovan commented 6 months ago

I agree, it's almost certainly a dup of https://github.com/golang/go/issues/64227.

ptodev commented 6 months ago

Thank you for responding!

Thanks for the report and providing the missing trace! @ptodev Can you try gopls@v0.15.0-pre.1 to check if the problem still exists?

I could try this out next week.

Is this one what you mean by Prometheus repo? https://github.com/prometheus/prometheus

Yes, but I am working on a branch with quite a few changes in it. I realised that there was a cyclic dependency, and once I removed it, gopls no longer crashed.

hyangah commented 6 months ago

Thanks @ptodev

I realised that there was a cyclic dependency, and once I removed it, gopls no longer crashed.

This is a very valuable observation. Is it possible to share how you created the cyclic dependency? That will be super helpful. We have some testing coverages around cyclic dependencies, but there must be more cases we need to test cases to reproduce the crash.

ptodev commented 6 months ago

I saved the branch at the time of the crash here: https://github.com/ptodev/prometheus/tree/gopls-crash

Unfortunately, since the time that I reported the problem I have not been able to reproduce the crash. This is odd, because back when it happened I tried restarting my Visual Studio Code several times. It'd crash after a few seconds after each restart, every time. I used the compiler to find the cyclic dependency. Once I resolved it, the crash no longer happened.

The gopls-crash branch that I linked to above has the same code as the one I used at the time. Before I fixed the dependency, I copied my repo folder on the side, and later I pushed the code to this new gopls-crash branch in my prometheus fork.

findleyr commented 6 months ago

Thank you for saving this state, and for investigating. I also can't yet reproduce in that branch. Since this happens in analysis, it does depend on which files were open (we only analyze open packages).

It's helpful to know that it was reproducible after a restart, as that helps rule out corrupted state resulting from state transitions (e.g. reloading a package).

findleyr commented 6 months ago

@ptodev do you happen to be using a custom go/packages driver? (I expect not, but thought I'd ask).

ptodev commented 6 months ago

do you happen to be using a custom go/packages driver?

I don't think so. I didn't even know about this feature until now. It seems to be configured using a GOPACKAGESDRIVER env var. I tried to echo $GOPACKAGESDRIVER from within a VS Code terminal and got an empty string.

gopherbot commented 5 months ago

Change https://go.dev/cl/560463 mentions this issue: gopls/internal/cache/metadata: assert graph is acyclic

findleyr commented 5 months ago

Closing as a dupe of https://github.com/golang/go/issues/64227. We've added some additional instrumentation to debug this failure mode.