fix(grep): adjust the command to fall through if the output would already be as small as possible#1456
Conversation
There was a problem hiding this comment.
Blockers
1. Missing --no-ignore-vcs in passthrough path
The duplicated rg command construction forgot --no-ignore-vcs (added by #791 to fix false negatives on gitignored files).
Result: rtk grep "pattern" . searches gitignored files, rtk grep -c "pattern" . does not. Same pattern, different results depending on output flag.
2. -l / --files-with-matches is dead code
Clap captures -l as max_len (#[arg(short = 'l', ...)]) before it reaches extra_args. Users get a parse error, not file listing. Same class of bug this PR fixed for -c.
Root cause: both bugs come from duplicating the command construction
The passthrough path copies ~25 lines of rg command setup instead of reusing it. Fix both bugs at once by moving the has_format_flag check to after execution, before formatting:
// existing rg command construction (unchanged, includes --no-ignore-vcs)
let result = exec_capture(&mut rg_cmd).or_else(|_| { /* grep fallback */ })?;
if has_format_flag(extra_args) {
print!("{}", result.stdout);
if !result.stderr.is_empty() {
eprintln!("{}", result.stderr.trim());
}
timer.track_passthrough(...);
return Ok(result.exit_code);
}
// existing formatter (unchanged)No duplication, no flag drift, --no-ignore-vcs inherited automatically.
|
I wasn't aware of #791 when I pushed this up. Sorry! I will update it accordingly. |
67ba4b9 to
c873c6d
Compare
…eady be as small as possible
c873c6d to
021827c
Compare
|
@aeppling I went ahead and made the change with slight tweaks to avoid empty spaces in the db when there was a pass-through. |
|
LGTM, thanks for contributing @JBF1991 Will be released during the week |
Summary
Fix
rtk grepcompatibility with standard grep format flags (-c,-l,-L,-o,-Z). These flags change output shape so fundamentally that RTK's grouped formatter produced garbage or ignored the flag entirely.Root Cause
-ccount flag ignored — returns match summary + context instead of line count #1452rtk grephardcoded-n --no-headingintorgregardless of user flags.file:line:contentformat, so-coutput (file:count) was misinterpreted.context_only: boolhad#[arg(short, long)], which captured-cbefore it reachedextra_args.Test plan
All tests pass:
$ rtk cargo test --all cargo test: 1680 passed, 6 ignoredFormat flags now produce raw output:
-crtk grep -c "fn run" src/cmds/system/grep_cmd.rs1-lrtk grep -l "fn run" src/cmds/system/grep_cmd.rssrc/cmds/system/grep_cmd.rs-Lrtk grep -L "nonexistent" src/cmds/system/grep_cmd.rssrc/cmds/system/grep_cmd.rs-ortk grep -o "fn" src/cmds/system/grep_cmd.rsfn\nfn\nfn...Normal path unchanged:
cargo fmt --all && cargo clippy --all-targets && cargo testrtk <command>output inspected