Repository navigation
fix(rewrite): passthrough native grep/ls when short flags collide with rtk options - #2334
BurpeeKiller wants to merge 1 commit into
Conversation
…h rtk options
Rewriting `grep`/`ls` to `rtk grep`/`rtk ls` is a pure prefix substitution,
so native short flags are re-parsed by clap under the rtk subcommand. Several
collide with a different semantics and silently change behavior:
grep -v clap --verbose (global) vs invert-match -> returns MATCHING lines
instead of non-matching
(worst: result inverted,
no error, no signal)
grep -h clap --help vs no-filename
grep -l clap --max-len vs files-with-matches
grep -m clap --max vs max-count
grep -V clap --version vs version
ls -h clap --help vs human-readable
ls -V clap --version
Add a conservative passthrough guard in the rewrite engine: when the native
command carries a short-flag cluster containing a colliding flag, skip the
rewrite (return None) so the native binary runs with its intended semantics.
Handles bundled clusters (`-rv`). Long flags (`--invert-match`, `--max-count`)
are unaffected — they reach ripgrep/ls via extra_args.
Safe flags (-i, -rn, -A, -c, ls -la, ...) still rewrite as before.
Mirrors the existing skip-rewrite pattern for `gh --json` and incompatible
`cat` flags. Adds regression tests covering each colliding flag, bundled
clusters, safe flags, and long-form equivalents.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a conservative “passthrough” guard to prevent rewriting native commands into rtk subcommands when certain short flags would be interpreted differently by clap, avoiding silent behavior changes (e.g., grep -v).
Changes:
- Introduced collision detection for short-flag clusters and skip rewriting when detected.
- Added a hardcoded per-subcommand list of colliding short flags for
rtk grepandrtk ls. - Added regression tests covering passthrough vs rewrite behavior for colliding/non-colliding flags.
| fn has_colliding_short_flag(cmd_part: &str, rtk_cmd: &str) -> bool { | ||
| let collisions = colliding_short_flags(rtk_cmd); | ||
| if collisions.is_empty() { | ||
| return false; | ||
| } | ||
| shell_split(cmd_part).iter().any(|token| { | ||
| let bytes = token.as_bytes(); | ||
| bytes.len() >= 2 | ||
| && bytes[0] == b'-' | ||
| && bytes[1] != b'-' | ||
| && token[1..].chars().all(|c| c.is_ascii_alphabetic()) | ||
| && token[1..].chars().any(|c| collisions.contains(&c)) | ||
| }) | ||
| } |
| fn colliding_short_flags(rtk_cmd: &str) -> &'static [char] { | ||
| match rtk_cmd { | ||
| "rtk grep" => &['v', 'h', 'l', 'm', 'V'], | ||
| "rtk ls" => &['h', 'V'], | ||
| _ => &[], | ||
| } | ||
| } |
|
This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet. |
|
Thanks for the flag by flag table, it made this easy to check. The collisions were fixed on develop by taking the flags back from clap instead of skipping the rewrite: commit 3b6b812 (2026-07-28) freed -m for GNU --max-count, commit 16228b7 (2026-08-30) stopped -l and -t shadowing native grep flags, and commit eafadce (2026-06-26) runs the engine the user actually named. On a current develop build, grep -v prints the non-matching lines, grep -rl lists file names like native grep, grep -m 1 stops at one match and grep -V prints grep's own version, so a passthrough guard would now switch rtk grep and rtk ls off for cases that already behave correctly. One case you found is still open: -h is clap's help on both subcommands, so grep -h and ls -h print rtk help instead of no-filename and human readable sizes, and that is worth an issue or a small PR against the current argument definitions. If a part of this was missed, please comment or reopen. |
Problem
The auto-rewrite hook turns
grep …/ls …intortk grep …/rtk ls …via a pure prefix substitution. Native short flags are then re-parsed by clap under the rtk subcommand, where several collide with a different semantics and silently change behavior — no error, no signal:grep -v--verbose(global)grep -h--helpgrep -l--max-lengrep -m--maxgrep -V--versionls -h--helpls -V--versionThe worst is
grep -v: it is parsed as the global verbose flag, sortk grep -v PATTERN FILEprints matching lines (and agrep: '…' in …verbose line) instead of the non-matching lines the user asked for. Silent, no error.Fix
Add a conservative passthrough guard in the rewrite engine (
registry::rewrite_command): when the native command carries a short-flag cluster containing a colliding flag, skip the rewrite (return None) so the command runs on the native binary with its intended semantics. This mirrors the existing skip-rewrite handling forgh --jsonand incompatiblecatflags.-rv→r,v).--invert-match,--max-count, …) are unaffected — they already reach ripgrep/ls intact viaextra_args.-i,-rn,-A,-c,ls -la, …) still rewrite exactly as before.Tests
Added regression tests covering each colliding flag, bundled clusters, the long-form equivalents, and the safe-flags-still-rewrite path. Verified end-to-end through the real
rtk hook claudeJSON protocol: colliding commands pass through unchanged, safe commands are rewritten.