Skip to content

x/tools/gopls: crash in goimports with line directives #51916

Closed
@griesemer

Description

@griesemer

gopls version: v0.8.1 (devel go1.19-e33711f8c1 Tue Mar 15 19:03:38 2022 -0700)
gopls flags:
update flags: proxy
extension version: 0.32.0
go version: devel 1.19-2f6c07744f
environment: Visual Studio Code darwin
initialization error: undefined
issue timestamp: Thu, 24 Mar 2022 04:46:28 GMT
restart history:
Thu, 24 Mar 2022 04:45:11 GMT: activation (enabled: true)

ATTENTION: PLEASE PROVIDE THE DETAILS REQUESTED BELOW.

  1. cd $GOROOT/src/bytes
  2. open the CWD with VSCode: code . (code is a small script that launches VSCode with . in this case)
  3. open (choose) bytes.go file
  4. add the line comment //line math.go:100 on the first line (before the copyright notice)
  5. delete the import keyword on line 10 (to provoke an error) and save the file
  6. using the VSCode terminal (in the CWD), run: go build -gcflags=-L - this will produce an error like:
$ go build -gcflags=-L
# bytes
math.go:108[/Users/gri/goroot/src/bytes/bytes.go:10:1]: syntax error: non-declaration statement outside function body
  1. Option+click on the filename in square brackets to locate the cursor at the error position.
  2. Command+Z in the editor to undo the deletion of the import keyword (don't yet save the file).
  3. After 1s or so the crash appears.

I've been able to reproduce this fairly reliably with these 9 steps.

panic: invalid line number 111 (should be < 19)

goroutine 23670 [running]:
go/token.(*File).MergeLine(0xc024266c60, 0x183%3F)
	  position.go:158  0x1ee
golang.org/x/tools/internal/imports.sortImports({0x0, 0x0}, 0x1c04cc0%3F, 0xc042678200)
	  sortimports.go:56  0x5e9
golang.org/x/tools/internal/imports.formatFile(0xc032ebde80%3F, 0xc042678200%3F, {0xc000712000, 0x7d44, 0x7d45}, 0x0, 0xc035ebabd0)
	  imports.go:108  0x8b
golang.org/x/tools/internal/imports.ApplyFixes({0x0, 0x0, 0x0}, {0x0, 0x0}, {0xc000712000, 0x7d44, 0x7d45}, 0xc035ebabd0, 0x2)
	  imports.go:103  0x165
golang.org/x/tools/internal/lsp/source.computeFixEdits({0x1c0d2d0, 0xc0302fcb40}, 0xc002ce0180, 0x7d44%3F, {0x0, 0x0, 0x0})
	  format.go:195  0x177
golang.org/x/tools/internal/lsp/source.computeImportEdits({0x1c0d2d0, 0xc0302fcb40}, 0xc002ce0180, 0x100f227%3F)
	  format.go:140  0xce
golang.org/x/tools/internal/lsp/source.AllImportsFixes.func1(0x18701a0%3F)
	  format.go:121  0x4a
golang.org/x/tools/internal/lsp/cache.(*importsState).runProcessEnvFunc(0xc0003e29c0, {0x1c05200, 0xc035eba270}, 0xc0302fcb40, 0xc03196c140)
	  imports.go:114  0x5a3
golang.org/x/tools/internal/lsp/cache.(*snapshot).RunProcessEnvFunc(0x1c05158%3F, {0x1c05200%3F, 0xc035eba270%3F}, 0x1c05468%3F)
	  view.go:374  0x34
golang.org/x/tools/internal/lsp/source.AllImportsFixes({0x1c05158, 0xc031108980}, {0x1c0d2d0%3F, 0xc0302fcb40}, {0x1c05468, 0xc02c8f4420})
	  format.go:120  0x308
golang.org/x/tools/internal/lsp.(*Server).codeAction(0x185b440%3F, {0x1c05158, 0xc031108980}, 0xc0436c2000)
	  code_action.go:98  0x97b
golang.org/x/tools/internal/lsp.(*Server).CodeAction(0xc034326000%3F, {0x1c05158%3F, 0xc031108980%3F}, 0x185b440%3F)
	  server_gen.go:16  0x25
golang.org/x/tools/internal/lsp/protocol.serverDispatch({0x1c05158, 0xc031108980}, {0x1c10280, 0xc000274360}, 0xc035eba150, {0x1c053c0, 0xc031108840})
	  tsserver.go:478  0x17a8
golang.org/x/tools/internal/lsp/protocol.ServerHandler.func1({0x1c05158, 0xc031108980}, 0xc035eba150, {0x1c053c0, 0xc031108840})
	  protocol.go:154  0x90
golang.org/x/tools/internal/lsp/lsprpc.handshaker.func1({0x1c05158, 0xc031108980}, 0xc035eba150, {0x1c053c0%3F, 0xc031108840%3F})
	  lsprpc.go:506  0xa43
golang.org/x/tools/internal/jsonrpc2.MustReplyHandler.func1({0x1c05158, 0xc031108980}, 0xc042604d20, {0x1c053c0%3F, 0xc031108840%3F})
	  handler.go:35  0xf6
golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1.2()
	  handler.go:103  0xa3
created by golang.org/x/tools/internal/jsonrpc2.AsyncHandler.func1
	  handler.go:100  0x20a
[Error - 9:46:21 PM] 

OPTIONAL: If you would like to share more information, you can attach your complete gopls logs.

NOTE: THESE MAY CONTAIN SENSITIVE INFORMATION ABOUT YOUR CODEBASE.
DO NOT SHARE LOGS IF YOU ARE WORKING IN A PRIVATE REPOSITORY.

<OPTIONAL: ATTACH LOGS HERE>

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions