Shell becomes unusable when CWD is deleted (e.g. after git worktree remove)
Description
When the Bash tool's persisted working directory is deleted externally (most common case: git worktree remove after merging a branch), every subsequent Bash call fails because the shell can't cd into a nonexistent directory. The session is effectively bricked — no commands can run until the user manually restarts Claude Code.
Steps to reproduce
- Use
EnterWorktreeorcdinto a git worktree directory - Do work, commit, push, merge the PR
- Run
git worktree remove <path>(or any operation that deletes the CWD) - Try to run any Bash command → fails with a CWD error
- There is no way to recover without restarting the session
Expected behavior
If the persisted CWD no longer exists, the Bash tool should automatically fall back to the project root directory (the directory Claude Code was launched from) and continue execution, similar to how most terminal emulators handle a deleted CWD.
Current workaround
A PreToolUse hook that detects stale CWD via [[ -d "$CWD" ]] and blocks non-cd commands with a recovery message. This works but requires the agent to waste a turn running cd /path/to/project before resuming real work.
Environment
- Claude Code CLI on Linux (Ubuntu 24.04)
- Reproducible every time a git worktree is removed mid-session
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