workspace: Pop a toast if manually spawning a task fails (#38405)

Lukas Wirth created

Release Notes:

- N/A *or* Added/Fixed/Improved ...

Change summary

crates/workspace/src/tasks.rs | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

Detailed changes

crates/workspace/src/tasks.rs 🔗

@@ -8,7 +8,7 @@ use remote::ConnectionState;
 use task::{DebugScenario, ResolvedTask, SpawnInTerminal, TaskContext, TaskTemplate};
 use ui::Window;
 
-use crate::Workspace;
+use crate::{Toast, Workspace, notifications::NotificationId};
 
 impl Workspace {
     pub fn schedule_task(
@@ -73,8 +73,10 @@ impl Workspace {
 
         if let Some(terminal_provider) = self.terminal_provider.as_ref() {
             let task_status = terminal_provider.spawn(spawn_in_terminal, window, cx);
-            let task = cx.background_spawn(async move {
-                match task_status.await {
+
+            let task = cx.spawn(async |w, cx| {
+                let res = cx.background_spawn(task_status).await;
+                match res {
                     Some(Ok(status)) => {
                         if status.success() {
                             log::debug!("Task spawn succeeded");
@@ -82,9 +84,15 @@ impl Workspace {
                             log::debug!("Task spawn failed, code: {:?}", status.code());
                         }
                     }
-                    Some(Err(e)) => log::error!("Task spawn failed: {e:#}"),
+                    Some(Err(e)) => {
+                        log::error!("Task spawn failed: {e:#}");
+                        _ = w.update(cx, |w, cx| {
+                            let id = NotificationId::unique::<ResolvedTask>();
+                            w.show_toast(Toast::new(id, format!("Task spawn failed: {e}")), cx);
+                        })
+                    }
                     None => log::debug!("Task spawn got cancelled"),
-                }
+                };
             });
             self.scheduled_tasks.push(task);
         }