From 63209ae0b95d31f28d14be5125fba3f61f5835e6 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Wed, 15 Oct 2025 21:05:29 -0400 Subject: [PATCH] printer: fix `--stats` for `--json` Somehow, the JSON printer seems to have never emitted correct summary statistics. And I believe #3178 is the first time anyone has ever reported it. I believe this bug has persisted for years. That's surprising. Anyway, the problem here was that we were bailing out of `finish()` on the sink if we weren't supposed to print anything. But we bailed out before we tallied our summary statistics. Obviously we shouldn't do that. Fixes #3178 --- CHANGELOG.md | 2 ++ crates/printer/src/json.rs | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 439c0b3..c52964f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,8 @@ Bug fixes: Statically compile PCRE2 into macOS release artifacts on `aarch64`. * [BUG #3173](https://github.com/BurntSushi/ripgrep/issues/3173): Fix ancestor ignore filter bug when searching whitelisted hidden files. +* [BUG #3178](https://github.com/BurntSushi/ripgrep/discussions/3178): + Fix bug causing incorrect summary statistics with `--json` flag. * [BUG #3179](https://github.com/BurntSushi/ripgrep/issues/3179): Fix gitignore bug when searching absolute paths with global gitignores. * [BUG #3180](https://github.com/BurntSushi/ripgrep/issues/3180): diff --git a/crates/printer/src/json.rs b/crates/printer/src/json.rs index f108c05..f2a42ba 100644 --- a/crates/printer/src/json.rs +++ b/crates/printer/src/json.rs @@ -817,10 +817,6 @@ impl<'p, 's, M: Matcher, W: io::Write> Sink for JSONSink<'p, 's, M, W> { _searcher: &Searcher, finish: &SinkFinish, ) -> Result<(), io::Error> { - if !self.begin_printed { - return Ok(()); - } - self.binary_byte_offset = finish.binary_byte_offset(); self.stats.add_elapsed(self.start_time.elapsed()); self.stats.add_searches(1); @@ -830,6 +826,9 @@ impl<'p, 's, M: Matcher, W: io::Write> Sink for JSONSink<'p, 's, M, W> { self.stats.add_bytes_searched(finish.byte_count()); self.stats.add_bytes_printed(self.json.wtr.count()); + if !self.begin_printed { + return Ok(()); + } let msg = jsont::Message::End(jsont::End { path: self.path, binary_offset: finish.binary_byte_offset(),