Skip to content

fix(snapshot): run git from worktree, not launch directory#27737

Open
danfry1 wants to merge 2 commits into
anomalyco:devfrom
danfry1:fix/snapshot-subdirectory-cwd
Open

fix(snapshot): run git from worktree, not launch directory#27737
danfry1 wants to merge 2 commits into
anomalyco:devfrom
danfry1:fix/snapshot-subdirectory-cwd

Conversation

@danfry1
Copy link
Copy Markdown

@danfry1 danfry1 commented May 15, 2026

Issue for this PR

Closes #27688

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Snapshot git operations ran with cwd set to the directory opencode was launched from (state.directory), while --git-dir/--work-tree already pointed at the repo root. When those two are different (running from a subdirectory of the worktree), ls-files returned worktree-relative pathspecs that were then fed back into git add with cwd=subdir, so git tried to resolve src/foo.txt under subdir/src/foo.txt and the snapshot silently failed.

This switches the remaining git calls and the related path.join to use state.worktree, matching what restore/revert/excludes/diff already do. It also drops the now-unused state.directory field.

How did you verify your code works?

8 new regression tests in test/snapshot/snapshot.test.ts use provideInstance(subdir) so directory != worktree. They cover patch, diff/diffFull, gitignore, drop of staged-then-later-ignored files, large-file skip, restore, and a linked worktree (.git file rather than directory) launched from a subdir. I confirmed each one fails against the pre-fix source.

  • bun test test/snapshot/snapshot.test.ts from packages/opencode: 60 pass / 1 skip / 0 fail
  • bun typecheck from packages/opencode

Manual repro before the fix: init a git repo, cd subdir && opencode ., edit a file, look for pathspec ... did not match in the snapshot warnings.

Screenshots / recordings

N/A (not a UI change).

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

When opencode is launched from a subdirectory of a git worktree,
state.directory points at the subdirectory while git is told the
worktree via --work-tree. Pathspecs from ls-files came back
worktree-relative but were then passed to subsequent git invocations
with cwd=subdir, so git tried to resolve src/foo.txt under subdir/src/
and the snapshot silently failed.

Use state.worktree consistently for cwd and path joins in this file so
operations are anchored to the same root as --work-tree.

Fixes anomalyco#27688
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels May 15, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Snapshot fails when opencode is opened in a git subdirectory (snapshot service cwd mismatch)

1 participant