Claude Code Hooks Not Working: Fix a Hook That Won't Fire

Returns a numbered diagnosis of your Claude Code hook (location, event, matcher, exit code, stdout, onFailure, path), the named cause, a corrected entry and a one-command test.

Agent prompt

You get: Numbered diagnosis, cause found and corrected hook entry and script as diffs. Wait for my OK…

The prompt

Prompt
You are a Claude Code engineer debugging hook configuration in this repository. My hook should: . It either does not fire or fires but does not block.

Inspect the repo first. Run claude --version and check --help or the hooks docs for anything below your installed version may not support. Ask at most 3 short numbered questions, in one message, only for what you cannot find.

Rules:
1. Read every hook location: ~/.claude/settings.json, .claude/settings.json, .claude/settings.local.json, managed settings, plugin hooks/hooks.json, and skill or subagent frontmatter. Compare them with the read-only /hooks list and the source it shows for each hook.
2. Check the event fits the goal. Use PreToolUse to block: exit 2 blocks with stderr as the reason, and exit 1 does not block. PostToolUse can only react after the tool ran. UserPromptSubmit, Stop and PostToolBatch silently ignore matchers.
3. Check the matcher. Only letters, digits, _, -, spaces, commas and | mean an exact match or list. Any other character makes it an unanchored regex: Edit.* also matches NotebookEdit, so use ^Edit$. MCP tools are named mcp__server__tool, and a bare mcp__memory matches nothing.
4. Check that stdout is only the JSON object. Check onFailure (if unset, a failed or timed-out hook lets the action proceed), the timeout (default 600 s), the script's executable bit, and an absolute path built from $CLAUDE_PROJECT_DIR.
5. Every finding cites file:line or command output. Mark anything else as a gap.

Output format:
1. Numbered diagnosis: check, finding, pass/fail.
2. Cause found, in one line.
3. Corrected hook entry and script as diffs. Wait for my OK before saving, then say where you saved them. Never commit or push.
4. A one-command manual test with the expected exit code and output.

End with what you could not confirm, and how to verify it with claude --debug or /debug.

Example output

Example for: hook goal = block rm -rf in Bash, in a fictional repo services/billing.

1. Diagnosis
#CheckFindingResult
1LocationsHook only in .claude/settings.json:14. No hooks in ~/.claude/settings.json. .claude/settings.local.json does not exist. No plugin or frontmatter hooks foundPass
2/hooks listOne PreToolUse hook, source: project settingsPass
3EventPreToolUse, which is correct for blockingPass
4Matcher"Bash", an exact matchPass
5Exit code.claude/hooks/block-rm.sh:11 uses exit 1, which is non-blockingFail
6stdoutblock-rm.sh:6 echoes debug text to stdoutFail
7onFailureUnset, so if jq is missing the command still runsFail
8Executable bitls -l shows -rw-r--r--Fail
9PathRelative .claude/hooks/block-rm.sh, which breaks when cwd changesFail
10TimeoutNot set (default 600 s); the script finishes in under a secondPass
2. Cause found

The script exits 1 on a match. Exit 1 is non-blocking, so Claude Code logs the hook and runs rm -rf anyway.

3. Corrected entry (not saved yet; waiting for your OK)

.claude/settings.json:

-            "command": ".claude/hooks/block-rm.sh"
+            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-rm.sh",
+            "onFailure": "block"

.claude/hooks/block-rm.sh:

 cmd=$(jq -r '.tool_input.command')
-echo "checking $cmd"
+echo "checking $cmd" >&2
 if echo "$cmd" | grep -Eq 'rm[[:space:]]+-[a-zA-Z]*r[a-zA-Z]*f|rm[[:space:]]+-[a-zA-Z]*f[a-zA-Z]*r'; then
-  echo "rm -rf blocked" >&2
-  exit 1
+  echo "Blocked: rm -rf is not allowed in services/billing" >&2
+  exit 2
 fi

Plus:

chmod +x .claude/hooks/block-rm.sh
4. Manual test
jq -n '.tool_name="Bash" | .tool_input.command="rm -rf build"' | "$CLAUDE_PROJECT_DIR"/.claude/hooks/block-rm.sh; echo "exit=$?"

Expected output: checking rm -rf build, then Blocked: rm -rf is not allowed in services/billing on stderr, then exit=2.

Not confirmed
  • Whether managed settings exist on your machine. I found no managed settings file at the paths I could read.
  • Whether onFailure is supported in your installed version. Check the hooks docs for that version.

What to check: run the test above, then ask Claude to run rm -rf build in a scratch folder and confirm it is refused. Watch claude --debug to confirm the hook loads from project settings.

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • It reads every settings location and the /hooks source list, so a hook defined in the wrong file is caught before any other debugging.
  • It names the specific traps: exit 1 vs 2, unanchored regex matchers, bare mcp__ names and events that ignore matchers.
  • The fix ends with a one-command test that pipes fake hook input into the script, so you can verify it without waiting for Claude to trigger it.

Use in Claude Code at the repo root when a hook silently does nothing or lets a blocked action through. Follows the hooks reference as of Claude Code 2.1.329. Have the hook's goal ready and know whether you edited any user-level settings.

Common edits

  • Second example, ready to paste: set {hook_goal} = "run prettier on the changed file after every Edit or Write, but not NotebookEdit".
  • Change the goal to a PostToolUse hook such as auto-formatting, and ask the agent to drop the blocking checks.
  • Ask the agent to also check plugin hooks/hooks.json in a specific plugin folder you name.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

More in Claude Code
AI Coding AgentsClaude Code

Generate Reproducible Bug Fix Specs for Coding Agents

Get a standalone reproduction test script and a precise technical plan to fix a bug, ensuring your AI agent stays focused and avoids unnecessary file changes.

Cursor Call Graph Trace Before You Change a Function

Cursor maps who calls a function and what it calls, with file and line for every step, the callers your planned change could break and the exact tests to run, before any code is edited.

Claude Code Permission Allowlist and Deny Rules for This Repo in settings.json

Has Claude Code read your repo's scripts and CI, then propose allow, ask and deny permission rules with a reason for each, split into shared and local settings files, plus commands to test them.

AI Coding AgentsClaude Code

Root-Cause Bug Fix With a Failing Test and a Minimal Patch

Get a reproduced bug, a regression test that fails before the fix and passes after it, the smallest patch and a log of the commands Claude Code actually ran.

Coding

Debug an Error From the Message and the Code Around It

Finds the most likely cause of an error from the message and the relevant code, ranks other possible causes, and gives the smallest fix plus a way to prove it worked.

Claude Code Hook Writer: Auto-Run Lint, Typecheck or Tests After Edits

Produces a PostToolUse hook entry for settings.json, the script it calls and a concrete procedure to test that the check runs after file edits.