Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: panic when textDocument/prepareCallHierarchy applied to package name #49125

Closed
andrejtokarcik opened this issue Oct 23, 2021 · 2 comments
Labels
Documentation FrozenDueToAge gopls Issues related to the Go language server, gopls. NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@andrejtokarcik
Copy link

What version of Go are you using (go version)?

$ gopls version
golang.org/x/tools/gopls v0.7.3
    golang.org/x/tools/gopls@v0.7.3 h1:Lru57ht8vtDMouRskFC085VAjBAZRAISd/lwvwOOV0Q=
$ go version  # should be irrelevant?
go version go1.16.2 linux/amd64

Does this issue reproduce with the latest release?

Yes, with the latest release of gopls, including the current tools master golang/tools@316ba0b.

What operating system and processor architecture are you using (go env)?

go env Output (should be irrelevant?)
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/andrej/.cache/go-build"
GOENV="/home/andrej/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/andrej/.go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/andrej/.go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/andrej/.go1.16.2"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/andrej/.go1.16.2/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3160952985=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Create example.go consisting of the following single line:

package p

Run gopls, initialize an LSP exchange and send a textDocument/prepareCallHierarchy request with the position parameter pointing to the package name, e.g.:

{
    "method": "textDocument/prepareCallHierarchy",
    "params": {
        "textDocument": {
            "uri": "file:///home/andrej/gopls-panic/example.go"
        },
        "position": {
            "character": 8,
            "line": 0
        }
    },
    "jsonrpc": "2.0",
    "id": 1
}

What did you expect to see?

No panic.

A possible fix seems to be:

diff --git a/internal/lsp/source/call_hierarchy.go b/internal/lsp/source/call_hierarchy.go
index 26ef07eb..b02d1271 100644
--- a/internal/lsp/source/call_hierarchy.go
+++ b/internal/lsp/source/call_hierarchy.go
@@ -33,7 +33,7 @@ func PrepareCallHierarchy(ctx context.Context, snapshot Snapshot, fh FileHandle,
                return nil, err
        }
        // The identifier can be nil if it is an import spec.
-       if identifier == nil {
+       if identifier == nil || identifier.Declaration.obj == nil {
                return nil, nil
        }
 

What did you see instead?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x50 pc=0x800094]

goroutine 131 [running]:
golang.org/x/tools/internal/lsp/source.PrepareCallHierarchy(0x10bda18, 0xc00046cd00, 0x10d1998, 0xc0000aa600, 0x7fb0b82e5480, 0xc000342320, 0x800000000, 0x0, 0x0, 0x0, ...)
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/lsp/source/call_hierarchy.go:40 +0x154
golang.org/x/tools/internal/lsp.(*Server).prepareCallHierarchy(0xc000432480, 0x10bda18, 0xc00046cd00, 0xc00074d500, 0x0, 0x0, 0x0, 0x0, 0x0)
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/lsp/call_hierarchy.go:21 +0x170
golang.org/x/tools/internal/lsp.(*Server).PrepareCallHierarchy(0xc000432480, 0x10bda18, 0xc00046cd00, 0xc00074d500, 0xc00074d500, 0x0, 0x0, 0xc00013a060, 0x0)
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/lsp/server_gen.go:172 +0x4d
golang.org/x/tools/internal/lsp/protocol.serverDispatch(0x10bda18, 0xc00046cd00, 0x10d4e80, 0xc000432480, 0xc00074d4d0, 0x10bdc80, 0xc00046cc80, 0x0, 0x0, 0x10ac060)
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/lsp/protocol/tsserver.go:256 +0x7de8
golang.org/x/tools/internal/lsp/protocol.ServerHandler.func1(0x10bda18, 0xc00046cd00, 0xc00074d4d0, 0x10bdc80, 0xc00046cc80, 0x0, 0xc054f8e2aaa8b53c)
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/lsp/protocol/protocol.go:154 +0xc5
golang.org/x/tools/internal/lsp/lsprpc.handshaker.func1(0x10bda18, 0xc00046cd00, 0xc00074d4d0, 0x10bdc80, 0xc00046cc80, 0x0, 0x0)
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/lsp/lsprpc/lsprpc.go:506 +0x459
golang.org/x/tools/internal/jsonrpc2.MustReplyHandler.func1(0x10bda18, 0xc00046cd00, 0xc00021b248, 0x10bdc80, 0xc00046cc80, 0x29700000007, 0x1)
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/jsonrpc2/handler.go:35 +0xd7
golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1.2(0xc0000428a0, 0xc00043b9e0, 0xc0004a3020, 0x10bda18, 0xc00046cd00, 0xc00021b248, 0x10bdc80, 0xc00046cc80)
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/jsonrpc2/handler.go:103 +0x86
created by golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1
	/home/andrej/.go/pkg/mod/golang.org/x/tools@v0.1.8-0.20211014194737-fc98fb2abd48/internal/jsonrpc2/handler.go:100 +0x173
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Oct 23, 2021
@gopherbot gopherbot added this to the Unreleased milestone Oct 23, 2021
@findleyr
Copy link
Contributor

Thanks for the report, and for investigating!

Your suggested fix seems reasonable. I'll send a CL.

@findleyr findleyr added NeedsFix The path to resolution is known, but the work has not been done. and removed Documentation labels Oct 26, 2021
@findleyr findleyr modified the milestones: Unreleased, gopls/on-deck Oct 26, 2021
@gopherbot
Copy link

Change https://golang.org/cl/358549 mentions this issue: internal/lsp/source: check for nil objects in call hierarchy

@stamblerre stamblerre modified the milestones: gopls/on-deck, gopls/v0.7.4 Nov 2, 2021
@golang golang locked and limited conversation to collaborators Nov 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge gopls Issues related to the Go language server, gopls. NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants