@@ -67,6 +67,12 @@ struct State<'a, 'gctx> {
6767 /// dependency from a to b was added purely because it was a dev-dependency.
6868 /// This is used during `connect_run_custom_build_deps`.
6969 dev_dependency_edges : HashSet < ( Unit , Unit ) > ,
70+
71+ /// The set of packages explicitly selected by the user (e.g. via `-p`).
72+ /// Used to determine whether to apply the public-dependency doc filter:
73+ /// only root packages bypass the filter; their deps must be public to
74+ /// have their own docs generated.
75+ root_pkg_ids : HashSet < PackageId > ,
7076}
7177
7278/// A boolean-like to indicate if a `Unit` is an artifact or not.
@@ -126,6 +132,7 @@ pub fn build_unit_dependencies<'a, 'gctx>(
126132 interner,
127133 scrape_units,
128134 dev_dependency_edges : HashSet :: new ( ) ,
135+ root_pkg_ids : roots. iter ( ) . map ( |u| u. pkg . package_id ( ) ) . collect ( ) ,
129136 } ;
130137
131138 let std_unit_deps = calc_deps_of_std ( & mut state, std_roots) ?;
@@ -637,6 +644,21 @@ fn compute_deps_doc(
637644 // built. If we're documenting *all* libraries, then we also depend on
638645 // the documentation of the library being built.
639646 let mut ret = Vec :: new ( ) ;
647+
648+ // Check if public-dependency feature is enabled
649+ let public_deps_enabled = state. gctx . cli_unstable ( ) . public_dependency
650+ || unit
651+ . pkg
652+ . manifest ( )
653+ . unstable_features ( )
654+ . is_enabled ( Feature :: public_dependency ( ) ) ;
655+
656+ // Whether this package was explicitly selected by the user (e.g. via `-p`).
657+ // User-selected packages always have all their direct deps documented,
658+ // regardless of public/private status. For everything else, only public
659+ // deps get their own docs generated when public-dependency is enabled.
660+ let is_user_selected = state. root_pkg_ids . contains ( & unit. pkg . package_id ( ) ) ;
661+
640662 for ( id, deps) in state. deps ( unit, unit_for) {
641663 let Some ( dep_lib) = calc_artifact_deps ( unit, unit_for, id, & deps, state, & mut ret) ? else {
642664 continue ;
@@ -657,7 +679,20 @@ fn compute_deps_doc(
657679 IS_NO_ARTIFACT_DEP ,
658680 ) ?;
659681 ret. push ( lib_unit_dep) ;
660- if dep_lib. documented ( ) && state. intent . wants_deps_docs ( ) {
682+
683+ // Decide whether to document this dependency.
684+ // When public-dependency is enabled, only document:
685+ // - Direct dependencies of user-selected packages
686+ // - Public dependencies (recursively)
687+ // This dramatically speeds up documentation builds by excluding indirect
688+ // private dependencies that cannot be used by readers of the docs.
689+ let should_doc_dep = if is_user_selected || !public_deps_enabled {
690+ true
691+ } else {
692+ state. resolve ( ) . is_public_dep ( unit. pkg . package_id ( ) , id)
693+ } ;
694+
695+ if dep_lib. documented ( ) && state. intent . wants_deps_docs ( ) && should_doc_dep {
661696 // Document this lib as well.
662697 let doc_unit_dep = new_unit_dep (
663698 state,
0 commit comments