Skip to content

Commit 1ef8575

Browse files
committed
Auto merge of #13077 - GuillaumeGomez:filter-workspace-scrape-example, r=epage
Don't filter on workspace members when scraping doc examples Fixes #13074. I confirmed locally that it fixed the issue in docs.rs. cc `@willcrichton` r? `@epage`
2 parents 7ed235a + 689d9a7 commit 1ef8575

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/cargo/core/compiler/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ fn prepare_rustdoc(cx: &Context<'_, '_>, unit: &Unit) -> CargoResult<ProcessBuil
743743
.arg(scrape_output_path(cx, unit)?);
744744

745745
// Only scrape example for items from crates in the workspace, to reduce generated file size
746-
for pkg in cx.bcx.ws.members() {
746+
for pkg in cx.bcx.packages.packages() {
747747
let names = pkg
748748
.targets()
749749
.iter()

tests/testsuite/docscrape.rs

+78
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,84 @@ fn basic() {
4848
assert!(p.build_dir().join("doc/src/ex/ex.rs.html").exists());
4949
}
5050

51+
// This test ensures that even if there is no `[workspace]` in the top-level `Cargo.toml` file, the
52+
// dependencies will get their examples scraped and that they appear in the generated documentation.
53+
#[cargo_test(nightly, reason = "-Zrustdoc-scrape-examples is unstable")]
54+
fn scrape_examples_for_non_workspace_reexports() {
55+
let p = project()
56+
.file(
57+
"Cargo.toml",
58+
r#"
59+
[package]
60+
name = "foo"
61+
version = "0.0.1"
62+
edition = "2021"
63+
authors = []
64+
65+
[dependencies]
66+
a = { path = "crates/a" }
67+
"#,
68+
)
69+
.file("src/lib.rs", "pub use a::*;")
70+
// Example
71+
.file(
72+
"examples/one.rs",
73+
r#"use foo::*;
74+
fn main() {
75+
let foo = Foo::new("yes".into());
76+
foo.maybe();
77+
}"#,
78+
)
79+
// `a` crate
80+
.file(
81+
"crates/a/Cargo.toml",
82+
r#"
83+
[package]
84+
name = "a"
85+
version = "0.0.1"
86+
authors = []
87+
"#,
88+
)
89+
.file(
90+
"crates/a/src/lib.rs",
91+
r#"
92+
#[derive(Debug)]
93+
pub struct Foo {
94+
foo: String,
95+
yes: bool,
96+
}
97+
98+
impl Foo {
99+
pub fn new(foo: String) -> Self {
100+
Self { foo, yes: true }
101+
}
102+
103+
pub fn maybe(&self) {
104+
if self.yes {
105+
println!("{}", self.foo)
106+
}
107+
}
108+
}"#,
109+
)
110+
.build();
111+
112+
p.cargo("doc -Zunstable-options -Zrustdoc-scrape-examples --no-deps")
113+
.masquerade_as_nightly_cargo(&["rustdoc-scrape-examples"])
114+
.with_stderr_unordered(
115+
"\
116+
[CHECKING] a v0.0.1 ([CWD]/crates/a)
117+
[CHECKING] foo v0.0.1 ([CWD])
118+
[SCRAPING] foo v0.0.1 ([CWD])
119+
[DOCUMENTING] foo v0.0.1 ([CWD])
120+
[FINISHED] [..]
121+
[GENERATED] [CWD]/target/doc/foo/index.html",
122+
)
123+
.run();
124+
125+
let doc_html = p.read_file("target/doc/foo/struct.Foo.html");
126+
assert!(doc_html.contains("Examples found in repository"));
127+
}
128+
51129
#[cargo_test(nightly, reason = "rustdoc scrape examples flags are unstable")]
52130
fn avoid_build_script_cycle() {
53131
let p = project()

0 commit comments

Comments
 (0)