Blog / claude code
Stop your coding agent editing generated files
Agents edit the generated file instead of its source, and the next build wipes the fix. A Claude Code deny rule and a one-line check catch both cases.
On this page
- The setup we tested
- Guard 1: a Claude Code deny rule
- What about shell commands?
- What the agent does instead
- Guard 2: regenerate and compare
- Tell the agent where the source is
- Checklist
- Where Joxo fits
- Frequently asked questions
- How do I stop Claude Code editing a file?
- Does a deny rule stop Claude Code using sed on the file?
- How do I check a generated file is up to date?
- Should generated files be committed at all?
- Try it with your team
Most projects have files that a script writes: API clients from a schema, route tables, translation bundles, GraphQL types, a sitemap.xml. You change the source and run the generator. An agent in a hurry often skips that and edits the generated file directly, because that's where the code it needs to change is.
It works, until the next person runs the generator. Their run rewrites the file from the source and the agent's change is gone. Nobody notices until the feature it added stops working. On a team, it's worse: the hand edit and the regenerated file show up as a merge conflict in a file nobody should be resolving by hand.
Two guards cover it. A Claude Code deny rule stops the agent editing the file at all. A check that reruns the generator and compares catches any hand edit that got through, from any agent or person. We tested both with Claude Code 2.1.318, git 2.43 and Node.
The setup we tested#
A small site whose routes come from routes.json. A script turns them into src/:
import { readFileSync, writeFileSync } from 'node:fs';
const routes = JSON.parse(readFileSync('routes.json', 'utf8'));
const body = Object.entries(routes).map(([p, t]) => ` ${JSON.stringify(p)}: ${JSON.stringify(t)},`).join('\n');
writeFileSync('src/routes.generated.js',
`// GENERATED from routes.json by scripts/gen-routes.mjs. Do not edit.\nexport const routes = {\n${body}\n};\n`);Wired up as npm run gen. The output starts with a header saying where it comes from:
// GENERATED from routes.json by scripts/gen-routes.mjs. Do not edit.
export const routes = {
"/": "Home",
"/pricing": "Pricing",
};That header is the first guard and costs nothing. Agents read the top of a file before they edit it, and a plain "generated from X by Y, do not edit" tells them where to go instead.
Guard 1: a Claude Code deny rule#
Claude Code lets you deny tools on paths in .claude/. One rule covers every generated file if they share a naming pattern:
{
"permissions": {
"deny": ["Edit(**/*.generated.*)"]
}
}An Edit rule covers all of Claude's file-editing tools. We first wrote a second rule for Write as well, and Claude Code warned us about it at startup:
Permission deny rule (.claude/settings.json): Write(**/*.generated.*) is not matched by file permission checks — only Edit(path) rules are. Use Edit(**/*.generated.*) instead (Edit rules cover all file-editing tools).So one Edit(...) line is enough.
With the rule in place, we told Claude to edit the generated file directly and to stop if blocked:
claude -p "Open src/routes.generated.js and add the line \"/about\": \"About\", to it with the Edit tool, nothing else. If you are blocked, quote the exact message in one line and stop." --allowedTools "Edit,Read"Blocked: "File is in a directory that is denied by your permission settings."The file stayed as it was.
What about shell commands?#
An agent that can't use its edit tool can still try sed -i or >> from the shell. We tested both, with Bash allowed. Without the deny rule, sed -i on the generated file ran and changed it. With the rule, both were refused:
The command was denied by a permission check, so it didn't run and `src/routes.generated.js` is unchanged.So in this version, the path rule also covers obvious shell writes to that file. A script that writes the file is a different case. npm run gen still ran fine with the rule on, which is exactly what you want: the generator is allowed, hand edits are not. But it also means a clever enough command can get round it. That's what the second guard is for.
What the agent does instead#
The best result is that the agent changes the source. We gave Claude a normal task with the deny rule on, even pointing it at the wrong file:
claude -p "Add an /about route titled About by editing src/routes.generated.js. Then say in two lines what you changed." --allowedTools "Edit,Bash,Read"I added `"/about": "About"` to `routes.json`, since `src/routes.generated.js` is generated and says not to edit it by hand.
I then ran `npm run gen`, which regenerated `src/routes.generated.js` with the new `/about` route titled About.It read the header, edited routes.json, ran the generator, and both files changed together. Generated files should always change in the same commit as their source.
Guard 2: regenerate and compare#
The deny rule only covers Claude Code. Other agents, and people, can still edit by hand. A check that works for everyone: run the generator on the committed code and see whether anything changes. If it does, the generated file doesn't match its source.
npm run gen && git diff --exit-code src/routes.generated.jsWe committed a hand edit that added a /blog route only to the generated file, then ran the check:
diff --git a/src/routes.generated.js b/src/routes.generated.js
index b592a4d..39360c7 100644
--- a/src/routes.generated.js
+++ b/src/routes.generated.js
@@ -2,5 +2,4 @@
export const routes = {
"/": "Home",
"/pricing": "Pricing",
- "/blog": "Blog",
};It exits with 1, and the diff shows exactly which hand edit the generator would wipe. Run it in your test script, a pre-push hook or your CI, on a clean checkout. Run it locally on a dirty tree and it simply overwrites your uncommitted edit, which hides the problem.
Tell the agent where the source is#
The deny rule says "no". A line in CLAUDE.md or AGENTS.md says what to do instead, which saves the agent a few wrong turns:
Files named *.generated.* are written by scripts. Change the source (routes.json for src/routes.generated.js), run npm run gen, and commit both.If you have several generators, list each output with its source and command. It's the same list a new person on the team needs.
Checklist#
- A header on every generated file: what it's built from and by which command.
- A shared naming pattern (
*.generated.*or agenerated/folder) so one rule covers them all. "deny": ["Edit(<pattern>)"]in.claude/, committed.settings.json - One line in your agent instructions naming the sources.
- A regenerate-and-diff check on a clean checkout.
Where Joxo fits#
On a team, each person's agent has its own instructions file and its own settings, and they drift. Joxo keeps one set of team rules that every agent on the project reads, so "change the source, not the generated file" is written once. Setup is one line pasted into your agent.
Frequently asked questions#
How do I stop Claude Code editing a file?#
Add "deny": ["Edit(path/ under permissions in .claude/. Patterns like **/*.generated.* work. Edit rules cover all of Claude's file-editing tools.
Does a deny rule stop Claude Code using sed on the file?#
In our test with Claude Code 2.1.318, yes: sed -i and >> on the denied file were refused. A script that writes the file still runs, so keep a regenerate-and-compare check as well.
How do I check a generated file is up to date?#
Run the generator on a clean checkout, then git diff --exit-code <file>. Exit code 1 means the committed file doesn't match its source.
Should generated files be committed at all?#
If your build can generate them, you can leave them out of git. Many teams commit them so the app runs without a build step. If you commit them, add the check above.