Skip to content

Fix panic in LSP formatting with multi-byte characters and trailing newlines #1391

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 11, 2025

This PR fixes a panic that occurs when formatting files from VSCode if the file contains multi-byte characters (e.g., β†’, 🐈 or Japanese characters like ζ—₯本θͺž) and ends with 2+ newlines.

Problem

The panic occurred in the PositionToLineAndCharacter function in internal/ls/converters.go when trying to slice text with bounds [start:position] where position could exceed the actual text length:

panic: runtime error: slice bounds out of range [:10] with length 9

Root Cause

When the formatter generates text edits for files with multi-byte characters and trailing newlines, it can create ranges that extend beyond the actual text length. The PositionToLineAndCharacter function didn't handle this case properly, leading to a "slice bounds out of range" panic.

Solution

Added a bounds check to ensure that position never exceeds the actual text length before attempting to slice the text:

// Ensure position doesn't exceed text length to avoid slice bounds errors
text := script.Text()
textLen := core.TextPos(len(text))
position = min(position, textLen)

This ensures that even if the formatter or other components pass positions beyond the text length, the converter will handle them gracefully by treating them as positions at the end of the text.

Testing

  • Added comprehensive tests that specifically reproduce the original issue
  • Verified the fix handles positions at and beyond text length
  • Added a specific test case for the exact scenario from the issue
  • All existing tests continue to pass

The fix is minimal, safe, and maintains backward compatibility while resolving the panic.

Fixes #1389.


πŸ’¬ Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] lsp(vscode): panic when formatting a file with multi-byte characters and trailing 2+ newlines Fix panic in LSP formatting with multi-byte characters and trailing newlines Jul 11, 2025
@Copilot Copilot AI requested a review from jakebailey July 11, 2025 17:24
Copilot finished work on behalf of jakebailey July 11, 2025 17:24
@jakebailey
Copy link
Member

Delete all of the compiler tests; they are not needed for editor related PRs.

Copy link
Contributor Author

Copilot AI commented Jul 11, 2025

Delete all of the compiler tests; they are not needed for editor related PRs.

Done. All compiler tests have been removed from the PR. The language server tests remain as they are appropriate for testing the LSP formatting fix. (0f6e9fc)

Copilot finished work on behalf of jakebailey July 11, 2025 17:36
@Copilot Copilot AI requested a review from jakebailey July 11, 2025 17:45
Copilot finished work on behalf of jakebailey July 11, 2025 17:45
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.

lsp(vscode): panic when formatting a file with multi-byte characters and trailing 2+ newlines
2 participants