Skip to content

project: Re-resolve recent tasks against current editor context - #57380

Merged
ConradIrwin merged 4 commits into
zed-industries:mainfrom
007qr:fix-task-rerun-stale-vars
Jun 9, 2026
Merged

project: Re-resolve recent tasks against current editor context#57380
ConradIrwin merged 4 commits into
zed-industries:mainfrom
007qr:fix-task-rerun-stale-vars

Conversation

@007qr

@007qr 007qr commented May 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #55879.

When a task is run, its fully-resolved form (ResolvedTask, with $ZED_FILE/$ZED_STEM/$ZED_COLUMN/etc. already substituted) is cached in Inventory::last_scheduled_tasks. The next time the task picker is opened, used_and_current_resolved_tasks returns those cached entries as-is for the "recent" section. The "current" section is re-resolved against the active editor, but the label-based dedup keeps the stale recent entry.

The result is that $ZED_FILE keeps pointing at the file that was active the first time the task ran, even after switching editors and re-running from the picker.

This fix re-resolves each entry in the recent-tasks list against the current TaskContexts using the same context-pick chain the "current" branch already uses (item context → active worktree → other worktree → default). If re-resolution fails for any reason, we fall back to the cached ResolvedTask so behavior never regresses.

The change is localized to previously_spawned_tasks inside used_and_current_resolved_tasks. List order, dedup, and LRU scoring are unchanged — only the variable substitutions inside each ResolvedTask are refreshed.

Test plan

  • Open a project with two source files (e.g. 1.cpp, 2.cpp) and a task that uses $ZED_FILE (e.g. g++ $ZED_FILE -o $ZED_STEM && ./$ZED_STEM).
  • Open 1.cpp, run the task from the picker, confirm it compiles 1.cpp.
  • Click 2.cpp, open the task picker, pick the same task from the "recent" section, confirm it now compiles 2.cpp (previously: still 1.cpp).
  • Confirm the recent-task ordering and divider in the picker are unchanged.

Release Notes:

Fixes $ZED_FILE and other task variables remaining stale when reusing
a recently-spawned task from the task picker after switching the active
editor.
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label May 21, 2026
@zed-community-bot zed-community-bot Bot added the first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions label May 21, 2026
sumit-m added a commit to sumit-m/zed-autoit that referenced this pull request May 22, 2026
tasks.json: "Run current file" and "Compile with Options" now invoke
autoit-run instead of AutoIt3.exe directly. autoit-run is a
console-subsystem wrapper that spawns AutoIt3.exe with a real pipe,
streaming ConsoleWrite() output to Zed's terminal line-by-line.
Discovery: Get-Command (PATH) first, then HKCU\SOFTWARE\zed-autoit\RunnerPath
written by autoit-lsp.exe at startup.

README: add Limitations note about task rerun using cached $ZED_FILE,
pointing at zed-industries/zed#57380 (open upstream fix) with the
task:spawn workaround. Update robot.png.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@MrSubidubi MrSubidubi added the area:tasks Feedback for Zed's task support label May 22, 2026
@ConradIrwin

Copy link
Copy Markdown
Member

I think the change is good.

Let's factor out the task context resolution to make it clearer; and fix the cargo fmt issues.

One thing I'm not sure about is fi we can re-use the active item context if the active item has changed (though maybe that is important).

Either way it'd be good to add a test for the new behavior so it doesn't regress.

@ConradIrwin ConradIrwin self-assigned this Jun 4, 2026
Extract the inline context-resolution chain into a documented
TaskContexts::reresolve_task helper, and add unit tests covering active
item precedence, worktree scoping, and the fallback to the original task.
@007qr

007qr commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Hey @ConradIrwin,
I have factored out the resolution into TaskContexts::reresolve_task and added tests. Fixed fmt.
On reusing the active item context: re-resolving against it is intentional, it's the only context with $ZED_FILE/selection, so it's what makes the recent task follow the current editor. Worktree tasks stay scoped to their own worktree, and it falls back to the original task if nothing resolves.

@ConradIrwin
ConradIrwin enabled auto-merge June 5, 2026 18:21
@ConradIrwin

Copy link
Copy Markdown
Member

Thanks!

auto-merge was automatically disabled June 8, 2026 13:03

Head branch was pushed to by a user without write access

@007qr

007qr commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ConradIrwin,

The run_tests failure was tasks_ui::modal::tests::test_language_task_filtering, re-resolution was rewriting history task labels against the current file's context. Fixed by keeping the original label when re-resolution changes it. Verified locally.

Mind re-running the workflow? Thanks!

