Skip to content

fix(rewrite): passthrough native grep/ls when short flags collide with rtk options - #2334

Closed
BurpeeKiller wants to merge 1 commit into
rtk-ai:developfrom
BurpeeKiller:omnirealm/flag-passthrough
Closed

BurpeeKiller wants to merge 1 commit into
rtk-ai:developfrom
BurpeeKiller:omnirealm/flag-passthrough

Conversation

@BurpeeKiller

Copy link
Copy Markdown

Problem

The auto-rewrite hook turns grep …/ls … into rtk 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:

native clap meaning under rtk native meaning effect
grep -v --verbose (global) invert-match returns matching lines instead of non-matching — result inverted
grep -h --help no-filename help text, 0 matches
grep -l --max-len files-with-matches wrong arg consumed
grep -m --max max-count shifted semantics
grep -V --version version rtk version, not grep
ls -h --help human-readable help instead of listing
ls -V --version — rtk version

The worst is grep -v: it is parsed as the global verbose flag, so rtk grep -v PATTERN FILE prints matching lines (and a grep: '…' 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 for gh --json and incompatible cat flags.

  • Handles bundled clusters (-rv → r, v).
  • Long flags (--invert-match, --max-count, …) are unaffected — they already reach ripgrep/ls intact via extra_args.
  • Safe flags (-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 claude JSON protocol: colliding commands pass through unchanged, safe commands are rewritten.

…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.
Copilot AI review requested due to automatic review settings June 8, 2026 19:10
@CLAassistant

CLAassistant commented Jun 8, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 grep and rtk ls.
  • Added regression tests covering passthrough vs rewrite behavior for colliding/non-colliding flags.

Comment thread src/discover/registry.rs
Comment on lines +899 to +912
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))
})
}
Comment thread src/discover/registry.rs
Comment on lines +882 to +888
fn colliding_short_flags(rtk_cmd: &str) -> &'static [char] {
match rtk_cmd {
"rtk grep" => &['v', 'h', 'l', 'm', 'V'],
"rtk ls" => &['h', 'V'],
_ => &[],
}
}
@rtk-release-bot

Copy link
Copy Markdown
Contributor

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.

@KuSh

KuSh commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

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.

@KuSh KuSh closed this Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants