Skip to content

file_finder: Don’t use focused file’s directory for CreateNew unless it belongs to project worktrees#42076

Merged
SomeoneToIgnore merged 9 commits intozed-industries:mainfrom
CLoaKY233:file_finder_createfile_fix
Feb 12, 2026
Merged

file_finder: Don’t use focused file’s directory for CreateNew unless it belongs to project worktrees#42076
SomeoneToIgnore merged 9 commits intozed-industries:mainfrom
CLoaKY233:file_finder_createfile_fix

Conversation

@CLoaKY233
Copy link
Contributor

@CLoaKY233 CLoaKY233 commented Nov 6, 2025

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:

  1. Zed opened to a single file from the CLI (no worktree)

    • Create New was offered and then failed or targeted a non-project directory
  2. No worktree open with settings.json focused

    • Same failure mode as above
  3. A worktree is open but settings.json is focused

    • Create New used settings.json's directory
  4. A worktree is open but an external (non-worktree) file is focused

    • Create New used the external file's directory

Root Cause

set_search_matches unconditionally overwrote expect_worktree using the currently_opened_path worktree_id, even if the focused file was not part of any visible worktree.


Fix

Query-derived worktree remains primary:

  • Try to match the query path against the roots of visible worktrees
  • If it matches, use that worktree and strip the root prefix from the query to get the relative path

Only use the focused file as a secondary signal if it is relevant:

  • Check whether the focused file's worktree_id exists within visible worktrees
  • Only then override expect_worktree

If no worktree is determined (e.g., no worktrees are open), Create New is not offered.


Implementation Details

  • Iterate over &available_worktree when scanning roots and clone the matched entity to avoid moving out of the iterator

  • Validate focused file worktree membership:

    • Compute focused_file_in_available_worktree by scanning visible worktrees for a matching id
    • Override expect_worktree only if that check passes
  • Preserve existing guard rails for Create New:

    • Only push Match::CreateNew when a concrete expect_worktree is present and the query doesn't end with a trailing separator

Key Code Changes

Before

Always overwrote expect_worktree with the focused file's worktree_id, even for external or non-project files.

After

Only override expect_worktree when 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

  • The file picker will not offer "Create New"

Worktree Open + Non-Project File Focused

Example: A non-project file or settings.json is in focus

  • "Create New" is offered
  • New file is created within the project worktree (based on root match or project default)
  • New file is never created beside the external file

Multiple Worktrees Open + Query Rooted to One

Example: Query specifies a particular worktree root

  • The worktree is selected by root-name match
  • Project default selection applies if no match is found

Tests

Added: test_create_file_focused_file_not_belong_to_available_worktrees

  1. Set up two worktrees A and B; open an external file that belongs to neither
  2. Use the file picker to create "new-file.txt"
  3. Assert the new file opens in an editor whose ProjectPath.worktree_id equals 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)

  1. Launch Zed with a single file:

    zed /tmp/foo.txt

    Or open settings.json with no project

  2. Open the file finder and type a new filename, then press Enter

  3. Observe Create New trying to use the focused file's directory or failing unexpectedly


Expected vs Actual

Expected:

  • No Create New when there is no project worktree
  • When a project exists, Create New targets the appropriate worktree, not the focused external file's directory

Actual (pre-fix):

  • Create New used the focused file's directory, even if it was external or unrelated to the project

Migration and Compatibility

  • No user configuration changes required
  • Behavior now aligns with user expectation: Create New is only offered when a project worktree is available and it always targets a project worktree

Reference

Fixes: zed-industries/zed#41940
Fixes #41940


Appendix: Code Snippets

Guard before overriding with focused file:

let focused_file_in_available_worktree = available_worktree.iter().any(|wt| wt.read(cx).id() == worktree_id);

if focused_file_in_available_worktree { 
    expect_worktree = project.worktree_for_id(worktree_id, cx); 
}

