Skip to content

fix(cargo): preserve compiler warnings in cargo test output on passing runs - #2877

Open
CryptoKrad wants to merge 1 commit into
rtk-ai:developfrom
CryptoKrad:fix/cargo-test-warnings
Open

CryptoKrad wants to merge 1 commit into
rtk-ai:developfrom
CryptoKrad:fix/cargo-test-warnings

Conversation

@CryptoKrad

Copy link
Copy Markdown

Summary

  • rtk cargo test silently dropped compile-phase warning: blocks on passing runs — an agent reading cargo test: N passed concluded the build was clean while rustc had emitted warnings. Since exit-0 runs get no tee log, the information was unrecoverable (same class as the never_worse size-only guard: the lie is smaller, so it passes).
  • Fix mirrors CargoBuildHandler's warning capture into both paths: CargoTestHandler (streaming) now streams warning blocks live, and filter_cargo_test (buffered/pipe) collects them, capped at CAP_WARNINGS with an +N more marker. The compact summary gains [N compiler warnings]; the aggregate generated N warnings line stays stripped.
  • Buffered path hard-terminates a warning block on section markers (failures: / test result: / test / ---- / error) so a short warning block can never swallow a following section.

Test plan

  • cargo fmt --all --check && cargo clippy --all-targets && cargo test — fmt clean, clippy 0 issues, 2394 passed / 0 failed (+ integration suites green)
  • 2 new regression tests: test_filter_cargo_test_all_pass_preserves_compiler_warnings (buffered) and test_streamed_cargo_test_handler_preserves_compiler_warnings (streaming) — assert warning survival, [N compiler warnings] annotation, and aggregate-line stripping
  • Manual testing on a crate with a dead-code warning: passing run now shows the warning block + cargo test: 3 passed (2 suites, 0.00s) [1 compiler warnings] (exit 0); failing run still shows failure detail with exit 101 == raw; rtk cargo build output unchanged (consistency restored between build and test)

🤖 Generated with Claude Code

https://claude.ai/code/session_01K5bb4GKf9CNKP9uYhFDvsz

…g runs

What: CargoTestHandler (streaming) and filter_cargo_test (buffered/pipe path)
previously dropped compile-phase 'warning:' blocks entirely on passing runs.
Both now capture and emit warning blocks (mirroring CargoBuildHandler) and
annotate the compact summary with '[N compiler warnings]'. The aggregate
'generated N warnings' count line stays stripped. Buffered path caps blocks
at CAP_WARNINGS with an '+N more' marker and hard-terminates warning blocks
on section markers (failures:/test result:/test /----/error) so short blocks
cannot swallow subsequent sections.

Why: exit-0 runs get no tee log, so dropped warnings were unrecoverable —
an agent reading 'cargo test: N passed' concluded the build was clean when
rustc had emitted warnings. Confirmed behaviorally pre-fix: raw 'cargo test'
showed 3 warning lines; rtk showed none (review lane finding HIGH-2,
docs/research/runs/rtk-review-2026-07-07 in Claude.md-improve).

Verified: cargo test full suite 2394 passed / 0 failed (+ 6 integration
suites green); 2 new tests (buffered + streamed handler) assert warning
survival, warning-count annotation, and aggregate-line stripping; behavioral
A/B on a real crate: passing-with-warnings now shows the block + count
(exit 0), failing run still shows failure detail with exit 101 == raw.

Open risk: pytest wrapper has the analogous success-path loss (warnings
summary + stderr) — tracked as a follow-up, mitigated operationally by
tee.mode=always in local config.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01K5bb4GKf9CNKP9uYhFDvsz
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for tackling this — dropping compile warnings on passing runs is a real gap, and preserving them on both the streaming and buffered paths is the right shape for the fix.

I found five issues that need addressing before this can go in. The first two are behaviour regressions on the compile-error path rather than gaps in the new feature, so they're the blocking ones; the last three affect the warning preservation itself.

A general note: the streaming handler (CargoTestHandler) and the buffered filter (filter_cargo_test) now implement warning capture twice, with slightly different logic. Whatever the fixes end up being, it's worth checking each one lands on both paths — three of the five below are cases where the two implementations disagree.

Separately, and unrelated to the code: the CLA is still showing as unsigned for this PR, so it can't be merged until that's done. You can sign it here: https://cla-assistant.io/rtk-ai/rtk?pullRequest=2877

}

let mut result = String::new();
result.push_str(&warnings_section);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Blocking: compile errors are silently dropped whenever a warning was captured.

Seeding result with the warnings section here, before any body content, makes the result.trim().is_empty() guard further down unreachable once a single warning block exists.

Concrete failure — a crate that both warns and fails to compile:

warning: unused variable: `x`
 --> src/lib.rs:3:9
...
error[E0425]: cannot find value `y` in this scope
error: could not compile `failproj` (lib test) due to 1 previous error

