Skip to content

worktree: Include file size in DiskState to fix stale buffer reload#48691

Closed
lex00 wants to merge 2 commits intozed-industries:mainfrom
lex00:fix/38109-diskstate-size
Closed

worktree: Include file size in DiskState to fix stale buffer reload#48691
lex00 wants to merge 2 commits intozed-industries:mainfrom
lex00:fix/38109-diskstate-size

Conversation

@lex00
Copy link
Copy Markdown
Contributor

@lex00 lex00 commented Feb 7, 2026

Related to #38109

When an external tool writes a file using std::fs::write() (truncate + write), the scanner can read the 0-byte file during the truncation window. The buffer reloads to empty. If the subsequent write produces the same mtime, file_updated() sees no DiskState change and never triggers a second reload. The buffer is permanently stuck empty.

Adds size: u64 to DiskState::Present { mtime, size }. The derived PartialEq now detects size changes even when mtime matches, so a 0→N byte transition always triggers ReloadNeeded. The scanner already tracks file size in Entry.size, this threads it through File::for_entry() and local_worktree_entry_changed() into the reload comparison.

Remote/collab callers pass size: 0 since remote buffers don't make local reload decisions (the reload path checks !self.is_via_collab()).

Root cause analysis: #38109 (comment)

Related issues:

Note: the two RealFs tests use allow_parking() + flush_fs_events() and must be run individually (--exact), not in parallel with other RealFs tests. This matches the existing pattern in the crate.

  • Tests
  • Code Reviewed
  • Manual QA

Release Notes:

  • Fixed an issue where open buffers could become permanently empty when external tools (AI agents, formatters) wrote files while they were open in the editor.

@AdamJedl
Copy link
Copy Markdown
Contributor

AdamJedl commented Feb 7, 2026

I think this shouldn't close issue #38109 because it have multiple problems, as you said in this comment.

@lex00
Copy link
Copy Markdown
Contributor Author

lex00 commented Feb 7, 2026

Good catch, you're right. Updated to "Related to" instead. Thanks!

@zed-industries-bot
Copy link
Copy Markdown
Contributor

Messages
📖

This PR includes links to the following GitHub Issues: #38109
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 7ad2d06

lex00 and others added 2 commits February 13, 2026 11:52
When an external tool writes a file using `std::fs::write()` (truncate +
write), the scanner can read the 0-byte file during the truncation window.
The buffer reloads to empty. If the subsequent write produces the same
mtime, `file_updated()` sees no DiskState change and never triggers a
second reload — the buffer is permanently stuck empty.

Add `size: u64` to `DiskState::Present { mtime, size }`. The derived
PartialEq now detects size changes even when mtime matches, so a 0-to-N
byte transition always triggers ReloadNeeded. The scanner already tracks
file size in `Entry.size` — this threads it through `File::for_entry()`
and `local_worktree_entry_changed()` into the reload comparison.

Remote/collab callers pass `size: 0` since remote buffers don't make
local reload decisions.

Closes zed-industries#38109

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@lex00 lex00 force-pushed the fix/38109-diskstate-size branch from 7ad2d06 to 399e9df Compare February 13, 2026 18:54
pcfreak30 added a commit to pcfreak30/zed that referenced this pull request Feb 13, 2026
Applied 5 PRs to fix Zed getting out of sync with filesystem changes:
- zed-industries#48691: Include file size in DiskState to fix stale buffer reload
- zed-industries#48695: Map EventKind::Other to Changed instead of None
- zed-industries#48698: Reload after undo when file changed while dirty
- zed-industries#48704: Rescan repositories when window regains focus
- zed-industries#47462: Handle removed Linux watch paths
@lex00 lex00 closed this Mar 1, 2026
@lex00 lex00 deleted the fix/38109-diskstate-size branch March 1, 2026 20:21
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

None yet

Development

Successfully merging this pull request may close these issues.

Zed shows cached/old file content when file is modified outside the editor

3 participants