From 2a1ce8ed2005e4ab51e494a2a493c4bc11d37699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20H=C3=A4cker?= Date: Wed, 21 Oct 2026 09:00:16 +0200 Subject: [PATCH 1/6] refactor: Move run and shell commands into pixi_api --- crates/{pixi_cli/src/run.rs => pixi_api/src/workspace/run/mod.rs} | 0 .../src/shell.rs => pixi_api/src/workspace/shell/mod.rs} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename crates/{pixi_cli/src/run.rs => pixi_api/src/workspace/run/mod.rs} (100%) rename crates/{pixi_cli/src/shell.rs => pixi_api/src/workspace/shell/mod.rs} (100%) diff --git a/crates/pixi_cli/src/run.rs b/crates/pixi_api/src/workspace/run/mod.rs similarity index 100% rename from crates/pixi_cli/src/run.rs rename to crates/pixi_api/src/workspace/run/mod.rs diff --git a/crates/pixi_cli/src/shell.rs b/crates/pixi_api/src/workspace/shell/mod.rs similarity index 100% rename from crates/pixi_cli/src/shell.rs rename to crates/pixi_api/src/workspace/shell/mod.rs From 2479f44f804ef2083dc253bd04e4d45197c09c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20H=C3=A4cker?= Date: Mon, 26 Oct 2026 09:32:06 +0100 Subject: [PATCH 2/6] feat(pixi_api): Expose run and shell --- Cargo.lock | 10 +- .../pixi/tests/integration_rust/common/mod.rs | 1 + crates/pixi_api/Cargo.toml | 13 +- crates/pixi_api/src/context.rs | 15 + crates/pixi_api/src/interface.rs | 5 + crates/pixi_api/src/lib.rs | 1 + crates/pixi_api/src/workspace/init/mod.rs | 3 + crates/pixi_api/src/workspace/mod.rs | 6 + crates/pixi_api/src/workspace/run/mod.rs | 603 ++++-------------- crates/pixi_api/src/workspace/shell/mod.rs | 78 +-- .../src/workspace/workspace/platform.rs | 3 + crates/pixi_cli/Cargo.toml | 1 - crates/pixi_cli/src/cli_interface.rs | 15 + crates/pixi_cli/src/conda_script.rs | 6 +- crates/pixi_cli/src/run.rs | 445 +++++++++++++ crates/pixi_cli/src/shell.rs | 65 ++ crates/pixi_task/src/task_environment.rs | 96 ++- crates/pixi_task/src/task_graph.rs | 246 ++++--- 18 files changed, 958 insertions(+), 654 deletions(-) create mode 100644 crates/pixi_cli/src/run.rs create mode 100644 crates/pixi_cli/src/shell.rs diff --git a/Cargo.lock b/Cargo.lock index 9f716e2c8a..422fc7a3de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6244,13 +6244,16 @@ name = "pixi_api" version = "0.1.0" dependencies = [ "console", + "deno_task_shell", "dunce", "fancy_display", "fs-err", + "futures", "hex", "indexmap 2.14.2", "insta", "itertools 0.15.0", + "libc", "miette 7.6.0", "minijinja", "pep508_rs", @@ -6259,9 +6262,12 @@ dependencies = [ "pixi_core", "pixi_install_pypi", "pixi_manifest", + "pixi_progress", + "pixi_pty", "pixi_pypi_spec", "pixi_reporters", "pixi_spec", + "pixi_task", "pixi_test_utils", "pixi_utils", "pixi_uv_conversions", @@ -6269,6 +6275,7 @@ dependencies = [ "rattler_conda_types", "rattler_lock", "rattler_repodata_gateway", + "rattler_shell", "regex", "rstest", "same-file", @@ -6276,6 +6283,7 @@ dependencies = [ "tempfile", "thiserror 2.0.21", "tokio", + "tokio-util 0.7.20", "tracing", "url", "uv-distribution", @@ -6285,6 +6293,7 @@ dependencies = [ "uv-pep508", "uv-redacted", "uv-types", + "which", ] [[package]] @@ -6482,7 +6491,6 @@ dependencies = [ "pixi_manifest", "pixi_path", "pixi_progress", - "pixi_pty", "pixi_pypi_spec", "pixi_record", "pixi_reporters", diff --git a/crates/pixi/tests/integration_rust/common/mod.rs b/crates/pixi/tests/integration_rust/common/mod.rs index 4cb954acff..0ff401649d 100644 --- a/crates/pixi/tests/integration_rust/common/mod.rs +++ b/crates/pixi/tests/integration_rust/common/mod.rs @@ -723,6 +723,7 @@ impl PixiControl { }, args.templated, ) + .await .map_err(RunError::TaskGraphError)?; // Iterate over all tasks in the graph and execute them. diff --git a/crates/pixi_api/Cargo.toml b/crates/pixi_api/Cargo.toml index 1f09e5b4af..3492e75c0a 100644 --- a/crates/pixi_api/Cargo.toml +++ b/crates/pixi_api/Cargo.toml @@ -10,9 +10,11 @@ license.workspace = true [dependencies] console = { workspace = true } +deno_task_shell = { workspace = true } dunce = { workspace = true } fancy_display = { workspace = true } fs-err = { workspace = true } +futures = { workspace = true } hex = { workspace = true } indexmap = { workspace = true } itertools = { workspace = true } @@ -24,21 +26,25 @@ pixi_consts = { workspace = true } pixi_core = { workspace = true } pixi_install_pypi = { workspace = true } pixi_manifest = { workspace = true } +pixi_progress = { workspace = true } pixi_pypi_spec = { workspace = true } pixi_reporters = { workspace = true } pixi_spec = { workspace = true } +pixi_task = { workspace = true } pixi_utils = { workspace = true } pixi_uv_conversions = { workspace = true } pypi_modifiers = { workspace = true } rattler_conda_types = { workspace = true } rattler_lock = { workspace = true } rattler_repodata_gateway = { workspace = true } +rattler_shell = { workspace = true, features = ["sysinfo"] } regex = { workspace = true } same-file = { workspace = true } serde = { workspace = true, features = ["derive"] } tempfile = { workspace = true } thiserror = { workspace = true } -tokio = { workspace = true, features = ["fs"] } +tokio = { workspace = true, features = ["fs", "rt", "signal"] } +tokio-util = { workspace = true } tracing = { workspace = true } url = { workspace = true } uv-distribution = { workspace = true } @@ -48,9 +54,14 @@ uv-normalize = { workspace = true } uv-pep508 = { workspace = true } uv-redacted = { workspace = true } uv-types = { workspace = true } +which = { workspace = true } [dev-dependencies] insta = { workspace = true } pixi_test_utils = { workspace = true } rstest = { workspace = true } tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } + +[target."cfg(unix)".dependencies] +libc = { workspace = true, default-features = false } +pixi_pty = { path = "../pixi_pty" } diff --git a/crates/pixi_api/src/context.rs b/crates/pixi_api/src/context.rs index 8853aa6bb5..89d0d91ce4 100644 --- a/crates/pixi_api/src/context.rs +++ b/crates/pixi_api/src/context.rs @@ -20,6 +20,7 @@ use crate::interface::Interface; use crate::workspace::add::GitOptions; use crate::workspace::{ ChannelOptions, DependencyOptions, InitOptions, Package, ReinstallOptions, RemoveError, + RunOptions, ShellOptions, }; pub struct DefaultContext { @@ -549,6 +550,20 @@ impl WorkspaceContext { .await } + pub async fn run(&self, options: RunOptions) -> miette::Result { + crate::workspace::run::run( + &self.interface, + &self.workspace, + options, + self.progress.as_ref(), + ) + .await + } + + pub async fn shell(&self, options: ShellOptions) -> miette::Result { + crate::workspace::shell::shell(&self.workspace, options, self.progress.as_ref()).await + } + pub async fn list_tasks( &self, environment: Option, diff --git a/crates/pixi_api/src/interface.rs b/crates/pixi_api/src/interface.rs index 38f960fe46..6ce7c3a7f5 100644 --- a/crates/pixi_api/src/interface.rs +++ b/crates/pixi_api/src/interface.rs @@ -4,6 +4,11 @@ use std::future::Future; pub trait Interface { fn is_cli(&self) -> impl Future + Send; fn confirm(&self, msg: &str) -> impl Future> + Send; + fn select( + &self, + msg: &str, + options: &[String], + ) -> impl Future>> + Send; fn info(&self, msg: &str) -> impl Future + Send; fn success(&self, msg: &str) -> impl Future + Send; fn warning(&self, msg: &str) -> impl Future + Send; diff --git a/crates/pixi_api/src/lib.rs b/crates/pixi_api/src/lib.rs index b97d149751..0b5277e20a 100644 --- a/crates/pixi_api/src/lib.rs +++ b/crates/pixi_api/src/lib.rs @@ -11,6 +11,7 @@ pub use pep508_rs as pep508; pub use pixi_core as core; pub use pixi_manifest as manifest; pub use pixi_pypi_spec as pypi_spec; +pub use pixi_reporters as reporters; pub use pixi_spec as spec; pub use rattler_conda_types; diff --git a/crates/pixi_api/src/workspace/init/mod.rs b/crates/pixi_api/src/workspace/init/mod.rs index 53ce8e9e51..3e85460466 100644 --- a/crates/pixi_api/src/workspace/init/mod.rs +++ b/crates/pixi_api/src/workspace/init/mod.rs @@ -807,6 +807,9 @@ mod tests { async fn confirm(&self, _msg: &str) -> miette::Result { Ok(self.confirm_response) } + async fn select(&self, _msg: &str, _options: &[String]) -> miette::Result> { + Ok(None) + } async fn error(&self, _msg: &str) {} async fn info(&self, _msg: &str) {} async fn success(&self, _msg: &str) {} diff --git a/crates/pixi_api/src/workspace/mod.rs b/crates/pixi_api/src/workspace/mod.rs index c8f719a716..55f4c4d6b3 100644 --- a/crates/pixi_api/src/workspace/mod.rs +++ b/crates/pixi_api/src/workspace/mod.rs @@ -17,8 +17,14 @@ pub use list::{Package, PackageKind}; pub(crate) mod reinstall; pub use reinstall::ReinstallOptions; +pub(crate) mod run; +pub use run::{RunOptions, run_future_forwarding_signals}; + pub(crate) mod search; +pub(crate) mod shell; +pub use shell::ShellOptions; + pub(crate) mod task; #[allow(clippy::module_inception)] diff --git a/crates/pixi_api/src/workspace/run/mod.rs b/crates/pixi_api/src/workspace/run/mod.rs index edcdc26ba1..4307d82f31 100644 --- a/crates/pixi_api/src/workspace/run/mod.rs +++ b/crates/pixi_api/src/workspace/run/mod.rs @@ -1,29 +1,23 @@ use std::{ - collections::{HashMap, HashSet, hash_map::Entry}, + collections::{HashMap, hash_map::Entry}, ffi::OsString, - io::Read, - process::ExitCode, + rc::Rc, + sync::Arc, }; #[cfg(unix)] use std::io::IsTerminal; -use clap::Parser; -use deno_task_shell::KillSignal; -use dialoguer::theme::ColorfulTheme; +use deno_task_shell::{KillSignal, ShellCommand}; use fancy_display::FancyDisplay; -use indicatif::ProgressDrawTarget; use itertools::Itertools; use miette::{Diagnostic, IntoDiagnostic}; -use pixi_config::{ConfigCli, ConfigCliActivation}; -use pixi_core::host::{cli_detector_consent, probe_conda_script, probe_pep723_script}; - use pixi_core::{ - Workspace, WorkspaceLocator, - environment::sanity_check_workspace, + Workspace, + environment::{LockFileUsage, sanity_check_workspace}, lock_file::{ReinstallPackages, UpdateLockFileOptions, UpdateMode}, workspace::{ - Environment, + Environment, EnvironmentFromNameError, errors::UnsupportedPlatformError, virtual_packages::{ EnvironmentRunnability, classify_environment_runnability, @@ -31,352 +25,70 @@ use pixi_core::{ }, }, }; -use pixi_manifest::{HasWorkspaceManifest, PixiPlatformName, TaskName, WithWarnings}; -use pixi_progress::global_multi_progress; +use pixi_manifest::{EnvironmentName, HasWorkspaceManifest, PixiPlatformName}; use pixi_task::{ AmbiguousTask, CanSkip, ExecutableTask, FailedToParseShellScript, InvalidWorkingDirectory, PreferExecutable, SearchEnvironments, TaskAndEnvironment, TaskGraph, get_task_env, }; -use rattler_conda_types::Subdir; +use serde::{Deserialize, Serialize}; use thiserror::Error; use tokio_util::sync::CancellationToken; use tracing::Level; -use crate::cli_config::{ - LockAndInstallConfig, ScriptWorkspaceConfig, validate_transient_script_lock_file_usage, -}; -use crate::process_exit; -use crate::run_script::{ - RemoteScriptManifest, RunScriptInput, STDIN_SCRIPT_COMMAND, StdinScriptCommand, - prepare_remote_script, prepare_stdin_script, transient_script_cache_key, -}; -use crate::shared::install_platform::resolve_install_platform; +use crate::interface::Interface; -/// Run a task in an environment. -/// -/// This command is used to run tasks in the pixi environment. -/// It will activate the environment and run the task in the environment. -/// It is using the deno_task_shell to run the task. -/// -/// `pixi run` will also update the lock file and install the environment if it -/// is required. -#[derive(Parser, Debug, Default)] -#[clap(trailing_var_arg = true, disable_help_flag = true)] -pub struct Args { - #[clap(flatten)] - pub config_source: pixi_config::ConfigSourceCli, - - /// The pixi task or a task shell command you want to run in the workspace's - /// environment, which can be an executable in the environment's PATH. +#[derive(Default, Serialize, Deserialize)] +pub struct RunOptions { + /// The pixi task or a task shell command to run. pub task: Vec, - /// Execute the command as an executable without resolving Pixi tasks. - /// - /// Useful when a task name and an executable have the same name. - #[arg(long = "executable", short = 'x')] - pub executable: bool, - - /// Enable experimental `--script` features; currently the `conda-script` - /// block. - /// - /// The flag has no effect for PEP 723 scripts, so a shebang line can - /// pass it unconditionally. Setting `experimental.conda-script` in the - /// configuration opts in without the flag, with a warning on every run. - #[arg(long, requires = "script")] - pub experimental: bool, - - #[clap(flatten)] - pub workspace_config: ScriptWorkspaceConfig, - - #[clap(flatten)] - pub lock_and_install_config: LockAndInstallConfig, - - #[clap(flatten)] - pub config: ConfigCli, - - #[clap(flatten)] - pub activation_config: ConfigCliActivation, - /// The environment to run the task in. - #[arg(long, short)] - pub environment: Option, + pub environment: Option, - /// Install and run in the environment for the given platform; a warning is - /// printed when it doesn't run on this machine. Accepts a workspace - /// platform name; a bare conda subdir (e.g. `linux-64`) is also accepted. - #[arg(long, short)] + /// The workspace platform to install and run the environment for. pub platform: Option, - /// Use a clean environment to run the task - /// - /// Using this flag will ignore your current shell environment and use bare - /// minimum environment to activate the pixi environment in. - #[arg(long)] + /// Execute the command as an executable without resolving Pixi tasks. + pub executable: bool, + + /// Use a clean environment to run the task. pub clean_env: bool, - /// Don't run the dependencies of the task ('depends-on' field in the task - /// definition) - #[arg(long)] + /// Don't run the dependencies of the task. pub skip_deps: bool, /// Enable template rendering for the command arguments. - /// - /// By default, arguments passed to `pixi run` on the command line are not - /// processed by the template engine. Use this flag to enable rendering - /// of template variables like `{{ pixi.platform }}`. - #[arg(long)] pub templated: bool, - /// Run the task in dry-run mode (only print the command that would run) - #[clap(short = 'n', long)] + /// Only print the commands that would run. pub dry_run: bool, - #[clap(long, action = clap::ArgAction::HelpLong)] - pub help: Option, + pub lock_file_usage: LockFileUsage, - #[clap(short, action = clap::ArgAction::HelpShort)] - pub h: Option, -} - -impl Args { - fn validate_script_options(&self) -> miette::Result<()> { - if self.workspace_config.script.is_none() { - return Ok(()); - } + pub no_install: bool, - let mut unsupported = Vec::new(); - if self.environment.is_some() { - unsupported.push("--environment"); - } - if self.skip_deps { - unsupported.push("--skip-deps"); - } + /// Additional commands available to the task shell. + #[serde(skip)] + pub custom_commands: HashMap>, - if unsupported.is_empty() { - Ok(()) - } else { - Err(miette::miette!( - help = "A PEP 723 script has one implicit default run environment and no Pixi task graph.", - "`pixi run --script` does not support {}", - unsupported.join(", ") - )) - } - } + /// Shown instead of the task's command in the task header. + pub display_command: Option, } -/// CLI entry point for `pixi run` -/// When running the sigints are ignored and child can react to them. As it -/// pleases. -pub async fn execute(mut args: Args) -> miette::Result { - args.validate_script_options()?; - - // Following statements don't spawn any progress bar, so set - // progress draw target to hidden. Otherwise output may be - // incorrect. - let not_hidden = !global_multi_progress().is_hidden(); - global_multi_progress().set_draw_target(ProgressDrawTarget::hidden()); - - let cli_config = args - .activation_config +pub async fn run( + interface: &I, + workspace: &Workspace, + options: RunOptions, + progress: Option<&Arc>, +) -> miette::Result { + let explicit_environment = explicit_environment(workspace, options.environment.as_ref())?; + let environment = explicit_environment .clone() - .merge_config(args.config.clone().into()); - - let is_script = args.workspace_config.script.is_some(); - let script_input = args - .workspace_config - .script - .as_deref() - .map(RunScriptInput::classify); - let requested_lock_file_usage = args.lock_and_install_config.lock_file_usage()?; - let global_config_source = args.config_source.source(); - let mut _remote_script_directory = None; - let mut stdin_script_command = None; - let workspace = match script_input { - Some(RunScriptInput::Remote(url)) => { - validate_transient_script_lock_file_usage(requested_lock_file_usage)?; - let root = std::env::current_dir().into_diagnostic()?; - let config = pixi_config::Config::load_with(&root, &global_config_source) - .merge_config(cli_config); - let prepared = prepare_remote_script(url, &config, &root, args.experimental).await?; - let cache_key = - transient_script_cache_key(b"remote", prepared.original_url.as_str().as_bytes()); - let manifest = match prepared.manifest { - RemoteScriptManifest::Pep723(manifest) => manifest, - RemoteScriptManifest::CondaScript(manifest) => { - let entrypoint = manifest.metadata().entrypoint.clone(); - let host = - probe_conda_script(&manifest, &root, &config, cli_detector_consent(None)) - .await?; - let workspace = Workspace::from_transient_conda_script( - manifest, - config, - root, - &prepared.cache_name, - &cache_key, - host, - )?; - if not_hidden { - global_multi_progress() - .set_draw_target(ProgressDrawTarget::stderr_with_hz(20)); - } - let code = - crate::conda_script::execute_run(workspace, entrypoint, args).await?; - drop(prepared.directory); - return Ok(process_exit::exit_code_from_code(code)); - } - }; - let script_path = manifest.path().to_owned(); - let host = - probe_pep723_script(&manifest, &root, &config, cli_detector_consent(None)).await?; - let WithWarnings { - value: workspace, - warnings, - } = Workspace::from_transient_script( - manifest, - config, - root, - script_path, - &prepared.cache_name, - &cache_key, - host, - )?; - for warning in warnings { - tracing::warn!("{warning}"); - } - _remote_script_directory = Some(prepared.directory); - workspace - } - Some(RunScriptInput::Stdin) => { - validate_transient_script_lock_file_usage(requested_lock_file_usage)?; - let root = std::env::current_dir().into_diagnostic()?; - let config = pixi_config::Config::load_with(&root, &global_config_source) - .merge_config(cli_config); - let mut contents = Vec::new(); - std::io::stdin() - .read_to_end(&mut contents) - .into_diagnostic()?; - let prepared = prepare_stdin_script(contents, &root)?; - let cache_key = - transient_script_cache_key(b"stdin", prepared.manifest.metadata().as_bytes()); - let host = probe_pep723_script( - &prepared.manifest, - &root, - &config, - cli_detector_consent(None), - ) - .await?; - let WithWarnings { - value: workspace, - warnings, - } = Workspace::from_transient_script( - prepared.manifest, - config, - root, - "".into(), - "stdin", - &cache_key, - host, - )?; - for warning in warnings { - tracing::warn!("{warning}"); - } - stdin_script_command = Some(prepared.command); - workspace - } - Some(RunScriptInput::Local(path)) => { - let root = std::path::absolute(&path) - .into_diagnostic()? - .parent() - .expect("an absolute script path always has a parent") - .to_owned(); - let config = pixi_config::Config::load_with(&root, &global_config_source) - .merge_config(cli_config.clone()); - // The configuration option opts in for every run, so the flag is - // only needed when it is unset. - let opted_in = args.experimental || config.experimental_conda_script(); - // A conda-script block takes this file off the PEP 723 path; a - // file with both kinds of block is rejected by the detection. - if let Some(manifest) = crate::conda_script::detect_with_fallback(&path, opted_in)? { - crate::conda_script::ensure_enabled( - args.experimental, - &config, - &path.display().to_string(), - )?; - if not_hidden { - global_multi_progress().set_draw_target(ProgressDrawTarget::stderr_with_hz(20)); - } - let entrypoint = manifest.metadata().entrypoint.clone(); - let host = - probe_conda_script(&manifest, &root, &config, cli_detector_consent(None)) - .await?; - let workspace = Workspace::from_conda_script(manifest, config, host)?; - let code = crate::conda_script::execute_run(workspace, entrypoint, args).await?; - return Ok(process_exit::exit_code_from_code(code)); - } - WorkspaceLocator::for_cli() - .with_global_config_source(global_config_source) - .with_search_start(pixi_core::workspace::DiscoveryStart::Script(path)) - .with_cli_config(cli_config) - .locate() - .await? - } - None => { - WorkspaceLocator::for_cli() - .with_global_config_source(global_config_source) - .with_search_start(args.workspace_config.workspace_locator_start()) - .with_cli_config(cli_config) - .locate() - .await? - } - }; - - let stdin_display_args = if stdin_script_command.is_some() { - Some(args.task.clone()) - } else { - None - }; - if stdin_script_command.is_some() { - args.task.insert(0, STDIN_SCRIPT_COMMAND.to_owned()); - args.executable = true; - } else if is_script { - let script_path = workspace.workspace.provenance.path.clone(); - let script_path = script_path.into_os_string().into_string().map_err(|_| { - miette::miette!("the script path must contain only valid UTF-8 characters") - })?; - - args.task.insert(0, script_path); - args.task.insert(0, "python".to_owned()); - args.executable = true; - } + .unwrap_or_else(|| workspace.default_environment()); + let allow_installs = !options.no_install; - // Extract the passed in environment name. - let environment = if is_script { - workspace.default_environment() - } else { - workspace.environment_from_name_or_env_var(args.environment.clone())? - }; - - // Find the environment to run the task in, if any were specified. - let explicit_environment = - if is_script || (args.environment.is_none() && environment.is_default()) { - None - } else { - Some(environment.clone()) - }; - - // Print all available tasks if no task is provided - if args.task.is_empty() { - command_not_found(&workspace, explicit_environment); - return Ok(ExitCode::SUCCESS); - } - - // We expect progress bar to be used afterwards, so set draw - // target to the original one. - if not_hidden { - global_multi_progress().set_draw_target(ProgressDrawTarget::stderr_with_hz(20)); - } // Sanity check of prefix location - sanity_check_workspace(&workspace).await?; + sanity_check_workspace(workspace).await?; // Detect the host, the virtual packages this machine provides, only for the // environment this command targets. Other environments are detected when a @@ -390,7 +102,7 @@ pub async fn execute(mut args: Args) -> miette::Result { // environment was last installed for (so users need not repeat // `--platform`), falling back to the host-aware best match when the // environment isn't installed yet. - let user_platform = resolve_install_platform(&workspace, args.platform.as_ref())?; + let user_platform = options.platform.clone(); let run_platform = user_platform .clone() .or_else(|| environment.installed_resolved_platform_name()); @@ -398,7 +110,7 @@ pub async fn execute(mut args: Args) -> miette::Result { // A `--platform` the environment doesn't list is a membership error. With // no platform requested, defer to the install path's minimum fallback. - if args.lock_and_install_config.allow_installs() + if allow_installs && best_declared_platform.is_none() && let Some(name) = user_platform.as_ref() { @@ -409,20 +121,20 @@ pub async fn execute(mut args: Args) -> miette::Result { )); } - if args.lock_and_install_config.allow_installs() { + if allow_installs { environment.emit_emulation_warning(); } // Top-level progress, kept here so we can clear it between phases. - let progress = pixi_reporters::TopLevelProgress::from_global(); + let progress = progress.cloned(); // Ensure that the lock file is up-to-date. let mut lock_file = workspace .resolve_lock_file( - Some(progress.clone()), + progress.clone(), UpdateLockFileOptions { - lock_file_usage: requested_lock_file_usage, - no_install: args.lock_and_install_config.no_install(), + lock_file_usage: options.lock_file_usage, + no_install: options.no_install, max_concurrent_solves: workspace.config().max_concurrent_solves(), ..Default::default() }, @@ -443,38 +155,11 @@ pub async fn execute(mut args: Args) -> miette::Result { }); // Construct a task graph from the input arguments. - // Pin the search only to an explicit `--platform`; otherwise each - // environment resolves its own platform, so foreign-platform tasks are found. - let search_platform = if args.lock_and_install_config.allow_installs() { - user_platform.as_ref().and_then(|name| { - (&workspace) - .workspace_manifest() - .workspace - .platform_by_name(name) - }) - } else { - None - }; - let search_environment = - SearchEnvironments::from_opt_env(&workspace, explicit_environment.clone(), search_platform) - .with_disambiguate_fn(disambiguate_task_interactive); - - let task_graph = TaskGraph::from_cmd_args( - &workspace, - &search_environment, - args.task, - args.skip_deps, - if args.executable { - PreferExecutable::Always - } else { - PreferExecutable::TaskFirst - }, - args.templated, - )?; + let task_graph = build_task_graph(interface, workspace, explicit_environment, &options).await?; tracing::debug!("Task graph: {}", task_graph); // Print dry-run message if dry-run mode is enabled - if args.dry_run { + if options.dry_run { pixi_progress::println!( "{}{}\n\n", console::Emoji("🌵 ", ""), @@ -503,7 +188,7 @@ pub async fn execute(mut args: Args) -> miette::Result { continue; } - if args.lock_and_install_config.allow_installs() { + if allow_installs { workspace .refresh_environment_hosts_for_platform_selection(std::slice::from_ref( &executable_task.run_environment, @@ -513,13 +198,12 @@ pub async fn execute(mut args: Args) -> miette::Result { // Classify how this machine runs the task's environment. A `--platform` // override means the user vouches for the machine, so skip the check. - let runnability = - (args.lock_and_install_config.allow_installs() && user_platform.is_none()).then(|| { - classify_environment_runnability( - &executable_task.run_environment, - Some(lock_file.as_lock_file()), - ) - }); + let runnability = (allow_installs && user_platform.is_none()).then(|| { + classify_environment_runnability( + &executable_task.run_environment, + Some(lock_file.as_lock_file()), + ) + }); // Fail before announcing a task whose environment can't run here at // all; by-accident environments proceed and `--platform` overrides. @@ -540,19 +224,15 @@ pub async fn execute(mut args: Args) -> miette::Result { // Showing which command is being run if the level and type allows it. if tracing::enabled!(Level::WARN) - && (!executable_task.task().is_custom() || stdin_script_command.is_some()) + && (!executable_task.task().is_custom() || options.display_command.is_some()) { if task_idx > 0 { // Add a newline between task outputs pixi_progress::println!(); } - let display_command = if let Some(forwarded_args) = &stdin_display_args { - if forwarded_args.is_empty() { - "python -c ".to_owned() - } else { - format!("python -c {}", forwarded_args.iter().format(" ")) - } + let display_command = if let Some(display_command) = &options.display_command { + display_command.clone() } else { executable_task.display_command().to_string() }; @@ -584,7 +264,7 @@ pub async fn execute(mut args: Args) -> miette::Result { } // on dry-run mode, we just print the command and skip the execution - if args.dry_run { + if options.dry_run { task_idx += 1; continue; } @@ -633,9 +313,7 @@ pub async fn execute(mut args: Args) -> miette::Result { // require a virtual package the machine lacks, so skip the // prefix build and platform validation -- its tasks run // anywhere, relying only on the host environment. - if args.lock_and_install_config.allow_installs() - && runnability != Some(EnvironmentRunnability::NoDependencies) - { + if allow_installs && runnability != Some(EnvironmentRunnability::NoDependencies) { // No `--platform`: pin to the platform this environment was // last installed for, not a sibling's bare subdir. if user_platform.is_none() { @@ -666,7 +344,9 @@ pub async fn execute(mut args: Args) -> miette::Result { } // Clear the current progress reports. - progress.on_clear(); + if let Some(progress) = &progress { + progress.on_clear(); + } // Clear caches based on the filesystem. The tasks might change files on disk. lock_file.command_dispatcher.clear_filesystem_caches().await; @@ -674,7 +354,7 @@ pub async fn execute(mut args: Args) -> miette::Result { let command_env = get_task_env( &executable_task.run_environment, &executable_task.platform, - args.clean_env || executable_task.task().clean_env(), + options.clean_env || executable_task.task().clean_env(), Some(lock_file.as_lock_file()), workspace.config().force_activate(), workspace.config().experimental_activation_cache_usage(), @@ -690,13 +370,12 @@ pub async fn execute(mut args: Args) -> miette::Result { .collect(); // Execute the task itself within the command environment. If one of the tasks - // failed with a non-zero exit code, we exit this parent process with - // the same code. + // failed with a non-zero exit code, we return the same code. match execute_task( &executable_task, &task_env, signal.clone(), - stdin_script_command.as_ref(), + &options.custom_commands, ) .await { @@ -704,10 +383,7 @@ pub async fn execute(mut args: Args) -> miette::Result { task_idx += 1; } Err(TaskExecutionError::NonZeroExitCode(code)) => { - if code == 127 { - command_not_found(&workspace, explicit_environment.clone()); - } - return Ok(process_exit::exit_code_from_code(code)); + return Ok(code); } Err(err) => return Err(err.into()), } @@ -726,47 +402,59 @@ pub async fn execute(mut args: Args) -> miette::Result { .into_diagnostic()?; } - Ok(ExitCode::SUCCESS) + Ok(0) } -/// Called when a command was not found. -fn command_not_found<'p>(workspace: &'p Workspace, explicit_environment: Option>) { - let available_tasks: HashSet = - if let Some(explicit_environment) = explicit_environment { - explicit_environment.get_filtered_tasks() - } else { - workspace - .environments() - .into_iter() - .flat_map(|env| env.get_filtered_tasks()) - .collect() - }; +/// Looks up the environment the caller asked for. +fn explicit_environment<'p>( + workspace: &'p Workspace, + name: Option<&EnvironmentName>, +) -> miette::Result>> { + name.map(|name| { + workspace + .environment(name) + .ok_or_else(|| EnvironmentFromNameError::Unknown(name.clone()).into()) + }) + .transpose() +} - if !available_tasks.is_empty() { - pixi_progress::println!( - "\nAvailable tasks:\n{}", - available_tasks - .into_iter() - .sorted() - .format_with("\n", |name, f| { - f(&format_args!("\t{}", name.fancy_display().bold())) - }) - ); - } +async fn build_task_graph<'p, I: Interface>( + interface: &I, + workspace: &'p Workspace, + explicit_environment: Option>, + options: &RunOptions, +) -> miette::Result> { + // Pin the search only to an explicit `--platform`; otherwise each + // environment resolves its own platform, so foreign-platform tasks are found. + let search_platform = if !options.no_install { + options.platform.as_ref().and_then(|name| { + workspace + .workspace_manifest() + .workspace + .platform_by_name(name) + }) + } else { + None + }; + let search_environment = + SearchEnvironments::from_opt_env(workspace, explicit_environment, search_platform) + .with_disambiguate_fn(async |problem: &AmbiguousTask<'p>| { + disambiguate_task_interactive(interface, problem).await + }); - // Point at the missing platform only when it is genuinely what blocks the - // run. An environment that installs nothing, or whose packages the machine - // already satisfies, runs here regardless of the declared platforms, so - // suggesting `platform add` would send the user after the wrong problem. - if workspace.environments().iter().all(|env| { - classify_environment_runnability(env, None) == EnvironmentRunnability::Unsupported - }) { - pixi_progress::println!( - "\nHelp: This platform ({}) is not supported. Please run the following command to add this platform to the workspace:\n\n\tpixi workspace platform add {}", - Subdir::current().unwrap_or(Subdir::NoArch), - Subdir::current().unwrap_or(Subdir::NoArch) - ); - } + Ok(TaskGraph::from_cmd_args( + workspace, + &search_environment, + options.task.clone(), + options.skip_deps, + if options.executable { + PreferExecutable::Always + } else { + PreferExecutable::TaskFirst + }, + options.templated, + ) + .await?) } #[derive(Debug, Error, Diagnostic)] @@ -786,26 +474,21 @@ enum TaskExecutionError { } /// Called to execute a single command. -/// -/// This function is called from [`execute`]. async fn execute_task( task: &ExecutableTask<'_>, command_env: &HashMap, kill_signal: KillSignal, - stdin_script: Option<&StdinScriptCommand>, + custom_commands: &HashMap>, ) -> Result<(), TaskExecutionError> { let Some(script) = task.as_deno_script()? else { return Ok(()); }; let cwd = task.working_directory()?; - let custom_commands = stdin_script - .map(|command| HashMap::from([(STDIN_SCRIPT_COMMAND.to_owned(), command.shell_command())])) - .unwrap_or_default(); let execute_future = deno_task_shell::execute( script, command_env.clone(), cwd, - custom_commands, + custom_commands.clone(), kill_signal.clone(), ); @@ -819,7 +502,8 @@ async fn execute_task( } /// Called to disambiguate between environments to run a task in. -fn disambiguate_task_interactive<'p>( +async fn disambiguate_task_interactive<'p, I: Interface>( + interface: &I, problem: &AmbiguousTask<'p>, ) -> Option> { // If any of the candidate tasks declares a `default-environment` that @@ -838,27 +522,23 @@ fn disambiguate_task_interactive<'p>( let environment_names = problem .environments .iter() - .map(|(env, _)| env.name()) + .map(|(env, _)| env.name().to_string()) .collect_vec(); - let theme = ColorfulTheme { - active_item_style: console::Style::new().for_stderr().magenta(), - ..ColorfulTheme::default() - }; - dialoguer::Select::with_theme(&theme) - .with_prompt(format!( - "The task '{}' {}can be run in multiple environments.\n\nPlease select an environment to run the task in:", - problem.task_name.fancy_display(), - if let Some(dependency) = &problem.depended_on_by { - format!("(depended on by '{}') ", dependency.0.fancy_display()) - } else { - String::new() - } - )) - .report(false) - .items(&environment_names) - .default(0) - .interact_opt() + interface + .select( + &format!( + "The task '{}' {}can be run in multiple environments.\n\nPlease select an environment to run the task in:", + problem.task_name.fancy_display(), + if let Some(dependency) = &problem.depended_on_by { + format!("(depended on by '{}') ", dependency.0.fancy_display()) + } else { + String::new() + } + ), + &environment_names, + ) + .await .unwrap_or(None) .map(|idx| problem.environments[idx].clone()) } @@ -965,16 +645,3 @@ async fn listen_and_forward_all_signals(kill_signal: KillSignal) { } futures::future::join_all(futures).await; } - -#[cfg(test)] -mod tests { - use clap::Parser; - - use super::Args; - - #[test] - fn experimental_requires_a_script() { - assert!(Args::try_parse_from(["run", "--experimental", "--script", "main.c"]).is_ok()); - assert!(Args::try_parse_from(["run", "--experimental", "task"]).is_err()); - } -} diff --git a/crates/pixi_api/src/workspace/shell/mod.rs b/crates/pixi_api/src/workspace/shell/mod.rs index 9e288ef420..d1f8085e49 100644 --- a/crates/pixi_api/src/workspace/shell/mod.rs +++ b/crates/pixi_api/src/workspace/shell/mod.rs @@ -1,6 +1,5 @@ -use std::{collections::HashMap, io::Write, path::PathBuf, process::ExitCode}; +use std::{collections::HashMap, io::Write, path::PathBuf, sync::Arc}; -use clap::Parser; use miette::IntoDiagnostic; use rattler_conda_types::Subdir; use rattler_shell::{ @@ -9,48 +8,30 @@ use rattler_shell::{ }; use which::which; -use pixi_config::{ConfigCli, ConfigCliActivation, ConfigCliPrompt}; use pixi_core::{ - UpdateLockFileOptions, WorkspaceLocator, + UpdateLockFileOptions, Workspace, activation::CurrentEnvVarBehavior, - environment::{InstallFilter, get_update_lock_file_and_prefix}, + environment::{InstallFilter, LockFileUsage, get_update_lock_file_and_prefix}, lock_file::{ReinstallPackages, UpdateMode}, prompt, - workspace::get_activated_environment_variables, + workspace::{EnvironmentFromNameError, get_activated_environment_variables}, }; +use pixi_manifest::EnvironmentName; +use serde::{Deserialize, Serialize}; -use crate::cli_config::{LockAndInstallConfig, WorkspaceConfig}; #[cfg(target_family = "unix")] use pixi_pty::unix::PtySession; use pixi_utils::prefix::Prefix; -/// Start a shell in a pixi environment. -/// -/// Run `exit` to leave the shell. -#[derive(Parser, Debug)] -pub struct Args { - #[clap(flatten)] - pub config_source: pixi_config::ConfigSourceCli, - - #[clap(flatten)] - workspace_config: WorkspaceConfig, - - #[clap(flatten)] - pub lock_and_install_config: LockAndInstallConfig, - - #[clap(flatten)] - config: ConfigCli, - +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct ShellOptions { /// The environment to activate in the shell - #[arg(long, short)] - environment: Option, + pub environment: Option, - #[clap(flatten)] - prompt_config: ConfigCliPrompt, + pub lock_file_usage: LockFileUsage, - #[clap(flatten)] - activation_config: ConfigCliActivation, + pub no_install: bool, } /// Set up Ctrl-C handler to ignore it (the child process should react on CTRL-C) @@ -324,30 +305,27 @@ async fn start_nu_shell( Ok(process.wait().into_diagnostic()?.code()) } -pub async fn execute(args: Args) -> miette::Result { - let config = args - .activation_config - .merge_config(args.prompt_config.into()) - .merge_config(args.config.clone().into()); - - let workspace = WorkspaceLocator::for_cli() - .with_global_config_source(args.config_source.source()) - .with_search_start(args.workspace_config.workspace_locator_start()) - .with_cli_config(config) - .locate() - .await?; - - let environment = workspace.environment_from_name_or_env_var(args.environment)?; +pub async fn shell( + workspace: &Workspace, + options: ShellOptions, + progress: Option<&Arc>, +) -> miette::Result { + let environment = match options.environment { + Some(name) => workspace + .environment(&name) + .ok_or(EnvironmentFromNameError::Unknown(name))?, + None => workspace.default_environment(), + }; // Make sure environment is up-to-date, default to install, users can avoid this with frozen or locked. #[allow(unused_variables)] let (lock_file_data, prefix) = get_update_lock_file_and_prefix( &environment, - Some(pixi_reporters::TopLevelProgress::from_global()), + progress.cloned(), UpdateMode::QuickValidate, UpdateLockFileOptions { - lock_file_usage: args.lock_and_install_config.lock_file_usage()?, - no_install: args.lock_and_install_config.no_install(), + lock_file_usage: options.lock_file_usage, + no_install: options.no_install, max_concurrent_solves: workspace.config().max_concurrent_solves(), ..Default::default() }, @@ -462,11 +440,11 @@ pub async fn execute(args: Args) -> miette::Result { }; match res { - Ok(Some(code)) => Ok(crate::process_exit::exit_code_from_code(code)), - Ok(None) => Ok(ExitCode::SUCCESS), + Ok(Some(code)) => Ok(code), + Ok(None) => Ok(0), Err(error) => { eprintln!("Error starting shell: {error}"); - Ok(ExitCode::FAILURE) + Ok(1) } } } diff --git a/crates/pixi_api/src/workspace/workspace/platform.rs b/crates/pixi_api/src/workspace/workspace/platform.rs index a9f4e4d7b4..d99771e424 100644 --- a/crates/pixi_api/src/workspace/workspace/platform.rs +++ b/crates/pixi_api/src/workspace/workspace/platform.rs @@ -361,6 +361,9 @@ mod tests { async fn confirm(&self, _msg: &str) -> miette::Result { Ok(true) } + async fn select(&self, _msg: &str, _options: &[String]) -> miette::Result> { + Ok(None) + } async fn error(&self, _msg: &str) {} async fn info(&self, _msg: &str) {} async fn success(&self, _msg: &str) {} diff --git a/crates/pixi_cli/Cargo.toml b/crates/pixi_cli/Cargo.toml index 3811b9e469..cae09b96e1 100644 --- a/crates/pixi_cli/Cargo.toml +++ b/crates/pixi_cli/Cargo.toml @@ -115,7 +115,6 @@ rattler_repodata_gateway = { workspace = true } [target."cfg(unix)".dependencies] libc = { workspace = true, default-features = false } -pixi_pty = { path = "../pixi_pty" } [features] default = [] diff --git a/crates/pixi_cli/src/cli_interface.rs b/crates/pixi_cli/src/cli_interface.rs index b66be9aa0f..74459a1bcf 100644 --- a/crates/pixi_cli/src/cli_interface.rs +++ b/crates/pixi_cli/src/cli_interface.rs @@ -31,6 +31,21 @@ impl Interface for CliInterface { .into_diagnostic() } + async fn select(&self, msg: &str, options: &[String]) -> miette::Result> { + let theme = dialoguer::theme::ColorfulTheme { + active_item_style: console::Style::new().for_stderr().magenta(), + ..dialoguer::theme::ColorfulTheme::default() + }; + + Ok(dialoguer::Select::with_theme(&theme) + .with_prompt(msg) + .report(false) + .items(options) + .default(0) + .interact_opt() + .unwrap_or(None)) + } + async fn info(&self, msg: &str) { eprintln!("{msg}"); } diff --git a/crates/pixi_cli/src/conda_script.rs b/crates/pixi_cli/src/conda_script.rs index 5cac7f5a82..18f3e98c47 100644 --- a/crates/pixi_cli/src/conda_script.rs +++ b/crates/pixi_cli/src/conda_script.rs @@ -2,6 +2,7 @@ use std::{collections::HashMap, ffi::OsString, path::Path}; use deno_task_shell::KillSignal; use miette::{IntoDiagnostic, NamedSource, Report}; +use pixi_api::workspace::run_future_forwarding_signals; use pixi_core::{ Workspace, environment::sanity_check_workspace, @@ -17,10 +18,7 @@ use pixi_script_shell::{ShellContext, execute_sequence, parse_sequence}; use pixi_task::get_task_env; use tracing::Level; -use crate::{ - run::{Args, run_future_forwarding_signals}, - shared::install_platform::resolve_install_platform, -}; +use crate::{run::Args, shared::install_platform::resolve_install_platform}; /// Reads the `conda-script` block of a local `--script` file. /// diff --git a/crates/pixi_cli/src/run.rs b/crates/pixi_cli/src/run.rs new file mode 100644 index 0000000000..006b3aec03 --- /dev/null +++ b/crates/pixi_cli/src/run.rs @@ -0,0 +1,445 @@ +use std::{ + collections::{HashMap, HashSet}, + io::Read, + process::ExitCode, +}; + +use clap::Parser; +use fancy_display::FancyDisplay; +use indicatif::ProgressDrawTarget; +use itertools::Itertools; +use miette::IntoDiagnostic; +use pixi_api::workspace::RunOptions; +use pixi_config::{ConfigCli, ConfigCliActivation}; +use pixi_core::host::{cli_detector_consent, probe_conda_script, probe_pep723_script}; + +use pixi_core::{ + Workspace, WorkspaceLocator, + workspace::{ + Environment, + virtual_packages::{EnvironmentRunnability, classify_environment_runnability}, + }, +}; +use pixi_manifest::{PixiPlatformName, TaskName, WithWarnings}; +use pixi_progress::global_multi_progress; +use rattler_conda_types::Subdir; + +use crate::cli_config::{ + LockAndInstallConfig, ScriptWorkspaceConfig, validate_transient_script_lock_file_usage, +}; +use crate::cli_interface::cli_context; +use crate::process_exit; +use crate::run_script::{ + RemoteScriptManifest, RunScriptInput, STDIN_SCRIPT_COMMAND, prepare_remote_script, + prepare_stdin_script, transient_script_cache_key, +}; +use crate::shared::install_platform::resolve_install_platform; + +/// Run a task in an environment. +/// +/// This command is used to run tasks in the pixi environment. +/// It will activate the environment and run the task in the environment. +/// It is using the deno_task_shell to run the task. +/// +/// `pixi run` will also update the lock file and install the environment if it +/// is required. +#[derive(Parser, Debug, Default)] +#[clap(trailing_var_arg = true, disable_help_flag = true)] +pub struct Args { + #[clap(flatten)] + pub config_source: pixi_config::ConfigSourceCli, + + /// The pixi task or a task shell command you want to run in the workspace's + /// environment, which can be an executable in the environment's PATH. + pub task: Vec, + + /// Execute the command as an executable without resolving Pixi tasks. + /// + /// Useful when a task name and an executable have the same name. + #[arg(long = "executable", short = 'x')] + pub executable: bool, + + /// Enable experimental `--script` features; currently the `conda-script` + /// block. + /// + /// The flag has no effect for PEP 723 scripts, so a shebang line can + /// pass it unconditionally. Setting `experimental.conda-script` in the + /// configuration opts in without the flag, with a warning on every run. + #[arg(long, requires = "script")] + pub experimental: bool, + + #[clap(flatten)] + pub workspace_config: ScriptWorkspaceConfig, + + #[clap(flatten)] + pub lock_and_install_config: LockAndInstallConfig, + + #[clap(flatten)] + pub config: ConfigCli, + + #[clap(flatten)] + pub activation_config: ConfigCliActivation, + + /// The environment to run the task in. + #[arg(long, short)] + pub environment: Option, + + /// Install and run in the environment for the given platform; a warning is + /// printed when it doesn't run on this machine. Accepts a workspace + /// platform name; a bare conda subdir (e.g. `linux-64`) is also accepted. + #[arg(long, short)] + pub platform: Option, + + /// Use a clean environment to run the task + /// + /// Using this flag will ignore your current shell environment and use bare + /// minimum environment to activate the pixi environment in. + #[arg(long)] + pub clean_env: bool, + + /// Don't run the dependencies of the task ('depends-on' field in the task + /// definition) + #[arg(long)] + pub skip_deps: bool, + + /// Enable template rendering for the command arguments. + /// + /// By default, arguments passed to `pixi run` on the command line are not + /// processed by the template engine. Use this flag to enable rendering + /// of template variables like `{{ pixi.platform }}`. + #[arg(long)] + pub templated: bool, + + /// Run the task in dry-run mode (only print the command that would run) + #[clap(short = 'n', long)] + pub dry_run: bool, + + #[clap(long, action = clap::ArgAction::HelpLong)] + pub help: Option, + + #[clap(short, action = clap::ArgAction::HelpShort)] + pub h: Option, +} + +impl Args { + fn validate_script_options(&self) -> miette::Result<()> { + if self.workspace_config.script.is_none() { + return Ok(()); + } + + let mut unsupported = Vec::new(); + if self.environment.is_some() { + unsupported.push("--environment"); + } + if self.skip_deps { + unsupported.push("--skip-deps"); + } + + if unsupported.is_empty() { + Ok(()) + } else { + Err(miette::miette!( + help = "A PEP 723 script has one implicit default run environment and no Pixi task graph.", + "`pixi run --script` does not support {}", + unsupported.join(", ") + )) + } + } +} + +/// CLI entry point for `pixi run` +/// When running the sigints are ignored and child can react to them. As it +/// pleases. +pub async fn execute(mut args: Args) -> miette::Result { + args.validate_script_options()?; + + // Following statements don't spawn any progress bar, so set + // progress draw target to hidden. Otherwise output may be + // incorrect. + let not_hidden = !global_multi_progress().is_hidden(); + global_multi_progress().set_draw_target(ProgressDrawTarget::hidden()); + + let cli_config = args + .activation_config + .clone() + .merge_config(args.config.clone().into()); + + let is_script = args.workspace_config.script.is_some(); + let script_input = args + .workspace_config + .script + .as_deref() + .map(RunScriptInput::classify); + let requested_lock_file_usage = args.lock_and_install_config.lock_file_usage()?; + let global_config_source = args.config_source.source(); + let mut _remote_script_directory = None; + let mut stdin_script_command = None; + let workspace = match script_input { + Some(RunScriptInput::Remote(url)) => { + validate_transient_script_lock_file_usage(requested_lock_file_usage)?; + let root = std::env::current_dir().into_diagnostic()?; + let config = pixi_config::Config::load_with(&root, &global_config_source) + .merge_config(cli_config); + let prepared = prepare_remote_script(url, &config, &root, args.experimental).await?; + let cache_key = + transient_script_cache_key(b"remote", prepared.original_url.as_str().as_bytes()); + let manifest = match prepared.manifest { + RemoteScriptManifest::Pep723(manifest) => manifest, + RemoteScriptManifest::CondaScript(manifest) => { + let entrypoint = manifest.metadata().entrypoint.clone(); + let host = + probe_conda_script(&manifest, &root, &config, cli_detector_consent(None)) + .await?; + let workspace = Workspace::from_transient_conda_script( + manifest, + config, + root, + &prepared.cache_name, + &cache_key, + host, + )?; + if not_hidden { + global_multi_progress() + .set_draw_target(ProgressDrawTarget::stderr_with_hz(20)); + } + let code = + crate::conda_script::execute_run(workspace, entrypoint, args).await?; + drop(prepared.directory); + return Ok(process_exit::exit_code_from_code(code)); + } + }; + let script_path = manifest.path().to_owned(); + let host = + probe_pep723_script(&manifest, &root, &config, cli_detector_consent(None)).await?; + let WithWarnings { + value: workspace, + warnings, + } = Workspace::from_transient_script( + manifest, + config, + root, + script_path, + &prepared.cache_name, + &cache_key, + host, + )?; + for warning in warnings { + tracing::warn!("{warning}"); + } + _remote_script_directory = Some(prepared.directory); + workspace + } + Some(RunScriptInput::Stdin) => { + validate_transient_script_lock_file_usage(requested_lock_file_usage)?; + let root = std::env::current_dir().into_diagnostic()?; + let config = pixi_config::Config::load_with(&root, &global_config_source) + .merge_config(cli_config); + let mut contents = Vec::new(); + std::io::stdin() + .read_to_end(&mut contents) + .into_diagnostic()?; + let prepared = prepare_stdin_script(contents, &root)?; + let cache_key = + transient_script_cache_key(b"stdin", prepared.manifest.metadata().as_bytes()); + let host = probe_pep723_script( + &prepared.manifest, + &root, + &config, + cli_detector_consent(None), + ) + .await?; + let WithWarnings { + value: workspace, + warnings, + } = Workspace::from_transient_script( + prepared.manifest, + config, + root, + "".into(), + "stdin", + &cache_key, + host, + )?; + for warning in warnings { + tracing::warn!("{warning}"); + } + stdin_script_command = Some(prepared.command); + workspace + } + Some(RunScriptInput::Local(path)) => { + let root = std::path::absolute(&path) + .into_diagnostic()? + .parent() + .expect("an absolute script path always has a parent") + .to_owned(); + let config = pixi_config::Config::load_with(&root, &global_config_source) + .merge_config(cli_config.clone()); + // The configuration option opts in for every run, so the flag is + // only needed when it is unset. + let opted_in = args.experimental || config.experimental_conda_script(); + // A conda-script block takes this file off the PEP 723 path; a + // file with both kinds of block is rejected by the detection. + if let Some(manifest) = crate::conda_script::detect_with_fallback(&path, opted_in)? { + crate::conda_script::ensure_enabled( + args.experimental, + &config, + &path.display().to_string(), + )?; + if not_hidden { + global_multi_progress().set_draw_target(ProgressDrawTarget::stderr_with_hz(20)); + } + let entrypoint = manifest.metadata().entrypoint.clone(); + let host = + probe_conda_script(&manifest, &root, &config, cli_detector_consent(None)) + .await?; + let workspace = Workspace::from_conda_script(manifest, config, host)?; + let code = crate::conda_script::execute_run(workspace, entrypoint, args).await?; + return Ok(process_exit::exit_code_from_code(code)); + } + WorkspaceLocator::for_cli() + .with_global_config_source(global_config_source) + .with_search_start(pixi_core::workspace::DiscoveryStart::Script(path)) + .with_cli_config(cli_config) + .locate() + .await? + } + None => { + WorkspaceLocator::for_cli() + .with_global_config_source(global_config_source) + .with_search_start(args.workspace_config.workspace_locator_start()) + .with_cli_config(cli_config) + .locate() + .await? + } + }; + + let stdin_display_args = if stdin_script_command.is_some() { + Some(args.task.clone()) + } else { + None + }; + if stdin_script_command.is_some() { + args.task.insert(0, STDIN_SCRIPT_COMMAND.to_owned()); + args.executable = true; + } else if is_script { + let script_path = workspace.workspace.provenance.path.clone(); + let script_path = script_path.into_os_string().into_string().map_err(|_| { + miette::miette!("the script path must contain only valid UTF-8 characters") + })?; + + args.task.insert(0, script_path); + args.task.insert(0, "python".to_owned()); + args.executable = true; + } + + // Extract the passed in environment name. + let environment = if is_script { + workspace.default_environment() + } else { + workspace.environment_from_name_or_env_var(args.environment.clone())? + }; + + // Find the environment to run the task in, if any were specified. + let explicit_environment = + if is_script || (args.environment.is_none() && environment.is_default()) { + None + } else { + Some(environment.clone()) + }; + + // Print all available tasks if no task is provided + if args.task.is_empty() { + command_not_found(&workspace, explicit_environment); + return Ok(ExitCode::SUCCESS); + } + + // We expect progress bar to be used afterwards, so set draw + // target to the original one. + if not_hidden { + global_multi_progress().set_draw_target(ProgressDrawTarget::stderr_with_hz(20)); + } + + let options = RunOptions { + task: args.task, + environment: explicit_environment.as_ref().map(|env| env.name().clone()), + platform: resolve_install_platform(&workspace, args.platform.as_ref())?, + executable: args.executable, + clean_env: args.clean_env, + skip_deps: args.skip_deps, + templated: args.templated, + dry_run: args.dry_run, + lock_file_usage: requested_lock_file_usage, + no_install: args.lock_and_install_config.no_install(), + custom_commands: stdin_script_command + .map(|command| { + HashMap::from([(STDIN_SCRIPT_COMMAND.to_owned(), command.shell_command())]) + }) + .unwrap_or_default(), + display_command: stdin_display_args.map(|forwarded_args| { + if forwarded_args.is_empty() { + "python -c ".to_owned() + } else { + format!("python -c {}", forwarded_args.iter().format(" ")) + } + }), + }; + let code = cli_context(workspace.clone()).run(options).await?; + + if code == 127 { + command_not_found(&workspace, explicit_environment); + } + Ok(process_exit::exit_code_from_code(code)) +} + +/// Called when a command was not found. +fn command_not_found<'p>(workspace: &'p Workspace, explicit_environment: Option>) { + let available_tasks: HashSet = + if let Some(explicit_environment) = explicit_environment { + explicit_environment.get_filtered_tasks() + } else { + workspace + .environments() + .into_iter() + .flat_map(|env| env.get_filtered_tasks()) + .collect() + }; + + if !available_tasks.is_empty() { + pixi_progress::println!( + "\nAvailable tasks:\n{}", + available_tasks + .into_iter() + .sorted() + .format_with("\n", |name, f| { + f(&format_args!("\t{}", name.fancy_display().bold())) + }) + ); + } + + // Point at the missing platform only when it is genuinely what blocks the + // run. An environment that installs nothing, or whose packages the machine + // already satisfies, runs here regardless of the declared platforms, so + // suggesting `platform add` would send the user after the wrong problem. + if workspace.environments().iter().all(|env| { + classify_environment_runnability(env, None) == EnvironmentRunnability::Unsupported + }) { + pixi_progress::println!( + "\nHelp: This platform ({}) is not supported. Please run the following command to add this platform to the workspace:\n\n\tpixi workspace platform add {}", + Subdir::current().unwrap_or(Subdir::NoArch), + Subdir::current().unwrap_or(Subdir::NoArch) + ); + } +} + +#[cfg(test)] +mod tests { + use clap::Parser; + + use super::Args; + + #[test] + fn experimental_requires_a_script() { + assert!(Args::try_parse_from(["run", "--experimental", "--script", "main.c"]).is_ok()); + assert!(Args::try_parse_from(["run", "--experimental", "task"]).is_err()); + } +} diff --git a/crates/pixi_cli/src/shell.rs b/crates/pixi_cli/src/shell.rs new file mode 100644 index 0000000000..f9c36c5312 --- /dev/null +++ b/crates/pixi_cli/src/shell.rs @@ -0,0 +1,65 @@ +use std::process::ExitCode; + +use clap::Parser; +use pixi_api::workspace::ShellOptions; +use pixi_config::{ConfigCli, ConfigCliActivation, ConfigCliPrompt}; +use pixi_core::WorkspaceLocator; + +use crate::cli_config::{LockAndInstallConfig, WorkspaceConfig}; +use crate::cli_interface::cli_context; + +/// Start a shell in a pixi environment. +/// +/// Run `exit` to leave the shell. +#[derive(Parser, Debug)] +pub struct Args { + #[clap(flatten)] + pub config_source: pixi_config::ConfigSourceCli, + + #[clap(flatten)] + workspace_config: WorkspaceConfig, + + #[clap(flatten)] + pub lock_and_install_config: LockAndInstallConfig, + + #[clap(flatten)] + config: ConfigCli, + + /// The environment to activate in the shell + #[arg(long, short)] + environment: Option, + + #[clap(flatten)] + prompt_config: ConfigCliPrompt, + + #[clap(flatten)] + activation_config: ConfigCliActivation, +} + +pub async fn execute(args: Args) -> miette::Result { + let config = args + .activation_config + .merge_config(args.prompt_config.into()) + .merge_config(args.config.clone().into()); + + let workspace = WorkspaceLocator::for_cli() + .with_global_config_source(args.config_source.source()) + .with_search_start(args.workspace_config.workspace_locator_start()) + .with_cli_config(config) + .locate() + .await?; + + let environment = workspace + .environment_from_name_or_env_var(args.environment)? + .name() + .clone(); + + let options = ShellOptions { + environment: Some(environment), + lock_file_usage: args.lock_and_install_config.lock_file_usage()?, + no_install: args.lock_and_install_config.no_install(), + }; + + let code = cli_context(workspace).shell(options).await?; + Ok(crate::process_exit::exit_code_from_code(code)) +} diff --git a/crates/pixi_task/src/task_environment.rs b/crates/pixi_task/src/task_environment.rs index 33de9f38b5..a42939fa21 100644 --- a/crates/pixi_task/src/task_environment.rs +++ b/crates/pixi_task/src/task_environment.rs @@ -1,3 +1,5 @@ +use std::future::Future; + use miette::Diagnostic; use pixi_core::{ Workspace, @@ -18,7 +20,10 @@ pub enum FindTaskSource<'p> { pub type TaskAndEnvironment<'p> = (Environment<'p>, &'p Task); pub trait TaskDisambiguation<'p> { - fn disambiguate(&self, task: &AmbiguousTask<'p>) -> Option>; + fn disambiguate( + &self, + task: &AmbiguousTask<'p>, + ) -> impl Future>>; } #[derive(Default)] @@ -26,16 +31,16 @@ pub struct NoDisambiguation; pub struct DisambiguateFn(Fn); impl<'p> TaskDisambiguation<'p> for NoDisambiguation { - fn disambiguate(&self, _task: &AmbiguousTask<'p>) -> Option> { + async fn disambiguate(&self, _task: &AmbiguousTask<'p>) -> Option> { None } } -impl<'p, F: Fn(&AmbiguousTask<'p>) -> Option>> TaskDisambiguation<'p> +impl<'p, F: AsyncFn(&AmbiguousTask<'p>) -> Option>> TaskDisambiguation<'p> for DisambiguateFn { - fn disambiguate(&self, task: &AmbiguousTask<'p>) -> Option> { - self.0(task) + async fn disambiguate(&self, task: &AmbiguousTask<'p>) -> Option> { + (self.0)(task).await } } @@ -143,7 +148,9 @@ pub(crate) fn default_search_platform<'p>(env: &Environment<'p>) -> Option<&'p P impl<'p, D: TaskDisambiguation<'p>> SearchEnvironments<'p, D> { /// Returns a new `SearchEnvironments` with the given disambiguation /// function. - pub fn with_disambiguate_fn) -> Option>>( + pub fn with_disambiguate_fn< + F: AsyncFn(&AmbiguousTask<'p>) -> Option>, + >( self, func: F, ) -> SearchEnvironments<'p, DisambiguateFn> { @@ -167,7 +174,7 @@ impl<'p, D: TaskDisambiguation<'p>> SearchEnvironments<'p, D> { /// Finds the task with the given name or returns an error that explains why /// the task could not be found. - pub(crate) fn find_task( + pub(crate) async fn find_task( &self, name: TaskName, source: FindTaskSource<'p>, @@ -267,7 +274,7 @@ impl<'p, D: TaskDisambiguation<'p>> SearchEnvironments<'p, D> { environments: tasks, }; - match self.disambiguate.disambiguate(&ambiguous_task) { + match self.disambiguate.disambiguate(&ambiguous_task).await { Some(env) => Ok(env), None => Err(FindTaskError::AmbiguousTask(ambiguous_task.into())), } @@ -286,8 +293,8 @@ mod tests { /// this machine must still be found: each environment resolves its own /// search platform (falling back to its first declared platform), rather /// than inheriting the caller's. - #[test] - fn test_find_task_in_foreign_platform_environment() { + #[tokio::test] + async fn test_find_task_in_foreign_platform_environment() { let manifest_str = r#" [project] name = "foo" @@ -307,12 +314,13 @@ mod tests { let search = SearchEnvironments::from_opt_env(&project, None, None); let (env, _task) = search .find_task("flash".into(), FindTaskSource::CmdArgs, None) + .await .expect("task in a foreign-platform environment should be found"); assert_eq!(env.name().as_str(), "riscv"); } - #[test] - fn test_find_task_default_defined() { + #[tokio::test] + async fn test_find_task_default_defined() { let manifest_str = r#" [project] name = "foo" @@ -329,13 +337,15 @@ mod tests { let project = Workspace::from_str(Path::new("pixi.toml"), manifest_str).unwrap(); let env = project.default_environment(); let search = SearchEnvironments::from_opt_env(&project, None, env.best_declared_platform()); - let result = search.find_task("test".into(), FindTaskSource::CmdArgs, None); + let result = search + .find_task("test".into(), FindTaskSource::CmdArgs, None) + .await; assert!(result.is_ok()); assert!(result.unwrap().0.name().is_default()); } - #[test] - fn test_find_task_dual_defined() { + #[tokio::test] + async fn test_find_task_dual_defined() { let manifest_str = r#" [project] name = "foo" @@ -353,12 +363,14 @@ mod tests { "#; let project = Workspace::from_str(Path::new("pixi.toml"), manifest_str).unwrap(); let search = SearchEnvironments::from_opt_env(&project, None, None); - let result = search.find_task("test".into(), FindTaskSource::CmdArgs, None); + let result = search + .find_task("test".into(), FindTaskSource::CmdArgs, None) + .await; assert!(matches!(result, Err(FindTaskError::AmbiguousTask(_)))); } - #[test] - fn test_find_task_explicit_defined() { + #[tokio::test] + async fn test_find_task_explicit_defined() { let manifest_str = r#" [project] name = "foo" @@ -382,18 +394,22 @@ mod tests { "#; let project = Workspace::from_str(Path::new("pixi.toml"), manifest_str).unwrap(); let search = SearchEnvironments::from_opt_env(&project, None, None); - let result = search.find_task("test".into(), FindTaskSource::CmdArgs, None); + let result = search + .find_task("test".into(), FindTaskSource::CmdArgs, None) + .await; assert!(matches!(result, Err(FindTaskError::AmbiguousTask(_)))); // With explicit environment let search = SearchEnvironments::from_opt_env(&project, Some(project.default_environment()), None); - let result = search.find_task("test".into(), FindTaskSource::CmdArgs, None); + let result = search + .find_task("test".into(), FindTaskSource::CmdArgs, None) + .await; assert!(result.unwrap().0.name().is_default()); } - #[test] - fn test_find_non_default_feature_task() { + #[tokio::test] + async fn test_find_non_default_feature_task() { let manifest_str = r#" [project] name = "foo" @@ -417,7 +433,9 @@ mod tests { "#; let project = Workspace::from_str(Path::new("pixi.toml"), manifest_str).unwrap(); let search = SearchEnvironments::from_opt_env(&project, None, None); - let result = search.find_task("test".into(), FindTaskSource::CmdArgs, None); + let result = search + .find_task("test".into(), FindTaskSource::CmdArgs, None) + .await; assert!(result.unwrap().0.name().is_default()); // With explicit environment @@ -426,12 +444,14 @@ mod tests { Some(project.environment("prod").unwrap()), None, ); - let result = search.find_task("test".into(), FindTaskSource::CmdArgs, None); + let result = search + .find_task("test".into(), FindTaskSource::CmdArgs, None) + .await; assert!(matches!(result, Err(FindTaskError::MissingTask(_)))); } - #[test] - fn test_find_ambiguous_task() { + #[tokio::test] + async fn test_find_ambiguous_task() { let manifest_str = r#" [project] name = "foo" @@ -449,14 +469,16 @@ mod tests { "#; let project = Workspace::from_str(Path::new("pixi.toml"), manifest_str).unwrap(); let search = SearchEnvironments::from_opt_env(&project, None, None); - let result = search.find_task("bla".into(), FindTaskSource::CmdArgs, None); + let result = search + .find_task("bla".into(), FindTaskSource::CmdArgs, None) + .await; // Ambiguous task because it is the same name and code but it is defined in // different environments assert!(matches!(result, Err(FindTaskError::AmbiguousTask(_)))); } - #[test] - fn test_default_environment_preferred_when_multiple_envs() { + #[tokio::test] + async fn test_default_environment_preferred_when_multiple_envs() { let manifest_str = r#" [workspace] channels = [] @@ -490,7 +512,7 @@ mod tests { // Build a SearchEnvironments that will prefer a candidate environment // whose task declares a `default-environment` matching the env name. let search = SearchEnvironments::from_opt_env(&project, None, None).with_disambiguate_fn( - |amb: &AmbiguousTask| { + async |amb: &AmbiguousTask| { amb.environments .iter() .find(|(env, task)| { @@ -507,12 +529,13 @@ mod tests { // When resolving `test3` we expect the it to pick `three`. let result = search .find_task("test3".into(), FindTaskSource::CmdArgs, None) + .await .expect("should pick default environment"); assert_eq!(result.0.name().as_str(), "three"); } - #[test] - fn test_explicit_environment_overrides_task_default_environment() { + #[tokio::test] + async fn test_explicit_environment_overrides_task_default_environment() { let manifest_str = r#" [project] name = "foo" @@ -537,13 +560,15 @@ mod tests { // though the task has a `default-environment` set to `three`. let explicit_env = project.environment("two").unwrap(); let search = SearchEnvironments::from_opt_env(&project, Some(explicit_env), None); - let result = search.find_task("test3".into(), FindTaskSource::CmdArgs, None); + let result = search + .find_task("test3".into(), FindTaskSource::CmdArgs, None) + .await; assert!(result.is_ok()); assert_eq!(result.unwrap().0.name().as_str(), "two"); } - #[test] - fn test_top_level_task_default_environment_is_used() { + #[tokio::test] + async fn test_top_level_task_default_environment_is_used() { let manifest_str = r#" [workspace] channels = [] @@ -567,6 +592,7 @@ mod tests { // we expect the resolved environment to be `test` rather than the default. let result = search .find_task("test".into(), FindTaskSource::CmdArgs, None) + .await .expect("should resolve to an environment"); assert_eq!(result.0.name().as_str(), "test"); } diff --git a/crates/pixi_task/src/task_graph.rs b/crates/pixi_task/src/task_graph.rs index aa07a4b188..ccb1cdf66e 100644 --- a/crates/pixi_task/src/task_graph.rs +++ b/crates/pixi_task/src/task_graph.rs @@ -223,7 +223,7 @@ impl<'p> TaskGraph<'p> { /// name exists. Instead, the arguments are treated as a custom command to /// execute within the environment, allowing running executables that /// collide with task names. - pub fn from_cmd_args>( + pub async fn from_cmd_args>( project: &'p Workspace, search_envs: &SearchEnvironments<'p, D>, args: Vec, @@ -248,7 +248,10 @@ impl<'p> TaskGraph<'p> { && let Some(name) = args.first() { let task_name = TaskName::from(name.clone()); - match search_envs.find_task(task_name.clone(), FindTaskSource::CmdArgs, None) { + match search_envs + .find_task(task_name.clone(), FindTaskSource::CmdArgs, None) + .await + { Err(FindTaskError::MissingTask(_)) => { // The name may still be a task elsewhere (another env or // platform); a shell fallback would say "command not found". @@ -361,7 +364,8 @@ impl<'p> TaskGraph<'p> { .map(|a| TypedDependencyArg::Positional(a.clone())) .collect(), ), - ); + ) + .await; } } } @@ -409,10 +413,11 @@ impl<'p> TaskGraph<'p> { }, None, ) + .await } /// Constructs a new instance of a [`TaskGraph`] from a root task. - fn from_root>( + async fn from_root>( project: &'p Workspace, search_environments: &SearchEnvironments<'p, D>, root: TaskNode<'p>, @@ -479,11 +484,14 @@ impl<'p> TaskGraph<'p> { .expect("task environments are validated when the manifest is parsed") }); - let (task_env, task_dependency) = match search_environments.find_task( - dependency.task_name.clone(), - FindTaskSource::DependsOn(node_name, task_ref), - task_specific_environment, - ) { + let (task_env, task_dependency) = match search_environments + .find_task( + dependency.task_name.clone(), + FindTaskSource::DependsOn(node_name, task_ref), + task_specific_environment, + ) + .await + { Err(FindTaskError::MissingTask(err)) => { return Err(TaskGraphError::MissingTask(err)); } @@ -801,7 +809,7 @@ mod test { self } - fn build_graph(&self) -> Result<(Workspace, Vec), TaskGraphError> { + async fn build_graph(&self) -> Result<(Workspace, Vec), TaskGraphError> { let project = Workspace::from_str(Path::new("pixi.toml"), self.workspace_str).unwrap(); let environment = self .environment_name @@ -829,7 +837,8 @@ mod test { self.skip_deps, self.prefer_executable, self.templated, - )?; + ) + .await?; let commands = graph .topological_order() @@ -850,12 +859,12 @@ mod test { Ok((project, commands)) } - fn commands_in_order(&self) -> Vec { - self.build_graph().unwrap().1 + async fn commands_in_order(&self) -> Vec { + self.build_graph().await.unwrap().1 } - fn expect_error(&self) -> TaskGraphError { - self.build_graph().unwrap_err() + async fn expect_error(&self) -> TaskGraphError { + self.build_graph().await.unwrap_err() } } @@ -863,8 +872,8 @@ mod test { /// fall through to "execute as a shell command" (which reports a /// confusing "command not found") -- it errors, pointing at the /// environments that define the task. - #[test] - fn test_task_in_other_environment_is_not_treated_as_executable() { + #[tokio::test] + async fn test_task_in_other_environment_is_not_treated_as_executable() { let workspace_str = r#" [workspace] name = "pixi" @@ -883,7 +892,8 @@ mod test { "#; let err = TaskGraphTest::new(workspace_str, &["test"]) .environment("prod") - .expect_error(); + .expect_error() + .await; assert_matches!(err, TaskGraphError::UnrunnableTask(err) => { assert_eq!(err.task_name.as_str(), "test"); assert_eq!( @@ -896,8 +906,8 @@ mod test { /// The platform axis of the same guard: a task defined only for another /// *platform* must also surface as an `UnrunnableTask` carrying the pinned /// platform, rather than falling through to a shell command. - #[test] - fn test_task_on_other_platform_reports_pinned_platform() { + #[tokio::test] + async fn test_task_on_other_platform_reports_pinned_platform() { let workspace_str = r#" [workspace] name = "pixi" @@ -909,7 +919,8 @@ mod test { "#; let err = TaskGraphTest::new(workspace_str, &["mac-only"]) .platform(Subdir::Linux64) - .expect_error(); + .expect_error() + .await; assert_matches!(err, TaskGraphError::UnrunnableTask(err) => { assert_eq!(err.task_name.as_str(), "mac-only"); assert_eq!(err.platform.as_ref().map(|p| p.as_str()), Some("linux-64")); @@ -920,8 +931,8 @@ mod test { }); } - #[test] - fn test_ordered_commands() { + #[tokio::test] + async fn test_ordered_commands() { let workspace_str = r#" [workspace] name = "pixi" @@ -935,15 +946,17 @@ mod test { top = { cmd = "echo top", depends-on = ["task1", "task2"] } "#; let run_args = &["top", "--test"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!( commands, vec!["echo root", "echo task1", "echo task2", "echo top '--test'"] ); } - #[test] - fn test_cycle_ordered_commands() { + #[tokio::test] + async fn test_cycle_ordered_commands() { let workspace_str = r#" [workspace] name = "pixi" @@ -957,15 +970,17 @@ mod test { top = { cmd = "echo top", depends-on = ["task1", "task2"] } "#; let run_args = &["top"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!( commands, vec!["echo root", "echo task1", "echo task2", "echo top"] ); } - #[test] - fn test_platform_ordered_commands() { + #[tokio::test] + async fn test_platform_ordered_commands() { let workspace_str = r#" [workspace] name = "pixi" @@ -986,7 +1001,8 @@ mod test { // Linux should give hello linux let commands = TaskGraphTest::new(workspace_str, run_args) .platform(Subdir::Linux64) - .commands_in_order(); + .commands_in_order() + .await; assert_eq!( commands, vec!["echo linux", "echo task1", "echo task2", "echo top"] @@ -995,15 +1011,16 @@ mod test { // On other platforms we should get echo root let commands = TaskGraphTest::new(workspace_str, run_args) .platform(Subdir::OsxArm64) - .commands_in_order(); + .commands_in_order() + .await; assert_eq!( commands, vec!["echo root", "echo task1", "echo task2", "echo top"] ); } - #[test] - fn test_custom_command() { + #[tokio::test] + async fn test_custom_command() { let workspace_str = r#" [workspace] name = "pixi" @@ -1011,12 +1028,14 @@ mod test { platforms = ["linux-64", "osx-64", "win-64", "osx-arm64", "linux-riscv64"] "#; let run_args = &["echo bla"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo bla"]); } - #[test] - fn test_multi_env() { + #[tokio::test] + async fn test_multi_env() { let workspace_str = r#" [workspace] name = "pixi" @@ -1030,12 +1049,14 @@ mod test { build = ["build"] "#; let run_args = &["build"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo build"]); } - #[test] - fn test_multi_env_default() { + #[tokio::test] + async fn test_multi_env_default() { let workspace_str = r#" [workspace] name = "pixi" @@ -1052,12 +1073,14 @@ mod test { build = ["build"] "#; let run_args = &["start"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["hello world"]); } - #[test] - fn test_cross_env_task_dependency() { + #[tokio::test] + async fn test_cross_env_task_dependency() { let workspace_str = r#" [workspace] name = "pixi" @@ -1080,15 +1103,16 @@ mod test { let run_args = &["start"]; let commands = TaskGraphTest::new(workspace_str, run_args) .environment("cuda") - .commands_in_order(); + .commands_in_order() + .await; assert_eq!( commands, vec!["python train.py --cuda", "python test.py --cuda"] ); } - #[test] - fn test_multi_env_defaults() { + #[tokio::test] + async fn test_multi_env_defaults() { let workspace_str = r#" [workspace] name = "pixi" @@ -1106,12 +1130,14 @@ mod test { build = ["build"] "#; let run_args = &["foobar"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo foo", "echo bar"]); } - #[test] - fn test_multi_env_defaults_ambiguity() { + #[tokio::test] + async fn test_multi_env_defaults_ambiguity() { let workspace_str = r#" [workspace] name = "pixi" @@ -1132,12 +1158,14 @@ mod test { let run_args = &["foobar"]; // As foo is really ambiguous it should error - let error = TaskGraphTest::new(workspace_str, run_args).expect_error(); + let error = TaskGraphTest::new(workspace_str, run_args) + .expect_error() + .await; assert_matches!(error, TaskGraphError::AmbiguousTask(_)); } - #[test] - fn test_skip_deps() { + #[tokio::test] + async fn test_skip_deps() { let workspace_str = r#" [workspace] name = "pixi" @@ -1151,10 +1179,13 @@ mod test { let run_args = &["bar"]; let with_skip = TaskGraphTest::new(workspace_str, run_args) .skip_deps() - .commands_in_order(); + .commands_in_order() + .await; assert_eq!(with_skip, vec!["echo bar"]); - let without_skip = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let without_skip = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(without_skip, vec!["echo foo", "echo bar"]); } @@ -1183,8 +1214,8 @@ mod test { /// /// Verifies that `pixi run echo '{{ hello }}'` does not fail when /// templating is disabled (the default for CLI commands). - #[test] - fn test_custom_command_with_braces_no_template() { + #[tokio::test] + async fn test_custom_command_with_braces_no_template() { let workspace_str = r#" [workspace] name = "pixi" @@ -1209,6 +1240,7 @@ mod test { PreferExecutable::TaskFirst, false, ) + .await .expect("should not fail with braces when templating is disabled"); let order = graph.topological_order(); @@ -1231,8 +1263,8 @@ mod test { /// Verifies that `pixi run --templated echo '{{ pixi.platform }}'` /// renders template variables. - #[test] - fn test_custom_command_with_templated_flag() { + #[tokio::test] + async fn test_custom_command_with_templated_flag() { let workspace_str = r#" [workspace] name = "pixi" @@ -1257,6 +1289,7 @@ mod test { PreferExecutable::TaskFirst, true, ) + .await .expect("should succeed with valid template variable"); let order = graph.topological_order(); @@ -1277,8 +1310,8 @@ mod test { assert!(!cmd.contains("{{")); } - #[test] - fn test_choices_valid_value() { + #[tokio::test] + async fn test_choices_valid_value() { let workspace_str = r#" [workspace] name = "pixi" @@ -1290,12 +1323,14 @@ mod test { args = [{ arg = "target", choices = ["debug", "release"] }] "#; let run_args = &["build", "debug"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo debug"]); } - #[test] - fn test_choices_invalid_value() { + #[tokio::test] + async fn test_choices_invalid_value() { let workspace_str = r#" [workspace] name = "pixi" @@ -1307,7 +1342,9 @@ mod test { args = [{ arg = "target", choices = ["debug", "release"] }] "#; let run_args = &["build", "profile"]; - let error = TaskGraphTest::new(workspace_str, run_args).expect_error(); + let error = TaskGraphTest::new(workspace_str, run_args) + .expect_error() + .await; assert_matches!(error, TaskGraphError::InvalidArgValue(err) => { assert_eq!(err.arg, "target"); assert_eq!(err.task, "build"); @@ -1316,8 +1353,8 @@ mod test { }); } - #[test] - fn test_choices_missing_value() { + #[tokio::test] + async fn test_choices_missing_value() { let workspace_str = r#" [workspace] name = "pixi" @@ -1329,7 +1366,9 @@ mod test { args = [{ arg = "target", choices = ["debug", "release"] }] "#; let run_args = &["build"]; - let error = TaskGraphTest::new(workspace_str, run_args).expect_error(); + let error = TaskGraphTest::new(workspace_str, run_args) + .expect_error() + .await; assert_matches!(error, TaskGraphError::MissingArgument(err) => { assert_eq!(err.arg, "target"); assert_eq!(err.task, "build"); @@ -1337,8 +1376,8 @@ mod test { }); } - #[test] - fn test_choices_with_valid_default() { + #[tokio::test] + async fn test_choices_with_valid_default() { let workspace_str = r#" [workspace] name = "pixi" @@ -1349,12 +1388,14 @@ mod test { args = [{ arg = "target", default = "debug", choices = ["debug", "release"] }] "#; let run_args = &["build"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo debug"]); } - #[test] - fn test_prefer_executable_always() { + #[tokio::test] + async fn test_prefer_executable_always() { let workspace_str = r#" [workspace] name = "pixi" @@ -1378,6 +1419,7 @@ mod test { PreferExecutable::Always, false, ) + .await .expect("graph should be created"); // When PreferExecutable::Always is used, we should not resolve the command @@ -1388,8 +1430,8 @@ mod test { assert!(task.name.is_none()); } - #[test] - fn test_double_dash_separator_with_typed_args() { + #[tokio::test] + async fn test_double_dash_separator_with_typed_args() { // pixi run build release -- --verbose // "release" fills the typed arg; "--verbose" is forwarded to the command. // `--` can appear anywhere after the task name for tasks with typed args. @@ -1404,12 +1446,14 @@ mod test { args = [{ arg = "target", choices = ["debug", "release"] }] "#; let run_args = &["build", "release", "--", "--verbose"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo release '--verbose'"]); } - #[test] - fn test_double_dash_with_defaults_only() { + #[tokio::test] + async fn test_double_dash_with_defaults_only() { // pixi run build -- --verbose // No typed arg value provided; default "debug" is used; "--verbose" is appended let workspace_str = r#" @@ -1423,12 +1467,14 @@ mod test { args = [{ arg = "target", default = "debug", choices = ["debug", "release"] }] "#; let run_args = &["build", "--", "--verbose"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo debug '--verbose'"]); } - #[test] - fn test_double_dash_with_free_form_args() { + #[tokio::test] + async fn test_double_dash_with_free_form_args() { // Task has NO typed args. `--` only acts as a separator when it is the // first arg after the task name. Here "arg1" comes first, so `--` is // passed through verbatim to the underlying command. @@ -1442,12 +1488,14 @@ mod test { cmd = "echo" "#; let run_args = &["mytask", "arg1", "--", "arg2"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo 'arg1' '--' 'arg2'"]); } - #[test] - fn test_double_dash_first_with_free_form_args() { + #[tokio::test] + async fn test_double_dash_first_with_free_form_args() { // Task has NO typed args. `--` is the first arg after the task name, so // it is consumed by pixi and the remaining args are forwarded. let workspace_str = r#" @@ -1460,12 +1508,14 @@ mod test { cmd = "echo" "#; let run_args = &["mytask", "--", "arg1", "arg2"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo 'arg1' 'arg2'"]); } - #[test] - fn test_double_dash_custom_command_preserved() { + #[tokio::test] + async fn test_double_dash_custom_command_preserved() { // Custom commands (no pixi task found) pass `--` through verbatim. // e.g. `pixi run git log -- somefile` let workspace_str = r#" @@ -1475,12 +1525,14 @@ mod test { platforms = ["linux-64"] "#; let run_args = &["git", "log", "--", "somefile"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["'git' 'log' '--' 'somefile'"]); } - #[test] - fn test_double_dash_no_extra_args() { + #[tokio::test] + async fn test_double_dash_no_extra_args() { // Trailing "--" with nothing after it, and `--` is the first arg after // the task name, so it is consumed. Task uses its default value. let workspace_str = r#" @@ -1494,12 +1546,14 @@ mod test { args = [{ arg = "target", default = "debug", choices = ["debug", "release"] }] "#; let run_args = &["build", "--"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo debug"]); } - #[test] - fn test_double_dash_prefix_not_separator() { + #[tokio::test] + async fn test_double_dash_prefix_not_separator() { // `--verbose` is NOT the `--` separator — only the exact token `--` // (with a space after it) is recognised. A flag like `--verbose` as the // first arg is forwarded to the underlying command unchanged. @@ -1513,14 +1567,16 @@ mod test { cmd = "echo" "#; let run_args = &["mytask", "--verbose"]; - let commands = TaskGraphTest::new(workspace_str, run_args).commands_in_order(); + let commands = TaskGraphTest::new(workspace_str, run_args) + .commands_in_order() + .await; assert_eq!(commands, vec!["echo '--verbose'"]); } /// `{{ pixi.platform }}` renders the platform the search was pinned to /// (https://github.com/prefix-dev/pixi/issues/6773). - #[test] - fn test_platform_template_renders_pinned_custom_platform() { + #[tokio::test] + async fn test_platform_template_renders_pinned_custom_platform() { let current = Subdir::current().unwrap_or(Subdir::NoArch); let workspace_str = format!( r#" @@ -1541,6 +1597,7 @@ mod test { let (_project, commands) = TaskGraphTest::new(&workspace_str, &["which-platform"]) .named_platform("local") .build_graph() + .await .unwrap(); assert_eq!(commands, vec!["echo local"]); @@ -1548,6 +1605,7 @@ mod test { // first, no extra virtual packages), so that is what renders. let (_project, commands) = TaskGraphTest::new(&workspace_str, &["which-platform"]) .build_graph() + .await .unwrap(); assert_eq!(commands, vec!["echo generic"]); } From f4b3fba3968726820c00d3567653ff6660c55713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20H=C3=A4cker?= Date: Mon, 26 Oct 2026 10:26:12 +0100 Subject: [PATCH 3/6] Remove no longer needed deps --- Cargo.lock | 2 -- crates/pixi_cli/Cargo.toml | 2 -- 2 files changed, 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 422fc7a3de..7b76f19aee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6528,7 +6528,6 @@ dependencies = [ "tempfile", "thiserror 2.0.21", "tokio", - "tokio-util 0.7.20", "toml_edit", "tracing", "tracing-subscriber", @@ -6538,7 +6537,6 @@ dependencies = [ "uv-pypi-types", "uv-requirements-txt", "uv-threads", - "which", "zip", ] diff --git a/crates/pixi_cli/Cargo.toml b/crates/pixi_cli/Cargo.toml index cae09b96e1..6b0cac4274 100644 --- a/crates/pixi_cli/Cargo.toml +++ b/crates/pixi_cli/Cargo.toml @@ -95,7 +95,6 @@ temp-env = { workspace = true } tempfile = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true, features = ["macros", "rt-multi-thread", "signal"] } -tokio-util = { workspace = true } toml_edit = { workspace = true, features = ["parse", "serde"] } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } @@ -105,7 +104,6 @@ uv-pep508 = { workspace = true } uv-pypi-types = { workspace = true } uv-requirements-txt = { workspace = true } uv-threads = { workspace = true } -which = { workspace = true } zip = { workspace = true, features = ["deflate", "time"] } [dev-dependencies] From 4a64c575bf2009fb6a24e26f7ae1575871d24023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20H=C3=A4cker?= Date: Mon, 26 Oct 2026 10:35:04 +0100 Subject: [PATCH 4/6] Update into_diagnostic baseline --- scripts/into_diagnostic_baseline.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/into_diagnostic_baseline.toml b/scripts/into_diagnostic_baseline.toml index 478ac2c8a0..da5bdac430 100644 --- a/scripts/into_diagnostic_baseline.toml +++ b/scripts/into_diagnostic_baseline.toml @@ -1,5 +1,5 @@ # Generated by `scripts/check_into_diagnostic.py --update`. Crates not listed are pinned at 0. -pixi_api = 45 +pixi_api = 96 pixi_build_backend = 40 pixi_build_cmake = 1 pixi_build_mojo = 2 @@ -8,7 +8,7 @@ pixi_build_r = 16 pixi_build_rattler_build = 4 pixi_build_ros = 3 pixi_build_rust = 1 -pixi_cli = 249 +pixi_cli = 198 pixi_core = 1 pixi_global = 34 pixi_test_utils = 5 From 205f1567036903d0161f6d6e26ccbea54cd58a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20H=C3=A4cker?= Date: Tue, 27 Oct 2026 16:27:57 +0100 Subject: [PATCH 5/6] Error early when run is called without a task --- crates/pixi_api/src/workspace/run/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/pixi_api/src/workspace/run/mod.rs b/crates/pixi_api/src/workspace/run/mod.rs index 4307d82f31..b684204fcf 100644 --- a/crates/pixi_api/src/workspace/run/mod.rs +++ b/crates/pixi_api/src/workspace/run/mod.rs @@ -81,6 +81,11 @@ pub async fn run( options: RunOptions, progress: Option<&Arc>, ) -> miette::Result { + // Fail before resolving the lock file, there is nothing to run + if options.task.iter().all(|arg| arg.trim().is_empty()) { + miette::bail!("no task or command provided"); + } + let explicit_environment = explicit_environment(workspace, options.environment.as_ref())?; let environment = explicit_environment .clone() From c4a6b41ed716fd6ab2875c5f913e6808b9714674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20H=C3=A4cker?= Date: Wed, 28 Oct 2026 14:48:15 +0100 Subject: [PATCH 6/6] Remove some unnecessary comments --- crates/pixi_api/src/workspace/run/mod.rs | 3 --- crates/pixi_api/src/workspace/shell/mod.rs | 1 - 2 files changed, 4 deletions(-) diff --git a/crates/pixi_api/src/workspace/run/mod.rs b/crates/pixi_api/src/workspace/run/mod.rs index b684204fcf..7a77aa970c 100644 --- a/crates/pixi_api/src/workspace/run/mod.rs +++ b/crates/pixi_api/src/workspace/run/mod.rs @@ -163,7 +163,6 @@ pub async fn run( let task_graph = build_task_graph(interface, workspace, explicit_environment, &options).await?; tracing::debug!("Task graph: {}", task_graph); - // Print dry-run message if dry-run mode is enabled if options.dry_run { pixi_progress::println!( "{}{}\n\n", @@ -232,7 +231,6 @@ pub async fn run( && (!executable_task.task().is_custom() || options.display_command.is_some()) { if task_idx > 0 { - // Add a newline between task outputs pixi_progress::println!(); } @@ -348,7 +346,6 @@ pub async fn run( )?; } - // Clear the current progress reports. if let Some(progress) = &progress { progress.on_clear(); } diff --git a/crates/pixi_api/src/workspace/shell/mod.rs b/crates/pixi_api/src/workspace/shell/mod.rs index d1f8085e49..8eaa421636 100644 --- a/crates/pixi_api/src/workspace/shell/mod.rs +++ b/crates/pixi_api/src/workspace/shell/mod.rs @@ -279,7 +279,6 @@ async fn start_nu_shell( let mut shell_script = ShellScript::new(shell, Subdir::current().unwrap_or(Subdir::NoArch)); for (key, value) in env { if key == "PATH" { - // split path with PATHSEP let paths = std::env::split_paths(value).collect::>(); shell_script .set_path(&paths, PathModificationBehavior::Replace)