diff --git a/Cargo.lock b/Cargo.lock index 5271e598a2..1c4f50eeee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6288,6 +6288,7 @@ dependencies = [ "rstest", "same-file", "serde", + "temp-env", "tempfile", "thiserror 2.0.21", "tokio", @@ -6988,6 +6989,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "serde_with", "tempfile", "thiserror 2.0.21", "tokio", diff --git a/crates/pixi_api/Cargo.toml b/crates/pixi_api/Cargo.toml index 8c37326d43..fdd6177167 100644 --- a/crates/pixi_api/Cargo.toml +++ b/crates/pixi_api/Cargo.toml @@ -61,6 +61,7 @@ which = { workspace = true } insta = { workspace = true } pixi_test_utils = { workspace = true } rstest = { workspace = true } +temp-env = { workspace = true } tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } [target."cfg(unix)".dependencies] diff --git a/crates/pixi_api/src/context.rs b/crates/pixi_api/src/context.rs index f63ca5348d..cd6c2196b7 100644 --- a/crates/pixi_api/src/context.rs +++ b/crates/pixi_api/src/context.rs @@ -16,7 +16,7 @@ use pixi_pypi_spec::{PixiPypiSpec, PypiPackageName}; use pixi_spec::PixiSpec; use rattler_conda_types::{Channel, MatchSpec, NamedChannelOrUrl, PackageName, Subdir}; -use crate::global::{GlobalEnvironment, ListError}; +use crate::global::{GlobalEnvironment, InstallError, InstallOptions, ListError, UninstallError}; use crate::interface::Interface; use crate::workspace::add::GitOptions; use crate::workspace::{ @@ -85,6 +85,23 @@ impl GlobalContext { ) -> Result { crate::global::list::get(&self.project, name).await } + + pub async fn install_environment( + &mut self, + env_name: &pixi_global::EnvironmentName, + specs: &[pixi_global::project::GlobalSpec], + options: &InstallOptions, + ) -> Result { + crate::global::install::install_environment(&mut self.project, env_name, specs, options) + .await + } + + pub async fn uninstall_environment( + &mut self, + env_name: &pixi_global::EnvironmentName, + ) -> Result { + crate::global::uninstall::uninstall_environment(&mut self.project, env_name).await + } } pub struct WorkspaceContext { diff --git a/crates/pixi_api/src/global/install.rs b/crates/pixi_api/src/global/install.rs new file mode 100644 index 0000000000..c88651a918 --- /dev/null +++ b/crates/pixi_api/src/global/install.rs @@ -0,0 +1,250 @@ +use std::ops::Not; + +use itertools::Itertools; +use rattler_conda_types::{MatchSpec, NamedChannelOrUrl, Subdir}; +use serde::{Deserialize, Serialize}; + +use pixi_global::{ + EnvironmentName, Mapping, Project, StateChange, StateChanges, + common::{GlobalDirError, contains_menuinst_document}, + project::{ + EnvironmentInSyncError, ExposeError, ExposedType, FromMatchSpecError, GlobalSpec, + InstallEnvironmentError, ManifestError, RemoveEnvironmentError, ShortcutsError, + }, +}; +use pixi_utils::prefix::PrefixError; + +use crate::global::utils::{eventual_environment_channels, revert_environment_after_error}; + +/// An error from installing packages into a global environment. +#[derive(Debug, thiserror::Error, miette::Diagnostic)] +pub enum InstallError { + #[error(transparent)] + #[diagnostic(transparent)] + Manifest(#[from] ManifestError), + + #[error(transparent)] + #[diagnostic(transparent)] + RemoveEnvironment(#[from] Box), + + #[error(transparent)] + #[diagnostic(transparent)] + FromMatchSpec(#[from] FromMatchSpecError), + + #[error("could not find exact package name in MatchSpec {0}")] + MissingPackageName(String), + + #[error(transparent)] + #[diagnostic(transparent)] + InSync(#[from] EnvironmentInSyncError), + + #[error(transparent)] + #[diagnostic(transparent)] + InstallEnvironment(#[from] InstallEnvironmentError), + + #[error(transparent)] + #[diagnostic(transparent)] + Expose(#[from] ExposeError), + + #[error(transparent)] + #[diagnostic(transparent)] + GlobalDir(#[from] GlobalDirError), + + #[error(transparent)] + #[diagnostic(transparent)] + Prefix(#[from] PrefixError), + + #[error(transparent)] + #[diagnostic(transparent)] + Shortcuts(#[from] ShortcutsError), +} + +/// Options for installing packages into a global environment. +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct InstallOptions { + /// The channels of a newly created environment. If empty, the default + /// channels of the config are used. + pub channels: Vec, + + /// The platform to install the packages for. + pub platform: Option, + + /// Mappings which describe which executables are exposed. If empty, all + /// executables of the installed packages are exposed. + pub expose: Vec, + + /// Additional dependencies whose executables are not exposed. + pub with: Vec, + + /// Whether the environment should be reinstalled. + pub force_reinstall: bool, + + /// Whether no shortcuts should be created for the installed packages. + pub no_shortcuts: bool, +} + +/// Install `specs` into the environment `env_name`. On failure the +/// environment is reverted and `project` is left untouched. +pub async fn install_environment( + project: &mut Project, + env_name: &EnvironmentName, + specs: &[GlobalSpec], + options: &InstallOptions, +) -> Result { + let mut modified_project = project.clone(); + match setup_environment(env_name, options, specs, &mut modified_project).await { + Ok(state_changes) => { + // Only advance project on success + *project = modified_project; + Ok(state_changes) + } + Err(err) => { + revert_environment_after_error(env_name, project).await; + Err(err) + } + } +} + +async fn setup_environment( + env_name: &EnvironmentName, + options: &InstallOptions, + specs: &[GlobalSpec], + project: &mut Project, +) -> Result { + let mut state_changes = StateChanges::new_with_env(env_name.clone()); + + // The manifest as it was before this environment was touched, so the + // shortcut below can tell whether it still owes the file a write. + let manifest_before = project.manifest.document.to_string(); + + if options.force_reinstall && project.environment(env_name).is_some() { + state_changes |= project + .remove_environment(env_name) + .await + .map_err(Box::new)?; + } + + let channels = eventual_environment_channels( + project, + Some(env_name), + &options.channels, + options.force_reinstall, + ); + + // Modify the project to include the new environment + if !project.manifest.parsed.envs.contains_key(env_name) { + let channels = channels + .into_iter() + .map(|channel| channel.channel) + .collect(); + project.manifest.add_environment(env_name, Some(channels))?; + state_changes.insert_change(env_name, StateChange::AddedEnvironment); + } + + if let Some(platform) = options.platform { + project.manifest.set_platform(env_name, platform)?; + } + + let converted_with_inclusions = options + .with + .iter() + .map(|spec| { + GlobalSpec::try_from_matchspec_with_name( + spec.clone(), + project.config().global_channel_config(), + ) + }) + .collect::, _>>()?; + + // Add the dependencies to the environment + let packages_to_add = specs + .iter() + .chain(converted_with_inclusions.iter()) + .collect_vec(); + + for spec in &packages_to_add { + project.manifest.add_dependency(env_name, spec)?; + } + + if !options.expose.is_empty() { + project.manifest.remove_all_exposed_mappings(env_name)?; + // Only add the exposed mappings that were requested + for mapping in &options.expose { + project.manifest.add_exposed_mapping(env_name, mapping)?; + } + } + + // The prefix already satisfies the manifest, so there is nothing to solve, + // install or expose. The manifest itself can still have changed: it may + // have just gained the environment that the prefix was left over from, and + // that change still has to be written out. + if project.environment_in_sync_internal(env_name, true).await? { + if project.manifest.document.to_string() != manifest_before { + project.manifest.save().await?; + } + return Ok(state_changes); + } + + // Installing the environment to be able to find the bin paths later + let environment_update = project + .install_environment_with_options(env_name, options.force_reinstall) + .await?; + project.clear_progress(); + + // Sync exposed name + sync_exposed_names(env_name, project, options).await?; + + // Add shortcuts + if !options.no_shortcuts { + let prefix = project.environment_prefix(env_name).await?; + for spec in specs.iter() { + let prefix_record = prefix.find_designated_package(spec.name()).await?; + if contains_menuinst_document(&prefix_record, prefix.root()) { + project.manifest.add_shortcut(env_name, spec.name())?; + } + } + state_changes |= project.sync_shortcuts(env_name).await?; + } + + state_changes.insert_change( + env_name, + StateChange::UpdatedEnvironment(environment_update), + ); + + // Expose executables of the new environment + state_changes |= project + .expose_executables_from_environment(env_name) + .await?; + + // Sync completions + state_changes |= project.sync_completions(env_name).await?; + + project.manifest.save().await?; + Ok(state_changes) +} + +async fn sync_exposed_names( + env_name: &EnvironmentName, + project: &mut Project, + options: &InstallOptions, +) -> Result<(), InstallError> { + let with_package_names = options + .with + .iter() + .map(|spec| { + spec.name + .as_exact() + .cloned() + .ok_or_else(|| InstallError::MissingPackageName(spec.to_string())) + }) + .collect::, _>>()?; + let expose_type = if options.expose.is_empty().not() { + ExposedType::Mappings(options.expose.clone()) + } else if with_package_names.is_empty() { + ExposedType::All + } else { + ExposedType::Ignore(with_package_names) + }; + project.sync_exposed_names(env_name, expose_type).await?; + Ok(()) +} diff --git a/crates/pixi_api/src/global/mod.rs b/crates/pixi_api/src/global/mod.rs index 9ff1926146..c191895e99 100644 --- a/crates/pixi_api/src/global/mod.rs +++ b/crates/pixi_api/src/global/mod.rs @@ -1,7 +1,16 @@ //! This module contains everything which is related to pixi global. +pub(crate) mod install; +pub use install::{InstallError, InstallOptions}; + pub(crate) mod list; pub use list::{GlobalDependency, GlobalEnvironment, GlobalExposedMapping, ListError}; +pub(crate) mod uninstall; +pub use uninstall::UninstallError; + +pub(crate) mod utils; +pub use utils::{eventual_environment_channels, revert_environment_after_error}; + // Reexport for pixi_api consumers -pub use pixi_global::*; +pub use pixi_global::{EnvironmentName, Mapping, Project, StateChanges, project::GlobalSpec}; diff --git a/crates/pixi_api/src/global/uninstall.rs b/crates/pixi_api/src/global/uninstall.rs new file mode 100644 index 0000000000..bb3c3b5ff1 --- /dev/null +++ b/crates/pixi_api/src/global/uninstall.rs @@ -0,0 +1,53 @@ +use pixi_global::{ + EnvironmentName, Project, StateChanges, + project::{ManifestError, RemoveEnvironmentError}, +}; + +use crate::global::utils::revert_environment_after_error; + +/// An error from uninstalling a global environment. +#[derive(Debug, thiserror::Error, miette::Diagnostic)] +pub enum UninstallError { + #[error(transparent)] + #[diagnostic(transparent)] + RemoveEnvironment(#[from] Box), + + #[error(transparent)] + #[diagnostic(transparent)] + Manifest(#[from] ManifestError), +} + +/// Uninstall the environment `env_name`. On failure the environment is +/// reverted and `project` is left untouched. +pub async fn uninstall_environment( + project: &mut Project, + env_name: &EnvironmentName, +) -> Result { + let mut modified_project = project.clone(); + match apply_changes(env_name, &mut modified_project).await { + Ok(state_changes) => { + // Only advance the project when successful + *project = modified_project; + Ok(state_changes) + } + Err(err) => { + // Revert any partial change for this environment + revert_environment_after_error(env_name, project).await; + Err(err) + } + } +} + +async fn apply_changes( + env_name: &EnvironmentName, + project_modified: &mut Project, +) -> Result { + let mut state_changes = StateChanges::new_with_env(env_name.clone()); + state_changes |= project_modified + .remove_environment(env_name) + .await + .map_err(Box::new)?; + + project_modified.manifest.save().await?; + Ok(state_changes) +} diff --git a/crates/pixi_api/src/global/utils.rs b/crates/pixi_api/src/global/utils.rs new file mode 100644 index 0000000000..335b64ed61 --- /dev/null +++ b/crates/pixi_api/src/global/utils.rs @@ -0,0 +1,184 @@ +use pixi_manifest::PrioritizedChannel; +use rattler_conda_types::NamedChannelOrUrl; +use tokio::fs as tokio_fs; + +use pixi_global::{EnvironmentName, project::SyncEnvironmentError}; + +/// An error from reverting an environment after a failed operation. +#[derive(Debug, thiserror::Error, miette::Diagnostic)] +enum RevertEnvironmentError { + #[error("couldn't revert environment {env_name}")] + Sync { + env_name: EnvironmentName, + #[source] + source: Box, + }, + + #[error("failed to remove {}", .path.display())] + RemoveDir { + path: std::path::PathBuf, + #[source] + source: std::io::Error, + }, +} + +/// The channels an environment ends up with once it is set up: an +/// environment that already exists in the manifest keeps its channels +/// (unless `--force-reinstall` recreates it), a new one gets the +/// `--channel` arguments or the config's default channels. Name inference +/// runs before the manifest is touched and has to solve the build backend +/// against these same channels. +pub fn eventual_environment_channels( + project: &pixi_global::Project, + environment: Option<&EnvironmentName>, + cli_channels: &[NamedChannelOrUrl], + force_reinstall: bool, +) -> Vec { + if !force_reinstall + && let Some(environment) = environment.and_then(|name| project.environment(name)) + { + return environment.prioritized_channels().cloned().collect(); + } + let channels = if cli_channels.is_empty() { + project.config().default_channels() + } else { + cli_channels.to_vec() + }; + channels.into_iter().map(PrioritizedChannel::from).collect() +} + +/// Reverts the changes made to the project for a specific environment after an error occurred. +pub async fn revert_environment_after_error( + env_name: &EnvironmentName, + project_to_revert_to: &pixi_global::Project, +) { + if let Err(err) = try_revert_environment(env_name, project_to_revert_to).await { + tracing::warn!("Reverting of the operation failed"); + tracing::info!("Reversion error: {:?}", miette::Report::new(err)); + } +} + +async fn try_revert_environment( + env_name: &EnvironmentName, + project_to_revert_to: &pixi_global::Project, +) -> Result<(), RevertEnvironmentError> { + if project_to_revert_to.environment(env_name).is_some() { + // We don't want to report on changes done by the reversion + let _ = project_to_revert_to + .sync_environment(env_name, None) + .await + .map_err(|source| RevertEnvironmentError::Sync { + env_name: env_name.clone(), + source: Box::new(source), + })?; + } else { + // clean up if directory exists for the failed new environment + let env_dir_path = project_to_revert_to.env_root_path().join(env_name.as_str()); + if env_dir_path.exists() { + tokio_fs::remove_dir_all(&env_dir_path) + .await + .map_err(|source| RevertEnvironmentError::RemoveDir { + path: env_dir_path.clone(), + source, + })?; + tracing::debug!( + "Cleaned up failed environment directory: {}", + env_dir_path.display() + ); + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + use tempfile::tempdir; + + use super::*; + + /// Create a project in an isolated `PIXI_HOME` so tests never read the + /// global manifest of the machine they run on. + async fn isolated_project(temp_dir: &std::path::Path) -> pixi_global::Project { + let pixi_home_dir = temp_dir.join("pixi-home"); + temp_env::async_with_vars( + [("PIXI_HOME", Some(pixi_home_dir.to_str().unwrap()))], + pixi_global::Project::discover_or_create(), + ) + .await + .unwrap() + } + + #[tokio::test] + async fn test_eventual_environment_channels() { + let temp_dir = tempdir().unwrap(); + let mut project = isolated_project(temp_dir.path()).await; + + let existing = EnvironmentName::from_str("existing").unwrap(); + let missing = EnvironmentName::from_str("missing").unwrap(); + let env_channel = NamedChannelOrUrl::from_str("env-channel").unwrap(); + let cli_channel = NamedChannelOrUrl::from_str("cli-channel").unwrap(); + project + .manifest + .add_environment(&existing, Some(vec![env_channel.clone()])) + .unwrap(); + + let defaults = project + .config() + .default_channels() + .into_iter() + .map(PrioritizedChannel::from) + .collect::>(); + + // No target environment: --channel arguments or the defaults. + assert_eq!( + eventual_environment_channels(&project, None, &[], false), + defaults + ); + assert_eq!( + eventual_environment_channels( + &project, + None, + std::slice::from_ref(&cli_channel), + false + ), + vec![PrioritizedChannel::from(cli_channel.clone())] + ); + + // A named environment that does not exist yet behaves the same. + assert_eq!( + eventual_environment_channels( + &project, + Some(&missing), + std::slice::from_ref(&cli_channel), + false + ), + vec![PrioritizedChannel::from(cli_channel.clone())] + ); + + // An existing environment keeps its manifest channels; --channel + // arguments are not applied to it. + assert_eq!( + eventual_environment_channels( + &project, + Some(&existing), + std::slice::from_ref(&cli_channel), + false + ), + vec![PrioritizedChannel::from(env_channel)] + ); + + // --force-reinstall recreates the environment, so the manifest + // channels no longer apply. + assert_eq!( + eventual_environment_channels( + &project, + Some(&existing), + std::slice::from_ref(&cli_channel), + true + ), + vec![PrioritizedChannel::from(cli_channel)] + ); + } +} diff --git a/crates/pixi_api/src/lib.rs b/crates/pixi_api/src/lib.rs index 2c50ff1309..0aa6e9d63d 100644 --- a/crates/pixi_api/src/lib.rs +++ b/crates/pixi_api/src/lib.rs @@ -9,6 +9,7 @@ pub use interface::{Interface, InterfaceError}; // Reexport for pixi_api consumers pub use pep508_rs as pep508; +pub use pixi_config as config; pub use pixi_core as core; pub use pixi_manifest as manifest; pub use pixi_pypi_spec as pypi_spec; diff --git a/crates/pixi_cli/src/global/add.rs b/crates/pixi_cli/src/global/add.rs index 3a7aa0861d..6822a945ae 100644 --- a/crates/pixi_cli/src/global/add.rs +++ b/crates/pixi_cli/src/global/add.rs @@ -109,12 +109,7 @@ pub async fn execute(args: Args) -> miette::Result<()> { Ok(()) } Err(err) => { - if let Err(revert_err) = - revert_environment_after_error(&args.environment, &project_original).await - { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } + revert_environment_after_error(&args.environment, &project_original).await; Err(err.wrap_err(format!( "Couldn't add packages to environment {}", args.environment diff --git a/crates/pixi_cli/src/global/expose.rs b/crates/pixi_cli/src/global/expose.rs index c63303888c..6ed8af7a0c 100644 --- a/crates/pixi_cli/src/global/expose.rs +++ b/crates/pixi_cli/src/global/expose.rs @@ -98,12 +98,7 @@ pub async fn add(args: AddArgs) -> miette::Result<()> { Ok(()) } Err(err) => { - if let Err(revert_err) = - revert_environment_after_error(&args.environment, &project_original).await - { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } + revert_environment_after_error(&args.environment, &project_original).await; Err(err) } } @@ -157,12 +152,7 @@ pub async fn remove(args: RemoveArgs) -> miette::Result<()> { all_changes |= state_changes; } Err(err) => { - if let Err(revert_err) = - revert_environment_after_error(&env_name, &last_updated_project).await - { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } + revert_environment_after_error(&env_name, &last_updated_project).await; // The names before this one are already removed and saved, so // they are still reported even though the command fails. all_changes.report(&last_updated_project).await; diff --git a/crates/pixi_cli/src/global/install.rs b/crates/pixi_cli/src/global/install.rs index a96a4b9e95..fd86a6bb71 100644 --- a/crates/pixi_cli/src/global/install.rs +++ b/crates/pixi_cli/src/global/install.rs @@ -1,22 +1,18 @@ -use std::{ops::Not, str::FromStr}; +use std::str::FromStr; use indexmap::IndexMap; use clap::Parser; -use itertools::Itertools; use miette::Report; use rattler_conda_types::{MatchSpec, NamedChannelOrUrl, Subdir}; +use crate::cli_interface::cli_global_context; use crate::global::{ EnvironmentAction, eventual_environment_channels, global_specs::GlobalSpecs, - report_failed_environment, report_failed_environments, revert_environment_after_error, + report_failed_environment, report_failed_environments, }; +use pixi_api::global::{EnvironmentName, GlobalSpec, InstallOptions, Mapping, Project}; use pixi_config::{self, Config, ConfigCli}; -use pixi_global::{ - self, EnvironmentName, Mapping, Project, StateChange, StateChanges, - common::contains_menuinst_document, - project::{ExposedType, GlobalSpec}, -}; /// Installs the defined packages in a globally accessible location and exposes their command line applications. /// @@ -86,7 +82,7 @@ pub async fn execute(args: Args) -> miette::Result<()> { // Load the global config and ensure // that the root_dir is relative to the manifest directory - let mut project_original = pixi_global::Project::discover_or_create() + let mut project_original = Project::discover_or_create() .await? .with_cli_config(config.clone()); @@ -133,172 +129,32 @@ pub async fn execute(args: Args) -> miette::Result<()> { miette::bail!("Can't add packages with `--with` for more than one environment"); } - let mut last_updated_project = project_original; + let options = InstallOptions { + channels: args.channels, + platform: args.platform, + expose: args.expose, + with: args.with, + force_reinstall: args.force_reinstall, + no_shortcuts: args.no_shortcuts, + }; + + let mut context = cli_global_context(project_original); let mut errors: Vec<(EnvironmentName, Report)> = Vec::new(); // Convert the packages into named global specs for (env_name, specs) in &env_to_specs { - let mut project = last_updated_project.clone(); - match setup_environment(env_name, &args, specs, &mut project).await { + match context.install_environment(env_name, specs, &options).await { Ok(state_changes) => { // The user named these environments, so they get a block even // when nothing changed. - state_changes.report(&project).await; - // Only advance project on success - last_updated_project = project; + state_changes.report(context.project()).await; } Err(err) => { - if let Err(revert_err) = - revert_environment_after_error(env_name, &last_updated_project).await - { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } report_failed_environment(env_name); - errors.push((env_name.clone(), err)); + errors.push((env_name.clone(), err.into())); } } } report_failed_environments(EnvironmentAction::Install, errors) } - -async fn setup_environment( - env_name: &EnvironmentName, - args: &Args, - specs: &[GlobalSpec], - project: &mut Project, -) -> miette::Result { - let mut state_changes = StateChanges::new_with_env(env_name.clone()); - - // The manifest as it was before this environment was touched, so the - // shortcut below can tell whether it still owes the file a write. - let manifest_before = project.manifest.document.to_string(); - - if args.force_reinstall && project.environment(env_name).is_some() { - state_changes |= project.remove_environment(env_name).await?; - } - - let channels = eventual_environment_channels( - project, - Some(env_name), - &args.channels, - args.force_reinstall, - ); - - // Modify the project to include the new environment - if !project.manifest.parsed.envs.contains_key(env_name) { - let channels = channels - .into_iter() - .map(|channel| channel.channel) - .collect(); - project.manifest.add_environment(env_name, Some(channels))?; - state_changes.insert_change(env_name, StateChange::AddedEnvironment); - } - - if let Some(platform) = args.platform { - project.manifest.set_platform(env_name, platform)?; - } - - let converted_with_inclusions = args - .with - .iter() - .map(|spec| { - GlobalSpec::try_from_matchspec_with_name( - spec.clone(), - project.config().global_channel_config(), - ) - }) - .collect::, _>>()?; - - // Add the dependencies to the environment - let packages_to_add = specs - .iter() - .chain(converted_with_inclusions.iter()) - .collect_vec(); - - for spec in &packages_to_add { - project.manifest.add_dependency(env_name, spec)?; - } - - if !args.expose.is_empty() { - project.manifest.remove_all_exposed_mappings(env_name)?; - // Only add the exposed mappings that were requested - for mapping in &args.expose { - project.manifest.add_exposed_mapping(env_name, mapping)?; - } - } - - // The prefix already satisfies the manifest, so there is nothing to solve, - // install or expose. The manifest itself can still have changed: it may - // have just gained the environment that the prefix was left over from, and - // that change still has to be written out. - if project.environment_in_sync_internal(env_name, true).await? { - if project.manifest.document.to_string() != manifest_before { - project.manifest.save().await?; - } - return Ok(state_changes); - } - - // Installing the environment to be able to find the bin paths later - let environment_update = project - .install_environment_with_options(env_name, args.force_reinstall) - .await?; - project.clear_progress(); - - // Sync exposed name - sync_exposed_names(env_name, project, args).await?; - - // Add shortcuts - if !args.no_shortcuts { - let prefix = project.environment_prefix(env_name).await?; - for spec in specs.iter() { - let prefix_record = prefix.find_designated_package(spec.name()).await?; - if contains_menuinst_document(&prefix_record, prefix.root()) { - project.manifest.add_shortcut(env_name, spec.name())?; - } - } - state_changes |= project.sync_shortcuts(env_name).await?; - } - - state_changes.insert_change( - env_name, - StateChange::UpdatedEnvironment(environment_update), - ); - - // Expose executables of the new environment - state_changes |= project - .expose_executables_from_environment(env_name) - .await?; - - // Sync completions - state_changes |= project.sync_completions(env_name).await?; - - project.manifest.save().await?; - Ok(state_changes) -} - -async fn sync_exposed_names( - env_name: &EnvironmentName, - project: &mut Project, - args: &Args, -) -> Result<(), miette::Error> { - let with_package_names = args - .with - .iter() - .map(|spec| { - spec.name.as_exact().cloned().ok_or_else(|| { - miette::miette!("could not find exact package name in MatchSpec {}", spec) - }) - }) - .collect::>>()?; - let expose_type = if args.expose.is_empty().not() { - ExposedType::Mappings(args.expose.clone()) - } else if with_package_names.is_empty() { - ExposedType::All - } else { - ExposedType::Ignore(with_package_names) - }; - project.sync_exposed_names(env_name, expose_type).await?; - Ok(()) -} diff --git a/crates/pixi_cli/src/global/list.rs b/crates/pixi_cli/src/global/list.rs index eb1d6403b3..0274f3b75c 100644 --- a/crates/pixi_cli/src/global/list.rs +++ b/crates/pixi_cli/src/global/list.rs @@ -8,10 +8,10 @@ use itertools::Itertools; use miette::IntoDiagnostic; use pixi_api::global::{ EnvironmentName, GlobalDependency, GlobalEnvironment, GlobalExposedMapping, ListError, Project, - report::{self, EnvReport, Item, Label, Marker, Row}, }; use pixi_config::{Config, ConfigCli}; use pixi_core::environment::list::{PackageToOutput, print_package_table}; +use pixi_global::report::{self, EnvReport, Item, Label, Marker, Row}; use rattler_conda_types::PackageName; use serde::Serialize; use std::str::FromStr; diff --git a/crates/pixi_cli/src/global/mod.rs b/crates/pixi_cli/src/global/mod.rs index 953225b8c2..1a92d395c2 100644 --- a/crates/pixi_cli/src/global/mod.rs +++ b/crates/pixi_cli/src/global/mod.rs @@ -1,10 +1,9 @@ use clap::Parser; -use miette::{IntoDiagnostic, Report, WrapErr}; -use pixi_manifest::PrioritizedChannel; -use rattler_conda_types::NamedChannelOrUrl; -use tokio::fs as tokio_fs; +use miette::Report; -use pixi_global::{EnvironmentName, report::EnvReport}; +use pixi_api::global::EnvironmentName; +pub(crate) use pixi_api::global::{eventual_environment_channels, revert_environment_after_error}; +use pixi_global::report::EnvReport; mod add; mod edit; @@ -134,148 +133,3 @@ fn report_failed_environments( "couldn't {action} {count} environment{plural}" )) } - -/// The channels an environment ends up with once it is set up: an -/// environment that already exists in the manifest keeps its channels -/// (unless `--force-reinstall` recreates it), a new one gets the -/// `--channel` arguments or the config's default channels. Name inference -/// runs before the manifest is touched and has to solve the build backend -/// against these same channels. -fn eventual_environment_channels( - project: &pixi_global::Project, - environment: Option<&EnvironmentName>, - cli_channels: &[NamedChannelOrUrl], - force_reinstall: bool, -) -> Vec { - if !force_reinstall - && let Some(environment) = environment.and_then(|name| project.environment(name)) - { - return environment.prioritized_channels().cloned().collect(); - } - let channels = if cli_channels.is_empty() { - project.config().default_channels() - } else { - cli_channels.to_vec() - }; - channels.into_iter().map(PrioritizedChannel::from).collect() -} - -/// Reverts the changes made to the project for a specific environment after an error occurred. -async fn revert_environment_after_error( - env_name: &EnvironmentName, - project_to_revert_to: &pixi_global::Project, -) -> miette::Result<()> { - if project_to_revert_to.environment(env_name).is_some() { - // We don't want to report on changes done by the reversion - let _ = project_to_revert_to - .sync_environment(env_name, None) - .await - .wrap_err_with(|| format!("Couldn't revert environment {env_name}"))?; - } else { - // clean up if directory exists for the failed new environment - let env_dir_path = project_to_revert_to.env_root_path().join(env_name.as_str()); - if env_dir_path.exists() { - tokio_fs::remove_dir_all(&env_dir_path) - .await - .into_diagnostic()?; - tracing::debug!( - "Cleaned up failed environment directory: {}", - env_dir_path.display() - ); - } - } - Ok(()) -} - -#[cfg(test)] -mod tests { - use std::str::FromStr; - - use tempfile::tempdir; - - use super::*; - - /// Create a project in an isolated `PIXI_HOME` so tests never read the - /// global manifest of the machine they run on. - async fn isolated_project(temp_dir: &std::path::Path) -> pixi_global::Project { - let pixi_home_dir = temp_dir.join("pixi-home"); - temp_env::async_with_vars( - [("PIXI_HOME", Some(pixi_home_dir.to_str().unwrap()))], - pixi_global::Project::discover_or_create(), - ) - .await - .unwrap() - } - - #[tokio::test] - async fn test_eventual_environment_channels() { - let temp_dir = tempdir().unwrap(); - let mut project = isolated_project(temp_dir.path()).await; - - let existing = EnvironmentName::from_str("existing").unwrap(); - let missing = EnvironmentName::from_str("missing").unwrap(); - let env_channel = NamedChannelOrUrl::from_str("env-channel").unwrap(); - let cli_channel = NamedChannelOrUrl::from_str("cli-channel").unwrap(); - project - .manifest - .add_environment(&existing, Some(vec![env_channel.clone()])) - .unwrap(); - - let defaults = project - .config() - .default_channels() - .into_iter() - .map(PrioritizedChannel::from) - .collect::>(); - - // No target environment: --channel arguments or the defaults. - assert_eq!( - eventual_environment_channels(&project, None, &[], false), - defaults - ); - assert_eq!( - eventual_environment_channels( - &project, - None, - std::slice::from_ref(&cli_channel), - false - ), - vec![PrioritizedChannel::from(cli_channel.clone())] - ); - - // A named environment that does not exist yet behaves the same. - assert_eq!( - eventual_environment_channels( - &project, - Some(&missing), - std::slice::from_ref(&cli_channel), - false - ), - vec![PrioritizedChannel::from(cli_channel.clone())] - ); - - // An existing environment keeps its manifest channels; --channel - // arguments are not applied to it. - assert_eq!( - eventual_environment_channels( - &project, - Some(&existing), - std::slice::from_ref(&cli_channel), - false - ), - vec![PrioritizedChannel::from(env_channel)] - ); - - // --force-reinstall recreates the environment, so the manifest - // channels no longer apply. - assert_eq!( - eventual_environment_channels( - &project, - Some(&existing), - std::slice::from_ref(&cli_channel), - true - ), - vec![PrioritizedChannel::from(cli_channel)] - ); - } -} diff --git a/crates/pixi_cli/src/global/remove.rs b/crates/pixi_cli/src/global/remove.rs index fb90b7a0b2..73e1d33198 100644 --- a/crates/pixi_cli/src/global/remove.rs +++ b/crates/pixi_cli/src/global/remove.rs @@ -97,12 +97,7 @@ pub async fn execute(args: Args) -> miette::Result<()> { state_changes.report(&project).await; } Err(err) => { - if let Err(revert_err) = - revert_environment_after_error(env_name, &project_original).await - { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } + revert_environment_after_error(env_name, &project_original).await; return Err(err); } } diff --git a/crates/pixi_cli/src/global/shortcut.rs b/crates/pixi_cli/src/global/shortcut.rs index 8de3af18d8..80c471ef51 100644 --- a/crates/pixi_cli/src/global/shortcut.rs +++ b/crates/pixi_cli/src/global/shortcut.rs @@ -82,12 +82,7 @@ pub async fn add(args: AddArgs) -> miette::Result<()> { Ok(()) } Err(err) => { - if let Err(revert_err) = - revert_environment_after_error(&args.environment, &project_original).await - { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } + revert_environment_after_error(&args.environment, &project_original).await; Err(err) } } @@ -171,12 +166,7 @@ pub async fn remove(args: RemoveArgs) -> miette::Result<()> { all_changes |= state_changes; } Err(err) => { - if let Err(revert_err) = - revert_environment_after_error(&env_name, &last_updated_project).await - { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } + revert_environment_after_error(&env_name, &last_updated_project).await; // The environments before this one are already changed and // saved, so they are still reported even though the command // fails. diff --git a/crates/pixi_cli/src/global/uninstall.rs b/crates/pixi_cli/src/global/uninstall.rs index 37c43e8b88..375683ee9e 100644 --- a/crates/pixi_cli/src/global/uninstall.rs +++ b/crates/pixi_cli/src/global/uninstall.rs @@ -1,12 +1,9 @@ -use crate::global::{ - EnvironmentAction, report_failed_environment, report_failed_environments, - revert_environment_after_error, -}; +use crate::cli_interface::cli_global_context; +use crate::global::{EnvironmentAction, report_failed_environment, report_failed_environments}; use clap::Parser; use miette::Report; +use pixi_api::global::{EnvironmentName, Project}; use pixi_config::{Config, ConfigCli}; -use pixi_global::StateChanges; -use pixi_global::{EnvironmentName, Project}; /// Uninstalls environments from the global environment. /// @@ -24,41 +21,20 @@ pub struct Args { pub async fn execute(args: Args) -> miette::Result<()> { let config = Config::try_with_cli_config(&args.config)?; - let project_original = pixi_global::Project::discover_or_create() + let project = Project::discover_or_create() .await? .with_cli_config(config.clone()); - - async fn apply_changes( - env_name: &EnvironmentName, - project_modified: &mut Project, - ) -> miette::Result { - let mut state_changes = StateChanges::new_with_env(env_name.clone()); - state_changes |= project_modified.remove_environment(env_name).await?; - - project_modified.manifest.save().await?; - Ok(state_changes) - } + let mut context = cli_global_context(project); let mut errors: Vec<(EnvironmentName, Report)> = Vec::new(); - let mut last_updated_project = project_original; for env_name in &args.environment { - let mut project = last_updated_project.clone(); - match apply_changes(env_name, &mut project).await { + match context.uninstall_environment(env_name).await { Ok(state_changes) => { - state_changes.report(&project).await; - // Only advance the project when successful - last_updated_project = project; + state_changes.report(context.project()).await; } Err(err) => { - // Revert any partial change for this environment, then continue - if let Err(revert_err) = - revert_environment_after_error(env_name, &last_updated_project).await - { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } report_failed_environment(env_name); - errors.push((env_name.clone(), err)); + errors.push((env_name.clone(), err.into())); } } } diff --git a/crates/pixi_cli/src/global/update.rs b/crates/pixi_cli/src/global/update.rs index fc306d0e45..7a9c44d869 100644 --- a/crates/pixi_cli/src/global/update.rs +++ b/crates/pixi_cli/src/global/update.rs @@ -158,10 +158,7 @@ pub async fn execute(args: Args) -> miette::Result<()> { } } Err(err) => { - if let Err(revert_err) = revert_environment_after_error(env_name, &project).await { - tracing::warn!("Reverting of the operation failed"); - tracing::info!("Reversion error: {:?}", revert_err); - } + revert_environment_after_error(env_name, &project).await; report_failed_environment(env_name); errors.push((env_name.clone(), err)); } diff --git a/crates/pixi_global/Cargo.toml b/crates/pixi_global/Cargo.toml index e420206621..c84f3e9716 100644 --- a/crates/pixi_global/Cargo.toml +++ b/crates/pixi_global/Cargo.toml @@ -51,6 +51,7 @@ rstest = { workspace = true } serde = { workspace = true } serde_derive = { workspace = true } serde_json = { workspace = true } +serde_with = { workspace = true } tempfile = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true } diff --git a/crates/pixi_global/src/project/manifest.rs b/crates/pixi_global/src/project/manifest.rs index 90aeb71b2b..f601f83cc1 100644 --- a/crates/pixi_global/src/project/manifest.rs +++ b/crates/pixi_global/src/project/manifest.rs @@ -15,6 +15,7 @@ use pixi_manifest::{ use pixi_toml::TomlIndexMap; use pixi_utils::{executable_from_path, strip_executable_extension}; use rattler_conda_types::{NamedChannelOrUrl, PackageName, Subdir}; +use serde_with::{DeserializeFromStr, SerializeDisplay}; use toml_edit::{DocumentMut, Item}; use toml_span::{DeserError, Value}; @@ -730,7 +731,7 @@ impl Manifest { } } -#[derive(Debug, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Clone, Hash, PartialEq, Eq, SerializeDisplay, DeserializeFromStr)] pub struct Mapping { exposed_name: ExposedName, // The executable_relname is a executable name possibly with a parts of a path in it to match diff --git a/scripts/into_diagnostic_baseline.toml b/scripts/into_diagnostic_baseline.toml index a3ff9d8367..ead82d5c86 100644 --- a/scripts/into_diagnostic_baseline.toml +++ b/scripts/into_diagnostic_baseline.toml @@ -8,7 +8,7 @@ pixi_build_r = 16 pixi_build_rattler_build = 4 pixi_build_ros = 3 pixi_build_rust = 1 -pixi_cli = 199 +pixi_cli = 198 pixi_core = 1 pixi_test_utils = 5 trampoline = 7