ATTENTION: PLEASE PROVIDE THE DETAILS REQUESTED BELOW.
Describe what you observed:
Go language server (gopls) restarts several times and crashes most of them. Occasionally it will stay alive for a few minutes (before crashing again).
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 from that window and paste it in this issue.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x110af99]
goroutine 92 [running]:
go/ast.Walk(0x1a94da0, 0xc01b8d1e80, 0x1a98f60, 0x0)
/Users/sides/code/impira/opt/go/src/go/ast/walk.go:349 +0x25f9
go/ast.Inspect(...)
/Users/sides/code/impira/opt/go/src/go/ast/walk.go:385
golang.org/x/tools/internal/lsp/cache.astCost(0x0, 0x17cb5e0)
/Users/sides/code/go_tools/pkg/mod/golang.org/x/[email protected]/internal/lsp/cache/cache.go:238 +0x97
golang.org/x/tools/internal/lsp/cache.(*Cache).PackageStats.func1(0x17f2480, 0xc0002d9150, 0x17cb420, 0xc007aaaee0)
/Users/sides/code/go_tools/pkg/mod/golang.org/x/[email protected]/internal/lsp/cache/cache.go:202 +0x21b
golang.org/x/tools/internal/memoize.(*Store).DebugOnlyIterate(0xc0002e9520, 0xc04fc485b0)
/Users/sides/code/go_tools/pkg/mod/golang.org/x/[email protected]/internal/memoize/memoize.go:184 +0x176
golang.org/x/tools/internal/lsp/cache.(*Cache).PackageStats(0xc0002e9500, 0xc037261f01, 0xc, 0x1a93600)
/Users/sides/code/go_tools/pkg/mod/golang.org/x/[email protected]/internal/lsp/cache/cache.go:175 +0x94
golang.org/x/tools/internal/lsp/debug.(*Instance).writeMemoryDebug(0xc0002fe000, 0x1, 0x1, 0x0, 0x0)
/Users/sides/code/go_tools/pkg/mod/golang.org/x/[email protected]/internal/lsp/debug/serve.go:460 +0x5df
golang.org/x/tools/internal/lsp/debug.(*Instance).MonitorMemory.func1(0xc00022cfa0, 0xc0002ca570, 0xc0002fe000, 0x1aa7e20, 0xc0002e5380)
/Users/sides/code/go_tools/pkg/mod/golang.org/x/[email protected]/internal/lsp/debug/serve.go:414 +0xb6
created by golang.org/x/tools/internal/lsp/debug.(*Instance).MonitorMemory
/Users/sides/code/go_tools/pkg/mod/golang.org/x/[email protected]/internal/lsp/debug/serve.go:406 +0x99
[Error - 8:55:20 PM] Connection to server got closed. Server will not be restarted.
Additional Information:
I believe this issue is related to PackageStats. I was poking around on the debug site mentioned here https://github.com/golang/tools/blob/master/gopls/doc/vscode.md#build-tags (localhost:6060)
When I loaded the cache tab [ http://localhost:6060/cache/1 ] (as the segfault seems to be on a cache access above):
The page had the follow text
""
Cache 1
memoize.Store entries
cache.actionHandleKey - 31
cache.packageHandleKey - 891
cache.parseKey - 4042
source.FileIdentity - 1
Per-package usage - not accurate, for guidance only
template: :7:2: executing "body" at <.PackageStats>: error calling PackageStats: runtime error: invalid memory address or nil pointer dereference
"""
There's no chance this can come from the codebase I'm working on, correct? This does not seem to be related to any changes made in our repo, and reverting pretty far back in time does not fix the issue.
I can provide more information if needed, but I would have to redact some pieces, so please let me know what is helpful! I tend to use moderately basic features (autocomplete, go to def/type/impl), so if there are portions of the Go extension that I can disable entirely, that might also serve as a good workaround. Thanks!

Thanks for the detailed report! To confirm, this is with gopls/v0.4.3, right? (gopls version to check.)
@heschik: Looks like this is crashing in the call to ast.Inspect. I guess we need a case to handle a nil file.
Yup! I've updated (and reinstalled the entire extension) several times to verify.
[Info - 9:51:06 PM] 2020/07/14 21:51:06 Build info
----------
golang.org/x/tools/gopls 0.4.3
golang.org/x/tools/[email protected] h1:irz7Q+XdHNECamFKbNWKvMV2Ak6zBbwdwbZndG4545I=
github.com/BurntSushi/[email protected] h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/sergi/[email protected] h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
golang.org/x/[email protected] h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/[email protected] h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/[email protected] h1:Vc0Vx98oU/O3+qPQ36fnTT5UduS55KLh2uSGbL7mqEo=
golang.org/x/[email protected] h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
honnef.co/go/[email protected] h1:UoveltGrhghAA7ePc+e+QYDHXrBps2PqFZiHkGR/xK8=
mvdan.cc/xurls/[email protected] h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=
Go info
-------
go version go1.13 darwin/amd64
Change https://golang.org/cl/242797 mentions this issue: internal/lsp: handle nil pointer in PackageStats
Thanks!
Should I be waiting for a minor version update to gopls? @stamblerre
You can try it out now by installing gopls at master: GO111MODULE=on go get golang.org/x/tools/gopls@master golang.org/x/tools@master. I have to admit, I am surprised you are encountering this frequently, as it's really a corner case. Let us know if the latest version fixes it.
I've been working with them for a few hours now. Seems to be working perfectly, thanks!
Most helpful comment
You can try it out now by installing
goplsat master:GO111MODULE=on go get golang.org/x/tools/gopls@master golang.org/x/tools@master. I have to admit, I am surprised you are encountering this frequently, as it's really a corner case. Let us know if the latest version fixes it.