Skip to content

Commit 0a275ab

Browse files
committed
copy doc output files by format r=ozkanonur
Signed-off-by: ozkanonur <[email protected]>
1 parent e4d6307 commit 0a275ab

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

src/bootstrap/doc.rs

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,49 @@ fn doc_std(
551551
extra_args: &[&OsStr],
552552
requested_crates: &[String],
553553
) {
554+
// `cargo` uses the same directory for both JSON docs and HTML docs.
555+
// This could lead to cross-contamination when copying files into the specified `out` directory.
556+
// For example:
557+
// ```bash
558+
// x doc std
559+
// x doc std --json
560+
// ```
561+
// could lead to HTML docs being copied into the JSON docs output directory.
562+
// To avoid this issue, we copy generated docs instead of whole directory by
563+
// checking doc format and generated files.
564+
fn cp_docs_by_doc_format(
565+
format: &DocumentationFormat,
566+
builder: &Builder<'_>,
567+
src: &Path,
568+
dst: &Path,
569+
) {
570+
for f in builder.read_dir(src) {
571+
let path = f.path();
572+
let name = path.file_name().unwrap();
573+
let dst = dst.join(name);
574+
575+
if t!(f.file_type()).is_dir() && format == &DocumentationFormat::HTML {
576+
t!(fs::create_dir_all(&dst));
577+
cp_docs_by_doc_format(format, builder, &path, &dst);
578+
} else {
579+
let _ = fs::remove_file(&dst);
580+
let extension = path.extension().and_then(OsStr::to_str);
581+
582+
match format {
583+
DocumentationFormat::HTML if extension != Some("json") => {
584+
builder.copy(&path, &dst)
585+
}
586+
DocumentationFormat::JSON
587+
if extension == Some("json") || name.to_str() == Some(".stamp") =>
588+
{
589+
builder.copy(&path, &dst)
590+
}
591+
_ => {}
592+
}
593+
}
594+
}
595+
}
596+
554597
builder.info(&format!(
555598
"Documenting stage{} std ({}) in {} format",
556599
stage,
@@ -568,18 +611,6 @@ fn doc_std(
568611
// We will then copy the files from this directory into the final `out` directory, the specified
569612
// as a function parameter.
570613
let out_dir = builder.stage_out(compiler, Mode::Std).join(target.triple).join("doc");
571-
// `cargo` uses the same directory for both JSON docs and HTML docs.
572-
// This could lead to cross-contamination when copying files into the specified `out` directory.
573-
// For example:
574-
// ```bash
575-
// x doc std
576-
// x doc std --json
577-
// ```
578-
// could lead to HTML docs being copied into the JSON docs output directory.
579-
// To avoid this issue, we clean the doc folder before invoking `cargo`.
580-
if out_dir.exists() {
581-
builder.remove_dir(&out_dir);
582-
}
583614

584615
let run_cargo_rustdoc_for = |package: &str| {
585616
let mut cargo = builder.cargo(compiler, Mode::Std, SourceType::InTree, target, "rustdoc");
@@ -605,7 +636,9 @@ fn doc_std(
605636
}
606637
}
607638

608-
builder.cp_r(&out_dir, &out);
639+
if !builder.config.dry_run() {
640+
cp_docs_by_doc_format(&format, builder, &out_dir, &out);
641+
}
609642
}
610643

611644
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
include ../../run-make-fulldeps/tools.mk
2+
3+
OUTPUT_DIR := "$(TMPDIR)/rustdoc"
4+
TMP_OUTPUT_DIR := "$(TMPDIR)/tmp-rustdoc"
5+
6+
all:
7+
# Generate html docs
8+
$(RUSTDOC) src/lib.rs --crate-name foobar --crate-type lib --out-dir $(OUTPUT_DIR)
9+
10+
# Copy first output for to check if it's exactly same after second compilation
11+
cp -R $(OUTPUT_DIR) $(TMP_OUTPUT_DIR)
12+
13+
# Generate html docs once again on same output
14+
$(RUSTDOC) src/lib.rs --crate-name foobar --crate-type lib --out-dir $(OUTPUT_DIR)
15+
16+
# Check if everything exactly same
17+
$(DIFF) -r -q $(OUTPUT_DIR) $(TMP_OUTPUT_DIR)
18+
19+
# Generate json doc on the same output
20+
$(RUSTDOC) src/lib.rs --crate-name foobar --crate-type lib --out-dir $(OUTPUT_DIR) -Z unstable-options --output-format json
21+
22+
# Check if expected json file is generated
23+
[ -e $(OUTPUT_DIR)/foobar.json ]
24+
25+
# TODO
26+
# We should re-generate json doc once again and compare the diff with previously
27+
# generated one. Because layout of json docs changes in each compilation, we can't
28+
# do that currently.
29+
#
30+
# See https://github.com/rust-lang/rust/issues/103785#issuecomment-1307425590 for details.
31+
32+
# remove generated json doc
33+
rm $(OUTPUT_DIR)/foobar.json
34+
35+
# Check if json doc compilation broke any of the html files generated previously
36+
$(DIFF) -r -q $(OUTPUT_DIR) $(TMP_OUTPUT_DIR)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// nothing to see here

0 commit comments

Comments
 (0)