Skip to content

Commit 9255dc5

Browse files
fix(diff): Handle line number stripping for deletions in apply_diff (RooCodeInc#2193)
The multi-search-replace diff strategy previously did not correctly strip line numbers (`number | `) from the SEARCH block when the REPLACE block was empty. This occurred because the condition for stripping required both blocks to consistently have line numbers. This prevented successful deletion operations when the SEARCH block content was copied from `read_file` output (which includes line numbers) and the REPLACE block was empty. This commit updates the line number stripping condition in `applyDiff` to also trigger if the SEARCH block has line numbers and the REPLACE block is empty or contains only whitespace, resolving the bug. Additionally, a new test case has been added to `multi-search-replace.test.ts` to specifically verify this deletion scenario. All tests now pass with this updated logic.
1 parent 7409c48 commit 9255dc5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/core/diff/strategies/__tests__/multi-search-replace.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,23 @@ function five() {
14861486
}`)
14871487
}
14881488
})
1489+
1490+
it("should delete a line when search block has line number prefix and replace is empty", async () => {
1491+
const originalContent = "line 1\nline to delete\nline 3"
1492+
const diffContent = `
1493+
<<<<<<< SEARCH
1494+
:start_line:2
1495+
:end_line:2
1496+
-------
1497+
2 | line to delete
1498+
=======
1499+
>>>>>>> REPLACE`
1500+
const result = await strategy.applyDiff(originalContent, diffContent)
1501+
expect(result.success).toBe(true)
1502+
if (result.success) {
1503+
expect(result.content).toBe("line 1\nline 3")
1504+
}
1505+
})
14891506
})
14901507

14911508
describe("insertion", () => {

src/core/diff/strategies/multi-search-replace.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,10 @@ Only use a single line of '=======' between search and replacement content, beca
301301
replaceContent = this.unescapeMarkers(replaceContent)
302302

303303
// Strip line numbers from search and replace content if every line starts with a line number
304-
if (everyLineHasLineNumbers(searchContent) && everyLineHasLineNumbers(replaceContent)) {
304+
if (
305+
(everyLineHasLineNumbers(searchContent) && everyLineHasLineNumbers(replaceContent)) ||
306+
(everyLineHasLineNumbers(searchContent) && replaceContent.trim() === "")
307+
) {
305308
searchContent = stripLineNumbers(searchContent)
306309
replaceContent = stripLineNumbers(replaceContent)
307310
}

0 commit comments

Comments
 (0)