zed/crates/worktree
Frank Loesche 53b39e8e89
worktree: Fix global gitignore matching outside the worktree root (#62130)
# Objective

When a worktree has no enclosing git repository, the `global-gitignore`
check falls back to matching the raw, unbounded absolute path. This lets
an ancestor directory outside the worktree, or the worktree's own root
name, incorrectly match a bare global-ignore pattern (e.g. a global
entry like `tmp` or `*.com`) and mark the entire worktree as ignored,
which silently breaks project search and greys out every file in the
project panel.
  
Fixes #62126
Fixes #48887

## Solution

- In `Snapshot::ignore_stack_for_abs_path`, when no containing
repository is found, fall back to the worktree's own root as the
boundary for `IgnoreStack.repo_root`, instead of leaving it `None`.
- This reuses the existing repo-root-relative matching logic in
`IgnoreStack::is_abs_path_ignored` (already correct for the
git-repository case since #61689) without needing any changes to
`ignore.rs` itself: ancestors outside the worktree now correctly fail
the prefix check and are treated as not ignored, while the global
gitignore still applies to files and directories within the worktree.

## Testing

- Added `test_global_gitignore_without_repository` in
`crates/worktree/tests/integration/worktree_tests.rs`, covering both
variants in one worktree: an ancestor directory outside a non-git
worktree matching a global pattern, and the worktree's own root name
matching one, while also confirming a file that matches the pattern from
within the worktree is still ignored.
- Verified the new test fails without the fix (reproducing both #62126
and #48887) and passes with it.
- Ran the full `worktree` integration test suite (`cargo test -p
worktree --test integration`); all tests pass.
- Not tested on Windows or macOS, only Linux.

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] Tests cover the changed behavior


---

Release Notes:

- Fixed global gitignore entries incorrectly matching directories
outside the opened project and marking the entire project as ignored

---------

Co-authored-by: dino <dinojoaocosta@gmail.com>
2026-08-20 12:29:17 +00:00
..
src worktree: Fix global gitignore matching outside the worktree root (#62130) 2026-08-20 12:29:17 +00:00
tests/integration worktree: Fix global gitignore matching outside the worktree root (#62130) 2026-08-20 12:29:17 +00:00
Cargo.toml Switch from cargo-machete to cargo-shear (#62643) 2026-08-20 10:08:48 +00:00
LICENSE-GPL