Fix stale markdown preview after reopening document #332081
Pull request overview
Prevents stale Markdown previews by tying token cache entries to a specific document instance.
Changes:
<details open>
<summary>Show a summary per file</summary>
| File | Description |
| ---- | ----------- |
| extensions/markdown-language-features/src/markdownEngine.ts | Prevents cache reuse across document instances. |
| extensions/markdown-language-features/src/test/engine.test.ts | Tests reopening with identical URI/version but changed content. |
</details>
Review details
💡 <a href="/microsoft/vscode/new/main?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a code-review agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/en/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
Verified the diff. Changes align with project standards.
Heads up — I've been chasing #331900 from the other end, and I think this PR is correct but
resolves only half of it. Worth knowing before it merges, so the issue isn't closed prematurely.
There are two independent caches keyed on (uri, version), and a re-opened document restarts at
version 1, so both go stale together:
1. TokenCache in markdownEngine.ts — what this PR fixes.
2. PreviewDocumentVersion.equals in preview/preview.ts — compares resource + version, so
#updatePreview returns early and never calls the renderer at all.
Because layer 2 short-circuits before the engine is reached, this PR alone doesn't change the
observed behaviour. I built all four combinations and measured the rendered preview DOM after the
document lease lapsed:
| preview.ts | markdownEngine.ts | preview after an external write |
|---|---|---|
| upstream | upstream | stale |
| upstream | this PR | stale |
| identity-aware equals | upstream | stale |
| identity-aware equals | this PR | updates correctly |
I've opened https://github.com/microsoft/vscode/pull/333712 for layer 2. It's 6 lines and doesn't touch markdownEngine.ts, so there's no
overlap with this one — they just need to land together to close the issue. I've written mine as
"Part of #331900" rather than "Fixes" for that reason; you may want to do the same here.
Repro, if you want to check it yourself (the wait matters — before the lease lapses everything works,
which is why this is awkward to reproduce interactively):
1. mkdir /tmp/mdbug && printf '# V0\n' > /tmp/mdbug/test.md
2. Open the folder, right-click test.md in the explorer → Open Preview. Don't open it in an editor.
3. Wait more than three minutes.
4. printf '# CHANGED\n' > /tmp/mdbug/test.md
5. Preview still shows V0.
Heads up — I've been chasing #331900 from the other end, and I think this PR is correct but resolves only half of it. Worth knowing before it merges, so the issue isn't closed prematurely.
Thanks, nice catch. I'll defer to the maintainers on how they want to handle closing the issue. I'm fine either way.
dianatofficial
dkontyko
Fixes #331900
Summary
Markdown token caching currently identifies documents by URI and
TextDocument.version. Because versions are scoped to a document instance and can restart after a file is closed and reopened, a new document can collide with cached tokens from the previous instance and render stale preview content.Cache tokens by document identity in addition to the existing version and Markdown configuration checks. This preserves cache hits during the lifetime of an open document while preventing reuse across reopened document instances.
A regression test renders two document instances with the same URI and version but different content and verifies that the reopened document is tokenized again.
Testing
markdown.enginetest file passed (10 tests).git diff --checkpassed.