Skip to content

buffer: Reload after undo when file changed while dirty#48698

Closed
lex00 wants to merge 1 commit intozed-industries:mainfrom
lex00:fix/dirty-buffer-reload-after-undo
Closed

buffer: Reload after undo when file changed while dirty#48698
lex00 wants to merge 1 commit intozed-industries:mainfrom
lex00:fix/dirty-buffer-reload-after-undo

Conversation

@lex00
Copy link
Copy Markdown
Contributor

@lex00 lex00 commented Feb 7, 2026

Fixes #48697

Problem

If you edit a file and an external tool writes to it while you have unsaved changes, Zed tracks the new file but skips the reload to preserve your edits. If you then undo everything, the buffer goes back to clean but still shows the old content. The disk has moved on, but nothing triggers a reload.

Part of #38109. Discovered during research for #48691.

Fix

In did_edit(), when the buffer transitions from dirty to clean, check if the file's mtime changed while it was dirty. If so, emit ReloadNeeded. Only fires for files that still exist on disk (DiskState::Present).

7 lines in crates/language/src/buffer.rs.

No double reload

file_updated() suppresses ReloadNeeded when the buffer is dirty (that's the whole bug). So by the time did_edit() fires on dirty-to-clean, no prior reload was emitted for this file change. The two paths are mutually exclusive.

Independent of #48691

This fix compares mtime only, which is sufficient here because the external write always produces a new mtime. The size-based fix in #48691 addresses a different scenario (same mtime, different size). The two PRs don't conflict and don't depend on each other.

Test plan

  • New: test_dirty_buffer_reloads_after_undo
  • No regression in test_buffer_is_dirty or other buffer tests
  • All 54 language buffer tests pass
  • clippy clean

Release Notes:

  • Fixed an issue where buffer content could become stale after undoing edits when an external tool wrote to the file while the buffer was dirty.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Feb 7, 2026
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
When a buffer is dirty and an external tool writes to the file,
`file_updated()` correctly updates the file metadata but suppresses
`ReloadNeeded` because the buffer has unsaved edits. If the user then
undoes all edits, the buffer becomes clean but still shows the old
content while the disk has newer content.

Fix: in `did_edit()`, after detecting a dirty-to-clean transition,
check if the file's disk mtime differs from the buffer's saved mtime.
If so, emit `ReloadNeeded` to trigger a reload from disk.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@lex00 lex00 force-pushed the fix/dirty-buffer-reload-after-undo branch from b34a7d7 to 7894576 Compare February 13, 2026 19:32
@lex00 lex00 closed this Mar 1, 2026
@lex00 lex00 deleted the fix/dirty-buffer-reload-after-undo branch March 1, 2026 20:21
probably-neb added a commit that referenced this pull request Mar 10, 2026
Closes #48697
Supersedes #48698
Related to #38109

## Problem

If you edit a file and an external tool writes to it while you have
unsaved changes, Zed tracks the new file but skips the reload to
preserve your edits. If you then undo everything, the buffer goes back
to clean but still shows the old content. The disk has moved on, but
nothing triggers a reload.

## Fix

In `did_edit()`, when the buffer transitions from dirty to clean, check
if the file's mtime changed while it was dirty. If so, emit
`ReloadNeeded`. Only fires for files that still exist on disk
(`DiskState::Present`).

7 lines in `crates/language/src/buffer.rs`.

### No double reload

`file_updated()` suppresses `ReloadNeeded` when the buffer is dirty
(that's the whole bug). So by the time `did_edit()` fires on
dirty-to-clean, no prior reload was emitted for this file change. The
two paths are mutually exclusive.

## Test plan

- New: `test_dirty_buffer_reloads_after_undo`
- No regression in `test_buffer_is_dirty` or other buffer tests
- All project integration tests pass
- clippy clean

Release Notes:

- Fixed an issue where buffer content could become stale after undoing
edits when an external tool wrote to the file while the buffer was
dirty.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Ben Kunkle <ben@zed.dev>
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.

Buffer shows stale content after undo when file changed externally while dirty

1 participant