From 4b3f7117460f64ebe2e3d2337841aed89105c677 Mon Sep 17 00:00:00 2001 From: Eric Wheeler Date: Fri, 21 Mar 2025 19:41:41 -0700 Subject: [PATCH] fix: enforce newlines between diff section separators Require newlines between diff section markers (SEARCH, ======, REPLACE) to prevent content confusion when searching input contains separator markers. Error message mentions required marker newlines. Signed-off-by: Eric Wheeler --- .../__tests__/multi-search-replace.test.ts | 2 -- .../diff/strategies/multi-search-replace.ts | 35 +++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/core/diff/strategies/__tests__/multi-search-replace.test.ts b/src/core/diff/strategies/__tests__/multi-search-replace.test.ts index 8fc16d2303..20bb6e8f7e 100644 --- a/src/core/diff/strategies/__tests__/multi-search-replace.test.ts +++ b/src/core/diff/strategies/__tests__/multi-search-replace.test.ts @@ -980,7 +980,6 @@ function test() { :end_line:4 ------- ======= - // End of file >>>>>>> REPLACE` @@ -990,7 +989,6 @@ function test() { expect(result.content).toBe(`function test() { return true; } - // End of file`) } }) diff --git a/src/core/diff/strategies/multi-search-replace.ts b/src/core/diff/strategies/multi-search-replace.ts index 238d218e62..bf80f09052 100644 --- a/src/core/diff/strategies/multi-search-replace.ts +++ b/src/core/diff/strategies/multi-search-replace.ts @@ -234,16 +234,47 @@ Only use a single line of '=======' between search and replacement content, beca } } + /* + Regex parts: + + 1. (?:^|\n) +   Ensures the first marker starts at the beginning of the file or right after a newline. + + 2. (?>>>>>> REPLACE)(?=\n|$) +   Matches the final “>>>>>>> REPLACE” marker on its own line (and requires a following newline or the end of file). + */ + let matches = [ ...diffContent.matchAll( - /(?>>>>>> REPLACE/g, + /(?:^|\n)(?>>>>>> REPLACE)(?=\n|$)/g, ), ] if (matches.length === 0) { return { success: false, - error: `Invalid diff format - missing required sections\n\nDebug Info:\n- Expected Format: <<<<<<< SEARCH\\n:start_line: start line\\n:end_line: end line\\n-------\\n[search content]\\n=======\\n[replace content]\\n>>>>>>> REPLACE\n- Tip: Make sure to include start_line/end_line/SEARCH/REPLACE sections with correct markers`, + error: `Invalid diff format - missing required sections\n\nDebug Info:\n- Expected Format: <<<<<<< SEARCH\\n:start_line: start line\\n:end_line: end line\\n-------\\n[search content]\\n=======\\n[replace content]\\n>>>>>>> REPLACE\n- Tip: Make sure to include start_line/end_line/SEARCH/=======/REPLACE sections with correct markers on new lines`, } } // Detect line ending from original content