diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index ac243ac85..3b21e4f43 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -188,11 +188,7 @@ impl<'cfg> Compilation<'cfg> { unit: &Unit, script_meta: Option, ) -> CargoResult { - let mut rustdoc = ProcessBuilder::new(&*self.config.rustdoc()?); - if self.config.extra_verbose() { - rustdoc.display_env_vars(); - } - + let rustdoc = ProcessBuilder::new(&*self.config.rustdoc()?); let cmd = fill_rustc_tool_env(rustdoc, unit); let mut p = self.fill_env(cmd, &unit.pkg, script_meta, unit.kind, true)?; unit.target.edition().cmd_edition_arg(&mut p); diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 2a2f3e74e..2f58b60ab 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -21,7 +21,6 @@ mod unit; pub mod unit_dependencies; pub mod unit_graph; -use std::collections::HashSet; use std::env; use std::ffi::{OsStr, OsString}; use std::fs::{self, File}; @@ -665,18 +664,14 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { .arg("--scrape-examples-output-path") .arg(scrape_output_path(unit)?); - // Limit the scraped examples to just crates in the root set - let root_packages = cx - .bcx - .roots - .iter() - .map(|root| root.pkg.name()) - .collect::>(); - for pkg in root_packages { - rustdoc.arg("--scrape-examples-target-crate").arg(pkg); + // Only scrape example for items from crates in the workspace, to reduce generated file size + for pkg in cx.bcx.ws.members() { + rustdoc + .arg("--scrape-examples-target-crate") + .arg(pkg.name()); } - } else if cx.bcx.scrape_units.len() > 0 && cx.bcx.roots.contains(unit) { - // We only pass scraped examples to packages in the workspace (bcx.roots) + } else if cx.bcx.scrape_units.len() > 0 && cx.bcx.ws.is_member(&unit.pkg) { + // We only pass scraped examples to packages in the workspace // since examples are only coming from reverse-dependencies of workspace packages rustdoc.arg("-Zunstable-options"); diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index db73143fe..ae8ca69aa 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -473,19 +473,21 @@ fn compute_deps_doc( } // Add all units being scraped for examples as a dependency of Doc units. - for scrape_unit in state.scrape_units.iter() { - // This needs to match the FeaturesFor used in cargo_compile::generate_targets. - let unit_for = UnitFor::new_host(scrape_unit.target.proc_macro()); - deps_of(scrape_unit, state, unit_for)?; - ret.push(new_unit_dep( - state, - scrape_unit, - &scrape_unit.pkg, - &scrape_unit.target, - unit_for, - scrape_unit.kind, - scrape_unit.mode, - )?); + if state.ws.is_member(&unit.pkg) { + for scrape_unit in state.scrape_units.iter() { + // This needs to match the FeaturesFor used in cargo_compile::generate_targets. + let unit_for = UnitFor::new_host(scrape_unit.target.proc_macro()); + deps_of(scrape_unit, state, unit_for)?; + ret.push(new_unit_dep( + state, + scrape_unit, + &scrape_unit.pkg, + &scrape_unit.target, + unit_for, + scrape_unit.kind, + scrape_unit.mode, + )?); + } } Ok(ret) diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 80f500464..9f278e318 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -2235,3 +2235,66 @@ fn scrape_examples_avoid_build_script_cycle() { .masquerade_as_nightly_cargo() .run(); } + + +#[cargo_test] +fn scrape_examples_complex_reverse_dependencies() { + if !is_nightly() { + // --scrape-examples is unstable + return; + } + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + + [dev-dependencies] + a = {path = "a", features = ["feature"]} + b = {path = "b"} + + [workspace] + members = ["b"] + "#, + ) + .file("src/lib.rs", "") + .file("examples/ex.rs", "fn main() { a::f(); }") + .file( + "a/Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + authors = [] + + [lib] + proc-macro = true + + [dependencies] + b = {path = "../b"} + + [features] + feature = [] + "#, + ) + .file("a/src/lib.rs", "#[cfg(feature)] pub fn f();") + .file( + "b/Cargo.toml", + r#" + [package] + name = "b" + version = "0.0.1" + authors = [] + "#, + ) + .file("b/src/lib.rs", "") + .build(); + + p.cargo("doc -Zunstable-options --scrape-examples all") + .masquerade_as_nightly_cargo() + .run(); +}