worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

[Feature Request] Recover gracefully when current worktree directory has been removed

Open davidbeesley opened this issue 9 months ago • 7 comments

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:

  1. Read $PWD (shells preserve the logical path in this variable even after the directory is deleted, while getcwd() fails)
  2. Load the user's worktree-path template.
  3. Reverse-match the template against $PWD to infer the primary worktree path
  4. 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.

davidbeesley avatar Feb 19 '26 06:02 davidbeesley

Happy to submit a PR for this if there's interest, but no worries either way.

davidbeesley avatar Feb 19 '26 06:02 davidbeesley

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.

worktrunk-bot avatar Feb 19 '26 06:02 worktrunk-bot

yes I agree

should we auto-cd to the primary worktree? or is that a bit magic?

max-sixty avatar Feb 19 '26 06:02 max-sixty

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!)

max-sixty avatar Feb 19 '26 06:02 max-sixty

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😂.

davidbeesley avatar Feb 19 '26 06:02 davidbeesley

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.

davidbeesley avatar Feb 19 '26 07:02 davidbeesley

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.

If the main repo can be resolved successfully non wt switch commands could suggest wt switch instead of a potentially long cd command.

davidbeesley avatar Feb 19 '26 07:02 davidbeesley