Support transparent Python patch version upgrades (#13954)
> NOTE: The PRs that were merged into this feature branch have all been independently reviewed. But it's also useful to see all of the changes in their final form. I've added comments to significant changes throughout the PR to aid discussion. This PR introduces transparent Python version upgrades to uv, allowing for a smoother experience when upgrading to new patch versions. Previously, upgrading Python patch versions required manual updates to each virtual environment. Now, virtual environments can transparently upgrade to newer patch versions. Due to significant changes in how uv installs and executes managed Python executables, this functionality is initially available behind a `--preview` flag. Once an installation has been made upgradeable through `--preview`, subsequent operations (like `uv venv -p 3.10` or patch upgrades) will work without requiring the flag again. This is accomplished by checking for the existence of a minor version symlink directory (or junction on Windows). ### Features * New `uv python upgrade` command to upgrade installed Python versions to the latest available patch release: ``` # Upgrade specific minor version uv python upgrade 3.12 --preview # Upgrade all installed minor versions uv python upgrade --preview ``` * Transparent upgrades also occur when installing newer patch versions: ``` uv python install 3.10.8 --preview # Automatically upgrades existing 3.10 environments uv python install 3.10.18 ``` * Support for transparently upgradeable Python `bin` installations via `--preview` flag ``` uv python install 3.13 --preview # Automatically upgrades the `bin` installation if there is a newer patch version available uv python upgrade 3.13 --preview ``` * Virtual environments can still be tied to a patch version if desired (ignoring patch upgrades): ``` uv venv -p 3.10.8 ``` ### Implementation Transparent upgrades are implemented using: * Minor version symlink directories (Unix) or junctions (Windows) * On Windows, trampolines simulate paths with junctions * Symlink directory naming follows Python build standalone format: e.g., `cpython-3.10-macos-aarch64-none` * Upgrades are scoped to the minor version key (as represented in the naming format: implementation-minor version+variant-os-arch-libc) * If the context does not provide a patch version request and the interpreter is from a managed CPython installation, the `Interpreter` used by `uv python run` will use the full symlink directory executable path when available, enabling transparently upgradeable environments created with the `venv` module (`uv run python -m venv`) New types: * `PythonMinorVersionLink`: in a sense, the core type for this PR, this is a representation of a minor version symlink directory (or junction on Windows) that points to the highest installed managed CPython patch version for a minor version key. * `PythonInstallationMinorVersionKey`: provides a view into a `PythonInstallationKey` that excludes the patch and prerelease. This is used for grouping installations by minor version key (e.g., to find the highest available patch installation for that minor version key) and for minor version directory naming. ### Compatibility * Supports virtual environments created with: * `uv venv` * `uv run python -m venv` (using managed Python that was installed or upgraded with `--preview`) * Virtual environments created within these environments * Existing virtual environments from before these changes continue to work but aren't transparently upgradeable without being recreated * Supports both standard Python (`python3.10`) and freethreaded Python (`python3.10t`) * Support for transparently upgrades is currently only available for managed CPython installations Closes #7287 Closes #7325 Closes #7892 Closes #9031 Closes #12977 --------- Co-authored-by: Zanie Blue <contact@zanie.dev>
This commit is contained in:
@@ -8,6 +8,7 @@ use std::{env, io, iter};
|
||||
use std::{path::Path, path::PathBuf, str::FromStr};
|
||||
use thiserror::Error;
|
||||
use tracing::{debug, instrument, trace};
|
||||
use uv_configuration::PreviewMode;
|
||||
use which::{which, which_all};
|
||||
|
||||
use uv_cache::Cache;
|
||||
@@ -25,7 +26,7 @@ use crate::implementation::ImplementationName;
|
||||
use crate::installation::PythonInstallation;
|
||||
use crate::interpreter::Error as InterpreterError;
|
||||
use crate::interpreter::{StatusCodeError, UnexpectedResponseError};
|
||||
use crate::managed::ManagedPythonInstallations;
|
||||
use crate::managed::{ManagedPythonInstallations, PythonMinorVersionLink};
|
||||
#[cfg(windows)]
|
||||
use crate::microsoft_store::find_microsoft_store_pythons;
|
||||
use crate::virtualenv::Error as VirtualEnvError;
|
||||
@@ -35,12 +36,12 @@ use crate::virtualenv::{
|
||||
};
|
||||
#[cfg(windows)]
|
||||
use crate::windows_registry::{WindowsPython, registry_pythons};
|
||||
use crate::{BrokenSymlink, Interpreter, PythonVersion};
|
||||
use crate::{BrokenSymlink, Interpreter, PythonInstallationKey, PythonVersion};
|
||||
|
||||
/// A request to find a Python installation.
|
||||
///
|
||||
/// See [`PythonRequest::from_str`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
|
||||
pub enum PythonRequest {
|
||||
/// An appropriate default Python installation
|
||||
///
|
||||
@@ -173,7 +174,7 @@ pub enum PythonVariant {
|
||||
}
|
||||
|
||||
/// A Python discovery version request.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
|
||||
pub enum VersionRequest {
|
||||
/// Allow an appropriate default Python version.
|
||||
#[default]
|
||||
@@ -334,6 +335,7 @@ fn python_executables_from_installed<'a>(
|
||||
implementation: Option<&'a ImplementationName>,
|
||||
platform: PlatformRequest,
|
||||
preference: PythonPreference,
|
||||
preview: PreviewMode,
|
||||
) -> Box<dyn Iterator<Item = Result<(PythonSource, PathBuf), Error>> + 'a> {
|
||||
let from_managed_installations = iter::once_with(move || {
|
||||
ManagedPythonInstallations::from_settings(None)
|
||||
@@ -359,7 +361,29 @@ fn python_executables_from_installed<'a>(
|
||||
true
|
||||
})
|
||||
.inspect(|installation| debug!("Found managed installation `{installation}`"))
|
||||
.map(|installation| (PythonSource::Managed, installation.executable(false))))
|
||||
.map(move |installation| {
|
||||
// If it's not a patch version request, then attempt to read the stable
|
||||
// minor version link.
|
||||
let executable = version
|
||||
.patch()
|
||||
.is_none()
|
||||
.then(|| {
|
||||
PythonMinorVersionLink::from_installation(
|
||||
&installation,
|
||||
preview,
|
||||
)
|
||||
.filter(PythonMinorVersionLink::exists)
|
||||
.map(
|
||||
|minor_version_link| {
|
||||
minor_version_link.symlink_executable.clone()
|
||||
},
|
||||
)
|
||||
})
|
||||
.flatten()
|
||||
.unwrap_or_else(|| installation.executable(false));
|
||||
(PythonSource::Managed, executable)
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
.flatten_ok();
|
||||
@@ -452,6 +476,7 @@ fn python_executables<'a>(
|
||||
platform: PlatformRequest,
|
||||
environments: EnvironmentPreference,
|
||||
preference: PythonPreference,
|
||||
preview: PreviewMode,
|
||||
) -> Box<dyn Iterator<Item = Result<(PythonSource, PathBuf), Error>> + 'a> {
|
||||
// Always read from `UV_INTERNAL__PARENT_INTERPRETER` — it could be a system interpreter
|
||||
let from_parent_interpreter = iter::once_with(|| {
|
||||
@@ -472,7 +497,7 @@ fn python_executables<'a>(
|
||||
|
||||
let from_virtual_environments = python_executables_from_virtual_environments();
|
||||
let from_installed =
|
||||
python_executables_from_installed(version, implementation, platform, preference);
|
||||
python_executables_from_installed(version, implementation, platform, preference, preview);
|
||||
|
||||
// Limit the search to the relevant environment preference; this avoids unnecessary work like
|
||||
// traversal of the file system. Subsequent filtering should be done by the caller with
|
||||
@@ -671,16 +696,23 @@ fn python_interpreters<'a>(
|
||||
environments: EnvironmentPreference,
|
||||
preference: PythonPreference,
|
||||
cache: &'a Cache,
|
||||
preview: PreviewMode,
|
||||
) -> impl Iterator<Item = Result<(PythonSource, Interpreter), Error>> + 'a {
|
||||
python_interpreters_from_executables(
|
||||
// Perform filtering on the discovered executables based on their source. This avoids
|
||||
// unnecessary interpreter queries, which are generally expensive. We'll filter again
|
||||
// with `interpreter_satisfies_environment_preference` after querying.
|
||||
python_executables(version, implementation, platform, environments, preference).filter_ok(
|
||||
move |(source, path)| {
|
||||
source_satisfies_environment_preference(*source, path, environments)
|
||||
},
|
||||
),
|
||||
python_executables(
|
||||
version,
|
||||
implementation,
|
||||
platform,
|
||||
environments,
|
||||
preference,
|
||||
preview,
|
||||
)
|
||||
.filter_ok(move |(source, path)| {
|
||||
source_satisfies_environment_preference(*source, path, environments)
|
||||
}),
|
||||
cache,
|
||||
)
|
||||
.filter_ok(move |(source, interpreter)| {
|
||||
@@ -919,6 +951,7 @@ pub fn find_python_installations<'a>(
|
||||
environments: EnvironmentPreference,
|
||||
preference: PythonPreference,
|
||||
cache: &'a Cache,
|
||||
preview: PreviewMode,
|
||||
) -> Box<dyn Iterator<Item = Result<FindPythonResult, Error>> + 'a> {
|
||||
let sources = DiscoveryPreferences {
|
||||
python_preference: preference,
|
||||
@@ -1010,6 +1043,7 @@ pub fn find_python_installations<'a>(
|
||||
environments,
|
||||
preference,
|
||||
cache,
|
||||
preview,
|
||||
)
|
||||
.map_ok(|tuple| Ok(PythonInstallation::from_tuple(tuple)))
|
||||
}),
|
||||
@@ -1022,6 +1056,7 @@ pub fn find_python_installations<'a>(
|
||||
environments,
|
||||
preference,
|
||||
cache,
|
||||
preview,
|
||||
)
|
||||
.map_ok(|tuple| Ok(PythonInstallation::from_tuple(tuple)))
|
||||
}),
|
||||
@@ -1038,6 +1073,7 @@ pub fn find_python_installations<'a>(
|
||||
environments,
|
||||
preference,
|
||||
cache,
|
||||
preview,
|
||||
)
|
||||
.map_ok(|tuple| Ok(PythonInstallation::from_tuple(tuple)))
|
||||
})
|
||||
@@ -1051,6 +1087,7 @@ pub fn find_python_installations<'a>(
|
||||
environments,
|
||||
preference,
|
||||
cache,
|
||||
preview,
|
||||
)
|
||||
.filter_ok(|(_source, interpreter)| {
|
||||
interpreter
|
||||
@@ -1072,6 +1109,7 @@ pub fn find_python_installations<'a>(
|
||||
environments,
|
||||
preference,
|
||||
cache,
|
||||
preview,
|
||||
)
|
||||
.filter_ok(|(_source, interpreter)| {
|
||||
interpreter
|
||||
@@ -1096,6 +1134,7 @@ pub fn find_python_installations<'a>(
|
||||
environments,
|
||||
preference,
|
||||
cache,
|
||||
preview,
|
||||
)
|
||||
.filter_ok(|(_source, interpreter)| request.satisfied_by_interpreter(interpreter))
|
||||
.map_ok(|tuple| Ok(PythonInstallation::from_tuple(tuple)))
|
||||
@@ -1113,8 +1152,10 @@ pub(crate) fn find_python_installation(
|
||||
environments: EnvironmentPreference,
|
||||
preference: PythonPreference,
|
||||
cache: &Cache,
|
||||
preview: PreviewMode,
|
||||
) -> Result<FindPythonResult, Error> {
|
||||
let installations = find_python_installations(request, environments, preference, cache);
|
||||
let installations =
|
||||
find_python_installations(request, environments, preference, cache, preview);
|
||||
let mut first_prerelease = None;
|
||||
let mut first_error = None;
|
||||
for result in installations {
|
||||
@@ -1210,12 +1251,13 @@ pub(crate) fn find_best_python_installation(
|
||||
environments: EnvironmentPreference,
|
||||
preference: PythonPreference,
|
||||
cache: &Cache,
|
||||
preview: PreviewMode,
|
||||
) -> Result<FindPythonResult, Error> {
|
||||
debug!("Starting Python discovery for {}", request);
|
||||
|
||||
// First, check for an exact match (or the first available version if no Python version was provided)
|
||||
debug!("Looking for exact match for request {request}");
|
||||
let result = find_python_installation(request, environments, preference, cache);
|
||||
let result = find_python_installation(request, environments, preference, cache, preview);
|
||||
match result {
|
||||
Ok(Ok(installation)) => {
|
||||
warn_on_unsupported_python(installation.interpreter());
|
||||
@@ -1243,7 +1285,7 @@ pub(crate) fn find_best_python_installation(
|
||||
_ => None,
|
||||
} {
|
||||
debug!("Looking for relaxed patch version {request}");
|
||||
let result = find_python_installation(&request, environments, preference, cache);
|
||||
let result = find_python_installation(&request, environments, preference, cache, preview);
|
||||
match result {
|
||||
Ok(Ok(installation)) => {
|
||||
warn_on_unsupported_python(installation.interpreter());
|
||||
@@ -1260,14 +1302,16 @@ pub(crate) fn find_best_python_installation(
|
||||
debug!("Looking for a default Python installation");
|
||||
let request = PythonRequest::Default;
|
||||
Ok(
|
||||
find_python_installation(&request, environments, preference, cache)?.map_err(|err| {
|
||||
// Use a more general error in this case since we looked for multiple versions
|
||||
PythonNotFound {
|
||||
request,
|
||||
python_preference: err.python_preference,
|
||||
environment_preference: err.environment_preference,
|
||||
}
|
||||
}),
|
||||
find_python_installation(&request, environments, preference, cache, preview)?.map_err(
|
||||
|err| {
|
||||
// Use a more general error in this case since we looked for multiple versions
|
||||
PythonNotFound {
|
||||
request,
|
||||
python_preference: err.python_preference,
|
||||
environment_preference: err.environment_preference,
|
||||
}
|
||||
},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1645,6 +1689,24 @@ impl PythonRequest {
|
||||
Ok(rest.parse().ok())
|
||||
}
|
||||
|
||||
/// Check if this request includes a specific patch version.
|
||||
pub fn includes_patch(&self) -> bool {
|
||||
match self {
|
||||
PythonRequest::Default => false,
|
||||
PythonRequest::Any => false,
|
||||
PythonRequest::Version(version_request) => version_request.patch().is_some(),
|
||||
PythonRequest::Directory(..) => false,
|
||||
PythonRequest::File(..) => false,
|
||||
PythonRequest::ExecutableName(..) => false,
|
||||
PythonRequest::Implementation(..) => false,
|
||||
PythonRequest::ImplementationVersion(_, version) => version.patch().is_some(),
|
||||
PythonRequest::Key(request) => request
|
||||
.version
|
||||
.as_ref()
|
||||
.is_some_and(|request| request.patch().is_some()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a given interpreter satisfies the interpreter request.
|
||||
pub fn satisfied(&self, interpreter: &Interpreter, cache: &Cache) -> bool {
|
||||
/// Returns `true` if the two paths refer to the same interpreter executable.
|
||||
@@ -2086,6 +2148,11 @@ impl fmt::Display for ExecutableName {
|
||||
}
|
||||
|
||||
impl VersionRequest {
|
||||
/// Derive a [`VersionRequest::MajorMinor`] from a [`PythonInstallationKey`]
|
||||
pub fn major_minor_request_from_key(key: &PythonInstallationKey) -> Self {
|
||||
Self::MajorMinor(key.major, key.minor, key.variant)
|
||||
}
|
||||
|
||||
/// Return possible executable names for the given version request.
|
||||
pub(crate) fn executable_names(
|
||||
&self,
|
||||
|
||||
Reference in New Issue
Block a user