Skip to content

Commit 03bb80c

Browse files
authored
Rollup merge of #144600 - Noratrieb:rustdoc-dep-info-paths, r=GuillaumeGomez
Ensure external paths passed via flags end up in rustdoc depinfo rustdoc has many flags to pass external HTML/Markdown/CSS files that end up in the build. These need to be recorded in depinfo so that Cargo will rebuild the crate if they change.
2 parents 0abc0c4 + 327ee15 commit 03bb80c

File tree

9 files changed

+53
-17
lines changed

9 files changed

+53
-17
lines changed

src/librustdoc/config.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl Options {
380380
early_dcx: &mut EarlyDiagCtxt,
381381
matches: &getopts::Matches,
382382
args: Vec<String>,
383-
) -> Option<(InputMode, Options, RenderOptions)> {
383+
) -> Option<(InputMode, Options, RenderOptions, Vec<PathBuf>)> {
384384
// Check for unstable options.
385385
nightly_options::check_nightly_options(early_dcx, matches, &opts());
386386

@@ -643,10 +643,13 @@ impl Options {
643643

644644
let extension_css = matches.opt_str("e").map(|s| PathBuf::from(&s));
645645

646-
if let Some(ref p) = extension_css
647-
&& !p.is_file()
648-
{
649-
dcx.fatal("option --extend-css argument must be a file");
646+
let mut loaded_paths = Vec::new();
647+
648+
if let Some(ref p) = extension_css {
649+
loaded_paths.push(p.clone());
650+
if !p.is_file() {
651+
dcx.fatal("option --extend-css argument must be a file");
652+
}
650653
}
651654

652655
let mut themes = Vec::new();
@@ -690,6 +693,7 @@ impl Options {
690693
))
691694
.emit();
692695
}
696+
loaded_paths.push(theme_file.clone());
693697
themes.push(StylePath { path: theme_file });
694698
}
695699
}
@@ -708,6 +712,7 @@ impl Options {
708712
&mut id_map,
709713
edition,
710714
&None,
715+
&mut loaded_paths,
711716
) else {
712717
dcx.fatal("`ExternalHtml::load` failed");
713718
};
@@ -799,7 +804,8 @@ impl Options {
799804

800805
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
801806
let with_examples = matches.opt_strs("with-examples");
802-
let call_locations = crate::scrape_examples::load_call_locations(with_examples, dcx);
807+
let call_locations =
808+
crate::scrape_examples::load_call_locations(with_examples, dcx, &mut loaded_paths);
803809
let doctest_build_args = matches.opt_strs("doctest-build-arg");
804810

805811
let unstable_features =
@@ -885,7 +891,7 @@ impl Options {
885891
parts_out_dir,
886892
disable_minification,
887893
};
888-
Some((input, options, render_options))
894+
Some((input, options, render_options, loaded_paths))
889895
}
890896
}
891897

src/librustdoc/externalfiles.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::Path;
1+
use std::path::{Path, PathBuf};
22
use std::{fs, str};
33

