Skip to content

Commit 9faaba0

Browse files
borsweihanglo
authored andcommitted
Auto merge of #11672 - weihanglo:recompile-verify, r=epage
Verify source before recompile
1 parent 62ff2eb commit 9faaba0

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

src/cargo/core/compiler/fingerprint.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,17 +379,21 @@ pub fn prepare_target(cx: &mut Context<'_, '_>, unit: &Unit, force: bool) -> Car
379379
let compare = compare_old_fingerprint(&loc, &*fingerprint, mtime_on_use);
380380
log_compare(unit, &compare);
381381

382-
// If our comparison failed (e.g., we're going to trigger a rebuild of this
383-
// crate), then we also ensure the source of the crate passes all
384-
// verification checks before we build it.
382+
// If our comparison failed or reported dirty (e.g., we're going to trigger
383+
// a rebuild of this crate), then we also ensure the source of the crate
384+
// passes all verification checks before we build it.
385385
//
386386
// The `Source::verify` method is intended to allow sources to execute
387387
// pre-build checks to ensure that the relevant source code is all
388388
// up-to-date and as expected. This is currently used primarily for
389389
// directory sources which will use this hook to perform an integrity check
390390
// on all files in the source to ensure they haven't changed. If they have
391391
// changed then an error is issued.
392-
if compare.is_err() {
392+
if compare
393+
.as_ref()
394+
.map(|dirty| dirty.is_some())
395+
.unwrap_or(true)
396+
{
393397
let source_id = unit.pkg.package_id().source_id();
394398
let sources = bcx.packages.sources();
395399
let source = sources

tests/testsuite/freshness.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,3 +2744,73 @@ fn changing_linker() {
27442744
)
27452745
.run();
27462746
}
2747+
2748+
#[cargo_test]
2749+
fn verify_source_before_recompile() {
2750+
Package::new("bar", "0.1.0")
2751+
.file("src/lib.rs", "")
2752+
.publish();
2753+
let p = project()
2754+
.file(
2755+
"Cargo.toml",
2756+
r#"
2757+
[package]
2758+
name = "foo"
2759+
version = "0.1.0"
2760+
2761+
[dependencies]
2762+
bar = "0.1.0"
2763+
"#,
2764+
)
2765+
.file("src/lib.rs", "")
2766+
.build();
2767+
2768+
p.cargo("vendor --respect-source-config").run();
2769+
p.change_file(
2770+
".cargo/config.toml",
2771+
r#"
2772+
[source.crates-io]
2773+
replace-with = 'vendor'
2774+
2775+
[source.vendor]
2776+
directory = 'vendor'
2777+
"#,
2778+
);
2779+
// Sanity check: vendoring works correctly.
2780+
p.cargo("check --verbose")
2781+
.with_stderr_contains("[RUNNING] `rustc --crate-name bar [CWD]/vendor/bar/src/lib.rs[..]")
2782+
.run();
2783+
// Now modify vendored crate.
2784+
p.change_file(
2785+
"vendor/bar/src/lib.rs",
2786+
r#"compile_error!("You shall not pass!");"#,
2787+
);
2788+
// Should ignore modifed sources without any recompile.
2789+
p.cargo("check --verbose")
2790+
.with_stderr(
2791+
"\
2792+
[FRESH] bar v0.1.0
2793+
[FRESH] foo v0.1.0 ([CWD])
2794+
[FINISHED] dev [..]
2795+
",
2796+
)
2797+
.run();
2798+
2799+
// Add a `RUSTFLAGS` to trigger a recompile.
2800+
//
2801+
// Cargo should refuse to build because of checksum verfication failure.
2802+
// Cargo shouldn't recompile dependency `bar`.
2803+
p.cargo("check --verbose")
2804+
.env("RUSTFLAGS", "-W warnings")
2805+
.with_status(101)
2806+
.with_stderr(
2807+
"\
2808+
error: the listed checksum of `[CWD]/vendor/bar/src/lib.rs` has changed:
2809+
expected: [..]
2810+
actual: [..]
2811+
2812+
directory sources are not [..]
2813+
",
2814+
)
2815+
.run();
2816+
}

0 commit comments

Comments
 (0)