Skip to content

fix(worktree): resolve existing worktrees on Windows#6

Open
logan-jl-cc wants to merge 1 commit into
lovstudio:mainfrom
logan-jl-cc:fix/worktree-getworktree-windows-separator
Open

fix(worktree): resolve existing worktrees on Windows#6
logan-jl-cc wants to merge 1 commit into
lovstudio:mainfrom
logan-jl-cc:fix/worktree-getworktree-windows-separator

Conversation

@logan-jl-cc

Copy link
Copy Markdown

Summary

Fixes getWorktree() returning undefined on Windows, so Yoda detects an already-checked-out worktree instead of re-provisioning every time.

Root cause

getWorktree() compared the git-reported checked-out path against the pool's realpath with a bare startsWith:

if (checkedOutPath.startsWith(realPoolPath)) return checkedOutPath;

On Windows, git worktree list --porcelain emits forward-slash paths (C:/Users/.../wt-pool-xxx/branch) while fs.realpath returns native backslashes (C:\Users\...\wt-pool-xxx). The prefix check always failed → getWorktree() returned undefined.

Fix

Normalize both sides before the prefix compare, and require a directory boundary so the pool prefix can't match an unrelated sibling:

const normalizedTarget = path.normalize(checkedOutPath);
const normalizedPool = path.normalize(realPoolPath);
if (
  normalizedTarget === normalizedPool ||
  normalizedTarget.startsWith(normalizedPool + path.sep)
) {
  return normalizedTarget;
}

Returns the normalized path so callers get consistent native separators. On POSIX, path.normalize is effectively a no-op, so behavior is unchanged.

Verification

The worktree-service "falls back to the flattened branch name" test (which exercises getWorktree for an already-checked-out branch) now passes on Windows; all 8 worktree-service tests pass. No regression on POSIX — path.normalize preserves forward-slash paths.

🤖 Generated with Claude Code

getWorktree() compared the git-reported checked-out path against the pool's
realpath with a bare startsWith. On Windows, git emits forward-slash paths
(C:/Users/.../wt) while realpath uses native backslashes (C:\Users\...\wt),
so the prefix check always failed and getWorktree() returned undefined — Yoda
could not detect an already-checked-out worktree and re-provisioned each time.

Normalize both sides before the prefix compare and require a directory boundary
(pool + sep) so the prefix can't match an unrelated sibling. Return the
normalized path so callers get consistent native separators.

Co-Authored-By: Claude <noreply@anthropic.com>
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.

1 participant