Skip to content

Conversation

@SURTBLAZE
Copy link

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

  • If languageId !== "plaintext", use the VS Code language.
  • Fallback to inferredParser only when the language is plaintext or not supported.

@auto-assign auto-assign bot requested a review from ntotten October 6, 2025 14:49
@ntotten ntotten requested a review from Copilot November 27, 2025 14:09
Copilot finished reviewing on behalf of ntotten November 27, 2025 14:12
Copy link
Contributor

Copilot AI left a 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 languageId first (unless it's "plaintext"), then fall back to inferredParser
  • 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.`
Copy link

Copilot AI Nov 27, 2025

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.

Suggested change
`VS Code language not selected, trying inferredParser.`
`languageId is plaintext, using inferredParser=${fileInfo.inferredParser}.`

Copilot uses AI. Check for mistakes.
`Parser not inferred, trying VS Code language.`
);
const { languages } = await prettierInstance.getSupportInfo({
plugins: [],
Copy link

Copilot AI Nov 27, 2025

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.

Suggested change
plugins: [],
plugins:
resolvedConfig?.plugins?.filter(
(item): item is string => typeof item === "string"
) || [],

Copilot uses AI. Check for mistakes.
Comment on lines +475 to 496
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;
}
Copy link

Copilot AI Nov 27, 2025

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:

  1. User manually selects a language that differs from the file extension
  2. languageId is plaintext and falls back to inferredParser
  3. languageId is set but no parser is found, then falls back to inferredParser

This would help prevent regressions and document the expected behavior.

Copilot uses AI. Check for mistakes.
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.

2 participants