Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix index fallback of CJS package from ESM-mode import when main is present but does not resolve #49327

Merged
merged 1 commit into from
May 31, 2022

Conversation

andrewbranch
Copy link
Member

Fixes #49326

To check that this is consistent with Node’s behavior, I tried these package.json modifications in @types/dedent and added an index.js of module.exports = "index.js" and ran

node --input-type=module -e 'import dedent from "@types/dedent"; console.log(dedent)'
node -e 'console.log(require("@types/dedent"))'

and ensured both log index.js. If they did, I assumed that means we should be able to resolve to index.d.ts.

@andrewbranch andrewbranch requested a review from weswigham May 31, 2022 17:27
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label May 31, 2022
@andrewbranch
Copy link
Member Author

If @weswigham agrees this is a bug / this or something close to it is a good fix, we should consider it for a 4.7 patch as there could be a bunch of old DT packages like this that are broken with nodenext.

@andrewbranch andrewbranch added this to the TypeScript 4.7.3 milestone May 31, 2022
&& packageInfo.packageJsonContent.exports === undefined
&& packageInfo.packageJsonContent.main === undefined
// eslint-disable-next-line no-null/no-null
&& (packageInfo.packageJsonContent.exports === undefined || packageInfo.packageJsonContent.exports === null)
Copy link
Member

Choose a reason for hiding this comment

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

Double checking on https://nodejs.org/api/esm.html#resolution-algorithm, recognizing both null and undefined here for exports seems good (only one usage makes that explicit, but it should be fine), however ignoring main and looking up an index regardless is not what PACKAGE_RESOLVE describes for an exportless package... it just says Return the URL resolution of packageSubpath in packageURL., which doesn't imply any fallback to index.js lookup if the main fails. If in practice it is, the algorithm in the node docs may be wrong. (Which doesn't surprise me, if I'm honest, I think the people writing the code are more pragmatic than the people reviewing/writing the docs.)

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, there's a large legacy main resolution code block which isn't included in the documented algorithm.

Copy link
Member

Choose a reason for hiding this comment

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

I've opened nodejs/node#43269 for the docs issue on node.

@andrewbranch
Copy link
Member Author

@typescript-bot cherry-pick this to release-4.7

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 31, 2022

Heya @andrewbranch, I've started to run the task to cherry-pick this into release-4.7 on this PR at 79957c1. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

Hey @andrewbranch, I've opened #49329 for you.

typescript-bot pushed a commit to typescript-bot/TypeScript that referenced this pull request May 31, 2022
Component commits:
79957c1 Fix index fallback of CJS package from ESM-mode import when `main` is present but does not resolve
andrewbranch added a commit that referenced this pull request Jun 1, 2022
Component commits:
79957c1 Fix index fallback of CJS package from ESM-mode import when `main` is present but does not resolve

Co-authored-by: Andrew Branch <andrew@wheream.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESM-mode default import does not resolve to old DT packages with no "types" field
3 participants