]> git.proxmox.com Git - cargo.git/commitdiff
Ensure host units don't depend on Docscrape units, fixes #10535
authorWill Crichton <wcrichto@cs.stanford.edu>
Fri, 8 Apr 2022 22:00:24 +0000 (15:00 -0700)
committerWill Crichton <wcrichto@cs.stanford.edu>
Fri, 8 Apr 2022 22:01:01 +0000 (15:01 -0700)
src/cargo/core/compiler/mod.rs
src/cargo/core/compiler/unit_dependencies.rs
src/cargo/core/workspace.rs
tests/testsuite/doc.rs

index 5abd06a75bd3a3ea34c560f2226cbd0f9966a1ea..2474a9202bd0152485d72fe91f061a0d252c262e 100644 (file)
@@ -682,7 +682,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
                 rustdoc.arg("--scrape-examples-target-crate").arg(name);
             }
         }
-    } else if cx.bcx.scrape_units.len() > 0 && cx.bcx.ws.is_member(&unit.pkg) {
+    } else if cx.bcx.scrape_units.len() > 0 && cx.bcx.ws.unit_needs_doc_scrape(unit) {
         // We only pass scraped examples to packages in the workspace
         // since examples are only coming from reverse-dependencies of workspace packages
 
index cf1b74a7367be2018d26aca1ff492e420c1ca4e3..4bd8aedf2b2559efea6b2458ac12b429d427ccda 100644 (file)
@@ -708,8 +708,8 @@ fn compute_deps_doc(
         }
     }
 
-    // Add all units being scraped for examples as a dependency of Doc units.
-    if state.ws.is_member(&unit.pkg) {
+    // Add all units being scraped for examples as a dependency of top-level Doc units.
+    if state.ws.unit_needs_doc_scrape(unit) {
         for scrape_unit in state.scrape_units.iter() {
             deps_of(scrape_unit, state, unit_for)?;
             ret.push(new_unit_dep(
index 4c9a5936ff41cd44d0d3431a3d20cd006d44df7b..b24ab13046eeb145e5cc16181b2f83f8261af90e 100644 (file)
@@ -11,6 +11,7 @@ use log::debug;
 use toml_edit::easy as toml;
 use url::Url;
 
+use crate::core::compiler::Unit;
 use crate::core::features::Features;
 use crate::core::registry::PackageRegistry;
 use crate::core::resolver::features::CliFeatures;
@@ -1500,6 +1501,15 @@ impl<'cfg> Workspace<'cfg> {
 
         ms
     }
+
+    /// Returns true if `unit` should depend on the output of Docscrape units.
+    pub fn unit_needs_doc_scrape(&self, unit: &Unit) -> bool {
+        // We do not add scraped units for Host units, as they're either build scripts
+        // (not documented) or proc macros (have no scrape-able exports). Additionally,
+        // naively passing a proc macro's unit_for to new_unit_dep will currently cause
+        // Cargo to panic, see issue #10545.
+        self.is_member(&unit.pkg) && !unit.target.for_host()
+    }
 }
 
 impl<'cfg> Packages<'cfg> {
index dd6a3ec46ca9c98e7ddb6c76d8085e16b8e9b6a3..9e18cee1e10e13dc86e6cc83c4de60e3096f2b68 100644 (file)
@@ -2593,6 +2593,58 @@ fn scrape_examples_configure_profile() {
     assert!(doc_html.contains("More examples"));
 }
 
+#[cargo_test]
+fn scrape_examples_issue_10545() {
+    if !is_nightly() {
+        // -Z rustdoc-scrape-examples is unstable
+        return;
+    }
+
+    let p = project()
+        .file(
+            "Cargo.toml",
+            r#"                
+                [workspace]
+                resolver = "2"
+                members = ["a", "b"]              
+            "#,
+        )
+        .file(
+            "a/Cargo.toml",
+            r#"
+            [package]
+            name = "a"
+            version = "0.0.1"
+            authors = []
+            edition = "2021"
+
+            [features]
+            default = ["foo"]
+            foo = []
+        "#,
+        )
+        .file("a/src/lib.rs", "")
+        .file(
+            "b/Cargo.toml",
+            r#"
+                [package]
+                name = "b"
+                version = "0.0.1"
+                authors = []
+                edition = "2021"
+
+                [lib]
+                proc-macro = true
+            "#,
+        )
+        .file("b/src/lib.rs", "")
+        .build();
+
+    p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples=all")
+        .masquerade_as_nightly_cargo()
+        .run();
+}
+
 #[cargo_test]
 fn lib_before_bin() {
     // Checks that the library is documented before the binary.