Skip to content

Commit 5a769f7

Browse files
committed
Address several review comments
1 parent 39e75f3 commit 5a769f7

File tree

5 files changed

+48
-57
lines changed

5 files changed

+48
-57
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ shell-escape.workspace = true
199199
supports-hyperlinks.workspace = true
200200
tar.workspace = true
201201
tempfile.workspace = true
202+
thiserror.workspace = true
202203
time.workspace = true
203204
toml.workspace = true
204205
toml_edit.workspace = true

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ pub enum DirtyReason {
3636
},
3737
ChecksumUseChanged {
3838
old: bool,
39-
new: bool,
4039
},
4140
DepInfoOutputChanged {
4241
old: PathBuf,
@@ -187,7 +186,7 @@ impl DirtyReason {
187186
DirtyReason::PrecalculatedComponentsChanged { .. } => {
188187
s.dirty_because(unit, "the precalculated components changed")
189188
}
190-
DirtyReason::ChecksumUseChanged { old, new: _ } => {
189+
DirtyReason::ChecksumUseChanged { old } => {
191190
if *old {
192191
s.dirty_because(
193192
unit,
@@ -236,6 +235,13 @@ impl DirtyReason {
236235
format_args!("the file `{}` is missing", file.display()),
237236
)
238237
}
238+
StaleItem::UnableToReadFile(file) => {
239+
let file = file.strip_prefix(root).unwrap_or(&file);
240+
s.dirty_because(
241+
unit,
242+
format_args!("the file `{}` could not be read", file.display()),
243+
)
244+
}
239245
StaleItem::FailedToReadMetadata(file) => {
240246
let file = file.strip_prefix(root).unwrap_or(&file);
241247
s.dirty_because(
@@ -265,10 +271,8 @@ impl DirtyReason {
265271
s.dirty_because(
266272
unit,
267273
format_args!(
268-
"the file `{}` has changed (checksum didn't match, {} != {})",
274+
"the file `{}` has changed (checksum didn't match, {stored_checksum} != {new_checksum})",
269275
file.display(),
270-
stored_checksum,
271-
new_checksum,
272276
),
273277
)
274278
}

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

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ use std::collections::hash_map::{Entry, HashMap};
365365

366366
use std::env;
367367
use std::fmt::{self, Display};
368-
use std::fs::File;
368+
use std::fs::{self, File};
369369
use std::hash::{self, Hash, Hasher};
370370
use std::io::{self, Read};
371371
use std::path::{Path, PathBuf};
@@ -763,6 +763,7 @@ enum LocalFingerprint {
763763
#[derive(Clone, Debug)]
764764
pub enum StaleItem {
765765
MissingFile(PathBuf),
766+
UnableToReadFile(PathBuf),
766767
FailedToReadMetadata(PathBuf),
767768
FileSizeChanged {
768769
path: PathBuf,
@@ -831,9 +832,8 @@ impl LocalFingerprint {
831832
// rustc.
832833
LocalFingerprint::CheckDepInfo { dep_info, checksum } => {
833834
let dep_info = target_root.join(dep_info);
834-
let cargo_exe = cargo_exe;
835835
let Some(info) = parse_dep_info(pkg_root, target_root, &dep_info)? else {
836-
return Ok(Some(StaleItem::MissingFile(dep_info.clone())));
836+
return Ok(Some(StaleItem::MissingFile(dep_info)));
837837
};
838838
for (key, previous) in info.env.iter() {
839839
let current = if key == CARGO_ENV {
@@ -860,24 +860,25 @@ impl LocalFingerprint {
860860
current,
861861
}));
862862
}
863-
macro_rules! find_stale_file {
864-
($iter:expr) => {
865-
Ok(find_stale_file(
866-
mtime_cache,
867-
checksum_cache,
868-
&dep_info,
869-
$iter,
870-
*checksum,
871-
))
872-
};
873-
}
874863
if *checksum {
875-
find_stale_file!(info
876-
.files
877-
.iter()
878-
.map(|file| (file.clone(), info.checksum.get(file).cloned())))
864+
Ok(find_stale_file(
865+
mtime_cache,
866+
checksum_cache,
867+
&dep_info,
868+
info.files.iter().map(|file| {
869+
let checksum = info.checksum.get(file.as_path()).cloned();
870+
(file, checksum)
871+
}),
872+
*checksum,
873+
))
879874
} else {
880-
find_stale_file!(info.files.into_iter().map(|p| (p, None)))
875+
Ok(find_stale_file(
876+
mtime_cache,
877+
checksum_cache,
878+
&dep_info,
879+
info.files.into_iter().map(|p| (p, None)),
880+
*checksum,
881+
))
881882
}
882883
}
883884

@@ -1026,10 +1027,7 @@ impl Fingerprint {
10261027
};
10271028
}
10281029
if checksum_a != checksum_b {
1029-
return DirtyReason::ChecksumUseChanged {
1030-
old: *checksum_b,
1031-
new: *checksum_a,
1032-
};
1030+
return DirtyReason::ChecksumUseChanged { old: *checksum_b };
10331031
}
10341032
}
10351033
(
@@ -1356,6 +1354,9 @@ impl StaleItem {
13561354
StaleItem::MissingFile(path) => {
13571355
info!("stale: missing {:?}", path);
13581356
}
1357+
StaleItem::UnableToReadFile(path) => {
1358+
info!("stale: unable to read {:?}", path);
1359+
}
13591360
StaleItem::FailedToReadMetadata(path) => {
13601361
info!("stale: couldn't read metadata {:?}", path);
13611362
}
@@ -2017,12 +2018,12 @@ where
20172018
let path_checksum = match checksum_cache.entry(path_buf) {
20182019
Entry::Occupied(o) => *o.get(),
20192020
Entry::Vacant(v) => {
2021+
let Ok(current_file_len) = fs::metadata(&path).map(|m| m.len()) else {
2022+
return Some(StaleItem::FailedToReadMetadata(path.to_path_buf()));
2023+
};
20202024
let Ok(file) = File::open(path) else {
20212025
return Some(StaleItem::MissingFile(path.to_path_buf()));
20222026
};
2023-
let Ok(current_file_len) = file.metadata().map(|m| m.len()) else {
2024-
return Some(StaleItem::FailedToReadMetadata(path.to_path_buf()));
2025-
};
20262027
if current_file_len != file_len {
20272028
return Some(StaleItem::FileSizeChanged {
20282029
path: path.to_path_buf(),
@@ -2031,7 +2032,7 @@ where
20312032
});
20322033
}
20332034
let Ok(checksum) = Checksum::compute(prior_checksum.algo, file) else {
2034-
return Some(StaleItem::MissingFile(path.to_path_buf()));
2035+
return Some(StaleItem::UnableToReadFile(path.to_path_buf()));
20352036
};
20362037
*v.insert(checksum)
20372038
}
@@ -2620,36 +2621,16 @@ impl Display for Checksum {
26202621
}
26212622
}
26222623

2623-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
2624+
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
26242625
pub enum InvalidChecksum {
2626+
#[error("algorithm portion incorrect, {0}")]
26252627
InvalidChecksumAlgo(InvalidChecksumAlgo),
2628+
#[error("expected {} hexadecimal digits in checksum portion", .0.hash_len() * 2)]
26262629
InvalidChecksum(ChecksumAlgo),
2630+
#[error("expected a string with format \"algorithm=hex_checksum\"")]
26272631
InvalidFormat,
26282632
}
26292633

2630-
impl Display for InvalidChecksum {
2631-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2632-
match self {
2633-
InvalidChecksum::InvalidChecksumAlgo(e) => {
2634-
write!(f, "algorithm portion incorrect, {e}")
2635-
}
2636-
InvalidChecksum::InvalidChecksum(algo) => {
2637-
let expected_len = algo.hash_len() * 2;
2638-
write!(
2639-
f,
2640-
"expected {expected_len} hexadecimal digits in checksum portion"
2641-
)
2642-
}
2643-
InvalidChecksum::InvalidFormat => write!(
2644-
f,
2645-
"expected a string with format \"algorithm=hex_checksum\""
2646-
),
2647-
}
2648-
}
2649-
}
2650-
2651-
impl std::error::Error for InvalidChecksum {}
2652-
26532634
impl From<InvalidChecksumAlgo> for InvalidChecksum {
26542635
fn from(value: InvalidChecksumAlgo) -> Self {
26552636
InvalidChecksum::InvalidChecksumAlgo(value)

src/doc/src/reference/unstable.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,11 @@ otherwise be untracked for change-detection.
518518

519519
The `-Z checksum-freshness` flag will replace the use of file mtimes in cargo's
520520
fingerprints with a file checksum value. This is most useful on systems with a poor
521-
mtime implementation, or in CI/CD. The checksum algorithm can change without notice between cargo versions. Fingerprints are used by cargo to determine when a crate needs to be rebuilt.
521+
mtime implementation, or in CI/CD. The checksum algorithm can change without notice
522+
between cargo versions. Fingerprints are used by cargo to determine when a crate needs to be rebuilt.
523+
524+
For the time being files ingested by build script will continue to use mtimes, even when `checksum-freshness`
525+
is enabled. This is not intended as a long term solution.
522526

523527
## panic-abort-tests
524528
* Tracking Issue: [#67650](https://github.com/rust-lang/rust/issues/67650)

0 commit comments

Comments
 (0)