There are no test result: lines and no test failures, so the only thing in result is the warnings section. That's non-empty, so the guard is false, and both the filter_cargo_build_labeled compile-error rendering and the last-5-meaningful-lines fallback are skipped. The output is the warning block and zero errors, on a non-zero exit. On develop the same input renders cargo test: 1 errors plus the error blocks, so this is a regression.

test_filter_cargo_test_compile_error_preserves_error_header doesn't catch it because its fixture has no warning in it.

Suggested fix: build the body into its own string and test that for emptiness, prepending warnings_section only at return. One thing to watch when you do — the tail fallback will then start running in cases it previously didn't, so it needs to exclude lines the warnings section already printed, or cargo test --no-run on a warning-only crate will print them twice.

}
// Compile-phase warnings on a passing run were previously dropped
// (exit 0 → no tee → unrecoverable); stream them like CargoBuildHandler.
if !self.in_failure_section

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Blocking: every warning is reported twice on the streaming compile-error path.

Emitting warning blocks live here is fine on its own, but format_summary's compile-error branch still hands the whole raw output to filter_cargo_build_labeled, and CargoBuildHandler collects warning blocks as well as error blocks. rtk cargo test goes through run_cargo_streamed, so on a crate with a warning plus a compile error the warning block is streamed once from here and then rendered a second time inside the summary.

The fix needs to suppress warning-block rendering for that one call site only — build, check and clippy share that helper and should keep rendering their warnings as they do today.

}
// Aggregate count line ("warning: `x` generated N warnings"); individual
// warning blocks are already streamed via is_block_start below.
if line.starts_with("warning:") && line.contains("generated") && line.contains("warning") {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This heuristic swallows genuine warnings.

line.starts_with("warning:") && line.contains("generated") && line.contains("warning") — the third clause is tautological given the first, so this reduces to starts_with("warning:") && contains("generated"). Any real rustc or clippy warning whose message happens to contain "generated" matches, for example in a prost/tonic crate:

warning: unused import: `crate::generated::pb`
 --> src/lib.rs:4:5

should_skip returns true, so is_block_start never fires, self.warnings isn't incremented, and the trailing --> / | / = note: lines match nothing and are dropped. The warning disappears entirely and [N compiler warnings] under-counts — the exact failure this PR is meant to fix.

Anchoring on the real shape of the aggregate line would avoid it, e.g. matching a generated \d+ warnings?$ suffix rather than a substring.

The same expression appears in the buffered path (filter_cargo_test), where the continue leaves in_warning_block false and the block body falls through to the failure detector and is discarded. Worth fixing in both places. The buffered copy also isn't gated on !in_failure_section, so a failing test whose captured stdout contains such a line loses it from the failure block.

fn is_block_continuation(&mut self, line: &str, _block: &[String]) -> bool {
fn is_block_continuation(&mut self, line: &str, block: &[String]) -> bool {
if self.in_warning_block {
return !(line.trim().is_empty() && block.len() > 3);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

block.len() > 3 means short warning blocks never terminate.

The blank-line terminator requires more than 3 lines already buffered, so span-less warnings never close on their trailing blank:

warning: unused manifest key: package.metadata.foo
warning: profiles for the non root package will be ignored

Running unittests ... and Doc-tests ... aren't in should_skip (it covers Compiling/Downloading/Downloaded/Finished, plus the lowercase running harness line), so they're treated as continuations and glued into the warning block.

The two paths then fail differently: the buffered filter emits that noise inside the warnings section, while the streaming handler holds the block open past test result: (consumed by should_skip) so it only surfaces at flush() — out of order, after everything else.

Terminating on the first blank line regardless of block length, or keying off the diagnostic's indentation, would handle both.


let mut warnings_section = String::new();
if !warnings.is_empty() {
for block in warnings.iter().take(CAP_WARNINGS) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The cap is only applied on the path that isn't used for cargo test.

This caps warning blocks at CAP_WARNINGS with a … +N more warnings marker, but the streaming handler emits every warning block with no cap at all — and streaming is what rtk cargo test actually runs.

On a crate with 200 dead-code warnings, a passing rtk cargo test now emits all 200 blocks, which is a significant output regression on the most frequently run command, while the same raw output through the buffered path shows 10. Same input, two materially different outputs.

The cap should be enforced on the streaming side too, so both paths agree.

@KuSh KuSh self-assigned this Aug 29, 2026
yungSNAT added a commit to yungSNAT/PascalsLetter that referenced this pull request Nov 3, 2026
* fix(cargo): keep compiler warnings on a passing cargo test

Ported from CryptoKrad/rtk b95acce (Apache-2.0; rtk-ai#2877). The
buffered filter keeps warnings out of the result until the end, so a
compile error after a warning still reaches the build-error fallback.

* docs(readme): list the cargo test change beside the other command fixes
@rtk-release-bot

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants