Ensure `python pin --global` creates parent directories if missing (#12180)

## Summary

Closes https://github.com/astral-sh/uv/issues/12178

## Test Plan

Added new test. Manually tested on Windows and Linux.

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
samypr100 2025-03-14 22:50:45 -04:00 committed by GitHub
parent 83271f0185
commit 34e1d44199
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View File

@ -144,6 +144,7 @@ pub(crate) async fn pin(
let Some(config_dir) = user_uv_config_dir() else {
return Err(anyhow::anyhow!("No user-level config directory found."));
};
fs_err::tokio::create_dir_all(&config_dir).await?;
PythonVersionFile::new(config_dir.join(PYTHON_VERSION_FILENAME))
.with_versions(vec![request])
} else {

View File

@ -328,6 +328,31 @@ fn python_pin_global_use_local_if_available() -> Result<()> {
Ok(())
}
#[test]
fn python_pin_global_creates_parent_dirs() {
let context: TestContext = TestContext::new_with_versions(&["3.12"]);
let uv_global_config_dir = context.user_config_dir.child("uv");
assert!(
!uv_global_config_dir.exists(),
"Global config directory should not exist yet."
);
uv_snapshot!(context.filters(), context.python_pin().arg("3.12").arg("--global"), @r"
success: true
exit_code: 0
----- stdout -----
Pinned `[UV_USER_CONFIG_DIR]/.python-version` to `3.12`
----- stderr -----
");
assert!(
uv_global_config_dir.exists(),
"Global config directory should be automatically created (if missing) after global pin."
);
}
/// We do not need a Python interpreter to pin without `--resolved`
/// (skip on Windows because the snapshot is different and the behavior is not platform dependent)
#[cfg(unix)]