From a481387e7b1e9912d6166dc78b09b03474d3a265 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 9 Feb 2023 20:09:46 +0000 Subject: [PATCH 1/2] test(docscrape): must fail with bad build script --- tests/testsuite/docscrape.rs | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/testsuite/docscrape.rs b/tests/testsuite/docscrape.rs index 6a20540e1fb..6435338e0d9 100644 --- a/tests/testsuite/docscrape.rs +++ b/tests/testsuite/docscrape.rs @@ -366,6 +366,43 @@ warning: `foo` (example \"ex2\") generated 1 warning .run(); } +#[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")] +fn fail_bad_build_script() { + // See rust-lang/cargo#11623 + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + "#, + ) + .file("src/lib.rs", "") + .file("build.rs", "fn main() { panic!(\"You shall not pass\")}") + .file("examples/ex.rs", "fn main() {}") + .build(); + + // `cargo doc` fails + p.cargo("doc") + .with_status(101) + .with_stderr_contains("[..]You shall not pass[..]") + .run(); + + // FIXME: scrape examples should fail whenever `cargo doc` fails. + p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples") + .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) + .with_stderr( + "\ +[COMPILING] foo v0.0.1 ([CWD]) +[SCRAPING] foo v0.0.1 ([CWD]) +[DOCUMENTING] foo v0.0.1 ([CWD]) +[FINISHED] dev [..] +", + ) + .run(); +} + #[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")] fn no_fail_bad_example() { let p = project() From 0da332c66e3afb8e4b0acbf4e488587af642dcc9 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 9 Feb 2023 20:15:00 +0000 Subject: [PATCH 2/2] fix(docscrape): must fail with bad build script When running `cargo doc -Zrustdoc-scrape-example`, it performs additional type checks that a plain old `cargo doc` doesn't. That leads to some extra failure when adopting scrape-example for docs.rs. In de34d60 we introduced `unit_can_fail_for_docscraping` in order to make `-Zrustdoc-scrape-example` not fail whenever `cargo doc` builds. Build script executions were accidentally included in the list of fallible units. A plain `cargo doc` does fail when a build script fails. `-Zrustdoc-scrape-example` should follow that. --- src/cargo/core/compiler/rustdoc.rs | 14 +++++++++++--- tests/testsuite/docscrape.rs | 12 +++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/cargo/core/compiler/rustdoc.rs b/src/cargo/core/compiler/rustdoc.rs index d6a6ab9755a..6443e54bf2f 100644 --- a/src/cargo/core/compiler/rustdoc.rs +++ b/src/cargo/core/compiler/rustdoc.rs @@ -211,16 +211,24 @@ impl RustdocScrapeExamples { } impl BuildContext<'_, '_> { - /// Returns the set of Docscrape units that have a direct dependency on `unit` + /// Returns the set of [`Docscrape`] units that have a direct dependency on `unit`. + /// + /// [`RunCustomBuild`] units are excluded because we allow failures + /// from type checks but not build script executions. + /// A plain old `cargo doc` would just die if a build script execution fails, + /// there is no reason for `-Zrustdoc-scrape-examples` to keep going. + /// + /// [`Docscrape`]: crate::core::compiler::CompileMode::Docscrape + /// [`RunCustomBuild`]: crate::core::compiler::CompileMode::Docscrape pub fn scrape_units_have_dep_on<'a>(&'a self, unit: &'a Unit) -> Vec<&'a Unit> { self.scrape_units .iter() .filter(|scrape_unit| { self.unit_graph[scrape_unit] .iter() - .any(|dep| &dep.unit == unit) + .any(|dep| &dep.unit == unit && !dep.unit.mode.is_run_custom_build()) }) - .collect::>() + .collect() } /// Returns true if this unit is needed for doing doc-scraping and is also diff --git a/tests/testsuite/docscrape.rs b/tests/testsuite/docscrape.rs index 6435338e0d9..c536a6738f0 100644 --- a/tests/testsuite/docscrape.rs +++ b/tests/testsuite/docscrape.rs @@ -389,17 +389,11 @@ fn fail_bad_build_script() { .with_stderr_contains("[..]You shall not pass[..]") .run(); - // FIXME: scrape examples should fail whenever `cargo doc` fails. + // scrape examples should fail whenever `cargo doc` fails. p.cargo("doc -Zunstable-options -Z rustdoc-scrape-examples") .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) - .with_stderr( - "\ -[COMPILING] foo v0.0.1 ([CWD]) -[SCRAPING] foo v0.0.1 ([CWD]) -[DOCUMENTING] foo v0.0.1 ([CWD]) -[FINISHED] dev [..] -", - ) + .with_status(101) + .with_stderr_contains("[..]You shall not pass[..]") .run(); }