<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amer Malik Mohammed</title>
    <description>The latest articles on DEV Community by Amer Malik Mohammed (@amermmohammed).</description>
    <link>https://dev.to/amermmohammed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3924299%2Fd7f0c118-3456-4e91-992c-9e119bc7e09f.jpeg</url>
      <title>DEV Community: Amer Malik Mohammed</title>
      <link>https://dev.to/amermmohammed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amermmohammed"/>
    <language>en</language>
    <item>
      <title>My Claude Code formatting hook was correct, and it never ran</title>
      <dc:creator>Amer Malik Mohammed</dc:creator>
      <pubDate>Thu, 03 Dec 2026 05:54:01 +0000</pubDate>
      <link>https://dev.to/amermmohammed/my-claude-code-formatting-hook-was-correct-and-it-never-ran-1eg0</link>
      <guid>https://dev.to/amermmohammed/my-claude-code-formatting-hook-was-correct-and-it-never-ran-1eg0</guid>
      <description>&lt;h2&gt;
  
  
  The setup that every guide shows
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;This is the configuration, in &lt;code&gt;.claude/settings.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"PostToolUse"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;"matcher"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Edit|Write"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/format-file.sh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nl"&gt;"timeout"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nl"&gt;"statusMessage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Formatting"&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;a href="https://github.com/RunIO-dev/claude-code-automation-cookbook/tree/main/recipes/01-format-after-edit" rel="noopener noreferrer"&gt;in the repository&lt;/a&gt;. The part that matters here is short:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;input&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;cat&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;jq -r &lt;span class="s1"&gt;'.tool_input.file_path // empty'&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="s2"&gt;"$input"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt; -n &lt;span class="s2"&gt;"$file"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; -f &lt;span class="s2"&gt;"$file"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JSON was valid. The script was executable. &lt;code&gt;/hooks&lt;/code&gt; listed the hook under &lt;code&gt;PostToolUse&lt;/code&gt;. Everything looked right.&lt;/p&gt;

&lt;h2&gt;
  
  
  The first run did nothing
&lt;/h2&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;My first guess was a broken script, so I ran the session again with a debug log:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;claude --debug-file /tmp/claude.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why: Claude did not use the tool I had matched
&lt;/h2&gt;

&lt;p&gt;The log showed the tool call that created the file. It was not &lt;code&gt;Write&lt;/code&gt;. It was &lt;code&gt;Bash&lt;/code&gt;. The model had decided that a one-line file is quickest to create with a shell command, something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'export const sum=(a,b)=&amp;gt;{return a+b}'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; sum.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My matcher was &lt;code&gt;Edit|Write&lt;/code&gt;. A &lt;code&gt;Bash&lt;/code&gt; call does not match it, so the hook was never a candidate. From the point of view of Claude Code, nothing had gone wrong.&lt;/p&gt;

&lt;p&gt;When I asked for the &lt;code&gt;Write&lt;/code&gt; tool by name in the prompt, the hook ran, took 452 milliseconds, and the file became:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the hook worked. It only did not cover every way in which a file can appear.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this means
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;Edit&lt;/code&gt; or &lt;code&gt;Write&lt;/code&gt; is followed by the formatter" sound like the same sentence, and they are not.&lt;/p&gt;

&lt;p&gt;The model chooses its tools freely. Most of the time it edits files with &lt;code&gt;Edit&lt;/code&gt; and &lt;code&gt;Write&lt;/code&gt;. For a small new file, for a file generated by a command, or for a search-and-replace over many files with &lt;code&gt;sed&lt;/code&gt;, it can use the shell. Each of those goes past a matcher on &lt;code&gt;Edit|Write&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two ways to close the gap
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Tell Claude which tool to use.&lt;/strong&gt; A line in &lt;code&gt;CLAUDE.md&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Format at the end of the turn, whatever tool was used.&lt;/strong&gt; A &lt;code&gt;Stop&lt;/code&gt; hook runs when Claude is about to finish. At that point &lt;code&gt;git&lt;/code&gt; knows every file that changed, and it does not care how. This script hands each changed file to the formatting script from above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# Stop hook: format every file that git reports as changed or new,&lt;/span&gt;
&lt;span class="c"&gt;# by handing each one to the PostToolUse script.&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="s2"&gt;"${CLAUDE_PROJECT_DIR:-$PWD}"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;exit&lt;/span&gt; 0
git status --porcelain --untracked-files=all -z &lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="nv"&gt;IFS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;read&lt;/span&gt; -r -d &lt;span class="s1"&gt;''&lt;/span&gt; entry&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;${entry:3}&lt;/span&gt;
    &lt;span class="o"&gt;[&lt;/span&gt; -f &lt;span class="s2"&gt;"$file"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;continue&lt;/span&gt;
    jq -n --arg f &lt;span class="s2"&gt;"$PWD/$file"&lt;/span&gt; &lt;span class="s1"&gt;'{tool_input: {file_path: $f}}'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
      .claude/hooks/format-file.sh &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null
  &lt;span class="k"&gt;done&lt;/span&gt;
&lt;span class="nb"&gt;exit&lt;/span&gt; 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Register it next to the first hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"Stop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/format-changed.sh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nl"&gt;"timeout"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;Stop&lt;/code&gt; hook inside a session. The first hook, the one from the beginning of this article, I ran in real sessions.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to check your own hooks
&lt;/h2&gt;

&lt;p&gt;Three steps, and they take five minutes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Type &lt;code&gt;/hooks&lt;/code&gt; in a session. If your hook is not in the list, the configuration was not loaded, and the problem is in the JSON.&lt;/li&gt;
&lt;li&gt;Start a session with &lt;code&gt;claude --debug-file /tmp/claude.log&lt;/code&gt;, 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.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this comes from
&lt;/h2&gt;

&lt;p&gt;This is one of the findings from testing the recipes of my book, &lt;em&gt;The Claude Code Automation Cookbook&lt;/em&gt;: 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 &lt;a href="https://github.com/RunIO-dev/claude-code-automation-cookbook" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;, with a test for every script. The book is available on Amazon as &lt;a href="https://www.amazon.com/dp/B0HT9HH7N5" rel="noopener noreferrer"&gt;paperback&lt;/a&gt; and for &lt;a href="https://www.amazon.com/dp/B0HK64TVY8" rel="noopener noreferrer"&gt;Kindle&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was written with the help of Claude Code, based on my own test sessions. I checked the commands and the output myself.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>claudecode</category>
      <category>ai</category>
      <category>automation</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
