Skip to content

Commit 0c79e06

Browse files
cliffhallclaude
andcommitted
fix(filesystem): start the closest-text search only at real file lines
Copilot round 3 on #5379. The empty element after a terminal newline could win as a starting position and report a near match as running past the end of the file. Starts are now real lines; the element is still compared within a window. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 97e9fe3 commit 0c79e06

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎src/filesystem/__tests__/edit-file-mismatch.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ describe("describeEditMismatch", () => {
193193
expect(message).not.toContain("without the line-number prefixes");
194194
});
195195

196+
it("never starts the closest text after the file's terminal newline", () => {
197+
const message = describeEditMismatch(
198+
"function f() {\nreturn value;\n}\n",
199+
"\nreturn vaLue;",
200+
0,
201+
1,
202+
);
203+
expect(message).toContain("(lines 1-3 of 3):");
204+
expect(message).not.toContain("runs past the end");
205+
expect(message).toContain("oldText line 1 against file line 1");
206+
});
207+
196208
it("caps the quoted region at 30 lines", () => {
197209
const content = lines(50).join("\n");
198210
const oldText = [...lines(39), "line 40 changed"].join("\n");

‎src/filesystem/lib.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,12 @@ export function describeEditMismatch(
822822

823823
// Every starting line is scored, including those near the end where fewer
824824
// lines than oldText has remain, so a near match at the end of the file is
825-
// still found. The earliest start wins a tie.
825+
// still found. The earliest start wins a tie. A start is always a real
826+
// line, never the empty element after a terminal newline.
826827
const fileGrams = fileLines.map(lineGrams);
827828
let best = 0;
828829
let bestScore = 0;
829-
for (let i = 0; i < fileLines.length; i++) {
830+
for (let i = 0; i < lineCount; i++) {
830831
const width = Math.min(oldLines.length, fileLines.length - i);
831832
let score = 0;
832833
for (let j = 0; j < width; j++) {

0 commit comments

Comments
 (0)