Skip to content

Conversation

@andrewbranch
Copy link
Member

Fixes #52167

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Apr 6, 2023
Comment on lines -150 to +152
return [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension];
return preferredEnding === ModuleSpecifierEnding.JsExtension
? [ModuleSpecifierEnding.JsExtension, ModuleSpecifierEnding.Index]
: [ModuleSpecifierEnding.Index, ModuleSpecifierEnding.JsExtension];
Copy link
Member Author

@andrewbranch andrewbranch Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change alone fixes the issue reported by @jespertheend in #52167, which was different from the OP’s issue. When --moduleResolution is classic (the unfortunate default of --module esnext), we simply weren’t respecting the inferred preference. classic resolution doesn’t support dropping /index.js suffixes, which is why it gets its own if block here that never returns ModuleSpecifierEnding.Minimal.

Comment on lines -9159 to +9163
return firstDefined(imports, ({ text }) => pathIsRelative(text) ? hasExtension(text) : undefined) || false;
return firstDefined(imports, ({ text }) => pathIsRelative(text) && !fileExtensionIsOneOf(text, extensionsNotSupportingExtensionlessResolution)
? hasExtension(text)
: undefined) || false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change, and the similar one below, fixes the main issue of #52167. This code is used to detect a preference for module specifier endings from existing imports in a file. Previously, any relative-path module specifier that included an extension was taken as an indication that the user prefers including extensions. However, only .js/.ts extensions can ever be omitted—if an extension included .mjs or .cjs, for example, that cannot indicate a preference of the user because that file extension was required to make the import resolve. So we need to skip those if we’re looking for clues as to what the user prefers; we can only infer a preference if the user had a choice.

If you look at the (video) repro in #52167, one of the existing imports in the big file had a .mjs extension, which caused a new import added in that file to include an extension. When the user tried the same auto-import in an empty file, the import excluded the optional extension, because that’s the default in absence of any evidence that the user wants extensions.

@andrewbranch andrewbranch merged commit 287ee41 into microsoft:main Apr 6, 2023
@andrewbranch andrewbranch deleted the bug/52167 branch April 6, 2023 20:07
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typescript file imports being appended with .js extensions intermittently

4 participants