[BUG] Crash (ENOENT: spawn pgrep) when Current Working Directory is deleted during session
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Claude Code crashes and exits the process with an ENOENT error related to posix_spawn 'pgrep' if the current working directory (CWD) the session was initialized in is deleted from the filesystem while the session is active.
In the log provided, the user deleted the test-project folder while Claude was running inside it. When Claude attempted to recover from a Bash tool error caused by the missing directory ("Path ... does not exist"), it seemingly attempted to spawn a child process (likely for process management/cleanup using pgrep), which failed because the CWD context was invalid.
What Should Happen?
Claude Code should detect that the CWD has been deleted and gracefully handle the error (e.g., by falling back to the parent directory or the user's home directory) rather than crashing the entire application with an unhandled exception.
Error Messages/Logs
ENOENT: no such file or directory, posix_spawn 'pgrep'
path: "pgrep",
syscall: "spawn pgrep",
errno: -2,
spawnargs: [ "-P", 27406 ],
code: "ENOENT"
at spawn (unknown:1:1)
at spawn (node:child_process:667:35)
Steps to Reproduce
- Create a temporary directory:
mkdir test-crash-project - Navigate into it:
cd test-crash-project - Launch Claude Code:
claude - Send a simple message to start the session (e.g., "hi").
- In a separate terminal window, delete the directory you are currently running Claude in:
rm -rf path/to/test-crash-project - Back in Claude Code, send a prompt that requires Bash usage or triggers internal process checks (e.g., "Check what shell I am using").
- Claude will attempt to execute, fail to find the path, attempt to recover, and then crash.
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.0.57
Platform
Other
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
- Context: The crash occurs immediately after Claude thinks: "The current working directory was set to the test-project directory which was deleted. Let me change to a different directory and try again." It seems the crash happens before the logic to change directory is successfully executed.
Related log context:
⏺ Bash(echo $SHELL; ls /bin/)
⎿ Error: Path "/Users/user/.claude/hooks/count/test-project" does not exist
∴ Thinking…
The current working directory was set to the test-project directory which was deleted. Let me change to a
different directory and try again.
[CRASH OCCURS HERE]This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