Repository navigation
Description
Related issues (searched before filing)
- rtk-rewrite.sh: No such file or directory #1962 — the dangling reference half of what is reported here. This issue reproduces it deterministically and gives its precondition (a co-resident non-rtk hook in the same
Bashmatcher group), plus the second, quieter variant it does not cover. - rtk init --agent cursor deletes ~/.cursor/hooks/rtk-rewrite.sh, leaves hooks.json referencing it, and reports success #3465 — the Cursor mirror image. PR fix(init): migrate Cursor hooks.json before deleting the legacy hook script #3570 fixes the Cursor path and explicitly scopes the Claude path out.
- rtk init -g --uninstall removes foreign PreToolUse hooks sharing a matcher group #3036 —
rtk init -g --uninstallremoving foreignPreToolUsehooks. Different command; related in that both concern which entries survive a rewrite of the matcher group. - fix(init): replace stale legacy hook registrations #2588 — open PR touching this exact code. It does not fix either variant and widens variant A; see below.
rtk inithangs in non-interactive environments (v0.36.0 telemetry consent gate) #1307 (closed) — ancestor of the non-interactive prompt behaviour.
Summary
rtk init -g deletes ~/.claude/hooks/rtk-rewrite.sh before it has written any replacement registration, and prints RTK hook registered (global). at a point in the flow where settings.json has not been touched yet. Depending on what else lives in the Bash matcher group, the user is left in one of two broken states — both with exit 0 and a success banner:
| Precondition | Result |
|---|---|
A. rtk's legacy entry is the only hook in the Bash matcher group |
Script deleted and the entry removed. settings.json ends as "PreToolUse": []. rtk is fully unregistered. |
B. another hook shares the Bash matcher group |
Script deleted, entry kept. settings.json still points at a file that no longer exists — the state reported in #1962. |
Variant A is the quieter of the two: Claude Code raises no hook error, because there is no hook. Every subsequent agent command runs unfiltered with nothing to signal it.
Environment
- rtk 0.45.0 (Homebrew), macOS arm64 (Darwin 25.6.0)
- Command:
rtk init -g, stdin redirected from/dev/null(non-TTY) - Both variants reproduced in an isolated
HOME; targeting verified with--dry-runfirst - Root-caused against
develop, so this is not specific to the 0.45.0 build
Reproduce — variant A (rtk alone in the matcher group)
Before:
$ ls -l $SB/.claude/hooks/
-rwxr-xr-x 53 rtk-rewrite.sh
$ cat $SB/.claude/settings.json
{
"hooks": {
"PreToolUse": [
{ "matcher": "Bash",
"hooks": [ { "type": "command", "command": "$SB/.claude/hooks/rtk-rewrite.sh" } ] }
]
}
}
Run:
$ HOME=$SB rtk init -g < /dev/null
RTK hook registered (global).
Command: rtk hook claude
RTK.md: $SB/.claude/RTK.md (10 lines)
CLAUDE.md: @RTK.md reference added
Patch existing $SB/.claude/settings.json? [y/N]
(non-interactive mode, defaulting to N)
MANUAL STEP: Add this to $SB/.claude/settings.json:
...
$ echo $?
0
After:
$ ls -la $SB/.claude/hooks/
(empty)
$ cat $SB/.claude/settings.json
{
"hooks": {
"PreToolUse": []
}
}
$ grep -rl "rtk" $SB/.claude --include="*.json"
(no match)
Reproduce — variant B (a second hook shares the group)
Same starting point, except the Bash group also contains /opt/acme/audit-hook.sh, and a Write group contains /opt/acme/write-guard.sh. After the same command:
settings.jsonis byte-identical — the legacy entry, the co-resident hook and theWritegroup all survive;~/.claude/hooks/rtk-rewrite.shis gone;- so
settings.jsonnow registers aPreToolUsecommand whose script does not exist. This is rtk-rewrite.sh: No such file or directory #1962.
The foreign hooks surviving is correct behaviour and should be preserved by any fix.
Root cause (line numbers verified against develop, src/hooks/init.rs)
init.rs:1160—migrate_old_hook_script(ctx)runs as step 1.init.rs:1249— deletes~/.claude/hooks/rtk-rewrite.sh. On success,init.rs:1257callsremove_legacy_settings_entries. The deletion is unconditional; nothing has been written in its place yet.init.rs:1344—remove_legacy_hook_entries_from_jsondrops a matcher entry only whendominated_by_legacyholds, i.e. when every command in the group is the legacy script. That single predicate is what splits variant A from variant B.init.rs:1179—println!("\nRTK hook registered (global).\n")fires at step 4, beforesettings.jsonis touched at all at step 5. The banner is unconditional and cannot reflect an outcome that has not happened.init.rs:1198 → 1001 → 445— under the defaultPatchMode::Ask,prompt_user_consentreturnsOk(false)on non-TTY stdin ((non-interactive mode, defaulting to N)) →PatchResult::Declined.init.rs:1211— theDeclined | Skippedarm prints nothing further, so the success banner stands as the run's last status line.
To be fair to the current behaviour: the MANUAL STEP block is printed, and a human watching a terminal can act on it. The defect is that the banner above it says the opposite, the exit code is 0, and the previously working install has already been destroyed by the time either is printed — so nothing recoverable is left if the output is not read.
--dry-run does not disclose either effect
rtk init -g --dry-run on the variant-A fixture prints only:
[dry-run] would migrate legacy hook script: …/rtk-rewrite.sh
[dry-run] would create RTK.md: …
[dry-run] would add @RTK.md reference to CLAUDE.md: …
[dry-run] would create global filters template: …
[dry-run] Nothing written.
There is no would remove legacy rtk-rewrite.sh entry from settings.json line (that branch at init.rs:1320 is only reached from the non-dry-run delete path) and no would prompt before patching. So the one command a cautious user would run to preview this cannot show them the destructive part.
Interaction with #2588
PR #2588 ("Fixes #1962") hoists remove_legacy_settings_entries out of the deletion-success branch, so the entry is stripped even when the script removal failed and when no legacy script exists. It does not touch the banner at init.rs:1179 or the Declined path.
That converts variant B into variant A for more users rather than fixing either: it removes the dangling reference by removing the registration, while the replacement write can still be declined. Worth resolving the ordering here first, or folding it in.
Suggested fix
- Move the
RTK hook registered (global).banner after step 5 and derive its wording fromPatchResult.DeclinedandSkippedmust not print a registration banner, and arguably should not exit 0. - Write before deleting: patch
settings.jsonwithrtk hook claudefirst, and remove the legacy script and legacy entry only after that write succeeds — the ordering PR fix(init): migrate Cursor hooks.json before deleting the legacy hook script #3570 adopted for Cursor. This fixes both variants at once and preserves co-resident foreign hooks. - Make
--dry-runreport the legacy-entry removal and the consent prompt, so the preview matches the real run.
I have the fixture scripts for both variants and am happy to send a PR with regression coverage if that is useful — flagging first since src/hooks/init.rs is a Tier-1 file under SECURITY.md and #2588 already claims part of this code.
Activity
pszymkowiak commentedon Aug 24, 2026
🔍 Automated Triage
bughighSummary
rtk init -g deletes the legacy hook script before writing a replacement and prints a false success banner, leaving rtk either fully unregistered or pointing at a missing script (exit 0, no error signal) — reorder write-before-delete and fix the banner/dry-run reporting in src/hooks/init.rs.
📁 Relevant files
src/hooks/init.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
docs(rtk): record RTK_TELEMETRY_DISABLED as the supported opt-out (#117)
docs(rtk): record RTK_TELEMETRY_DISABLED as the supported opt-out (#117…
fix(install): migrate legacy rtk hook under --skip-config
KuSh commentedon Oct 3, 2026
Two more states from the same migration, found while checking the Windows docs in #4350 (develop 4f9015f, isolated
HOME, legacy setups made with v0.36.0).1.
rtk init --showreports any legacy entry as the native hook. This applies whether or not the script is still on disk. Before any migration, with~/.claude/hooks/rtk-rewrite.shpresent andsettings.jsonpointing at it:The same output appears after the script has been deleted, which is the state variant B of this issue ends in. There,
rtk init -galso sayssettings.json: hook already presentand leaves the entry pointing at the missing file:--showcallshook_already_present(src/hooks/init/mod.rs:1448), which matches any command containingrtk-rewrite.sh. Its result is checked first, so the script-file branches below it ("legacy script v…", "outdated") are never reached while such an entry exists. So the check that the docs point people to afterrtk init -ggives a green result both before the migration and for a hook that fails on every Bash call.2. Declining the prompt on the CLAUDE.md-fallback state. The same order of operations shows up on the state that native Windows installs before v0.37.2 were left in (
init -g --claude-mdfrom v0.36.0, so the full block in~/.claude/CLAUDE.mdand no hook).rtk init -greplaces that block with@RTK.mdbefore it asks aboutsettings.json. Declining, or running without a TTY, then leaves:The default-awareness
RTK.mdassumes the hook is in place ("Command output here is condensed…"; it never says to prefix commands). So that setup goes from rtk used via the instructions to rtk not used at all, under the sameRTK hook registered (global).banner.3. The backup loses the original. Answering
yon a legacy setup writessettings.json.baktwice: first when the legacy entry is removed, then again whenrtk hook claudeis added. The final.bakholds the in-between file ("PreToolUse": []), not the original. Other keys survive in both, but the backup no longer shows what the user had before running init.