From 8611f0b67c7aea4c43d5a3f39b0e84a6d0950868 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Wed, 29 Oct 2025 11:30:37 -0700 Subject: [PATCH 1/2] rustdoc: fix `--emit=dep-info` on scraped examples --- src/librustdoc/lib.rs | 2 +- src/librustdoc/scrape_examples.rs | 5 +++++ .../examples/ex.rs | 6 ++++++ .../rustdoc-scrape-examples-dep-info/rmake.rs | 19 +++++++++++++++++++ .../src/lib.rs | 3 +++ .../rmake.rs | 2 +- .../rustdoc-scrape-examples-multiple/rmake.rs | 2 +- .../rustdoc-scrape-examples-ordering/rmake.rs | 2 +- .../rustdoc-scrape-examples-remap/rmake.rs | 2 +- .../rustdoc-scrape-examples-remap/scrape.rs | 5 +++-- .../rustdoc-scrape-examples-test/rmake.rs | 2 +- .../rmake.rs | 2 +- 12 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 tests/run-make/rustdoc-scrape-examples-dep-info/examples/ex.rs create mode 100644 tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs create mode 100644 tests/run-make/rustdoc-scrape-examples-dep-info/src/lib.rs diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index dd6378b25def7..c6a0682a80c48 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -896,7 +896,7 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) { // Register the loaded external files in the source map so they show up in depinfo. // We can't load them via the source map because it gets created after we process the options. for external_path in &loaded_paths { - let _ = sess.source_map().load_file(external_path); + let _ = sess.source_map().load_binary_file(external_path); } if sess.opts.describe_lints { diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 471e966e2c24b..1b3c96d82b410 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -273,6 +273,7 @@ pub(crate) fn run( bin_crate: bool, ) { let inner = move || -> Result<(), String> { + let emit_dep_info = renderopts.dep_info().is_some(); // Generates source files for examples renderopts.no_emit_shared = true; let (cx, _) = Context::init(krate, renderopts, cache, tcx, Default::default()) @@ -320,6 +321,10 @@ pub(crate) fn run( calls.encode(&mut encoder); encoder.finish().map_err(|(_path, e)| e.to_string())?; + if emit_dep_info { + rustc_interface::passes::write_dep_info(tcx); + } + Ok(()) }; diff --git a/tests/run-make/rustdoc-scrape-examples-dep-info/examples/ex.rs b/tests/run-make/rustdoc-scrape-examples-dep-info/examples/ex.rs new file mode 100644 index 0000000000000..c37b8dd48853a --- /dev/null +++ b/tests/run-make/rustdoc-scrape-examples-dep-info/examples/ex.rs @@ -0,0 +1,6 @@ +fn main() {} + +#[test] +fn a_test() { + foobar::ok(); +} diff --git a/tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs b/tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs new file mode 100644 index 0000000000000..7dfa6584785ae --- /dev/null +++ b/tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs @@ -0,0 +1,19 @@ +//@ needs-target-std +use run_make_support::{assert_contains, rfs}; + +#[path = "../rustdoc-scrape-examples-remap/scrape.rs"] +mod scrape; + +fn main() { + scrape::scrape( + &["--scrape-tests", "--emit=dep-info"], + &["--emit=dep-info,invocation-specific"], + ); + + let content = rfs::read_to_string("foobar.d"); + assert_contains(&content, "lib.rs:"); + assert_contains(&content, "rustdoc/ex.calls:"); + + let content = rfs::read_to_string("ex.d"); + assert_contains(&content, "examples/ex.rs:"); +} diff --git a/tests/run-make/rustdoc-scrape-examples-dep-info/src/lib.rs b/tests/run-make/rustdoc-scrape-examples-dep-info/src/lib.rs new file mode 100644 index 0000000000000..93d56b7e972f8 --- /dev/null +++ b/tests/run-make/rustdoc-scrape-examples-dep-info/src/lib.rs @@ -0,0 +1,3 @@ +//@ has foobar/fn.ok.html '//*[@class="docblock scraped-example-list"]' '' + +pub fn ok() {} diff --git a/tests/run-make/rustdoc-scrape-examples-invalid-expr/rmake.rs b/tests/run-make/rustdoc-scrape-examples-invalid-expr/rmake.rs index 8996ff184c900..38943b663c420 100644 --- a/tests/run-make/rustdoc-scrape-examples-invalid-expr/rmake.rs +++ b/tests/run-make/rustdoc-scrape-examples-invalid-expr/rmake.rs @@ -3,5 +3,5 @@ mod scrape; fn main() { - scrape::scrape(&[]); + scrape::scrape(&[], &[]); } diff --git a/tests/run-make/rustdoc-scrape-examples-multiple/rmake.rs b/tests/run-make/rustdoc-scrape-examples-multiple/rmake.rs index 8996ff184c900..38943b663c420 100644 --- a/tests/run-make/rustdoc-scrape-examples-multiple/rmake.rs +++ b/tests/run-make/rustdoc-scrape-examples-multiple/rmake.rs @@ -3,5 +3,5 @@ mod scrape; fn main() { - scrape::scrape(&[]); + scrape::scrape(&[], &[]); } diff --git a/tests/run-make/rustdoc-scrape-examples-ordering/rmake.rs b/tests/run-make/rustdoc-scrape-examples-ordering/rmake.rs index 8996ff184c900..38943b663c420 100644 --- a/tests/run-make/rustdoc-scrape-examples-ordering/rmake.rs +++ b/tests/run-make/rustdoc-scrape-examples-ordering/rmake.rs @@ -3,5 +3,5 @@ mod scrape; fn main() { - scrape::scrape(&[]); + scrape::scrape(&[], &[]); } diff --git a/tests/run-make/rustdoc-scrape-examples-remap/rmake.rs b/tests/run-make/rustdoc-scrape-examples-remap/rmake.rs index ead3920c76132..920a65d56f8e5 100644 --- a/tests/run-make/rustdoc-scrape-examples-remap/rmake.rs +++ b/tests/run-make/rustdoc-scrape-examples-remap/rmake.rs @@ -2,5 +2,5 @@ mod scrape; fn main() { - scrape::scrape(&[]); + scrape::scrape(&[], &[]); } diff --git a/tests/run-make/rustdoc-scrape-examples-remap/scrape.rs b/tests/run-make/rustdoc-scrape-examples-remap/scrape.rs index c4d7814c3c831..668cb3797fb9e 100644 --- a/tests/run-make/rustdoc-scrape-examples-remap/scrape.rs +++ b/tests/run-make/rustdoc-scrape-examples-remap/scrape.rs @@ -2,7 +2,7 @@ use std::path::Path; use run_make_support::{htmldocck, rfs, rustc, rustdoc}; -pub fn scrape(extra_args: &[&str]) { +pub fn scrape(extra_args_scrape: &[&str], extra_args_doc: &[&str]) { let out_dir = Path::new("rustdoc"); let crate_name = "foobar"; let deps = rfs::read_dir("examples") @@ -27,7 +27,7 @@ pub fn scrape(extra_args: &[&str]) { .arg(&out_example) .arg("--scrape-examples-target-crate") .arg(crate_name) - .args(extra_args) + .args(extra_args_scrape) .run(); out_deps.push(out_example); } @@ -42,6 +42,7 @@ pub fn scrape(extra_args: &[&str]) { for dep in out_deps { rustdoc.arg("--with-examples").arg(dep); } + rustdoc.args(extra_args_doc); rustdoc.run(); htmldocck().arg(out_dir).arg("src/lib.rs").run(); diff --git a/tests/run-make/rustdoc-scrape-examples-test/rmake.rs b/tests/run-make/rustdoc-scrape-examples-test/rmake.rs index 0868507c4ae5b..c0c4df91d517d 100644 --- a/tests/run-make/rustdoc-scrape-examples-test/rmake.rs +++ b/tests/run-make/rustdoc-scrape-examples-test/rmake.rs @@ -3,5 +3,5 @@ mod scrape; fn main() { - scrape::scrape(&["--scrape-tests"]); + scrape::scrape(&["--scrape-tests"], &[]); } diff --git a/tests/run-make/rustdoc-scrape-examples-whitespace/rmake.rs b/tests/run-make/rustdoc-scrape-examples-whitespace/rmake.rs index 8996ff184c900..38943b663c420 100644 --- a/tests/run-make/rustdoc-scrape-examples-whitespace/rmake.rs +++ b/tests/run-make/rustdoc-scrape-examples-whitespace/rmake.rs @@ -3,5 +3,5 @@ mod scrape; fn main() { - scrape::scrape(&[]); + scrape::scrape(&[], &[]); } From 65aed3b6e63bc7ad9ed61785197f882bfc071089 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 31 Oct 2025 11:16:00 -0700 Subject: [PATCH 2/2] Fix test on Windows --- tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs b/tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs index 7dfa6584785ae..00a87477ab5e4 100644 --- a/tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs +++ b/tests/run-make/rustdoc-scrape-examples-dep-info/rmake.rs @@ -10,10 +10,10 @@ fn main() { &["--emit=dep-info,invocation-specific"], ); - let content = rfs::read_to_string("foobar.d"); + let content = rfs::read_to_string("foobar.d").replace(r"\", "/"); assert_contains(&content, "lib.rs:"); assert_contains(&content, "rustdoc/ex.calls:"); - let content = rfs::read_to_string("ex.d"); + let content = rfs::read_to_string("ex.d").replace(r"\", "/"); assert_contains(&content, "examples/ex.rs:"); }