From 4a608b202669d57ead254dece8d951ca51a6dd15 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Mon, 18 May 2026 09:52:50 +0200 Subject: [PATCH] snapshot: fix cwd for git commands in subdirs When opencode is launched from a git repository subdirectory, the snapshot service passes file paths relative to the worktree root but sets cwd to the launch directory. Git cannot resolve the paths and fails with "did not match any files". Use the worktree root as cwd so paths resolve correctly regardless of which subdirectory opencode is started from. Fix #27688 Close #27737 --- packages/opencode/src/snapshot/index.ts | 94 ++++++++++++------- .../opencode/test/snapshot/snapshot.test.ts | 93 ++++++++++++++++++ 2 files changed, 155 insertions(+), 32 deletions(-) diff --git a/packages/opencode/src/snapshot/index.ts b/packages/opencode/src/snapshot/index.ts index f974a457ad7b..a7a45110d781 100644 --- a/packages/opencode/src/snapshot/index.ts +++ b/packages/opencode/src/snapshot/index.ts @@ -85,6 +85,10 @@ export const layer: Layer.Layer ["--git-dir", state.gitdir, "--work-tree", state.worktree, ...cmd] const feed = (list: string[]) => list.join("\0") + "\0" + const feedSpec = (list: string[]) => feed(list.map((item) => `:(top,literal)${item}`)) + + const scope = path.relative(state.worktree, state.directory).replaceAll("\\", "/") + const spec = scope ? `:(top,literal)${scope}` : "." const git = Effect.fnUntraced( function* (cmd: string[], opts?: { cwd?: string; env?: Record; stdin?: string }) { @@ -122,7 +126,7 @@ export const layer: Layer.Layer fs - .stat(path.join(state.directory, item)) + .stat(path.join(state.worktree, item)) .pipe(Effect.catch(() => Effect.void)) .pipe( Effect.map((stat) => { @@ -306,9 +310,9 @@ export const layer: Layer.Layer() const statuses = yield* git( - [...quote, ...args(["diff", "--no-ext-diff", "--name-status", "--no-renames", from, to, "--", "."])], - { cwd: state.directory }, + [...quote, ...args(["diff", "--no-ext-diff", "--name-status", "--no-renames", from, to, "--", spec])], + { cwd: state.worktree }, ) for (const line of statuses.text.trim().split("\n")) { @@ -649,9 +679,9 @@ export const layer: Layer.Layer