fix(desktop): solve #5611 — orphaned worktrees from worktreeExists path mismatch
Root cause
worktreeExists() in apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts decided whether a worktree was still registered in git by comparing the caller's path against git worktree list --porcelain output with an exact string match:
const worktreePrefix = `worktree ${worktreePath}`;
return lines.some((line) => line.trim() === worktreePrefix);
Any semantically-equal but textually-different path reports the worktree as missing:
- a trailing slash (
/path/wt/) - a
./..segment - a symlinked parent — e.g. macOS
/var→/private/var, so a stored DB path and git's recorded path differ only by symlink resolution.
This is a false negative. worktreeExists() gates the delete flow (canDelete, canDeleteWorktree, and deleteWorktree in procedures/delete.ts). When it wrongly returns false:
deleteWorktreeruns its removal onlyif (exists), so it skips on-disk cleanup and the DB record is still deleted → the worktree directory (incl.node_modules) is orphaned on disk while git metadata lingers. This is exactly the symptom reported in #2863 (DB record gone ✅, directory + git metadata NOT removed ❌).- the UI surfaces a misleading "Worktree not found in git (may have been manually removed)" warning even though the worktree is present on disk, matching #4555 / #4521.
Notably, the sibling helper isWorktreeRegistered() in the same file already normalizes with resolve(), and procedures/delete.ts normalizes with realpathSync. worktreeExists() was the odd one out.
The fix
Add a normalizeWorktreePath() helper that resolves symlinks via realpathSync when the path exists (handling the macOS /var case) and falls back to resolve() (which collapses trailing slashes and ./.. segments) when it doesn't. Compare git's listed paths against the caller's path after normalizing both sides — the same approach already used by isWorktreeRegistered and delete.ts.
Tests
Added worktreeExists path normalization to git.test.ts. It creates a real repo + registered worktree and asserts worktreeExists returns true for the canonical path, a trailing-slash path, a .-segment path, and a symlinked-parent path.
- Before the fix: the trailing-slash / symlink assertions fail (reproducing the bug).
- After the fix: all 35 tests in the file pass.
$ bun test src/lib/trpc/routers/workspaces/utils/git.test.ts
35 pass, 0 fail
Closes #5611
Refs #2863, #4555, #4521
Summary by cubic
Fixes orphaned worktrees by normalizing paths in worktreeExists so deletions no longer skip on-disk cleanup. Prevents false "not found" warnings and aligns with the requirements in #5611.
- Bug Fixes
- Added
normalizeWorktreePath(usesrealpathSync, falls back toresolve) and applied it inworktreeExists. - Compare normalized paths from
git worktree list --porcelaininstead of exact string matches. - Added tests for trailing slashes, "." segments, and symlinked parents (e.g., macOS
/var→/private/var); all pass.
- Added