@ 261027.5

251015.2 · 63209ae0b95d31f28d14be5125fba3f61f5835e6

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

reading declarations

Changes to crates/printer/src/json.rs. All files

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(),