From c83eb02c62d508c372b58f3e30afe9ff31fdd9be Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 6 Apr 2020 20:50:51 -0700 Subject: [PATCH] Fix freshness when linking is interrupted. --- src/cargo/core/compiler/fingerprint.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cargo/core/compiler/fingerprint.rs b/src/cargo/core/compiler/fingerprint.rs index 4c1674b1493..6bef2fd7596 100644 --- a/src/cargo/core/compiler/fingerprint.rs +++ b/src/cargo/core/compiler/fingerprint.rs @@ -190,6 +190,7 @@ use std::collections::hash_map::{Entry, HashMap}; use std::env; +use std::fs; use std::hash::{self, Hasher}; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; @@ -277,6 +278,15 @@ pub fn prepare_target<'a, 'cfg>( return Ok(Job::new(Work::noop(), Fresh)); } + // Delete the old fingerprint file if it exists. This protects the following scenario: + // 1. The fingerprint already exists, but is dirty due to an mtime failure. + // 2. The build starts. + // 3. The build is interrupted (particularly when linking with gcc), such + // as with Ctrl-C. This leaves the output file in a partially written state. + // 4. The next time Cargo runs, the fingerprint appears fresh, so it + // refuses to build, even though the output file is corrupt. + drop(fs::remove_file(&loc)); + let write_fingerprint = if unit.mode.is_run_custom_build() { // For build scripts the `local` field of the fingerprint may change // while we're executing it. For example it could be in the legacy