Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify source before recompile #11672

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}