Skip to content

fix: send user preferences to TS server even without visible editor#304987

Merged
mjbvz merged 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/ts-source-add-imports-preferences
Mar 26, 2026
Merged

fix: send user preferences to TS server even without visible editor#304987
mjbvz merged 1 commit intomicrosoft:mainfrom
yogeshwaran-c:fix/ts-source-add-imports-preferences

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

When source.addMissingImports code action is triggered, it calls ensureConfigurationForDocument to send the user's preferences to the TypeScript server. However, this method calls getFormattingOptions(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 returns undefined and ensureConfigurationForDocument returns early without sending any configuration to the TS server.

This means user preferences like preferTypeOnlyAutoImports are never communicated to the TypeScript server, causing it to use its default behavior instead. Users report that source.addMissingImports always adds type keyword to imports regardless of their preferTypeOnlyAutoImports: false setting, 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 (including preferTypeOnlyAutoImports, 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:

// Before
const formattingOptions = this.getFormattingOptions(document);
if (formattingOptions) {
    return this.ensureConfigurationOptions(document, formattingOptions, token);
}

// After
const formattingOptions = this.getFormattingOptions(document)
    ?? { tabSize: undefined, insertSpaces: undefined };
return this.ensureConfigurationOptions(document, formattingOptions, token);

The tabSize: undefined and insertSpaces: undefined values are already valid for the FormattingOptions interface and the TS server's configure command handles them gracefully by using its own defaults.

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
@vs-code-engineering vs-code-engineering bot added this to the 1.114.0 milestone Mar 26, 2026
@mjbvz mjbvz enabled auto-merge March 26, 2026 04:52
@mjbvz mjbvz merged commit abdf722 into microsoft:main Mar 26, 2026
27 of 29 checks passed
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.

The typescript source.addMissingImports code action always using type keyword and doesn't respect the settings: preferTypeOnlyAutoImport

3 participants