rust-lang / rust-lang/cargo

Doctests don't work in bin targets

Open
#5,477 15 comments 36 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-doctests Command-test S-blocked-external
Dominant language
Rust
Stars
15.5k
Forks
3k
Avg merge
23h 30m
Merged PRs (30d)
51

Description

It's surprising that doctests don't work binary targets:

cargo new foo --bin
/// ```rust
/// assert!(false);
/// ```
fn main() {
    println!("Hello, world!");
}
cargo test --all

I'd expect the above to fail, but it looks like the doc-comment code is never ran. Even if I explicitly enable doctests for the binary:

[[bin]]
name = "foo"
path = "src/main.rs"
doctest = true

they still aren't run.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/main.rs and the binary configuration in Cargo.toml, then run cargo test --all with and without doctest = true. Trace how Cargo handles doctests for bin targets; done means the documented assertion is executed and causes the expected test failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
build-system, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.