From 37d6464764ec3c5264ea31ddb45dbbc86d5f7c9d Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Tue, 21 Jul 2026 21:10:50 -0500 Subject: [PATCH] fix(core): preserve patch parser leniency --- packages/core/test/patch.test.ts | 21 ++++++++++++++------- packages/util/src/patch.ts | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/core/test/patch.test.ts b/packages/core/test/patch.test.ts index f0b7a981b9..67d37e411c 100644 --- a/packages/core/test/patch.test.ts +++ b/packages/core/test/patch.test.ts @@ -185,6 +185,19 @@ describe("Patch", () => { ]) }) + test("allows an end-of-file marker before an explicit chunk", () => { + expect( + parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n@@\n-old\n+new\n*** End Patch"), + ).toEqual([ + { + type: "update", + path: "file.txt", + movePath: undefined, + chunks: [{ oldLines: ["old"], newLines: ["new"], changeContext: undefined }], + }, + ]) + }) + test("derives fuzzy line updates while preserving BOM", () => { const update = Patch.derive("update.txt", [{ oldLines: [" old "], newLines: ["new"] }], "\uFEFFold\n") expect(update).toEqual({ content: "new\n", bom: true }) @@ -388,16 +401,10 @@ describe("Patch", () => { ), ).toThrow("Invalid hunk at line 2: Update file hunk for path 'old.txt' is empty") expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n*** End Patch")).toThrow( - "Invalid hunk at line 3: Update hunk does not contain any lines", + "Invalid hunk at line 2: Update file hunk for path 'file.txt' is empty", ) }) - test("rejects an end-of-file marker before update lines", () => { - expect(() => - parse("*** Begin Patch\n*** Update File: file.txt\n*** End of File\n@@\n-old\n+new\n*** End Patch"), - ).toThrow("Invalid hunk at line 3: Update hunk does not contain any lines") - }) - test("rejects an empty update chunk", () => { expect(() => parse("*** Begin Patch\n*** Update File: file.txt\n@@\n*** End Patch")).toThrow( "Invalid hunk at line 4: Update hunk does not contain any lines", diff --git a/packages/util/src/patch.ts b/packages/util/src/patch.ts index f4be526152..5744a108f3 100644 --- a/packages/util/src/patch.ts +++ b/packages/util/src/patch.ts @@ -167,7 +167,7 @@ function parseUpdate( } if (updateLine === "*** End of File") { const chunk = chunks.at(-1) - if (!chunk || (chunk.oldLines.length === 0 && chunk.newLines.length === 0)) { + if (chunk && chunk.oldLines.length === 0 && chunk.newLines.length === 0) { return { error: new InvalidHunkError({ line: updateLine,