Skip to content

fix(grep): stop -l/-m/-t shadowing native grep flags (adopt upstream #2628) - #91

Merged
kylehgc merged 2 commits into
developfrom
fix/grep-flag-shadowing
Aug 1, 2026
Merged

kylehgc merged 2 commits into
developfrom
fix/grep-flag-shadowing

Conversation

@kylehgc

@kylehgc kylehgc commented Aug 1, 2026

Copy link
Copy Markdown
Owner

Closes #52. Adopts upstream rtk-ai/rtk#2628 by @devnulled.

Commits

Commit Origin
fix(grep): stop -l/-m/-t shadowing native grep flags Cherry-picked from upstream rtk-ai#2628 (Brandon Harper), byte-identical — verified by diffing the two patches; only hunk offsets differ (fork's main.rs is longer).

No fork-authored amendments needed: the upstream commit already carries clap-layer characterization tests covering the fix and the surrounding routing.

What the bug actually is on current develop

The ticket said rtk grep -l <pattern> hard-fails with exit 2. That's stale — run_fallback now rescues clap parse failures by executing raw grep. The live defect is worse: when the shadowed flag's value happens to parse, clap silently eats the flag and applies the wrong semantics with no error:

  • -m 5 (native max-count) binds to rtk's --max — flag never reaches grep
  • -l 40 binds 40 to --max-len — -l (files-with-matches) is dropped, output shows lines instead of filenames
  • -t rust binds to --file-type, which the handler ignores entirely (file_type: _ in main.rs) — flag silently vanishes

And when the value doesn't parse (-l fn), the clap failure routes through the raw-exec fallback: output is correct but unfiltered, and a parse failure is recorded on every such call.

Repro (debug binary, Git Bash on Windows)

Before (develop @ 62430db):

$ rtk grep -m 5 "fn " src/core/guard.rs        # native prints 5 lines
pub fn never_worse<'a>(raw: &'a str, filtered: &'a str) -> &'a str {
    fn keeps_filtered_when_smaller() {
    ... 8 lines total — -m was eaten by rtk, grep never saw it (exit 0, silent)

After (this branch):

$ rtk grep -m 5 "fn " src/core/guard.rs
pub fn never_worse<'a>(raw: &'a str, filtered: &'a str) -> &'a str {
    fn keeps_filtered_when_smaller() {
    fn falls_back_to_raw_when_filtered_bigger() {
    fn tie_keeps_filtered() {
    fn token_boundary_follows_estimate_tokens() {
exit: 0        # identical to /usr/bin/grep -m 5
$ rtk grep -l fn src/core/guard.rs
src/core/guard.rs
exit: 0        # files-with-matches, now via the filtered path, not the parse-error fallback

-t now forwards faithfully; native grep rejects it visibly (grep: unknown option -- t) instead of rtk silently discarding it. Type filtering remains rtk rg -t.

Blast radius checked

Quality gate

cargo fmt --all (no changes) && cargo clippy --all-targets (zero warnings) && cargo test --all — 2599 passed, 0 failed (x86_64 host toolchain, Windows).

Deliberately not fixed

  • --file-type remains parsed-but-ignored (file_type: _) — dead knob inherited from upstream; removing it is upstream's call, not adoption scope.
  • Post-pattern flag ordering quirks and search.rs-internal flag handling — covered by the sibling upstream PRs listed above.

🤖 Generated with Claude Code

devnulled and others added 2 commits August 1, 2026 12:49
The Grep variant's own short options -l (max_len), -m (max) and -t
(file_type) shadowed native grep/rg flags of the same letter. A leading
`grep -l PATTERN` rewritten by the hook to `rtk grep -l PATTERN` made clap
parse the pattern as a usize and abort (exit 2 -> raw fallback, zero token
savings); -m and -t silently applied the wrong semantics.

Make these tuning options long-only (--max-len/--max/--file-type) so the
native flags route to extra_args and are parsed by grep_cmd.rs, which
already handles them. Completes the -v/-n/pattern/path cleanup from
84616d1; complements the rg-only-flag handling tracked in rtk-ai#2224.

Add clap-layer tests for the fix plus characterization tests for the
previously-untested routing (combined -rn clusters, -v invert, -nE
alternation, long-option binding, the trailing_var_arg ordering gotcha,
--version passthrough and -- handling).

Refs: rtk-ai#2614, rtk-ai#1604, rtk-ai#1436
…_cmd.rs

The cherry-picked rtk-ai#2628 comments reference src/cmds/system/grep_cmd.rs,
which upstream has since reworked into search.rs. Comment-only amendment;
kept separate from the contributor's commit per adoption policy.

Co-Authored-By: Claude Fable 5 <[email protected]>
@kylehgc

kylehgc commented Aug 1, 2026

Copy link
Copy Markdown
Owner Author

Review round (pre-merge)

Ran the two-axis /code-review (Standards + Spec, parallel sub-agents) against develop.

Standards: no hard violations. Noted: new tests use .unwrap() where rust-patterns.md prefers .expect(\"reason\") in tests — consistent with the ~40 existing .unwrap()s in this test module and byte-identical to upstream, so left as-is. Tuple-return smell in parse_grep (#[allow(clippy::type_complexity)]) acknowledged; not worth deviating from the verbatim cherry-pick.

Spec: intent fully met — traced -l through search.rs has_format_flag (faithful passthrough) and -m/-t through VALUE_FLAGS_SHORT (values forwarded verbatim); all 14 new clap-layer tests pass. One actionable finding: the adopted comments referenced src/cmds/system/grep_cmd.rs, which upstream has since reworked into search.rs — fixed in the separate fork-authored amendment 0f1497e (comment-only, 7 lines), keeping the contributor's commit untouched.

Quality gate re-run after the amendment: fmt clean, clippy zero warnings, 2599 unit tests + integration suites all green.

@kylehgc
kylehgc merged commit 2566bbc into develop Aug 1, 2026
3 checks passed
@kylehgc
kylehgc deleted the fix/grep-flag-shadowing branch August 1, 2026 17:00
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.

Adopt upstream #2628: grep - stop -l/-m/-t shadowing native grep flags

2 participants