Fix out-of-range failure when formatting a stale range - #2806
Merged
Aasim Khan (aasimkhan30) merged 2 commits intoSep 2, 2026
Conversation
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 started reviewing on behalf of
Aasim Khan (aasimkhan30)
September 1, 2026 17:25
View session
Contributor
There was a problem hiding this comment.
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
ScriptFileline bounds (GetLine,ValidatePosition) to disallowFileLines.Count + 1and avoid raw list indexer exceptions. - Add
ScriptFile.IsRangeValidand 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 started reviewing on behalf of
Aasim Khan (aasimkhan30)
September 2, 2026 04:46
View session
Contributor
There was a problem hiding this comment.
🟢 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
Hai Cao (caohai)
approved these changes
Sep 2, 2026
Aasim Khan (aasimkhan30)
deleted the
aasim/fix/format-range-out-of-range
branch
September 2, 2026 22:45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes an out-of-range error during SQL range formatting when the service has a stale document buffer.
ScriptFile.Code Changes Checklist
dotnet test)Testing
Microsoft.SqlTools.ServiceLayer.UnitTests: 1949 passedMicrosoft.SqlTools.LanguageService.UnitTests: 81 passedReviewers: Please read our reviewer guidelines