Skip to content

PreToolUse hook rewrites rg to rtk grep, losing ripgrep's recursive-by-default behavior #3611

Closed

Description

@deglan-rivas

Summary

The Claude Code PreToolUse hook (rtk hook claude) rewrites rg (ripgrep) commands into rtk grep ..., treating rg as an alias for grep. This breaks any rg command that relies on ripgrep's recursive-by-default search over a directory, since the rewritten command shells out to plain grep without -r.

Steps to reproduce

rtk --version
# rtk 0.42.4

mkdir -p /tmp/rtktest && echo "hello world" > /tmp/rtktest/a.txt

# Simulate what the PreToolUse hook does with an `rg` command
echo '{"tool_name":"Bash","tool_input":{"command":"rg -l foo /tmp/rtktest"}}' | rtk hook claude

Actual output

{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk grep -l foo /tmp/rtktest"}}}

Running the rewritten command fails:

$ rtk grep -l "hello" /tmp/rtktest
/usr/bin/grep: /tmp/rtktest: Es un directorio

Expected output

rg -l foo /tmp/rtktest is valid ripgrep syntax — ripgrep is recursive by default and does not need -r. The hook should either:

  1. Not rewrite rg at all (it's already a fast, self-sufficient binary — there's no token-savings reason to route it through rtk grep), or
  2. If routed through rtk grep, inject -r automatically when the target is a directory, so the semantics of the original rg invocation are preserved.

Additional context

  • Confirmed that passing -r explicitly to the rewritten command works around the bug: rtk grep -r "hello" /tmp/rtktest succeeds.
  • Confirmed fd, bat, sd, and eza are not rewritten by the hook (pass through unmodified).
  • Confirmed ls → rtk ls, find → rtk find, and cat → rtk read rewrites work correctly on directory/file targets — this issue appears specific to the rg/grep mapping.

Environment

  • rtk --version: 0.42.4
  • OS: Ubuntu (Linux 6.17.0-40-generic)
  • Invoked via Claude Code's PreToolUse hook (rtk hook claude)

Activity

  1. pszymkowiak commented on Aug 19, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 93%

    Summary

    rg->rtk grep rewrite in the Claude PreToolUse hook drops ripgrep's recursive-by-default semantics on directory targets, breaking valid rg invocations; hook should skip rewriting rg or inject -r for directory targets.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/hook/claude.rs
    • src/rewrite/grep.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. bvierra commented on Aug 23, 2026

    @bvierra

    This appears fixed in 0.45.0:
    rtk --version: rtk 0.45.0

    echo '{"tool_name":"Bash","tool_input":{"command":"rg -l foo /tmp/rtktest"}}' | rtk hook claude
    {"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk rg -l foo /tmp/rtktest"}}}

  3. KuSh commented on Sep 5, 2026

    @KuSh
    Collaborator

    Fixed — rg now routes to its own rtk rg handler instead of being remapped onto rtk grep, so ripgrep's recursive-by-default behaviour is preserved. Running the reported payload on the current build returns rtk rg -l foo /tmp/rtktest, matching the confirmation on v0.45.0. Please reopen or comment if you still see it on the latest version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions