Fix content leakage in applyPatch when trailing newline counts differ#4668
Open
ShehabSherif0 wants to merge 3 commits intomicrosoft:mainfrom
Open
Fix content leakage in applyPatch when trailing newline counts differ#4668ShehabSherif0 wants to merge 3 commits intomicrosoft:mainfrom
ShehabSherif0 wants to merge 3 commits intomicrosoft:mainfrom
Conversation
…ewline counts differ The replace range used lines.length (new content line count) instead of textDocument.lineCount (original), leaving a gap of original lines between the replace end and the delete start when originalTrailing > newTrailing. Fix: Build adjusted content with trailing newlines appended, then replace the entire original document in a single operation. This matches the correct pattern already used in codeMapper.ts. Fixes microsoft#4667
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a correctness issue in the apply_patch tool where shortening a file could leave (“leak”) original trailing lines in the final document when the original and new contents have different trailing newline counts.
Changes:
- Replace the entire original text document range (based on the original document’s
lineCount) instead of only replacing up to the new content’s line count. - Normalize the replacement content by appending trailing newlines to match the original document’s trailing empty-line count, avoiding separate insert/delete edits.
- Add test: verify original content does not leak when trailing newline counts differ (the AI shortens file, LLM omits trailing newline) - Optimize: use '\n'.repeat() instead of loop for trailing adjustment
Use ALPHA/BETA/GAMMA instead of A/B/C in the trailing newline regression test. Short single-character content triggered the parser edit-distance fuzzy matching at the wrong position, causing false test failures unrelated to the fix under test. Also add CRLF normalization when reading the fixture for cross-platform safety.
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.
Fixes #4667
Problem
generateUpdateTextDocumentEditinapplyPatchTool.tsxuseslines.length(from the new content) as the replace range end instead of covering the entire original document. When the AI shortens a file and the trailing newline count differs between old and new content, original lines leak into the result.Root cause
The original code applies three separate TextEdits (all on original positions, atomically):
newContent\nat Position(lines.length + i, 0) for each missing trailing lineWhen
originalTrailing > newTrailing, a gap of(originalTrailing - newTrailing)original lines exists between the replace end and the delete start. These lines are neither replaced nor deleted.Example
Original:
"ALPHA\nBETA\nGAMMA\n"(lineCount=4, trailing=1). AI removes BETA and GAMMA:newContent = "ALPHA"(trailing=0).\nat Position(1,0)Original line 1 ("BETA") is in the gap, untouched by any edit. Result:
"ALPHA\nBETA\n"instead of"ALPHA\n".When it triggers
Cascading impact
turnEditedDocumentsstores the expected content, creating a divergence between the AI's view and the actual file state. Subsequent edits then operate on wrong assumptions.Fix
Build
adjustedContentwith trailing newlines appended, then replace the entire original document in a single operation:This replaces the prior three-operation approach (replace + insert loop + conditional delete) with a single replace that covers the full document. No gap is possible.
This matches the correct pattern already used in
codeMapper.ts.Testing
applyPatch.spec.tsx)"ALPHA\nBETA\nGAMMA\n", AI produces"ALPHA", asserts result is"ALPHA\n"not"ALPHA\nBETA\n")