Skip to content

fix: resolve worktree path from ProjectRoot, not WorkDir (#174)#218

Merged
marcus merged 1 commit intomainfrom
shrike/174-worktree-subfolder-fix
Mar 1, 2026
Merged

fix: resolve worktree path from ProjectRoot, not WorkDir (#174)#218
marcus merged 1 commit intomainfrom
shrike/174-worktree-subfolder-fix

Conversation

@marcus
Copy link
Owner

@marcus marcus commented Mar 1, 2026

Problem

When sidecar is started from a subfolder of a git repo (e.g. cd myrepo/src && sidecar), WorkDir is set to that subfolder. Both doCreateWorktree (worktree.go) and fetchAndCreateWorktree (fetch_pr.go) computed the new worktree's parent as filepath.Dir(WorkDir), which resolves to the git root itself — placing the new worktree inside the main repo instead of beside it.

This caused git worktree add to fail with fatal: invalid reference: <branch>: exit status 128 because the path conflicted with existing repo contents.

Fix

Use ProjectRoot (already correctly resolved to the main worktree path via GetMainWorktreePath in main.go) as the anchor for the parent-dir calculation. Falls back to WorkDir if ProjectRoot is empty.

Before (broken when started from subfolder):

WorkDir   = /repos/myrepo/subfolder
parentDir = /repos/myrepo        ← git root, not its parent!
wtPath    = /repos/myrepo/feat   ← INSIDE the main repo

After (fixed):

ProjectRoot = /repos/myrepo
parentDir   = /repos             ← parent of main repo ✓
wtPath      = /repos/feat        ← sibling of main repo ✓

Testing

  • All existing tests pass (go test ./...)
  • Added TestWorktreePathResolvesFromProjectRoot regression test

Fixes #174

When sidecar is started from a git repo subfolder, WorkDir is set to
that subfolder (e.g. /repos/myrepo/subfolder). Both doCreateWorktree
and fetchAndCreateWorktree were computing the new worktree's parent
directory as filepath.Dir(WorkDir), which resolves to the git root
itself — placing the new worktree *inside* the main repo instead of
beside it.

Fix: use ProjectRoot (already correctly resolved to the main worktree
path via GetMainWorktreePath) instead. When ProjectRoot is empty fall
back to WorkDir for safety.

Adds a regression test documenting the path-computation invariant.

Fixes #174
@marcus marcus merged commit d95f96e into main Mar 1, 2026
2 checks passed
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.

Can not create worktree from subfolder

1 participant