-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix(fingerprint): clean doc dirs for only requested targets #16331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdb5046
refactor: move RustDocFingerprint to fingerprint mod
weihanglo 95b8e3c
docs(fingerprint): reorganize rustdoc fingerprint docs
weihanglo 874d482
refactor(fingerprint): move out inner `clean_doc`
weihanglo 21ba213
test(rustdoc): show only one fingerprint for all targets
weihanglo fac0ff7
refactor(fingerprint): extract rustdoc fingerprint check
weihanglo 03abcfb
fix(fingerprint): clean doc dirs for only requested targets
weihanglo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| use std::path::Path; | ||
|
|
||
| use anyhow::Context as _; | ||
| use cargo_util::paths; | ||
| use serde::Deserialize; | ||
| use serde::Serialize; | ||
|
|
||
| use crate::CargoResult; | ||
| use crate::core::compiler::BuildRunner; | ||
| use crate::core::compiler::CompileKind; | ||
|
|
||
| /// Structure used to deal with Rustdoc fingerprinting | ||
| /// | ||
| /// This is important because the `.js`/`.html` & `.css` files | ||
| /// that are generated by Rustc don't have any versioning yet | ||
| /// (see <https://github.com/rust-lang/cargo/issues/8461>). | ||
| /// Therefore, we can end up with weird bugs and behaviours | ||
| /// if we mix different versions of these files. | ||
| /// | ||
| /// We need to make sure that if there were any previous docs already compiled, | ||
| /// they were compiled with the same Rustc version that we're currently using. | ||
| /// Otherwise we must remove the `doc/` folder and compile again forcing a rebuild. | ||
| #[derive(Debug, Serialize, Deserialize)] | ||
| pub struct RustDocFingerprint { | ||
| /// `rustc -vV` verbose version output. | ||
| pub rustc_vv: String, | ||
| } | ||
|
|
||
| impl RustDocFingerprint { | ||
| /// Checks whether the latest version of rustc used to compile this workspace's docs | ||
| /// was the same as the one is currently being used in this `cargo doc` call. | ||
| /// | ||
| /// In case it's not, | ||
| /// it takes care of removing the `<build-dir>/doc/` folder | ||
| /// as well as overwriting the rustdoc fingerprint info. | ||
| /// This is to guarantee that we won't end up with mixed versions of the `js/html/css` files | ||
| /// which `rustdoc` autogenerates without any versioning. | ||
| /// | ||
| /// Each requested target platform maintains its own fingerprint file. | ||
| /// That is, if you run `cargo doc` and then `cargo doc --target wasm32-wasip1`, | ||
| /// you will have two separate fingerprint files: | ||
| /// | ||
| /// * `<build-dir>/.rustdoc_fingerprint.json` for host | ||
| /// * `<build-dir>/wasm32-wasip1/.rustdoc_fingerprint.json` | ||
| pub fn check_rustdoc_fingerprint(build_runner: &BuildRunner<'_, '_>) -> CargoResult<()> { | ||
| if build_runner | ||
| .bcx | ||
| .gctx | ||
| .cli_unstable() | ||
| .skip_rustdoc_fingerprint | ||
| { | ||
| return Ok(()); | ||
| } | ||
| let new_fingerprint = RustDocFingerprint { | ||
| rustc_vv: build_runner.bcx.rustc().verbose_version.clone(), | ||
| }; | ||
|
|
||
| for kind in &build_runner.bcx.build_config.requested_kinds { | ||
| check_fingerprint(build_runner, &new_fingerprint, *kind)?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Checks rustdoc fingerprint file for a given [`CompileKind`]. | ||
| fn check_fingerprint( | ||
| build_runner: &BuildRunner<'_, '_>, | ||
| new_fingerprint: &RustDocFingerprint, | ||
| kind: CompileKind, | ||
| ) -> CargoResult<()> { | ||
| let fingerprint_path = build_runner | ||
| .files() | ||
| .layout(kind) | ||
| .build_dir() | ||
| .root() | ||
| .join(".rustdoc_fingerprint.json"); | ||
|
|
||
| let write_fingerprint = || -> CargoResult<()> { | ||
| paths::write(&fingerprint_path, serde_json::to_string(new_fingerprint)?) | ||
| }; | ||
|
|
||
| let Ok(rustdoc_data) = paths::read(&fingerprint_path) else { | ||
| // If the fingerprint does not exist, do not clear out the doc | ||
| // directories. Otherwise this ran into problems where projects | ||
| // like bootstrap were creating the doc directory before running | ||
| // `cargo doc` in a way that deleting it would break it. | ||
| return write_fingerprint(); | ||
| }; | ||
|
|
||
| match serde_json::from_str::<RustDocFingerprint>(&rustdoc_data) { | ||
| Ok(on_disk_fingerprint) => { | ||
| if on_disk_fingerprint.rustc_vv == new_fingerprint.rustc_vv { | ||
| return Ok(()); | ||
| } else { | ||
| tracing::debug!( | ||
| "doc fingerprint changed:\noriginal:\n{}\nnew:\n{}", | ||
| on_disk_fingerprint.rustc_vv, | ||
| new_fingerprint.rustc_vv | ||
| ); | ||
| } | ||
| } | ||
| Err(e) => { | ||
| tracing::debug!("could not deserialize {:?}: {}", fingerprint_path, e); | ||
| } | ||
| }; | ||
| // Fingerprint does not match, delete the doc directories and write a new fingerprint. | ||
| tracing::debug!( | ||
| "fingerprint {:?} mismatch, clearing doc directories", | ||
| fingerprint_path | ||
| ); | ||
| let doc_dir = build_runner | ||
| .files() | ||
| .layout(kind) | ||
| .artifact_dir() | ||
| .expect("artifact-dir was not locked") | ||
| .doc(); | ||
| if doc_dir.exists() { | ||
| clean_doc(doc_dir)?; | ||
| } | ||
|
|
||
| write_fingerprint()?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn clean_doc(path: &Path) -> CargoResult<()> { | ||
| let entries = path | ||
| .read_dir() | ||
| .with_context(|| format!("failed to read directory `{}`", path.display()))?; | ||
| for entry in entries { | ||
| let entry = entry?; | ||
| // Don't remove hidden files. Rustdoc does not create them, | ||
| // but the user might have. | ||
| if entry | ||
| .file_name() | ||
| .to_str() | ||
| .map_or(false, |name| name.starts_with('.')) | ||
| { | ||
| continue; | ||
| } | ||
| let path = entry.path(); | ||
| if entry.file_type()?.is_dir() { | ||
| paths::remove_dir_all(path)?; | ||
| } else { | ||
| paths::remove_file(path)?; | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.