Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025

Problem

The formatter was panicking when handling certain code patterns, specifically when formatting code with trailing semicolons and comments. The panic manifested as:

runtime error: slice bounds out of range [:130] with length 128

Example code that triggered the panic:

const _enableDisposeWithListenerWarning = false
	// || Boolean("TRUE") // causes a linter warning so that it cannot be pushed
	;

Root Causes

After cross-comparing with the TypeScript reference implementation in _submodules/TypeScript/src/services/formatting/formatting.ts, two related issues were identified:

1. Negative indentation handling

When processing comments, the formatter can receive an indentation value of -1 (representing Constants.Unknown in TypeScript). Go's strings.Repeat panics on negative counts, while TypeScript's custom repeatString function gracefully handles this by using a for loop that simply returns an empty string when the count is negative.

2. Slice bounds checking

The indentationIsDifferent function attempted to slice the source text without verifying the slice wouldn't exceed the text length, causing a bounds panic.

Solution

In internal/format/span.go:

  1. Added safety check in getIndentationString to return an empty string for negative indentation values, matching TypeScript's behavior
  2. Added bounds checking in indentationIsDifferent before slicing to prevent out-of-range panics

In internal/format/comment_test.go:

  1. Added test case TestSliceBoundsPanic that reproduces the original panic scenario and verifies the fix

Testing

  • ✅ New test passes and reproduces the original panic (before fix)
  • ✅ All existing format tests continue to pass
  • ✅ Language service formatting tests pass
  • ✅ Linter and formatter checks pass
  • ✅ CodeQL security scan clean

Fixes the panic reported in the issue while maintaining compatibility with all existing formatting behavior.

Original prompt

This section details on the original issue you should resolve

<issue_title>panic handling request textDocument/formatting: runtime error: slice bounds out of range</issue_title>
<issue_description>## Stack trace


[Error - 10:17:39 AM] Request textDocument/formatting failed.
  Message: InternalError: panic handling request textDocument/formatting: runtime error: slice bounds out of range [:130] with length 128
  Code: -32603 
panic handling request textDocument/formatting runtime error: slice bounds out of range [:130] with length 128 goroutine 275 [running]:
runtime/debug.Stack()
	runtime/debug/stack.go:26 +0x64
github.com/microsoft/typescript-go/internal/lsp.(*Server).recover(0x14000000140, 0x14001723500)
	github.com/microsoft/typescript-go/internal/lsp/server.go:539 +0x44
panic({0x104a3c700?, 0x1400021e0a8?})
	runtime/panic.go:783 +0x120
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).indentationIsDifferent(...)
	github.com/microsoft/typescript-go/internal/format/span.go:886
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).insertIndentation(0x14000554c30, 0x7f, 0x4, 0x0)
	github.com/microsoft/typescript-go/internal/format/span.go:867 +0x2fc
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).consumeTokenAndAdvanceScanner(0x14000554c30, {{0x14000238390, 0x4, 0x4}, {{0x7f, 0x80}, 0x1a}, {0x0, 0x0, 0x0}}, ...)
	github.com/microsoft/typescript-go/internal/format/span.go:1065 +0x308
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).processNode(0x14000554c30, 0x1400051cd20, 0x1400051cd20, 0x0, 0x0, 0x0, 0x4)
	github.com/microsoft/typescript-go/internal/format/span.go:628 +0x160
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).execute(0x14000554c30, 0x1041b48cc?)
	github.com/microsoft/typescript-go/internal/format/span.go:251 +0x3d4
github.com/microsoft/typescript-go/internal/format.newFormattingScanner({0x14000242771, 0x80}, 0x0, 0x0, 0x80, 0x14000554c30)
	github.com/microsoft/typescript-go/internal/format/scanner.go:57 +0x1fc
github.com/microsoft/typescript-go/internal/format.FormatSpan({0x104b86340, 0x140023284b0}, {0x0, 0x80}, 0x1400044e908, 0x0)
	github.com/microsoft/typescript-go/internal/format/api.go:60 +0x1c8
github.com/microsoft/typescript-go/internal/format.FormatDocument({0x104b86340?, 0x140023284b0?}, 0x18?)
	github.com/microsoft/typescript-go/internal/format/api.go:108 +0x34
github.com/microsoft/typescript-go/internal/ls.(*LanguageService).getFormattingEditsForDocument(0x140002541e0?, {0x104b86378?, 0x14000158910?}, 0x1400044e908, 0x0?)
	github.com/microsoft/typescript-go/internal/ls/format.go:104 +0x44
github.com/microsoft/typescript-go/internal/ls.(*LanguageService).ProvideFormatDocument(0x14002328360, {0x104b86378, 0x14000158910}, {0x140003341b0?, 0x1400036c000?}, 0x140002541e0)
	github.com/microsoft/typescript-go/internal/ls/format.go:46 +0xb4
github.com/microsoft/typescript-go/internal/lsp.(*Server).handleDocumentFormat(0x1400036c000?, {0x104b86378?, 0x14000158910?}, 0x140003341b0?, 0x28?)
	github.com/microsoft/typescript-go/internal/lsp/server.go:817 +0x38
github.com/microsoft/typescript-go/internal/lsp.init.func1.registerLanguageServiceDocumentRequestHandler[...].19({0x104b86378, 0x14000158910}, 0x14001723500)
	github.com/microsoft/typescript-go/internal/lsp/server.go:525 +0xe0
github.com/microsoft/typescript-go/internal/lsp.(*Server).handleRequestOrNotification(0x14000000140, {0x104b86378, 0x14000158910}, 0x14001723500)
	github.com/microsoft/typescript-go/internal/lsp/server.go:422 +0xf4
github.com/microsoft/typescript-go/internal/lsp.(*Server).dispatchLoop.func1()
	github.com/microsoft/typescript-go/internal/lsp/server.go:327 +0x34
created by github.com/microsoft/typescript-go/internal/lsp.(*Server).dispatchLoop in goroutine 35
	github.com/microsoft/typescript-go/internal/lsp/server.go:347 +0x6f8

Steps to reproduce

Try formatting the following document:

const _enableDisposeWithListenerWarning = false
	// || Boolean("TRUE") // causes a linter warning so that it cannot be pushed
	;
```</issue_description>

<agent_instructions>Create and commit a failing test FIRST, then cross compare the formatting implementation against _submodules/TypeScript/src/services/formatting/formatting.ts</agent_instructions>

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

<comments>
</comments>

Fixes #1925


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix panic handling request textDocument/formatting Fix panic in textDocument/formatting with slice bounds out of range Oct 22, 2025
Copilot AI requested a review from jakebailey October 22, 2025 19:03
Copilot finished work on behalf of jakebailey October 22, 2025 19:03
@jakebailey
Copy link
Member

This might be #1929 anyway.

@jakebailey
Copy link
Member

Yep, the test in this PR is fixed by #1929.

@jakebailey jakebailey closed this Oct 22, 2025
@jakebailey jakebailey deleted the copilot/fix-slice-bounds-error branch October 30, 2025 23:51
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 handling request textDocument/formatting: runtime error: slice bounds out of range

2 participants