Skip to content

Commit 97e9fe3

Browse files
cliffhallclaude
andcommitted
fix(filesystem): cut long lines in the no-match error; number an EOF blank line right
Copilot round 2 on #5379. A quoted file line is cut at 500 characters, marked, and the retry hint then says to read the file, so a one-line minified file cannot make the error as large as the file. A missing line past the end of the file is detected against the file's line count, so an extra blank line in oldText after the file's last line is reported as that line. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent bf689d7 commit 97e9fe3

2 files changed

Lines changed: 46 additions & 8 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,30 @@ describe("describeEditMismatch", () => {
169169
);
170170
});
171171

172+
it("reports an extra blank line at the end of the file at its own line", () => {
173+
const message = describeEditMismatch("alpha\n", "alpha\n\n", 0, 1);
174+
expect(message).toContain("(lines 1-1 of 1):\n1: alpha");
175+
expect(message).toContain(
176+
'oldText line 2 ("") runs past the end of the file, which ends at line 1.',
177+
);
178+
});
179+
180+
it("cuts a long file line, in the region and in the difference", () => {
181+
const long = "x".repeat(1_000_000);
182+
const message = describeEditMismatch(`${long}\n`, "xxxx yyyy", 0, 1);
183+
expect(message.length).toBeLessThan(2_000);
184+
expect(message).toContain(
185+
`1: ${"x".repeat(500)} [... cut: the line has 1000000 characters]`,
186+
);
187+
expect(message).toContain(
188+
` file: "${"x".repeat(500)} [... cut: the line has 1000000 characters]"`,
189+
);
190+
expect(message).toContain(
191+
"Some lines above were cut. Read the file to copy oldText from it exactly.",
192+
);
193+
expect(message).not.toContain("without the line-number prefixes");
194+
});
195+
172196
it("caps the quoted region at 30 lines", () => {
173197
const content = lines(50).join("\n");
174198
const oldText = [...lines(39), "line 40 changed"].join("\n");

‎src/filesystem/lib.ts‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,9 @@ function reindentLine(
739739
const MISMATCH_WORK_LIMIT = 10_000_000;
740740
const MISMATCH_CONTEXT_LINES = 2;
741741
const MISMATCH_MAX_REGION_LINES = 30;
742+
// A quoted file line is cut at this many characters, so a long line (minified
743+
// code, one-line JSON) cannot blow the error up to the size of the file.
744+
const MISMATCH_MAX_LINE_CHARS = 500;
742745

743746
interface LineGrams {
744747
grams: Map<string, number>;
@@ -852,9 +855,18 @@ export function describeEditMismatch(
852855
best + oldLines.length + MISMATCH_CONTEXT_LINES,
853856
start + MISMATCH_MAX_REGION_LINES,
854857
);
858+
let clipped = false;
859+
const clip = (line: string): string => {
860+
if (line.length <= MISMATCH_MAX_LINE_CHARS) return line;
861+
clipped = true;
862+
return (
863+
line.slice(0, MISMATCH_MAX_LINE_CHARS) +
864+
` [... cut: the line has ${line.length} characters]`
865+
);
866+
};
855867
const numbered = fileLines
856868
.slice(start, end)
857-
.map((line, k) => `${start + k + 1}: ${line}`);
869+
.map((line, k) => `${start + k + 1}: ${clip(line)}`);
858870
const after = editIndex > 0 ? ", after the earlier edits in this call" : "";
859871
parts.push(
860872
`Closest text in the file (lines ${start + 1}-${end} of ` +
@@ -868,22 +880,24 @@ export function describeEditMismatch(
868880
0,
869881
oldLines.findIndex(
870882
(line, j) =>
871-
best + j >= fileLines.length ||
872-
line.trim() !== fileLines[best + j].trim(),
883+
best + j >= lineCount || line.trim() !== fileLines[best + j].trim(),
873884
),
874885
);
875886
parts.push(
876887
best + differs >= lineCount
877-
? `oldText line ${differs + 1} (${JSON.stringify(oldLines[differs])}) ` +
888+
? `oldText line ${differs + 1} (${JSON.stringify(clip(oldLines[differs]))}) ` +
878889
`runs past the end of the file, which ends at line ${lineCount}.`
879890
: `First difference, oldText line ${differs + 1} against file line ` +
880891
`${best + differs + 1}:\n` +
881-
` oldText: ${JSON.stringify(oldLines[differs])}\n` +
882-
` file: ${JSON.stringify(fileLines[best + differs])}`,
892+
` oldText: ${JSON.stringify(clip(oldLines[differs]))}\n` +
893+
` file: ${JSON.stringify(clip(fileLines[best + differs]))}`,
883894
);
884895
parts.push(
885-
"Retry with oldText copied exactly from the file, without the " +
886-
"line-number prefixes.",
896+
clipped
897+
? "Some lines above were cut. Read the file to copy oldText from it " +
898+
"exactly."
899+
: "Retry with oldText copied exactly from the file, without the " +
900+
"line-number prefixes.",
887901
);
888902
return parts.join("\n\n");
889903
}

0 commit comments

Comments
 (0)