Skip to content

doctests: Assertion panic message is escaped and is unreadable terrible on 2024 edition. #137970

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
aDotInTheVoid opened this issue Mar 3, 2025 · 8 comments · Fixed by #139328
Labels
A-doctests Area: Documentation tests, run by rustdoc A-edition-2024 Area: The 2024 edition A-panic Area: Panicking machinery C-bug Category: This is a bug. I-edition-triaged Issue: This issue has been reviewed and triaged by the Edition team. P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@aDotInTheVoid
Copy link
Member

I tried this code:

//! ```rust
//! assert_eq!(2 + 2, 5);
//! ```

Running the doctests gives a terrible error message:

gh-aDotInTheVoid@dev-desktop-eu-1:~/tmp$ rustdoc +nightly --test ./mcve.rs --edition=2024

running 1 test
test ./mcve.rs - (line 1) ... FAILED

failures:

---- ./mcve.rs - (line 1) stdout ----
Error: "\nthread 'main' panicked at /tmp/rustdoctestKogNj0/doctest_2024.rs:11:1:\nassertion `left == right` failed\n  left: 4\n right: 5\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n"


failures:
    ./mcve.rs - (line 1)

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

It should instead format the assertion error well, like it does on edition=2015

$ rustdoc +nightly --test ./mcve.rs --edition=2015

running 1 test
test ./mcve.rs - (line 1) ... FAILED

failures:

---- ./mcve.rs - (line 1) stdout ----
Test executable failed (exit status: 101).

stderr:

thread 'main' panicked at ./mcve.rs:3:1:
assertion `left == right` failed
  left: 4
 right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace



failures:
    ./mcve.rs - (line 1)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.16s
It works as expected on beta/stable
gh-aDotInTheVoid@dev-desktop-eu-1:~/tmp$ rustdoc +beta --test ./mcve.rs --edition=2015

running 1 test
test ./mcve.rs - (line 1) ... FAILED

failures:

---- ./mcve.rs - (line 1) stdout ----
Test executable failed (exit status: 101).

stderr:

thread 'main' panicked at ./mcve.rs:3:1:
assertion `left == right` failed
  left: 4
 right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace



failures:
    ./mcve.rs - (line 1)

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

gh-aDotInTheVoid@dev-desktop-eu-1:~/tmp$ rustdoc +beta --test ./mcve.rs --edition=2024

running 1 test
test ./mcve.rs - (line 1) ... FAILED

failures:

---- ./mcve.rs - (line 1) stdout ----
Test executable failed (exit status: 101).

stderr:

thread 'main' panicked at ./mcve.rs:3:1:
assertion `left == right` failed
  left: 4
 right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace



failures:
    ./mcve.rs - (line 1)

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

gh-aDotInTheVoid@dev-desktop-eu-1:~/tmp$ rustdoc +stable --test ./mcve.rs --edition=2015

running 1 test
test ./mcve.rs - (line 1) ... FAILED

failures:

---- ./mcve.rs - (line 1) stdout ----
Test executable failed (exit status: 101).

stderr:

thread 'main' panicked at ./mcve.rs:3:1:
assertion `left == right` failed
  left: 4
 right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace



failures:
    ./mcve.rs - (line 1)

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

gh-aDotInTheVoid@dev-desktop-eu-1:~/tmp$ rustdoc +stable --test ./mcve.rs --edition=2024

running 1 test
test ./mcve.rs - (line 1) ... FAILED

failures:

---- ./mcve.rs - (line 1) stdout ----
Test executable failed (exit status: 101).

stderr:

thread 'main' panicked at ./mcve.rs:3:1:
assertion `left == right` failed
  left: 4
 right: 5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace



failures:
    ./mcve.rs - (line 1)

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

Possibly releated: https://users.rust-lang.org/t/question-about-doctest-speedup-in-rust-2024-edition/126431 / #137898

CC @notriddle @GuillaumeGomez

@aDotInTheVoid aDotInTheVoid added the C-bug Category: This is a bug. label Mar 3, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 3, 2025
@aDotInTheVoid aDotInTheVoid added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. A-doctests Area: Documentation tests, run by rustdoc A-edition-2024 Area: The 2024 edition and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 3, 2025
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Mar 3, 2025
@fmease fmease added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-panic Area: Panicking machinery and removed regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Mar 3, 2025
@aDotInTheVoid aDotInTheVoid added the regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. label Mar 4, 2025
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Mar 4, 2025
@traviscross traviscross added the I-edition-triaged Issue: This issue has been reviewed and triaged by the Edition team. label Mar 4, 2025
@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Mar 5, 2025
@slluk
Copy link

slluk commented Apr 1, 2025

Hi, I'm experiencing the same issue since migrating to Rust edition 2024.

Notes:

Impact:

  • This makes the output of any non-trivial assertion failure in doc examples unreadable.
  • My current workaround is staying on edition 2021.
  • Projects with only a few doc examples can instead use the standalone_crate tag.

My setup:

$ rustc --version --verbose
rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-unknown-linux-gnu
release: 1.85.1
LLVM version: 19.1.7

