fix(core): rename instead of deleting when a patch only changes a file's case (#63655)

This commit is contained in:
Aiden Cline
2026-11-12 21:07:23 -06:00
committed by GitHub
Unverified
parent 3f073962bd
commit 2f8a867eae
2 changed files with 57 additions and 2 deletions
+23 -2
View File
@@ -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(
+34
View File
@@ -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* () {