cd /news/ai-agents/my-claude-code-formatting-hook-was-c… · home › topics › ai-agents › article
[ARTICLE · art-180768] src=ainexusdaily.vercel.app ↗ pub= topic=ai-agents verified=true sentiment=· neutral

My Claude Code formatting hook was correct, and it never ran

A developer's Claude Code PostToolUse hook configured with the matcher "Edit|Write" never ran because the model created the test file with a Bash shell command instead of the Write tool, a gap the developer confirmed via a debug log (claude --debug-file /tmp/claude.log) that showed no hook invocation at all. When the Write tool was requested by name, the hook fired in 452 milliseconds and reformatted the file to `export const sum = (a, b) => { return a + b; };`. The developer recommends adding a CLAUDE.md instruction to use Write and Edit rather than shell commands, or running formatting from a Stop hook that formats every file git reports as changed, since a tool matcher is a rule about the tool and not about the result.

recommendation

Back up Edit|Write hooks with a Stop hook that formats every file git reports as changed, since the model can create or modify files through Bash and bypass tool matchers.

more in playbook: Working with AI coding agents
read6 min views24 publishedDec 3, 2026
My Claude Code formatting hook was correct, and it never ran
Image: Ainexusdaily (auto-discovered)

The setup that every guide shows Claude Code writes correct code in its own style. My projects have a formatter, and I do not want pull requests where half of the changed lines are quotes and commas. The usual answer is a hook: after every file edit, run the formatter on that file. This is the con

figuration, in .claude/settings.json: { "hooks": { "PostToolUse": [ { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": ""$CLAUDE_PROJECT_DIR"/.claude/hooks/format-file.sh", "timeout": 30, "statusMessage": "Formatting" } ] } ] } } The script behind it reads the file path from the JSON that Claude Code sends on standard input, picks a formatter by file extension, and runs it. The full script, with Prettier, Pint, PHP-CS-Fixer, Ruff and gofmt, is in the repository. The part that matters here is short: input=$(cat) file=$(jq -r '.tool_input.file_path // empty' <<<"$input") [ -n "$file" ] && [ -f "$file" ] || exit 0 The JSON was valid. The script was executable.

/hooks listed the hook under PostToolUse. Everything looked right. I tested it with the smallest task I could think of. I asked Claude for a file with one line that no formatter would leave alone: export const sum=(a,b)=>{return a+b} Claude created the file.

I opened it, and it contained exactly that line. No spaces, no line breaks, no semicolon. The formatter had not touched it. My first guess was a broken script, so I ran the session again with a debug log: claude --debug-file /tmp/claude.log The log is the fastest way to see what Claude Code really did.

I expected an error from my script. I found something else: there was no hook in the log at all. Claude Code had not even tried to run it. The log showed the tool call that created the file.

It was not Write. It was Bash. The model had decided that a one-line file is quickest to create with a shell command, something like: echo 'export const sum=(a,b)=>{return a+b}' > sum.js My matcher was Edit|Write. A Bash call does not match it, so the hook was never a candidate.

From the point of view of Claude Code, nothing had gone wrong. When I asked for the Write tool by name in the prompt, the hook ran, took 452 milliseconds, and the file became: export const sum = (a, b) => { return a + b; }; So the hook worked. It only did not cover every way in which a file can appear. A hook on a tool is a rule about that tool. It is not a rule about the result. "Every file that Claude writes is formatted" and "every call of Edit or Write is followed by the formatter" sound like the same sentence, and they are not. The model chooses its tools freely. Most of the time it edits files with Edit and Write.

For a small new file, for a file generated by a command, or for a search-and-replace over many files with sed, it can use the shell. Each of those goes past a matcher on Edit|Write. This matters more for other hooks than for a formatter. If the same matcher protects files from being changed, the gap is a security problem and not a style problem. But the formatter is where I saw it first. 1. Tell Claude which tool to use. A line in CLAUDE.md such as "Create and change files with the Write and Edit tools, not with shell commands" makes the shell route rare.

It is a request to the model, and the model follows it most of the time. That is good enough for formatting. It is not a guarantee. 2.

Format at the end of the turn, whatever tool was used. A Stop hook runs when Claude is about to finish. At that point git knows every file that changed, and it does not care how. This script hands each changed file to the formatting script from above: #!/usr/bin/env bash # Stop hook: format every file that git reports as changed or new, # by handing each one to the PostToolUse script.

cd "${CLAUDE_PROJECT_DIR:-$PWD}" || exit 0 git status --porcelain --untracked-files=all -z | while IFS= read -r -d '' entry; do file=${entry:3} [ -f "$file" ] || continue jq -n --arg f "$PWD/$file" '{tool_input: {file_path: $f}}' | .claude/hooks/format-file.sh >/dev/null done exit 0 Register it next to the first hook: "Stop": [ { "hooks": [ { "type": "command", "command": ""$CLAUDE_PROJECT_DIR"/.claude/hooks/format-changed.sh", "timeout": 60 } ] } ] To be exact about what I tested: I ran this second script by hand in a project with unformatted files, including a file in a subfolder and a file with a space in its name, and it formatted them. I have not yet run it as a Stop hook inside a session. The first hook, the one from the beginning of this article, I ran in real sessions. Keep both hooks.

The first one formats a file at once, so Claude sees the formatted version when it edits the file again. The second one catches what the first one cannot see. Three steps, and they take five minutes: Type /hooks in a session. If your hook is not in the list, the configuration was not loaded, and the problem is in the JSON.

Start a session with claude --debug-file /tmp/claude.log, give Claude a small task, and search the log for the name of your script. If the name is not there, the hook was never started. In the same log, look at which tool Claude used. If it is not the tool in your matcher, you have found the same gap that I found.

The general lesson is the one I now apply to every hook that I write: test it by giving Claude a task, not by reading the configuration. Configuration that looks correct tells you nothing about which tool the model will choose. This is one of the findings from testing the recipes of my book, The Claude Code Automation Cookbook: 40 recipes for hooks, subagents, skills, MCP and CI workflows, each one run in a real session before it was written down. The code for all of them is in the GitHub repository, with a test for every script.

The book is available on Amazon as paperback and for Kindle. This article was written with the help of Claude Code, based on my own test sessions. I checked the commands and the output myself.

Key Takeaways #

  • •The full script, with Prettier, Pint, PHP-CS-Fixer, Ruff and gofmt, is in the repository.
  • •This story was reported by Dev.to , covering developments in thedev space.
  • •AI advancements continue to reshape industries — read the full article on Dev.to for complete coverage.

📖 Continue reading the full article:

Read Full Article on Dev.to →

── more in #ai-agents 4 stories · sorted by recency
── more on @claude code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/my-claude-code-forma…] indexed:0 read:6min 2026-12-03 · —