Skip to content

Cargo does not show ouput (e.g. stdout) from doctests with --nocapture #142295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
w1th0utnam3 opened this issue Jun 10, 2025 · 3 comments
Closed
Labels
A-doctests Area: Documentation tests, run by rustdoc

Comments

@w1th0utnam3
Copy link

Problem

As described in this closed issue: rust-lang/cargo#1732, there is no output (e.g. stdout) printed from doctests ran through cargo (e.g. cargo test --doc -- --nocapture) even when explicitly providing the --nocapture flag. Unfortunately the previous issue was closed without being fully resolved as indicated by recent comments.

Support for --nocapture was added to rustdoc in this PR: #86230

A PR to support this in cargo was proposed but it was never resolved: rust-lang/cargo#9705

Steps

  1. cargo new --lib doc_testing
  2. Add the following code:
/// This function prints a message to the console.
///
/// ```rust
/// println!("Test");
/// doc_testing::test_function();
/// ```
pub fn test_function() {
    println!("This is a test function.");
}
  1. Run cargo test --doc -- --nocapture

I expect cargo to show the output of the two print statements, however the output is only:

cargo test --doc -- --nocapture
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.02s
   Doc-tests doc_testing

running 1 test
test src/lib.rs - test_function (line 3) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.87.0 (99624be96 2025-05-06)
release: 1.87.0
commit-hash: 99624be96e9d213b0e9b1e36451271f24e4a41d8
commit-date: 2025-05-06
host: aarch64-apple-darwin
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.7.1 (sys:0.4.80+curl-8.12.1 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: Mac OS 15.5.0 [64-bit]
@w1th0utnam3 w1th0utnam3 added the C-bug Category: This is a bug. label Jun 10, 2025
@epage
Copy link
Contributor

epage commented Jun 10, 2025

Looking at cargo test --doc -- --nocapture, it appears we are passing the argument down to rustdoc to be process. We are doing so with --test-args --nocapture. That can cause issues for some CLI parsers, so to verify its being passed down correctly, I then ran cargo test --doc -- --quiet and rustdoc correctly handled that.

So this seems more like a rustdoc issue, so transferring this over.

@epage epage transferred this issue from rust-lang/cargo Jun 10, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 10, 2025
@epage epage added A-doctests Area: Documentation tests, run by rustdoc and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 10, 2025
@ehuss
Copy link
Contributor

ehuss commented Jun 10, 2025

I'm going to close this as a duplicate of #98550.

The solution for now is to use RUSTDOCFLAGS="-Zunstable-options --nocapture". The difference is whether or not this uses --test-args or to rustdoc directly. I don't know what the answer is for how this should actually work.

@w1th0utnam3
Copy link
Author

Oh, sorry for reporting it again, I didn't find this issue from the other issues I mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-doctests Area: Documentation tests, run by rustdoc
Projects
Development

No branches or pull requests

4 participants