Skip to content

Commit 12fa44b

Browse files
author
Ojus Chugh
committed
Respect public-dependency when documenting deps
Fixes #2025 When the public-dependency feature is enabled, cargo doc now only documents direct dependencies and their public dependencies recursively. This reduces documentation clutter by excluding transitive private dependencies that users don't need to know about. Behavior: - Without public-dependency: all deps documented (backward compatible) - With public-dependency enabled: * Direct dependencies always documented * Transitive deps only if marked public This allows library authors to control their documentation surface by marking which dependencies are part of their public API.
1 parent 172fb93 commit 12fa44b

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,15 @@ 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_root = state.ws.is_member(&unit.pkg);
641+
642+
// Check if public-dependency feature is enabled
643+
let public_dep = state.gctx.cli_unstable().public_dependency
644+
|| unit.pkg.manifest()
645+
.unstable_features()
646+
.require(Feature::public_dependency())
647+
.is_ok();
648+
640649
for (id, deps) in state.deps(unit, unit_for) {
641650
let Some(dep_lib) = calc_artifact_deps(unit, unit_for, id, &deps, state, &mut ret)? else {
642651
continue;
@@ -657,7 +666,20 @@ fn compute_deps_doc(
657666
IS_NO_ARTIFACT_DEP,
658667
)?;
659668
ret.push(lib_unit_dep);
660-
if dep_lib.documented() && state.intent.wants_deps_docs() {
669+
670+
// Decide whether to document this dependency.
671+
// When public-dependency is enabled, only document:
672+
// - Direct dependencies of workspace members
673+
// - Public dependencies (recursively)
674+
let should_doc = dep_lib.documented() && state.intent.wants_deps_docs() && {
675+
if public_dep {
676+
is_root || state.resolve().is_public_dep(unit.pkg.package_id(), id)
677+
} else {
678+
true
679+
}
680+
};
681+
682+
if should_doc {
661683
// Document this lib as well.
662684
let doc_unit_dep = new_unit_dep(
663685
state,

0 commit comments

Comments
 (0)