From 4e0b6f8f849bdbc9e21ab302eb8c8dcb1e81fe6d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 16 Feb 2024 17:08:49 -0500 Subject: [PATCH] Avoid attempting rename in copy fallback path (#1546) ## Summary This _could_ fix https://github.com/astral-sh/uv/issues/1454, but I'm not sure. I was able to replicate by forcing a bunch of error states. But, in short, if we fail to hardlink on the initial copy due to a file existing, and then fail _again_, we fallback to copying. But if we copy, then the tempfile doesn't exist, and so the `fs_err::rename(&tempfile, &out_path)?;` will fail with "File not found". This PR just ensures that the cases are explicitly mutually exclusive: we only attempt to rename if the hardlink succeeded. --- crates/install-wheel-rs/src/linker.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/install-wheel-rs/src/linker.rs b/crates/install-wheel-rs/src/linker.rs index ada887eb4884..7ea00b9531fd 100644 --- a/crates/install-wheel-rs/src/linker.rs +++ b/crates/install-wheel-rs/src/linker.rs @@ -416,11 +416,12 @@ fn hardlink_wheel_files( // Removing and recreating would lead to race conditions. let tempdir = tempdir_in(&site_packages)?; let tempfile = tempdir.path().join(entry.file_name()); - if fs::hard_link(path, &tempfile).is_err() { + if fs::hard_link(path, &tempfile).is_ok() { + fs_err::rename(&tempfile, &out_path)?; + } else { fs::copy(path, &out_path)?; attempt = Attempt::UseCopyFallback; } - fs_err::rename(&tempfile, &out_path)?; } else { fs::copy(path, &out_path)?; attempt = Attempt::UseCopyFallback;