Repository navigation
Compare document identity when deciding if the preview needs updating - #333712
Jaeke Barkin (jrbarkin) wants to merge 2 commits into
Conversation
|
@microsoft-github-policy-service agree |
There was a problem hiding this comment.
Pull request overview
Updates Markdown preview version tracking to distinguish reopened document instances and avoid suppressing required refreshes.
Changes:
- Compares document identity alongside version.
- Adds regression tests for identity and version behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
preview.ts |
Tracks document identity during preview updates. |
previewDocumentVersion.test.ts |
Tests version and identity comparisons. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
|
||
| public constructor(document: vscode.TextDocument) { | ||
| this.resource = document.uri; | ||
| this.#document = document; |
There was a problem hiding this comment.
Good catch — verified and fixed in 8e0623a.
I checked this against the source rather than assuming: MirrorTextModel.dispose() does clear the
text (this._lines.length = 0), but ExtHostDocumentData deliberately overrides dispose() and
does not call super — "we don't really dispose documents but let extensions still read from them" —
so _lines survives. Since every getter on the facade closes over that object, holding the facade
did keep a closed document's full text alive for the life of the preview, which defeats exactly the
reclamation the document lease exists to perform.
Switched to a WeakMap<vscode.TextDocument, number> handing out identity tokens. The map doesn't
retain its keys and the stored token is a primitive, so identity stays comparable without holding
the document.
Re-verified after the change: typecheck and eslint clean, 96 passing / 0 failing (95 passing /
1 failing against main, so the regression test still discriminates), and the end-to-end check on
a build from source still shows the preview updating after the lease lapses.
Part of microsoft#331900 A markdown preview opened from the explorer has no editor holding the document open, so the document is kept alive only by the lease taken in #updatePreview, which lapses after a few minutes. Once it lapses, the next update re-opens the file as a new text document whose version numbering restarts at 1. PreviewDocumentVersion compares only the resource and the version number, so that re-opened document can compare equal to the version last rendered even though the file on disk has changed, and #updatePreview returns early without rendering. The preview then stays stale for that write and every write after it. Compare document identity instead, so a version recorded from a different document object can never suppress an update. Identity implies the uris match, so they are no longer compared separately. The same restarted version number also defeats TokenCache in markdownEngine.ts, which keys on uri and version, so both layers need fixing before a preview recovers. That second layer is a separate bug with its own reproduction that does not involve the preview at all, and is already open as microsoft#332081; this change does not duplicate it. This commit is therefore "part of" rather than "fixes", so the issue is not closed while half the fix is still outstanding.
Documents are not really disposed when they close: ExtHostDocumentData overrides dispose so extensions can keep reading from them, which means it does not clear the underlying lines the way MirrorTextModel.dispose does. Every getter on the TextDocument facade closes over that data, so holding the facade kept a closed document's full text alive for as long as the preview lived, defeating the reclamation the document lease exists to perform. Hand out identity tokens from a WeakMap keyed on the document instead. The map does not retain its keys and the token is a primitive, so identity stays comparable without holding on to the document.
8e0623a to
83f2365
Compare
|
hey Henning Dieterichs (@hediet), no rush, just thought i'd ping. This PR and #332081 are the two halves of #331900. Both layers identify a document by URI + version, and the version restarts at 1 when the file is reopened after the preview's document lease lapses. This PR fixes Note that #332081 says There's also a fresh report on 1.140.0 in #265277 that looks like the same bug: #265277 (comment) Rebased onto main a few days ago; the markdown test suite passes. |
|
hey Henning Dieterichs (@hediet), just circling back on this when you have a moment. happy to adjust anything if needed. |
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.
Part of #331900 — deliberately not
Fixes, because this is one of two changes needed and theissue shouldn't auto-close when this merges. See "Needs #332081 as well" below.
#331900 isn't labelled yet, so per the contributing guide I'm happy to close this if an external
contribution isn't wanted here — #332081 is already open against the same issue, so I've assumed
it is. Glad to move the discussion to the issue first if that's preferred.
Same underlying bug as #265277, #271233 and #316067. I've referenced #331900 rather than #265277
because #265277 carries
team-low-hanging("No external contributions will be accepted") — happyto re-target if a maintainer prefers.
The bug
A markdown preview opened from the explorer has no editor holding the document open. The document
stays alive only through the lease taken by
#updatePreview'sopenTextDocumentcall(
BoundModelReferenceCollection,_maxAgeof a few minutes), which is never renewed. Observed inan instrumented extension host:
After that, the next update re-opens the file as a new text document, whose version numbering
restarts at 1.
PreviewDocumentVersioncompares only resource and version:So the re-opened document compares equal to the version last rendered even though the file changed
on disk, and
#updatePreviewreturns early at its!forceUpdate && this.#currentVersion?.equals(pendingVersion)guard without rendering. The preview stays stale for that write and every write after it.
This is why the reports cluster around code generators, formatters and CLI agents: they write to a
file nobody has open in an editor, then keep writing to it.
Reproduction
mkdir /tmp/mdbug && printf '# V0\n' > /tmp/mdbug/test.mdtest.md→ Open Preview.Do not open the file in an editor.
printf '# CHANGED\n' > /tmp/mdbug/test.mdV0, and stays that way.The wait in step 3 is why this is awkward to reproduce interactively: before the lease lapses the
document is still resident and
onDidChangeTextDocumentdrives the refresh, so everything works.The change
Compare document identity in
PreviewDocumentVersion, so a version recorded from a differentdocument object can never suppress an update. Identity implies the uris match —
TextDocument.uriis set once in
MirrorTextModel's constructor and the facade is memoized and frozen — so the uricomparison is dropped rather than kept alongside.
#currentVersionnow retains a reference to a possibly-closedTextDocument. It is bounded to oneper live preview, replaced on every update, and only ever compared by identity — never dereferenced.
Needs #332081 as well
These are two independent bugs on the same root pattern, and both must be fixed before a preview
recovers.
TokenCacheinmarkdownEngine.tskeys on uri and version, so it hits the same restartedversion number and serves the previously cached tokens — meaning even a successful re-render draws
the old content. That is #332081, which I have not duplicated here.
Verified by building each combination and measuring the rendered preview DOM after the lease lapsed.
Each fix's necessity comes from the row where the other fix is already applied and the bug survives:
preview.tsmarkdownEngine.tsScope of that result: measured on macOS (darwin 25.5.0) against
mainat 717304b, using thereproduction below. I haven't tested Windows or Linux, and I can't rule out some other path where
one change alone is enough.
Approaches that don't work
Measured, in case it saves anyone time:
onDidCreatedoes nothing. I assumed atomic writers(temp file +
rename) would surface only as a create event and that theonDidChange-onlysubscription was the bug. Logging watcher events for four write styles — in-place, same-directory
rename, cross-directory rename, unlink-then-create — every one emits
onDidChangeas well asonDidCreate. The existing subscription already receives the event. Built it, measured it, noimprovement.
refresh(true)) from the watcher does not work either. The token cachedefeats it one layer down, and it costs a full
webview.htmlreload per write instead of thecheap
postMessagepath. Separately,refresh()drops the call when#throttleTimeris alreadyarmed, so the force is racy anyway.
This also answers the question asked when closing #297649 ("Have you explored why the existing
events aren't working?"): the events fire correctly. The update is discarded afterwards — first by
this version comparison, then by the token cache.
Tests
Adds
PreviewDocumentVersiontests. The identity case fails onmainand passes with this change:Run with
npx vscode-test --label markdown-language-features.