Skip to content

Commit 9530355

Browse files
committed
refactor: DirtyReason::FreshBuild to represent build from scratch
the only beahvior change in this commit is that source verification is also performed for `DirtyReason::Forced`.
1 parent d3a3c32 commit 9530355

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pub enum DirtyReason {
7777
FsStatusOutdated(FsStatus),
7878
NothingObvious,
7979
Forced,
80+
/// First time to build something.
81+
FreshBuild,
8082
}
8183

8284
trait ShellExt {
@@ -131,6 +133,11 @@ impl fmt::Display for After {
131133
}
132134

133135
impl DirtyReason {
136+
/// Whether a build is dirty because it is a fresh build being kicked off.
137+
pub fn is_fresh_build(&self) -> bool {
138+
matches!(self, DirtyReason::FreshBuild)
139+
}
140+
134141
fn after(old_time: FileTime, new_time: FileTime, what: &'static str) -> After {
135142
After {
136143
old_time,
@@ -257,6 +264,7 @@ impl DirtyReason {
257264
s.dirty_because(unit, "the fingerprint comparison turned up nothing obvious")
258265
}
259266
DirtyReason::Forced => s.dirty_because(unit, "forced"),
267+
DirtyReason::FreshBuild => s.dirty_because(unit, "fresh build"),
260268
}
261269
}
262270
}

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

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -415,42 +415,27 @@ pub fn prepare_target(cx: &mut Context<'_, '_>, unit: &Unit, force: bool) -> Car
415415
// information about failed comparisons to aid in debugging.
416416
let fingerprint = calculate(cx, unit)?;
417417
let mtime_on_use = cx.bcx.config.cli_unstable().mtime_on_use;
418-
let compare = compare_old_fingerprint(unit, &loc, &*fingerprint, mtime_on_use);
418+
let dirty_reason = compare_old_fingerprint(unit, &loc, &*fingerprint, mtime_on_use, force);
419419

420-
// If our comparison failed or reported dirty (e.g., we're going to trigger
421-
// a rebuild of this crate), then we also ensure the source of the crate
422-
// passes all verification checks before we build it.
420+
let Some(dirty_reason) = dirty_reason else {
421+
return Ok(Job::new_fresh());
422+
};
423+
424+
// We're going to rebuild, so ensure the source of the crate passes all
425+
// verification checks before we build it.
423426
//
424427
// The `Source::verify` method is intended to allow sources to execute
425428
// pre-build checks to ensure that the relevant source code is all
426429
// up-to-date and as expected. This is currently used primarily for
427430
// directory sources which will use this hook to perform an integrity check
428431
// on all files in the source to ensure they haven't changed. If they have
429432
// changed then an error is issued.
430-
if compare
431-
.as_ref()
432-
.map(|dirty| dirty.is_some())
433-
.unwrap_or(true)
434-
{
435-
let source_id = unit.pkg.package_id().source_id();
436-
let sources = bcx.packages.sources();
437-
let source = sources
438-
.get(source_id)
439-
.ok_or_else(|| internal("missing package source"))?;
440-
source.verify(unit.pkg.package_id())?;
441-
}
442-
443-
let dirty_reason = match compare {
444-
Ok(None) => {
445-
if force {
446-
Some(DirtyReason::Forced)
447-
} else {
448-
return Ok(Job::new_fresh());
449-
}
450-
}
451-
Ok(reason) => reason,
452-
Err(_) => None,
453-
};
433+
let source_id = unit.pkg.package_id().source_id();
434+
let sources = bcx.packages.sources();
435+
let source = sources
436+
.get(source_id)
437+
.ok_or_else(|| internal("missing package source"))?;
438+
source.verify(unit.pkg.package_id())?;
454439

455440
// Clear out the old fingerprint file if it exists. This protects when
456441
// compilation is interrupted leaving a corrupt file. For example, a
@@ -521,7 +506,7 @@ pub fn prepare_target(cx: &mut Context<'_, '_>, unit: &Unit, force: bool) -> Car
521506
Work::new(move |_| write_fingerprint(&loc, &fingerprint))
522507
};
523508

524-
Ok(Job::new_dirty(write_fingerprint, dirty_reason))
509+
Ok(Job::new_dirty(write_fingerprint, Some(dirty_reason)))
525510
}
526511

527512
/// Dependency edge information for fingerprints. This is generated for each
@@ -1755,10 +1740,15 @@ fn compare_old_fingerprint(
17551740
old_hash_path: &Path,
17561741
new_fingerprint: &Fingerprint,
17571742
mtime_on_use: bool,
1758-
) -> CargoResult<Option<DirtyReason>> {
1743+
forced: bool,
1744+
) -> Option<DirtyReason> {
17591745
let compare = _compare_old_fingerprint(old_hash_path, new_fingerprint, mtime_on_use);
17601746
log_compare(unit, &compare);
1761-
compare
1747+
match compare {
1748+
Ok(None) if forced => Some(DirtyReason::Forced),
1749+
Ok(reason) => reason,
1750+
Err(_) => Some(DirtyReason::FreshBuild),
1751+
}
17621752
}
17631753

17641754
fn _compare_old_fingerprint(

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,9 +1111,11 @@ impl<'cfg> DrainState<'cfg> {
11111111
// being a compiled package.
11121112
Dirty(dirty_reason) => {
11131113
if let Some(reason) = dirty_reason {
1114-
config
1115-
.shell()
1116-
.verbose(|shell| reason.present_to(shell, unit, ws_root))?;
1114+
if !reason.is_fresh_build() {
1115+
config
1116+
.shell()
1117+
.verbose(|shell| reason.present_to(shell, unit, ws_root))?;
1118+
}
11171119
}
11181120

11191121
if unit.mode.is_doc() {

0 commit comments

Comments
 (0)