Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(diff): compare by whole seconds when the library is dated on one, as on drvfs (#1296)

jakalijey73697690b001
diff --git a/CHANGELOG.md b/CHANGELOG.md
index af3b5862..861cc30d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
## Unreleased
+- `diff` no longer calls a library stale on a file system that keeps only whole seconds (a crate
+ on a Windows drive under WSL): a library dated on a whole second is compared with the sources
+ by seconds, so the `Cargo.lock` its build wrote earlier in that second is not newer (#1293 by
+ @jakalijey).
- Java: a string with a lone UTF-16 surrogate (such as `substring` cutting an emoji in half) is
now written as `{"$utf16": [code units]}`, as the other languages do, and the driver accepts
that marker as a `String` argument. Before, the driver wrote `\ud83d` on its own, which a strict
diff --git a/src/harness.rs b/src/harness.rs
index 4b62d006..25a5a659 100644
--- a/src/harness.rs
+++ b/src/harness.rs
@@ -1183,10 +1183,16 @@ fn stale_build(crate_dir: &Path, manifest: &Manifest) -> Option<PathBuf> {
/// True when the crate's `Cargo.toml`, `Cargo.lock`, `build.rs` or any `.rs` file under `src/`,
/// or the same of a crate it depends on by `path` (a split port, the crates a front door wires
/// in), is newer than `built`.
+///
+/// A library dated on a whole second, as a file system that keeps only seconds dates it (drvfs
+/// under WSL), is compared by whole seconds: the `Cargo.lock` its own build wrote a moment before
+/// the link can carry a fraction of the same second and would otherwise always look newer.
pub(crate) fn sources_newer_than(crate_dir: &Path, built: &Path) -> bool {
let Ok(built_at) = std::fs::metadata(built).and_then(|m| m.modified()) else {
return false;
};
+ let since_epoch = |t: std::time::SystemTime| t.duration_since(std::time::UNIX_EPOCH).ok();
+ let whole_seconds = since_epoch(built_at).is_some_and(|d| d.subsec_nanos() == 0);
let mut sources = Vec::new();
crate_sources(
crate_dir,
@@ -1196,7 +1202,12 @@ pub(crate) fn sources_newer_than(crate_dir: &Path, built: &Path) -> bool {
sources
.iter()
.filter_map(|p| std::fs::metadata(p).and_then(|m| m.modified()).ok())
- .any(|changed| changed > built_at)
+ .any(
+ |changed| match (whole_seconds, since_epoch(changed), since_epoch(built_at)) {
+ (true, Some(changed), Some(built)) => changed.as_secs() > built.as_secs(),
+ _ => changed > built_at,
+ },
+ )
}
/// The files a crate is built from: its manifest, lockfile, build script and sources, then those
@@ -4506,6 +4517,54 @@ done"#;
);
}
+ /// On a file system that keeps whole seconds, the library is dated on the second; a
+ /// `Cargo.lock` written earlier in that second, with a fraction, is not newer, while a source
+ /// saved in a later second is. A library with a fraction is compared exactly.
+ #[test]
+ fn a_library_dated_on_a_whole_second_is_compared_by_seconds() {
+ let dir = crate::app::temp_dir("whole-second").unwrap();
+ std::fs::create_dir_all(dir.join("src")).unwrap();
+ std::fs::write(
+ dir.join("Cargo.toml"),
+ "[package]
+name = \"x\"
+",
+ )
+ .unwrap();
+ std::fs::write(dir.join("Cargo.lock"), "").unwrap();
+ std::fs::write(
+ dir.join("src/port.rs"),
+ "pub fn f() {}
+",
+ )
+ .unwrap();
+ std::fs::write(dir.join("built.so"), b"lib").unwrap();
+ let second = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_800_000_000);
+ let at = |path: &str, millis: u64| {
+ std::fs::File::options()
+ .write(true)
+ .open(dir.join(path))
+ .unwrap()
+ .set_modified(second + std::time::Duration::from_millis(millis))
+ .unwrap();
+ };
+ let built = dir.join("built.so");
+ at("Cargo.toml", 0);
+ at("src/port.rs", 0);
+ at("built.so", 0);
+ at("Cargo.lock", 353);
+ let same_second = super::sources_newer_than(&dir, &built);
+ at("src/port.rs", 1_200);
+ let next_second = super::sources_newer_than(&dir, &built);
+ at("src/port.rs", 0);
+ at("built.so", 200);
+ let fraction = super::sources_newer_than(&dir, &built);
+ std::fs::remove_dir_all(&dir).unwrap();
+ assert!(!same_second, "Cargo.lock in the library's own second");
+ assert!(next_second, "a source saved a second later");
+ assert!(fraction, "a library with a fraction is compared exactly");
+ }
+
#[test]
fn a_diff_is_stamped_with_the_port_it_started_on() {
let dir = crate::app::temp_dir("diff-stamp-start").unwrap();