$ cargo --version --verbose
cargo 1.85.1 (d73d2caf9 2024-12-31)
release: 1.85.1
commit-hash: d73d2caf9e41a39daf2a8d6ce60ec80bf354d2a7
commit-date: 2024-12-31
host: x86_64-unknown-linux-gnu
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.9.0-DEV (sys:0.4.74+curl-8.9.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Debian 11.0.0 (bullseye) [64-bit]

@fmease fmease added the P-high High priority label Apr 1, 2025
@fmease
Copy link
Member

fmease commented Apr 1, 2025

Marking this issue as a high priority item. CC @rust-lang/rustdoc

@GuillaumeGomez
Copy link
Member

Gonna take a look tomorrow.

@GuillaumeGomez
Copy link
Member

Finally figured out what went wrong. Cleaning up then sending the fix.

@GuillaumeGomez
Copy link
Member

Opened #139328.

@aDotInTheVoid
Copy link
Member Author

This now affects stable/beta.

alona@Ashtabula:~/tmp$ rustdoc +stable --test ./mcve.rs --edition=2024

running 1 test
test ./mcve.rs - (line 1) ... FAILED

failures:

---- ./mcve.rs - (line 1) stdout ----
Error: "\nthread 'main' panicked at /tmp/rustdoctestzLuU0f/doctest_bundle_2024.rs:6:1:\nassertion `left == right` failed\n  left: 4\n right: 5\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n"


failures:
    ./mcve.rs - (line 1)

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

alona@Ashtabula:~/tmp$ rustdoc +stable --version
rustdoc 1.86.0 (05f9846f8 2025-03-31)
alona@Ashtabula:~/tmp$ rustdoc +beta --test ./mcve.rs --edition=2024

running 1 test
test ./mcve.rs - (line 1) ... FAILED

failures:

---- ./mcve.rs - (line 1) stdout ----
Error: "\nthread 'main' panicked at /tmp/rustdoctestAEl3Lk/doctest_bundle_2024.rs:6:1:\nassertion `left == right` failed\n  left: 4\n right: 5\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n"


failures:
    ./mcve.rs - (line 1)

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

alona@Ashtabula:~/tmp$ rustdoc +beta --version
rustdoc 1.87.0-beta.1 (45165c82a 2025-04-01)

@aDotInTheVoid aDotInTheVoid added regression-from-stable-to-beta Performance or correctness regression from stable to beta. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Apr 3, 2025
Zalathar added a commit to Zalathar/rust that referenced this issue Apr 4, 2025
…7970, r=fmease

Fix 2024 edition doctest panic output

Fixes rust-lang#137970.

The problem was that the output was actually displayed by rustc itself because we're exiting with `Result<(), String>`, and the display is really not great. So instead, we get the output, we print it and then we return an `ExitCode`.

r? `@aDotInTheVoid`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 4, 2025
…7970, r=fmease

Fix 2024 edition doctest panic output

Fixes rust-lang#137970.

The problem was that the output was actually displayed by rustc itself because we're exiting with `Result<(), String>`, and the display is really not great. So instead, we get the output, we print it and then we return an `ExitCode`.

r? ``@aDotInTheVoid``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 4, 2025
…7970, r=fmease

Fix 2024 edition doctest panic output

Fixes rust-lang#137970.

The problem was that the output was actually displayed by rustc itself because we're exiting with `Result<(), String>`, and the display is really not great. So instead, we get the output, we print it and then we return an `ExitCode`.

r? ```@aDotInTheVoid```
@bors bors closed this as completed in cfc2297 Apr 5, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 5, 2025
Rollup merge of rust-lang#139328 - GuillaumeGomez:fix-panic-output-137970, r=fmease

Fix 2024 edition doctest panic output

Fixes rust-lang#137970.

The problem was that the output was actually displayed by rustc itself because we're exiting with `Result<(), String>`, and the display is really not great. So instead, we get the output, we print it and then we return an `ExitCode`.

r? ````@aDotInTheVoid````
cuviper pushed a commit to cuviper/rust that referenced this issue Apr 17, 2025
@slluk
Copy link

slluk commented Apr 18, 2025

Hi, I don't know if you're aware of this, but there's still an issue with the output.
While in nightly the panic output itself is now well formatted, any preceding data sent to stdout is still discarded.

Let's see an example:

  //! ```
  //! println!("######## from a DOC TEST ########");
  //! assert_eq!("doc", "test");
  //! ```
  
  #[cfg(test)]
  mod tests {
  
      #[test]
      fn some_test() {
          println!("######## from a REGULAR TEST ########");
          assert_eq!("regular", "test");
      }
  }

Let's run the regular test:

  $ cargo +nightly test --quiet
  
  running 1 test
  tests::some_test --- FAILED
  
  failures:
  
  ---- tests::some_test stdout ----
  ######## from a REGULAR TEST ########
  
  thread 'tests::some_test' panicked at src/lib.rs:12:9:
  assertion `left == right` failed
    left: "regular"
   right: "test"
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  
  
  failures:
      tests::some_test
  
  test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
  
  error: test failed, to rerun pass `--lib`

As we can see, the test's stdout was correctly printed as always.
But for doctests, this is still broken:

  $ cargo +nightly test --quiet --doc
  
  running 1 test
  src/lib.rs - (line 1) --- FAILED
  
  failures:
  
  ---- src/lib.rs - (line 1) stdout ----
  
  thread 'main' panicked at /tmp/rustdoctest5R7LFh/doctest_bundle_2024.rs:7:1:
  assertion `left == right` failed
    left: "doc"
   right: "test"
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  
  
  failures:
      src/lib.rs - (line 1)
  
  test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
  
  error: doctest failed, to rerun pass `--doc`

This breaks our custom assert macros, which output a diff on failure and still print the data on success for clarity and context in case of consecutive assertions.

@GuillaumeGomez
Copy link
Member

Indeed, stdout should have been kept. Opening an issue and sending a fix.

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 A-edition-2024 Area: The 2024 edition A-panic Area: Panicking machinery C-bug Category: This is a bug. I-edition-triaged Issue: This issue has been reviewed and triaged by the Edition team. P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
Development

Successfully merging a pull request may close this issue.

7 participants