From 3d6cc605356f0919ae0bd024aba6cec5a9981ea7 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Sun, 30 Jun 2024 08:21:20 +0000 Subject: [PATCH 1/2] Try renaming the file if removing fails --- src/bootstrap/src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 730d0ae5f3d7c..6d00ff9982d96 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -26,6 +26,7 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::str; use std::sync::OnceLock; +use std::time::SystemTime; use build_helper::ci::{gha, CiEnv}; use build_helper::exit; @@ -1676,7 +1677,14 @@ impl Build { if src == dst { return; } - let _ = fs::remove_file(dst); + if let Err(e) = fs::remove_file(dst) { + if e.kind() != io::ErrorKind::NotFound { + // workaround for https://github.com/rust-lang/rust/issues/127126 + // if removing the file fails, attempt to rename it instead. + let now = t!(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)); + let _ = fs::rename(dst, format!("{}-{}", dst.display(), now.as_nanos())); + } + } let metadata = t!(src.symlink_metadata(), format!("src = {}", src.display())); let mut src = src.to_path_buf(); if metadata.file_type().is_symlink() { From b998cff5582b819910cc8e0f092c15151b930d02 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 2 Jul 2024 06:04:23 +0000 Subject: [PATCH 2/2] Use `cfg!(windows)` --- src/bootstrap/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 6d00ff9982d96..f5941e31e251f 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -1678,7 +1678,7 @@ impl Build { return; } if let Err(e) = fs::remove_file(dst) { - if e.kind() != io::ErrorKind::NotFound { + if cfg!(windows) && e.kind() != io::ErrorKind::NotFound { // workaround for https://github.com/rust-lang/rust/issues/127126 // if removing the file fails, attempt to rename it instead. let now = t!(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH));