core: fix snapshot revert to properly delete files when using relative paths

Fixes snapshot revert functionality so it correctly removes files that were
deleted between snapshots. Previously, revert would fail to delete files because
it was trying to delete relative paths (like 'file.txt') instead of absolute
paths ('/path/to/file.txt').

This ensures snapshot revert operations work reliably across all file types
and directory structures, maintaining consistent project state when rolling
back changes.
This commit is contained in:
Dax Raad 2025-10-05 03:52:35 -04:00
parent 7913f4a65d
commit c5a4afeb40
2 changed files with 18 additions and 5 deletions

View file

@ -10,7 +10,19 @@ import { Instance } from "../project/instance"
export namespace Snapshot {
const log = Log.create({ service: "snapshot" })
export function init() {}
export function init() {
Array.fromAsync(
new Bun.Glob("**/snapshot").scan({
absolute: true,
onlyFiles: false,
cwd: Global.Path.data,
}),
).then((files) => {
for (const file of files) {
fs.rmdir(file, { recursive: true })
}
})
}
export async function track() {
if (Instance.project.vcs !== "git") return
@ -101,7 +113,8 @@ export namespace Snapshot {
log.info("file existed in snapshot but checkout failed, keeping", { file })
} else {
log.info("file did not exist in snapshot, deleting", { file })
await fs.unlink(file).catch(() => {})
const absolutePath = path.join(Instance.worktree, file)
await fs.unlink(absolutePath).catch(() => {})
}
}
files.add(file)

View file

@ -549,7 +549,7 @@ test("revert should not delete files that existed but were deleted in snapshot",
await Bun.write(`${tmp.path}/a.txt`, "recreated content")
const patch = await Snapshot.patch(snapshot2!)
expect(patch.files).toContain(`${tmp.path}/a.txt`)
expect(patch.files).toContain(`a.txt`)
await Snapshot.revert([patch])
@ -573,8 +573,8 @@ test("revert preserves file that existed in snapshot when deleted then recreated
await Bun.write(`${tmp.path}/newfile.txt`, "new")
const patch = await Snapshot.patch(snapshot!)
expect(patch.files).toContain(`${tmp.path}/existing.txt`)
expect(patch.files).toContain(`${tmp.path}/newfile.txt`)
expect(patch.files).toContain(`existing.txt`)
expect(patch.files).toContain(`newfile.txt`)
await Snapshot.revert([patch])