Repository navigation
Fix markdown preview not refreshing on external file changes - #297649
yuppppppppppqi wants to merge 2 commits into
Conversation
The file system watcher callback previously skipped refreshing the preview when VS Code already had the document open in memory. This caused the preview to show stale content when files were modified by external processes such as AI coding agents, scripts, or git sync. Remove the `textDocuments.some()` guard so the watcher always triggers a refresh, and pass `forceUpdate=true` to bypass the internal version check (which may not reflect external changes). The existing 300ms throttle in `refresh()` prevents performance issues from duplicate events when both `onDidChangeTextDocument` and the watcher fire. Fixes microsoft#265277 Fixes microsoft#194421 Co-Authored-By: Claude Opus 4.6 <[email protected]>
| // Always refresh on file system changes, including external modifications | ||
| // (e.g. AI agents, scripts, git sync). The throttle in refresh() prevents | ||
| // performance issues from duplicate events with onDidChangeTextDocument. | ||
| this.refresh(true); |
There was a problem hiding this comment.
This feels like a rather brute force workaround instead of fixing the underlying issue. You'll trigger extra refreshes for cases such as a dirty editor tab where the underlying document is changed by the file system (this shouldn't update the preview)
Have you explored why the existing events aren't working? For example, we may need to refresh on close events too
There was a problem hiding this comment.
Thanks for the review — you're right that the previous approach was too brute-force. I've pushed a new commit that addresses both concerns.
Root cause
The real issue is a lifecycle mismatch between VS Code's document layers:
- When an editor tab is closed,
TextFileEditorModelis disposed and its file watcher stops. - However, the
TextDocumentobject lingers inworkspace.textDocumentsfor up to ~5 minutes (this is by design — see workspace.textDocuments includes closed documents #47140, Documents opened usingwindow.showTextDocumentsend a delayeddidCloseevent to LSP #201294). - During this window,
onDidChangeTextDocumentnever fires for external changes because the underlying model is gone, yet the oldtextDocuments.some()guard still suppresses the file-system event.
So the file-system watcher fires, we find the stale TextDocument, skip the refresh, and onDidChangeTextDocument never comes to the rescue. The preview goes stale.
New approach: isDirty guard
Instead of removing the guard entirely, the updated code now checks doc.isDirty:
const doc = vscode.workspace.textDocuments.find(
d => d.uri.toString() === uri.toString());
if (!doc || !doc.isDirty) {
this.refresh(true);
}- Dirty document → skip the FS event. The editor owns the content;
onDidChangeTextDocumentkeeps the preview current. This also handles your concern about dirty tabs where the underlying file changes on disk — the preview correctly shows the unsaved editor content, not the disk version. - No document / clean document → refresh from disk. This covers closed tabs (zombie
TextDocumentwithisDirty === false) and open-but-saved tabs alike.
This mirrors the pattern used by TextFileEditorModelManager.onDidFilesChange() internally, which also skips dirty models when processing file-system events.
Why not onDidCloseTextDocument?
I explored adding a refresh on close events, but onDidCloseTextDocument fires when the extension host drops the document (up to 5 min after tab close), not when the user closes the tab. There's no reliable extension API event for "user closed this specific editor tab and the file watcher just stopped." The isDirty check sidesteps this gap cleanly.
Replace the blanket `this.refresh(true)` with a targeted isDirty guard. When `TextFileEditorModel` is disposed (editor tab closed), the `TextDocument` can linger in `workspace.textDocuments` for up to 5 minutes while `onDidChangeTextDocument` no longer fires for it. The previous `textDocuments.some()` guard therefore suppressed legitimate file-system events for these zombie documents. The new logic: - If the document is dirty (has unsaved edits), skip the FS event because the preview is already kept current via onDidChangeTextDocument. - Otherwise (document not open or saved), refresh from disk. This mirrors the approach used internally by `TextFileEditorModelManager.onDidFilesChange()`. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Closing this as it doesn't seem like the correct approach |
Summary
textDocuments.some()guard in the file system watcher callback that prevented preview refresh when VS Code already had the document openforceUpdate=truetorefresh()to bypass the internal version check, which may not reflect external changesrefresh()prevents performance issues from duplicate eventsProblem
When a markdown file is modified by an external process (AI coding agents like Claude Code / Copilot, scripts, git sync, file copy/overwrite), the preview does not update. Even closing and reopening the preview tab shows stale content.
Root cause: The watcher callback checks
!vscode.workspace.textDocuments.some(doc => doc.uri.toString() === uri.toString())before refreshing. Since the document is almost always already known to VS Code when a preview is open, external file changes are silently ignored. Additionally,onDidChangeTextDocumentdoes not fire for external overwrites, creating a gap where no refresh path is triggered.Test plan
echo "# Updated" >> file.md)Fixes #265277
Fixes #194421
🤖 Generated with Claude Code