From c5a4afeb4070edc294697bb5242aecf2a983d7b9 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Sun, 5 Oct 2025 03:52:35 -0400 Subject: [PATCH] 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. --- packages/opencode/src/snapshot/index.ts | 17 +++++++++++++++-- .../opencode/test/snapshot/snapshot.test.ts | 6 +++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/snapshot/index.ts b/packages/opencode/src/snapshot/index.ts index e4fed2d68..2f8661f5e 100644 --- a/packages/opencode/src/snapshot/index.ts +++ b/packages/opencode/src/snapshot/index.ts @@ -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) diff --git a/packages/opencode/test/snapshot/snapshot.test.ts b/packages/opencode/test/snapshot/snapshot.test.ts index ca74389b6..5849a2675 100644 --- a/packages/opencode/test/snapshot/snapshot.test.ts +++ b/packages/opencode/test/snapshot/snapshot.test.ts @@ -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])