Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/core/src/tool/plugin/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 34 additions & 0 deletions packages/core/test/tool-patch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand Down
Loading