Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{BrokenSymlink, Interpreter, PythonInstallationKey, PythonVersion};
/// A request to find a Python installation.
///
/// See [`PythonRequest::from_str`].
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash)]
#[derive(Debug, Clone, Eq, Default)]
pub enum PythonRequest {
/// An appropriate default Python installation
///
Expand All @@ -68,6 +68,18 @@ pub enum PythonRequest {
Key(PythonDownloadRequest),
}

impl PartialEq for PythonRequest {
fn eq(&self, other: &Self) -> bool {
self.to_canonical_string() == other.to_canonical_string()
}
}

impl std::hash::Hash for PythonRequest {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.to_canonical_string().hash(state);
}
}

impl<'a> serde::Deserialize<'a> for PythonRequest {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
126 changes: 100 additions & 26 deletions crates/uv/src/commands/tool/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ use uv_distribution_types::{
ExtraBuildRequires, NameRequirementSpecification, Requirement, RequirementSource,
UnresolvedRequirementSpecification,
};
use uv_fs::CWD;
use uv_installer::{InstallationStrategy, SatisfiesResult, SitePackages};
use uv_normalize::PackageName;
use uv_pep440::{VersionSpecifier, VersionSpecifiers};
use uv_pep508::MarkerTree;
use uv_preview::Preview;
use uv_python::{
EnvironmentPreference, PythonDownloads, PythonInstallation, PythonPreference, PythonRequest,
EnvironmentPreference, Interpreter, PythonDownloads, PythonEnvironment, PythonInstallation,
PythonPreference, PythonRequest, PythonVersionFile, VersionFileDiscoveryOptions,
};
use uv_requirements::{RequirementsSource, RequirementsSpecification};
use uv_settings::{PythonInstallMirrors, ResolverInstallerOptions, ToolOptions};
Expand Down Expand Up @@ -66,13 +68,31 @@ pub(crate) async fn install(
python_downloads: PythonDownloads,
installer_metadata: bool,
concurrency: Concurrency,
no_config: bool,
cache: Cache,
printer: Printer,
preview: Preview,
) -> Result<ExitStatus> {
let reporter = PythonDownloadReporter::single(printer);

let python_request = python.as_deref().map(PythonRequest::parse);
let (python_request, explicit_python_request) = if let Some(request) = python.as_deref() {
(Some(PythonRequest::parse(request)), true)
} else {
// Discover a global Python version pin, if no request was made
(
PythonVersionFile::discover(
// TODO(zanieb): We don't use the directory, should we expose another interface?
// Should `no_local` be implied by `None` here?
&*CWD,
&VersionFileDiscoveryOptions::default()
.with_no_config(no_config)
.with_no_local(true),
)
.await?
.and_then(PythonVersionFile::into_version),
false,
)
};

// Pre-emptively identify a Python interpreter. We need an interpreter to resolve any unnamed
// requirements, even if we end up using a different interpreter for the tool install itself.
Expand Down Expand Up @@ -344,26 +364,20 @@ pub(crate) async fn install(
}
};

let existing_environment =
installed_tools
.get_environment(package_name, &cache)?
.filter(|environment| {
if environment.uses(&interpreter) {
trace!(
"Existing interpreter matches the requested interpreter for `{}`: {}",
package_name,
environment.interpreter().sys_executable().display()
);
true
} else {
let _ = writeln!(
printer.stderr(),
"Ignoring existing environment for `{}`: the requested Python interpreter does not match the environment interpreter",
package_name.cyan(),
);
false
}
});
let existing_environment = installed_tools
.get_environment(package_name, &cache)?
.filter(|environment| {
existing_environment_usable(
environment,
&interpreter,
package_name,
python_request.as_ref(),
explicit_python_request,
&settings,
existing_tool_receipt.as_ref(),
printer,
)
});

// If the requested and receipt requirements are the same...
if let Some(environment) = existing_environment.as_ref().filter(|_| {
Expand Down Expand Up @@ -394,9 +408,13 @@ pub(crate) async fn install(
)
.into_inner();

// Determine the markers and tags to use for the resolution.
let markers = resolution_markers(None, python_platform.as_ref(), &interpreter);
let tags = resolution_tags(None, python_platform.as_ref(), &interpreter)?;
// Determine the markers and tags to use for the resolution. We use the existing
// environment for markers here — above we filter the environment to `None` if
// `existing_environment_usable` is `false`, so we've determined it's valid.
let markers =
resolution_markers(None, python_platform.as_ref(), environment.interpreter());
let tags =
resolution_tags(None, python_platform.as_ref(), environment.interpreter())?;

// Check if the installed packages meet the requirements.
let site_packages = SitePackages::from_environment(environment)?;
Expand Down Expand Up @@ -640,7 +658,12 @@ pub(crate) async fn install(
&installed_tools,
&options,
force || invalid_tool_receipt,
python_request,
// Only persist the Python request if it was explicitly provided
if explicit_python_request {
python_request
} else {
None
},
requirements,
constraints,
overrides,
Expand All @@ -650,3 +673,54 @@ pub(crate) async fn install(

Ok(ExitStatus::Success)
}

fn existing_environment_usable(
environment: &PythonEnvironment,
interpreter: &Interpreter,
package_name: &PackageName,
python_request: Option<&PythonRequest>,
explicit_python_request: bool,
settings: &ResolverInstallerSettings,
existing_tool_receipt: Option<&uv_tool::Tool>,
printer: Printer,
) -> bool {
// If the environment matches the interpreter, it's usable
if environment.uses(interpreter) {
trace!(
"Existing interpreter matches the requested interpreter for `{}`: {}",
package_name,
environment.interpreter().sys_executable().display()
);
return true;
}

// If there was an explicit Python request that does not match, we'll invalidate the
// environment.
if explicit_python_request {
let _ = writeln!(
printer.stderr(),
"Ignoring existing environment for `{}`: the requested Python interpreter does not match the environment interpreter",
package_name.cyan(),
);
return false;
}

// Otherwise, we'll invalidate the environment if all of the following are true:
// - The user requested a reinstall
// - The tool was not previously pinned to a Python version
// - There is _some_ alternative Python request
if let Some(tool_receipt) = existing_tool_receipt
&& settings.reinstall.is_all()
&& tool_receipt.python().is_none()
&& python_request.is_some()
{
let _ = writeln!(
printer.stderr(),
"Ignoring existing environment for `{from}`: the Python interpreter does not match the environment interpreter",
from = package_name.cyan(),
);
return false;
}

true
}
70 changes: 34 additions & 36 deletions crates/uv/src/commands/tool/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ use uv_distribution_types::{
IndexUrl, Name, NameRequirementSpecification, Requirement, RequirementSource,
UnresolvedRequirement, UnresolvedRequirementSpecification,
};
use uv_fs::CWD;
use uv_fs::Simplified;
use uv_installer::{InstallationStrategy, SatisfiesResult, SitePackages};
use uv_normalize::PackageName;
use uv_pep440::{VersionSpecifier, VersionSpecifiers};
use uv_pep508::MarkerTree;
use uv_preview::Preview;
use uv_python::PythonVersionFile;
use uv_python::VersionFileDiscoveryOptions;
use uv_python::{
EnvironmentPreference, PythonDownloads, PythonEnvironment, PythonInstallation,
PythonPreference, PythonRequest,
Expand Down Expand Up @@ -699,43 +702,38 @@ async fn get_or_create_environment(
) -> Result<(ToolRequirement, PythonEnvironment), ProjectError> {
let reporter = PythonDownloadReporter::single(printer);

// Figure out what Python we're targeting, either explicitly like `uvx python@3`, or via the
// -p/--python flag.
let python_request = match request {
ToolRequest::Python {
request: tool_python_request,
..
} => {
match python {
None => Some(tool_python_request.clone()),

// The user is both invoking a python interpreter directly and also supplying the
// -p/--python flag. Cases like `uvx -p pypy python` are allowed, for two reasons:
// 1) Previously this was the only way to invoke e.g. PyPy via `uvx`, and it's nice
// to remain compatible with that. 2) A script might define an alias like `uvx
// --python $MY_PYTHON ...`, and it's nice to be able to run the interpreter
// directly while sticking to that alias.
//
// However, we want to error out if we see conflicting or redundant versions like
// `uvx -p python38 python39`.
//
// Note that a command like `uvx default` doesn't bring us here. ToolRequest::parse
// returns ToolRequest::Package rather than ToolRequest::Python in that case. See
// PythonRequest::try_from_tool_name.
Some(python_flag) => {
if tool_python_request != &PythonRequest::Default {
return Err(anyhow::anyhow!(
"Received multiple Python version requests: `{}` and `{}`",
python_flag.to_string().cyan(),
tool_python_request.to_canonical_string().cyan()
)
.into());
}
Some(PythonRequest::parse(python_flag))
}
}
// Determine explicit Python version requests
let explicit_python_request = python.map(PythonRequest::parse);
let tool_python_request = match request {
ToolRequest::Python { request, .. } => Some(request.clone()),
ToolRequest::Package { .. } => None,
};

// Resolve Python request with version file lookup when no explicit request
let python_request = match (explicit_python_request, tool_python_request) {
// e.g., `uvx --python 3.10 python3.12`
(Some(explicit), Some(tool_request)) if tool_request != PythonRequest::Default => {
// Conflict: both --python flag and versioned tool name
return Err(anyhow::anyhow!(
"Received multiple Python version requests: `{}` and `{}`",
explicit.to_canonical_string().cyan(),
tool_request.to_canonical_string().cyan()
)
.into());
}
ToolRequest::Package { .. } => python.map(PythonRequest::parse),
// e.g, `uvx --python 3.10 ...`
(Some(explicit), _) => Some(explicit),
// e.g., `uvx python` or `uvx <tool>`
(None, Some(PythonRequest::Default) | None) => PythonVersionFile::discover(
&*CWD,
&VersionFileDiscoveryOptions::default()
.with_no_config(false)
.with_no_local(true),
)
.await?
.and_then(PythonVersionFile::into_version),
// e.g., `uvx python3.12`
(None, Some(tool_request)) => Some(tool_request),
};

// Discover an interpreter.
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
globals.python_downloads,
globals.installer_metadata,
globals.concurrency,
cli.top_level.no_config,
cache,
printer,
globals.preview,
Expand Down
Loading
Loading