Skip to content

Commit 24a93e7

Browse files
Rollup merge of #160576 - GuillaumeGomez:rustdoc-standalone-md, r=Urgau,fmease
[rustdoc] Create output file after we checked that the standalone markdown file is valid This PR makes the creation of the output (HTML) file after we checked that the input markdown is valid to prevent the output file content to be truncated ([`File::create` doc](https://doc.rust-lang.org/nightly/std/fs/struct.File.html#method.create)) in any case. r? @Urgau
2 parents 34e12b4 + 1de2daa commit 24a93e7

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/librustdoc/markdown.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ pub(crate) fn render_and_write(
6969
let playground_url = options.markdown_playground_url.or(options.playground_url);
7070
let playground = playground_url.map(|url| markdown::Playground { crate_name: None, url });
7171

72-
let mut out =
73-
File::create(&output).map_err(|e| format!("{output}: {e}", output = output.display()))?;
74-
7572
let (metadata, text) = extract_leading_metadata(&input_str);
7673
if metadata.is_empty() {
7774
return Err("invalid markdown file: no initial lines starting with `# ` or `%`".to_owned());
7875
}
76+
77+
let mut out =
78+
File::create(&output).map_err(|e| format!("{output}: {e}", output = output.display()))?;
79+
7980
let title = metadata[0];
8081

8182
let error_codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// When rustdoc gets a markdown file as input, we want to ensure that if the markdown is invalid,
2+
// the output file won't be truncated in case this markdown is invalid.
3+
4+
//@ needs-target-std
5+
6+
use run_make_support::{path, rfs, rustdoc};
7+
8+
fn main() {
9+
let output_content = "output";
10+
let base_file_name = "input";
11+
12+
let out_dir = path("out");
13+
rfs::create_dir(&out_dir);
14+
15+
// We create the file that should be created by rustdoc and add some content
16+
// into it that we will check is still there once rustdoc failed.
17+
let output = out_dir.join(format!("{base_file_name}.html"));
18+
rfs::write(&output, output_content);
19+
20+
// We create an "invalid" markdown file (ie no title).
21+
let md_file = format!("{base_file_name}.md");
22+
rfs::write(&md_file, "Markdown without a title");
23+
24+
// We run the failing rustdoc.
25+
rustdoc()
26+
.input(&md_file)
27+
.out_dir(&out_dir)
28+
.run_fail()
29+
.assert_exit_code(1)
30+
.assert_stderr_contains(
31+
"error: invalid markdown file: no initial lines starting with `# ` or `%`",
32+
);
33+
34+
// Shouldn't have changed.
35+
assert_eq!(rfs::read_to_string(&output), output_content);
36+
37+
// We update the input markdown to make it valid for rustdoc.
38+
rfs::write(&md_file, "# a title\n\nMarkdown with a title");
39+
40+
// We run rustdoc successfully.
41+
rustdoc().input(&md_file).out_dir(&out_dir).run();
42+
43+
// Should have changed.
44+
assert_ne!(rfs::read_to_string(output), output_content);
45+
}

0 commit comments

Comments
 (0)