Skip to content

Commit 4369396

Browse files
committed
Auto merge of rust-lang#9419 - ehuss:doc-meta-rebuild, r=alexcrichton
Fix rebuild issues with rustdoc. This fixes two issues related to rebuilds with rustdoc: * Switching features when running `cargo doc` would result in Cargo not rebuilding the documentation. This is because it was keeping the fingerprints in separate directories based on the features used. However, the rustdoc output isn't keyed off the metadata hash, so although the old fingerprint seemed "up to date", in reality the documentation was rewritten and needs to be rebuilt. The solution is to use a simplified hash for the fingerprint directory name. * Removing items does not remove the files from the doc directory. This changes it to clear the package's doc directory before running rustdoc, to ensure any stale files are removed. I'm a little concerned about potential performance impact of running `remove_dir_all`, but I think it shouldn't be too bad? Fixes rust-lang#7370
2 parents 52a0ca5 + 44c549e commit 4369396

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/cargo/core/compiler/context/compilation_files.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) {
615615

616616
/// Returns whether or not this unit should use a metadata hash.
617617
fn should_use_metadata(bcx: &BuildContext<'_, '_>, unit: &Unit) -> bool {
618-
if unit.mode.is_doc_test() {
618+
if unit.mode.is_doc_test() || unit.mode.is_doc() {
619619
// Doc tests do not have metadata.
620620
return false;
621621
}

src/cargo/core/compiler/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
599599
// script_metadata is not needed here, it is only for tests.
600600
let mut rustdoc = cx.compilation.rustdoc_process(unit, None)?;
601601
rustdoc.inherit_jobserver(&cx.jobserver);
602-
rustdoc.arg("--crate-name").arg(&unit.target.crate_name());
602+
let crate_name = unit.target.crate_name();
603+
rustdoc.arg("--crate-name").arg(&crate_name);
603604
add_path_args(bcx.ws, unit, &mut rustdoc);
604605
add_cap_lints(bcx, unit, &mut rustdoc);
605606

@@ -613,7 +614,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
613614
// it doesn't already exist.
614615
paths::create_dir_all(&doc_dir)?;
615616

616-
rustdoc.arg("-o").arg(doc_dir);
617+
rustdoc.arg("-o").arg(&doc_dir);
617618

618619
for feat in &unit.features {
619620
rustdoc.arg("--cfg").arg(&format!("feature=\"{}\"", feat));
@@ -653,6 +654,13 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
653654
}
654655
}
655656
}
657+
let crate_dir = doc_dir.join(&crate_name);
658+
if crate_dir.exists() {
659+
// Remove output from a previous build. This ensures that stale
660+
// files for removed items are removed.
661+
log::debug!("removing pre-existing doc directory {:?}", crate_dir);
662+
paths::remove_dir_all(crate_dir)?;
663+
}
656664
state.running(&rustdoc);
657665

658666
rustdoc

tests/testsuite/doc.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -863,10 +863,44 @@ fn features() {
863863
r#"#[cfg(feature = "bar")] pub fn bar() {}"#,
864864
)
865865
.build();
866-
p.cargo("doc --features foo").run();
866+
p.cargo("doc --features foo")
867+
.with_stderr(
868+
"\
869+
[COMPILING] bar v0.0.1 [..]
870+
[DOCUMENTING] bar v0.0.1 [..]
871+
[DOCUMENTING] foo v0.0.1 [..]
872+
[FINISHED] [..]
873+
",
874+
)
875+
.run();
867876
assert!(p.root().join("target/doc").is_dir());
868877
assert!(p.root().join("target/doc/foo/fn.foo.html").is_file());
869878
assert!(p.root().join("target/doc/bar/fn.bar.html").is_file());
879+
// Check that turning the feature off will remove the files.
880+
p.cargo("doc")
881+
.with_stderr(
882+
"\
883+
[COMPILING] bar v0.0.1 [..]
884+
[DOCUMENTING] bar v0.0.1 [..]
885+
[DOCUMENTING] foo v0.0.1 [..]
886+
[FINISHED] [..]
887+
",
888+
)
889+
.run();
890+
assert!(!p.root().join("target/doc/foo/fn.foo.html").is_file());
891+
assert!(!p.root().join("target/doc/bar/fn.bar.html").is_file());
892+
// And switching back will rebuild and bring them back.
893+
p.cargo("doc --features foo")
894+
.with_stderr(
895+
"\
896+
[DOCUMENTING] bar v0.0.1 [..]
897+
[DOCUMENTING] foo v0.0.1 [..]
898+
[FINISHED] [..]
899+
",
900+
)
901+
.run();
902+
assert!(p.root().join("target/doc/foo/fn.foo.html").is_file());
903+
assert!(p.root().join("target/doc/bar/fn.bar.html").is_file());
870904
}
871905

872906
#[cargo_test]

0 commit comments

Comments
 (0)