Skip to content
Myr

cli: document that `-c/--count` can be inconsistent with `-l/--files-with-matches`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This is unfortunate, but is a known bug that I don't think can be fixed without either making `-l/--files-with-matches` much slower or changing what "binary filtering" means by default. In this PR, we document this inconsistency since users may find it quite surprising. The actual work-around is to disable binary filtering with the `--binary` flag. We add a test confirming this behavior. Closes #3131

authored by Andrew Gallant · committed by Andrew Gallant · parent c1fc6a5 · Browse files · 4 files+56 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+56 added−0 removed

  • tests:+38 added−0 removed
  • other:+18 added−0 removed

4 files+56 added−0 removed

modified:crates/core/flags/defs.rs+16 added
1274 unmodified lines
1275is a match. The \flag{with-filename} flag can be used to force printing the1276file path in this case. If you need a count to be printed regardless of whether1277there is a match, then use \flag{include-zero}.1278.sp1279Note that it is possible for this flag to have results inconsistent with1280the output of \flag{files-with-matches}. Notably, by default, ripgrep tries1281to avoid searching files with binary data. With this flag, ripgrep needs to1282search the entire content of files, which may include binary data. But with1283\flag{files-with-matches}, ripgrep can stop as soon as a match is observed,1284which may come well before any binary data. To avoid this inconsistency without1285disabling binary detection, use the \flag{binary} flag.1286.sp1287This overrides the \flag{count-matches} flag. Note that when \flag{count}1288is combined with \flag{only-matching}, then ripgrep behaves as if1289\flag{count-matches} was given.1290"
900 unmodified lines
2191 fn doc_long(&self) -> &'static str {2192 r"2193Print only the paths with at least one match and suppress match contents.2194.sp2195Note that it is possible for this flag to have results inconsistent with the2196output of \flag{count}. Notably, by default, ripgrep tries to avoid searching2197files with binary data. With this flag, ripgrep might stop searching before2198the binary data is observed. But with \flag{count}, ripgrep has to search the2199entire contents to determine the match count, which means it might see binary2200data that causes it to skip searching that file. To avoid this inconsistency2201without disabling binary detection, use the \flag{binary} flag.2202.sp2203This overrides \flag{files-without-match}.2204"2205 }2206
5562 unmodified lines
modified:tests/binary.rs+37 added
427 unmodified lines
428hay:1867:\"And yet you say he is not a medical student?\"429";430 eqnice!(expected, cmd.stdout());431});432433// See: https://github.com/BurntSushi/ripgrep/issues/3131434rgtest!(435 matching_files_inconsistent_with_count,436 |dir: Dir, _cmd: TestCommand| {437 let mut file1 = String::new();438 file1.push_str("cat here\n");439 for _ in 0..150_000 {440 file1.push_str("padding line\n");441 }442 file1.push_str("\x00");443444 dir.create("file1.txt", &file1);445 dir.create("file2.txt", "cat here");446447 let got = dir.command().args(&["--sort=path", "-l", "cat"]).stdout();448 eqnice!("file1.txt\nfile2.txt\n", got);449450 // This is the inconsistent result that can't really be avoided without451 // either making `-l/--files-with-matches` much slower or changing452 // what "binary filtering" means.453 let got = dir.command().args(&["--sort=path", "-c", "cat"]).stdout();454 eqnice!("file2.txt:1\n", got);455456 let got = dir457 .command()458 .args(&["--sort=path", "-c", "cat", "--binary"])459 .stdout();460 eqnice!("file1.txt:1\nfile2.txt:1\n", got);461462 let got = dir463 .command()464 .args(&["--sort=path", "-c", "cat", "--text"])465 .stdout();466 eqnice!("file1.txt:1\nfile2.txt:1\n", got);467 }468);
modified:tests/regression.rs+1 added
1543 unmodified lines
1544 cmd.args(&["--files", "-g", "[abc"]).assert_err();1545 }1546);15471548// See: https://github.com/BurntSushi/ripgrep/issues/31391549rgtest!(1550 r3139_multiline_lookahead_files_with_matches,1551 |dir: Dir, _cmd: TestCommand| {1552 // Only PCRE2 supports look-around.
36 unmodified lines
modified:CHANGELOG.md+2 added
29 unmodified lines
30* [BUG #3100](https://github.com/BurntSushi/ripgrep/pull/3100):31 Preserve line terminators when using `-r/--replace` flag.32* [BUG #3108](https://github.com/BurntSushi/ripgrep/issues/3108):33 Fix a bug where `-q --files-without-match` inverted the exit code.34* [BUG #3131](https://github.com/BurntSushi/ripgrep/issues/3131):35 Document inconsistency between `-c/--count` and `--files-with-matches`.36* [BUG #3140](https://github.com/BurntSushi/ripgrep/issues/3140):37 Ensure hyphens in flag names are escaped in the roff text for the man page.38* [BUG #3155](https://github.com/BurntSushi/ripgrep/issues/3155):39 Statically compile PCRE2 into macOS release artifacts on `aarch64`.
1737 unmodified lines