From 83a23dfc301cd2177a8b254a2be0ca2c6fc0dc25 Mon Sep 17 00:00:00 2001 From: Olivier Lacroix Date: Mon, 11 Nov 2024 11:31:26 +1100 Subject: [PATCH] Handle dependency groups in init --- crates/pixi_manifest/src/pyproject.rs | 28 ++++++++++++++++++--------- src/cli/init.rs | 7 ++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/crates/pixi_manifest/src/pyproject.rs b/crates/pixi_manifest/src/pyproject.rs index c6c116920..300fc3b00 100644 --- a/crates/pixi_manifest/src/pyproject.rs +++ b/crates/pixi_manifest/src/pyproject.rs @@ -196,15 +196,14 @@ impl PyProjectManifest { self.dependency_groups.as_ref().map(|dg| dg.resolve()) } - /// Builds a list of pixi environments from pyproject groups of extra - /// dependencies: - /// - one environment is created per group of extra, with the same name as - /// the group of extra - /// - each environment includes the feature of the same name as the group - /// of extra + /// Builds a list of pixi environments from pyproject groups of optional + /// dependencies and/or dependency groups: + /// - one environment is created per group with the same name + /// - each environment includes the feature of the same name /// - it will also include other features inferred from any self references - /// to other groups of extras - pub fn environments_from_extras(&self) -> HashMap> { + /// to other groups of optional dependencies (but won't for dependency groups, + /// as recursion between groups is resolved upstream) + pub fn environments_from_extras(&self) -> Result>, Pep735Error> { let mut environments = HashMap::new(); if let Some(extras) = self.optional_dependencies() { let pname = self.package_name(); @@ -222,7 +221,18 @@ impl PyProjectManifest { environments.insert(extra.replace('_', "-").clone(), features); } } - environments + + if let Some(groups) = self.dependency_groups().transpose()? { + for group in groups.into_keys() { + let normalised = group.replace('_', "-"); + // Nothing to do if a group of optional dependencies has the same name as the dependency group + if environments.contains_key(&normalised) { + environments.insert(normalised.clone(), vec![normalised]); + } + } + } + + Ok(environments) } } diff --git a/src/cli/init.rs b/src/cli/init.rs index 3b5a1a9af..499b27099 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -313,7 +313,7 @@ pub async fn execute(args: Args) -> miette::Result<()> { Some(name) => (name, false), None => (default_name.clone(), true), }; - let environments = pyproject.environments_from_extras(); + let environments = pyproject.environments_from_extras().into_diagnostic()?; let rv = env .render_named_str( consts::PYPROJECT_MANIFEST, @@ -346,11 +346,12 @@ pub async fn execute(args: Args) -> miette::Result<()> { console::style(console::Emoji("✔ ", "")).green(), name ); - // Inform about the addition of environments from extras (if any) + // Inform about the addition of environments from optional dependencies + // or dependency groups (if any) if !environments.is_empty() { let envs: Vec<&str> = environments.keys().map(AsRef::as_ref).collect(); eprintln!( - "{}Added environment{} '{}' from optional extras.", + "{}Added environment{} '{}' from optional dependencies or dependency groups.", console::style(console::Emoji("✔ ", "")).green(), if envs.len() > 1 { "s" } else { "" }, envs.join("', '")