Repository navigation
Markdown preview: refresh on file-system change even when document is open - #316756
Open
Yogeshwaran C (yogeshwaran-c) wants to merge 1 commit into
Open
Yogeshwaran C (yogeshwaran-c) wants to merge 1 commit into
Yogeshwaran C (yogeshwaran-c) wants to merge 1 commit into
Conversation
… open
When the previewed markdown file is overwritten on disk (e.g. a different
file copied over it, an external generator regenerates the file, an editor
agent rewrites it), the preview can keep showing stale content.
The file-system watcher in `MarkdownPreview` was guarded by:
if (!vscode.workspace.textDocuments.some(doc => areUrisEqual(doc.uri, uri))) {
this.refresh();
}
The intent was to avoid a duplicate refresh when the file was also being
tracked as an open `TextDocument` (in which case `onDidChangeTextDocument`
would handle the update). In practice that assumption does not always
hold: the text document model's reload from disk can be skipped or
delayed by etag/stat caching, network/remote FS timing, or
content-equality short-circuits, so `onDidChangeTextDocument` never
fires for the change. The watcher event was the only remaining signal,
and the guard silently dropped it -- leaving the preview frozen on the
pre-overwrite content until the user manually edited or refreshed it.
Drop the guard so the watcher always calls `refresh()`. Any genuine
overlap with `onDidChangeTextDocument` is already coalesced by the
300ms `#throttleTimer` debounce and the `PreviewDocumentVersion`
equality check inside `#updatePreview`, so unconditionally refreshing
here does not introduce duplicate renders.
Fixes microsoft#265277
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to make Markdown previews refresh when the backing file changes on disk, even if the document is already open in VS Code.
Changes:
- Removes the open-document guard from the Markdown preview file-system watcher.
- Adds explanatory comments about why file-system events should trigger refreshes unconditionally.
| // content. Any genuine duplicate refresh that overlaps with `onDidChangeTextDocument` | ||
| // is already coalesced by the `#throttleTimer` debounce and version-equality check in | ||
| // `#updatePreview`, so unconditionally refreshing here is safe. | ||
| this.refresh(); |
|
Any updates? 🥹 |
This was referenced Sep 5, 2026
Henning Dieterichs (hediet)
added a commit
that referenced
this pull request
Oct 31, 2026
Fixes #271233 Part of #265277 Several reports in those issues describe the same sequence: the preview updates correctly while it is visible, the user switches to another tab and back, and the preview shows the content from before the update. This applies to the dynamic preview (`markdown.showPreview`) and to the `vscode.markdown.preview.editor` custom editor, with edits made in VS Code as well as with files overwritten on disk. ### Root cause `MarkdownPreview` applies updates made while the panel is visible by posting an `updateContent` message to the webview, and leaves `webview.html` untouched. Neither preview uses `retainContextWhenHidden`, so VS Code discards the webview when the panel is hidden and rebuilds it from `webview.html` when the panel is revealed. That html is whatever the last full render produced, so every update applied through `updateContent` since then is lost on reveal. Nothing in the preview reacts to the panel being hidden, and the version check in `#updatePreview` keeps a later refresh from re-rendering unchanged content. ### Fix - Track whether `webview.html` is behind what the webview shows (`#webviewHtmlOutOfDate`). - When the panel is hidden while the html is behind, request a forced refresh, which goes through the existing full render path and brings `webview.html` up to date before the panel can be revealed. Panels that never received an in-place update, and panels updated while hidden (which already re-render fully), are unaffected. - If the panel is hidden while an in-place update is still rendering, the update requests the forced refresh itself: the hide event found the html up to date and scheduled nothing, and the webview the message was meant for is already gone. - `refresh(true)` now keeps its force flag when an update is already scheduled. Before, a forced refresh requested while the 300 ms throttle timer was pending was silently downgraded to a regular update. ### Verification Measured by driving the extension from source in Insiders 1.137.0-insider (de8cc55) with `--extensionDevelopmentPath`, reading the rendered `.markdown-body` text of the preview iframe over the remote debugging port after each step. "hidden" reads as no preview iframe at all, which is the webview being discarded. | Scenario | Step | main (42b37b7) | with this change | | --- | --- | --- | --- | | Dynamic preview, file overwritten on disk while visible, tab switch | 1 preview opened | D1-A | D1-A | | | 2 after external write | D1-B | D1-B | | | 3 preview hidden behind another tab | hidden | hidden | | | 4 preview tab shown again | **D1-A (stale)** | D1-B | | Dynamic preview, edit + save in a text editor, tab switch | 1 preview opened | D1-A | D1-A | | | 2 after edit and save | D1-C | D1-C | | | 3 preview hidden behind another tab | hidden | hidden | | | 4 preview tab shown again | **D1-A (stale)** | D1-C | | Dynamic preview, file overwritten while hidden (control) | 1 preview opened | D1-A | D1-A | | | 2 preview tab shown after the hidden write | D1-D | D1-D | | `vscode.markdown.preview.editor`, file overwritten on disk while visible, tab switch | 1 editor opened | D1-A | D1-A | | | 2 after external write | D1-E | D1-E | | | 3 editor hidden behind another tab | hidden | hidden | | | 4 editor tab shown again | **D1-A (stale)** | D1-E | The new tests in `src/test/preview.test.ts` revive a `DynamicMarkdownPreview` on a real webview panel. The first applies an edit while the panel is visible, hides the panel behind a text editor and asserts that `webview.html` catches up; it fails on main at that last assertion and passes with this change. The second holds the in-place render of an edit, hides the panel while it is in flight, releases the render and asserts the same; it fails when the branch in `#updateWebviewContent` is removed. The third makes a further edit so a regular refresh is pending, hides and re-reveals the panel, and asserts the reveal still gets a full reload. The full markdown-language-features suite passes (99 tests, 1 skipped). ### Related issues - #147718's original report and recording show the same stale-content reset after an edit and tab switch. Its newer comments about preserving an independent preview scroll position describe a separate behavior that this PR does not claim to fix. - #271233 is this sequence exactly (modify the file, hide the preview behind another editor in the same group, show it again). The later comment there about closing and reopening the preview is a different mechanism, see below. - #165188 (closed as a duplicate of #147718) and #244328 (closed) describe the same tab-switch sequence. - #331900, #194421 and #316067 are about a preview that is closed and reopened, or opened fresh, showing old content. The webview is created new in those flows, so this change does not apply; #332081 and #333712 target that path. ### Relation to the other open PRs - #308427 (`refresh()` when the panel becomes visible, claims to fix #271233) calls the non-forced `refresh()` of the base class. The document version has not changed since the in-place update, so `#updatePreview` returns at the version check and `webview.html` stays stale. Measured with that diff applied on top of 1.128.0: step 4 of the first scenario above still shows D1-A. - #166932 (`refresh()` when the dynamic preview becomes inactive, 2022) goes through `DynamicMarkdownPreview.refresh()`, which is forced, so it does fix the dynamic preview (steps 4 above render D1-B and D1-C with it). It reloads the whole page every time the preview loses focus, whether or not anything changed, which is the concern raised in its review, and it does not cover `vscode.markdown.preview.editor` (step 4 of the last scenario stays D1-A). This change reloads only when an in-place update has actually happened and covers both previews. - #307245 defers refreshes while the panel is hidden and re-renders on reveal only if a change arrived while hidden. It does not change what happens to updates applied before the hide, so the two changes are independent. - #333712 (identity-aware `PreviewDocumentVersion.equals`) and #332081 (token cache identity) address refreshes being skipped while the preview is visible; no overlap. #316756 removes the watcher guard for documents VS Code already tracks; it does not change what a hidden panel is rebuilt from, so the tab-switch case above stays stale with it.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #265277 — markdown preview keeps showing stale content after the file is overwritten on disk (external generator, file copy, editor agent,
git pull, etc.).Root cause
MarkdownPreviewinstalls aFileSystemWatcherfor the previewed resource but wraps the refresh in a guard:The intent was to avoid a duplicate refresh when the file is also tracked as an open
TextDocument, deferring toonDidChangeTextDocumentin that case. In practice that assumption does not always hold for external overwrites: the text document model's reload from disk can be skipped or delayed by etag/stat caching, network/remote FS timing, or content-equality short-circuits, soonDidChangeTextDocumentnever fires for the change. The file-system event was the only remaining signal, and the guard silently dropped it — leaving the preview frozen on the pre-overwrite content until the user manually edited or invokedMarkdown: Refresh preview.This matches the persistent reports in the issue thread after #300629 landed (the case-insensitive URI fix only addressed the Windows-casing variant; users on Ubuntu/macOS/remote SSH continued to report it for non-casing reasons).
Fix
Drop the open-document guard so the watcher always calls
refresh()when the previewed file changes on disk. The two paths can no longer drop each other's events.Any genuine duplicate refresh that overlaps with
onDidChangeTextDocumentis already coalesced downstream:MarkdownPreview.refresh()debounces via the 300 ms#throttleTimer, so tworefresh()calls within the window collapse into one#updatePreviewinvocation.#updatePreviewearly-returns whenPreviewDocumentVersionmatches the last rendered version, so a redundant invocation against an unchanged in-memory document is a no-op.So unconditionally refreshing here does not introduce duplicate renders.
Test plan
Manual repro on Windows 11 (the original report's platform), confirmed before/after the change:
repro.mdwith some content, open it, then open the markdown preview side-by-side.repro.mdwith a different markdown file (copy a different file with the same name on top of it; or have an external script regenerate it).Markdown: Refresh previewis required.onDidChangeTextDocumentpath is unchanged).tsc -p extensions/markdown-language-features/tsconfig.json --noEmitclean).A unit test isn't really feasible here without a real file-system + watcher harness — the bug is specifically about events that the in-process text model misses.