A write guard that only protects .env will still let Claude Code "help" by regenerating dist, .next, or coverage. Those folders are build output. A PreToolUse hook can reject Edit and Write paths that land there, so the model has to change source instead.
The hook should name the blocked folder and exit 2.
The hook
Save this as .claude/hooks/block_generated.py and make it executable:
#!/usr/bin/env python3 import json import sys from pathlib import Path GENERATED = { "dist", "build", "out", "coverage", "target", ".next", ".cache", "node_modules", } def blocked(path: str) -> str | None: parts = Path(path).parts for part in parts: if part in GENERATED: return part return None try: payload = json.load(sys.stdin) except json.JSONDecodeError: sys.exit(0) tool_input = payload.get("tool_input") or {} path = tool_input.get("file_path") or tool_input.get("path") or "" folder = blocked(str(path)) if folder: print(f"PreToolUse generated-folder guard: refusing to edit under '{folder}'. Change the source and rebuild.") sys.exit(2) Wire it in .claude/settings.json under PreToolUse with a matcher of Edit|Write and command set to python3 .claude/hooks/block_generated.py.
Why this beats a pasted prompt
Hooks run on the event, not when you remember to say "don't touch dist". Generated folders are cheap to regenerate and expensive to review when a model rewrites them mid-task. If you genuinely need to inspect a built file, open it read-only or temporarily narrow the matcher rather than deleting the hook.
Tips
- Keep the hook local, fast, and free of network calls.
- Pair it with permission deny rules for the same folders if your Claude Code version supports path denies.
- If a tool uses a different input key than
file_path, add that key to the lookup rather than failing open without logging.
Want the rest of the setup?
I keep copy-ready Claude Code configs (CLAUDE.md templates, slash commands, hooks, permission rules) in Vildanden Agent Config. There's a free sample: https://justly-mia-429.rehost.page/
![AMR Operations, Deployment, and MLOps — Ch.04: Fleet Management [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fphajz60uq731julzkt59.png)
![AMR Operations, Deployment, and MLOps — Ch.03: Remote Monitoring [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgf8gh1nt81hujq8e6uup.png)
![AMR Operations, Deployment, and MLOps — Ch.02: Field Operations [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkzb8hmbgu0kqd06z853j.png)
![AMR Operations, Deployment, and MLOps — Ch.01: Deployment Strategy [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3kfyl7aa0ui2y8vm900y.png)