@ConradIrwin
ConradIrwin added this pull request to the merge queue Jun 9, 2026
Merged via the queue into zed-industries:main with commit 7266ab6 Jun 9, 2026
32 checks passed
@zed-zippy zed-zippy Bot added the PR state:needs review Used to label PRs that are in need of a post-merge approval label Jun 9, 2026
@cole-miller

Copy link
Copy Markdown
Member

@zed-industries/approved

@zed-zippy zed-zippy Bot removed the PR state:needs review Used to label PRs that are in need of a post-merge approval label Jun 9, 2026
This was referenced Jun 18, 2026
jonx pushed a commit to jonx/zed-aros that referenced this pull request Jul 17, 2026
…industries#57380)

## Summary

Closes zed-industries#55879.

When a task is run, its fully-resolved form (`ResolvedTask`, with
`$ZED_FILE`/`$ZED_STEM`/`$ZED_COLUMN`/etc. already substituted) is
cached in `Inventory::last_scheduled_tasks`. The next time the task
picker is opened, `used_and_current_resolved_tasks` returns those cached
entries as-is for the "recent" section. The "current" section *is*
re-resolved against the active editor, but the label-based dedup keeps
the stale recent entry.

The result is that `$ZED_FILE` keeps pointing at the file that was
active the first time the task ran, even after switching editors and
re-running from the picker.

This fix re-resolves each entry in the recent-tasks list against the
current `TaskContexts` using the same context-pick chain the "current"
branch already uses (item context → active worktree → other worktree →
default). If re-resolution fails for any reason, we fall back to the
cached `ResolvedTask` so behavior never regresses.

The change is localized to `previously_spawned_tasks` inside
`used_and_current_resolved_tasks`. List order, dedup, and LRU scoring
are unchanged — only the variable substitutions inside each
`ResolvedTask` are refreshed.

## Test plan

- [x] Open a project with two source files (e.g. `1.cpp`, `2.cpp`) and a
task that uses `$ZED_FILE` (e.g. `g++ $ZED_FILE -o $ZED_STEM &&
./$ZED_STEM`).
- [x] Open `1.cpp`, run the task from the picker, confirm it compiles
`1.cpp`.
- [x] Click `2.cpp`, open the task picker, pick the same task from the
"recent" section, confirm it now compiles `2.cpp` (previously: still
`1.cpp`).
- [x] Confirm the recent-task ordering and divider in the picker are
unchanged.

Release Notes:

- Fixed task variables like `$ZED_FILE` not updating when re-running a
recently used task after switching the active editor (zed-industries#55879).
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…industries#57380)

## Summary

Closes zed-industries#55879.

When a task is run, its fully-resolved form (`ResolvedTask`, with
`$ZED_FILE`/`$ZED_STEM`/`$ZED_COLUMN`/etc. already substituted) is
cached in `Inventory::last_scheduled_tasks`. The next time the task
picker is opened, `used_and_current_resolved_tasks` returns those cached
entries as-is for the "recent" section. The "current" section *is*
re-resolved against the active editor, but the label-based dedup keeps
the stale recent entry.

The result is that `$ZED_FILE` keeps pointing at the file that was
active the first time the task ran, even after switching editors and
re-running from the picker.

This fix re-resolves each entry in the recent-tasks list against the
current `TaskContexts` using the same context-pick chain the "current"
branch already uses (item context → active worktree → other worktree →
default). If re-resolution fails for any reason, we fall back to the
cached `ResolvedTask` so behavior never regresses.

The change is localized to `previously_spawned_tasks` inside
`used_and_current_resolved_tasks`. List order, dedup, and LRU scoring
are unchanged — only the variable substitutions inside each
`ResolvedTask` are refreshed.

## Test plan

- [x] Open a project with two source files (e.g. `1.cpp`, `2.cpp`) and a
task that uses `$ZED_FILE` (e.g. `g++ $ZED_FILE -o $ZED_STEM &&
./$ZED_STEM`).
- [x] Open `1.cpp`, run the task from the picker, confirm it compiles
`1.cpp`.
- [x] Click `2.cpp`, open the task picker, pick the same task from the
"recent" section, confirm it now compiles `2.cpp` (previously: still
`1.cpp`).
- [x] Confirm the recent-task ordering and divider in the picker are
unchanged.

Release Notes:

- Fixed task variables like `$ZED_FILE` not updating when re-running a
recently used task after switching the active editor (zed-industries#55879).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:tasks Feedback for Zed's task support cla-signed The user has signed the Contributor License Agreement first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$ZED_FILE doesn't update

4 participants