claude-code - 💡(How to fix) Fix Stop hook fails with ENOENT when project directory has been deleted [1 comments, 2 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#49300Fetched 2026-04-17 08:45:10
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Author
Timeline (top)
labeled ×3commented ×1

When a Claude Code session's working directory is deleted during the session, the Stop hook fails with a non-blocking error:

Stop hook error: Failed with non-blocking status code: Error occurred while
executing hook command: ENOENT: no such file or directory, posix_spawn '/bin/sh'

Error Message

Stop hook error: Failed with non-blocking status code: Error occurred while executing hook command: ENOENT: no such file or directory, posix_spawn '/bin/sh'

Root Cause

Claude Code spawns the hook shell process with cwd set to the project directory. When that directory no longer exists, Node.js's child_process.spawn throws ENOENT before the shell process can start — even though the shell binary itself (/bin/sh) exists. There is no way to work around this from within the hook command itself.

Fix Action

Workaround

Adding "async": true to the Stop hook suppresses the visible error, but the hook still does not execute.

Code Example

Stop hook error: Failed with non-blocking status code: Error occurred while
executing hook command: ENOENT: no such file or directory, posix_spawn '/bin/sh'
RAW_BUFFERClick to expand / collapse

Summary

When a Claude Code session's working directory is deleted during the session, the Stop hook fails with a non-blocking error:

Stop hook error: Failed with non-blocking status code: Error occurred while
executing hook command: ENOENT: no such file or directory, posix_spawn '/bin/sh'

Steps to Reproduce

  1. Open a Claude Code session in a project directory (e.g. ~/Developer/my-project)
  2. During the session, delete the working directory (e.g. via rm -rf ~/Developer/my-project)
  3. End the session — the Stop hook fires and fails with ENOENT

Root Cause

Claude Code spawns the hook shell process with cwd set to the project directory. When that directory no longer exists, Node.js's child_process.spawn throws ENOENT before the shell process can start — even though the shell binary itself (/bin/sh) exists. There is no way to work around this from within the hook command itself.

Expected Behavior

Stop hooks should fall back to $HOME (or /tmp) as the working directory when the original project directory no longer exists.

Actual Behavior

The hook fails with ENOENT: no such file or directory, posix_spawn '/bin/sh' because Node.js cannot spawn the shell with a non-existent cwd.

Workaround

Adding "async": true to the Stop hook suppresses the visible error, but the hook still does not execute.

Additional Context

  • The hook schema does not expose a cwd field to let users override the working directory per-hook.
  • Platform: macOS (darwin 25.3.0)
  • Suggested fix: if the hook's cwd does not exist at fire time, fall back to the user's home directory.

extent analysis

TL;DR

Modify the Stop hook to fall back to the user's home directory as the working directory when the original project directory no longer exists.

Guidance

  • Identify the point where the child_process.spawn is called in the Stop hook and modify it to check if the specified cwd exists before attempting to spawn the process.
  • If the cwd does not exist, update the cwd to the user's home directory ($HOME) to prevent the ENOENT error.
  • Consider adding a check to ensure the hook command can handle being executed from a different working directory.
  • Review the hook schema to determine if adding a cwd field would be beneficial for users to override the working directory per-hook.

Example

const spawn = require('child_process').spawn;
const cwd = '/path/to/project/directory';

// Check if cwd exists before spawning process
if (!fs.existsSync(cwd)) {
  cwd = process.env.HOME; // Fall back to user's home directory
}

const child = spawn('/bin/sh', [], { cwd: cwd });

Notes

This solution assumes that the Stop hook's functionality is not dependent on the original project directory's existence. If the hook relies on files within the project directory, additional modifications may be necessary.

Recommendation

Apply workaround: Modify the Stop hook to fall back to the user's home directory as the working directory when the original project directory no longer exists, as this directly addresses the root cause of the issue.

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