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 in token.File.Name within PackageDocHTML (telemetry) #70149

Closed
adonovan opened this issue Oct 31, 2024 · 4 comments
Closed
Assignees
Labels
gopls/telemetry-wins gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan adonovan added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. gopls/telemetry-wins labels Oct 31, 2024
@gopherbot gopherbot added this to the Unreleased milestone Oct 31, 2024
@IlyasYOY
Copy link

IlyasYOY commented Nov 1, 2024

@adonovan Hello! I guess I found a bug. May I contribute a fix?

I created empty file:
image

Then I called action:
image

After that I have the same stacktrace in my neovim log:

[START][2024-11-01 21:02:04] LSP logging initiated
[ERROR][2024-11-01 21:02:04] ...lsp/handlers.lua:623	"2024/11/01 21:02:04 imports fixes: allImportsFixes: /Users/ilyasyoy/Projects/IlyasYOY/main.go:1:2: expected ';', found 'EOF' (and 2 more errors)\n\tfile=\"/Users/ilyasyoy/Projects/IlyasYOY/main.go\"\n"
[ERROR][2024-11-01 21:02:05] .../vim/lsp/rpc.lua:770	"rpc"	"gopls"	"stderr"	"server.go:3489: http: panic serving 127.0.0.1:61794: runtime error: invalid memory address or nil pointer dereference\ngoroutine 128 [running]:\nnet/http.(*conn).serve.func1()\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:1947 +0xb0\npanic({0x101962300?, 0x102119110?})\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/runtime/panic.go:785 +0x124\ngo/token.(*File).Name(...)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/go/token/position.go:114\ngolang.org/x/tools/gopls/internal/golang.PackageDocHTML({0x1015eb532, 0x1}, 0x14000499da0, {0x101a95c40, 0x1400051c8c0})\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/golang/pkgdoc.go:284 +0x160\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.func6({0x101a99798, 0x140001d0380}, 0x140000aac80)\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/server/server.go:350 +0x27c\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0x101a99798?, 0x140001d0380?}, 0x5?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.StripPrefix.func11({0x101a99798, 0x140001d0380}, 0x140000aab40)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2282 +0x228\nnet/http.HandlerFunc.ServeHTTP(0x1400050e178?, {0x101a99798?, 0x140001d0380?}, 0x10155f2dc?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\nnet/http.(*ServeMux).ServeHTTP(0x0?, {0x101a99798, 0x140001d0380}, 0x140000aab40)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2747 +0x1b4\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.StripPrefix.func9({0x101a99798, 0x140001d0380}, 0x140000aaa00)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2282 +0x228\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0x101a99798?, 0x140001d0380?}, 0x10?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.withPanicHandler.func10({0x101a99798?, 0x140001d0380?}, 0x140000c3b28?)\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/server/server.go:537 +0x74\nnet/http.HandlerFunc.ServeHTTP(0x1400050e098?, {0x101a99798?, 0x140001d0380?}, 0x100f40504?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\nnet/http.(*ServeMux).ServeHTTP(0x0?, {0x101a99798, 0x140001d0380}, 0x140000aaa00)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2747 +0x1b4\nnet/http.serverHandler.ServeHTTP({0x140005a3590?}, {0x101a99798?, 0x140001d0380?}, 0x6?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:3210 +0xbc\nnet/http.(*conn).serve(0x140000ffc20, {0x101a9b0e8, 0x140005a2de0})\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2092 +0x4fc\ncreated by net/http.(*Server).Serve in goroutine 200\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:3360 +0x3dc\n"
[ERROR][2024-11-01 21:02:06] .../vim/lsp/rpc.lua:770	"rpc"	"gopls"	"stderr"	"server.go:3489: http: panic serving 127.0.0.1:61795: runtime error: invalid memory address or nil pointer dereference\ngoroutine 209 [running]:\nnet/http.(*conn).serve.func1()\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:1947 +0xb0\npanic({0x101962300?, 0x102119110?})\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/runtime/panic.go:785 +0x124\ngo/token.(*File).Name(...)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/go/token/position.go:114\ngolang.org/x/tools/gopls/internal/golang.PackageDocHTML({0x1015eb532, 0x1}, 0x14000499da0, {0x101a95c40, 0x1400051c8c0})\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/golang/pkgdoc.go:284 +0x160\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.func6({0x101a99798, 0x140001d0460}, 0x140000aaf00)\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/server/server.go:350 +0x27c\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0x101a99798?, 0x140001d0460?}, 0x5?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.StripPrefix.func11({0x101a99798, 0x140001d0460}, 0x140000aadc0)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2282 +0x228\nnet/http.HandlerFunc.ServeHTTP(0x1400050e178?, {0x101a99798?, 0x140001d0460?}, 0x10155f2dc?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\nnet/http.(*ServeMux).ServeHTTP(0x0?, {0x101a99798, 0x140001d0460}, 0x140000aadc0)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2747 +0x1b4\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.StripPrefix.func9({0x101a99798, 0x140001d0460}, 0x1400025c140)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2282 +0x228\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0x101a99798?, 0x140001d0460?}, 0x10?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.withPanicHandler.func10({0x101a99798?, 0x140001d0460?}, 0x140004f5b28?)\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/server/server.go:537 +0x74\nnet/http.HandlerFunc.ServeHTTP(0x1400050e098?, {0x101a99798?, 0x140001d0460?}, 0x100f40504?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\nnet/http.(*ServeMux).ServeHTTP(0x0?, {0x101a99798, 0x140001d0460}, 0x1400025c140)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2747 +0x1b4\nnet/http.serverHandler.ServeHTTP({0x140004e6600?}, {0x101a99798?, 0x140001d0460?}, 0x6?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:3210 +0xbc\nnet/http.(*conn).serve(0x140000ffcb0, {0x101a9b0e8, 0x140005a2de0})\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2092 +0x4fc\ncreated by net/http.(*Server).Serve in goroutine 200\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:3360 +0x3dc\n"
[ERROR][2024-11-01 21:02:06] .../vim/lsp/rpc.lua:770	"rpc"	"gopls"	"stderr"	"server.go:3489: http: panic serving 127.0.0.1:61796: runtime error: invalid memory address or nil pointer dereference\ngoroutine 187 [running]:\nnet/http.(*conn).serve.func1()\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:1947 +0xb0\npanic({0x101962300?, 0x102119110?})\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/runtime/panic.go:785 +0x124\ngo/token.(*File).Name(...)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/go/token/position.go:114\ngolang.org/x/tools/gopls/internal/golang.PackageDocHTML({0x1015eb532, 0x1}, 0x14000499da0, {0x101a95c40, 0x1400051c8c0})\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/golang/pkgdoc.go:284 +0x160\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.func6({0x101a99798, 0x140001d0540}, 0x140000ab2c0)\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/server/server.go:350 +0x27c\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0x101a99798?, 0x140001d0540?}, 0x5?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.StripPrefix.func11({0x101a99798, 0x140001d0540}, 0x140000ab180)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2282 +0x228\nnet/http.HandlerFunc.ServeHTTP(0x1400050e178?, {0x101a99798?, 0x140001d0540?}, 0x10155f2dc?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\nnet/http.(*ServeMux).ServeHTTP(0x0?, {0x101a99798, 0x140001d0540}, 0x140000ab180)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2747 +0x1b4\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.StripPrefix.func9({0x101a99798, 0x140001d0540}, 0x140000ab040)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2282 +0x228\nnet/http.HandlerFunc.ServeHTTP(0x0?, {0x101a99798?, 0x140001d0540?}, 0x10?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\ngolang.org/x/tools/gopls/internal/server.(*server).initWeb.withPanicHandler.func10({0x101a99798?, 0x140001d0540?}, 0x140004f5b28?)\n\t/Users/ilyasyoy/go/pkg/mod/golang.org/x/tools/[email protected]/internal/server/server.go:537 +0x74\nnet/http.HandlerFunc.ServeHTTP(0x1400050e098?, {0x101a99798?, 0x140001d0540?}, 0x100f40504?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2220 +0x38\nnet/http.(*ServeMux).ServeHTTP(0x0?, {0x101a99798, 0x140001d0540}, 0x140000ab040)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2747 +0x1b4\nnet/http.serverHandler.ServeHTTP({0x140005a3bc0?}, {0x101a99798?, 0x140001d0540?}, 0x6?)\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:3210 +0xbc\nnet/http.(*conn).serve(0x140003f5050, {0x101a9b0e8, 0x140005a2de0})\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:2092 +0x4fc\ncreated by net/http.(*Server).Serve in goroutine 200\n\t/opt/homebrew/Cellar/go/1.23.0/libexec/src/net/http/server.go:3360 +0x3dc\n"

I guess the fix might be here: /src/go/ast/ast.go#L1057 (or in tools code near the caller logic)

There is no package in the file so the documentation might not be generated.

@adonovan
Copy link
Member Author

adonovan commented Nov 1, 2024

Thanks for explaining the input that caused the crash! There are actually two bugs here. The first is that the loop in PackageDocHTML should not assume that File.Pos() is valid, as it may not be for (e.g.) empty files; instead it should use File.FileStart. However, the second bug is that the parser fails to populate FileStart and FileEnd in the case of an empty file; I've filed that separately as #70162.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/624437 mentions this issue: x/tools: fset.File(file.Pos()) -> fset.File(file.FileStart)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/telemetry-wins gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants