Skip to content

Commit d2572a2

Browse files
committed
Address latest reivew suggestions
- Instead of `fs` we use the `utils::paths` functions to interact with the filesystem. - The doc fingerprint is now stored under `target/` instead of `target/doc/`. - The code in `compile` has been reduced to a single function call.
1 parent 8ddc56f commit d2572a2

File tree

4 files changed

+59
-125
lines changed

4 files changed

+59
-125
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
use crate::core::compiler::{BuildOutput, CompileKind, CompileMode, CompileTarget, CrateType};
1+
use crate::core::compiler::{
2+
BuildOutput, CompileKind, CompileMode, CompileTarget, Context, CrateType,
3+
};
24
use crate::core::{Dependency, Target, TargetKind, Workspace};
35
use crate::util::config::{Config, StringList, TargetConfig};
4-
use crate::util::{CargoResult, CargoResultExt, ProcessBuilder, Rustc};
6+
use crate::util::{paths, CargoResult, CargoResultExt, ProcessBuilder, Rustc};
57
use cargo_platform::{Cfg, CfgExpr};
68
use serde::{Deserialize, Serialize};
79
use std::cell::RefCell;
810
use std::collections::hash_map::{Entry, HashMap};
911
use std::env;
10-
use std::fs::File;
11-
use std::path::{Path, PathBuf};
12+
use std::path::PathBuf;
1213
use std::str::{self, FromStr};
1314

1415
/// Information about the platform target gleaned from querying rustc.
@@ -754,65 +755,53 @@ impl RustcTargetData {
754755
/// Structure used to deal with Rustdoc fingerprinting
755756
#[derive(Debug, Serialize, Deserialize)]
756757
pub struct RustDocFingerprint {
757-
rustc_verbose_version: String,
758+
rustc_vv: String,
758759
}
759760

760761
impl RustDocFingerprint {
761-
/// Returns the version contained by this `RustDocFingerprint` instance.
762-
fn version(&self) -> &str {
763-
self.rustc_verbose_version.as_str()
764-
}
765-
766-
/// Given the `doc` dir, returns the path to the rustdoc fingerprint file.
767-
fn path_to_fingerprint(doc_dir: &Path) -> PathBuf {
768-
doc_dir.to_path_buf().join(".rustdoc_fingerprint.json")
762+
/// Read the `RustDocFingerprint` info from the fingerprint file.
763+
fn read<'a, 'cfg>(cx: &Context<'a, 'cfg>) -> CargoResult<Self> {
764+
let rustdoc_data = paths::read(
765+
&cx.files()
766+
.host_root()
767+
.join(".rustdoc_fingerprint")
768+
.with_extension("json"),
769+
)?;
770+
serde_json::from_str(&rustdoc_data).map_err(|e| anyhow::anyhow!("{:?}", e))
769771
}
770772

771773
/// Write the `RustDocFingerprint` info into the fingerprint file.
772-
pub fn write(&self, doc_dir: &Path) -> std::io::Result<()> {
773-
crate::util::paths::create_dir_all(doc_dir)
774-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", e)))?;
775-
let rustdoc_fingerprint_file =
776-
File::create(RustDocFingerprint::path_to_fingerprint(doc_dir))?;
777-
// We write the actual `Rustc` version to it so that we just need to compile it straight
778-
// since there's no `doc/` folder to remove.
779-
serde_json::to_writer(&rustdoc_fingerprint_file, &self)
780-
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("{:?}", e)))
774+
fn write<'a, 'cfg>(&self, cx: &Context<'a, 'cfg>) -> CargoResult<()> {
775+
paths::write(
776+
&cx.files()
777+
.host_root()
778+
.join(".rustdoc_fingerprint.json")
779+
.with_extension("json"),
780+
serde_json::to_string(&self)?.as_bytes(),
781+
)
781782
}
782783

783784
/// This function checks whether the latest version of `Rustc` used to compile this
784785
/// `Workspace`'s docs was the same as the one is currently being used in this `cargo doc`
785-
/// call, returning `(Self, bool)` where the `bool` says if the `doc` folder was removed.
786+
/// call.
786787
///
787788
/// In case it's not, it takes care of removig the `doc/` folder as well as overwriting
788789
/// the rustdoc fingerprint info in order to guarantee that we won't end up with mixed
789790
/// versions of the `js/html/css` files that `Rustc` autogenerates which do not have
790791
/// any versioning.
791-
pub fn check_rustdoc_fingerprint(
792-
doc_dir: &Path,
793-
rustc_verbose_ver: &str,
794-
) -> anyhow::Result<(Self, bool)> {
792+
pub fn check_rustdoc_fingerprint<'a, 'cfg>(cx: &Context<'a, 'cfg>) -> CargoResult<()> {
795793
let actual_rustdoc_target_data = RustDocFingerprint {
796-
rustc_verbose_version: rustc_verbose_ver.to_string(),
794+
rustc_vv: cx.bcx.rustc().verbose_version.clone(),
797795
};
798-
799-
// Check wether `.rustdoc_fingerprint.json exists
800-
match std::fs::read_to_string(Self::path_to_fingerprint(doc_dir)) {
801-
Ok(rustdoc_data) => {
802-
let rustdoc_fingerprint: Self = match serde_json::from_str(&rustdoc_data) {
803-
Ok(res) => res,
804-
Err(_) => {
805-
crate::util::paths::remove_dir_all(doc_dir)?;
806-
return Ok((actual_rustdoc_target_data, true));
807-
}
808-
};
796+
let doc_dir = cx.files().host_root().join("doc");
797+
// Check wether `.rustdoc_fingerprint.json` exists
798+
match Self::read(cx) {
799+
Ok(fingerprint) => {
809800
// Check if rustc_version matches the one we just used. Otherways,
810801
// remove the `doc` folder to trigger a re-compilation of the docs.
811-
if rustdoc_fingerprint.version() != actual_rustdoc_target_data.version() {
812-
crate::util::paths::remove_dir_all(doc_dir)?;
813-
Ok((actual_rustdoc_target_data, true))
814-
} else {
815-
Ok((actual_rustdoc_target_data, false))
802+
if fingerprint.rustc_vv != actual_rustdoc_target_data.rustc_vv {
803+
paths::remove_dir_all(&doc_dir)?;
804+
actual_rustdoc_target_data.write(cx)?
816805
}
817806
}
818807
// If the file does not exist, then we cannot assume that the docs were compiled
@@ -821,9 +810,10 @@ impl RustDocFingerprint {
821810
// exists neither, we simply do nothing and continue.
822811
Err(_) => {
823812
// We don't care if this suceeds as explained above.
824-
let _ = crate::util::paths::remove_dir_all(doc_dir);
825-
Ok((actual_rustdoc_target_data, true))
813+
let _ = paths::remove_dir_all(doc_dir);
814+
actual_rustdoc_target_data.write(cx)?
826815
}
827816
}
817+
Ok(())
828818
}
829819
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,10 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
140140
// This is important because the `.js`/`.html` & `.css` files that are generated by Rustc don't have
141141
// any versioning (See https://github.com/rust-lang/cargo/issues/8461).
142142
// Therefore, we can end up with weird bugs and behaviours if we mix different
143-
// compiler versions of these files.
144-
let doc_dir = self.files().host_root().join("doc");
145-
let (fingerprint, doc_dir_removed) = RustDocFingerprint::check_rustdoc_fingerprint(
146-
&doc_dir,
147-
self.bcx.rustc().verbose_version.as_str(),
148-
)?;
149-
if doc_dir_removed {
150-
fingerprint.write(&doc_dir)?
151-
};
143+
// versions of these files.
144+
if self.bcx.build_config.mode.is_doc() {
145+
RustDocFingerprint::check_rustdoc_fingerprint(&self)?
146+
}
152147

153148
for unit in &self.bcx.roots {
154149
// Build up a list of pending jobs, each of which represent

tests/testsuite/clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ fn assert_all_clean(build_dir: &Path) {
432432
}) {
433433
let entry = entry.unwrap();
434434
let path = entry.path();
435-
if let ".rustc_info.json" | ".cargo-lock" | "CACHEDIR.TAG" | ".rustdoc_fingerprint.json" =
435+
if let ".rustc_info.json" | ".cargo-lock" | "CACHEDIR.TAG" =
436436
path.file_name().unwrap().to_str().unwrap()
437437
{
438438
continue;

tests/testsuite/doc.rs

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,14 +1644,14 @@ fn crate_versions_flag_is_overridden() {
16441644
fn doc_fingerprint_versioning_consistent() {
16451645
#[derive(Debug, Serialize, Deserialize)]
16461646
pub struct RustDocFingerprint {
1647-
pub rustc_verbose_version: String,
1647+
rustc_vv: String,
16481648
}
16491649

16501650
// Test that using different Rustc versions forces a
16511651
// doc re-compilation producing new html, css & js files.
16521652

16531653
// Random rustc verbose version
1654-
let stable_rustc_verbose_version = format!(
1654+
let old_rustc_verbose_version = format!(
16551655
"\
16561656
rustc 1.41.1 (f3e1a954d 2020-02-24)
16571657
binary: rustc
@@ -1680,28 +1680,18 @@ LLVM version: 9.0
16801680

16811681
dummy_project.cargo("doc").run();
16821682

1683-
let fingerprint: RustDocFingerprint = serde_json::from_str(
1684-
dummy_project
1685-
.read_file(
1686-
std::path::PathBuf::from("target")
1687-
.join("doc")
1688-
.join(".rustdoc_fingerprint.json")
1689-
.to_str()
1690-
.expect("Malformed path"),
1691-
)
1692-
.as_str(),
1693-
)
1694-
.expect("JSON Serde fail");
1683+
let fingerprint: RustDocFingerprint =
1684+
serde_json::from_str(&dummy_project.read_file("target/.rustdoc_fingerprint.json"))
1685+
.expect("JSON Serde fail");
16951686

16961687
// Check that the fingerprint contains the actual rustc version
16971688
// which has been used to compile the docs.
16981689
let output = std::process::Command::new("rustc")
16991690
.arg("-vV")
1700-
.stdout(std::process::Stdio::piped())
17011691
.output()
17021692
.expect("Failed to get actual rustc verbose version");
17031693
assert_eq!(
1704-
fingerprint.rustc_verbose_version,
1694+
fingerprint.rustc_vv,
17051695
(String::from_utf8_lossy(&output.stdout).as_ref())
17061696
);
17071697

@@ -1710,74 +1700,33 @@ LLVM version: 9.0
17101700
// So we will remove it and create a new fingerprint with an old rustc version
17111701
// inside it. We will also place a bogus file inside of the `doc/` folder to ensure
17121702
// it gets removed as we expect on the next doc compilation.
1713-
fs::remove_file(
1714-
dummy_project
1715-
.root()
1716-
.join("target")
1717-
.join("doc")
1718-
.join(".rustdoc_fingerprint.json"),
1719-
)
1720-
.expect("Failed to read fingerprint on tests");
1721-
1722-
fs::write(
1723-
dummy_project
1724-
.root()
1725-
.join("target")
1726-
.join("doc")
1727-
.join(".rustdoc_fingerprint.json"),
1728-
stable_rustc_verbose_version,
1729-
)
1730-
.expect("Error writing old rustc version");
1703+
dummy_project.change_file(
1704+
"target/.rustdoc_fingerprint.json",
1705+
&old_rustc_verbose_version,
1706+
);
17311707

17321708
fs::write(
1733-
dummy_project
1734-
.root()
1735-
.join("target")
1736-
.join("doc")
1737-
.join("bogus_file"),
1709+
dummy_project.build_dir().join("doc/bogus_file"),
17381710
String::from("This is a bogus file and should be removed!"),
17391711
)
17401712
.expect("Error writing test bogus file");
17411713

17421714
// Now if we trigger another compilation, since the fingerprint contains an old version
17431715
// of rustc, cargo should remove the entire `/doc` folder (including the fingerprint)
17441716
// and generating another one with the actual version.
1745-
// It should also remove the bogus file we created above
1746-
dummy_project.change_file("src/lib.rs", "//! These are the docs 2!");
1717+
// It should also remove the bogus file we created above.
17471718
dummy_project.cargo("doc").run();
17481719

1749-
assert!(fs::File::open(
1750-
dummy_project
1751-
.root()
1752-
.join("target")
1753-
.join("doc")
1754-
.join("bogus_file")
1755-
)
1756-
.is_err());
1757-
1758-
let fingerprint: RustDocFingerprint = serde_json::from_str(
1759-
dummy_project
1760-
.read_file(
1761-
std::path::PathBuf::from("target")
1762-
.join("doc")
1763-
.join(".rustdoc_fingerprint.json")
1764-
.to_str()
1765-
.expect("Malformed path"),
1766-
)
1767-
.as_str(),
1768-
)
1769-
.expect("JSON Serde fail");
1720+
assert!(!dummy_project.build_dir().join("doc/bogus_file").exists());
1721+
1722+
let fingerprint: RustDocFingerprint =
1723+
serde_json::from_str(&dummy_project.read_file("target/.rustdoc_fingerprint.json"))
1724+
.expect("JSON Serde fail");
17701725

17711726
// Check that the fingerprint contains the actual rustc version
17721727
// which has been used to compile the docs.
1773-
let output = std::process::Command::new("rustc")
1774-
.arg("-vV")
1775-
.stdout(std::process::Stdio::piped())
1776-
.output()
1777-
.expect("Failed to get actual rustc verbose version");
1778-
17791728
assert_eq!(
1780-
fingerprint.rustc_verbose_version,
1729+
fingerprint.rustc_vv,
17811730
(String::from_utf8_lossy(&output.stdout).as_ref())
17821731
);
17831732
}

0 commit comments

Comments
 (0)