Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions crates/pixi_api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::workspace::{
AddError, ChannelError, ChannelOptions, DependencyOptions, EditActivationError,
EnvironmentError, InitError, InitOptions, Package, PlatformError, PreviewFlagsError,
ReinstallOptions, RemoveError, RemoveFeatureError, RunOptions, SetDescriptionError,
SetNameError, ShellError, ShellOptions,
SetNameError, ShellError, ShellOptions, TaskError,
};

pub struct DefaultContext<I: Interface> {
Expand Down Expand Up @@ -569,9 +569,11 @@ impl<I: Interface> WorkspaceContext<I> {
pub async fn list_tasks(
&self,
environment: Option<EnvironmentName>,
) -> miette::Result<HashMap<EnvironmentName, (EnvironmentRunnability, HashMap<TaskName, Task>)>>
{
crate::workspace::task::list_tasks(&self.workspace, environment).await
) -> Result<
HashMap<EnvironmentName, (EnvironmentRunnability, HashMap<TaskName, Task>)>,
TaskError,
> {
crate::workspace::task::list_tasks(&self.interface, &self.workspace, environment).await
}

pub async fn add_task(
Expand All @@ -580,7 +582,7 @@ impl<I: Interface> WorkspaceContext<I> {
task: Task,
feature: FeatureName,
platform: Option<PixiPlatformName>,
) -> miette::Result<()> {
) -> Result<(), TaskError> {
crate::workspace::task::add_task(
&self.interface,
self.workspace_mut()?,
Expand All @@ -598,7 +600,7 @@ impl<I: Interface> WorkspaceContext<I> {
task: Task,
feature: FeatureName,
platform: Option<PixiPlatformName>,
) -> miette::Result<()> {
) -> Result<(), TaskError> {
crate::workspace::task::alias_task(
&self.interface,
self.workspace_mut()?,
Expand All @@ -615,7 +617,7 @@ impl<I: Interface> WorkspaceContext<I> {
names: Vec<TaskName>,
platform: Option<PixiPlatformName>,
feature: FeatureName,
) -> miette::Result<()> {
) -> Result<(), TaskError> {
crate::workspace::task::remove_tasks(
&self.interface,
self.workspace_mut()?,
Expand Down
1 change: 1 addition & 0 deletions crates/pixi_api/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub(crate) mod shell;
pub use shell::{ShellError, ShellOptions};

pub(crate) mod task;
pub use task::TaskError;

#[allow(clippy::module_inception)]
pub(crate) mod workspace;
Expand Down
90 changes: 68 additions & 22 deletions crates/pixi_api/src/workspace/task/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
use std::collections::{HashMap, HashSet};
use std::{
collections::{HashMap, HashSet},
error::Error,
};

use fancy_display::FancyDisplay;
use miette::IntoDiagnostic;
use miette::Diagnostic;
use pixi_core::{
Workspace,
workspace::{
Environment, WorkspaceMut,
Environment, EnvironmentFromNameError, WorkspaceMut,
virtual_packages::{EnvironmentRunnability, classify_environment_runnability},
},
};
use pixi_manifest::{
EnvironmentName, FeatureName, HasWorkspaceManifest, PixiPlatform, PixiPlatformName, Task,
TaskName,
EnvironmentName, FeatureName, GetFeatureError, HasWorkspaceManifest, LoadManifestsError,
PixiPlatform, PixiPlatformName, PlatformEditError, Task, TaskEditError, TaskName,
};
use thiserror::Error;

use crate::interface::Interface;
use crate::workspace::platforms::resolve_platforms;
use crate::workspace::{
platforms::resolve_platforms,
workspace::{SaveManifestError, save},
};

#[derive(Debug, Error, Diagnostic)]
pub enum TaskError {
#[error(transparent)]
#[diagnostic(transparent)]
LoadWorkspace(#[from] LoadManifestsError),

#[error(transparent)]
#[diagnostic(transparent)]
EnvironmentFromName(#[from] EnvironmentFromNameError),

#[error(transparent)]
#[diagnostic(transparent)]
Platform(#[from] PlatformEditError),

#[error(transparent)]
#[diagnostic(transparent)]
Edit(#[from] TaskEditError),

#[error(transparent)]
#[diagnostic(transparent)]
Feature(#[from] GetFeatureError),

#[error(transparent)]
#[diagnostic(transparent)]
Save(#[from] SaveManifestError),
}

/// Resolve `name` the same way the dependency CLI does: look it up in the
/// workspace; if it's not declared, accept it as a bare conda subdir and
Expand All @@ -27,7 +61,7 @@ use crate::workspace::platforms::resolve_platforms;
fn resolve_task_platform(
workspace: &Workspace,
name: Option<&PixiPlatformName>,
) -> miette::Result<Option<PixiPlatform>> {
) -> Result<Option<PixiPlatform>, PlatformEditError> {
let Some(name) = name else { return Ok(None) };
let workspace_platforms = workspace.workspace_manifest().workspace.platforms.clone();
Ok(
Expand All @@ -47,7 +81,7 @@ fn declare_platform_and_add_task(
task: &Task,
feature: &FeatureName,
platform: Option<&PixiPlatformName>,
) -> miette::Result<()> {
) -> Result<(), TaskError> {
let pixi_platform = resolve_task_platform(workspace.workspace(), platform)?;
// The auto-declare is idempotent on already-declared entries.
if let Some(p) = &pixi_platform {
Expand All @@ -61,23 +95,35 @@ fn declare_platform_and_add_task(
Ok(())
}

pub async fn list_tasks(
pub async fn list_tasks<I: Interface>(
interface: &I,
workspace: &Workspace,
environment: Option<EnvironmentName>,
) -> miette::Result<HashMap<EnvironmentName, (EnvironmentRunnability, HashMap<TaskName, Task>)>> {
) -> Result<HashMap<EnvironmentName, (EnvironmentRunnability, HashMap<TaskName, Task>)>, TaskError>
{
let explicit_environment = environment
.map(|n| {
workspace
.environment(&n)
.ok_or_else(|| miette::miette!("unknown environment '{n}'"))
.ok_or(EnvironmentFromNameError::Unknown(n))
})
.transpose()?;

let lock_file = workspace
.load_lock_file()
.await
.ok()
.map(|r| r.into_lock_file_or_empty_with_warning());
// The lock file only refines the runnability of environments the machine
// doesn't declare, so a broken one shouldn't stop the listing.
let lock_file = match workspace.load_lock_file().await {
Ok(result) => Some(result.into_lock_file_or_empty_with_warning()),
Err(err) => {
let mut message = err.to_string();
let mut source = err.source();
while let Some(cause) = source {
message.push_str(&format!(": {cause}"));
source = cause.source();
}
interface.warning(&message).await;
None
}
};

let env_task_map: HashMap<Environment, (EnvironmentRunnability, HashSet<TaskName>)> =
if let Some(explicit_environment) = explicit_environment {
Expand Down Expand Up @@ -123,9 +169,9 @@ pub async fn add_task<I: Interface>(
task: Task,
feature: FeatureName,
platform: Option<PixiPlatformName>,
) -> miette::Result<()> {
) -> Result<(), TaskError> {
declare_platform_and_add_task(&mut workspace, &name, &task, &feature, platform.as_ref())?;
workspace.save().await.into_diagnostic()?;
save(workspace).await?;

interface
.success(&format!(
Expand All @@ -145,9 +191,9 @@ pub async fn alias_task<I: Interface>(
task: Task,
feature: FeatureName,
platform: Option<PixiPlatformName>,
) -> miette::Result<()> {
) -> Result<(), TaskError> {
declare_platform_and_add_task(&mut workspace, &name, &task, &feature, platform.as_ref())?;
workspace.save().await.into_diagnostic()?;
save(workspace).await?;

interface
.success(&format!(
Expand All @@ -166,7 +212,7 @@ pub async fn remove_tasks<I: Interface>(
names: Vec<TaskName>,
platform: Option<PixiPlatformName>,
feature: FeatureName,
) -> miette::Result<()> {
) -> Result<(), TaskError> {
let mut to_remove = Vec::new();

// No auto-declare on removal: if the name doesn't match a declared
Expand Down Expand Up @@ -223,7 +269,7 @@ pub async fn remove_tasks<I: Interface>(
removed.push(name);
}

workspace.save().await.into_diagnostic()?;
save(workspace).await?;

for name in removed {
interface
Expand Down
4 changes: 3 additions & 1 deletion crates/pixi_cli/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ async fn remove_tasks(

workspace_ctx
.remove_task(args.names, args.platform, feature)
.await
.await?;

Ok(())
}

fn print_tasks_json(project: &Workspace) -> miette::Result<()> {
Expand Down
6 changes: 3 additions & 3 deletions crates/pixi_manifest/src/manifests/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2187,10 +2187,10 @@ pub struct FeatureNotFoundError(pub FeatureName);
/// [`WorkspaceManifestMut::remove_task`].
#[derive(Debug, thiserror::Error, miette::Diagnostic)]
pub enum TaskEditError {
#[error("task {0} already exists")]
#[error("task '{0}' already exists")]
AlreadyExists(TaskName),

#[error("task {0} does not exist")]
#[error("task '{0}' does not exist")]
NotFound(TaskName),

#[error("the environment '{0}' does not exist")]
Expand Down Expand Up @@ -7321,7 +7321,7 @@ platforms = [
)
.unwrap_err();
assert_matches::assert_matches!(&err, TaskEditError::AlreadyExists(name) if *name == hello);
assert_eq!(err.to_string(), "task hello already exists");
assert_eq!(err.to_string(), "task 'hello' already exists");
let err = manifest
.remove_task(TaskName::from("nope"), None, &FeatureName::Default)
.unwrap_err();
Expand Down
2 changes: 1 addition & 1 deletion scripts/into_diagnostic_baseline.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated by `scripts/check_into_diagnostic.py --update`. Crates not listed are pinned at 0.
pixi_api = 10
pixi_api = 7
pixi_build_backend = 40
pixi_build_cmake = 1
pixi_build_mojo = 2
Expand Down
Loading