Skip to content

Commit 642ca54

Browse files
committed
Ensure host units don't depend on Docscrape units, fixes #10535
1 parent b36cc6e commit 642ca54

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
682682
rustdoc.arg("--scrape-examples-target-crate").arg(name);
683683
}
684684
}
685-
} else if cx.bcx.scrape_units.len() > 0 && cx.bcx.ws.is_member(&unit.pkg) {
685+
} else if cx.bcx.scrape_units.len() > 0 && cx.bcx.ws.unit_needs_doc_scrape(unit) {
686686
// We only pass scraped examples to packages in the workspace
687687
// since examples are only coming from reverse-dependencies of workspace packages
688688

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,8 @@ fn compute_deps_doc(
708708
}
709709
}
710710

711-
// Add all units being scraped for examples as a dependency of Doc units.
712-
if state.ws.is_member(&unit.pkg) {
711+
// Add all units being scraped for examples as a dependency of top-level Doc units.
712+
if state.ws.unit_needs_doc_scrape(unit) {
713713
for scrape_unit in state.scrape_units.iter() {
714714
deps_of(scrape_unit, state, unit_for)?;
715715
ret.push(new_unit_dep(

src/cargo/core/workspace.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use log::debug;
1111
use toml_edit::easy as toml;
1212
use url::Url;
1313

14+
use crate::core::compiler::Unit;
1415
use crate::core::features::Features;
1516
use crate::core::registry::PackageRegistry;
1617
use crate::core::resolver::features::CliFeatures;
@@ -1500,6 +1501,15 @@ impl<'cfg> Workspace<'cfg> {
15001501

15011502
ms
15021503
}
1504+
1505+
/// Returns true if `unit` should depend on the output of Docscrape units.
1506+
pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
1507+
// We do not add scraped units for Host units, as they're either build scripts
1508+
// (not documented) or proc macros (have no scrape-able exports). Additionally,
1509+
// naively passing a proc macro's unit_for to new_unit_dep will currently cause
1510+
// Cargo to panic, see issue #10545.
1511+
self.is_member(&unit.pkg) && !unit.target.for_host()
1512+
}
15031513
}
15041514

15051515
impl<'cfg> Packages<'cfg> {

tests/testsuite/doc.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,58 @@ fn scrape_examples_configure_profile() {
25932593
assert!(doc_html.contains("More examples"));
25942594
}
25952595

2596+
#[cargo_test]
2597+
fn scrape_examples_issue_10545() {
2598+
if !is_nightly() {
2599+
// -Z rustdoc-scrape-examples is unstable
2600+
return;
2601+
}
2602+
2603+
let p = project()
2604+
.file(
2605+
"Cargo.toml",
2606+
r#"
2607+
[workspace]
2608+
resolver = "2"
2609+
members = ["a", "b"]
2610+
"#,
2611+
)
2612+
.file(
2613+
"a/Cargo.toml",
2614+
r#"
2615+
[package]
2616+
name = "a"
2617+
version = "0.0.1"
2618+
authors = []
2619+
edition = "2021"
2620+
2621+
[features]
2622+
default = ["foo"]
2623+
foo = []
2624+
"#,
2625+
)
2626+
.file("a/src/lib.rs", "")
2627+
.file(
2628+
"b/Cargo.toml",
2629+
r#"
2630+
[package]
2631+
name = "b"
2632+
version = "0.0.1"
2633+
authors = []
2634+
edition = "2021"
2635+
2636+
[lib]
2637+
proc-macro = true
2638+
"#,
2639+
)
2640+
.file("b/src/lib.rs", "")
2641+
.build();
2642+
2643+
p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples=all")
2644+
.masquerade_as_nightly_cargo()
2645+
.run();
2646+
}
2647+
25962648
#[cargo_test]
25972649
fn lib_before_bin() {
25982650
// Checks that the library is documented before the binary.

0 commit comments

Comments
 (0)