Skip to content

Commit

Permalink
lock with overrides from tool.uv (#4108)
Browse files Browse the repository at this point in the history
  • Loading branch information
idlsoft committed Jun 21, 2024
1 parent dd45fce commit 2ae60ae
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 28 deletions.
3 changes: 3 additions & 0 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use pypi_types::Requirement;
use uv_client::{BaseClientBuilder, Connectivity, FlatIndexClient, RegistryClientBuilder};
use uv_dispatch::BuildDispatch;
use uv_distribution::pyproject::{Source, SourceError};
Expand All @@ -25,6 +26,7 @@ use crate::settings::ResolverInstallerSettings;
#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
pub(crate) async fn add(
requirements: Vec<RequirementsSource>,
overrides: Vec<Requirement>,
workspace: bool,
dev: bool,
editable: Option<bool>,
Expand Down Expand Up @@ -197,6 +199,7 @@ pub(crate) async fn add(
&settings.prerelease,
&settings.config_setting,
settings.exclude_newer.as_ref(),
overrides,
&settings.link_mode,
&settings.build_options,
preview,
Expand Down
9 changes: 8 additions & 1 deletion crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anstream::eprint;

use distribution_types::{IndexLocations, UnresolvedRequirementSpecification};
use install_wheel_rs::linker::LinkMode;
use pypi_types::Requirement;
use uv_cache::Cache;
use uv_client::{Connectivity, FlatIndexClient, RegistryClientBuilder};
use uv_configuration::{
Expand Down Expand Up @@ -32,6 +33,7 @@ use crate::settings::ResolverSettings;
pub(crate) async fn lock(
python: Option<String>,
settings: ResolverSettings,
overrides: Vec<Requirement>,
preview: PreviewMode,
toolchain_preference: ToolchainPreference,
connectivity: Connectivity,
Expand Down Expand Up @@ -71,6 +73,7 @@ pub(crate) async fn lock(
&settings.prerelease,
&settings.config_setting,
settings.exclude_newer.as_ref(),
overrides,
&settings.link_mode,
&settings.build_options,
preview,
Expand Down Expand Up @@ -108,6 +111,7 @@ pub(super) async fn do_lock(
prerelease: &PreReleaseMode,
config_setting: &ConfigSettings,
exclude_newer: Option<&ExcludeNewer>,
overrides: Vec<Requirement>,
link_mode: &LinkMode,
build_options: &BuildOptions,
preview: PreviewMode,
Expand All @@ -124,7 +128,10 @@ pub(super) async fn do_lock(
.map(UnresolvedRequirementSpecification::from)
.collect();
let constraints = vec![];
let overrides = vec![];
let overrides = overrides
.into_iter()
.map(UnresolvedRequirementSpecification::from)
.collect();
let dev = vec![DEV_DEPENDENCIES.clone()];

let source_trees = vec![];
Expand Down
3 changes: 3 additions & 0 deletions crates/uv/src/commands/project/remove.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;

use pep508_rs::PackageName;
use pypi_types::Requirement;
use uv_cache::Cache;
use uv_client::Connectivity;
use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode};
Expand All @@ -18,6 +19,7 @@ use crate::settings::{InstallerSettings, ResolverSettings};
#[allow(clippy::too_many_arguments)]
pub(crate) async fn remove(
requirements: Vec<PackageName>,
overrides: Vec<Requirement>,
dev: bool,
python: Option<String>,
toolchain_preference: ToolchainPreference,
Expand Down Expand Up @@ -109,6 +111,7 @@ pub(crate) async fn remove(
&settings.prerelease,
&settings.config_setting,
settings.exclude_newer.as_ref(),
overrides,
&settings.link_mode,
&settings.build_options,
preview,
Expand Down
3 changes: 3 additions & 0 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;

use anyhow::{Context, Result};
use itertools::Itertools;
use pypi_types::Requirement;
use tokio::process::Command;
use tracing::debug;

Expand Down Expand Up @@ -30,6 +31,7 @@ pub(crate) async fn run(
dev: bool,
command: ExternalCommand,
requirements: Vec<RequirementsSource>,
overrides: Vec<Requirement>,
python: Option<String>,
package: Option<PackageName>,
settings: ResolverInstallerSettings,
Expand Down Expand Up @@ -86,6 +88,7 @@ pub(crate) async fn run(
&settings.prerelease,
&settings.config_setting,
settings.exclude_newer.as_ref(),
overrides,
&settings.link_mode,
&settings.build_options,
preview,
Expand Down
4 changes: 4 additions & 0 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ async fn run() -> Result<ExitStatus> {
args.dev,
args.command,
requirements,
args.overrides_from_workspace,
args.python,
args.package,
args.settings,
Expand Down Expand Up @@ -697,6 +698,7 @@ async fn run() -> Result<ExitStatus> {
commands::lock(
args.python,
args.settings,
args.overrides_from_workspace,
globals.preview,
globals.toolchain_preference,
globals.connectivity,
Expand All @@ -717,6 +719,7 @@ async fn run() -> Result<ExitStatus> {

commands::add(
args.requirements,
args.overrides_from_workspace,
args.workspace,
args.dev,
args.editable,
Expand Down Expand Up @@ -746,6 +749,7 @@ async fn run() -> Result<ExitStatus> {

commands::remove(
args.requirements,
args.overrides_from_workspace,
args.dev,
args.python,
globals.toolchain_preference,
Expand Down
55 changes: 28 additions & 27 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub(crate) struct RunSettings {
pub(crate) package: Option<PackageName>,
pub(crate) refresh: Refresh,
pub(crate) settings: ResolverInstallerSettings,
pub(crate) overrides_from_workspace: Vec<Requirement>,
}

impl RunSettings {
Expand Down Expand Up @@ -184,6 +185,7 @@ impl RunSettings {
python,
package,
refresh: Refresh::from(refresh),
overrides_from_workspace: overrides_from_workspace(&filesystem),
settings: ResolverInstallerSettings::combine(
resolver_installer_options(installer, build),
filesystem,
Expand Down Expand Up @@ -366,6 +368,7 @@ pub(crate) struct LockSettings {
pub(crate) python: Option<String>,
pub(crate) refresh: Refresh,
pub(crate) settings: ResolverSettings,
pub(crate) overrides_from_workspace: Vec<Requirement>,
}

impl LockSettings {
Expand All @@ -382,6 +385,7 @@ impl LockSettings {
Self {
python,
refresh: Refresh::from(refresh),
overrides_from_workspace: overrides_from_workspace(&filesystem),
settings: ResolverSettings::combine(resolver_options(resolver, build), filesystem),
}
}
Expand All @@ -392,6 +396,7 @@ impl LockSettings {
#[derive(Debug, Clone)]
pub(crate) struct AddSettings {
pub(crate) requirements: Vec<RequirementsSource>,
pub(crate) overrides_from_workspace: Vec<Requirement>,
pub(crate) dev: bool,
pub(crate) workspace: bool,
pub(crate) editable: Option<bool>,
Expand Down Expand Up @@ -439,6 +444,7 @@ impl AddSettings {
branch,
python,
refresh: Refresh::from(refresh),
overrides_from_workspace: overrides_from_workspace(&filesystem),
settings: ResolverInstallerSettings::combine(
resolver_installer_options(installer, build),
filesystem,
Expand All @@ -454,12 +460,13 @@ pub(crate) struct RemoveSettings {
pub(crate) requirements: Vec<PackageName>,
pub(crate) dev: bool,
pub(crate) python: Option<String>,
pub(crate) overrides_from_workspace: Vec<Requirement>,
}

impl RemoveSettings {
/// Resolve the [`RemoveSettings`] from the CLI and filesystem configuration.
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn resolve(args: RemoveArgs, _filesystem: Option<FilesystemOptions>) -> Self {
pub(crate) fn resolve(args: RemoveArgs, filesystem: Option<FilesystemOptions>) -> Self {
let RemoveArgs {
dev,
requirements,
Expand All @@ -470,6 +477,7 @@ impl RemoveSettings {
requirements,
dev,
python,
overrides_from_workspace: overrides_from_workspace(&filesystem),
}
}
}
Expand Down Expand Up @@ -536,19 +544,7 @@ impl PipCompileSettings {
compat_args: _,
} = args;

let overrides_from_workspace = if let Some(configuration) = &filesystem {
configuration
.override_dependencies
.clone()
.unwrap_or_default()
.into_iter()
.map(|requirement| {
Requirement::from(requirement.with_origin(RequirementOrigin::Workspace))
})
.collect()
} else {
Vec::new()
};
let overrides_from_workspace = overrides_from_workspace(&filesystem);

Self {
src_file,
Expand Down Expand Up @@ -733,19 +729,7 @@ impl PipInstallSettings {
compat_args: _,
} = args;

let overrides_from_workspace = if let Some(configuration) = &filesystem {
configuration
.override_dependencies
.clone()
.unwrap_or_default()
.into_iter()
.map(|requirement| {
Requirement::from(requirement.with_origin(RequirementOrigin::Workspace))
})
.collect()
} else {
Vec::new()
};
let overrides_from_workspace = overrides_from_workspace(&filesystem);

Self {
package,
Expand Down Expand Up @@ -2050,3 +2034,20 @@ fn resolver_installer_options(
no_binary_package: Some(no_binary_package),
}
}

/// Used by multiple commands to extract dependency overrides
fn overrides_from_workspace(filesystem: &Option<FilesystemOptions>) -> Vec<Requirement> {
if let Some(configuration) = &filesystem {
configuration
.override_dependencies
.clone()
.unwrap_or_default()
.into_iter()
.map(|requirement| {
Requirement::from(requirement.with_origin(RequirementOrigin::Workspace))
})
.collect()
} else {
Vec::new()
}
}
50 changes: 50 additions & 0 deletions crates/uv/tests/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,56 @@ fn lock_project_extra() -> Result<()> {
Ok(())
}

#[test]
fn lock_project_with_overrides() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(
r#"
[project]
name = "project"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["flask==3.0.0"]
[tool.uv]
override-dependencies = ["werkzeug==2.3.8"]
"#,
)?;

uv_snapshot!(context.filters(), context.lock(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv lock` is experimental and may change without warning.
Resolved 9 packages in [TIME]
"###);

// Install the base dependencies from the lockfile.
uv_snapshot!(context.filters(), context.sync(), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
warning: `uv sync` is experimental and may change without warning.
Prepared 8 packages in [TIME]
Installed 8 packages in [TIME]
+ blinker==1.7.0
+ click==8.1.7
+ flask==3.0.0
+ itsdangerous==2.1.2
+ jinja2==3.1.3
+ markupsafe==2.1.5
+ project==0.1.0 (from file://[TEMP_DIR]/)
+ werkzeug==2.3.8
"###);

Ok(())
}
/// Lock a project with a dependency that has an extra.
#[test]
fn lock_dependency_extra() -> Result<()> {
Expand Down

0 comments on commit 2ae60ae

Please sign in to comment.