file_finder: Don’t use focused file’s directory for CreateNew unless it belongs to project worktrees#42076
Conversation
|
files Prevent incorrect worktree selection when creating files through the file finder. Previously, if a file from a different worktree was focused, new files would be created in that worktree instead of the query-based target. Now validates that the focused file belongs to available project worktrees before using it to determine the target directory. External or irrelevant files are ignored, preserving the query-based worktree selection. This fixes the issue where opening files from worktree B while working in worktree A would cause new files to be created in B instead of A.
new file when focused file does not belong to any active worktrees.
d4d86d4 to
425247e
Compare
|
Hi team! Following up on this PR - the bug in #41940 is still present in current main. Issue: When settings.json or an external file is focused with a worktree open, This PR: Validates that the focused file belongs to an available worktree before using it as the target directory. Rebased on latest |
There was a problem hiding this comment.
Pull request overview
Adjusts File Finder “Create New” target selection so it only uses the focused file’s worktree when that file actually belongs to one of the project’s visible worktrees, preventing misdirected file creation.
Changes:
- Update worktree selection logic to avoid overriding the expected worktree with an unrelated focused file.
- Add a regression test covering “focused external file + project worktrees open” behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| crates/file_finder/src/file_finder.rs | Ensures focused-file worktree only influences Create New when that worktree is among visible project worktrees. |
| crates/file_finder/src/file_finder_tests.rs | Adds a test to verify Create New stays within project worktrees even when an external file is focused. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| workspace.open_abs_path( | ||
| PathBuf::from(path!("/external/external-file.txt")), | ||
| OpenOptions { | ||
| visible: Some(OpenVisible::None), | ||
| ..OpenOptions::default() | ||
| }, | ||
| window, | ||
| cx, | ||
| ) | ||
| }) | ||
| .await | ||
| .unwrap(); |
There was a problem hiding this comment.
This test opens /external/external-file.txt without first creating it in the fake filesystem, which can make the test rely on incidental behavior of open_abs_path (e.g., whether it implicitly creates missing files). To make the test deterministic and self-contained, create the external file (or insert an /external tree) in app_state.fs.as_fake() before calling open_abs_path.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Head branch was pushed to by a user without write access
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
b0000a0 to
23772d0
Compare
Summary
Creating a new file via the file picker incorrectly used the focused file's directory as the target even when that file did not belong to any open worktree (e.g., Zed opened to a single file, settings.json focused without a worktree, or an external file focused). This PR changes the selection logic so the picker only uses the focused file's worktree if that file actually belongs to one of the visible project worktrees; otherwise, it falls back to the existing project heuristic (query-root match or project default), which prevents misdirected file creation. This addresses zed-industries/zed#41940.
Problem
Scenarios that errored or created in the wrong place:
Zed opened to a single file from the CLI (no worktree)
No worktree open with settings.json focused
A worktree is open but settings.json is focused
A worktree is open but an external (non-worktree) file is focused
Root Cause
set_search_matchesunconditionally overwroteexpect_worktreeusing thecurrently_opened_pathworktree_id, even if the focused file was not part of any visible worktree.Fix
Query-derived worktree remains primary:
Only use the focused file as a secondary signal if it is relevant:
worktree_idexists within visible worktreesexpect_worktreeIf no worktree is determined (e.g., no worktrees are open), Create New is not offered.
Implementation Details
Iterate over
&available_worktreewhen scanning roots and clone the matched entity to avoid moving out of the iteratorValidate focused file worktree membership:
focused_file_in_available_worktreeby scanning visible worktrees for a matching idexpect_worktreeonly if that check passesPreserve existing guard rails for Create New:
Match::CreateNewwhen a concreteexpect_worktreeis present and the query doesn't end with a trailing separatorKey Code Changes
Before
Always overwrote
expect_worktreewith the focused file'sworktree_id, even for external or non-project files.After
Only override
expect_worktreewhen the focused file belongs to a visible worktree. Otherwise, keep the query-derived or default project worktree.User-Facing Behavior
No Worktree Open
Example: Zed launched with a single file or only settings.json visible
Worktree Open + Non-Project File Focused
Example: A non-project file or settings.json is in focus
Multiple Worktrees Open + Query Rooted to One
Example: Query specifies a particular worktree root
Tests
Added:
test_create_file_focused_file_not_belong_to_available_worktreesProjectPath.worktree_idequals either A or B, and the relative path is "new-file.txt"Existing tests for Create New, absolute/relative matching, and multi-worktree behavior continue to pass.
Reproduction Steps (Pre-Fix)
Launch Zed with a single file:
Or open settings.json with no project
Open the file finder and type a new filename, then press Enter
Observe Create New trying to use the focused file's directory or failing unexpectedly
Expected vs Actual
Expected:
Actual (pre-fix):
Migration and Compatibility
Reference
Fixes: zed-industries/zed#41940
Fixes #41940
Appendix: Code Snippets
Guard before overriding with focused file:
Root-based worktree selection with non-moving iteration:
This PR includes a targeted test ensuring that when the focused file is outside all worktrees, Create New still creates within the project worktree(s), preventing regressions.
Release Notes