Skip to content

Commit

Permalink
fix: Make sure that environments are not completely overridden (#2396)
Browse files Browse the repository at this point in the history
  • Loading branch information
hameerabbasi authored Nov 15, 2024
1 parent f0ef487 commit ff5ae24
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
11 changes: 9 additions & 2 deletions crates/pixi_manifest/src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,19 @@ impl Feature {
///
/// Returns `None` if this feature does not define any target with an
/// activation.
pub fn activation_env(&self, platform: Option<Platform>) -> Option<&IndexMap<String, String>> {
pub fn activation_env(&self, platform: Option<Platform>) -> IndexMap<String, String> {
self.targets
.resolve(platform)
.filter_map(|t| t.activation.as_ref())
.filter_map(|a| a.env.as_ref())
.next()
.fold(IndexMap::new(), |mut acc, x| {
for (k, v) in x {
if !acc.contains_key(k) {
acc.insert(k.clone(), v.clone());
}
}
acc
})
}

/// Returns true if the feature contains any reference to a pypi
Expand Down
72 changes: 58 additions & 14 deletions src/project/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<'p> Environment<'p> {
/// are defined for the environment.
pub(crate) fn activation_env(&self, platform: Option<Platform>) -> IndexMap<String, String> {
self.features()
.filter_map(|f| f.activation_env(platform))
.map(|f| f.activation_env(platform))
.fold(IndexMap::new(), |mut acc, env| {
acc.extend(env.iter().map(|(k, v)| (k.clone(), v.clone())));
acc
Expand Down Expand Up @@ -329,6 +329,7 @@ impl<'p> Hash for Environment<'p> {
mod tests {
use std::{collections::HashSet, path::Path};

use indexmap::indexmap;
use insta::assert_snapshot;
use itertools::Itertools;
use pixi_manifest::CondaDependencies;
Expand Down Expand Up @@ -493,23 +494,23 @@ mod tests {
let manifest = Project::from_str(
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
channels = []
platforms = ["linux-64", "osx-64"]
[project]
name = "foobar"
channels = []
platforms = ["linux-64", "osx-64"]
[activation]
scripts = ["default.bat"]
[activation]
scripts = ["default.bat"]
[target.linux-64.activation]
scripts = ["linux.bat"]
[target.linux-64.activation]
scripts = ["linux.bat"]
[feature.foo.activation]
scripts = ["foo.bat"]
[feature.foo.activation]
scripts = ["foo.bat"]
[environments]
foo = ["foo"]
"#,
[environments]
foo = ["foo"]
"#,
)
.unwrap();

Expand All @@ -524,6 +525,49 @@ mod tests {
);
}

#[test]
fn test_activation_env() {
let manifest = Project::from_str(
Path::new("pixi.toml"),
r#"
[project]
name = "foobar"
channels = []
platforms = ["linux-64", "osx-64"]
[activation.env]
DEFAULT_VAR = "1"
[target.linux-64.activation.env]
LINUX_VAR = "1"
[feature.foo.activation.env]
FOO_VAR = "1"
[environments]
foo = ["foo"]
"#,
)
.unwrap();

let default_env = manifest.default_environment();
let foo_env = manifest.environment("foo").unwrap();
assert_eq!(
foo_env.activation_env(None),
indexmap! {
"FOO_VAR".to_string() => "1".to_string(),
"DEFAULT_VAR".to_string() => "1".to_string(),
}
);
assert_eq!(
default_env.activation_env(Some(Platform::Linux64)),
indexmap! {
"LINUX_VAR".to_string() => "1".to_string(),
"DEFAULT_VAR".to_string() => "1".to_string(),
}
);
}

#[test]
fn test_channel_feature_priority() {
let manifest = Project::from_str(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/project/mod.rs
expression: "format!(\"= Linux64\\n{}\\n\\n= Win64\\n{}\\n\\n= OsxArm64\\n{}\",\n fmt_activation_scripts(project.activation_scripts(Platform ::\n Linux64).unwrap()),\n fmt_activation_scripts(project.activation_scripts(Platform ::\n Win64).unwrap()),\n fmt_activation_scripts(project.activation_scripts(Platform ::\n OsxArm64).unwrap()))"
expression: "format!(\"= Linux64\\n{}\\n\\n= Win64\\n{}\\n\\n= OsxArm64\\n{}\",\nfmt_activation_scripts(project.default_environment().activation_scripts(Some(Platform::Linux64))),\nfmt_activation_scripts(project.default_environment().activation_scripts(Some(Platform::Win64))),\nfmt_activation_scripts(project.default_environment().activation_scripts(Some(Platform::OsxArm64))))"
snapshot_kind: text
---
= Linux64
Cargo.toml
Expand Down

0 comments on commit ff5ae24

Please sign in to comment.