Skip to content
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
19 changes: 19 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,25 @@ fn rustc(
paths::set_file_time_no_err(dep_info_loc, timestamp);
}

// This mtime shift for .rmeta is a workaround as rustc incremental build
// since rust-lang/rust#114669 (1.90.0) skips unnecessary rmeta generation.
//
// The situation is like this:
//
// 1. When build script execution's external dependendies
// (rerun-if-changed, rerun-if-env-changed) got updated,
// the execution unit reran and got a newer mtime.
// 2. rustc type-checked the associated crate, though with incremental
// compilation, no rmeta regeneration. Its `.rmeta` stays old.
// 3. Run `cargo check` again. Cargo found build script execution had
// a new mtime than existing crate rmeta, so re-checking the crate.
// However the check is a no-op (input has no change), so stuck.
if mode.is_check() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make sense to do this for all modes?

I guess eventually we have to clarify the contract between rustc and cargo here: is rustc allowed to just do nothing if an output file is up-to-date, or is it required to always at least update the mtime?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally don't think rustc should care and update mtime.

keeping this patch small and suitable for backport, but yeah to be more robust and future proof, we probably want to do it for all modes, or at least adding tests to capture possible regression in a full cargo build.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other build systems will probably get confused by the exact same issue, so perhaps this should actually be changed in rustc itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, though OTOH a build system also shouldn't get into an internally inconsistent state (like what we saw in #16104) just because a tool it invokes has slightly odd behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make can't have any other behavior than continuous rebuilding as it doesn't store any information and only uses the mtime stored in the filesystem as would ninja I believe (which together with make are the main backends of cmake and which is the only backend of meson that supports Linux). Basically any mtime based build system (which is the fast majority of the build systems that are used) is broken when a build doesn't update the mtime of output files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo not updating on no difference in output is great in terms of early cut off. It is just getting all of the semantics right that is the issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not discuss this in a closed PR... I have opened rust-lang/rust#148948.

for output in outputs.iter() {
paths::set_file_time_no_err(&output.path, timestamp);
}
}

Ok(())
}));

Expand Down
60 changes: 60 additions & 0 deletions tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3184,3 +3184,63 @@ fn use_mtime_cache_in_cargo_home() {
"#]])
.run();
}

#[cargo_test]
fn incremental_build_script_execution_got_new_mtime_and_cargo_check() {
// See https://github.com/rust-lang/cargo/issues/16104
let p = project()
.file("src/lib.rs", "")
.file("touch-me", "")
.file(
"build.rs",
r#"fn main() { println!("cargo::rerun-if-changed=touch-me") }"#,
)
.build();

p.cargo("check")
.env("CARGO_INCREMENTAL", "1")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

if is_coarse_mtime() {
sleep_ms(1000);
}

p.change_file("touch-me", "oops");

// The first one is expected to rerun build script
p.cargo("check -v")
.env("CARGO_INCREMENTAL", "1")
.with_stderr_data(str![[r#"
[DIRTY] foo v0.0.1 ([ROOT]/foo): the file `touch-me` has changed ([TIME_DIFF_AFTER_LAST_BUILD])
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
[RUNNING] `rustc --crate-name foo [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

// subsequent cargo check gets stuck...
p.cargo("check -v")
.env("CARGO_INCREMENTAL", "1")
.with_stderr_data(str![[r#"
[FRESH] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

p.cargo("check -v")
.env("CARGO_INCREMENTAL", "1")
.with_stderr_data(str![[r#"
[FRESH] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}
60 changes: 60 additions & 0 deletions tests/testsuite/freshness_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2963,3 +2963,63 @@ fn use_checksum_cache_in_cargo_home() {
"#]])
.run();
}

#[cargo_test(nightly, reason = "requires -Zchecksum-hash-algorithm")]
fn incremental_build_script_execution_got_new_mtime_and_cargo_check() {
// See https://github.com/rust-lang/cargo/issues/16104
let p = project()
.file("src/lib.rs", "")
.file("touch-me", "")
.file(
"build.rs",
r#"fn main() { println!("cargo::rerun-if-changed=touch-me") }"#,
)
.build();

p.cargo("check -Zchecksum-freshness")
.masquerade_as_nightly_cargo(&["checksum-freshness"])
.env("CARGO_INCREMENTAL", "1")
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

p.change_file("touch-me", "oops");

// The first one is expected to rerun build script
p.cargo("check -Zchecksum-freshness -v")
.masquerade_as_nightly_cargo(&["checksum-freshness"])
.env("CARGO_INCREMENTAL", "1")
.with_stderr_data(str![[r#"
[DIRTY] foo v0.0.1 ([ROOT]/foo): the file `touch-me` has changed ([TIME_DIFF_AFTER_LAST_BUILD])
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
[RUNNING] `rustc --crate-name foo [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

// subsequent cargo check gets stuck...
p.cargo("check -Zchecksum-freshness -v")
.masquerade_as_nightly_cargo(&["checksum-freshness"])
.env("CARGO_INCREMENTAL", "1")
.with_stderr_data(str![[r#"
[FRESH] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

p.cargo("check -Zchecksum-freshness -v")
.masquerade_as_nightly_cargo(&["checksum-freshness"])
.env("CARGO_INCREMENTAL", "1")
.with_stderr_data(str![[r#"
[FRESH] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();
}