Skip to content

Commit

Permalink
Auto merge of #11672 - weihanglo:recompile-verify, r=epage
Browse files Browse the repository at this point in the history
Verify source before recompile
  • Loading branch information
bors committed Feb 3, 2023
2 parents 0b2c38f + 98a7339 commit 47ec597
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 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
70 changes: 70 additions & 0 deletions tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2744,3 +2744,73 @@ fn changing_linker() {
)
.run();
}

#[cargo_test]
fn verify_source_before_recompile() {
Package::new("bar", "0.1.0")
.file("src/lib.rs", "")
.publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = "0.1.0"
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("vendor --respect-source-config").run();
p.change_file(
".cargo/config.toml",
r#"
[source.crates-io]
replace-with = 'vendor'
[source.vendor]
directory = 'vendor'
"#,
);
// Sanity check: vendoring works correctly.
p.cargo("check --verbose")
.with_stderr_contains("[RUNNING] `rustc --crate-name bar [CWD]/vendor/bar/src/lib.rs[..]")
.run();
// Now modify vendored crate.
p.change_file(
"vendor/bar/src/lib.rs",
r#"compile_error!("You shall not pass!");"#,
);
// Should ignore modifed sources without any recompile.
p.cargo("check --verbose")
.with_stderr(
"\
[FRESH] bar v0.1.0
[FRESH] foo v0.1.0 ([CWD])
[FINISHED] dev [..]
",
)
.run();

// Add a `RUSTFLAGS` to trigger a recompile.
//
// Cargo should refuse to build because of checksum verfication failure.
// Cargo shouldn't recompile dependency `bar`.
p.cargo("check --verbose")
.env("RUSTFLAGS", "-W warnings")
.with_status(101)
.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 47ec597

Please sign in to comment.