Skip to content

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

Merged
probably-neb merged 2 commits intozed-industries:mainfrom
lex00:fix/dirty-buffer-reload-after-undo
Mar 10, 2026
Merged

buffer: Reload after undo when file changed while dirty#51037
probably-neb merged 2 commits intozed-industries:mainfrom
lex00:fix/dirty-buffer-reload-after-undo

Conversation

@lex00
Copy link
Copy Markdown
Contributor

@lex00 lex00 commented Mar 8, 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.

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>
@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Mar 8, 2026
@probably-neb probably-neb self-assigned this Mar 10, 2026
@probably-neb
Copy link
Copy Markdown
Collaborator

Seems like a rare case, but the fix and issue both make sense. Thanks!

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

2 participants