diff --git a/src/cargo/core/compiler/context/unit_dependencies.rs b/src/cargo/core/compiler/context/unit_dependencies.rs index 6a8549464c7..db0744476c7 100644 --- a/src/cargo/core/compiler/context/unit_dependencies.rs +++ b/src/cargo/core/compiler/context/unit_dependencies.rs @@ -339,8 +339,8 @@ fn compute_deps_doc<'a, 'cfg, 'tmp>( // Be sure to build/run the build script for documented libraries. ret.extend(dep_build_script(unit, bcx)); - // If we document a binary, we need the library available. - if unit.target.is_bin() { + // If we document a binary/example, we need the library available. + if unit.target.is_bin() || unit.target.is_example() { ret.extend(maybe_lib(unit, bcx, UnitFor::new_normal())); } Ok(ret) diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 23943ea7b0b..40f9423913a 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -1364,3 +1364,41 @@ fn short_message_format() { ) .run(); } + +#[cargo_test] +fn doc_example() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2018" + + [[example]] + crate-type = ["lib"] + name = "ex1" + doc = true + "#, + ) + .file("src/lib.rs", "pub fn f() {}") + .file( + "examples/ex1.rs", + r#" + use foo::f; + + /// Example + pub fn x() { f(); } + "#, + ) + .build(); + + p.cargo("doc").run(); + assert!(p + .build_dir() + .join("doc") + .join("ex1") + .join("fn.x.html") + .exists()); +}