Skip to content

Crash in range formatting when requested on a line that is different from the containing function #1992

@DanielRosenwasser

Description

@DanielRosenwasser
function foo() {
    [|return (1  + 2);|]
}

Format the range (trigger with Ctrl+K; Ctrl+F).

panic handling request textDocument/rangeFormatting runtime error: invalid memory address or nil pointer dereference goroutine 2169 [running]:
runtime/debug.Stack()
	runtime/debug/stack.go:26 +0x64
github.com/microsoft/typescript-go/internal/lsp.(*Server).recover(0x4000000f00, 0x400c378180)
	github.com/microsoft/typescript-go/internal/lsp/server.go:567 +0x44
panic({0x7ff78a61ad20?, 0x7ff78af56550?})
	runtime/panic.go:783 +0x120
github.com/microsoft/typescript-go/internal/ast.(*Node).Pos(...)
	github.com/microsoft/typescript-go/internal/ast/ast.go:240
github.com/microsoft/typescript-go/internal/scanner.GetTokenPosOfNode(0xf?, 0x29?, 0xc0?)
	github.com/microsoft/typescript-go/internal/scanner/scanner.go:2323 +0xa8
github.com/microsoft/typescript-go/internal/format.getStartLineAndCharacterForNode(0x400c2b46c0?, 0x40060cbb08)
	github.com/microsoft/typescript-go/internal/format/indent.go:250 +0x24
github.com/microsoft/typescript-go/internal/format.getIndentationForNodeWorker(0x7ff78a97b790?, 0x40060cbb08?, 0xf?, 0x4006151d48, 0x7ff78a97d0e8?, 0x40060cbb08, 0x0, 0x40034ea150)
	github.com/microsoft/typescript-go/internal/format/indent.go:69 +0x154
github.com/microsoft/typescript-go/internal/format.GetIndentationForNode(0x400c2b46c0, 0x4006151d48, 0x40060cbb08, 0x40034ea150)
	github.com/microsoft/typescript-go/internal/format/indent.go:17 +0x6c
github.com/microsoft/typescript-go/internal/format.FormatSpan({0x7ff78a97d0e8, 0x400ac40180}, {0x12, 0x26}, 0x40060cbb08, 0x1)
	github.com/microsoft/typescript-go/internal/format/api.go:69 +0xbc
github.com/microsoft/typescript-go/internal/format.FormatSelection({0x7ff78a97d0e8, 0x400ac40180}, 0x40060cbb08, 0x7ff78a970f20?, 0x26)
	github.com/microsoft/typescript-go/internal/format/api.go:112 +0x50
github.com/microsoft/typescript-go/internal/ls.(*LanguageService).getFormattingEditsForRange(0x400a301470?, {0x7ff78a97d120?, 0x4009878000?}, 0x40060cbb08, 0x17?, {0x0?, 0x0?})
	github.com/microsoft/typescript-go/internal/ls/format.go:95 +0x5c
github.com/microsoft/typescript-go/internal/ls.(*LanguageService).ProvideFormatDocumentRange(0x400ac400f0, {0x7ff78a97d120, 0x4009878000}, {0x4009ba8300?, 0x4006151e78?}, 0x400ac1a000, {{0x46bcb97d?, 0x12?}, {0x634352ec?, 0x1395cb5?}})
	github.com/microsoft/typescript-go/internal/ls/format.go:61 +0xf8
github.com/microsoft/typescript-go/internal/lsp.(*Server).handleDocumentRangeFormat(0x40002a0a20?, {0x7ff78a97d120?, 0x4009878000?}, 0x4009ba8300?, 0x13?)
	github.com/microsoft/typescript-go/internal/lsp/server.go:869 +0x40
github.com/microsoft/typescript-go/internal/lsp.init.func1.registerLanguageServiceDocumentRequestHandler[...].21({0x7ff78a97d120, 0x4009878000}, 0x400c378180)
	github.com/microsoft/typescript-go/internal/lsp/server.go:553 +0xe0
github.com/microsoft/typescript-go/internal/lsp.(*Server).handleRequestOrNotification(0x4000000f00, {0x7ff78a97d120, 0x4009878000}, 0x400c378180)
	github.com/microsoft/typescript-go/internal/lsp/server.go:449 +0xf4
github.com/microsoft/typescript-go/internal/lsp.(*Server).dispatchLoop.func1()
	github.com/microsoft/typescript-go/internal/lsp/server.go:354 +0x34
created by github.com/microsoft/typescript-go/internal/lsp.(*Server).dispatchLoop in goroutine 10
	github.com/microsoft/typescript-go/internal/lsp/server.go:374 +0x79c

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions