Skip to content

Commit 6e56869

Browse files
committed
Don't document libs with doc=false
1 parent da8dd70 commit 6e56869

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

src/cargo/core/compiler/unit_dependencies.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ fn compute_deps(
326326
if unit.target.is_lib() && unit.mode != CompileMode::Doctest {
327327
return Ok(ret);
328328
}
329-
ret.extend(maybe_lib(unit, state, unit_for, None)?);
329+
ret.extend(maybe_lib(unit, state, unit_for)?);
330330

331331
// If any integration tests/benches are being run, make sure that
332332
// binaries are built as well.
@@ -431,7 +431,7 @@ fn compute_deps_doc(
431431
let mut ret = Vec::new();
432432
for (id, _deps) in deps {
433433
let dep = state.get(id);
434-
let lib = match dep.targets().iter().find(|t| t.is_lib()) {
434+
let lib = match dep.targets().iter().find(|t| t.is_lib() && t.documented()) {
435435
Some(lib) => lib,
436436
None => continue,
437437
};
@@ -470,9 +470,26 @@ fn compute_deps_doc(
470470
// If we document a binary/example, we need the library available.
471471
if unit.target.is_bin() || unit.target.is_example() {
472472
// build the lib
473-
ret.extend(maybe_lib(unit, state, unit_for, None)?);
473+
ret.extend(maybe_lib(unit, state, unit_for)?);
474474
// and also the lib docs for intra-doc links
475-
ret.extend(maybe_lib(unit, state, unit_for, Some(unit.mode))?);
475+
if let Some(lib) = unit
476+
.pkg
477+
.targets()
478+
.iter()
479+
.find(|t| t.is_linkable() && t.documented())
480+
{
481+
let dep_unit_for = unit_for.with_dependency(unit, lib);
482+
let lib_doc_unit = new_unit_dep(
483+
state,
484+
unit,
485+
&unit.pkg,
486+
lib,
487+
dep_unit_for,
488+
unit.kind.for_target(lib),
489+
unit.mode,
490+
)?;
491+
ret.push(lib_doc_unit);
492+
}
476493
}
477494

478495
// Add all units being scraped for examples as a dependency of Doc units.
@@ -500,14 +517,13 @@ fn maybe_lib(
500517
unit: &Unit,
501518
state: &mut State<'_, '_>,
502519
unit_for: UnitFor,
503-
force_mode: Option<CompileMode>,
504520
) -> CargoResult<Option<UnitDep>> {
505521
unit.pkg
506522
.targets()
507523
.iter()
508524
.find(|t| t.is_linkable())
509525
.map(|t| {
510-
let mode = force_mode.unwrap_or_else(|| check_or_build_mode(unit.mode, t));
526+
let mode = check_or_build_mode(unit.mode, t);
511527
let dep_unit_for = unit_for.with_dependency(unit, t);
512528
new_unit_dep(
513529
state,

tests/testsuite/doc.rs

+50
Original file line numberDiff line numberDiff line change
@@ -2513,3 +2513,53 @@ fn lib_before_bin() {
25132513
let bin_html = p.read_file("target/doc/somebin/index.html");
25142514
assert!(bin_html.contains("../foo/fn.abc.html"));
25152515
}
2516+
2517+
#[cargo_test]
2518+
fn doc_lib_false() {
2519+
// doc = false for a library
2520+
let p = project()
2521+
.file(
2522+
"Cargo.toml",
2523+
r#"
2524+
[package]
2525+
name = "foo"
2526+
version = "0.1.0"
2527+
2528+
[lib]
2529+
doc = false
2530+
2531+
[dependencies]
2532+
bar = {path = "bar"}
2533+
"#,
2534+
)
2535+
.file("src/lib.rs", "")
2536+
.file("src/bin/some-bin.rs", "fn main() {}")
2537+
.file(
2538+
"bar/Cargo.toml",
2539+
r#"
2540+
[package]
2541+
name = "bar"
2542+
version = "0.1.0"
2543+
2544+
[lib]
2545+
doc = false
2546+
"#,
2547+
)
2548+
.file("bar/src/lib.rs", "")
2549+
.build();
2550+
2551+
p.cargo("doc")
2552+
.with_stderr(
2553+
"\
2554+
[CHECKING] bar v0.1.0 [..]
2555+
[CHECKING] foo v0.1.0 [..]
2556+
[DOCUMENTING] foo v0.1.0 [..]
2557+
[FINISHED] [..]
2558+
",
2559+
)
2560+
.run();
2561+
2562+
assert!(!p.build_dir().join("doc/foo").exists());
2563+
assert!(!p.build_dir().join("doc/bar").exists());
2564+
assert!(p.build_dir().join("doc/some_bin").exists());
2565+
}

0 commit comments

Comments
 (0)