Skip to content

Commit

Permalink
Avoid error for --group defined in non-root workspace member (#8734)
Browse files Browse the repository at this point in the history
## Summary

Closes #8722.
  • Loading branch information
charliermarsh authored Oct 31, 2024
1 parent 24ad43e commit 5a46b5e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) async fn export(
};

// Determine the default groups to include.
validate_dependency_groups(project.pyproject_toml(), &dev)?;
validate_dependency_groups(&project, &dev)?;
let defaults = default_dependency_groups(project.pyproject_toml())?;

let VirtualProject::Project(project) = project else {
Expand Down
47 changes: 38 additions & 9 deletions crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use uv_types::{BuildIsolation, EmptyInstalledPackages, HashStrategy};
use uv_warnings::{warn_user, warn_user_once};
use uv_workspace::dependency_groups::DependencyGroupError;
use uv_workspace::pyproject::PyProjectToml;
use uv_workspace::Workspace;
use uv_workspace::{VirtualProject, Workspace};

use crate::commands::pip::loggers::{InstallLogger, ResolveLogger};
use crate::commands::pip::operations::{Changelog, Modifications};
Expand Down Expand Up @@ -126,7 +126,10 @@ pub(crate) enum ProjectError {
),

#[error("Group `{0}` is not defined in the project's `dependency-group` table")]
MissingGroup(GroupName),
MissingGroupProject(GroupName),

#[error("Group `{0}` is not defined in any project's `dependency-group` table")]
MissingGroupWorkspace(GroupName),

#[error("Default group `{0}` (from `tool.uv.default-groups`) is not defined in the project's `dependency-group` table")]
MissingDefaultGroup(GroupName),
Expand Down Expand Up @@ -1366,20 +1369,46 @@ pub(crate) async fn script_python_requirement(
/// Validate the dependency groups requested by the [`DevGroupsSpecification`].
#[allow(clippy::result_large_err)]
pub(crate) fn validate_dependency_groups(
pyproject_toml: &PyProjectToml,
project: &VirtualProject,
dev: &DevGroupsSpecification,
) -> Result<(), ProjectError> {
for group in dev
.groups()
.into_iter()
.flat_map(GroupsSpecification::names)
{
if !pyproject_toml
.dependency_groups
.as_ref()
.is_some_and(|groups| groups.contains_key(group))
{
return Err(ProjectError::MissingGroup(group.clone()));
match project {
VirtualProject::Project(project) => {
// The group must be defined in the target project.
if !project
.current_project()
.pyproject_toml()
.dependency_groups
.as_ref()
.is_some_and(|groups| groups.contains_key(group))
{
return Err(ProjectError::MissingGroupProject(group.clone()));
}
}
VirtualProject::NonProject(workspace) => {
// The group must be defined in at least one workspace package.
if !workspace
.pyproject_toml()
.dependency_groups
.as_ref()
.is_some_and(|groups| groups.contains_key(group))
{
if workspace.packages().values().all(|package| {
!package
.pyproject_toml()
.dependency_groups
.as_ref()
.is_some_and(|groups| groups.contains_key(group))
}) {
return Err(ProjectError::MissingGroupWorkspace(group.clone()));
}
}
}
}
}
Ok(())
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 @@ -551,7 +551,7 @@ pub(crate) async fn run(
}
} else {
// Determine the default groups to include.
validate_dependency_groups(project.pyproject_toml(), &dev)?;
validate_dependency_groups(&project, &dev)?;
let defaults = default_dependency_groups(project.pyproject_toml())?;

// Determine the lock mode.
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub(crate) async fn sync(
}

// Determine the default groups to include.
validate_dependency_groups(project.pyproject_toml(), &dev)?;
validate_dependency_groups(&project, &dev)?;
let defaults = default_dependency_groups(project.pyproject_toml())?;

// Discover or create the virtual environment.
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/src/commands/project/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use uv_configuration::{Concurrency, DevGroupsSpecification, LowerBound, TargetTr
use uv_pep508::PackageName;
use uv_python::{PythonDownloads, PythonPreference, PythonRequest, PythonVersion};
use uv_resolver::TreeDisplay;
use uv_workspace::{DiscoveryOptions, Workspace};
use uv_workspace::{DiscoveryOptions, VirtualProject, Workspace};

use crate::commands::pip::loggers::DefaultResolveLogger;
use crate::commands::pip::resolution_markers;
Expand Down Expand Up @@ -50,7 +50,7 @@ pub(crate) async fn tree(
let workspace = Workspace::discover(project_dir, &DiscoveryOptions::default()).await?;

// Determine the default groups to include.
validate_dependency_groups(workspace.pyproject_toml(), &dev)?;
validate_dependency_groups(&VirtualProject::NonProject(workspace.clone()), &dev)?;
let defaults = default_dependency_groups(workspace.pyproject_toml())?;

// Find an interpreter for the project, unless `--frozen` and `--universal` are both set.
Expand Down
26 changes: 26 additions & 0 deletions crates/uv/tests/it/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ fn sync_legacy_non_project_group() -> Result<()> {
requires-python = ">=3.12"
dependencies = ["iniconfig>1"]
[dependency-groups]
baz = ["typing-extensions"]
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
Expand Down Expand Up @@ -559,6 +562,29 @@ fn sync_legacy_non_project_group() -> Result<()> {
+ typing-extensions==4.10.0
"###);

uv_snapshot!(context.filters(), context.sync().arg("--group").arg("baz"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 6 packages in [TIME]
Uninstalled 1 package in [TIME]
Installed 2 packages in [TIME]
+ child==0.1.0 (from file://[TEMP_DIR]/child)
+ iniconfig==2.0.0
- typing-extensions==4.10.0
"###);

uv_snapshot!(context.filters(), context.sync().arg("--group").arg("bop"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: Group `bop` is not defined in any project's `dependency-group` table
"###);

Ok(())
}

Expand Down

0 comments on commit 5a46b5e

Please sign in to comment.