-
-
Notifications
You must be signed in to change notification settings - Fork 501
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -472,20 +472,27 @@ export default class PrettierEditService implements Disposable { | |||||
| } | ||||||
|
|
||||||
| let parser: PrettierBuiltInParserName | string | undefined; | ||||||
| if (fileInfo && fileInfo.inferredParser) { | ||||||
| parser = fileInfo.inferredParser; | ||||||
| } else if (languageId !== "plaintext") { | ||||||
| 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.` | ||||||
|
||||||
| `VS Code language not selected, trying inferredParser.` | |
| `languageId is plaintext, using inferredParser=${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.
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
getSupportInfocall uses an empty plugins array, butresolvedConfig?.pluginsis 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 fromlanguageId, potentially causing parser resolution to fail unnecessarily. Consider passingresolvedConfig?.plugins?.filter((item): item is string => typeof item === "string") || []instead of an empty array, similar to how plugins are passed togetFileInfoat line 459.