Skip to content

Commit 241ad88

Browse files
authored
Confirm that the directory name is a valid Python install key during managed check (#16080)
Closes #16077 ``` ❯ UV_PYTHON_INSTALL_DIR=/opt uv python list --no-managed-python cpython-3.9.6-macos-aarch64-none /usr/bin/python3 ❯ alias uv=$(pwd)/target/debug/uv ❯ UV_PYTHON_INSTALL_DIR=/opt uv python list --no-managed-python cpython-3.13.3-macos-aarch64-none /opt/homebrew/bin/python3.13 -> ../Cellar/python@3.13/3.13.3/bin/python3.13 cpython-3.13.3-macos-aarch64-none /opt/homebrew/bin/python3 -> ../Cellar/python@3.13/3.13.3/bin/python3 cpython-3.12.10-macos-aarch64-none /opt/homebrew/bin/python3.12 -> ../Cellar/python@3.12/3.12.10/bin/python3.12 cpython-3.11.12-macos-aarch64-none /opt/homebrew/bin/python3.11 -> ../Cellar/python@3.11/3.11.12/bin/python3.11 cpython-3.9.6-macos-aarch64-none /usr/bin/python3 pypy-3.10.14-macos-aarch64-none /opt/homebrew/bin/pypy3 -> ../Cellar/pypy3.10/7.3.17_1/bin/pypy3 ``` Prevents false positives when the `UV_PYTHON_INSTALL_DIR` is a prefix of some other path with Python installations
1 parent b06f02f commit 241ad88

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

crates/uv-python/src/interpreter.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::env::consts::ARCH;
33
use std::fmt::{Display, Formatter};
44
use std::path::{Path, PathBuf};
55
use std::process::{Command, ExitStatus};
6+
use std::str::FromStr;
67
use std::sync::OnceLock;
78
use std::{env, io};
89

@@ -301,7 +302,19 @@ impl Interpreter {
301302
return false;
302303
};
303304

304-
self.sys_base_prefix.starts_with(installations.root())
305+
let Ok(suffix) = self.sys_base_prefix.strip_prefix(installations.root()) else {
306+
return false;
307+
};
308+
309+
let Some(first_component) = suffix.components().next() else {
310+
return false;
311+
};
312+
313+
let Some(name) = first_component.as_os_str().to_str() else {
314+
return false;
315+
};
316+
317+
PythonInstallationKey::from_str(name).is_ok()
305318
}
306319

307320
/// Returns `Some` if the environment is externally managed, optionally including an error

0 commit comments

Comments
 (0)