Error when --group includes non-existent groups (#8394)

## Summary

Part of https://github.com/astral-sh/uv/pull/8272.
This commit is contained in:
Charlie Marsh 2024-10-21 22:08:31 -04:00 committed by Zanie Blue
parent 4d134a4ffe
commit c7ccf88939
6 changed files with 96 additions and 0 deletions

View file

@ -163,10 +163,12 @@ impl DevGroupsSpecification {
(self.dev.is_none() || self.dev.as_ref().is_some_and(DevMode::prod)) && self.groups.prod()
}
/// Returns the flag that was used to request development dependencies.
pub fn dev_mode(&self) -> Option<&DevMode> {
self.dev.as_ref()
}
/// Returns the list of groups to include.
pub fn groups(&self) -> &GroupsSpecification {
&self.groups
}

View file

@ -555,6 +555,11 @@ impl DependencyGroups {
self.0.get(group)
}
/// Returns `true` if the dependency group is in the list.
pub fn contains_key(&self, group: &GroupName) -> bool {
self.0.contains_key(group)
}
/// Returns an iterator over the dependency groups.
pub fn iter(&self) -> impl Iterator<Item = (&GroupName, &Vec<DependencyGroupSpecifier>)> {
self.0.iter()

View file

@ -70,6 +70,20 @@ pub(crate) async fn export(
VirtualProject::discover(project_dir, &DiscoveryOptions::default()).await?
};
// Validate the requested dependency groups.
for group in dev.groups().iter() {
if !project
.pyproject_toml()
.dependency_groups
.as_ref()
.is_some_and(|groups| groups.contains_key(group))
{
return Err(anyhow::anyhow!(
"Group `{group}` is not defined in the project's `dependency-group` table"
));
}
}
let VirtualProject::Project(project) = project else {
return Err(anyhow::anyhow!("Legacy non-project roots are not supported in `uv export`; add a `[project]` table to your `pyproject.toml` to enable exports"));
};

View file

@ -469,6 +469,20 @@ pub(crate) async fn run(
);
}
// Validate the requested dependency groups.
for group in dev.groups().iter() {
if !project
.pyproject_toml()
.dependency_groups
.as_ref()
.is_some_and(|groups| groups.contains_key(group))
{
return Err(anyhow::anyhow!(
"Group `{group}` is not defined in the project's `dependency-group` table"
));
}
}
let venv = if isolated {
debug!("Creating isolated virtual environment");

View file

@ -93,6 +93,20 @@ pub(crate) async fn sync(
warn_user!("Skipping installation of entry points (`project.scripts`) because this project is not packaged; to install entry points, set `tool.uv.package = true` or define a `build-system`");
}
// Validate the requested dependency groups.
for group in dev.groups().iter() {
if !project
.pyproject_toml()
.dependency_groups
.as_ref()
.is_some_and(|groups| groups.contains_key(group))
{
return Err(anyhow::anyhow!(
"Group `{group}` is not defined in the project's `dependency-group` table"
));
}
}
// Discover or create the virtual environment.
let venv = project::get_or_init_environment(
target.workspace(),

View file

@ -1226,6 +1226,53 @@ fn sync_dev_group() -> Result<()> {
Ok(())
}
#[test]
fn sync_non_existent_group() -> 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 = ["typing-extensions"]
[dependency-groups]
foo = []
bar = ["requests"]
"#,
)?;
context.lock().assert().success();
// Requesting a non-existent group should fail.
uv_snapshot!(context.filters(), context.sync().arg("--group").arg("baz"), @r###"
success: false
exit_code: 2
----- stdout -----
----- stderr -----
error: Group `baz` is not defined in the project's `dependency-group` table
"###);
// Requesting an empty group should succeed.
uv_snapshot!(context.filters(), context.sync().arg("--group").arg("foo"), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Resolved 7 packages in [TIME]
Prepared 1 package in [TIME]
Installed 1 package in [TIME]
+ typing-extensions==4.10.0
"###);
Ok(())
}
/// Regression test for <https://github.com/astral-sh/uv/issues/6316>.
///
/// Previously, we would read metadata statically from pyproject.toml and write that to `uv.lock`. In