Skip to content

Automatically install Python version needed for tool #7827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
52 changes: 52 additions & 0 deletions crates/uv-resolver/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Formatter;
use std::ops::Bound;
use std::sync::Arc;

use indexmap::IndexSet;
use itertools::Itertools;
use pubgrub::{DefaultStringReporter, DerivationTree, Derived, External, Range, Reporter};
use rustc_hash::FxHashMap;

Expand Down Expand Up @@ -197,6 +199,56 @@ impl NoSolutionError {
pub fn header(&self) -> NoSolutionHeader {
NoSolutionHeader::new(self.markers.clone())
}

pub fn find_largest_required_python_version(&self) -> Option<Version> {
fn find(derivation_tree: &ErrorTree, largest_version: &mut Option<Version>) {
match derivation_tree {
// For derived incompatibilities, recursively search each subtree to
// find the root cause
DerivationTree::Derived(derived) => {
find(derived.cause1.as_ref(), largest_version);
find(derived.cause2.as_ref(), largest_version);
}
// Base case — if a missing external dependency a Python package,
// update the largest one seen thus far
DerivationTree::External(External::FromDependencyOf(
pkg_1,
ver_1,
pkg_2,
ver_2,
)) => match (&**pkg_1, &**pkg_2) {
(PubGrubPackageInner::Python(_), _) => {
if let Some((Bound::Included(version), _)) =
ver_1.iter().collect_vec().first()
{
*largest_version = match largest_version {
Some(ref v) if version > v => Some(version.clone()),
None => Some(version.clone()),
_ => largest_version.clone(),
};
}
}
(_, PubGrubPackageInner::Python(_)) => {
if let Some((Bound::Included(version), _)) =
ver_2.iter().collect_vec().first()
{
*largest_version = match largest_version {
Some(ref v) if version > v => Some(version.clone()),
None => Some(version.clone()),
_ => largest_version.clone(),
};
}
}
(_, _) => {}
},
DerivationTree::External(_) => {}
}
}

let mut largest_version: Option<Version> = None;
find(&self.error, &mut largest_version);
largest_version
}
}

impl std::error::Error for NoSolutionError {}
Expand Down
70 changes: 62 additions & 8 deletions crates/uv/src/commands/tool/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use crate::commands::pip::loggers::{DefaultInstallLogger, DefaultResolveLogger};

use crate::commands::project::{
resolve_environment, resolve_names, sync_environment, update_environment,
EnvironmentSpecification,
EnvironmentSpecification, ProjectError,
};
use crate::commands::tool::common::remove_entrypoints;
use crate::commands::tool::Target;
use crate::commands::{pip, ExitStatus, SharedState};
use crate::commands::{reporters::PythonDownloadReporter, tool::common::install_executables};
use crate::commands::{ExitStatus, SharedState};
use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings;

Expand Down Expand Up @@ -328,12 +328,13 @@ pub(crate) async fn install(
}

// Create a `RequirementsSpecification` from the resolved requirements, to avoid re-resolving.
let spec_requirements: Vec<UnresolvedRequirementSpecification> = requirements
.iter()
.cloned()
.map(UnresolvedRequirementSpecification::from)
.collect();
let spec = RequirementsSpecification {
requirements: requirements
.iter()
.cloned()
.map(UnresolvedRequirementSpecification::from)
.collect(),
requirements: spec_requirements.clone(),
..spec
};

Expand Down Expand Up @@ -380,7 +381,60 @@ pub(crate) async fn install(
&cache,
printer,
)
.await?;
.await;

let resolution = match resolution {
Ok(resolution) => resolution,
Err(err) => match err {
ProjectError::Operation(pip::operations::Error::Resolve(
uv_resolver::ResolveError::NoSolution(ref no_solution_err),
)) => {
let (None, Some(version)) = (
python_request,
no_solution_err.find_largest_required_python_version(),
) else {
return Err(err.into());
};

writeln!(
printer.stderr(),
"Couldn't find acceptable Python version for {package}, downloading Python {version} and re-attempting install."
)?;

let interpreter = PythonInstallation::find_or_download(
Some(&PythonRequest::parse(version.to_string().as_str())),
EnvironmentPreference::OnlySystem,
python_preference,
python_downloads,
&client_builder,
&cache,
Some(&reporter),
)
.await?
.into_interpreter();

let new_spec = RequirementsSpecification {
requirements: spec_requirements,
..RequirementsSpecification::default()
};

resolve_environment(
EnvironmentSpecification::from(new_spec),
&interpreter,
settings.as_ref().into(),
&state,
Box::new(DefaultResolveLogger),
connectivity,
concurrency,
native_tls,
&cache,
printer,
)
.await?
}
_ => return Err(err.into()),
},
};

let environment = installed_tools.create_environment(&from.name, interpreter)?;

Expand Down
Loading