[Feature Request] Recover gracefully when current worktree directory has been removed
Problem
When a linked worktree is removed (i.e via wt remove or wt merge from another terminal or an agent action) while a shell is still in that directory, all wt commands fail:
❯ wt switch
✗ fatal: Unable to read current working directory: No such file or directory
This happens because Repository::current() calls git rev-parse --git-common-dir from . (the CWD), and the OS can't resolve a deleted directory. The user has to manually cd somewhere else before wt works again.
This is admittedly a minor annoyance.
Proposed behavior
When wt detects the CWD no longer exists:
- Read
$PWD(shells preserve the logical path in this variable even after the directory is deleted, whilegetcwd()fails) - Load the user's
worktree-pathtemplate. - Reverse-match the template against
$PWDto infer the primary worktree path - Print a helpful message:
Current directory was removed. Try: cd ~/git/orchestrator
For wt switch specifically, run the command as if it was invoked from the primary worktree instead of failing.
Approach: template-to-regex reverse matching
Convert the user's worktree-path template into a regex, match it against $PWD, and use filesystem validation to find the primary worktree.
recover(deleted_path, template):
expand ~ in template
convert template variables to regex capture groups
match regex against deleted_path
if template uses "{{ repo_path }}/../" (sibling layout):
# /../ is normalized away by OS, so capture "parent/repo_branch" as one string
# split repo_branch on the separator (., -, _ — extracted from template)
# try each split point left-to-right, check parent/candidate/.git exists
# first hit is the primary worktree
if template uses "{{ repo_path }}/..." (subdirectory layout):
# repo_path is captured directly — return it
Graceful fallback — returns None (generic error) for templates with Jinja2 control flow, value-transforming filters, or no repo_path variable.
Happy to submit a PR for this if there's interest, but no worries either way.
Thanks for the suggestion! This is a feature request rather than a bug, so I'll leave it for a maintainer to evaluate and prioritize.
yes I agree
should we auto-cd to the primary worktree? or is that a bit magic?
Happy to submit a PR for this if there's interest, but no worries either way.
most helpful is the ideas tbh, the actual coding is the easy part (how times have changed!)
Happy to submit a PR for this if there's interest, but no worries either way.
most helpful is the ideas tbh, the actual coding is the easy part (how times have changed!)
This got a full bellied laugh out of me. And furthermore... any offers to submit a PR aren't nearly as magnanimous as they used to be😂.
yes I agree
should we auto-cd to the primary worktree? or is that a bit magic?
My thought was only on wt switch since the user's intent is to cd in that case. Other commands would normally act on the current checked out worktree and a helpful error is the more appropriate in those cases imo.
yes I agree
should we auto-cd to the primary worktree? or is that a bit magic?
My thought was only on
wt switchsince the user's intent is to cd in that case. Other commands would normally act on the current checked out worktree and a helpful error is the more appropriate in those cases imo.
If the main repo can be resolved successfully non wt switch commands could suggest wt switch instead of a potentially long cd command.