-
Notifications
You must be signed in to change notification settings - Fork 664
Fix comment formatting bug that mangled */
tokens
#1401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: jakebailey <[email protected]>
Co-authored-by: jakebailey <[email protected]>
*/
tokens
Co-authored-by: jakebailey <[email protected]>
…ormatting pass Co-authored-by: DanielRosenwasser <[email protected]>
You can keep trying to coerce it into generating a functional test (but if we're honest, enabling Somewhere in the formatting scanner/worker there's an off-by-one error in the position returned for the intra-comment whitespace span. Likely because we're taking the full comment end position and mistakenly adding a In any case, this is probably easier to find by stepping through the formatter in a debugger than by trying to just read the code. |
You're absolutely right - my approach was just a band-aid fix. I found the real issue: the TypeScript source has special handling for asterisks in comments that reduces the column by 1 when the first non-whitespace character is |
Co-authored-by: weswigham <[email protected]>
The off-by-one error was fixed elsewhere (line 925), so the special handling for asterisks in comment lines is no longer needed. Co-authored-by: jakebailey <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a critical bug in the auto-formatter that was corrupting multi-line comments and adjacent code. The formatter was incorrectly replacing */
tokens with /
and subsequently mangling nearby tokens like async
becoming sync
.
- Fixed off-by-one error in line start position calculation within
indentMultilineComment
function - Added comprehensive tests to verify comment preservation and prevent regression
- Ensured formatting stability across multiple passes
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
internal/format/span.go | Fixed line start calculation bug by changing [line] to [line+1] in comment processing |
internal/format/comment_test.go | Added new test file with comprehensive formatting tests for multi-line comments |
Comments suppressed due to low confidence (1)
internal/format/comment_test.go:50
- The test assertion uses a very specific string pattern that may not catch all variations of the corruption. Consider testing for the presence of malformed comment endings more broadly or testing that the comment structure remains valid.
assert.Check(t, !contains(firstFormatted, "*/\n /"), "should not corrupt */ to /")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, much better now. The test is still... questionable (an exact match check would be better rather than asserting all these random things, and what's with the contains
helper), but it's good enough. It'll get deleted when we enable formatting fourslash tests, probably.
The auto-formatter was incorrectly mangling multi-line comments, causing dangerous code corruption. When formatting code like:
The formatter would produce:
And on subsequent formatting passes, it would further corrupt the code:
This was a critical bug that:
*/
with just/
, breaking comment syntaxasync
becomingsync
Root Cause
The issue was in the
indentMultilineComment
function ininternal/format/span.go
:scanner.GetLineStarts(w.sourceFile)[line]
instead ofscanner.GetLineStarts(w.sourceFile)[line+1]
when processing comment lines*
, the function was replacing too many characters, including the*
itselfFix
*
, only replace the whitespace before it, not the*
itselfTesting
Added comprehensive tests that verify:
*/
token is not mangledasync
are not corruptedAll existing format and language server tests continue to pass, ensuring no regressions.
Fixes #1400.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.