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
…
…
…
…
2026-10-26 10:10:32 +00:00
2026-09-11 09:13:44 +00:00
…
…
…

Zed

Zed CI

Welcome to Zed, a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.


Installation

On macOS, Linux, and Windows you can download Zed directly or install Zed via your local package manager (macOS/Linux/Windows).

Other platforms are not yet available:

Developing Zed

Contributing

See CONTRIBUTING.md for ways you can contribute to Zed.

Also... we're hiring! Check out our jobs page for open roles.

Licensing

Zed source code is licensed primarily under GPL-3.0-or-later, with Apache-2.0 components where marked.

License information for third party dependencies must be correctly provided for CI to pass.

We use cargo-about to automatically comply with open source licenses. If CI is failing, check the following:

  • Is it showing a no license specified error for a crate you've created? If so, add publish = false under [package] in your crate's Cargo.toml.
  • Is the error failed to satisfy license requirements for a dependency? If so, first determine what license the project has and whether this system is sufficient to comply with this license's requirements. If you're unsure, ask a lawyer. Once you've verified that this system is acceptable add the license's SPDX identifier to the accepted array in script/licenses/zed-licenses.toml.
  • Is cargo-about unable to find the license for a dependency? If so, add a clarification field at the end of script/licenses/zed-licenses.toml, as specified in the cargo-about book.

Sponsorship

Zed is developed by Zed Industries, Inc., a for-profit company.

If you’d like to financially support the project, you can do so via GitHub Sponsors. Sponsorships go directly to Zed Industries and are used as general company revenue. There are no perks or entitlements associated with sponsorship.

S
Description
No description provided
Readme 1.6 GiB
Languages
Rust 95.4%
JSON-with-Comments 2.4%
Python 0.8%
Inno Setup 0.3%
Tree-sitter Query 0.3%
Other 0.4%