-
-
Notifications
You must be signed in to change notification settings - Fork 495
Fix: respect VS Code languageId over inferredParser #3797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix: respect VS Code languageId over inferredParser #3797
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes an issue where the Prettier VS Code extension was incorrectly prioritizing the file extension-based parser (inferredParser) over the user's explicitly selected language mode in VS Code. The fix ensures that when a user manually selects a language in VS Code, that choice is respected first, with inferredParser serving as a fallback only when needed.
Key changes:
- Inverts parser selection priority to check
languageIdfirst (unless it's "plaintext"), then fall back toinferredParser - Adds improved warning messages to help users understand which parser resolution strategy is being used
- Maintains backward compatibility for plaintext language mode and fallback scenarios
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
| } else if (fileInfo && fileInfo.inferredParser) { | ||
| this.loggingService.logWarning( | ||
| `VS Code language not selected, trying inferredParser.` |
Copilot
AI
Nov 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The warning message "VS Code language not selected" is misleading. At this point, we know languageId === "plaintext", which means VS Code has selected a language (plaintext), not that no language was selected. Consider updating the message to something like "languageId is plaintext, using inferredParser" to better reflect the actual condition.
| `VS Code language not selected, trying inferredParser.` | |
| `languageId is plaintext, using inferredParser=${fileInfo.inferredParser}.` |
| `Parser not inferred, trying VS Code language.` | ||
| ); | ||
| const { languages } = await prettierInstance.getSupportInfo({ | ||
| plugins: [], |
Copilot
AI
Nov 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The getSupportInfo call uses an empty plugins array, but resolvedConfig?.plugins is available at this point and could contain custom plugins that add support for additional languages. This means custom plugins won't be considered when resolving the parser from languageId, potentially causing parser resolution to fail unnecessarily. Consider passing resolvedConfig?.plugins?.filter((item): item is string => typeof item === "string") || [] instead of an empty array, similar to how plugins are passed to getFileInfo at line 459.
| plugins: [], | |
| plugins: | |
| resolvedConfig?.plugins?.filter( | |
| (item): item is string => typeof item === "string" | |
| ) || [], |
| if (languageId !== "plaintext") { | ||
| // Don't attempt VS Code language for plaintext because we never have | ||
| // a formatter for plaintext and most likely the reason for this is | ||
| // somebody has registered a custom file extension without properly | ||
| // configuring the parser in their prettier config. | ||
| this.loggingService.logWarning( | ||
| `Parser not inferred, trying VS Code language.` | ||
| ); | ||
| const { languages } = await prettierInstance.getSupportInfo({ | ||
| plugins: [], | ||
| }); | ||
| parser = getParserFromLanguageId(languages, uri, languageId); | ||
|
|
||
| if (!parser && fileInfo?.inferredParser) { | ||
| this.loggingService.logWarning( | ||
| `No parser found for languageId=${languageId}, using inferredParser=${fileInfo.inferredParser}.` | ||
| ); | ||
| parser = fileInfo.inferredParser; | ||
| } | ||
| } else if (fileInfo && fileInfo.inferredParser) { | ||
| this.loggingService.logWarning( | ||
| `VS Code language not selected, trying inferredParser.` | ||
| ); | ||
| parser = fileInfo.inferredParser; | ||
| } |
Copilot
AI
Nov 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces a significant behavioral modification to parser selection logic, prioritizing languageId over inferredParser. Consider adding test coverage for scenarios such as:
- User manually selects a language that differs from the file extension
languageIdis plaintext and falls back toinferredParserlanguageIdis set but no parser is found, then falls back toinferredParser
This would help prevent regressions and document the expected behavior.
Summary
Fixes an issue where Prettier-VSCode incorrectly used the inferred parser
(based on file extension) even after the user manually selected a different
language in VS Code.
Details
languageId !== "plaintext", use the VS Code language.inferredParseronly when the language isplaintextor not supported.