Skip to content

Commit f93fee6

Browse files
authored
protect global .python-versions from removal without --global flag (#21992)
#14169 implemented a protection for a global .python-version, but did not protect .python-versions
1 parent 7467a0d commit f93fee6

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎crates/uv-python/src/version_files.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ impl PythonVersionFile {
239239

240240
/// Returns `true` if the version file is a global version file.
241241
pub fn is_global(&self) -> bool {
242-
Self::global().is_some_and(|global| self.path() == global.path())
242+
user_uv_config_dir().is_some_and(|directory| {
243+
self.path() == directory.join(PYTHON_VERSION_FILENAME)
244+
|| self.path() == directory.join(PYTHON_VERSIONS_FILENAME)
245+
})
243246
}
244247

245248
/// Return the first request declared in the file, if any.

‎crates/uv/tests/python/python_pin.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,41 @@ fn python_pin_rm() {
852852
error: No Python version file found; use `--rm --global` to remove the global pin
853853
");
854854
}
855+
856+
#[test]
857+
fn python_pin_rm_versions() -> Result<()> {
858+
let context = uv_test::test_context_with_versions!(&[]);
859+
let global = context.user_config_dir.child("uv");
860+
global.create_dir_all()?;
861+
let global_versions = global.child(PYTHON_VERSIONS_FILENAME);
862+
global_versions.write_str("3.11\n3.12\n")?;
863+
864+
let local_versions = context.temp_dir.child(PYTHON_VERSIONS_FILENAME);
865+
local_versions.write_str("3.12\n")?;
866+
867+
uv_snapshot!(context.filters(), context.python_pin().arg("--rm"), @"
868+
exit_code: 0 (success)
869+
----- stdout -----
870+
Removed Python version file at `.python-versions`
871+
");
872+
assert!(!local_versions.exists());
873+
874+
uv_snapshot!(context.filters(), context.python_pin().arg("--rm"), @"
875+
exit_code: 2 (failure)
876+
----- stderr -----
877+
error: No Python version file found; use `--rm --global` to remove the global pin
878+
");
879+
assert_snapshot!(context.read(&global_versions), @"
880+
3.11
881+
3.12
882+
");
883+
884+
uv_snapshot!(context.filters(), context.python_pin().arg("--rm").arg("--global"), @"
885+
exit_code: 0 (success)
886+
----- stdout -----
887+
Removed global Python pin at `[UV_USER_CONFIG_DIR]/.python-versions`
888+
");
889+
assert!(!global_versions.exists());
890+
891+
Ok(())
892+
}

0 commit comments

Comments
 (0)