Skip to content

Commit fac0ff7

Browse files
committed
refactor(fingerprint): extract rustdoc fingerprint check
This is a preparation for per-target check. Currently it checks only host build-dir for the entire workspace
1 parent 21ba213 commit fac0ff7

File tree

1 file changed

+63
-53
lines changed

1 file changed

+63
-53
lines changed

src/cargo/core/compiler/fingerprint/rustdoc.rs

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,64 +47,74 @@ impl RustDocFingerprint {
4747
rustc_vv: build_runner.bcx.rustc().verbose_version.clone(),
4848
};
4949

50-
let fingerprint_path = build_runner
51-
.files()
52-
.host_build_root()
53-
.join(".rustdoc_fingerprint.json");
54-
let write_fingerprint = || -> CargoResult<()> {
55-
paths::write(
56-
&fingerprint_path,
57-
serde_json::to_string(&actual_rustdoc_target_data)?,
58-
)
59-
};
60-
let Ok(rustdoc_data) = paths::read(&fingerprint_path) else {
61-
// If the fingerprint does not exist, do not clear out the doc
62-
// directories. Otherwise this ran into problems where projects
63-
// like bootstrap were creating the doc directory before running
64-
// `cargo doc` in a way that deleting it would break it.
65-
return write_fingerprint();
66-
};
67-
match serde_json::from_str::<RustDocFingerprint>(&rustdoc_data) {
68-
Ok(fingerprint) => {
69-
if fingerprint.rustc_vv == actual_rustdoc_target_data.rustc_vv {
70-
return Ok(());
71-
} else {
72-
tracing::debug!(
73-
"doc fingerprint changed:\noriginal:\n{}\nnew:\n{}",
74-
fingerprint.rustc_vv,
75-
actual_rustdoc_target_data.rustc_vv
76-
);
77-
}
78-
}
79-
Err(e) => {
80-
tracing::debug!("could not deserialize {:?}: {}", fingerprint_path, e);
81-
}
82-
};
83-
// Fingerprint does not match, delete the doc directories and write a new fingerprint.
84-
tracing::debug!(
85-
"fingerprint {:?} mismatch, clearing doc directories",
86-
fingerprint_path
87-
);
88-
build_runner
89-
.bcx
90-
.all_kinds
91-
.iter()
92-
.map(|kind| {
93-
build_runner
94-
.files()
95-
.layout(*kind)
96-
.artifact_dir()
97-
.expect("artifact-dir was not locked")
98-
.doc()
99-
})
100-
.filter(|path| path.exists())
101-
.try_for_each(|path| clean_doc(path))?;
102-
write_fingerprint()?;
50+
check_fingerprint(build_runner, &actual_rustdoc_target_data)?;
10351

10452
Ok(())
10553
}
10654
}
10755

56+
/// Checks rustdoc fingerprint file.
57+
fn check_fingerprint(
58+
build_runner: &BuildRunner<'_, '_>,
59+
actual_rustdoc_target_data: &RustDocFingerprint,
60+
) -> CargoResult<()> {
61+
let fingerprint_path = build_runner
62+
.files()
63+
.host_build_root()
64+
.join(".rustdoc_fingerprint.json");
65+
let write_fingerprint = || -> CargoResult<()> {
66+
paths::write(
67+
&fingerprint_path,
68+
serde_json::to_string(&actual_rustdoc_target_data)?,
69+
)
70+
};
71+
let Ok(rustdoc_data) = paths::read(&fingerprint_path) else {
72+
// If the fingerprint does not exist, do not clear out the doc
73+
// directories. Otherwise this ran into problems where projects
74+
// like bootstrap were creating the doc directory before running
75+
// `cargo doc` in a way that deleting it would break it.
76+
return write_fingerprint();
77+
};
78+
match serde_json::from_str::<RustDocFingerprint>(&rustdoc_data) {
79+
Ok(fingerprint) => {
80+
if fingerprint.rustc_vv == actual_rustdoc_target_data.rustc_vv {
81+
return Ok(());
82+
} else {
83+
tracing::debug!(
84+
"doc fingerprint changed:\noriginal:\n{}\nnew:\n{}",
85+
fingerprint.rustc_vv,
86+
actual_rustdoc_target_data.rustc_vv
87+
);
88+
}
89+
}
90+
Err(e) => {
91+
tracing::debug!("could not deserialize {:?}: {}", fingerprint_path, e);
92+
}
93+
};
94+
// Fingerprint does not match, delete the doc directories and write a new fingerprint.
95+
tracing::debug!(
96+
"fingerprint {:?} mismatch, clearing doc directories",
97+
fingerprint_path
98+
);
99+
build_runner
100+
.bcx
101+
.all_kinds
102+
.iter()
103+
.map(|kind| {
104+
build_runner
105+
.files()
106+
.layout(*kind)
107+
.artifact_dir()
108+
.expect("artifact-dir was not locked")
109+
.doc()
110+
})
111+
.filter(|path| path.exists())
112+
.try_for_each(|path| clean_doc(path))?;
113+
write_fingerprint()?;
114+
115+
Ok(())
116+
}
117+
108118
fn clean_doc(path: &Path) -> CargoResult<()> {
109119
let entries = path
110120
.read_dir()

0 commit comments

Comments
 (0)