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

feat: add nodefaults to conda envs #2097

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
67 changes: 63 additions & 4 deletions src/cli/project/export/conda_environment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::PathBuf;

use clap::Parser;
use itertools::Itertools;
use miette::{Context, IntoDiagnostic};
Expand All @@ -9,9 +7,10 @@ use pixi_manifest::{
FeaturesExt, HasFeaturesIter, PyPiRequirement,
};
use rattler_conda_types::{
EnvironmentYaml, MatchSpec, MatchSpecOrSubSection, ParseStrictness, Platform,
EnvironmentYaml, MatchSpec, MatchSpecOrSubSection, NamedChannelOrUrl, ParseStrictness, Platform,
};
use rattler_lock::FindLinksUrlOrPath;
use std::path::PathBuf;

use crate::project::Environment;
use crate::Project;
Expand Down Expand Up @@ -129,9 +128,11 @@ fn build_env_yaml(
platform: &Platform,
environment: &Environment,
) -> miette::Result<EnvironmentYaml> {
let channels =
channels_with_nodefaults(environment.channels().into_iter().cloned().collect_vec());
let mut env_yaml = rattler_conda_types::EnvironmentYaml {
name: Some(environment.name().as_str().to_string()),
channels: environment.channels().into_iter().cloned().collect_vec(),
channels,
..Default::default()
};

Expand Down Expand Up @@ -207,6 +208,18 @@ fn build_env_yaml(
Ok(env_yaml)
}

/// Add `nodefaults` channel if the environment doesn't have `main`, `r`, or `msys2`
fn channels_with_nodefaults(channels: Vec<NamedChannelOrUrl>) -> Vec<NamedChannelOrUrl> {
let mut channels = channels;
if !channels.iter().any(|channel| {
let channel = channel.as_str().to_lowercase();
channel == "main" || channel == "r" || channel == "msys2"
}) {
channels.push(NamedChannelOrUrl::Name("nodefaults".to_string()));
}
channels
}

pub async fn execute(project: Project, args: Args) -> miette::Result<()> {
let environment = project.environment_from_name_or_env_var(args.environment)?;
let platform = args.platform.unwrap_or_else(|| environment.best_platform());
Expand Down Expand Up @@ -359,4 +372,50 @@ mod tests {
env_yaml.unwrap().to_yaml_string()
);
}

#[test]
fn test_export_conda_env_yaml_with_defaults() {
let toml = r#"
[project]
name = "test"
channels = ["main"]
platforms = ["osx-64"]

[dependencies]
python = "3.9"
"#;
let project = Project::from_str(Path::new("pixi.toml"), toml).unwrap();
let args = Args {
output_path: None,
platform: Some(Platform::Osx64),
environment: None,
};
let environment = project
.environment_from_name_or_env_var(args.environment)
.unwrap();
let platform = args.platform.unwrap_or_else(|| environment.best_platform());

let env_yaml = build_env_yaml(&platform, &environment);
insta::assert_snapshot!(
"test_export_conda_env_yaml_with_defaults",
env_yaml.unwrap().to_yaml_string()
);
}

#[test]
fn test_channels_with_nodefaults() {
let channels = vec![NamedChannelOrUrl::Name("main".to_string())];
let channels = channels_with_nodefaults(channels);
assert_eq!(channels, vec![NamedChannelOrUrl::Name("main".to_string())]);

let channels = vec![NamedChannelOrUrl::Name("conda-forge".to_string())];
let channels = channels_with_nodefaults(channels);
assert_eq!(
channels,
vec![
NamedChannelOrUrl::Name("conda-forge".to_string()),
NamedChannelOrUrl::Name("nodefaults".to_string())
]
);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
source: src/cli/project/export/conda_environment.rs
assertion_line: 130
expression: env_yaml.unwrap().to_yaml_string()
---
name: default
channels:
- conda-forge
- nodefaults
dependencies:
- python >=3.12.5,<4
- pyyaml >=6.0.2,<7
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
source: src/cli/project/export/conda_environment.rs
assertion_line: 367
expression: env_yaml.unwrap().to_yaml_string()
---
name: default
channels:
- conda-forge
- nodefaults
dependencies:
- pytest *
- hatch ==1.12.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: src/cli/project/export/conda_environment.rs
expression: env_yaml.unwrap().to_yaml_string()
---
name: default
channels:
- main
dependencies:
- python ==3.9
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
source: src/cli/project/export/conda_environment.rs
assertion_line: 315
expression: env_yaml.unwrap().to_yaml_string()
---
name: alternative
channels:
- conda-forge
- nodefaults
dependencies:
- python ==3.12
- pip
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
source: src/cli/project/export/conda_environment.rs
assertion_line: 187
expression: env_yaml.unwrap().to_yaml_string()
---
name: default
channels:
- conda-forge
- nodefaults
dependencies:
- libclang ~=16.0.6
- numpy 1.26.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
source: src/cli/project/export/conda_environment.rs
assertion_line: 339
expression: env_yaml.unwrap().to_yaml_string()
---
name: default
channels:
- conda-forge
- nodefaults
dependencies:
- python ==3.12
- pip
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
source: src/cli/project/export/conda_environment.rs
assertion_line: 250
expression: env_yaml.unwrap().to_yaml_string()
---
name: default
channels:
- conda-forge
- nodefaults
dependencies:
- python *
- pip
Expand Down
Loading