Skip to content

Commit

Permalink
fix: verify source before recompile
Browse files Browse the repository at this point in the history
This fixes a regression introduced by rust-lang#11407,
which Cargo should always verify a source before it recompiles.
  • Loading branch information
weihanglo committed Feb 3, 2023
1 parent 419a56f commit 98a7339
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/cargo/core/compiler/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,21 @@ pub fn prepare_target(cx: &mut Context<'_, '_>, unit: &Unit, force: bool) -> Car
let compare = compare_old_fingerprint(&loc, &*fingerprint, mtime_on_use);
log_compare(unit, &compare);

// If our comparison failed (e.g., we're going to trigger a rebuild of this
// crate), then we also ensure the source of the crate passes all
// verification checks before we build it.
// If our comparison failed or reported dirty (e.g., we're going to trigger
// a rebuild of this crate), then we also ensure the source of the crate
// passes all verification checks before we build it.
//
// The `Source::verify` method is intended to allow sources to execute
// pre-build checks to ensure that the relevant source code is all
// up-to-date and as expected. This is currently used primarily for
// directory sources which will use this hook to perform an integrity check
// on all files in the source to ensure they haven't changed. If they have
// changed then an error is issued.
if compare.is_err() {
if compare
.as_ref()
.map(|dirty| dirty.is_some())
.unwrap_or(true)
{
let source_id = unit.pkg.package_id().source_id();
let sources = bcx.packages.sources();
let source = sources
Expand Down
11 changes: 9 additions & 2 deletions tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2800,10 +2800,17 @@ fn verify_source_before_recompile() {
//
// Cargo should refuse to build because of checksum verfication failure.
// Cargo shouldn't recompile dependency `bar`.
// TODO: fix this wrong behaviour
p.cargo("check --verbose")
.env("RUSTFLAGS", "-W warnings")
.with_status(101)
.with_stderr_contains("[..]error: You shall not pass![..]")
.with_stderr(
"\
error: the listed checksum of `[CWD]/vendor/bar/src/lib.rs` has changed:
expected: [..]
actual: [..]
directory sources are not [..]
",
)
.run();
}

0 comments on commit 98a7339

Please sign in to comment.