Skip to content

fix: print search stats after matches - #146

Merged
Shengyu Fu (shengyfu) merged 5 commits into
microsoft:mainfrom
rksharma-owg:codex/fix-stats-order
Sep 9, 2026
Merged

Shengyu Fu (shengyfu) merged 5 commits into
microsoft:mainfrom
rksharma-owg:codex/fix-stats-order

Conversation

@rksharma-owg

Copy link
Copy Markdown
Contributor

Closes #145

Summary

  • Flush buffered match output before human-readable --stats messages.
  • Defer indexed-search query-plan stats until after matches are written.
  • Add brute-force and indexed regression tests using a shared stdout/stderr stream.

Previously, stdout buffering caused --stats to appear before matches when stdout and stderr were combined, so the summary was easy to miss during large searches.

Validation

  • Regression test failed against the pre-change implementation with stats before the match.
  • Targeted brute-force and indexed ordering tests pass after the fix.
  • cargo test --workspace passes.
  • cargo fmt --all -- --check passes.
  • cargo clippy --workspace --all-targets -- -D warnings passes.
  • Manual CLI verification confirms matches precede the final stats line.

Copilot AI balanced review requested due to automatic review settings September 8, 2026 19:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new indexed ordering test should use an isolated --index-path (consistent with the existing indexed tests) to avoid writing .tgrep into the searched tree and reduce brittleness.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes --stats output ordering so that human-readable stats are emitted after buffered match output when stdout/stderr are combined (aligning behavior with ripgrep), and adds regression tests to prevent the ordering from regressing.

Changes:

  • Flush stdout match output before printing human-readable --stats lines in brute-force, local-index, and server search paths.
  • Defer local-index “Query plan” stats until after matches are written.
  • Add regression tests validating match output precedes stats when stdout and stderr are merged.
File summaries
File Description
tgrep-cli/src/search.rs Flushes buffered match output before emitting --stats lines; defers query-plan stats until after matches.
tgrep-cli/tests/ripgrep_compat.rs Adds brute-force and indexed regression tests that validate match output precedes stats on a combined stream.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tgrep-cli/tests/ripgrep_compat.rs Outdated
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Signed-off-by: Shengyu Fu <[email protected]>
Copilot AI review requested due to automatic review settings September 9, 2026 00:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The new combined-stream tests keep write handles open while reading the merged output file, which can make the tests less robust (especially across platforms/filesystems).

Review details

Suppressed comments (2)

Previously missed (1) — in code that hasn't changed since the last review.

tgrep-cli/tests/ripgrep_compat.rs:187

  • The test reads merged_path while the parent process still holds write handles (stdout/merged) open. On some platforms this can cause sharing/flush issues and makes the test less robust. Drop the write handles before reading the file contents.

This issue also appears on line 222 of the same file.

tgrep-cli/tests/ripgrep_compat.rs:224

  • As above, drop the merged-output write handles before reading the file so the test is robust across platforms and filesystems.
    assert!(status.success());

    let output = fs::read_to_string(merged_path).unwrap();
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 9, 2026 01:43
@rksharma-owg

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback in commit 71a5e84:

  • The indexed regression already uses an isolated --index-path in the current branch.
  • Wrapped both combined-output subprocess sections so their file handles are dropped before reading the merged output.
  • Ran cargo fmt --check.
  • Ran cargo test --test ripgrep_compat: 220 passed.

Copilot review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is low-risk, directly addresses the reported ordering problem, and includes targeted regression coverage.

Review details

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

tgrep-cli/src/search.rs:1327

  • The --stats message for a single-file brute-force search says "(1 files)", which is grammatically incorrect and user-visible in --stats output.
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 9, 2026 01:46
@rksharma-owg

Copy link
Copy Markdown
Contributor Author

Addressed the final review note in commit 2ce8336: single-file --stats output now says 1 file instead of 1 files.

Revalidated with cargo fmt --check and cargo test --test ripgrep_compat (220 passed).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The new pre-stats writer.flush()? calls can propagate BrokenPipe and prevent --stats from being printed to stderr in common pipeline scenarios where stdout is closed early.

Review details

Suppressed comments (4)

Previously missed (1) — in code that hasn't changed since the last review.

tgrep-cli/src/search.rs:1088

  • writer.flush()? here is only needed to preserve stdout/stderr ordering, but it can return BrokenPipe when stdout is closed early (e.g., tgrep ... | head). Using ? will abort before writing the --stats summary to stderr, which is surprising since stderr is still writable. Consider ignoring BrokenPipe for this pre-stats flush and only erroring on other flush failures.

This issue also appears in the following locations of the same file:

  • line 1204
  • line 1324
  • line 1369

tgrep-cli/src/search.rs:1204

  • This pre-stats writer.flush()? is intended for output ordering, but ? will propagate BrokenPipe and prevent the --stats lines from being printed to stderr when stdout is closed early by a downstream consumer. Consider ignoring BrokenPipe for this flush (only for ordering) so stats can still be emitted.
        writer.flush()?;

tgrep-cli/src/search.rs:1324

  • writer.flush()? before printing stats can return BrokenPipe when stdout is closed early (common in pipelines). Using ? here will skip the stats line entirely even though it is written to stderr. Consider ignoring BrokenPipe for this ordering flush so --stats remains observable.
            writer.flush()?;

tgrep-cli/src/search.rs:1369

  • Same concern as the other stats flush sites: propagating BrokenPipe here can prevent --stats from being printed to stderr when stdout is closed early. If the goal is just ordering, consider ignoring BrokenPipe for this flush.
        writer.flush()?;
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 9, 2026 01:54
@rksharma-owg

Copy link
Copy Markdown
Contributor Author

Addressed the latest review feedback in commit 5828d3f:

  • Added flush_before_stats, which ignores only BrokenPipe for the ordering-only pre-stats flushes.
  • Other flush errors still propagate normally.
  • Revalidated with cargo fmt --check, cargo test --test ripgrep_compat (220 passed), and git diff --check.

Copilot review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is narrowly scoped, behavior is covered by targeted regression tests, and the updated output ordering matches the stated requirements without introducing conflicting behavior in the reviewed paths.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@shengyfu
Shengyu Fu (shengyfu) merged commit 50f5d8f into microsoft:main Sep 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--stats should report at the end

3 participants