Skip to content

fix(hook): keep commands inside command substitutions untouched - #3869

Closed
jsiu93 wants to merge 1 commit into
rtk-ai:developfrom
jsiu93:fix/hook-skip-subshell
Closed

jsiu93 wants to merge 1 commit into
rtk-ai:developfrom
jsiu93:fix/hook-skip-subshell

Conversation

@jsiu93

@jsiu93 jsiu93 commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Problem

The hook rewrites commands that appear inside $(...) or backtick substitutions. For example:

curl -X POST "$URL" --data-binary "$(jq -cn --arg p "$P" '{payload:$p}')"

becomes "$(rtk jq -cn ...)". The inner command's output is data consumed by the outer command, so the compacted output plus the tee pointer corrupt it. Same root cause as #1109 (git inside $(...)).

Fix

Before the existing top-level rewrite, a single pass masks complete $(...) and paired backtick substitutions (nesting, escapes, quotes and comments respected; single-quoted text stays literal). The top-level decision runs on the masked command, then the original slices are restored byte-for-byte. No regex, no new dependency, no change to heredoc, pipeline or xargs handling.

Tests

8 unit tests in registry.rs: outer rewrite with inner $(...) kept, unsupported outer command returns None, backtick variant, single-quoted literal, nested substitutions, operators and ) inside a substitution comment. cargo test, cargo clippy --all-targets -- -D warnings, cargo fmt --check all pass.

Fixes #1109. Refs #3558, #1282. Supersedes the subshell part of #2693, which has conflicts.

…ouched

Mask command substitutions before applying the existing top-level rewrite pipeline.
Restore their original bytes without regexes or new dependencies.

Refs rtk-ai#1109 rtk-ai#3558 rtk-ai#1282
@pszymkowiak

Copy link
Copy Markdown
Collaborator

Closing: already fixed on develop. Since #2286 (v0.42.3) the hook defers any command containing an unquoted $(...), backtick or process substitution before the registry rewrite runs, and #3343 (v0.49.0) handles the piped-producer case from #3558. Verified with rtk rewrite and rtk hook claude on the #1109, #3558 and this PR's own test commands: every substitution-bearing line is left untouched. Rewriting the outer command around a masked substitution, which this PR adds, would be unreachable behind that defer; whether nested commands should ever be rewritten is tracked in #1252. Thanks for the PR.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude Code hook: git inside $(...) subshells inserts rtk as a git subcommand instead of prefixing

2 participants