Files
Aliaksandr Zhyburtovich d71ff73440 tasks_ui: Fix tasks with file variables missing for single-file worktrees (#66195)
## Summary

When a single file is opened without a folder (e.g. via `ctrl-o`), it
gets a worktree whose root is the file itself. `task_contexts` only
built the editor context for items from worktrees rooted at a directory,
so variables like `ZED_FILE` were never set. Tasks referencing them,
including global tasks from `~/.config/zed/tasks.json`, failed to
resolve and were silently dropped from `task: spawn`. Tasks without
variables were still listed, which made the bug look inconsistent.

This PR:

- builds the editor context for any active item with a project path,
while keeping the worktree context limited to directory worktrees (as
introduced in #25605);
- makes `BasicContextProvider` set `ZED_WORKTREE_ROOT`, the relative
path variables and the default cwd only for visible directory worktrees,
so they don't point at the file itself or at an unrelated directory such
as the Zed config directory;
- moves the task picker's source-aware context selection into
`TaskContexts` and uses it when spawning tasks by name or tag, so
worktree tasks are resolved against their own worktree even when an
external file is active;
- gives items outside of visible directory worktrees (e.g. the global
`tasks.json` opened via `zed: open tasks`) the visible project's
`ZED_WORKTREE_ROOT` and cwd, preserving the behavior from #55727, and
keeps only their file and cursor variables, so nothing derived from
their own worktree (relative paths, git repository, language-provided
values such as Python test targets) leaks next to the project's root;
- uses the first visible directory worktree, skipping single-file ones,
as the active worktree when the active item is not in a visible
directory worktree, so the fallback above and the worktree context still
apply when a standalone file was opened before a folder was added to the
project.

As a side effect, language tasks (e.g. Python runners) now also show up
for single-file worktrees, since the active location is no longer
dropped.

Closes #66148

## Testing

- Added `test_single_file_worktree_provides_file_context`: the item
context of a single-file editor contains `ZED_FILE`, has no
`ZED_WORKTREE_ROOT` and no `cwd`.
- Added tests that spawn a worktree-local task by name, or rerun it with
`reevaluate_context: true`, while a file-backed external editor is
active (single-file and invisible directory worktrees): the task
resolves against its own worktree and doesn't get the external file's
`ZED_FILENAME`.
- Added
`test_spawn_by_name_resolves_global_task_with_global_tasks_file_editor_active`:
with a file-backed global `tasks.json` editor from an invisible worktree
active, a global task gets the visible project's root and cwd, keeps
`ZED_FILENAME` and has no `ZED_RELATIVE_FILE`.
- Added
`test_spawn_by_name_resolves_global_task_with_single_file_worktree_listed_first`:
the same check with a visible single-file worktree listed before the
visible directory worktree.
- Extended
`test_external_item_context_does_not_carry_its_worktree_environment`: an
external item's `ZED_MAIN_GIT_WORKTREE` and language-provided variables
don't reach global tasks.
- Added
`test_spawn_by_name_does_not_use_language_targets_derived_from_external_relative_path`:
with the built-in languages registered and a Python file from an
invisible worktree active, the real `PythonContextProvider` derives a
relative `ZED_CUSTOM_PYTHON_TEST_TARGET` for the editor, but a global
`pytest` task spawned by name gets the `.` default and the visible
project's cwd.
- `cargo test -p tasks_ui` and task-related tests in `project` pass.
- Manually reproduced on Linux before the fix: with global tasks using
`$ZED_FILE` and a task without variables, only the latter was listed for
a file opened via `ctrl-o`, while both were listed with a folder opened.
- Manually verified on Linux after the fix: global tasks using
`$ZED_FILE` are listed and run for a file opened via `ctrl-o`.
- `./script/clippy` passes.

To test: add a global task with `"command": "echo $ZED_FILE"`, open a
file via `ctrl-o` in an empty window, run `task: spawn`.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Fixed tasks that use file variables such as `$ZED_FILE` not being
listed in `task: spawn` when a single file is opened without a folder.
2026-11-04 15:54:12 +00:00
..