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 1 commit
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
62 changes: 54 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,52 @@ pub(crate) async fn install(
&cache,
printer,
)
.await?;
.await;

let resolution = match resolution {
Ok(resolution) => resolution,
Err(
err @ ProjectError::Operation(pip::operations::Error::Resolve(
uv_resolver::ResolveError::NoSolution(_),
)),
) => {
if python_request.is_some() {
return Err(err.into());
}

let interpreter = PythonInstallation::find_or_download(
Some(&PythonRequest::parse("3.12")),
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?
}
Err(e) => return Err(e.into()),
};

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

Expand Down
22 changes: 22 additions & 0 deletions crates/uv/tests/tool_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3007,3 +3007,25 @@ fn tool_install_at_latest_upgrade() {
"###);
});
}

#[test]
fn tool_install_python() {
let context = TestContext::new("3.8");
let tool_dir = context.temp_dir.child("tools");
let bin_dir = context.temp_dir.child("bin");

uv_snapshot!(context.filters(), context.tool_install()
.arg("posting")
.arg("--python")
.arg("3.8")
.env("UV_TOOL_DIR", tool_dir.as_os_str())
.env("XDG_BIN_HOME", bin_dir.as_os_str())
.env("PATH", bin_dir.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----

"###);
}
Loading