Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make sure that environments are not completely overridden #2396

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading