Skip to content

Commit

Permalink
Don't document libs with doc=false
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Dec 15, 2021
1 parent da8dd70 commit 6e56869
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -500,14 +517,13 @@ fn maybe_lib(
unit: &Unit,
state: &mut State<'_, '_>,
unit_for: UnitFor,
force_mode: Option<CompileMode>,
) -> CargoResult<Option<UnitDep>> {
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,
Expand Down
50 changes: 50 additions & 0 deletions tests/testsuite/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit 6e56869

Please sign in to comment.