Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025

  • Understand the issue: panic when formatting chained method calls with comments
  • Create a test case that reproduces the panic
  • Compare with TypeScript source to identify the fix
  • Fix the getIndentationForComment function to handle -1 (Unknown) indentation
  • Verify the test passes
  • Run format test suite to ensure no regressions
  • Run full test suite to ensure no broader regressions
Original prompt

This section details on the original issue you should resolve

<issue_title>Panic formatting chained method call</issue_title>
<issue_description>## Stack trace

panic handling request textDocument/formatting strings: negative Repeat count goroutine 473 [running]:
runtime/debug.Stack()
	runtime/debug/stack.go:26 +0x64
github.com/microsoft/typescript-go/internal/lsp.(*Server).recover(0x14000000f00, 0x14001b29110)
	github.com/microsoft/typescript-go/internal/lsp/server.go:539 +0x44
panic({0x10323fae0?, 0x103455c10?})
	runtime/panic.go:783 +0x120
strings.Repeat({0x1031b4db8?, 0x140030cf970?}, 0x14001b64a98?)
	strings/strings.go:628 +0x574
github.com/microsoft/typescript-go/internal/format.getIndentationString(0xffffffffffffffff, 0x102c74d04?)
	github.com/microsoft/typescript-go/internal/format/span.go:976 +0x54
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).insertIndentation(0x140006beff0, 0xd, 0xffffffffffffffff, 0x0)
	github.com/microsoft/typescript-go/internal/format/span.go:859 +0x3c
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).consumeTokenAndAdvanceScanner.func1({{0x2a34570?, 0x140?}, 0x2?})
	github.com/microsoft/typescript-go/internal/format/span.go:1059 +0x2c
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).indentTriviaItems(0x140006beff0, {0x1400239d2f0?, 0x1d?, 0x1400017f398?}, 0xffffffffffffffff, 0x98?, 0x14001b64ba0)
	github.com/microsoft/typescript-go/internal/format/span.go:900 +0x94
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).consumeTokenAndAdvanceScanner(0x140006beff0, {{0x1400239d2f0, 0x4, 0x4}, {{0x1f, 0x20}, 0x18}, {0x0, 0x0, 0x0}}, ...)
	github.com/microsoft/typescript-go/internal/format/span.go:1058 +0x2d4
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).processChildNode(0x140006beff0, 0x1400017f398, 0x14002361af0?, 0x1400032a9c0?, 0x14003394dd8?, 0x140006127e0, 0xffffffffffffffff, 0x1400017f398, 0x14002a34570, 0x0, ...)
	github.com/microsoft/typescript-go/internal/format/span.go:389 +0x2d4
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).execute.func1(0x140006127e0)
	github.com/microsoft/typescript-go/internal/format/span.go:230 +0x54
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitNode(0x14003394f38?, 0x2f51b60?)
	github.com/microsoft/typescript-go/internal/ast/visitor.go:51 +0x30
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).visitNode(0x103b5dd58?, 0x30?)
	github.com/microsoft/typescript-go/internal/ast/visitor.go:198 +0x44
github.com/microsoft/typescript-go/internal/ast.(*PropertyAccessExpression).VisitEachChild(0x1400017f398, 0x1400032aae0)
	github.com/microsoft/typescript-go/internal/ast/ast.go:6716 +0x58
github.com/microsoft/typescript-go/internal/ast.(*Node).VisitEachChild(...)
	github.com/microsoft/typescript-go/internal/ast/ast.go:245
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).executeProcessNodeVisitor(0x140006beff0, 0x102c74d04?, 0x14003394e98?, 0x102f3d370?, 0x0?)
	github.com/microsoft/typescript-go/internal/format/span.go:523 +0x6c
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).processNode(0x140006beff0, 0x1400017f398, 0x140006d0f08, 0x0, 0x0, 0xffffffffffffffff, 0x4)
	github.com/microsoft/typescript-go/internal/format/span.go:620 +0x120
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).processChildNode(0x140006beff0, 0x1400017f448, 0x14000968a20?, 0x102ae925c?, 0x140033950d8?, 0x1400017f398, 0xffffffffffffffff, 0x1400017f448, 0x14002a34540, 0x0, ...)
	github.com/microsoft/typescript-go/internal/format/span.go:413 +0x5a8
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).execute.func1(0x1400017f398)
	github.com/microsoft/typescript-go/internal/format/span.go:230 +0x54
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).VisitNode(0x103b5dd58?, 0x2030cf8b0?)
	github.com/microsoft/typescript-go/internal/ast/visitor.go:51 +0x30
github.com/microsoft/typescript-go/internal/ast.(*NodeVisitor).visitNode(0x500000000?, 0x12ab97e80?)
	github.com/microsoft/typescript-go/internal/ast/visitor.go:198 +0x44
github.com/microsoft/typescript-go/internal/ast.(*CallExpression).VisitEachChild(0x1400017f448, 0x1400032aae0)
	github.com/microsoft/typescript-go/internal/ast/ast.go:6827 +0x30
github.com/microsoft/typescript-go/internal/ast.(*Node).VisitEachChild(...)
	github.com/microsoft/typescript-go/internal/ast/ast.go:245
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).executeProcessNodeVisitor(0x140006beff0, 0x102c74d04?, 0x14003395228?, 0x102f3d370?, 0x0?)
	github.com/microsoft/typescript-go/internal/format/span.go:523 +0x6c
github.com/microsoft/typescript-go/internal/format.(*formatSpanWorker).processNode(0x140006beff0, 0x1400017f448, 0x140006d0f08, 0x0, 0x0, 0xffffffffffffffff, 0x4)
	github.com/microsoft/typescript-go/internal/format/span.go:620 +0x120
github.com/microsoft/typesc...

</details>

Fixes microsoft/typescript-go#1928

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://survey3.medallia.com/?EAHeSx-AP01bZqG0Ld9QLQ).

Copilot AI and others added 2 commits October 22, 2025 18:46
The issue was that getIndentationForComment was not handling the case where
tokenIndentation is -1 (Unknown). In the TypeScript source, when tokenIndentation
is not Unknown, it returns tokenIndentation, otherwise it returns the base indentation.

This fixes the panic that occurred when formatting code like:
```
foo
  .bar()
  // A second call
  .baz();
```

Co-authored-by: jakebailey <[email protected]>
@jakebailey
Copy link
Member

Same as #1929

@jakebailey jakebailey closed this Oct 22, 2025
Copilot AI requested a review from jakebailey October 22, 2025 18:54
Copilot finished work on behalf of jakebailey October 22, 2025 18:54
@jakebailey jakebailey deleted the copilot/fix-formatting-chained-method-call 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.

2 participants