-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/cmd/golsp: panic when running gopls query definition internal/lsp/cmd/definition.go:#1277
#30280
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
Comments
nil pointer dereference error happen because (*File).GetToken() found an error and return nil back to the caller. Reproduce by adding logs into diff --git a/internal/lsp/cache/file.go b/internal/lsp/cache/file.go
index d75c6916..3a90fecb 100644
--- a/internal/lsp/cache/file.go
+++ b/internal/lsp/cache/file.go
@@ -8,6 +8,7 @@ import (
"go/ast"
"go/token"
"io/ioutil"
+ "log"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/lsp/source"
@@ -41,6 +42,7 @@ func (f *File) GetToken() *token.File {
defer f.view.mu.Unlock()
if f.token == nil {
if err := f.view.parse(f.URI); err != nil {
+ log.Println("cache: cannot parse uri:", err)
return nil
}
} Then build and run:
This will probably because of diff --git a/internal/lsp/source/uri_test.go b/internal/lsp/source/uri_test.go
index e3d1e7d8..3ca8a8ca 100644
--- a/internal/lsp/source/uri_test.go
+++ b/internal/lsp/source/uri_test.go
@@ -37,6 +37,14 @@ func TestURI(t *testing.T) {
uri: URI(`file:///a/b/c/src/bob.go`),
filename: `/a/b/c/src/bob.go`,
},
+ {
+ uri: URI(`file://./internal/lsp/cmd/definition.go`),
+ filename: `./internal/lsp/cmd/definition.go`,
+ },
+ {
+ uri: URI(`file://internal/lsp/cmd/definition.go`),
+ filename: `internal/lsp/cmd/definition.go`,
+ },
} {
if string(tt.uri) != toURI(tt.filename).String() {
t.Errorf("ToURI: expected %s, got %s", tt.uri, ToURI(tt.filename)) The result is:
|
Try to fixing by use diff --git a/internal/lsp/source/uri.go b/internal/lsp/source/uri.go
index d630e704..a7ee4945 100644
--- a/internal/lsp/source/uri.go
+++ b/internal/lsp/source/uri.go
@@ -46,7 +46,8 @@ func filename(uri URI) (string, error) {
// ToURI returns a protocol URI for the supplied path.
// It will always have the file scheme.
func ToURI(path string) URI {
- u := toURI(path)
+ abspath, _ := filepath.Abs(path)
+ u := toURI(abspath)
u.Path = filepath.ToSlash(u.Path)
return URI(u.String())
} This would work but not sure is it real solution or not. |
Thank you for this report @mbana and for the patch discussions @wingyplus! I shall kindly page @stamblerre to take a look. |
Thanks, anyone else think the interface should change from: func (f *File) GetToken() *token.File to func (f *File) GetToken() (*token.File, error) Actually to be a bit more pedantic, the interface below which is defined in type File interface {
GetAST() (*ast.File, error)
GetFileSet() (*token.FileSet, error)
GetPackage() (*packages.Package, error)
GetToken() (*token.File, error)
GetContent() ([]byte, error)
} Example of errorr being masked as already pointed out before: func (f *File) GetAST() *ast.File {
f.view.mu.Lock()
defer f.view.mu.Unlock()
if f.ast == nil {
if err := f.view.parse(f.URI); err != nil {
return nil
}
}
return f.ast
} |
I've created a PR to this effect. Please see golang/tools#75. There's more error handling that could be done from my reading of the code, but I'll do this in another PR. |
Thanks. In this PR, golang/tools#75, I added error handling across the board. Now, like you tests I now get: $ gopls query definition './internal/lsp/cmd/definition.go:#1277'
definition: no packages found for /internal/lsp/cmd/definition.go |
I think the issue here is that when using gopls in the server mode, we expect file URIs in the format that is sent by VSCode. It looks like we should probably absolutize the paths for the other modes. As @ianthehat mentioned on your CL, we intentionally removed the error handling in golang.org/cl/162399 because we never really did anything useful with these errors. |
Change https://golang.org/cl/163160 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I also tried with
go version go1.12rc1 darwin/amd64
and the issue seems to occur there as well.What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Start Go Docker image:
Inside Docker:
What did you expect to see?
The definition of the identifier.
What did you see instead?
The text was updated successfully, but these errors were encountered: