Skip to content
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

Don't document libs with doc=false #10201

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}