Skip to content

Discard rust_doc_test's captured stdout when the doctests pass - #87

Merged
dzbarsky merged 1 commit into
hermeticbuild:mainfrom
anagrius:rustdoc-test-reproducible-stdout
Oct 13, 2026
Merged

dzbarsky merged 1 commit into
hermeticbuild:mainfrom
anagrius:rustdoc-test-reproducible-stdout

Conversation

@anagrius

@anagrius anagrius commented Oct 11, 2026 •

Copy link
Copy Markdown

We had a problem where rust_doc_test wrote different outputs for the same cache key on different CI runners. Our reproducibility check caught a target with zero doctests whose captured stdout said finished in 0.00s on one runner and 0.01s on another.

rust_doc_test runs rustdoc --test in a build action and declares its stdout, stderr and exit code as outputs. The test only replays them. libtest prints results in completion order and reports wall-clock durations, and edition 2024 merged doctests add another timing line, so a passing run writes different stdout every time. Nothing reads that stdout except the replay, and the pass or fail result comes from the exit code file. Each time the bytes change, the replay misses the cache.

We considered several alternatives before settling on this one.

The first version of this PR normalized libtest's output: it sorted the result lines and stripped the durations. A review showed that matching lines by prefix also rewrote what the doctests themselves print, such as a line starting with test inside a failure, and that it missed other libtest output: --quiet marks in completion order, --show-output sections, --report-time durations and slow-test notices. We then wrote a parser for libtest's exact formats. It reached about 800 lines, and memory still grew with output size. That is a lot to review for output that only a replay reads.

Running the doctests with --test-threads=1 would fix the order, but the durations remain and the doctests lose their parallelism. Normalizing in a separate action, or in the replay runner, still leaves the raw stdout as a nondeterministic cached output. Running doctests at test time again, as upstream rules_rust does, would undo this fork's deliberate move of that work into the build. Exempting the action in our own reproducibility check would hide the noise for us, but the replay would still miss the cache for everyone.

Discarding the stdout of a passing run avoids parsing libtest at all, because the exit code already carries the result.

We solved it by adding --discard-stdout-on-success to the process wrapper and passing it from rust_doc_test. When rustdoc exits with 0, the wrapper empties the stdout file, so a passing run always writes the same bytes. A failing run keeps rustdoc's full output, because that is where the failure details are. Those details, such as thread ids and temporary paths, still differ between runs. The cost is that the test log of a passing rust_doc_test no longer lists the doctests that ran.

We added process wrapper tests for both paths: a passing child leaves an empty stdout, and a failing child keeps its stdout and records exit code 1. Each test fails when the wrapper is changed to break its path. The RustdocTest analysis test checks for the new flag. In this repo, //test/unit/rustdoc:lib_doctest and //test/chained_direct_deps:mod3_doc_test wrote an empty stdout over three forced reruns, and bazel test //test/process_wrapper/... //util/process_wrapper:process_wrapper_test //test/unit/rustdoc:all //test/chained_direct_deps:all passes.

Assisted-by: Claude

`rust_doc_test` runs `rustdoc --test` in a build action and declares its stdout, stderr and exit code as outputs, which the test only replays. libtest prints results in completion order with wall-clock durations, so a passing run writes different stdout every time, and the replay misses the cache. Nothing reads that stdout except the replay; the result comes from the exit code.

Add `--discard-stdout-on-success` to the process wrapper and pass it from `rust_doc_test`. When rustdoc exits with 0, the wrapper empties the stdout file, so a passing run always writes the same bytes. A failing run keeps rustdoc's full output for the failure details.

Assisted-by: Claude
@anagrius
anagrius force-pushed the rustdoc-test-reproducible-stdout branch from 2207175 to c9349f3 Compare October 11, 2026 11:09
@anagrius anagrius changed the title Make rust_doc_test's captured stdout reproducible Discard rust_doc_test's captured stdout when the doctests pass Oct 11, 2026
@anagrius
anagrius marked this pull request as ready for review October 11, 2026 11:12

@dzbarsky dzbarsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, that's a nice catch!

@dzbarsky
dzbarsky merged commit fcba899 into hermeticbuild:main Oct 13, 2026
0 of 3 checks 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.

2 participants