From 8b5d3d1c1e3b0e32f22aa634d000b314f6d4d465 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sun, 21 Sep 2025 13:56:40 -0400 Subject: [PATCH] printer: hack in a fix for `-l/--files-with-matches` when using `--pcre2 --multiline` with look-around The underlying issue here is #2528, which was introduced by commit efd9cfb2fc1f0233de9eda4c03416d32ef2c3ce8 which fixed another bug. For the specific case of "did a file match," we can always assume the match count is at least 1 here. But this doesn't fix the underlying problem. Fixes #3139 --- crates/printer/src/summary.rs | 6 ++++- tests/regression.rs | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/crates/printer/src/summary.rs b/crates/printer/src/summary.rs index 86ce095..615abd2 100644 --- a/crates/printer/src/summary.rs +++ b/crates/printer/src/summary.rs @@ -683,7 +683,11 @@ impl<'p, 's, M: Matcher, W: WriteColor> Sink for SummarySink<'p, 's, M, W> { true }, )?; - count + // Because of `find_iter_at_in_context` being a giant + // kludge internally, it's possible that it won't find + // *any* matches even though we clearly know that there is + // at least one. So make sure we record at least one here. + count.max(1) }; if is_multi_line { self.match_count += sink_match_count; diff --git a/tests/regression.rs b/tests/regression.rs index 2f1ba85..b3d7fb1 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -1544,3 +1544,44 @@ rgtest!( cmd.args(&["--files", "-g", "[abc"]).assert_err(); } ); + +rgtest!( + r3139_multiline_lookahead_files_with_matches, + |dir: Dir, _cmd: TestCommand| { + // Only PCRE2 supports look-around. + if !dir.is_pcre2() { + return; + } + dir.create( + "test", + "\ +Start \n \n\n \ +XXXXXXXXXXXXXXXXXXXXXXXXXX\n \ +YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY\n \ +\n thing2 \n\n", + ); + + let got = dir + .command() + .args(&[ + "--multiline", + "--pcre2", + r"(?s)Start(?=.*thing2)", + "test", + ]) + .stdout(); + eqnice!("Start \n", got); + + let got = dir + .command() + .args(&[ + "--multiline", + "--pcre2", + "--files-with-matches", + r"(?s)Start(?=.*thing2)", + "test", + ]) + .stdout(); + eqnice!("test\n", got); + } +);