Skip to content

Commit 07b6c1b

Browse files
committed
fix(doc): respect public-dependency when documenting deps
1 parent 9de8b94 commit 07b6c1b

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

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: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,7 +4097,6 @@ fn doc_with_public_dependency_transitive() {
40974097
)
40984098
.run();
40994099

4100-
// All four are documented: the whole chain is public.
41014100
assert!(p.root().join("target/doc/foo/index.html").is_file());
41024101
assert!(p.root().join("target/doc/bar/index.html").is_file());
41034102
assert!(p.root().join("target/doc/baz/index.html").is_file());
@@ -4168,7 +4167,7 @@ fn doc_direct_deps_always_documented() {
41684167
#[cargo_test(nightly, reason = "public-dependency feature is unstable")]
41694168
fn doc_with_private_dependency() {
41704169
// foo -> bar (any) -> baz (private)
4171-
// foo, bar, and baz are all documented
4170+
// Only foo and bar documented, not baz (transitive private dep)
41724171

41734172
Package::new("baz", "0.0.1")
41744173
.file("src/lib.rs", "pub fn baz() {}")
@@ -4185,7 +4184,7 @@ fn doc_with_private_dependency() {
41854184
"Cargo.toml",
41864185
r#"
41874186
cargo-features = ["public-dependency"]
4188-
4187+
41894188
[package]
41904189
name = "foo"
41914190
version = "0.0.1"
@@ -4207,7 +4206,6 @@ fn doc_with_private_dependency() {
42074206
[DOWNLOADING] crates ...
42084207
[DOWNLOADED] baz v0.0.1 (registry `dummy-registry`)
42094208
[DOWNLOADED] bar v0.0.1 (registry `dummy-registry`)
4210-
[DOCUMENTING] baz v0.0.1
42114209
[CHECKING] baz v0.0.1
42124210
[DOCUMENTING] bar v0.0.1
42134211
[CHECKING] bar v0.0.1
@@ -4222,8 +4220,7 @@ fn doc_with_private_dependency() {
42224220

42234221
assert!(p.root().join("target/doc/foo/index.html").is_file());
42244222
assert!(p.root().join("target/doc/bar/index.html").is_file());
4225-
// baz is documented because cargo docs all transitive deps
4226-
assert!(p.root().join("target/doc/baz/index.html").is_file());
4223+
assert!(!p.root().join("target/doc/baz/index.html").is_file());
42274224
}
42284225

42294226
#[cargo_test(nightly, reason = "public-dependency feature is unstable")]
@@ -4252,7 +4249,7 @@ fn doc_mixed_public_private_deps() {
42524249
"Cargo.toml",
42534250
r#"
42544251
cargo-features = ["public-dependency"]
4255-
4252+
42564253
[package]
42574254
name = "foo"
42584255
version = "0.0.1"
@@ -4281,6 +4278,6 @@ fn doc_mixed_public_private_deps() {
42814278
.is_file()
42824279
);
42834280

4284-
// transitive is also documented (all transitive deps are documented)
4285-
assert!(p.root().join("target/doc/transitive/index.html").is_file());
4281+
// transitive should NOT be documented
4282+
assert!(!p.root().join("target/doc/transitive/index.html").is_file());
42864283
}

0 commit comments

Comments
 (0)