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.
This commit is contained in:
Will Crichton 2021-10-28 00:35:34 -07:00
parent 0deeea8312
commit 11209570c9
4 changed files with 86 additions and 30 deletions

View file

@ -188,11 +188,7 @@ impl<'cfg> Compilation<'cfg> {
unit: &Unit,
script_meta: Option<Metadata>,
) -> CargoResult<ProcessBuilder> {
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);

View file

@ -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<Work> {
.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::<HashSet<_>>();
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");

View file

@ -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)

View file

@ -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();
}