claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG] Need reliable way to enforce file path constraints (prevent files in certain directories)

Open smenor opened this issue 1 month ago • 1 comments

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

Claude Code (both main agent and Task subagents) ignores project-level instructions about where to create files. Despite explicit instructions in CLAUDE.md and attempted hook configurations, temporary files like test*.py, run_.py, debug.py are repeatedly created in the repository root instead of appropriate subdirectories.

What Should Happen?

There should be a reliable mechanism to enforce file path constraints at the project level. When configured, Claude should either:

  • Be blocked from writing to disallowed paths (with a clear error message)
  • Consistently follow CLAUDE.md instructions about file placement
  • Automatically redirect writes to the correct location

Error Messages/Logs

No errors - that's the problem. The files are silently created in the wrong location.

  When hooks were attempted via .claude/settings.local.json:
  {
    "hooks": {
      "PreToolUse": {
        "Write": {
          "command": "/path/to/block_root_files.sh",
          "args": ["Write", "{{file_path}}"]
        }
      }
    }
  }
  The hook script was never invoked - files were created without triggering the hook.

Steps to Reproduce

Steps to reproduce:

  1. Create a project with a CLAUDE.md containing:

File Organization

CRITICAL: Never create files in the repository root directory.

  • Test/debug scripts → institutes/{state}/{school_id}/ Forbidden in root: test*.py, debug*.py, run_*.py
  1. Ask Claude to write a scraper for a school, which may involve debugging
  2. Observe that Claude or its subagents create files like test_fetch.py, run_scraper.py in the repository root despite the instructions

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

2.0.76 (Claude Code)

Platform

AWS Bedrock

Operating System

Ubuntu/Debian Linux

Terminal/Shell

Terminal.app (macOS)

Additional Information

Description:

I need a way to prevent Claude (including subagents) from creating files in certain locations - specifically, preventing temporary/debug files like test*.py, run_.py, debug.py from being created in the repository root.

What I've tried:

  1. CLAUDE.md instructions - Added explicit instructions like "Never create files in the repository root directory" with specific guidance on where files should go instead. These are frequently ignored, especially by Task subagents.
  2. Hooks - Attempted to set up PreToolUse hooks in .claude/settings.local.json to intercept Write tool calls and block writes to certain paths. Could not get this working reliably.
  3. .gitignore - This prevents committing the files but doesn't prevent their creation, which is the actual problem.

Expected behavior:

There should be a reliable mechanism to enforce project-level file path constraints. Either:

  • PreToolUse hooks that actually work and can reject tool calls based on file path patterns
  • A dedicated configuration option for allowed/disallowed write paths
  • CLAUDE.md instructions that are consistently followed by both the main agent and subagents

Why this matters:

In projects with established directory structures, having Claude scatter temporary files in the wrong places creates ongoing cleanup work and clutters the workspace. This is especially problematic when running batch operations that spawn many subagents.


This issue was drafted by Claude (claude-opus-4-5-20250101) at the user's request.


smenor avatar Jan 07 '26 23:01 smenor

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/2901
  2. https://github.com/anthropics/claude-code/issues/3514
  3. https://github.com/anthropics/claude-code/issues/10683

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

github-actions[bot] avatar Jan 07 '26 23:01 github-actions[bot]

This issue has been automatically closed as a duplicate of #2901.

If this is incorrect, please re-open this issue or create a new one.

🤖 Generated with Claude Code

github-actions[bot] avatar Jan 11 '26 09:01 github-actions[bot]

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

github-actions[bot] avatar Jan 18 '26 14:01 github-actions[bot]