From 95a27cdbd18424c564045091cea416c71760f335 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 30 Nov 2023 13:48:59 +0100 Subject: [PATCH 1/2] Add regression test for scraped examples in crates without `[workspace]` --- tests/testsuite/docscrape.rs | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tests/testsuite/docscrape.rs b/tests/testsuite/docscrape.rs index d4d011ff32f..354fbe42f0a 100644 --- a/tests/testsuite/docscrape.rs +++ b/tests/testsuite/docscrape.rs @@ -48,6 +48,85 @@ fn basic() { assert!(p.build_dir().join("doc/src/ex/ex.rs.html").exists()); } +// This test ensures that if there is no `[workspace]` in the top-level `Cargo.toml` file, the +// dependencies will not get their examples scraped and that they appear in the generated +// documentation. +#[cargo_test(nightly, reason = "-Zrustdoc-scrape-examples is unstable")] +fn scrape_examples_for_non_workspace_reexports() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2021" + authors = [] + + [dependencies] + a = { path = "crates/a" } + "#, + ) + .file("src/lib.rs", "pub use a::*;") + // Example + .file( + "examples/one.rs", + r#"use foo::*; +fn main() { + let foo = Foo::new("yes".into()); + foo.maybe(); +}"#, + ) + // `a` crate + .file( + "crates/a/Cargo.toml", + r#" + [package] + name = "a" + version = "0.0.1" + authors = [] + "#, + ) + .file( + "crates/a/src/lib.rs", + r#" +#[derive(Debug)] +pub struct Foo { + foo: String, + yes: bool, +} + +impl Foo { + pub fn new(foo: String) -> Self { + Self { foo, yes: true } + } + + pub fn maybe(&self) { + if self.yes { + println!("{}", self.foo) + } + } +}"#, + ) + .build(); + + p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples --no-deps") + .masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"]) + .with_stderr_unordered( + "\ +[CHECKING] a v0.0.1 ([CWD]/crates/a) +[CHECKING] foo v0.0.1 ([CWD]) +[SCRAPING] foo v0.0.1 ([CWD]) +[DOCUMENTING] foo v0.0.1 ([CWD]) +[FINISHED] [..] +[GENERATED] [CWD]/target/doc/foo/index.html", + ) + .run(); + + let doc_html = p.read_file("target/doc/foo/struct.Foo.html"); + assert!(!doc_html.contains("Examples found in repository")); +} + #[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")] fn avoid_build_script_cycle() { let p = project() From 689d9a7e4fe669bb2b12385f50331e7e0e0c56c0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 30 Nov 2023 11:42:19 +0100 Subject: [PATCH 2/2] Don't filter on workspace members when scraping doc examples --- src/cargo/core/compiler/mod.rs | 2 +- tests/testsuite/docscrape.rs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 0801783dc7b..3bab4462f60 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -743,7 +743,7 @@ fn prepare_rustdoc(cx: &Context<'_, '_>, unit: &Unit) -> CargoResult