Skip to content

PreToolUse hooks not called for Read tool when file doesn't exist #316

@feroult

Description

@feroult

PreToolUse hooks are not invoked when Claude calls the Read tool with a file path that doesn't exist. The hook is only called when the file exists. This contradicts the official documentation which states that PreToolUse hooks execute "after Claude creates tool parameters and before processing the tool call".

This breaks legitimate use cases like path translation where hooks need to modify file paths before validation occurs.

Expected Behavior

According to the documentation, PreToolUse hooks should be called before the tool processes the call, allowing hooks to:

  • Validate inputs
  • Modify tool parameters using updatedInput
  • Block execution with permissionDecision: "deny"

The hook should be invoked regardless of whether the file exists.

Actual Behavior

When Claude calls Read with a non-existent file path:

  1. The SDK validates that the file doesn't exist
  2. The SDK returns "File does not exist" error
  3. The PreToolUse hook is never called

When Claude calls Read with an existing file path:

  1. The PreToolUse hook is called ✅
  2. The file is read successfully ✅

Reproduction

See the minimal reproduction script: reproduce_hook_bug.py

Run it:

pip install claude-agent-sdk
python reproduce_hook_bug.py

Expected output:

[HOOK CALLED] for TEST 1 (/workspace/test.txt)
[HOOK CALLED] for TEST 2 (/tmp/hook_test/test.txt)

Actual output:

[HOOK CALLED] for TEST 2 only (/tmp/hook_test/test.txt)

Environment

  • Claude Code: 2.0.35 (latest)
  • claude-agent-sdk: 0.1.6 (latest - tested on 0.1.4, 0.1.5, 0.1.6)
  • Python: 3.11
  • OS: Linux

Impact

This bug breaks:

  1. Path translation hooks - Cannot translate paths like /workspace/* to actual locations before validation
  2. Custom file resolution - Cannot implement custom file lookup logic
  3. Input sanitization - Cannot normalize/validate file paths before they're checked
  4. Audit logging - Cannot log all Read attempts, only successful ones

The updatedInput feature is essentially useless for file path modification because hooks don't fire early enough.

Workaround

None found. The only way to make hooks work is to ensure files exist at the exact paths Claude uses, which defeats the purpose of path translation hooks.

Related Issues

Similar to #7082 but different - that issue was about permission deny rules, this is about hooks not being called at all for non-existent paths.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions