Skip to content

fix: also respect compilerOptions.moduleResolution for ending preferences - #53244

Closed
Josh Ghoulberg 👻 (JoshuaKGoldberg) wants to merge 4 commits into
microsoft:mainfrom
JoshuaKGoldberg:tsconfig-informed-module-specifier-endings
Closed

Josh Ghoulberg 👻 (JoshuaKGoldberg) wants to merge 4 commits into
microsoft:mainfrom
JoshuaKGoldberg:tsconfig-informed-module-specifier-endings

Conversation

@JoshuaKGoldberg

Copy link
Copy Markdown
Contributor

The root issue was that TypeScript only looks for user preferences or a package.json "type": "module" when determining whether to add .js to extensions. Some projects don't have either of those set, but still use "compilerOptions": "node16" or similar in their TSConfig.

Fixes #52886

@typescript-bot TypeScript Bot (typescript-bot) added the For Backlog Bug PRs that fix a backlog bug label Mar 14, 2023
@JoshuaKGoldberg
Josh Ghoulberg 👻 (JoshuaKGoldberg) marked this pull request as ready for review March 14, 2023 15:21
@JoshuaKGoldberg

Josh Ghoulberg 👻 (JoshuaKGoldberg) commented Mar 14, 2023 •

Copy link
Copy Markdown
Contributor Author

Lint failures will be fixed by #53243.

@Andarist

Copy link
Copy Markdown
Contributor

Josh Ghoulberg 👻 (@JoshuaKGoldberg) that PR landed shortly after you opened this one, you can sync with main to resolve the issue :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

--moduleResolution nodenext doesn’t mean that extensions are required; it just means that extensions are required in ESM files. What you have in your test are CJS files, where extensions are not required. The refactor should infer from the existing code that extensions are preferred, but it’s incorrect to infer that preference purely based on the module resolution setting. I’m not sure what the issue is in #53244, but most likely the refactor isn’t passing the resolutionMode correctly.

@JoshuaKGoldberg

Josh Ghoulberg 👻 (JoshuaKGoldberg) commented Mar 28, 2023 •

Copy link
Copy Markdown
Contributor Author

TIL, thanks for the explanation!

The refactor should infer from the existing code that extensions are preferred

Interesting - looks from 3505fb9 that TypeScript already is.

most likely the refactor isn’t passing the resolutionMode correctly

Hmm, looking through the SourceFile type it looks like impliedNodeFormat?: ResolutionMode is parsed & set separately from the refactor. So maybe the issue is no longer valid, and since fixed? I can't reproduce bad behavior in the test cases...

infer that preference purely based on the module resolution setting

Edit: actually, I'm confused - why is this not the case? I thought with moduleResolution: "bundler", it would be expected that Node16/Nodenext compiler options are expected to be .js?

@andrewbranch

Copy link
Copy Markdown
Member

Looks like the bug was a duplicate of #50710 and already fixed by #51702.

Edit: actually, I'm confused - why is this not the case? I thought with moduleResolution: "bundler", it would be expected that Node16/Nodenext compiler options are expected to be .js?

I’m confused about what you’re confused about 😆 bundler didn’t change anything about node16/nodenext. Module resolution strategies that support extensionless paths include:

  • Node require
  • Most bundlers and non-Node runtimes (even in import)

In these scenarios, you’re allowed to have an aesthetic preference as to whether you include file extensions on your relative module specifiers, since they work either way. This is what the VS Code preference controls (and what we’ll try to infer from existing usage).

In Node ESM imports, you’re not allowed to omit extensions, so the preference doesn’t come into play. You just have to have extensions, no matter what the VS Code preference says, and if auto-imports or whatever fails to add it, that’s a bug.

Does that clear things up?

@JoshuaKGoldberg

Copy link
Copy Markdown
Contributor Author

Got it, thanks - that's very helpful!

@JoshuaKGoldberg
Josh Ghoulberg 👻 (JoshuaKGoldberg) deleted the tsconfig-informed-module-specifier-endings branch March 28, 2023 22:37
@microsoft 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

For Backlog Bug PRs that fix a backlog bug

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Refactor "Move to a new file" does not honor explicit file extensions

6 participants