Use `fs_err` for paths in symlinking errors (#13303)

In #13302, there was an IO error without context. This error seems to be
caused by a symlink error. Switching as symlinking to `fs_err` ensures
these errors will carry context in the future.
This commit is contained in:
konsti 2025-05-05 18:29:27 +02:00 committed by GitHub
parent f557ea3823
commit 3218e364ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 18 additions and 14 deletions

View File

@ -35,4 +35,7 @@ disallowed-methods = [
"std::fs::soft_link", "std::fs::soft_link",
"std::fs::symlink_metadata", "std::fs::symlink_metadata",
"std::fs::write", "std::fs::write",
"std::os::unix::fs::symlink",
"std::os::windows::fs::symlink_dir",
"std::os::windows::fs::symlink_file",
] ]

View File

@ -653,13 +653,13 @@ impl Cache {
let dst = dst.as_ref(); let dst = dst.as_ref();
// Attempt to create the symlink directly. // Attempt to create the symlink directly.
match std::os::unix::fs::symlink(&src, dst) { match fs_err::os::unix::fs::symlink(&src, dst) {
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(err) if err.kind() == io::ErrorKind::AlreadyExists => { Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {
// Create a symlink, using a temporary file to ensure atomicity. // Create a symlink, using a temporary file to ensure atomicity.
let temp_dir = tempfile::tempdir_in(dst.parent().unwrap())?; let temp_dir = tempfile::tempdir_in(dst.parent().unwrap())?;
let temp_file = temp_dir.path().join("link"); let temp_file = temp_dir.path().join("link");
std::os::unix::fs::symlink(&src, &temp_file)?; fs_err::os::unix::fs::symlink(&src, &temp_file)?;
// Move the symlink into the target location. // Move the symlink into the target location.
fs_err::rename(&temp_file, dst)?; fs_err::rename(&temp_file, dst)?;

View File

@ -121,13 +121,13 @@ pub fn replace_symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io:
#[cfg(unix)] #[cfg(unix)]
pub fn replace_symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> { pub fn replace_symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
// Attempt to create the symlink directly. // Attempt to create the symlink directly.
match std::os::unix::fs::symlink(src.as_ref(), dst.as_ref()) { match fs_err::os::unix::fs::symlink(src.as_ref(), dst.as_ref()) {
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => { Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
// Create a symlink, using a temporary file to ensure atomicity. // Create a symlink, using a temporary file to ensure atomicity.
let temp_dir = tempfile::tempdir_in(dst.as_ref().parent().unwrap())?; let temp_dir = tempfile::tempdir_in(dst.as_ref().parent().unwrap())?;
let temp_file = temp_dir.path().join("link"); let temp_file = temp_dir.path().join("link");
std::os::unix::fs::symlink(src, &temp_file)?; fs_err::os::unix::fs::symlink(src, &temp_file)?;
// Move the symlink into the target location. // Move the symlink into the target location.
fs_err::rename(&temp_file, dst.as_ref())?; fs_err::rename(&temp_file, dst.as_ref())?;

View File

@ -493,14 +493,14 @@ fn synchronized_copy(from: &Path, to: &Path, locks: &Locks) -> std::io::Result<(
#[cfg(unix)] #[cfg(unix)]
fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> std::io::Result<()> { fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> std::io::Result<()> {
std::os::unix::fs::symlink(original, link) fs_err::os::unix::fs::symlink(original, link)
} }
#[cfg(windows)] #[cfg(windows)]
fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> std::io::Result<()> { fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> std::io::Result<()> {
if original.as_ref().is_dir() { if original.as_ref().is_dir() {
std::os::windows::fs::symlink_dir(original, link) fs_err::os::windows::fs::symlink_dir(original, link)
} else { } else {
std::os::windows::fs::symlink_file(original, link) fs_err::os::windows::fs::symlink_file(original, link)
} }
} }

View File

@ -761,7 +761,7 @@ impl ManagedPythonDownload {
// to that date do not. // to that date do not.
#[cfg(unix)] #[cfg(unix)]
{ {
match std::os::unix::fs::symlink( match fs_err::os::unix::fs::symlink(
format!("python{}.{}", self.key.major, self.key.minor), format!("python{}.{}", self.key.major, self.key.minor),
extracted.join("bin").join("python"), extracted.join("bin").join("python"),
) { ) {

View File

@ -384,7 +384,7 @@ pub(crate) fn create(
&& interpreter.markers().os_name() == "posix" && interpreter.markers().os_name() == "posix"
&& interpreter.markers().sys_platform() != "darwin" && interpreter.markers().sys_platform() != "darwin"
{ {
match std::os::unix::fs::symlink("lib", location.join("lib64")) { match fs_err::os::unix::fs::symlink("lib", location.join("lib64")) {
Ok(()) => {} Ok(()) => {}
Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {} Err(err) if err.kind() == io::ErrorKind::AlreadyExists => {}
Err(err) => { Err(err) => {

View File

@ -1765,7 +1765,7 @@ fn build_with_symlink() -> Result<()> {
requires = ["hatchling"] requires = ["hatchling"]
build-backend = "hatchling.build" build-backend = "hatchling.build"
"#})?; "#})?;
std::os::unix::fs::symlink( fs_err::os::unix::fs::symlink(
context.temp_dir.child("pyproject.toml.real"), context.temp_dir.child("pyproject.toml.real"),
context.temp_dir.child("pyproject.toml"), context.temp_dir.child("pyproject.toml"),
)?; )?;

View File

@ -3723,7 +3723,7 @@ fn launcher_with_symlink() -> Result<()> {
); );
#[cfg(windows)] #[cfg(windows)]
if let Err(error) = std::os::windows::fs::symlink_file( if let Err(error) = fs_err::os::windows::fs::symlink_file(
context.venv.join("Scripts\\simple_launcher.exe"), context.venv.join("Scripts\\simple_launcher.exe"),
context.temp_dir.join("simple_launcher.exe"), context.temp_dir.join("simple_launcher.exe"),
) { ) {
@ -3736,7 +3736,7 @@ fn launcher_with_symlink() -> Result<()> {
} }
#[cfg(unix)] #[cfg(unix)]
std::os::unix::fs::symlink( fs_err::os::unix::fs::symlink(
context.venv.join("bin/simple_launcher"), context.venv.join("bin/simple_launcher"),
context.temp_dir.join("simple_launcher"), context.temp_dir.join("simple_launcher"),
)?; )?;
@ -8123,7 +8123,7 @@ fn install_relocatable() -> Result<()> {
#[cfg(unix)] #[cfg(unix)]
{ {
let script_symlink_path = context.temp_dir.join("black"); let script_symlink_path = context.temp_dir.join("black");
std::os::unix::fs::symlink(script_path, script_symlink_path.clone())?; fs_err::os::unix::fs::symlink(script_path, script_symlink_path.clone())?;
Command::new(script_symlink_path.as_os_str()) Command::new(script_symlink_path.as_os_str())
.assert() .assert()
.success() .success()

View File

@ -746,7 +746,8 @@ fn python_required_python_major_minor() {
// Symlink it to `python3.11`. // Symlink it to `python3.11`.
fs_err::create_dir_all(context.temp_dir.child("child")).unwrap(); fs_err::create_dir_all(context.temp_dir.child("child")).unwrap();
std::os::unix::fs::symlink(path, context.temp_dir.child("child").join("python3.11")).unwrap(); fs_err::os::unix::fs::symlink(path, context.temp_dir.child("child").join("python3.11"))
.unwrap();
// Find `python3.11`, which is `>=3.11.4`. // Find `python3.11`, which is `>=3.11.4`.
uv_snapshot!(context.filters(), context.python_find().arg(">=3.11.4, <3.12").env(EnvVars::UV_TEST_PYTHON_PATH, context.temp_dir.child("child").path()), @r###" uv_snapshot!(context.filters(), context.python_find().arg(">=3.11.4, <3.12").env(EnvVars::UV_TEST_PYTHON_PATH, context.temp_dir.child("child").path()), @r###"