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/cmd/gopls: panic when selecting text in a module whose go.mod is not at the workspace root #32132

Closed
dpinela opened this issue May 18, 2019 · 2 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls.
Milestone

Comments

@dpinela
Copy link
Contributor

dpinela commented May 18, 2019

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

$ go version
go version go1.12.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes (built gopls at d88f79806bbd013f54a668506864ce559edf6f0a)

gopls build info:

#### Build info

golang.org/x/tools/cmd/gopls
    golang.org/x/tools@(devel)
    golang.org/x/sync@v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/dpinela/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/dpinela/dev/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/41/2xpv_r1j5n5bnflwb7s1hv580000gp/T/go-build061533766=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Create the following directory structure:

bug/
  x/
    go.mod
    main.go

Contents of go.mod:

module blorp

go 1.12

Contents of main.go:

package blorp

Then, open the bug directory in VS Code (with the Go extension set up of course), navigate to main.go, and select some text.

What did you expect to see?

Nothing happens.

What did you see instead?

gopls crashes repeatedly with the following error log and stack trace:

[Error - 11:51:22 PM] failed to deliver diagnostic for file:///Users/dpinela/Desktop/bug/no%20packages%20found%20for%20/Users/dpinela/Desktop/bug/x/main.go: stat /Users/dpinela/Desktop/bug/no packages found for /Users/dpinela/Desktop/bug/x/main.go: no such file or directory
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x132a235]

goroutine 18 [running]:
go/token.(*File).Name(...)
	/usr/local/Cellar/go/1.12.2/libexec/src/go/token/position.go:110
golang.org/x/tools/internal/lsp/source.Imports(0x14f47c0, 0xc00008c140, 0x14f88c0, 0xc0002a4000, 0xc0000c3640, 0x4, 0x7, 0x6, 0xc0000a4f50, 0xc0000c3640, ...)
	/Users/dpinela/dev/golang-dev/tools/internal/lsp/source/format.go:56 +0x75
golang.org/x/tools/internal/lsp.organizeImports(0x14f47c0, 0xc00008c140, 0x14fa0a0, 0xc00018a480, 0xc00022c3f0, 0x2b, 0x1, 0x4, 0x3, 0x1, ...)
	/Users/dpinela/dev/golang-dev/tools/internal/lsp/code_action.go:82 +0x1a9
golang.org/x/tools/internal/lsp.(*Server).codeAction(0xc00016c4d0, 0x14f47c0, 0xc00008c140, 0xc000230120, 0xc000230120, 0x0, 0x0, 0x0, 0xc000184460)
	/Users/dpinela/dev/golang-dev/tools/internal/lsp/code_action.go:32 +0x1f1
golang.org/x/tools/internal/lsp.(*Server).CodeAction(0xc00016c4d0, 0x14f47c0, 0xc00008c140, 0xc000230120, 0xc000230120, 0x0, 0x0, 0x0, 0x0)
	/Users/dpinela/dev/golang-dev/tools/internal/lsp/server.go:202 +0x4d
golang.org/x/tools/internal/lsp/protocol.serverHandler.func1(0x14f47c0, 0xc00008c140, 0xc00016c540, 0xc0002181c0)
	/Users/dpinela/dev/golang-dev/tools/internal/lsp/protocol/tsserver.go:366 +0x2e77
golang.org/x/tools/internal/jsonrpc2.(*Conn).Run.func1(0xc0000a0a80, 0xc00016c540)
	/Users/dpinela/dev/golang-dev/tools/internal/jsonrpc2/jsonrpc2.go:276 +0xda
created by golang.org/x/tools/internal/jsonrpc2.(*Conn).Run
	/Users/dpinela/dev/golang-dev/tools/internal/jsonrpc2/jsonrpc2.go:270 +0xba

The error seems to stop occurring if I replace "blorp" in the files with "x". I'm not sure why. It also does not occur when the go.mod is at the root of the workspace.

@gopherbot gopherbot added this to the Unreleased milestone May 18, 2019
@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label May 18, 2019
@gopherbot
Copy link

Change https://golang.org/cl/178158 mentions this issue: internal/lsp: add nil checks for ASTs and token

@stamblerre
Copy link
Contributor

For gopls to work with modules, VSCode has to be opened at the module root. Otherwise, gopls has no way of knowing the module root. I did add a check to avoid the crash.

gopherbot pushed a commit to golang/tools that referenced this issue May 20, 2019
Fixes golang/go#32120
Updates golang/go#32132

Change-Id: Ib4bb8a4818be7dec468c46b72afc3dd57b35f155
Reviewed-on: https://go-review.googlesource.com/c/tools/+/178158
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
@golang golang locked and limited conversation to collaborators May 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls.
Projects
None yet
Development

No branches or pull requests

3 participants