Skip to content

Commit fe28062

Browse files
committed
fix(doc): respect public-dependency when documenting deps
1 parent 5e0c96e commit fe28062

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,16 @@ fn compute_deps_doc(
637637
// built. If we're documenting *all* libraries, then we also depend on
638638
// the documentation of the library being built.
639639
let mut ret = Vec::new();
640+
let is_member = state.ws.is_member(&unit.pkg);
641+
642+
// Check if public-dependency feature is enabled
643+
let public_deps_enabled = state.gctx.cli_unstable().public_dependency
644+
|| unit
645+
.pkg
646+
.manifest()
647+
.unstable_features()
648+
.is_enabled(Feature::public_dependency());
649+
640650
for (id, deps) in state.deps(unit, unit_for) {
641651
let Some(dep_lib) = calc_artifact_deps(unit, unit_for, id, &deps, state, &mut ret)? else {
642652
continue;
@@ -657,7 +667,20 @@ fn compute_deps_doc(
657667
IS_NO_ARTIFACT_DEP,
658668
)?;
659669
ret.push(lib_unit_dep);
660-
if dep_lib.documented() && state.intent.wants_deps_docs() {
670+
671+
// Decide whether to document this dependency.
672+
// When public-dependency is enabled, only document:
673+
// - Direct dependencies of workspace members
674+
// - Public dependencies (recursively)
675+
// This dramatically speeds up documentation builds by excluding indirect
676+
// private dependencies that cannot be used by readers of the docs.
677+
let should_doc_dep = if is_member || !public_deps_enabled {
678+
true
679+
} else {
680+
state.resolve().is_public_dep(unit.pkg.package_id(), id)
681+
};
682+
683+
if dep_lib.documented() && state.intent.wants_deps_docs() && should_doc_dep {
661684
// Document this lib as well.
662685
let doc_unit_dep = new_unit_dep(
663686
state,

tests/testsuite/doc.rs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4205,7 +4205,6 @@ fn doc_with_private_dependency() {
42054205
[DOWNLOADING] crates ...
42064206
[DOWNLOADED] baz v0.0.1 (registry `dummy-registry`)
42074207
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
4208-
[DOCUMENTING] baz v0.0.1
42094208
[CHECKING] baz v0.0.1
42104209
[DOCUMENTING] bar v0.0.1
42114210
[CHECKING] bar v0.0.1
@@ -4220,7 +4219,7 @@ fn doc_with_private_dependency() {
42204219

42214220
assert!(p.root().join("target/doc/foo/index.html").is_file());
42224221
assert!(p.root().join("target/doc/bar/index.html").is_file());
4223-
assert!(p.root().join("target/doc/baz/index.html").is_file());
4222+
assert!(!p.root().join("target/doc/baz/index.html").is_file());
42244223
}
42254224

42264225
#[cargo_test(nightly, reason = "public-dependency feature is unstable")]
@@ -4277,5 +4276,78 @@ fn doc_mixed_public_private_deps() {
42774276
.join("target/doc/priv_dep_with_dep/index.html")
42784277
.is_file()
42794278
);
4280-
assert!(p.root().join("target/doc/transitive/index.html").is_file());
4279+
assert!(!p.root().join("target/doc/transitive/index.html").is_file());
4280+
}
4281+
4282+
#[cargo_test(nightly, reason = "public-dependency feature is unstable")]
4283+
fn doc_workspace_member_private_dep() {
4284+
// selected -> skipped (private dep), skipped -> transitive (registry dep)
4285+
// Both selected and skipped are workspace members.
4286+
// Running `cargo doc -p selected`:
4287+
// - skipped is documented because it is a workspace member (is_member = true),
4288+
// even though selected declares it as a private dep
4289+
// - transitive is documented because skipped is a workspace member, so
4290+
// is_member is true when computing skipped's deps
4291+
4292+
Package::new("transitive", "0.0.1")
4293+
.file("src/lib.rs", "pub fn transitive() {}")
4294+
.publish();
4295+
4296+
let p = project()
4297+
.file(
4298+
"Cargo.toml",
4299+
r#"
4300+
[workspace]
4301+
members = ["selected", "skipped"]
4302+
"#,
4303+
)
4304+
.file(
4305+
"selected/Cargo.toml",
4306+
r#"
4307+
cargo-features = ["public-dependency"]
4308+
4309+
[package]
4310+
name = "selected"
4311+
version = "0.0.1"
4312+
edition = "2021"
4313+
4314+
[dependencies]
4315+
skipped = { path = "../skipped", public = false }
4316+
"#,
4317+
)
4318+
.file("selected/src/lib.rs", "pub fn selected() {}")
4319+
.file(
4320+
"skipped/Cargo.toml",
4321+
r#"
4322+
[package]
4323+
name = "skipped"
4324+
version = "0.0.1"
4325+
edition = "2021"
4326+
4327+
[dependencies]
4328+
transitive = "0.0.1"
4329+
"#,
4330+
)
4331+
.file("skipped/src/lib.rs", "pub fn skipped() {}")
4332+
.build();
4333+
4334+
p.cargo("doc -p selected -Zpublic-dependency")
4335+
.masquerade_as_nightly_cargo(&["public-dependency"])
4336+
.run();
4337+
4338+
// selected is documented
4339+
assert!(p
4340+
.root()
4341+
.join("target/doc/selected/index.html")
4342+
.is_file());
4343+
// skipped is a workspace member so it is documented even though selected marks it private
4344+
assert!(p
4345+
.root()
4346+
.join("target/doc/skipped/index.html")
4347+
.is_file());
4348+
// transitive is documented because skipped is a workspace member
4349+
assert!(p
4350+
.root()
4351+
.join("target/doc/transitive/index.html")
4352+
.is_file());
42814353
}

0 commit comments

Comments
 (0)