Skip to content

Commit ddab215

Browse files
committed
Require --global for removal of the global Python pin
1 parent 1dbe750 commit ddab215

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/uv-python/src/version_files.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ impl PythonVersionFile {
217217
}
218218
}
219219

220+
/// Create a new representation of a global Python version file.
221+
pub fn new_global() -> Option<Self> {
222+
let path = user_uv_config_dir()?.join(PYTHON_VERSION_FILENAME);
223+
Some(Self::new(path))
224+
}
225+
220226
/// Return the first request declared in the file, if any.
221227
pub fn version(&self) -> Option<&PythonRequest> {
222228
self.versions.first()
@@ -260,6 +266,9 @@ impl PythonVersionFile {
260266
/// Update the version file on the file system.
261267
pub async fn write(&self) -> Result<(), std::io::Error> {
262268
debug!("Writing Python versions to `{}`", self.path.display());
269+
if let Some(parent) = self.path.parent() {
270+
fs_err::tokio::create_dir_all(parent).await?;
271+
}
263272
fs::tokio::write(
264273
&self.path,
265274
self.versions

crates/uv/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ uv-cli = { workspace = true }
2424
uv-client = { workspace = true }
2525
uv-configuration = { workspace = true }
2626
uv-console = { workspace = true }
27-
uv-dirs = { workspace = true }
2827
uv-dispatch = { workspace = true }
2928
uv-distribution = { workspace = true }
3029
uv-distribution-filename = { workspace = true }

crates/uv/src/commands/python/pin.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use tracing::debug;
99
use uv_cache::Cache;
1010
use uv_client::BaseClientBuilder;
1111
use uv_configuration::{DependencyGroupsWithDefaults, PreviewMode};
12-
use uv_dirs::user_uv_config_dir;
1312
use uv_fs::Simplified;
1413
use uv_python::{
1514
EnvironmentPreference, PYTHON_VERSION_FILENAME, PythonDownloads, PythonInstallation,
@@ -72,10 +71,22 @@ pub(crate) async fn pin(
7271
}
7372
bail!("No Python version file found");
7473
};
74+
75+
if !global
76+
&& PythonVersionFile::new_global().is_some_and(|global| file.path() == global.path())
77+
{
78+
bail!("No Python version file found; use `--rm --global` to remove the global pin");
79+
}
80+
7581
fs_err::tokio::remove_file(file.path()).await?;
7682
writeln!(
7783
printer.stdout(),
78-
"Removed Python version file at `{}`",
84+
"Removed {} at `{}`",
85+
if global {
86+
"global Python pin"
87+
} else {
88+
"Python version file"
89+
},
7990
file.path().user_display()
8091
)?;
8192
return Ok(ExitStatus::Success);
@@ -192,12 +203,11 @@ pub(crate) async fn pin(
192203
let existing = version_file.ok().flatten();
193204
// TODO(zanieb): Allow updating the discovered version file with an `--update` flag.
194205
let new = if global {
195-
let Some(config_dir) = user_uv_config_dir() else {
196-
return Err(anyhow::anyhow!("No user-level config directory found."));
206+
let Some(new) = PythonVersionFile::new_global() else {
207+
// TODO(zanieb): We should find a nice way to surface that as an error
208+
bail!("Failed to determine directory for global Python pin");
197209
};
198-
fs_err::tokio::create_dir_all(&config_dir).await?;
199-
PythonVersionFile::new(config_dir.join(PYTHON_VERSION_FILENAME))
200-
.with_versions(vec![request])
210+
new.with_versions(vec![request])
201211
} else {
202212
PythonVersionFile::new(project_dir.join(PYTHON_VERSION_FILENAME))
203213
.with_versions(vec![request])

crates/uv/tests/it/python_pin.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ fn python_pin_rm() {
855855
error: No Python version file found
856856
");
857857

858-
// Remove the local pin
858+
// Create and remove a local pin
859859
context.python_pin().arg("3.12").assert().success();
860860
uv_snapshot!(context.filters(), context.python_pin().arg("--rm"), @r"
861861
success: true
@@ -892,12 +892,41 @@ fn python_pin_rm() {
892892
.arg("--global")
893893
.assert()
894894
.success();
895+
895896
uv_snapshot!(context.filters(), context.python_pin().arg("--rm").arg("--global"), @r"
896897
success: true
897898
exit_code: 0
898899
----- stdout -----
899-
Removed Python version file at `[UV_USER_CONFIG_DIR]/.python-version`
900+
Removed global Python pin at `[UV_USER_CONFIG_DIR]/.python-version`
901+
902+
----- stderr -----
903+
");
904+
905+
// Add the global pin again
906+
context
907+
.python_pin()
908+
.arg("3.12")
909+
.arg("--global")
910+
.assert()
911+
.success();
912+
913+
// Remove the local pin
914+
uv_snapshot!(context.filters(), context.python_pin().arg("--rm"), @r"
915+
success: true
916+
exit_code: 0
917+
----- stdout -----
918+
Removed Python version file at `.python-version`
919+
920+
----- stderr -----
921+
");
922+
923+
// The global pin should not be removed without `--global`
924+
uv_snapshot!(context.filters(), context.python_pin().arg("--rm"), @r"
925+
success: false
926+
exit_code: 2
927+
----- stdout -----
900928
901929
----- stderr -----
930+
error: No Python version file found; use `--rm --global` to remove the global pin
902931
");
903932
}

0 commit comments

Comments
 (0)