rtk icon indicating copy to clipboard operation
rtk copied to clipboard

Hook silently fails when rtk not in Claude Code's restricted PATH (Homebrew installs)

Open sokol-matija opened this issue 8 months ago • 4 comments

Problem

When RTK is installed via Homebrew, the rtk-rewrite.sh hook silently stops working because Claude Code runs hooks with a restricted PATH that does not include /opt/homebrew/bin.

The hook does:

if ! command -v rtk &>/dev/null; then
  exit 0  # silent failure — no rewrite happens, no warning
fi

This means all Bash commands pass through unmodified with zero indication anything is wrong. rtk gain still works fine from the terminal (because the user's shell has Homebrew in PATH), so the breakage is invisible.

How it breaks

  1. User installs RTK via cargo install rtk → binary at ~/.cargo/bin/rtk
  2. Hook works (cargo bin may be in PATH, or user never notices)
  3. User switches to brew install rtk → binary moves to /opt/homebrew/bin/rtk
  4. Hook silently fails — command -v rtk returns nothing in Claude Code's hook environment
  5. rtk gain --history stops updating, but user assumes hook is fine

Verification

You can reproduce by running the hook with a clean environment:

env -i HOME="$HOME" echo '{"tool_input":{"command":"git status"}}' | env -i HOME="$HOME" bash ~/.claude/hooks/rtk-rewrite.sh
# Output: (nothing) — exits 0 silently, no rewrite

Root cause

rtk init -g installs a hook that uses rtk by name and relies on it being in PATH. But Claude Code's hook subprocess does not inherit the user's full shell PATH (no Homebrew, no cargo, etc. on macOS).

Suggested fixes

Option A (best): rtk init -g should inject the absolute path to the current binary into the hook at install time:

# Generated by rtk init -g
RTK_BIN="/opt/homebrew/bin/rtk"  # resolved at install time

Option B: The hook should probe known install locations as fallback:

export PATH="/opt/homebrew/bin:/usr/local/bin:$HOME/.cargo/bin:$PATH"

Option C: Emit a visible warning (not just exit 0) when rtk is not found — at minimum >&2 echo "[rtk] WARNING: rtk not found in PATH, hook disabled" so users know something is wrong.

Note: Option C has already been partially addressed (warnings added to the hook), but the silent failure on Homebrew installs is the main UX problem.

Environment

  • macOS (Apple Silicon) — Homebrew installs to /opt/homebrew/bin
  • RTK installed via brew install rtk
  • Claude Code hook PATH does not include /opt/homebrew/bin

Workaround

In ~/.claude/settings.json, change the hook command from:

/Users/<you>/.claude/hooks/rtk-rewrite.sh

to:

PATH="/opt/homebrew/bin:/usr/local/bin:$PATH" bash /Users/<you>/.claude/hooks/rtk-rewrite.sh

sokol-matija avatar Mar 18 '26 08:03 sokol-matija

Additional issue: updatedInput silently dropped when other PreToolUse hooks are present

After fixing the PATH issue (by prepending /opt/homebrew/bin to the hook command), I ran into a second silent failure.

Problem

Even with the PATH fix applied and the hook correctly producing updatedInput JSON, Claude Code was still executing the original unrewritten command. The hook ran (verified via debug log), produced the correct JSON output, but the rewrite was never applied.

Root cause

Claude Code bug: When multiple PreToolUse hooks match the same tool, updatedInput is not aggregated — the last hook's output wins, even if it's empty.

In my case, claude-code-wakatime was registered with matcher: "*" in PreToolUse, so it fired after the RTK hook and its empty output wiped the updatedInput rewrite. This is tracked as Claude Code issue #15897.

This means RTK's hook silently does nothing on any Claude Code setup that has any other matcher: "*" PreToolUse hook installed — a very common configuration.

Fix

Remove or reorder any matcher: "*" PreToolUse hooks so RTK fires last, or (more reliably) ensure no other PreToolUse hook fires for Bash tool calls after RTK.

In ~/.claude/settings.json, the RTK hook entry must be the last PreToolUse entry, and no other hook with matcher: "*" or matcher: "Bash" should follow it.

Suggested improvements for RTK

  1. Document this limitation in the hook installation docs — users with plugins like claude-code-wakatime will silently lose rewrites
  2. rtk verify could detect other PreToolUse hooks in settings.json and warn if any matcher: "*" hooks follow the RTK hook
  3. rtk init -g could automatically place the RTK hook last in the PreToolUse array

Environment

  • Claude Code 2.1.78
  • RTK 0.30.0
  • macOS (Apple Silicon)

sokol-matija avatar Mar 18 '26 10:03 sokol-matija

wshm · Automated triage by AI

🔍 Automated Triage

🐛 Category bug
🟠 Priority high
🎯 Confidence 91%

Summary

Hook silently no-ops for Homebrew RTK installs because Claude Code's restricted PATH excludes /opt/homebrew/bin; fix by injecting absolute binary path at rtk init -g time

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

📁 Relevant files
  • hooks/rtk-rewrite.sh
  • src/init.rs

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

pszymkowiak avatar Mar 20 '26 15:03 pszymkowiak

anyone got it working on claude code?

juanpablodlc avatar Mar 30 '26 22:03 juanpablodlc

anyone got it working on claude code?

I got it working by using the workaround described in the PR description message.

MiLk avatar Apr 01 '26 00:04 MiLk