Skip to content

Commit ba832ac

Browse files
committed
Auto merge of #8185 - ehuss:allow-mtime-failure, r=alexcrichton
Allow failure when setting file mtime. Some filesystems do not allow changing file mtimes (most recent example is a Windows mount in WSL). Since these operations are not critical to Cargo's operation, errors should probably just be ignored. (Currently they are used for mid-build modification protection, and the unstable mtime-on-use). Fixes #8184
2 parents bf1a26d + 5f9d9f2 commit ba832ac

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

Diff for: src/cargo/core/compiler/custom_build.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,9 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
387387
// state informing what variables were discovered via our script as
388388
// well.
389389
paths::write(&output_file, &output.stdout)?;
390-
log::debug!(
391-
"rewinding custom script output mtime {:?} to {}",
392-
output_file,
393-
timestamp
394-
);
395-
filetime::set_file_times(output_file, timestamp, timestamp)?;
390+
// This mtime shift allows Cargo to detect if a source file was
391+
// modified in the middle of the build.
392+
paths::set_file_time_no_err(output_file, timestamp);
396393
paths::write(&err_file, &output.stderr)?;
397394
paths::write(&root_output_file, util::path2bytes(&script_out_dir)?)?;
398395
let parsed_output =

Diff for: src/cargo/core/compiler/fingerprint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ fn compare_old_fingerprint(
15371537
// update the mtime so other cleaners know we used it
15381538
let t = FileTime::from_system_time(SystemTime::now());
15391539
debug!("mtime-on-use forcing {:?} to {}", loc, t);
1540-
filetime::set_file_times(loc, t, t)?;
1540+
paths::set_file_time_no_err(loc, t);
15411541
}
15421542

15431543
let new_hash = new_fingerprint.hash();

Diff for: src/cargo/core/compiler/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,9 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
321321
rustc_dep_info_loc.display()
322322
))
323323
})?;
324-
debug!("rewinding mtime of {:?} to {}", dep_info_loc, timestamp);
325-
filetime::set_file_times(dep_info_loc, timestamp, timestamp)?;
324+
// This mtime shift allows Cargo to detect if a source file was
325+
// modified in the middle of the build.
326+
paths::set_file_time_no_err(dep_info_loc, timestamp);
326327
}
327328

328329
Ok(())

Diff for: src/cargo/util/paths.rs

+18
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,21 @@ fn _link_or_copy(src: &Path, dst: &Path) -> CargoResult<()> {
393393
})?;
394394
Ok(())
395395
}
396+
397+
/// Changes the filesystem mtime (and atime if possible) for the given file.
398+
///
399+
/// This intentionally does not return an error, as this is sometimes not
400+
/// supported on network filesystems. For the current uses in Cargo, this is a
401+
/// "best effort" approach, and errors shouldn't be propagated.
402+
pub fn set_file_time_no_err<P: AsRef<Path>>(path: P, time: FileTime) {
403+
let path = path.as_ref();
404+
match filetime::set_file_times(path, time, time) {
405+
Ok(()) => log::debug!("set file mtime {} to {}", path.display(), time),
406+
Err(e) => log::warn!(
407+
"could not set mtime of {} to {}: {:?}",
408+
path.display(),
409+
time,
410+
e
411+
),
412+
}
413+
}

0 commit comments

Comments
 (0)