diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index 5d8306fca4d..a3bcecc1782 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -326,7 +326,7 @@ fn compute_deps( if unit.target.is_lib() && unit.mode != CompileMode::Doctest { return Ok(ret); } - ret.extend(maybe_lib(unit, state, unit_for, None)?); + ret.extend(maybe_lib(unit, state, unit_for)?); // If any integration tests/benches are being run, make sure that // binaries are built as well. @@ -431,7 +431,7 @@ fn compute_deps_doc( let mut ret = Vec::new(); for (id, _deps) in deps { let dep = state.get(id); - let lib = match dep.targets().iter().find(|t| t.is_lib()) { + let lib = match dep.targets().iter().find(|t| t.is_lib() && t.documented()) { Some(lib) => lib, None => continue, }; @@ -470,9 +470,26 @@ fn compute_deps_doc( // If we document a binary/example, we need the library available. if unit.target.is_bin() || unit.target.is_example() { // build the lib - ret.extend(maybe_lib(unit, state, unit_for, None)?); + ret.extend(maybe_lib(unit, state, unit_for)?); // and also the lib docs for intra-doc links - ret.extend(maybe_lib(unit, state, unit_for, Some(unit.mode))?); + if let Some(lib) = unit + .pkg + .targets() + .iter() + .find(|t| t.is_linkable() && t.documented()) + { + let dep_unit_for = unit_for.with_dependency(unit, lib); + let lib_doc_unit = new_unit_dep( + state, + unit, + &unit.pkg, + lib, + dep_unit_for, + unit.kind.for_target(lib), + unit.mode, + )?; + ret.push(lib_doc_unit); + } } // Add all units being scraped for examples as a dependency of Doc units. @@ -500,14 +517,13 @@ fn maybe_lib( unit: &Unit, state: &mut State<'_, '_>, unit_for: UnitFor, - force_mode: Option, ) -> CargoResult> { unit.pkg .targets() .iter() .find(|t| t.is_linkable()) .map(|t| { - let mode = force_mode.unwrap_or_else(|| check_or_build_mode(unit.mode, t)); + let mode = check_or_build_mode(unit.mode, t); let dep_unit_for = unit_for.with_dependency(unit, t); new_unit_dep( state, diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 99c8490fa44..b1a92a20ac8 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -2513,3 +2513,53 @@ fn lib_before_bin() { let bin_html = p.read_file("target/doc/somebin/index.html"); assert!(bin_html.contains("../foo/fn.abc.html")); } + +#[cargo_test] +fn doc_lib_false() { + // doc = false for a library + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [lib] + doc = false + + [dependencies] + bar = {path = "bar"} + "#, + ) + .file("src/lib.rs", "") + .file("src/bin/some-bin.rs", "fn main() {}") + .file( + "bar/Cargo.toml", + r#" + [package] + name = "bar" + version = "0.1.0" + + [lib] + doc = false + "#, + ) + .file("bar/src/lib.rs", "") + .build(); + + p.cargo("doc") + .with_stderr( + "\ +[CHECKING] bar v0.1.0 [..] +[CHECKING] foo v0.1.0 [..] +[DOCUMENTING] foo v0.1.0 [..] +[FINISHED] [..] +", + ) + .run(); + + assert!(!p.build_dir().join("doc/foo").exists()); + assert!(!p.build_dir().join("doc/bar").exists()); + assert!(p.build_dir().join("doc/some_bin").exists()); +}