Skip to content
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

Enable constraints in uv tool upgrade CLI #9375

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
4 changes: 2 additions & 2 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3849,9 +3849,9 @@ pub struct ToolUninstallArgs {
#[derive(Args)]
#[allow(clippy::struct_excessive_bools)]
pub struct ToolUpgradeArgs {
/// The name of the tool to upgrade.
/// The name of the tool to upgrade, along with an optional version specifier.
#[arg(required = true)]
pub name: Vec<PackageName>,
pub name: Vec<String>,

/// Upgrade all tools.
#[arg(long, conflicts_with("name"))]
Expand Down
18 changes: 17 additions & 1 deletion crates/uv-requirements/src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,25 @@ impl RequirementsSpecification {
}
}

/// Initialize a [`RequirementsSpecification`] from a list of [`Requirement`], including
/// constraints.
pub fn from_constraints(requirements: Vec<Requirement>, constraints: Vec<Requirement>) -> Self {
Self {
requirements: requirements
.into_iter()
.map(UnresolvedRequirementSpecification::from)
.collect(),
constraints: constraints
.into_iter()
.map(NameRequirementSpecification::from)
.collect(),
..Self::default()
}
}

/// Initialize a [`RequirementsSpecification`] from a list of [`Requirement`], including
/// constraints and overrides.
pub fn from_constraints(
pub fn from_overrides(
requirements: Vec<Requirement>,
constraints: Vec<Requirement>,
overrides: Vec<Requirement>,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub(crate) async fn run(
.collect::<Result<Vec<_>, _>>()?;

let spec =
RequirementsSpecification::from_constraints(requirements, constraints, overrides);
RequirementsSpecification::from_overrides(requirements, constraints, overrides);
let result = CachedEnvironment::get_or_create(
EnvironmentSpecification::from(spec),
interpreter,
Expand Down
34 changes: 23 additions & 11 deletions crates/uv/src/commands/tool/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::{collections::BTreeSet, fmt::Write};

use anyhow::Result;
use itertools::Itertools;
use owo_colors::OwoColorize;
use std::collections::BTreeMap;
use std::fmt::Write;
use tracing::debug;

use uv_cache::Cache;
use uv_client::{BaseClientBuilder, Connectivity};
use uv_configuration::{Concurrency, TrustedHost};
use uv_fs::CWD;
use uv_normalize::PackageName;
use uv_pypi_types::Requirement;
use uv_python::{
EnvironmentPreference, Interpreter, PythonDownloads, PythonInstallation, PythonPreference,
PythonRequest,
Expand All @@ -31,7 +33,7 @@ use crate::settings::ResolverInstallerSettings;

/// Upgrade a tool.
pub(crate) async fn upgrade(
name: Vec<PackageName>,
names: Vec<String>,
python: Option<String>,
install_mirrors: PythonInstallMirrors,
connectivity: Connectivity,
Expand All @@ -48,17 +50,24 @@ pub(crate) async fn upgrade(
let installed_tools = InstalledTools::from_settings()?.init()?;
let _lock = installed_tools.lock().await?;

// Collect the tools to upgrade.
let names: BTreeSet<PackageName> = {
if name.is_empty() {
// Collect the tools to upgrade, along with any constraints.
let names: BTreeMap<PackageName, Vec<Requirement>> = {
if names.is_empty() {
installed_tools
.tools()
.unwrap_or_default()
.into_iter()
.map(|(name, _)| name)
.map(|(name, _)| (name, Vec::new()))
.collect()
} else {
name.into_iter().collect()
let mut map = BTreeMap::new();
for name in names {
let requirement = Requirement::from(uv_pep508::Requirement::parse(&name, &*CWD)?);
map.entry(requirement.name.clone())
.or_insert_with(Vec::new)
.push(requirement);
}
map
}
};

Expand Down Expand Up @@ -102,10 +111,11 @@ pub(crate) async fn upgrade(
let mut did_upgrade_environment = vec![];

let mut errors = Vec::new();
for name in &names {
for (name, constraints) in &names {
debug!("Upgrading tool: `{name}`");
let result = upgrade_tool(
name,
constraints,
interpreter.as_ref(),
printer,
&installed_tools,
Expand Down Expand Up @@ -194,6 +204,7 @@ enum UpgradeOutcome {
/// Upgrade a specific tool.
async fn upgrade_tool(
name: &PackageName,
constraints: &[Requirement],
interpreter: Option<&Interpreter>,
printer: Printer,
installed_tools: &InstalledTools,
Expand Down Expand Up @@ -255,7 +266,8 @@ async fn upgrade_tool(

// Resolve the requirements.
let requirements = existing_tool_receipt.requirements();
let spec = RequirementsSpecification::from_requirements(requirements.to_vec());
let spec =
RequirementsSpecification::from_constraints(requirements.to_vec(), constraints.to_vec());

// Initialize any shared state.
let state = SharedState::default();
Expand All @@ -267,7 +279,7 @@ async fn upgrade_tool(
{
// If we're using a new interpreter, re-create the environment for each tool.
let resolution = resolve_environment(
RequirementsSpecification::from_requirements(requirements.to_vec()).into(),
spec.into(),
interpreter,
settings.as_ref().into(),
&state,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
let cache = cache.init()?.with_refresh(Refresh::All(Timestamp::now()));

Box::pin(commands::tool_upgrade(
args.name,
args.names,
args.python,
args.install_mirrors,
globals.connectivity,
Expand Down
7 changes: 5 additions & 2 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl ToolInstallSettings {
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone)]
pub(crate) struct ToolUpgradeSettings {
pub(crate) name: Vec<PackageName>,
pub(crate) names: Vec<String>,
pub(crate) python: Option<String>,
pub(crate) install_mirrors: PythonInstallMirrors,
pub(crate) args: ResolverInstallerOptions,
Expand Down Expand Up @@ -574,6 +574,9 @@ impl ToolUpgradeSettings {
if upgrade {
warn_user_once!("`--upgrade` is enabled by default on `uv tool upgrade`");
}
if !upgrade_package.is_empty() {
warn_user_once!("`--upgrade-package` is enabled by default on `uv tool upgrade`");
}

// Enable `--upgrade` by default.
let installer = ResolverInstallerArgs {
Expand Down Expand Up @@ -611,7 +614,7 @@ impl ToolUpgradeSettings {
.unwrap_or_default();

Self {
name: if all { vec![] } else { name },
names: if all { vec![] } else { name },
python: python.and_then(Maybe::into_option),
args,
filesystem: top_level,
Expand Down
30 changes: 26 additions & 4 deletions crates/uv/tests/it/tool_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,28 @@ fn tool_upgrade_constraint() {
Installed 1 executable: pybabel
"###);

// Upgrade `babel`, but apply a constraint.
// Upgrade `babel`, but apply a constraint inline.
uv_snapshot!(context.filters(), context.tool_upgrade()
.arg("babel<2.12.0")
.arg("--index-url")
.arg("https://pypi.org/simple/")
.env(EnvVars::UV_TOOL_DIR, tool_dir.as_os_str())
.env(EnvVars::XDG_BIN_HOME, bin_dir.as_os_str())
.env(EnvVars::PATH, bin_dir.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Updated babel v2.6.0 -> v2.11.0
- babel==2.6.0
+ babel==2.11.0
- pytz==2018.5
+ pytz==2024.1
Installed 1 executable: pybabel
"###);

// Upgrade `babel`, but apply a constraint via `--upgrade-package`.
uv_snapshot!(context.filters(), context.tool_upgrade()
.arg("babel")
.arg("--index-url")
Expand All @@ -482,10 +503,11 @@ fn tool_upgrade_constraint() {
----- stdout -----

----- stderr -----
Updated babel v2.6.0 -> v2.13.1
- babel==2.6.0
warning: `--upgrade-package` is enabled by default on `uv tool upgrade`
Updated babel v2.11.0 -> v2.13.1
- babel==2.11.0
+ babel==2.13.1
- pytz==2018.5
- pytz==2024.1
+ setuptools==69.2.0
Installed 1 executable: pybabel
"###);
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -3495,7 +3495,7 @@ uv tool upgrade [OPTIONS] <NAME>...

<h3 class="cli-reference">Arguments</h3>

<dl class="cli-reference"><dt><code>NAME</code></dt><dd><p>The name of the tool to upgrade</p>
<dl class="cli-reference"><dt><code>NAME</code></dt><dd><p>The name of the tool to upgrade, along with an optional version specifier</p>

</dd></dl>

Expand Down
Loading