Root-based worktree selection with non-moving iteration:

for worktree in &available_worktree { 
    if query_path.strip_prefix(worktree.read(cx).root_name()).is_ok() { 
        expect_worktree = Some(worktree.clone());} 
}

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

  • Fixed "Create New" in the file picker targeting the wrong directory when a non-project file was focused.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Nov 6, 2025
@CLoaKY233 CLoaKY233 marked this pull request as ready for review November 6, 2025 09:40
@zed-industries-bot
Copy link
Contributor

zed-industries-bot commented Nov 6, 2025

Warnings
⚠️

This PR is missing release notes.

Please add a "Release Notes" section that describes the change:

Release Notes:

- Added/Fixed/Improved ...

If your change is not user-facing, you can use "N/A" for the entry:

Release Notes:

- N/A
Messages
📖

This PR includes links to the following GitHub Issues: #41940
If this PR aims to close an issue, please include a Closes #ISSUE line at the top of the PR body.

Generated by 🚫 dangerJS against 23772d0

@CLoaKY233 CLoaKY233 changed the title file-finder: don’t use focused file’s directory for CreateNew unless it belongs to project worktrees; ad File-finder: don’t use focused file’s directory for CreateNew unless it belongs to project worktrees; ad Nov 6, 2025
@CLoaKY233 CLoaKY233 changed the title File-finder: don’t use focused file’s directory for CreateNew unless it belongs to project worktrees; ad File-finder: don’t use focused file’s directory for CreateNew unless it belongs to project worktrees Nov 6, 2025
@maxdeviant maxdeviant changed the title File-finder: don’t use focused file’s directory for CreateNew unless it belongs to project worktrees file_finder: Don’t use focused file’s directory for CreateNew unless it belongs to project worktrees Nov 6, 2025
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.
@CLoaKY233 CLoaKY233 force-pushed the file_finder_createfile_fix branch from d4d86d4 to 425247e Compare December 1, 2025 08:21
@CLoaKY233
Copy link
Contributor Author

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, Create New incorrectly tries to use that file's directory instead of the project worktree.

This PR: Validates that the focused file belongs to an available worktree before using it as the target directory.

Rebased on latest main with all tests passing. Happy to address any feedback!

@CLoaKY233 CLoaKY233 closed this Dec 16, 2025
@CLoaKY233 CLoaKY233 deleted the file_finder_createfile_fix branch December 16, 2025 17:02
@github-project-automation github-project-automation bot moved this from Community PRs to Done in Quality Week – December 2025 Dec 16, 2025
@CLoaKY233 CLoaKY233 restored the file_finder_createfile_fix branch December 16, 2025 17:02
@CLoaKY233 CLoaKY233 reopened this Dec 16, 2025
Copilot AI review requested due to automatic review settings February 12, 2026 10:54
Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

@SomeoneToIgnore SomeoneToIgnore enabled auto-merge (squash) February 12, 2026 10:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +1293 to +1304
workspace.open_abs_path(
PathBuf::from(path!("/external/external-file.txt")),
OpenOptions {
visible: Some(OpenVisible::None),
..OpenOptions::default()
},
window,
cx,
)
})
.await
.unwrap();
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
SomeoneToIgnore and others added 2 commits February 12, 2026 13:03
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
auto-merge was automatically disabled February 12, 2026 11:05

Head branch was pushed to by a user without write access

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@SomeoneToIgnore SomeoneToIgnore enabled auto-merge (squash) February 12, 2026 11:08
@SomeoneToIgnore SomeoneToIgnore force-pushed the file_finder_createfile_fix branch from b0000a0 to 23772d0 Compare February 12, 2026 11:20
@SomeoneToIgnore SomeoneToIgnore merged commit 7556609 into zed-industries:main Feb 12, 2026
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

Development

Successfully merging this pull request may close these issues.

Using the file picker to create a new file errors for worktree edge cases

5 participants