From 2f8a867eae483570853fe7071a7a59a1bfc8f265 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Thu, 12 Nov 2026 21:07:23 -0600 Subject: [PATCH] fix(core): rename instead of deleting when a patch only changes a file's case (#63655) --- packages/core/src/tool/plugin/patch.ts | 25 +++++++++++++++++-- packages/core/test/tool-patch.test.ts | 34 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/packages/core/src/tool/plugin/patch.ts b/packages/core/src/tool/plugin/patch.ts index b8043dc4923..b957cfe0593 100644 --- a/packages/core/src/tool/plugin/patch.ts +++ b/packages/core/src/tool/plugin/patch.ts @@ -207,15 +207,36 @@ export const Plugin = { Effect.gen(function* () { const moveTarget = change.type === "update" ? change.moveTarget : undefined const target = moveTarget ?? change.target + // On macOS and Windows a move that only changes case targets the same file, so the new name already + // exists, and writing it then removing the old one would delete the file. Such a move writes in + // place and renames. Symlinks keep the write-and-remove path, which leaves their target alone. + const recased = + moveTarget !== undefined && + moveTarget.absolute.toLowerCase() === change.target.absolute.toLowerCase() && + (yield* environment.files.stat(change.target.absolute).pipe( + Effect.map((info) => info.type === "file"), + Effect.orElseSucceed(() => false), + )) && + (yield* environment.files.stat(moveTarget.absolute).pipe( + Effect.as(true), + Effect.orElseSucceed(() => false), + )) if (change.type === "delete") yield* environment.files .remove(change.target.absolute) .pipe(Effect.mapError((error) => fail(`Failed to delete ${change.target.resource}`, error))) if (change.type !== "delete") yield* environment.files - .write(target.absolute, new TextEncoder().encode(change.content)) + .write( + recased ? change.target.absolute : target.absolute, + new TextEncoder().encode(change.content), + ) .pipe(Effect.mapError((error) => fail(`Failed to write ${target.resource}`, error))) - if (moveTarget) + if (moveTarget && recased) + yield* environment.files + .move(change.target.absolute, moveTarget.absolute) + .pipe(Effect.mapError((error) => fail(`Failed to rename ${change.target.resource}`, error))) + if (moveTarget && !recased) yield* environment.files .remove(change.target.absolute) .pipe( diff --git a/packages/core/test/tool-patch.test.ts b/packages/core/test/tool-patch.test.ts index 2f64c3a079f..dd38220e383 100644 --- a/packages/core/test/tool-patch.test.ts +++ b/packages/core/test/tool-patch.test.ts @@ -457,6 +457,40 @@ describe("PatchTool", () => { ), ) + it.live("renames a file when the move only changes its case", () => + withTempTool((directory, registry) => + Effect.gen(function* () { + yield* Effect.promise(() => fs.writeFile(path.join(directory, "same.txt"), "before\n")) + expect( + yield* executeTool( + registry, + call("*** Begin Patch\n*** Update File: same.txt\n*** Move to: Same.txt\n@@\n-before\n+after\n*** End Patch"), + ), + ).toMatchObject({ status: "completed" }) + expect(yield* Effect.promise(() => fs.readdir(directory))).toEqual(["Same.txt"]) + expect(yield* Effect.promise(() => fs.readFile(path.join(directory, "Same.txt"), "utf8"))).toBe("after\n") + }), + ), + ) + + it.live("moves into a directory whose name differs only in case", () => + withTempTool((directory, registry) => + Effect.gen(function* () { + yield* Effect.promise(async () => { + await fs.mkdir(path.join(directory, "Foo")) + await fs.writeFile(path.join(directory, "Foo", "a.txt"), "before\n") + }) + expect( + yield* executeTool( + registry, + call("*** Begin Patch\n*** Update File: Foo/a.txt\n*** Move to: foo/a.txt\n@@\n-before\n+after\n*** End Patch"), + ), + ).toMatchObject({ status: "completed" }) + expect(yield* Effect.promise(() => fs.readFile(path.join(directory, "foo", "a.txt"), "utf8"))).toBe("after\n") + }), + ), + ) + it.live("moves a symlink without deleting its target", () => withTempTool((directory, registry) => Effect.gen(function* () {