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: constant crashing with emacs-lsp #37984

Open
mikroskeem opened this issue Mar 21, 2020 · 8 comments
Open

x/tools/gopls: constant crashing with emacs-lsp #37984

mikroskeem opened this issue Mar 21, 2020 · 8 comments
Labels
Milestone

Comments

@mikroskeem
Copy link

@mikroskeem mikroskeem commented Mar 21, 2020

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

$ go version
go version go1.14.1 darwin/amd64

Does this issue reproduce with the latest release?

Currently 1.14.1 is the latest release.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mark/Library/Caches/go-build"
GOENV="/Users/mark/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/mark/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
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/03/l39qx4sd3g31crj98xtwz44h0000gn/T/go-build952685482=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Set up gopls, emacs-lsp & lsp-ui

GO111MODULE=on go get golang.org/x/tools/gopls@latest
(autoload 'go-mode "go-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.go\\'" . go-mode))
(add-hook 'go-mode-hook 'lsp-deferred)

... and trying to use Emacs for writing code

What did you expect to see?

Autocompletion etc. working without constant crashing

What did you see instead?

Emacs asking if I want to restart gopls and in *gopls::stderr*:

panic: runtime error: index out of range [0] with length 0

goroutine 5076 [running]:
golang.org/x/tools/internal/imports.(*ModuleResolver).init(0xc004132be0, 0x0, 0xc0006940c0)
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/imports/mod.go:85 +0xfac
golang.org/x/tools/internal/imports.(*ModuleResolver).ClearForNewMod(0xc004132be0)
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/imports/mod.go:194 +0xa2
golang.org/x/tools/internal/lsp/cache.(*view).RunProcessEnvFunc(0xc000290280, 0x1a123c0, 0xc005a352f0, 0xc006eef400, 0x0, 0x0)
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/lsp/cache/view.go:279 +0x33f
golang.org/x/tools/internal/lsp/source.AllImportsFixes(0x1a123c0, 0xc005a352f0, 0x1a245c0, 0xc00079c060, 0x1a10a80, 0xc0002b0720, 0x0, 0xc00035ed60, 0x2, 0x2, ...)
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/lsp/source/format.go:87 +0x479
golang.org/x/tools/internal/lsp.(*Server).codeAction(0xc00021dc80, 0x1a123c0, 0xc00595a390, 0xc005ea0780, 0xc005ea0780, 0x0, 0x0, 0x0, 0xc00466cd10)
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/lsp/code_action.go:68 +0x8f5
golang.org/x/tools/internal/lsp.(*Server).CodeAction(0xc00021dc80, 0x1a123c0, 0xc00595a390, 0xc005ea0780, 0xc005ea0780, 0x0, 0x0, 0x1038bc3, 0x17e0260)
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/lsp/server_gen.go:12 +0x4d
golang.org/x/tools/internal/lsp/protocol.serverHandler.Deliver(0x1a2cbc0, 0xc00021dc80, 0x1a123c0, 0xc00595a390, 0xc004291d00, 0x16e7e00, 0x1a10000)
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/lsp/protocol/tsserver.go:433 +0x1e38
golang.org/x/tools/internal/jsonrpc2.(*Conn).Run.func1(0xc00507cde0, 0xc004291d00, 0xc000365140, 0x1a123c0, 0xc00595a390, 0x0, 0x0, 0xc005e15ea0)
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/jsonrpc2/jsonrpc2.go:372 +0x160
created by golang.org/x/tools/internal/jsonrpc2.(*Conn).Run
	/Users/mark/go/pkg/mod/golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a/internal/jsonrpc2/jsonrpc2.go:356 +0x850

Process gopls stderr finished
@gopherbot gopherbot added this to the Unreleased milestone Mar 21, 2020
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Mar 21, 2020

Thank you for filing a gopls issue! Please take a look at the Troubleshooting guide, and make sure that you have provided all of the relevant information here.

@mikroskeem

This comment has been minimized.

Copy link
Author

@mikroskeem mikroskeem commented Mar 21, 2020

gopls build info:

golang.org/x/tools/gopls v0.3.4
    golang.org/x/tools/gopls@v0.3.4 h1:4GC7q/pXQ/tsxHBGVdsMdlB4gCxVC06m/7rIXg1Px4E=
    github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
    github.com/sergi/go-diff@v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
    golang.org/x/mod@v0.1.1-0.20191105210325-c90efee705ee h1:WG0RUwxtNT4qqaXX3DPA8zHFNm/D9xaBpxzHt1WcA/E=
    golang.org/x/sync@v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
    golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a h1:hKrQy/q8/Xivoqgw6nGiz1jqpn1WGBLDcWLZwW0983E=
    golang.org/x/xerrors@v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
    honnef.co/go/tools@v0.0.1-2020.1.3 h1:sXmLre5bzIR6ypkjXCDI3jHPssRhc8KD/Ome589sc3U=
    mvdan.cc/xurls/v2@v2.1.0 h1:KaMb5GLhlcSX+e+qhbRJODnUUBvlw01jt4yrjFIHAuA=

Cleared whole ~/go & caches up today for sure.

@stamblerre stamblerre changed the title x/tools/gopls: Constant crashing with emacs-lsp x/tools/gopls: constant crashing with emacs-lsp Mar 21, 2020
@stamblerre

This comment has been minimized.

Copy link
Contributor

@stamblerre stamblerre commented Mar 21, 2020

Thanks for the report! Seems like the environment setting for your GOPATH is empty, even though you do have a GOPATH setting in your go env. Can you please share the output of running gopls -rpc.trace -v check path/to/file.go within your module? It will show what gopls thinks the GOPATH is.

@stamblerre stamblerre modified the milestones: Unreleased, gopls/v0.4.0 Mar 21, 2020
@mikroskeem

This comment has been minimized.

Copy link
Author

@mikroskeem mikroskeem commented Mar 21, 2020

Oh interesting note, perhaps it's another dumb case of poor environment handling by OS X

2020/03/22 01:40:41 Info:2020/03/22 01:40:41 Build info
----------
golang.org/x/tools/gopls v0.3.4
    golang.org/x/tools/gopls@v0.3.4 h1:4GC7q/pXQ/tsxHBGVdsMdlB4gCxVC06m/7rIXg1Px4E=
    github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
    github.com/sergi/go-diff@v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
    golang.org/x/mod@v0.1.1-0.20191105210325-c90efee705ee h1:WG0RUwxtNT4qqaXX3DPA8zHFNm/D9xaBpxzHt1WcA/E=
    golang.org/x/sync@v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
    golang.org/x/tools@v0.0.0-20200316194252-fafb6e2e8a4a h1:hKrQy/q8/Xivoqgw6nGiz1jqpn1WGBLDcWLZwW0983E=
    golang.org/x/xerrors@v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
    honnef.co/go/tools@v0.0.1-2020.1.3 h1:sXmLre5bzIR6ypkjXCDI3jHPssRhc8KD/Ome589sc3U=
    mvdan.cc/xurls/v2@v2.1.0 h1:KaMb5GLhlcSX+e+qhbRJODnUUBvlw01jt4yrjFIHAuA=

Go info
-------
go version go1.14.1 darwin/amd64

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/mark/Library/Caches/go-build"
GOENV="/Users/mark/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/mark/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="(redacted)/go.mod"
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/03/l39qx4sd3g31crj98xtwz44h0000gn/T/go-build876904722=/tmp/go-build -gno-record-gcc-switches -fno-common"
2020/03/22 01:40:41 Info:2020/03/22 01:40:41 go/packages.Load
	snapshot = 0
	query = [./... builtin]
	packages = 4
(redacted)/internal/core/core.go:(redacted):(redacted): (redacted) undefined (type (redacted) has no field or method (redacted))
@mikroskeem

This comment has been minimized.

Copy link
Author

@mikroskeem mikroskeem commented Mar 21, 2020

Thank you for helping me to pinpoint the issue - missing GOPATH envvar was exactly the issue. Poor configuration on my side in the end :)

