From 45b9aa8f41a58f00bef4ac624c9d16511fc88f6d Mon Sep 17 00:00:00 2001 From: Ben Beasley Date: Wed, 18 Dec 2024 10:04:54 -0500 Subject: [PATCH] Make the backoff crate dependency Windows-only (#10002) ## Summary Since the `backoff` dependency is only *used* on Windows in practice, this PR would ensure that it is only *compiled* on Windows, too. This is helpful because it appears to be unmaintained upstream, https://github.com/astral-sh/uv/issues/10001, and it would be nice to be able to [drop it from Fedora](https://bugzilla.redhat.com/show_bug.cgi?id=2329729). ## Test Plan ``` $ cargo run python install $ cargo test ``` --- crates/uv-fs/Cargo.toml | 2 +- crates/uv-fs/src/lib.rs | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/uv-fs/Cargo.toml b/crates/uv-fs/Cargo.toml index 1f5627f6f..a77c71e10 100644 --- a/crates/uv-fs/Cargo.toml +++ b/crates/uv-fs/Cargo.toml @@ -17,7 +17,6 @@ workspace = true [dependencies] -backoff = { workspace = true } cachedir = { workspace = true } dunce = { workspace = true } either = { workspace = true } @@ -39,6 +38,7 @@ winsafe = { workspace = true } rustix = { workspace = true } [target.'cfg(windows)'.dependencies] +backoff = { workspace = true } junction = { workspace = true } [features] diff --git a/crates/uv-fs/src/lib.rs b/crates/uv-fs/src/lib.rs index b13486ec2..a906c10b6 100644 --- a/crates/uv-fs/src/lib.rs +++ b/crates/uv-fs/src/lib.rs @@ -216,6 +216,7 @@ pub fn copy_atomic_sync(from: impl AsRef, to: impl AsRef) -> std::io Ok(()) } +#[cfg(windows)] fn backoff_file_move() -> backoff::ExponentialBackoff { backoff::ExponentialBackoffBuilder::default() .with_initial_interval(std::time::Duration::from_millis(10)) @@ -229,7 +230,8 @@ pub async fn rename_with_retry( from: impl AsRef, to: impl AsRef, ) -> Result<(), std::io::Error> { - if cfg!(windows) { + #[cfg(windows)] + { // On Windows, antivirus software can lock files temporarily, making them inaccessible. // This is most common for DLLs, and the common suggestion is to retry the operation with // some backoff. @@ -255,7 +257,9 @@ pub async fn rename_with_retry( } }) .await - } else { + } + #[cfg(not(windows))] + { fs_err::tokio::rename(from, to).await } } @@ -265,7 +269,8 @@ pub fn rename_with_retry_sync( from: impl AsRef, to: impl AsRef, ) -> Result<(), std::io::Error> { - if cfg!(windows) { + #[cfg(windows)] + { // On Windows, antivirus software can lock files temporarily, making them inaccessible. // This is most common for DLLs, and the common suggestion is to retry the operation with // some backoff. @@ -299,7 +304,9 @@ pub fn rename_with_retry_sync( ), ) }) - } else { + } + #[cfg(not(windows))] + { fs_err::rename(from, to) } } @@ -309,7 +316,8 @@ pub fn persist_with_retry_sync( from: NamedTempFile, to: impl AsRef, ) -> Result<(), std::io::Error> { - if cfg!(windows) { + #[cfg(windows)] + { // On Windows, antivirus software can lock files temporarily, making them inaccessible. // This is most common for DLLs, and the common suggestion is to retry the operation with // some backoff. @@ -364,7 +372,9 @@ pub fn persist_with_retry_sync( format!("{err:?}"), )), } - } else { + } + #[cfg(not(windows))] + { fs_err::rename(from, to) } }