Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 30, 2025

The LSP server panics when receiving a textDocument/definition request with a line number exceeding the file's line count. This occurs during client/server synchronization issues where the client's position data is stale.

Changes

  • Test: Added TestPanicOnOutOfBoundsDefinition that sends a definition request for line 65 in an 8-line file
  • Helper: Added SendDefinitionRequestAtPosition() to fourslash framework to bypass bounds checking for error handling tests

Current Behavior

panic: bad line number. Line: 65, lineMap length: 8
at LineAndCharacterToPosition (converters.go:143)

The server recovers and returns InternalError (-32603). The test fails as expected, documenting the bug without fixing it.

// Test simulates stale client position
f.SendDefinitionRequestAtPosition(t, 65, 24)
// File only has 8 lines - should handle gracefully, not panic
Original prompt

This section details on the original issue you should resolve

<issue_title>Panic on textDocument/definition</issue_title>
<issue_description>## Stack trace

panic handling request textDocument/definition bad line number. Line: 65, lineMap length: 33 goroutine 74379 [running]:
runtime/debug.Stack()
	runtime/debug/stack.go:26 +0x5e
github.com/microsoft/typescript-go/internal/lsp.(*Server).recover(0xc0000f4dc0, 0xc05d9571d0)
	github.com/microsoft/typescript-go/internal/lsp/server.go:532 +0x58
panic({0x7ff76dc5a4c0?, 0xc02a3d9970?})
	runtime/panic.go:783 +0x132
github.com/microsoft/typescript-go/internal/ls.(*Converters).LineAndCharacterToPosition(0xc025592360, {0x7ff76dfe4430, 0xc03fbe7608}, {0x0?, 0x0?})
	github.com/microsoft/typescript-go/internal/ls/converters.go:143 +0x2d9
github.com/microsoft/typescript-go/internal/ls.(*LanguageService).ProvideDefinition(0xc010739488, {0x7ff76dfe59d0, 0xc05eecf360}, {0xc02e3db420?, 0x7ff76d5a94f0?}, {0x0?, 0x0?})
	github.com/microsoft/typescript-go/internal/ls/definition.go:17 +0xac
github.com/microsoft/typescript-go/internal/lsp.(*Server).handleDefinition(0xc0003501c0?, {0x7ff76dfe59d0?, 0xc05eecf360?}, 0xc02e3db420?, 0x65?)
	github.com/microsoft/typescript-go/internal/lsp/server.go:735 +0x35
github.com/microsoft/typescript-go/internal/lsp.init.func1.registerLanguageServiceDocumentRequestHandler[...].13({0x7ff76dfe59d0, 0xc05eecf360}, 0xc05d9571d0)
	github.com/microsoft/typescript-go/internal/lsp/server.go:518 +0x13f
github.com/microsoft/typescript-go/internal/lsp.(*Server).handleRequestOrNotification(0xc0000f4dc0, {0x7ff76dfe59d0, 0xc05eecf360}, 0xc05d9571d0)
	github.com/microsoft/typescript-go/internal/lsp/server.go:424 +0x10d
github.com/microsoft/typescript-go/internal/lsp.(*Server).dispatchLoop.func1()
	github.com/microsoft/typescript-go/internal/lsp/server.go:329 +0x3a
created by github.com/microsoft/typescript-go/internal/lsp.(*Server).dispatchLoop in goroutine 10
	github.com/microsoft/typescript-go/internal/lsp/server.go:349 +0x8aa

[Error - 9:42:06 AM] Request textDocument/definition failed.
  Message: InternalError: panic handling request textDocument/definition: bad line number. Line: 65, lineMap length: 33
  Code: -32603 

Steps to reproduce

I couldn't easily reproduce again. I was commenting out some code and then it appeared to error when the editor lost focus. I tried re-doing edits and it didn't occur.

The file I am in has 44 lines so not sure where bad line number 65 or line map length 33 came from.

Checking the LSP logs:

[Trace - 9:42:06 AM] Sending request 'textDocument/definition - (54)'.
Params: {
    "textDocument": {
        "uri": "file:///c%3A/Projects/a/b/c.play.ts"
    },
    "position": {
        "line": 65,
        "character": 24
    }
}


[Trace - 9:42:06 AM] Received response 'textDocument/definition - (54)' in 1ms. Request failed: InternalError: panic handling request textDocument/definition: bad line number. Line: 65, lineMap length: 33 (-32603).

so It seems it is a vscode issue - asking for a wrong line number?!

Feel free to close - I guess when this is production it will ignore errors like this instead of popping up.</issue_description>

<agent_instructions>Create a fourslash test for this issue in internal/fourslash/tests. Do not attempt to fix the issue. Only create a test that fails, reproducing the bug.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@iisaduan Do other files in the same folder have the same name but different extensions? @jakebailey ``` panic handling request textDocument/definition bad line number. Line: 24, lineMap length: 23 goroutine 43742 [running]: runtime/debug.Stack() runtime/debug/stack.go:26 +0x5e github.com/microsoft/typescript-go/internal/lsp.(*Server).recover(0xc00017cdc0, 0xc02ea55800) github.com/microsoft/typescript-go/internal/lsp/server.go:567 +0x58 panic({0xb147a0?, 0xc017495f00?}) runtime/panic.go:783 +0x132 github.com/microsoft/typescript-go/internal/ls.(*Converters).LineAndCharacterToPosition(0xc01695b8a8, {0xea6f90, 0xc01cfde908}, {0xf21dda8?, 0xc0?}) github.com/microsoft/typescript-go/internal/ls/converters.go:143 +0x2d9 github.com/microsoft/typescript-go/internal/ls.(*LanguageService).ProvideDefinition(0xc02d676900, {0xea88c0, 0xc01ba8a960}, {0xc01c5f6600?, 0x10000c02698b620?}, {0x0?, 0x0?}) github.com/microsoft/typescript-go/internal/ls/definition.go:17 +0xaf github.com/microsoft/typescript-go/internal/lsp.(*Server).handleDefinition(0xc00031ca20?, {0xea88c0?, 0xc01ba8a960?}, 0xc01c5f6600?, 0x53?) github.com/microsoft/typescript-go/internal/lsp/server.go:815 +0x35 github.com/microsoft/typescript-go/internal/lsp.init.func1.registerLanguageServiceDocumentRequestHandler[...].14({0xea88c0, 0xc01ba8a960}, 0xc02ea55800) github.com/microsoft/typescript-go/internal/lsp/server.go:553 +0x13f github.com/microsoft/typescri...

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix panic on textDocument/definition due to bad line number Add test reproducing panic on out-of-bounds textDocument/definition request Oct 30, 2025
Copilot finished work on behalf of jakebailey October 30, 2025 08:13
Copilot AI requested a review from jakebailey October 30, 2025 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic on textDocument/definition

2 participants