44
use rustc_errors::DiagCtxtHandle;
@@ -32,12 +32,13 @@ impl ExternalHtml {
3232
id_map: &mut IdMap,
3333
edition: Edition,
3434
playground: &Option<Playground>,
35+
loaded_paths: &mut Vec<PathBuf>,
3536
) -> Option<ExternalHtml> {
3637
let codes = ErrorCodes::from(nightly_build);
37-
let ih = load_external_files(in_header, dcx)?;
38+
let ih = load_external_files(in_header, dcx, loaded_paths)?;
3839
let bc = {
39-
let mut bc = load_external_files(before_content, dcx)?;
40-
let m_bc = load_external_files(md_before_content, dcx)?;
40+
let mut bc = load_external_files(before_content, dcx, loaded_paths)?;
41+
let m_bc = load_external_files(md_before_content, dcx, loaded_paths)?;
4142
Markdown {
4243
content: &m_bc,
4344
links: &[],
@@ -52,8 +53,8 @@ impl ExternalHtml {
5253
bc
5354
};
5455
let ac = {
55-
let mut ac = load_external_files(after_content, dcx)?;
56-
let m_ac = load_external_files(md_after_content, dcx)?;
56+
let mut ac = load_external_files(after_content, dcx, loaded_paths)?;
57+
let m_ac = load_external_files(md_after_content, dcx, loaded_paths)?;
5758
Markdown {
5859
content: &m_ac,
5960
links: &[],
@@ -79,8 +80,10 @@ pub(crate) enum LoadStringError {
7980
pub(crate) fn load_string<P: AsRef<Path>>(
8081
file_path: P,
8182
dcx: DiagCtxtHandle<'_>,
83+
loaded_paths: &mut Vec<PathBuf>,
8284
) -> Result<String, LoadStringError> {
8385
let file_path = file_path.as_ref();
86+
loaded_paths.push(file_path.to_owned());
8487
let contents = match fs::read(file_path) {
8588
Ok(bytes) => bytes,
8689
Err(e) => {
@@ -101,10 +104,14 @@ pub(crate) fn load_string<P: AsRef<Path>>(
101104
}
102105
}
103106

104-
fn load_external_files(names: &[String], dcx: DiagCtxtHandle<'_>) -> Option<String> {
107+
fn load_external_files(
108+
names: &[String],
109+
dcx: DiagCtxtHandle<'_>,
110+
loaded_paths: &mut Vec<PathBuf>,
111+
) -> Option<String> {
105112
let mut out = String::new();
106113
for name in names {
107-
let Ok(s) = load_string(name, dcx) else { return None };
114+
let Ok(s) = load_string(name, dcx, loaded_paths) else { return None };
108115
out.push_str(&s);
109116
out.push('\n');
110117
}

src/librustdoc/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) {
799799

800800
// Note that we discard any distinction between different non-zero exit
801801
// codes from `from_matches` here.
802-
let (input, options, render_options) =
802+
let (input, options, render_options, loaded_paths) =
803803
match config::Options::from_matches(early_dcx, &matches, args) {
804804
Some(opts) => opts,
805805
None => return,
@@ -870,6 +870,12 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) {
870870
interface::run_compiler(config, |compiler| {
871871
let sess = &compiler.sess;
872872

873+
// Register the loaded external files in the source map so they show up in depinfo.
874+
// We can't load them via the source map because it gets created after we process the options.
875+
for external_path in &loaded_paths {
876+
let _ = sess.source_map().load_file(external_path);
877+
}
878+
873879
if sess.opts.describe_lints {
874880
rustc_driver::describe_lints(sess, registered_lints);
875881
return;

src/librustdoc/scrape_examples.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,11 @@ pub(crate) fn run(
333333
pub(crate) fn load_call_locations(
334334
with_examples: Vec<String>,
335335
dcx: DiagCtxtHandle<'_>,
336+
loaded_paths: &mut Vec<PathBuf>,
336337
) -> AllCallLocations {
337338
let mut all_calls: AllCallLocations = FxIndexMap::default();
338339
for path in with_examples {
340+
loaded_paths.push(path.clone().into());
339341
let bytes = match fs::read(&path) {
340342
Ok(bytes) => bytes,
341343
Err(e) => dcx.fatal(format!("failed to load examples: {e}")),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
meow! :3

tests/run-make/rustdoc-dep-info/before.html

Whitespace-only changes.

tests/run-make/rustdoc-dep-info/extend.css

Whitespace-only changes.

tests/run-make/rustdoc-dep-info/rmake.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@ use run_make_support::{path, rfs, rustdoc};
99
fn main() {
1010
// We're only emitting dep info, so we shouldn't be running static analysis to
1111
// figure out that this program is erroneous.
12-
rustdoc().input("lib.rs").arg("-Zunstable-options").emit("dep-info").run();
12+
// Ensure that all kinds of input reading flags end up in dep-info.
13+
rustdoc()
14+
.input("lib.rs")
15+
.arg("-Zunstable-options")
16+
.arg("--html-before-content=before.html")
17+
.arg("--markdown-after-content=after.md")
18+
.arg("--extend-css=extend.css")
19+
.arg("--theme=theme.css")
20+
.emit("dep-info")
21+
.run();
1322

1423
let content = rfs::read_to_string("foo.d");
1524
assert_contains(&content, "lib.rs:");
1625
assert_contains(&content, "foo.rs:");
1726
assert_contains(&content, "bar.rs:");
1827
assert_contains(&content, "doc.md:");
28+
assert_contains(&content, "after.md:");
29+
assert_contains(&content, "before.html:");
30+
assert_contains(&content, "extend.css:");
31+
assert_contains(&content, "theme.css:");
1932

2033
// Now we check that we can provide a file name to the `dep-info` argument.
2134
rustdoc().input("lib.rs").arg("-Zunstable-options").emit("dep-info=bla.d").run();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* This is not a valid theme but that doesn't really matter */

0 commit comments

Comments
 (0)