From 9a6eafc0431da27d6aa4d1c5a91ed80c162774b9 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 4 Nov 2025 09:17:58 -0500 Subject: [PATCH] Accept multiple packages in `uv sync` (#16543) ## Summary Closes https://github.com/astral-sh/uv/issues/12130. --------- Co-authored-by: Zanie Blue Co-authored-by: konsti --- crates/uv-cli/src/lib.rs | 8 +- .../uv/src/commands/project/install_target.rs | 132 +++++++++++------- crates/uv/src/commands/project/mod.rs | 4 +- crates/uv/src/commands/project/sync.rs | 80 +++++++---- crates/uv/src/settings.rs | 2 +- crates/uv/tests/it/lock.rs | 10 ++ crates/uv/tests/it/sync.rs | 109 +++++++++++++++ docs/reference/cli.md | 6 +- 8 files changed, 265 insertions(+), 86 deletions(-) diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index c8c4c6637..db91e93a4 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -3669,14 +3669,14 @@ pub struct SyncArgs { #[arg(long, conflicts_with = "package")] pub all_packages: bool, - /// Sync for a specific package in the workspace. + /// Sync for specific packages in the workspace. /// /// The workspace's environment (`.venv`) is updated to reflect the subset of dependencies - /// declared by the specified workspace member package. + /// declared by the specified workspace member packages. /// - /// If the workspace member does not exist, uv will exit with an error. + /// If any workspace member does not exist, uv will exit with an error. #[arg(long, conflicts_with = "all_packages")] - pub package: Option, + pub package: Vec, /// Sync the environment for a Python script, rather than the current project. /// diff --git a/crates/uv/src/commands/project/install_target.rs b/crates/uv/src/commands/project/install_target.rs index dc7f08658..d11ae2738 100644 --- a/crates/uv/src/commands/project/install_target.rs +++ b/crates/uv/src/commands/project/install_target.rs @@ -26,6 +26,12 @@ pub(crate) enum InstallTarget<'lock> { name: &'lock PackageName, lock: &'lock Lock, }, + /// Multiple specific projects in a workspace. + Projects { + workspace: &'lock Workspace, + names: &'lock [PackageName], + lock: &'lock Lock, + }, /// An entire workspace. Workspace { workspace: &'lock Workspace, @@ -47,6 +53,7 @@ impl<'lock> Installable<'lock> for InstallTarget<'lock> { fn install_path(&self) -> &'lock Path { match self { Self::Project { workspace, .. } => workspace.install_path(), + Self::Projects { workspace, .. } => workspace.install_path(), Self::Workspace { workspace, .. } => workspace.install_path(), Self::NonProjectWorkspace { workspace, .. } => workspace.install_path(), Self::Script { script, .. } => script.path.parent().unwrap(), @@ -56,36 +63,38 @@ impl<'lock> Installable<'lock> for InstallTarget<'lock> { fn lock(&self) -> &'lock Lock { match self { Self::Project { lock, .. } => lock, + Self::Projects { lock, .. } => lock, Self::Workspace { lock, .. } => lock, Self::NonProjectWorkspace { lock, .. } => lock, Self::Script { lock, .. } => lock, } } - fn roots(&self) -> impl Iterator { + #[allow(refining_impl_trait)] + fn roots(&self) -> Box + '_> { match self { - Self::Project { name, .. } => Either::Left(Either::Left(std::iter::once(*name))), - Self::NonProjectWorkspace { lock, .. } => { - Either::Left(Either::Right(lock.members().iter())) - } + Self::Project { name, .. } => Box::new(std::iter::once(*name)), + Self::Projects { names, .. } => Box::new(names.iter()), + Self::NonProjectWorkspace { lock, .. } => Box::new(lock.members().iter()), Self::Workspace { lock, .. } => { // Identify the workspace members. // // The members are encoded directly in the lockfile, unless the workspace contains a // single member at the root, in which case, we identify it by its source. if lock.members().is_empty() { - Either::Right(Either::Left(lock.root().into_iter().map(Package::name))) + Box::new(lock.root().into_iter().map(Package::name)) } else { - Either::Left(Either::Right(lock.members().iter())) + Box::new(lock.members().iter()) } } - Self::Script { .. } => Either::Right(Either::Right(std::iter::empty())), + Self::Script { .. } => Box::new(std::iter::empty()), } } fn project_name(&self) -> Option<&PackageName> { match self { Self::Project { name, .. } => Some(name), + Self::Projects { .. } => None, Self::Workspace { .. } => None, Self::NonProjectWorkspace { .. } => None, Self::Script { .. } => None, @@ -98,6 +107,7 @@ impl<'lock> InstallTarget<'lock> { pub(crate) fn indexes(self) -> impl Iterator { match self { Self::Project { workspace, .. } + | Self::Projects { workspace, .. } | Self::Workspace { workspace, .. } | Self::NonProjectWorkspace { workspace, .. } => { Either::Left(workspace.indexes().iter().chain( @@ -130,6 +140,7 @@ impl<'lock> InstallTarget<'lock> { pub(crate) fn sources(&self) -> impl Iterator { match self { Self::Project { workspace, .. } + | Self::Projects { workspace, .. } | Self::Workspace { workspace, .. } | Self::NonProjectWorkspace { workspace, .. } => { Either::Left(workspace.sources().values().flat_map(Sources::iter).chain( @@ -158,6 +169,7 @@ impl<'lock> InstallTarget<'lock> { ) -> impl Iterator>> { match self { Self::Project { workspace, .. } + | Self::Projects { workspace, .. } | Self::Workspace { workspace, .. } | Self::NonProjectWorkspace { workspace, .. } => { Either::Left( @@ -256,6 +268,7 @@ impl<'lock> InstallTarget<'lock> { } match self { Self::Project { lock, .. } + | Self::Projects { lock, .. } | Self::Workspace { lock, .. } | Self::NonProjectWorkspace { lock, .. } => { if !lock.supports_provides_extra() { @@ -281,7 +294,10 @@ impl<'lock> InstallTarget<'lock> { Self::Project { .. } => { Err(ProjectError::MissingExtraProject(extra.clone())) } - _ => Err(ProjectError::MissingExtraWorkspace(extra.clone())), + Self::Projects { .. } => { + Err(ProjectError::MissingExtraProjects(extra.clone())) + } + _ => Err(ProjectError::MissingExtraProjects(extra.clone())), }; } } @@ -337,11 +353,11 @@ impl<'lock> InstallTarget<'lock> { for group in groups.explicit_names() { if !known_groups.contains(group) { - return Err(ProjectError::MissingGroupWorkspace(group.clone())); + return Err(ProjectError::MissingGroupProjects(group.clone())); } } } - Self::Project { lock, .. } => { + Self::Project { lock, .. } | Self::Projects { lock, .. } => { let roots = self.roots().collect::>(); let member_packages: Vec<&Package> = lock .packages() @@ -349,7 +365,7 @@ impl<'lock> InstallTarget<'lock> { .filter(|package| roots.contains(package.name())) .collect(); - // Extract the dependency groups defined in the relevant member. + // Extract the dependency groups defined in the relevant member(s). let known_groups = member_packages .iter() .flat_map(|package| package.dependency_groups().keys()) @@ -357,7 +373,15 @@ impl<'lock> InstallTarget<'lock> { for group in groups.explicit_names() { if !known_groups.contains(group) { - return Err(ProjectError::MissingGroupProject(group.clone())); + return match self { + Self::Project { .. } => { + Err(ProjectError::MissingGroupProject(group.clone())) + } + Self::Projects { .. } => { + Err(ProjectError::MissingGroupProjects(group.clone())) + } + _ => unreachable!(), + }; } } } @@ -380,59 +404,71 @@ impl<'lock> InstallTarget<'lock> { groups: &DependencyGroupsWithDefaults, ) -> BTreeSet<&PackageName> { match self { - Self::Project { name, lock, .. } => { - // Collect the packages by name for efficient lookup + Self::Project { lock, .. } | Self::Projects { lock, .. } => { + let roots = self.roots().collect::>(); + + // Collect the packages by name for efficient lookup. let packages = lock .packages() .iter() - .map(|p| (p.name(), p)) + .map(|package| (package.name(), package)) .collect::>(); - // We'll include the project itself + // We'll include all specified projects let mut required_members = BTreeSet::new(); - required_members.insert(*name); + for name in &roots { + required_members.insert(*name); + } - // Find all workspace member dependencies recursively + // Find all workspace member dependencies recursively for all specified packages let mut queue: VecDeque<(&PackageName, Option<&ExtraName>)> = VecDeque::new(); let mut seen: FxHashSet<(&PackageName, Option<&ExtraName>)> = FxHashSet::default(); - let Some(root_package) = packages.get(name) else { - return required_members; - }; - - if groups.prod() { - // Add the root package - queue.push_back((name, None)); - seen.insert((name, None)); - - // Add explicitly activated extras for the root package - for extra in extras.extra_names(root_package.optional_dependencies().keys()) { - if seen.insert((name, Some(extra))) { - queue.push_back((name, Some(extra))); - } - } - } - - // Add activated dependency groups for the root package - for (group_name, dependencies) in root_package.resolved_dependency_groups() { - if !groups.contains(group_name) { + for name in roots { + let Some(root_package) = packages.get(name) else { continue; + }; + + if groups.prod() { + // Add the root package + if seen.insert((name, None)) { + queue.push_back((name, None)); + } + + // Add explicitly activated extras for the root package + for extra in extras.extra_names(root_package.optional_dependencies().keys()) + { + if seen.insert((name, Some(extra))) { + queue.push_back((name, Some(extra))); + } + } } - for dependency in dependencies { - let name = dependency.package_name(); - queue.push_back((name, None)); - for extra in dependency.extra() { - queue.push_back((name, Some(extra))); + + // Add activated dependency groups for the root package + for (group_name, dependencies) in root_package.resolved_dependency_groups() { + if !groups.contains(group_name) { + continue; + } + for dependency in dependencies { + let dep_name = dependency.package_name(); + if seen.insert((dep_name, None)) { + queue.push_back((dep_name, None)); + } + for extra in dependency.extra() { + if seen.insert((dep_name, Some(extra))) { + queue.push_back((dep_name, Some(extra))); + } + } } } } - while let Some((pkg_name, extra)) = queue.pop_front() { - if lock.members().contains(pkg_name) { - required_members.insert(pkg_name); + while let Some((package_name, extra)) = queue.pop_front() { + if lock.members().contains(package_name) { + required_members.insert(package_name); } - let Some(package) = packages.get(pkg_name) else { + let Some(package) = packages.get(package_name) else { continue; }; diff --git a/crates/uv/src/commands/project/mod.rs b/crates/uv/src/commands/project/mod.rs index 5feddcf8f..4336ee33f 100644 --- a/crates/uv/src/commands/project/mod.rs +++ b/crates/uv/src/commands/project/mod.rs @@ -161,7 +161,7 @@ pub(crate) enum ProjectError { MissingGroupProject(GroupName), #[error("Group `{0}` is not defined in any project's `dependency-groups` table")] - MissingGroupWorkspace(GroupName), + MissingGroupProjects(GroupName), #[error("PEP 723 scripts do not support dependency groups, but group `{0}` was specified")] MissingGroupScript(GroupName), @@ -175,7 +175,7 @@ pub(crate) enum ProjectError { MissingExtraProject(ExtraName), #[error("Extra `{0}` is not defined in any project's `optional-dependencies` table")] - MissingExtraWorkspace(ExtraName), + MissingExtraProjects(ExtraName), #[error("PEP 723 scripts do not support optional dependencies, but extra `{0}` was specified")] MissingExtraScript(ExtraName), diff --git a/crates/uv/src/commands/project/sync.rs b/crates/uv/src/commands/project/sync.rs index 1225f3757..833c560fd 100644 --- a/crates/uv/src/commands/project/sync.rs +++ b/crates/uv/src/commands/project/sync.rs @@ -63,7 +63,7 @@ pub(crate) async fn sync( dry_run: DryRun, active: Option, all_packages: bool, - package: Option, + package: Vec, extras: ExtrasSpecification, groups: DependencyGroups, editable: Option, @@ -109,16 +109,28 @@ pub(crate) async fn sync( &workspace_cache, ) .await? - } else if let Some(package) = package.as_ref() { + } else if let [name] = package.as_slice() { VirtualProject::Project( Workspace::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache) .await? - .with_current_project(package.clone()) - .with_context(|| format!("Package `{package}` not found in workspace"))?, + .with_current_project(name.clone()) + .with_context(|| format!("Package `{name}` not found in workspace"))?, ) } else { - VirtualProject::discover(project_dir, &DiscoveryOptions::default(), &workspace_cache) - .await? + let project = VirtualProject::discover( + project_dir, + &DiscoveryOptions::default(), + &workspace_cache, + ) + .await?; + + for name in &package { + if !project.workspace().packages().contains_key(name) { + return Err(anyhow::anyhow!("Package `{name}` not found in workspace")); + } + } + + project }; // TODO(lucab): improve warning content @@ -379,8 +391,7 @@ pub(crate) async fn sync( } // Identify the installation target. - let sync_target = - identify_installation_target(&target, outcome.lock(), all_packages, package.as_ref()); + let sync_target = identify_installation_target(&target, outcome.lock(), all_packages, &package); let state = state.fork(); @@ -459,7 +470,7 @@ fn identify_installation_target<'a>( target: &'a SyncTarget, lock: &'a Lock, all_packages: bool, - package: Option<&'a PackageName>, + package: &'a [PackageName], ) -> InstallTarget<'a> { match &target { SyncTarget::Project(project) => { @@ -470,33 +481,45 @@ fn identify_installation_target<'a>( workspace: project.workspace(), lock, } - } else if let Some(package) = package { - InstallTarget::Project { - workspace: project.workspace(), - name: package, - lock, - } } else { - // By default, install the root package. - InstallTarget::Project { - workspace: project.workspace(), - name: project.project_name(), - lock, + match package { + // By default, install the root project. + [] => InstallTarget::Project { + workspace: project.workspace(), + name: project.project_name(), + lock, + }, + [name] => InstallTarget::Project { + workspace: project.workspace(), + name, + lock, + }, + names => InstallTarget::Projects { + workspace: project.workspace(), + names, + lock, + }, } } } VirtualProject::NonProject(workspace) => { if all_packages { InstallTarget::NonProjectWorkspace { workspace, lock } - } else if let Some(package) = package { - InstallTarget::Project { - workspace, - name: package, - lock, - } } else { - // By default, install the entire workspace. - InstallTarget::NonProjectWorkspace { workspace, lock } + match package { + // By default, install the entire workspace. + [] => InstallTarget::NonProjectWorkspace { workspace, lock }, + [name] => InstallTarget::Project { + workspace, + name, + lock, + }, + names => InstallTarget::Projects { + workspace, + names, + lock, + }, + } } } } @@ -613,6 +636,7 @@ pub(super) async fn do_sync( let extra_build_requires = match &target { InstallTarget::Workspace { workspace, .. } | InstallTarget::Project { workspace, .. } + | InstallTarget::Projects { workspace, .. } | InstallTarget::NonProjectWorkspace { workspace, .. } => { LoweredExtraBuildDependencies::from_workspace( extra_build_dependencies.clone(), diff --git a/crates/uv/src/settings.rs b/crates/uv/src/settings.rs index 4f44b36eb..e2a8e7d92 100644 --- a/crates/uv/src/settings.rs +++ b/crates/uv/src/settings.rs @@ -1311,7 +1311,7 @@ pub(crate) struct SyncSettings { pub(crate) install_options: InstallOptions, pub(crate) modifications: Modifications, pub(crate) all_packages: bool, - pub(crate) package: Option, + pub(crate) package: Vec, pub(crate) python: Option, pub(crate) python_platform: Option, pub(crate) install_mirrors: PythonInstallMirrors, diff --git a/crates/uv/tests/it/lock.rs b/crates/uv/tests/it/lock.rs index e2fb3ebb7..6e533beb8 100644 --- a/crates/uv/tests/it/lock.rs +++ b/crates/uv/tests/it/lock.rs @@ -3260,6 +3260,16 @@ fn lock_conflicting_workspace_members() -> Result<()> { error: Package `example` and package `subexample` are incompatible with the declared conflicts: {example, subexample} "); + // Attempt to install them together, i.e., with `--package` + uv_snapshot!(context.filters(), context.sync().arg("--frozen").arg("--package").arg("example").arg("--package").arg("subexample"), @r" + success: false + exit_code: 2 + ----- stdout ----- + + ----- stderr ----- + error: Package `example` and package `subexample` are incompatible with the declared conflicts: {example, subexample} + "); + Ok(()) } diff --git a/crates/uv/tests/it/sync.rs b/crates/uv/tests/it/sync.rs index 3178edc1f..46095a838 100644 --- a/crates/uv/tests/it/sync.rs +++ b/crates/uv/tests/it/sync.rs @@ -268,6 +268,115 @@ fn package() -> Result<()> { Ok(()) } +/// Sync multiple packages within a workspace. +#[test] +fn multiple_packages() -> Result<()> { + let context = TestContext::new("3.12"); + + let pyproject_toml = context.temp_dir.child("pyproject.toml"); + pyproject_toml.write_str( + r#" + [project] + name = "root" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["foo", "bar", "baz"] + + [tool.uv.sources] + foo = { workspace = true } + bar = { workspace = true } + baz = { workspace = true } + + [tool.uv.workspace] + members = ["packages/*"] + "#, + )?; + + context + .temp_dir + .child("packages") + .child("foo") + .child("pyproject.toml") + .write_str( + r#" + [project] + name = "foo" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["anyio"] + "#, + )?; + + context + .temp_dir + .child("packages") + .child("bar") + .child("pyproject.toml") + .write_str( + r#" + [project] + name = "bar" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["typing-extensions"] + "#, + )?; + + context + .temp_dir + .child("packages") + .child("baz") + .child("pyproject.toml") + .write_str( + r#" + [project] + name = "baz" + version = "0.1.0" + requires-python = ">=3.12" + dependencies = ["iniconfig"] + "#, + )?; + + // Sync `foo` and `bar`. + uv_snapshot!(context.filters(), context.sync() + .arg("--package").arg("foo") + .arg("--package").arg("bar"), @r" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 9 packages in [TIME] + Prepared 6 packages in [TIME] + Installed 6 packages in [TIME] + + anyio==4.3.0 + + bar==0.1.0 (from file://[TEMP_DIR]/packages/bar) + + foo==0.1.0 (from file://[TEMP_DIR]/packages/foo) + + idna==3.6 + + sniffio==1.3.1 + + typing-extensions==4.10.0 + "); + + // Sync `foo`, `bar`, and `baz`. + uv_snapshot!(context.filters(), context.sync() + .arg("--package").arg("foo") + .arg("--package").arg("bar") + .arg("--package").arg("baz"), @r" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Resolved 9 packages in [TIME] + Prepared 2 packages in [TIME] + Installed 2 packages in [TIME] + + baz==0.1.0 (from file://[TEMP_DIR]/packages/baz) + + iniconfig==2.0.0 + "); + + Ok(()) +} + /// Test json output #[test] fn sync_json() -> Result<()> { diff --git a/docs/reference/cli.md b/docs/reference/cli.md index ebb9bc5dd..a6295e781 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -1513,9 +1513,9 @@ uv sync [OPTIONS]
  • text: Display the result in a human-readable format
  • json: Display the result in JSON format
  • -
--package package

Sync for a specific package in the workspace.

-

The workspace's environment (.venv) is updated to reflect the subset of dependencies declared by the specified workspace member package.

-

If the workspace member does not exist, uv will exit with an error.

+
--package package

Sync for specific packages in the workspace.

+

The workspace's environment (.venv) is updated to reflect the subset of dependencies declared by the specified workspace member packages.

+

If any workspace member does not exist, uv will exit with an error.

--prerelease prerelease

The strategy to use when considering pre-release versions.

By default, uv will accept pre-releases for packages that only publish pre-releases, along with first-party requirements that contain an explicit pre-release marker in the declared specifiers (if-necessary-or-explicit).

May also be set with the UV_PRERELEASE environment variable.

Possible values: