diff --git a/Cargo.lock b/Cargo.lock index 0404e33240b..0025fa60ce2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17876,10 +17876,13 @@ dependencies = [ "gpui", "itertools 0.14.0", "language", + "languages", "menu", + "node_runtime", "picker", "project", "serde_json", + "settings", "task", "tree-sitter", "tree-sitter-json", diff --git a/crates/project/src/task_inventory.rs b/crates/project/src/task_inventory.rs index 75363e9d4b6..2dad9090f16 100644 --- a/crates/project/src/task_inventory.rs +++ b/crates/project/src/task_inventory.rs @@ -194,6 +194,54 @@ impl TaskContexts { }) } + /// Returns the first context, in the task picker's order of preference, in which the task resolves. + pub fn context_for_task( + &self, + kind: &TaskSourceKind, + template: &TaskTemplate, + ) -> Option<&TaskContext> { + let id_base = kind.to_id_base(); + self.contexts_for_source(kind) + .find(|&context| template.resolve_task(&id_base, context).is_some()) + } + + pub fn resolve_task( + &self, + kind: &TaskSourceKind, + template: &TaskTemplate, + ) -> Option { + let id_base = kind.to_id_base(); + self.contexts_for_source(kind) + .find_map(|context| template.resolve_task(&id_base, context)) + .or_else(|| template.resolve_task(&id_base, &TaskContext::default())) + } + + /// Worktree tasks are only resolved with contexts of their own worktree, so that an active item + /// from an unrelated (e.g. invisible or single-file) worktree does not affect their variables. + fn contexts_for_source(&self, kind: &TaskSourceKind) -> impl Iterator { + let source_worktree = match kind { + TaskSourceKind::Worktree { id, .. } => Some(*id), + _ => None, + }; + + let item_context = self + .active_item_context + .as_ref() + .filter(|(worktree_id, _, _)| { + source_worktree.is_none() || *worktree_id == source_worktree + }) + .map(|(_, _, context)| context); + + let worktree_context = match source_worktree { + Some(worktree_id) => self.task_context_for_worktree_id(worktree_id), + None => self + .active_worktree_context + .as_ref() + .map(|(_, context)| context), + }; + item_context.into_iter().chain(worktree_context) + } + pub fn location(&self) -> Option<&Location> { self.active_item_context .as_ref() @@ -230,44 +278,9 @@ impl TaskContexts { /// Re-resolves a previously resolved task against the current contexts, so that /// any context-dependent variables (e.g. the active file or selection) reflect the /// editor's current state rather than the state from when the task was first run. - /// - /// For worktree tasks the search is scoped to the task's own worktree; other tasks - /// fall back through the active item, active worktree, and finally an empty context. /// If none of the contexts resolve, the original `resolved_task` is returned unchanged. fn reresolve_task(&self, kind: &TaskSourceKind, resolved_task: &ResolvedTask) -> ResolvedTask { - let id_base = kind.to_id_base(); - let task = resolved_task.original_task(); - - let candidate_contexts: Vec> = match kind { - TaskSourceKind::Worktree { id, .. } => vec![ - self.active_item_context - .as_ref() - .filter(|(worktree_id, _, _)| worktree_id.as_ref() == Some(id)) - .map(|(_, _, context)| context), - self.active_worktree_context - .as_ref() - .filter(|(worktree_id, _)| worktree_id == id) - .map(|(_, context)| context), - self.other_worktree_contexts - .iter() - .find(|(worktree_id, _)| worktree_id == id) - .map(|(_, context)| context), - ], - _ => vec![ - self.active_item_context - .as_ref() - .map(|(_, _, context)| context), - self.active_worktree_context - .as_ref() - .map(|(_, context)| context), - ], - }; - - candidate_contexts - .into_iter() - .flatten() - .find_map(|context| task.resolve_task(&id_base, context)) - .or_else(|| task.resolve_task(&id_base, &TaskContext::default())) + self.resolve_task(kind, resolved_task.original_task()) .unwrap_or_else(|| resolved_task.clone()) } } @@ -592,50 +605,10 @@ impl Inventory { .chain(global_tasks); let new_resolved_tasks = worktree_tasks - .flat_map(|(kind, task)| { - let id_base = kind.to_id_base(); - - if let TaskSourceKind::Worktree { id, .. } = &kind { - None.or_else(|| { - let (_, _, item_context) = - task_contexts.active_item_context.as_ref().filter( - |(worktree_id, _, _)| Some(id) == worktree_id.as_ref(), - )?; - task.resolve_task(&id_base, item_context) - }) - .or_else(|| { - let (_, worktree_context) = task_contexts - .active_worktree_context - .as_ref() - .filter(|(worktree_id, _)| id == worktree_id)?; - task.resolve_task(&id_base, worktree_context) - }) - .or_else(|| { - if let TaskSourceKind::Worktree { id, .. } = &kind { - let worktree_context = task_contexts - .other_worktree_contexts - .iter() - .find(|(worktree_id, _)| worktree_id == id) - .map(|(_, context)| context)?; - task.resolve_task(&id_base, worktree_context) - } else { - None - } - }) - } else { - None.or_else(|| { - let (_, _, item_context) = - task_contexts.active_item_context.as_ref()?; - task.resolve_task(&id_base, item_context) - }) - .or_else(|| { - let (_, worktree_context) = - task_contexts.active_worktree_context.as_ref()?; - task.resolve_task(&id_base, worktree_context) - }) - } - .or_else(|| task.resolve_task(&id_base, &TaskContext::default())) - .map(move |resolved_task| (kind.clone(), resolved_task, not_used_score)) + .filter_map(|(kind, task)| { + task_contexts + .resolve_task(&kind, &task) + .map(move |resolved_task| (kind, resolved_task, not_used_score)) }) .filter(|(_, resolved_task, _)| { match task_labels_to_ids.entry(resolved_task.resolved_label.clone()) { @@ -1065,12 +1038,14 @@ impl ContextProvider for BasicContextProvider { .worktree_for_id(worktree_id, cx) }); - if let Some(worktree) = worktree { + if let Some(worktree) = worktree + && let Some(worktree_root) = worktree.read(cx).root_dir() + { let worktree = worktree.read(cx); let path_style = worktree.path_style(); task_variables.insert( VariableName::WorktreeRoot, - worktree.abs_path().to_string_lossy().into_owned(), + worktree_root.to_string_lossy().into_owned(), ); if let Some(current_file) = current_file.as_ref() { let relative_path = current_file.path(); diff --git a/crates/task/src/task.rs b/crates/task/src/task.rs index 37dc3d7556b..d85cb2db56f 100644 --- a/crates/task/src/task.rs +++ b/crates/task/src/task.rs @@ -303,6 +303,10 @@ impl TaskVariables { pub fn get(&self, key: &VariableName) -> Option<&str> { self.0.get(key).map(|s| s.as_str()) } + /// Removes the variable from the container, returning its value if it was present. + pub fn remove(&mut self, key: &VariableName) -> Option { + self.0.remove(key) + } /// Clear out variables obtained from tree-sitter queries, which are prefixed with '_' character pub fn sweep(&mut self) { self.0.retain(|name, _| { diff --git a/crates/tasks_ui/Cargo.toml b/crates/tasks_ui/Cargo.toml index 7512b225214..0d07c9fff42 100644 --- a/crates/tasks_ui/Cargo.toml +++ b/crates/tasks_ui/Cargo.toml @@ -36,8 +36,11 @@ zed_actions.workspace = true editor = { workspace = true, features = ["test-support"] } gpui = { workspace = true, features = ["test-support"] } language = { workspace = true, features = ["test-support"] } +languages = { workspace = true, features = ["test-support"] } +node_runtime.workspace = true project = { workspace = true, features = ["test-support"] } serde_json.workspace = true +settings = { workspace = true, features = ["test-support"] } tree-sitter-rust.workspace = true tree-sitter-typescript.workspace = true workspace = { workspace = true, features = ["test-support"] } diff --git a/crates/tasks_ui/src/tasks_ui.rs b/crates/tasks_ui/src/tasks_ui.rs index c2151c547a5..b2e7162d75d 100644 --- a/crates/tasks_ui/src/tasks_ui.rs +++ b/crates/tasks_ui/src/tasks_ui.rs @@ -134,20 +134,21 @@ pub fn init(cx: &mut App) { let default_context = TaskContext::default(); workspace .update_in(cx, |workspace, window, cx| { + let task_context = task_contexts + .context_for_task(&task_source_kind, &original_task) + .unwrap_or(&default_context); workspace.schedule_task( task_source_kind, &original_task, - task_contexts - .active_context() - .unwrap_or(&default_context), + task_context, false, window, cx, - ) + ); }) .ok() }) - .detach() + .detach(); } else { let resolved = &mut last_scheduled_task.resolved; @@ -310,7 +311,6 @@ where let did_spawn = workspace .update_in(cx, |workspace, window, cx| { let default_context = TaskContext::default(); - let active_context = task_contexts.active_context().unwrap_or(&default_context); tasks.retain_mut(|(task_source_kind, target_task)| { if predicate((task_source_kind, target_task)) { @@ -319,10 +319,14 @@ where { target_task.reveal_target = target_override; } + let task_context = task_contexts + .context_for_task(task_source_kind, target_task) + .unwrap_or(&default_context); + workspace.schedule_task( task_source_kind.clone(), target_task, - active_context, + task_context, false, window, cx, @@ -361,9 +365,9 @@ pub fn task_contexts( cx: &mut App, ) -> Task { let active_item = workspace.active_item(cx); - let active_item_worktree = active_item + let active_item_project_path = active_item.as_ref().and_then(|item| item.project_path(cx)); + let active_item_worktree = active_item_project_path .as_ref() - .and_then(|item| item.project_path(cx)) .map(|project_path| project_path.worktree_id) .filter(|worktree_id| { workspace @@ -375,12 +379,14 @@ pub fn task_contexts( let active_worktree = active_item_worktree.or_else(|| { workspace .visible_worktrees(cx) - .next() + .find(|worktree| is_visible_directory(worktree, cx)) .map(|tree| tree.read(cx).id()) }); let active_editor = active_item.and_then(|item| item.act_as::(cx)); - let active_project_editor = active_item_worktree + // Files from single-file worktrees provide no worktree context, but their editors + // must still provide file variables such as `ZED_FILE`. + let active_project_editor = active_item_project_path .is_some() .then(|| active_editor.clone()) .flatten(); @@ -435,8 +441,14 @@ pub fn task_contexts( task_contexts.latest_selection = latest_selection; if let Some(editor_context_task) = editor_context_task - && let Some(editor_context) = editor_context_task.await + && let Some(mut editor_context) = editor_context_task.await { + if active_item_worktree.is_none() + && let Some(project_root) = + active_worktree.and_then(|worktree_id| worktree_abs_paths.get(&worktree_id)) + { + use_project_root_for_external_item(&mut editor_context, project_root); + } task_contexts.active_item_context = Some((active_item_worktree, location, editor_context)); } @@ -462,6 +474,41 @@ pub fn task_contexts( }) } +/// Items outside of visible directory worktrees (e.g. the global `tasks.json` opened via +/// `zed: open tasks`) keep their file variables, but use the visible project's root and cwd. +/// Everything else is dropped, as it is derived from their own worktree: relative paths, its git +/// repository and language-specific variables built from them (e.g. Python test targets) would +/// point into an unrelated directory when run from the project's cwd, and its environment would +/// override the one the terminal loads for that cwd. +fn use_project_root_for_external_item(editor_context: &mut TaskContext, project_root: &Path) { + editor_context.task_variables = std::mem::take(&mut editor_context.task_variables) + .into_iter() + .filter(|(variable, _)| { + matches!( + variable, + VariableName::File + | VariableName::Filename + | VariableName::Stem + | VariableName::Dirname + | VariableName::Row + | VariableName::Column + | VariableName::SelectedText + | VariableName::Symbol + | VariableName::RunnableSymbol + | VariableName::Language + ) + }) + .collect(); + + editor_context.task_variables.insert( + VariableName::WorktreeRoot, + project_root.to_string_lossy().into_owned(), + ); + + editor_context.cwd = Some(project_root.to_path_buf()); + editor_context.project_env.clear(); +} + fn is_visible_directory(worktree: &Entity, cx: &App) -> bool { let worktree = worktree.read(cx); worktree.is_visible() && worktree.root_entry().is_some_and(|entry| entry.is_dir()) @@ -482,19 +529,38 @@ fn worktree_context(worktree_abs_path: &Path) -> TaskContext { #[cfg(test)] mod tests { - use std::{collections::HashMap, sync::Arc}; + use std::{ + collections::HashMap, + path::{Path, PathBuf}, + sync::Arc, + }; use editor::{Editor, MultiBufferOffset, SelectionEffects}; - use gpui::TestAppContext; + use gpui::{Entity, TestAppContext, UpdateGlobal, VisualTestContext}; use language::{Language, LanguageConfig}; - use project::{BasicContextProvider, FakeFs, Project, task_store::TaskStore}; + use node_runtime::NodeRuntime; + use project::{ + BasicContextProvider, FakeFs, Inventory, Project, TaskContexts, TaskSourceKind, WorktreeId, + task_store::{TaskSettingsLocation, TaskStore}, + }; use serde_json::json; - use task::{TaskContext, TaskVariables, VariableName}; + use settings::SettingsStore; + use task::{ResolvedTask, TaskContext, TaskTemplate, TaskVariables, VariableName}; use ui::VisualContext; use util::{path, rel_path::rel_path}; - use workspace::{AppState, MultiWorkspace}; + use workspace::{AppState, MultiWorkspace, OpenOptions, OpenVisible, Workspace}; - use crate::task_contexts; + use crate::{ + Rerun, Spawn, task_contexts, use_project_root_for_external_item, worktree_context, + }; + + const GLOBAL_TASKS_JSON: &str = r#"[ + { + "label": "print global task context", + "command": "echo", + "args": ["$ZED_WORKTREE_ROOT", "$ZED_FILENAME", "${ZED_RELATIVE_FILE:no file}"] + } + ]"#; #[gpui::test] async fn test_non_project_active_editor_uses_visible_worktree_context(cx: &mut TestAppContext) { @@ -543,6 +609,587 @@ mod tests { ); } + #[gpui::test] + async fn test_single_file_worktree_provides_file_context(cx: &mut TestAppContext) { + init_test(cx); + let fs = FakeFs::new(cx.executor()); + + fs.insert_tree(path!("/dir"), json!({"test.py": "print(1)"})) + .await; + + let project = Project::test(fs, [path!("/dir/test.py").as_ref()], cx).await; + let (multi_workspace, cx) = + cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx)); + let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()); + + let buffer = project + .update(cx, |project, cx| { + project.open_local_buffer(path!("/dir/test.py"), cx) + }) + .await + .unwrap(); + let editor = cx + .new_window_entity(|window, cx| Editor::for_buffer(buffer, Some(project), window, cx)); + + let contexts = workspace + .update_in(cx, |workspace, window, cx| { + workspace.add_item_to_center(Box::new(editor), window, cx); + task_contexts(workspace, window, cx) + }) + .await; + + assert!(contexts.active_worktree_context.is_none()); + let (worktree_id, _, item_context) = contexts + .active_item_context + .as_ref() + .expect("single-file editor should provide an item context"); + + assert_eq!(*worktree_id, None); + assert_eq!(item_context.cwd, None); + assert_eq!( + item_context.task_variables.get(&VariableName::File), + Some(path!("/dir/test.py")) + ); + assert_eq!( + item_context.task_variables.get(&VariableName::WorktreeRoot), + None + ); + } + + #[gpui::test] + async fn test_spawn_by_name_resolves_worktree_task_with_single_file_editor_active( + cx: &mut TestAppContext, + ) { + let (_, workspace, cx) = init_project_with_worktree_task(cx).await; + open_external_file_in_single_file_worktree(&workspace, cx).await; + + assert_task_spawned_by_name_in_project_root(&workspace, cx); + } + + #[gpui::test] + async fn test_invisible_directory_worktree_editor_keeps_its_worktree_context( + cx: &mut TestAppContext, + ) { + let (project, workspace, cx) = init_project_with_worktree_task(cx).await; + open_file_in_invisible_worktree(&project, &workspace, path!("/external"), "notes.txt", cx) + .await; + + let editor = workspace + .read_with(cx, |workspace, cx| workspace.active_item_as::(cx)) + .expect("active item should be an editor"); + + // Gutter tasks resolve against this context directly, without the workspace's + // fallback to the visible project, so it must describe the editor's own worktree. + let item_context = editor + .update_in(cx, |editor, window, cx| editor.task_context(window, cx)) + .await + .expect("editor should provide a task context"); + assert_eq!( + item_context.task_variables.get(&VariableName::WorktreeRoot), + Some(path!("/external")) + ); + assert_eq!( + item_context.task_variables.get(&VariableName::RelativeFile), + Some("notes.txt") + ); + assert_eq!( + item_context.task_variables.get(&VariableName::RelativeDir), + Some(".") + ); + assert_eq!( + item_context.cwd.as_deref(), + Some(Path::new(path!("/external"))) + ); + } + + #[gpui::test] + async fn test_spawn_by_name_resolves_worktree_task_with_invisible_worktree_editor_active( + cx: &mut TestAppContext, + ) { + let (project, workspace, cx) = init_project_with_worktree_task(cx).await; + + open_file_in_invisible_worktree(&project, &workspace, path!("/external"), "notes.txt", cx) + .await; + + assert_task_spawned_by_name_in_project_root(&workspace, cx); + } + + #[gpui::test] + async fn test_reevaluated_rerun_resolves_worktree_task_with_single_file_editor_active( + cx: &mut TestAppContext, + ) { + let (_, workspace, cx) = init_project_with_worktree_task(cx).await; + spawn_task_with_project_file_active(&workspace, cx).await; + open_external_file_in_single_file_worktree(&workspace, cx).await; + + assert_reevaluated_rerun_in_project_root(&workspace, cx); + } + + #[gpui::test] + async fn test_reevaluated_rerun_resolves_worktree_task_with_invisible_worktree_editor_active( + cx: &mut TestAppContext, + ) { + let (project, workspace, cx) = init_project_with_worktree_task(cx).await; + spawn_task_with_project_file_active(&workspace, cx).await; + open_file_in_invisible_worktree(&project, &workspace, path!("/external"), "notes.txt", cx) + .await; + + assert_reevaluated_rerun_in_project_root(&workspace, cx); + } + + #[gpui::test] + async fn test_spawn_by_name_resolves_global_task_with_global_tasks_file_editor_active( + cx: &mut TestAppContext, + ) { + let (project, workspace, cx) = init_project_with_worktree_task(cx).await; + assert_global_task_spawned_in_project_root(&project, &workspace, cx).await; + } + + #[gpui::test] + async fn test_spawn_by_name_resolves_global_task_with_single_file_worktree_listed_first( + cx: &mut TestAppContext, + ) { + let (project, workspace, cx) = init_project_with_worktree_task_and_roots( + cx, + &[path!("/external/notes.txt"), path!("/project")], + ) + .await; + + assert_global_task_spawned_in_project_root(&project, &workspace, cx).await; + } + + #[gpui::test] + async fn test_spawn_by_name_does_not_use_language_targets_derived_from_external_relative_path( + cx: &mut TestAppContext, + ) { + let (project, workspace, cx) = init_project_with_worktree_task(cx).await; + cx.update(|_, cx| { + // Built-in languages come with language server adapters, which must not be started here. + SettingsStore::update_global(cx, |store, cx| { + store.update_user_settings(cx, |settings| { + settings + .project + .all_languages + .defaults + .enable_language_server = Some(false); + }); + }); + let languages = project.read(cx).languages().clone(); + let fs = project.read(cx).fs().clone(); + languages::init(languages, fs, NodeRuntime::unavailable(), cx); + }); + + task_inventory(&workspace, cx) + .update(cx, |inventory, _| { + inventory.update_file_based_tasks( + TaskSettingsLocation::Global(Path::new(path!("/zed-config/tasks.json"))), + Some( + r#"[ + { + "label": "pytest from global tasks", + "command": "python3", + "args": ["-m", "pytest", "${ZED_CUSTOM_PYTHON_TEST_TARGET:.}"], + "cwd": "$ZED_WORKTREE_ROOT" + } + ]"#, + ), + ) + }) + .unwrap(); + + open_file_in_invisible_worktree( + &project, + &workspace, + path!("/external"), + "test_external.py", + cx, + ) + .await; + + cx.run_until_parked(); + + let editor = workspace + .read_with(cx, |workspace, cx| workspace.active_item_as::(cx)) + .expect("active item should be an editor"); + let item_context = editor + .update_in(cx, |editor, window, cx| editor.task_context(window, cx)) + .await + .expect("editor should provide a task context"); + assert_eq!( + item_context + .task_variables + .get(&VariableName::Custom("PYTHON_TEST_TARGET".into())), + Some("test_external.py"), + "the Python context provider should derive the test target from the file's relative path" + ); + + cx.dispatch_action(Spawn::ByName { + task_name: "pytest from global tasks".to_string(), + reveal_target: None, + }); + cx.run_until_parked(); + + let scheduled_task = last_scheduled_task(&workspace, cx); + assert_eq!( + scheduled_task.resolved.args, + vec!["-m".to_string(), "pytest".to_string(), ".".to_string()] + ); + assert_eq!( + scheduled_task.resolved.cwd.as_deref(), + Some(Path::new(path!("/project"))) + ); + } + + #[test] + fn test_external_item_context_does_not_carry_its_worktree_environment() { + let project_root = Path::new(path!("/project")); + let project_worktree_id = WorktreeId::from_usize(1); + + let mut external_item_context = TaskContext { + cwd: Some(PathBuf::from(path!("/zed-config"))), + task_variables: TaskVariables::from_iter([ + ( + VariableName::File, + path!("/zed-config/tasks.json").to_string(), + ), + (VariableName::Filename, "tasks.json".to_string()), + (VariableName::WorktreeRoot, path!("/zed-config").to_string()), + (VariableName::RelativeFile, "tasks.json".to_string()), + (VariableName::RelativeDir, ".".to_string()), + ( + VariableName::MainGitWorktree, + path!("/zed-config").to_string(), + ), + ( + VariableName::Custom("PYTHON_TEST_TARGET".into()), + "tasks.json".to_string(), + ), + ]), + project_env: HashMap::from_iter([ + ("SOURCE_DIRECTORY".to_string(), "zed-config".to_string()), + ("ZED_CONFIG_ONLY".to_string(), "1".to_string()), + ]), + }; + + use_project_root_for_external_item(&mut external_item_context, project_root); + + let mut project_worktree_context = worktree_context(project_root); + project_worktree_context.project_env = + HashMap::from_iter([("SOURCE_DIRECTORY".to_string(), "project".to_string())]); + + let task_contexts = TaskContexts { + active_item_context: Some((None, None, external_item_context)), + active_worktree_context: Some((project_worktree_id, project_worktree_context)), + ..TaskContexts::default() + }; + + let global_task = task_contexts + .resolve_task( + &TaskSourceKind::AbsPath { + id_base: "global tasks".into(), + abs_path: PathBuf::from(path!("/zed-config/tasks.json")), + }, + &TaskTemplate { + label: "print global task context".to_string(), + command: "echo".to_string(), + args: vec![ + "$ZED_WORKTREE_ROOT".to_string(), + "$ZED_FILENAME".to_string(), + "${ZED_RELATIVE_FILE:no file}".to_string(), + "${ZED_MAIN_GIT_WORKTREE:no repository}".to_string(), + "${ZED_CUSTOM_PYTHON_TEST_TARGET:.}".to_string(), + ], + ..TaskTemplate::default() + }, + ) + .expect("global task should resolve"); + + assert_eq!( + global_task.resolved.args, + vec![ + path!("/project").to_string(), + "tasks.json".to_string(), + "no file".to_string(), + "no repository".to_string(), + ".".to_string() + ] + ); + assert_eq!(global_task.resolved.cwd.as_deref(), Some(project_root)); + assert_eq!(global_task.resolved.env.get("ZED_CONFIG_ONLY"), None); + assert_eq!(global_task.resolved.env.get("SOURCE_DIRECTORY"), None); + + let worktree_task = task_contexts + .resolve_task( + &TaskSourceKind::Worktree { + id: project_worktree_id, + directory_in_worktree: rel_path(".zed").into(), + id_base: "local worktree tasks".into(), + }, + &TaskTemplate { + label: "print worktree root".to_string(), + command: "echo".to_string(), + args: vec!["$ZED_WORKTREE_ROOT".to_string()], + ..TaskTemplate::default() + }, + ) + .expect("worktree task should resolve"); + + assert_eq!(worktree_task.resolved.cwd.as_deref(), Some(project_root)); + assert_eq!( + worktree_task + .resolved + .env + .get("SOURCE_DIRECTORY") + .map(String::as_str), + Some("project") + ); + } + + async fn open_external_file_in_single_file_worktree( + workspace: &Entity, + cx: &mut VisualTestContext, + ) { + workspace + .update_in(cx, |workspace, window, cx| { + workspace.open_abs_path( + PathBuf::from(path!("/external/notes.txt")), + OpenOptions { + visible: Some(OpenVisible::None), + ..Default::default() + }, + window, + cx, + ) + }) + .await + .unwrap(); + } + + async fn open_file_in_invisible_worktree( + project: &Entity, + workspace: &Entity, + worktree_path: &str, + file_name: &str, + cx: &mut VisualTestContext, + ) { + let (worktree, _) = project + .update(cx, |project, cx| { + project.find_or_create_worktree(worktree_path, false, cx) + }) + .await + .unwrap(); + let worktree_id = worktree.read_with(cx, |worktree, _| worktree.id()); + cx.run_until_parked(); + + workspace + .update_in(cx, |workspace, window, cx| { + workspace.open_path((worktree_id, rel_path(file_name)), None, true, window, cx) + }) + .await + .unwrap(); + } + + async fn spawn_task_with_project_file_active( + workspace: &Entity, + cx: &mut VisualTestContext, + ) { + workspace + .update_in(cx, |workspace, window, cx| { + workspace.open_abs_path( + PathBuf::from(path!("/project/main.rs")), + OpenOptions::default(), + window, + cx, + ) + }) + .await + .unwrap(); + + cx.dispatch_action(Spawn::ByName { + task_name: "print worktree root and file".to_string(), + reveal_target: None, + }); + cx.run_until_parked(); + + assert_eq!( + last_scheduled_task(workspace, cx).resolved.args, + vec![ + path!("/project").to_string(), + "main.rs".to_string(), + "main.rs".to_string() + ] + ); + } + + async fn init_project_with_worktree_task( + cx: &mut TestAppContext, + ) -> (Entity, Entity, &mut VisualTestContext) { + init_project_with_worktree_task_and_roots(cx, &[path!("/project")]).await + } + + async fn init_project_with_worktree_task_and_roots<'a>( + cx: &'a mut TestAppContext, + roots: &[&str], + ) -> ( + Entity, + Entity, + &'a mut VisualTestContext, + ) { + init_test(cx); + let fs = FakeFs::new(cx.executor()); + fs.insert_tree( + path!("/project"), + json!({ + ".zed": { + "tasks.json": r#"[ + { + "label": "print worktree root", + "command": "echo", + "args": ["$ZED_WORKTREE_ROOT", "${ZED_FILENAME:no file}"] + }, + { + "label": "print worktree root and file", + "command": "echo", + "args": ["$ZED_WORKTREE_ROOT", "${ZED_RELATIVE_FILE:no file}", "${ZED_FILENAME:no file}"] + } + ]"#, + }, + "main.rs": "", + }), + ) + .await; + fs.insert_tree( + path!("/external"), + json!({"notes.txt": "", "test_external.py": "x = 1\n"}), + ) + .await; + + fs.insert_tree( + path!("/zed-config"), + json!({ "tasks.json": GLOBAL_TASKS_JSON }), + ) + .await; + + let project = Project::test(fs, roots.iter().map(|root| Path::new(*root)), cx).await; + let (multi_workspace, cx) = + cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx)); + let workspace = multi_workspace.read_with(cx, |mw, _| mw.workspace().clone()); + cx.run_until_parked(); + + (project, workspace, cx) + } + + fn assert_task_spawned_by_name_in_project_root( + workspace: &Entity, + cx: &mut VisualTestContext, + ) { + cx.dispatch_action(Spawn::ByName { + task_name: "print worktree root".to_string(), + reveal_target: None, + }); + cx.run_until_parked(); + + let scheduled_task = last_scheduled_task(workspace, cx); + assert_eq!( + scheduled_task.resolved.args, + vec![path!("/project").to_string(), "no file".to_string()] + ); + assert_eq!( + scheduled_task.resolved.cwd.as_deref(), + Some(Path::new(path!("/project"))) + ); + } + + fn assert_reevaluated_rerun_in_project_root( + workspace: &Entity, + cx: &mut VisualTestContext, + ) { + cx.dispatch_action(Rerun { + reevaluate_context: true, + ..Rerun::default() + }); + cx.run_until_parked(); + + let scheduled_task = last_scheduled_task(workspace, cx); + assert_eq!( + scheduled_task.resolved.args, + vec![ + path!("/project").to_string(), + "no file".to_string(), + "no file".to_string() + ] + ); + assert_eq!( + scheduled_task.resolved.cwd.as_deref(), + Some(Path::new(path!("/project"))) + ); + } + + async fn assert_global_task_spawned_in_project_root( + project: &Entity, + workspace: &Entity, + cx: &mut VisualTestContext, + ) { + task_inventory(workspace, cx) + .update(cx, |inventory, _| { + inventory.update_file_based_tasks( + TaskSettingsLocation::Global(Path::new(path!("/zed-config/tasks.json"))), + Some(GLOBAL_TASKS_JSON), + ) + }) + .unwrap(); + + open_file_in_invisible_worktree(project, workspace, path!("/zed-config"), "tasks.json", cx) + .await; + + cx.dispatch_action(Spawn::ByName { + task_name: "print global task context".to_string(), + reveal_target: None, + }); + cx.run_until_parked(); + + let scheduled_task = last_scheduled_task(workspace, cx); + assert_eq!( + scheduled_task.resolved.args, + vec![ + path!("/project").to_string(), + "tasks.json".to_string(), + "no file".to_string(), + ] + ); + assert_eq!( + scheduled_task.resolved.cwd.as_deref(), + Some(Path::new(path!("/project"))) + ); + } + + fn task_inventory( + workspace: &Entity, + cx: &mut VisualTestContext, + ) -> Entity { + workspace + .read_with(cx, |workspace, cx| { + workspace + .project() + .read(cx) + .task_store() + .read(cx) + .task_inventory() + .cloned() + }) + .expect("project should have a task inventory") + } + + fn last_scheduled_task( + workspace: &Entity, + cx: &mut VisualTestContext, + ) -> ResolvedTask { + let (_, scheduled_task) = task_inventory(workspace, cx) + .read_with(cx, |inventory, _| inventory.last_scheduled_task(None)) + .expect("a task should be scheduled"); + scheduled_task + } + #[gpui::test] async fn test_default_language_context(cx: &mut TestAppContext) { init_test(cx);