claude-code - 💡(How to fix) Fix Regression: Bash tool permanently broken when CWD is deleted mid-session (was fixed in #21580 / #26136, returned in 2.1.119) [2 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#52747Fetched 2026-04-24 10:40:41
View on GitHub
Comments
2
Participants
3
Timeline
7
Reactions
0
Author
Timeline (top)
labeled ×5commented ×2

This is a regression of #21580 ("Bash tool becomes permanently stuck when cached working directory is moved or deleted", closed COMPLETED 2026-02-20) and #26136 ("Deleting its CWD breaks its shell", closed COMPLETED 2026-02-16). Both were fixed in February 2026; the bug is back in Claude Code 2.1.119.

If the agent deletes the directory the harness is using as its Bash cwd, every subsequent Bash tool call returns:

Working directory "<path>" no longer exists. Please restart Claude from an existing directory.

The agent has no way to recover. cd /tmp && ls, absolute-path commands, pushd, /bin/bash -c '...' — all fail with the same error, because the harness validates cwd before handing the command to the shell. Read, Write, Edit, WebFetch, WebSearch, and subagents continue to work, proving the session itself is alive — but anything that needs a shell (tests, git, docker, pytest) is impossible for the rest of the session.

Error Message

The agent has no way to recover. cd /tmp && ls, absolute-path commands, pushd, /bin/bash -c '...' — all fail with the same error, because the harness validates cwd before handing the command to the shell. Read, Write, Edit, WebFetch, WebSearch, and subagents continue to work, proving the session itself is alive — but anything that needs a shell (tests, git, docker, pytest) is impossible for the rest of the session. 5. Ask the agent to cd /tmp && ls. Same error — the harness refuses to spawn the shell because cwd validation runs before the command. Every Bash call returns the "no longer exists" error until session restart. The same fallback that landed for #21580/#26136: in the harness's pre-command hook that does chdir(session_cwd) before spawning bash, on ENOENT retry with project_root → $HOME → /tmp, update session_cwd to whichever succeeds, and emit a system-reminder so the model knows. One extra syscall on the error path; no change on the happy path.

Root Cause

The agent has no way to recover. cd /tmp && ls, absolute-path commands, pushd, /bin/bash -c '...' — all fail with the same error, because the harness validates cwd before handing the command to the shell. Read, Write, Edit, WebFetch, WebSearch, and subagents continue to work, proving the session itself is alive — but anything that needs a shell (tests, git, docker, pytest) is impossible for the rest of the session.

Code Example

Working directory "<path>" no longer exists. Please restart Claude from an existing directory.
RAW_BUFFERClick to expand / collapse

Summary

This is a regression of #21580 ("Bash tool becomes permanently stuck when cached working directory is moved or deleted", closed COMPLETED 2026-02-20) and #26136 ("Deleting its CWD breaks its shell", closed COMPLETED 2026-02-16). Both were fixed in February 2026; the bug is back in Claude Code 2.1.119.

If the agent deletes the directory the harness is using as its Bash cwd, every subsequent Bash tool call returns:

Working directory "<path>" no longer exists. Please restart Claude from an existing directory.

The agent has no way to recover. cd /tmp && ls, absolute-path commands, pushd, /bin/bash -c '...' — all fail with the same error, because the harness validates cwd before handing the command to the shell. Read, Write, Edit, WebFetch, WebSearch, and subagents continue to work, proving the session itself is alive — but anything that needs a shell (tests, git, docker, pytest) is impossible for the rest of the session.

Steps to reproduce

Hits every time in a fresh session:

  1. mkdir /tmp/cwd-repro && cd /tmp/cwd-repro
  2. Start Claude Code: claude
  3. Ask the agent: "run rm -rf /tmp/cwd-repro then ls"
  4. The rm succeeds. The ls (and every Bash call after it, forever) returns: Working directory "/tmp/cwd-repro" no longer exists. Please restart Claude from an existing directory.
  5. Ask the agent to cd /tmp && ls. Same error — the harness refuses to spawn the shell because cwd validation runs before the command.

Expected

What #21580 and #26136 originally fixed: on ENOENT for cwd, fall back to a known-good directory (project root → $HOME/tmp), update session cwd, and emit a one-line warning the agent can see ("cwd was removed; now running from /Users/…").

Actual

Every Bash call returns the "no longer exists" error until session restart.

Impact

Common real-world triggers:

  • Agent is asked to "move this app into ~/projects/foo" and the repo being moved happens to be the cwd.
  • Any rm -rf on a path that contains cwd (the canonical repro from #26136).
  • git worktree remove when cwd is the removed worktree (see related open issues #18236, #48405, and recently-NOT_PLANNED'd #34344).
  • Test suites that clean up tempdirs.

Environment

  • Claude Code: 2.1.119
  • OS: macOS 15.x (Darwin 25.3.0)
  • Shell: zsh

Related

  • #21580 (closed COMPLETED 2026-02-20, locked) — original canonical
  • #26136 (closed COMPLETED 2026-02-16, locked) — sibling fix
  • #18236 (open, worktree variant, no activity since Jan)
  • #48405 (open, filed 2026-04-15, worktree variant) — likely the same regression scoped to worktrees
  • #34344 (closed NOT_PLANNED 2026-04-14) — worktree variant declined 10 days ago, possibly mistakenly given that #21580/#26136 were marked completed

Proposed fix

The same fallback that landed for #21580/#26136: in the harness's pre-command hook that does chdir(session_cwd) before spawning bash, on ENOENT retry with project_root → $HOME → /tmp, update session_cwd to whichever succeeds, and emit a system-reminder so the model knows. One extra syscall on the error path; no change on the happy path.

extent analysis

TL;DR

The issue can be fixed by implementing a fallback mechanism in the harness's pre-command hook to retry with a known-good directory (project_root → $HOME → /tmp) when the current working directory (cwd) no longer exists.

Guidance

  • Verify that the issue is indeed caused by the cwd being deleted or moved by checking the error message and the steps to reproduce.
  • Implement the proposed fix by modifying the harness's pre-command hook to catch ENOENT errors and retry with a fallback directory.
  • Update the session_cwd variable to the new directory and emit a warning message to the agent.
  • Test the fix by running the steps to reproduce and verifying that the issue is resolved.

Example

try:
    os.chdir(session_cwd)
except OSError as e:
    if e.errno == errno.ENOENT:
        # Fallback to known-good directories
        for dir in [project_root, os.path.expanduser('~'), '/tmp']:
            try:
                os.chdir(dir)
                session_cwd = dir
                print(f"Warning: cwd was removed; now running from {dir}")
                break
            except OSError:
                continue
        else:
            # If all fallbacks fail, raise an error
            raise

Notes

The proposed fix is based on the original fixes for #21580 and #26136, which were marked as completed. However, the issue has regressed in Claude Code 2.1.119, and the fix needs to be re-implemented.

Recommendation

Apply the proposed workaround by implementing the fallback mechanism in the harness's pre-command hook, as it is a targeted fix that addresses the specific issue caused by the cwd being deleted or moved.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Regression: Bash tool permanently broken when CWD is deleted mid-session (was fixed in #21580 / #26136, returned in 2.1.119) [2 comments, 3 participants]