Skip to content

Fix custom editor not resolving for CLI-opened files at startup (#197374) - #312551

Draft
Mukunda Rao Katta (MukundaKatta) wants to merge 1 commit into
microsoft:mainfrom
MukundaKatta:fix/markdown-preview-cli-open
Draft

Mukunda Rao Katta (MukundaKatta) wants to merge 1 commit into
microsoft:mainfrom
MukundaKatta:fix/markdown-preview-cli-open

Conversation

@MukundaKatta

Copy link
Copy Markdown

Summary

Fixes #197374. workbench.editorAssociations (e.g. "*.md": "vscode.markdown.preview.editor") is not honored when a file is opened via CLI and no VS Code instance is running.

Root cause

EditorResolverService.resolveEditor (src/vs/workbench/services/editor/browser/editorResolverService.ts:97-130) snapshots the flattened editor map at line 99 (this._flattenedEditors = this._flattenEditorsMap()) before awaiting whenInstalledExtensionsRegistered(). During that await, extensions register their custom editor contributions through the customEditors extension point, mutating _editors and setting _shouldReFlattenEditors = true. Subsequent lookups (getEditor, findMatchingEditors, getAssociationsForResource) keep iterating the stale snapshot, so the freshly-registered vscode.markdown.preview.editor is invisible and the resolver falls back to the default text editor.

PR #244597 (merged Apr 2026) added the await for user-configured associations but did not re-flatten afterwards.

Fix

Re-flatten after the await. One-line addition + comment.

Files

  • src/vs/workbench/services/editor/browser/editorResolverService.ts (+6)
  • src/vs/workbench/services/editor/test/browser/editorResolverService.test.ts (+83) — regression test using a controllable IExtensionService whose whenInstalledExtensionsRegistered only resolves after a custom editor registers mid-await.

Test plan

When opening a file via CLI before any VS Code instance is running and
the user has a `workbench.editorAssociations` mapping pointing to a
custom editor (e.g. `"*.md": "vscode.markdown.preview.editor"`), the
file would open in the default text editor instead of the custom one.

`EditorResolverService.resolveEditor` snapshots the flattened editor
map at the top of the call, then awaits `whenInstalledExtensionsRegistered`
when the user has a non-default editor association for the resource.
During that await, extensions register their custom editor contributions
through the `customEditors` extension point, which mutates `_editors`
and sets `_shouldReFlattenEditors`. Because the snapshot was taken
before the await, subsequent lookups (`getEditor`, `findMatchingEditors`,
`getAssociationsForResource`) iterated a stale map without the
freshly-registered custom editor, so the resolver fell back to the
default text editor.

Re-flatten after the await so the new contributions are visible. Adds
a regression test that registers a custom editor while the
`whenInstalledExtensionsRegistered` promise is still pending and
verifies the user-configured association is honored.

Fixes microsoft#197374
@tzipora-rose

Copy link
Copy Markdown

This line is needed, but on a cold start it isn't enough on its own: CustomEditorService is registered Delayed, so the custom editors may not be registered with the resolver yet when the startup files are resolved (in my starts they weren't), and re-reading the map then finds nothing new. The details, and a second fix we found, are in my comment on #244597; with both, a cold start from File Explorer opens the associated custom editor in my install of VSCodium 1.135. Line 161 on main now passes editorAssociationType to resourceMatchesUserAssociation, so this may need a rebase. Could it be marked ready for review?

Investigated and written in collaboration with Claudia in Claude Code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown preview is not automatically opened when md file is opened via command-line and no VS code instance was already running

3 participants