Skip to content

Fix markdown preview not refreshing on external file changes - #297649

Closed
yuppppppppppqi wants to merge 2 commits into
microsoft:mainfrom
yuppppppppppqi:fix/markdown-preview-refresh-on-external-change
Closed

yuppppppppppqi wants to merge 2 commits into
microsoft:mainfrom
yuppppppppppqi:fix/markdown-preview-refresh-on-external-change

Conversation

@yuppppppppppqi

Copy link
Copy Markdown

Summary

  • Remove the textDocuments.some() guard in the file system watcher callback that prevented preview refresh when VS Code already had the document open
  • Pass forceUpdate=true to refresh() to bypass the internal version check, which may not reflect external changes
  • The existing 300ms throttle in refresh() prevents performance issues from duplicate events

Problem

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, onDidChangeTextDocument does not fire for external overwrites, creating a gap where no refresh path is triggered.

Test plan

  • Open a markdown file and show preview side by side
  • From a terminal, modify the file externally (e.g. echo "# Updated" >> file.md)
  • Verify the preview updates automatically
  • Verify that normal in-editor editing still updates the preview (no regression)
  • Verify that switching tabs and returning shows current content

Fixes #265277
Fixes #194421

🤖 Generated with Claude Code

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. When an editor tab is closed, TextFileEditorModel is disposed and its file watcher stops.
  2. However, the TextDocument object lingers in workspace.textDocuments for up to ~5 minutes (this is by design — see workspace.textDocuments includes closed documents #47140, Documents opened using window.showTextDocument send a delayed didClose event to LSP #201294).
  3. During this window, onDidChangeTextDocument never fires for external changes because the underlying model is gone, yet the old textDocuments.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; onDidChangeTextDocument keeps 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 TextDocument with isDirty === 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]>
@mjbvz

Copy link
Copy Markdown
Contributor

Closing this as it doesn't seem like the correct approach

@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Jul 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown preview doesn't refresh/reload when the file is overwritten Markdown Preview shows outdated file content

2 participants