Skip to content

Fix out-of-range failure when formatting a stale range - #2806

Merged
Aasim Khan (aasimkhan30) merged 2 commits into
mainfrom
aasim/fix/format-range-out-of-range
Sep 2, 2026
Merged

Fix out-of-range failure when formatting a stale range#2806
Aasim Khan (aasimkhan30) merged 2 commits into
mainfrom
aasim/fix/format-range-out-of-range

Conversation

@aasimkhan30

@aasimkhan30 Aasim Khan (aasimkhan30) commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes an out-of-range error during SQL range formatting when the service has a stale document buffer.

  • Correct line bounds in ScriptFile.
  • Skip range formatting when the requested range is outside the current buffer.
  • Add regression tests for position, range, and formatter behavior.

Code Changes Checklist

  • New or updated unit tests added
  • All existing tests pass (dotnet test)
  • Code follows contributing guidelines
  • Logging/telemetry updated if relevant
  • No protocol or behavioral regressions

Testing

  • Microsoft.SqlTools.ServiceLayer.UnitTests: 1949 passed
  • Microsoft.SqlTools.LanguageService.UnitTests: 81 passed
  • Integration tests not run (SQL instance required)

Reviewers: Please read our reviewer guidelines

ScriptFile.ValidatePosition accepted a line one past the end of the file
(FileLines.Count + 1) and then indexed FileLines[line - 1], so instead of
the intended "position is outside of file line range" error the caller got
a raw list indexer failure: "Index was out of range. Must be non-negative
and less than the size of the collection. (Parameter 'index')". GetLine had
the same off-by-one bound.

textDocument/rangeFormatting surfaced this to users. The formatter passes
the client's range straight to GetTextInRange, and this copy of a document
can lag the editor's (Workspace.GetFile loads from disk when it has not
seen a didOpen), so a range that is valid in the editor can end exactly one
line past this buffer. The exception failed the request and VS Code showed
it as an error notification.

Tighten the bound so an out-of-range line is reported as such, and have the
range formatter skip when the requested range is outside the buffer it
holds. Formatting stale text would rewrite the wrong span of the client's
document, so returning no edits is the safe outcome.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an out-of-range exception surfaced to VS Code users during textDocument/rangeFormatting when the server’s ScriptFile buffer is stale relative to the editor, ensuring the request safely no-ops instead of returning an error.

Changes:

  • Tighten ScriptFile line bounds (GetLine, ValidatePosition) to disallow FileLines.Count + 1 and avoid raw list indexer exceptions.
  • Add ScriptFile.IsRangeValid and use it in range formatting to skip formatting when the requested range falls outside the server buffer.
  • Add unit tests covering the off-by-one behavior and the new “skip stale range” behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
test/Microsoft.SqlTools.ServiceLayer.UnitTests/ServiceHost/ScriptFileTests.cs Adds regression tests ensuring “just past EOF” is reported as an out-of-range line (not a list index error) and validates IsRangeValid.
test/Microsoft.SqlTools.ServiceLayer.UnitTests/Formatter/TSqlFormatterServiceTests.cs Adds a range-formatting test ensuring stale/out-of-buffer ranges return 0 edits instead of failing.
src/Microsoft.SqlTools.LanguageService/Workspace/Contracts/ScriptFile.cs Fixes off-by-one validation and introduces IsRangeValid/GetMaxColumn helpers.
src/Microsoft.SqlTools.LanguageService/Formatter/TSqlFormatterService.cs Skips range formatting when the requested range is outside the current buffer to prevent incorrect edits and user-facing errors.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI review requested due to automatic review settings September 2, 2026 04:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes directly address the confirmed off-by-one failure mode, add a safe guard for stale client ranges, and include targeted unit tests for the new behavior.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@aasimkhan30
Aasim Khan (aasimkhan30) merged commit a1ce9fc into main Sep 2, 2026
7 checks passed
@aasimkhan30
Aasim Khan (aasimkhan30) deleted the aasim/fix/format-range-out-of-range branch September 2, 2026 22: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.

3 participants