Maybe add a note about OS X & GOPATH environment variable to somewhere so someone else wouldn't step into the same bucket like I did?

(My solution:)

(setenv "GOPATH" (concat (getenv "HOME") "/go"))
@stamblerre

This comment has been minimized.

Copy link
Contributor

@stamblerre stamblerre commented Mar 22, 2020

Glad that was the fix! I wonder if this is an Emacs issue - maybe @muirdm can give some insight on if it's expected for users to have to explicitly set the GOPATH in the Emacs config.

Also, @heschik - do you think there is any way we could handle this a bit more gracefully or is this fine?

@muirdm

This comment has been minimized.

Copy link

@muirdm muirdm commented Mar 22, 2020

Regarding GOPATH in Emacs, https://github.com/golang/tools/blob/master/gopls/doc/emacs.md#troubleshooting has a tip:

Emacs must have your environment set properly (PATH, GOPATH, etc). You can run M-x getenv PATH to see if your PATH is set in Emacs. If not, you can try starting Emacs from your terminal, using this package, or moving your shell config from .bashrc into .bashenv (or .zshenv).

Basically, none of your shell config will be loaded if you start Emacs from Finder/Spotlight.

@heschik

This comment has been minimized.

Copy link
Contributor

@heschik heschik commented Mar 23, 2020

@stamblerre I'm not clear on what exactly happened, honestly. https://cs.opensource.google/go/tools/+/master:internal/lsp/cache/view.go;l=332;drc=521f4a0cd458c441dec3bafd8ba24526a6cb9b09 is supposed to have used the output of the go command to populate GOPATH, so why didn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.