Skip to content

Commit 1120957

Browse files
committed
Change scraping to apply to all workspace packages instead of just
root units. Only attach Docscrape unit dependencies to workspace Doc units. Add test for scraping examples with complex reverse dependencies.
1 parent 0deeea8 commit 1120957

File tree

4 files changed

+86
-30
lines changed

4 files changed

+86
-30
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,7 @@ impl<'cfg> Compilation<'cfg> {
188188
unit: &Unit,
189189
script_meta: Option<Metadata>,
190190
) -> CargoResult<ProcessBuilder> {
191-
let mut rustdoc = ProcessBuilder::new(&*self.config.rustdoc()?);
192-
if self.config.extra_verbose() {
193-
rustdoc.display_env_vars();
194-
}
195-
191+
let rustdoc = ProcessBuilder::new(&*self.config.rustdoc()?);
196192
let cmd = fill_rustc_tool_env(rustdoc, unit);
197193
let mut p = self.fill_env(cmd, &unit.pkg, script_meta, unit.kind, true)?;
198194
unit.target.edition().cmd_edition_arg(&mut p);

src/cargo/core/compiler/mod.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ mod unit;
2121
pub mod unit_dependencies;
2222
pub mod unit_graph;
2323

24-
use std::collections::HashSet;
2524
use std::env;
2625
use std::ffi::{OsStr, OsString};
2726
use std::fs::{self, File};
@@ -665,18 +664,14 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
665664
.arg("--scrape-examples-output-path")
666665
.arg(scrape_output_path(unit)?);
667666

668-
// Limit the scraped examples to just crates in the root set
669-
let root_packages = cx
670-
.bcx
671-
.roots
672-
.iter()
673-
.map(|root| root.pkg.name())
674-
.collect::<HashSet<_>>();
675-
for pkg in root_packages {
676-
rustdoc.arg("--scrape-examples-target-crate").arg(pkg);
667+
// Only scrape example for items from crates in the workspace, to reduce generated file size
668+
for pkg in cx.bcx.ws.members() {
669+
rustdoc
670+
.arg("--scrape-examples-target-crate")
671+
.arg(pkg.name());
677672
}
678-
} else if cx.bcx.scrape_units.len() > 0 && cx.bcx.roots.contains(unit) {
679-
// We only pass scraped examples to packages in the workspace (bcx.roots)
673+
} else if cx.bcx.scrape_units.len() > 0 && cx.bcx.ws.is_member(&unit.pkg) {
674+
// We only pass scraped examples to packages in the workspace
680675
// since examples are only coming from reverse-dependencies of workspace packages
681676

682677
rustdoc.arg("-Zunstable-options");

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,21 @@ fn compute_deps_doc(
473473
}
474474

475475
// Add all units being scraped for examples as a dependency of Doc units.
476-
for scrape_unit in state.scrape_units.iter() {
477-
// This needs to match the FeaturesFor used in cargo_compile::generate_targets.
478-
let unit_for = UnitFor::new_host(scrape_unit.target.proc_macro());
479-
deps_of(scrape_unit, state, unit_for)?;
480-
ret.push(new_unit_dep(
481-
state,
482-
scrape_unit,
483-
&scrape_unit.pkg,
484-
&scrape_unit.target,
485-
unit_for,
486-
scrape_unit.kind,
487-
scrape_unit.mode,
488-
)?);
476+
if state.ws.is_member(&unit.pkg) {
477+
for scrape_unit in state.scrape_units.iter() {
478+
// This needs to match the FeaturesFor used in cargo_compile::generate_targets.
479+
let unit_for = UnitFor::new_host(scrape_unit.target.proc_macro());
480+
deps_of(scrape_unit, state, unit_for)?;
481+
ret.push(new_unit_dep(
482+
state,
483+
scrape_unit,
484+
&scrape_unit.pkg,
485+
&scrape_unit.target,
486+
unit_for,
487+
scrape_unit.kind,
488+
scrape_unit.mode,
489+
)?);
490+
}
489491
}
490492

491493
Ok(ret)

tests/testsuite/doc.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,3 +2235,66 @@ fn scrape_examples_avoid_build_script_cycle() {
22352235
.masquerade_as_nightly_cargo()
22362236
.run();
22372237
}
2238+
2239+
2240+
#[cargo_test]
2241+
fn scrape_examples_complex_reverse_dependencies() {
2242+
if !is_nightly() {
2243+
// --scrape-examples is unstable
2244+
return;
2245+
}
2246+
2247+
let p = project()
2248+
.file(
2249+
"Cargo.toml",
2250+
r#"
2251+
[package]
2252+
name = "foo"
2253+
version = "0.0.1"
2254+
authors = []
2255+
2256+
[dev-dependencies]
2257+
a = {path = "a", features = ["feature"]}
2258+
b = {path = "b"}
2259+
2260+
[workspace]
2261+
members = ["b"]
2262+
"#,
2263+
)
2264+
.file("src/lib.rs", "")
2265+
.file("examples/ex.rs", "fn main() { a::f(); }")
2266+
.file(
2267+
"a/Cargo.toml",
2268+
r#"
2269+
[package]
2270+
name = "a"
2271+
version = "0.0.1"
2272+
authors = []
2273+
2274+
[lib]
2275+
proc-macro = true
2276+
2277+
[dependencies]
2278+
b = {path = "../b"}
2279+
2280+
[features]
2281+
feature = []
2282+
"#,
2283+
)
2284+
.file("a/src/lib.rs", "#[cfg(feature)] pub fn f();")
2285+
.file(
2286+
"b/Cargo.toml",
2287+
r#"
2288+
[package]
2289+
name = "b"
2290+
version = "0.0.1"
2291+
authors = []
2292+
"#,
2293+
)
2294+
.file("b/src/lib.rs", "")
2295+
.build();
2296+
2297+
p.cargo("doc -Zunstable-options --scrape-examples all")
2298+
.masquerade_as_nightly_cargo()
2299+
.run();
2300+
}

0 commit comments

Comments
 (0)