Skip to content

Commit

Permalink
Handle dependency groups in init
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lacroix committed Nov 11, 2024
1 parent cf6c9ca commit 83a23df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
28 changes: 19 additions & 9 deletions crates/pixi_manifest/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Vec<String>> {
/// 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<HashMap<String, Vec<String>>, Pep735Error> {
let mut environments = HashMap::new();
if let Some(extras) = self.optional_dependencies() {
let pname = self.package_name();
Expand All @@ -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)
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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("', '")
Expand Down

0 comments on commit 83a23df

Please sign in to comment.