fix: send user preferences to TS server even without visible editor#304987
Merged
mjbvz merged 1 commit intomicrosoft:mainfrom Mar 26, 2026
Merged
Conversation
When ensureConfigurationForDocument is called and no visible text editor is found for the document, getFormattingOptions returns undefined and the method returns early without sending any configuration including user preferences like preferTypeOnlyAutoImports to the TS server. This causes source.addMissingImports to ignore the user's preferTypeOnlyAutoImports setting. Fix by falling back to undefined formatting options when no visible editor is found, ensuring user preferences are always sent. Closes microsoft#272479
mjbvz
approved these changes
Mar 26, 2026
bpasero
approved these changes
Mar 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix
What is the current behavior?
When
source.addMissingImportscode action is triggered, it callsensureConfigurationForDocumentto send the user's preferences to the TypeScript server. However, this method callsgetFormattingOptions(document)which looks for a visible text editor matching the document. If no visible editor is found (which can happen when the code action is triggered via keybinding or command palette), the method returnsundefinedandensureConfigurationForDocumentreturns early without sending any configuration to the TS server.This means user preferences like
preferTypeOnlyAutoImportsare never communicated to the TypeScript server, causing it to use its default behavior instead. Users report thatsource.addMissingImportsalways addstypekeyword to imports regardless of theirpreferTypeOnlyAutoImports: falsesetting, while the quick fix lightbulb (which goes through a different code path) works correctly.Closes #272479
What is the new behavior?
When no visible editor is found for the document, the method now falls back to
{ tabSize: undefined, insertSpaces: undefined }instead of returning early. This means the formatting options will be unset (letting the TS server use its own defaults for tab size and spaces), but critically, all user preferences (includingpreferTypeOnlyAutoImports,quotePreference,importModuleSpecifierPreference, etc.) will still be sent to the TS server.Additional context
The fix is minimal: replace the conditional guard with a nullish coalescing fallback:
The
tabSize: undefinedandinsertSpaces: undefinedvalues are already valid for theFormattingOptionsinterface and the TS server'sconfigurecommand handles them gracefully by using its own defaults.