Repository: go
Issue: x/tools/gopls: panic in analysis
Labels: NeedsInvestigation,gopls,Tools

<!--
Please answer these questions before submitting your issue. Thanks!
-->

### gopls version


Build info
----------
golang.org/x/tools/gopls v0.12.0
    golang.org/x/tools/gopls@v0.12.0 h1:O8KGdGuvu3YmAwim3KYmV2l20Cce5VMS74LayO2cDw8=
    github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/exp@v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
    golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
    golang.org/x/mod@v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
    golang.org/x/sync@v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
    golang.org/x/sys@v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
    golang.org/x/text@v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
    golang.org/x/tools@v0.9.2-0.20230524171317-188b785caf35 h1:B5e02cBXQWRAs5SloauCQ9RXMKeqz96eIBJ3W9Ofy+U=
    golang.org/x/vuln@v0.0.0-20230110180137-6ad3e3d07815 h1:A9kONVi4+AnuOr1dopsibH6hLi1Huy54cbeJxnq4vmU=
    honnef.co/go/tools@v0.4.2 h1:6qXr+R5w+ktL5UkwEbPp+fEvfyoMPche6GkOpGHZcLc=
    mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
    mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.19.2



### go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kaurkerdevourer/Library/Caches/go-build"
GOENV="/Users/kaurkerdevourer/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/kaurkerdevourer/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/kaurkerdevourer"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.19.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/kaurkerdevourer/Desktop/git/repo/go.mod"
GOWORK=""
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 x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gn/x8n96lv93171yc5pcv09r02w0000gn/T/go-build509356644=/tmp/go-build -gno-record-gcc-switches -fno-common"


### What did you do?

Tried to open VSCode



### What did you expect to see?
Normal working VSCode


### What did you see instead?
Nothing works, can't run tests, can't go to definition. In console I can see
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x58 pc=0x1007e49f1]

goroutine 111 [running]:
golang.org/x/tools/gopls/internal/lsp/cache.(*snapshot).Analyze(0xc0006a6000, {0x100d6a3c0, 0xc00045a870}, {0xc0004bec00, 0x3f}, {0xc001cdf000, 0xc9, 0x100819dc5?})
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools/gopls@v0.12.0/internal/lsp/cache/analysis.go:229 +0x491
golang.org/x/tools/gopls/internal/lsp/source.Analyze({0x100d6a3c0, 0xc00045a870}, {0x100d76cd8, 0xc0006a6000}, {0xc0004bec00, 0x3f}, 0x1)
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools/gopls@v0.12.0/internal/lsp/source/diagnostics.go:48 +0x264
golang.org/x/tools/gopls/internal/lsp.(*Server).codeAction(0x10095a8e0?, {0x100d6a318?, 0xc0001f2540?}, 0xc0002d6700)
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools/gopls@v0.12.0/internal/lsp/code_action.go:206 +0x16b8
golang.org/x/tools/gopls/internal/lsp.(*Server).CodeAction(0xc000346000?, {0x100d6a318?, 0xc0001f2540?}, 0x10095a8e0?)
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools/gopls@v0.12.0/internal/lsp/server_gen.go:16 +0x25
golang.org/x/tools/gopls/internal/lsp/protocol.serverDispatch({0x100d6a318, 0xc0001f2540}, {0x100d7a110, 0xc0001e64e0}, 0xc00045a330, {0x100d6a5f0, 0xc0001f2440})
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools/gopls@v0.12.0/internal/lsp/protocol/tsserver.go:237 +0x1f57
golang.org/x/tools/gopls/internal/lsp/protocol.ServerHandler.func1({0x100d6a318, 0xc0001f2540}, 0xc00045a330, {0x100d6a5f0, 0xc0001f2440})
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools/gopls@v0.12.0/internal/lsp/protocol/protocol.go:157 +0x90
golang.org/x/tools/gopls/internal/lsp/lsprpc.handshaker.func1({0x100d6a318, 0xc0001f2540}, 0xc00045a330, {0x100d6a5f0?, 0xc0001f2440?})
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools/gopls@v0.12.0/internal/lsp/lsprpc/lsprpc.go:519 +0xa39
golang.org/x/tools/internal/jsonrpc2.MustReplyHandler.func1({0x100d6a318, 0xc0001f2540}, 0xc000216e70, {0x100d6a5f0?, 0xc0001f2440?})
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools@v0.9.2-0.20230524171317-188b785caf35/internal/jsonrpc2/handler.go:35 +0xf6
golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1.2()
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools@v0.9.2-0.20230524171317-188b785caf35/internal/jsonrpc2/handler.go:103 +0xa3
created by golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1
	/Users/kaurkerdevourer/go/pkg/mod/golang.org/x/tools@v0.9.2-0.20230524171317-188b785caf35/internal/jsonrpc2/handler.go:100 +0x20a


### Editor and settings

<!--
Your editor and any settings you have configured (for example, your VSCode settings.json file)
-->
VSCODE
{
    "security.workspace.trust.untrustedFiles": "open",
    "files.autoSave": "afterDelay",
    "editor.formatOnSave": false,
    "[go]": {
        "editor.formatOnSave": false
    },
    "[python]": {
        "editor.formatOnType": true
    },
    "go.toolsManagement.autoUpdate": false,
    "dev.containers.gitCredentialHelperConfigLocation": "none",
    "dev.containers.copyGitConfig": false,
    "cmake.configureOnOpen": true,
    "cmake.buildDirectory": "/tmp/vscode-build",
    "C_Cpp.intelliSenseEngine": "disabled",
    "window.zoomLevel": -1,
    "gopls": {
        
    },
}


### Logs

<!--
If possible please include gopls logs. Instructions for capturing them can be found here:
https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md#capture-logs
-->
https://cs.opensource.google/go/x/tools/+/refs/tags/gopls/v0.12.0:gopls/internal/lsp/cache/analysis.go;l=228
"result" can be nil, so .Err will result in panic