Use proper Workspace references when querying for tasks by name (#10383)

Kirill Bulatov created

Closes https://github.com/zed-industries/zed/issues/10380

Release Notes:

- Fixed Zed panicking when running tasks via a keybinding
([10380](https://github.com/zed-industries/zed/issues/10380))

Change summary

crates/tasks_ui/src/lib.rs   | 21 +++++++++------------
crates/tasks_ui/src/modal.rs | 29 ++++++++++++++++++++++++++---
2 files changed, 35 insertions(+), 15 deletions(-)

Detailed changes

crates/tasks_ui/src/lib.rs 🔗

@@ -2,7 +2,7 @@ use std::{path::PathBuf, sync::Arc};
 
 use ::settings::Settings;
 use editor::Editor;
-use gpui::{AppContext, ViewContext, WeakView, WindowContext};
+use gpui::{AppContext, ViewContext, WindowContext};
 use language::{Language, Point};
 use modal::{Spawn, TasksModal};
 use project::{Location, WorktreeId};
@@ -60,17 +60,17 @@ fn spawn_task_or_modal(workspace: &mut Workspace, action: &Spawn, cx: &mut ViewC
 fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
     cx.spawn(|workspace, mut cx| async move {
         let did_spawn = workspace
-            .update(&mut cx, |this, cx| {
-                let (worktree, language) = active_item_selection_properties(&workspace, cx);
-                let tasks = this.project().update(cx, |project, cx| {
+            .update(&mut cx, |workspace, cx| {
+                let (worktree, language) = active_item_selection_properties(workspace, cx);
+                let tasks = workspace.project().update(cx, |project, cx| {
                     project.task_inventory().update(cx, |inventory, cx| {
                         inventory.list_tasks(language, worktree, false, cx)
                     })
                 });
                 let (_, target_task) = tasks.into_iter().find(|(_, task)| task.name() == name)?;
-                let cwd = task_cwd(this, cx).log_err().flatten();
-                let task_context = task_context(this, cwd, cx);
-                schedule_task(this, &target_task, task_context, false, cx);
+                let cwd = task_cwd(workspace, cx).log_err().flatten();
+                let task_context = task_context(workspace, cwd, cx);
+                schedule_task(workspace, &target_task, task_context, false, cx);
                 Some(())
             })
             .ok()
@@ -88,13 +88,10 @@ fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
 }
 
 fn active_item_selection_properties(
-    workspace: &WeakView<Workspace>,
+    workspace: &Workspace,
     cx: &mut WindowContext,
 ) -> (Option<WorktreeId>, Option<Arc<Language>>) {
-    let active_item = workspace
-        .update(cx, |workspace, cx| workspace.active_item(cx))
-        .ok()
-        .flatten();
+    let active_item = workspace.active_item(cx);
     let worktree_id = active_item
         .as_ref()
         .and_then(|item| item.project_path(cx))

crates/tasks_ui/src/modal.rs 🔗

@@ -195,8 +195,13 @@ impl PickerDelegate for TasksModalDelegate {
             let Some(candidates) = picker
                 .update(&mut cx, |picker, cx| {
                     let candidates = picker.delegate.candidates.get_or_insert_with(|| {
-                        let (worktree, language) =
-                            active_item_selection_properties(&picker.delegate.workspace, cx);
+                        let Ok((worktree, language)) =
+                            picker.delegate.workspace.update(cx, |workspace, cx| {
+                                active_item_selection_properties(workspace, cx)
+                            })
+                        else {
+                            return Vec::new();
+                        };
                         picker.delegate.inventory.update(cx, |inventory, cx| {
                             inventory.list_tasks(language, worktree, true, cx)
                         })
@@ -376,6 +381,8 @@ mod tests {
     use project::{FakeFs, Project};
     use serde_json::json;
 
+    use crate::modal::Spawn;
+
     use super::*;
 
     #[gpui::test]
@@ -514,13 +521,29 @@ mod tests {
             vec!["echo 4", "another one", "example task", "echo 40"],
             "Last recently used one show task should be listed last, as it is a fire-and-forget task"
         );
+
+        cx.dispatch_action(Spawn {
+            task_name: Some("example task".to_string()),
+        });
+        let tasks_picker = workspace.update(cx, |workspace, cx| {
+            workspace
+                .active_modal::<TasksModal>(cx)
+                .unwrap()
+                .read(cx)
+                .picker
+                .clone()
+        });
+        assert_eq!(
+            task_names(&tasks_picker, cx),
+            vec!["echo 4", "another one", "example task", "echo 40"],
+        );
     }
 
     fn open_spawn_tasks(
         workspace: &View<Workspace>,
         cx: &mut VisualTestContext,
     ) -> View<Picker<TasksModalDelegate>> {
-        cx.dispatch_action(crate::modal::Spawn::default());
+        cx.dispatch_action(Spawn::default());
         workspace.update(cx, |workspace, cx| {
             workspace
                 .active_modal::<TasksModal>(cx)