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

esm: resolve main from package.json when importing from directory #32612

Conversation

lingsamuel
Copy link
Contributor

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

Fixes #32103
Ref: My issue comment

@nodejs-github-bot nodejs-github-bot added the esm Issues and PRs related to the ECMAScript Modules implementation. label Apr 2, 2020
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

The Node.js modules documentation tries to make it clear that explicit relative paths are always required. See https://nodejs.org/dist/latest-v13.x/docs/api/esm.html#esm_differences_between_es_modules_and_commonjs.

If you can suggest improvements to these docs to avoid any misunderstandings that would be very welcome.

Alternatively you can use the --es-module-specifier-resolution=node flag here.

@MylesBorins
Copy link
Contributor

@guybedford I think this is meant to specifically fix a bug in the --experimental-specifier-resolution flag

Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

@lingsamuel my sincere apologies for misreading this PR - the change is definitely a bug fix for the flagged support.

My main review point further is that this check should be combined into the finalizeResolution code path as a unified LOAD_AS_DIRECTORY routine, as per https://nodejs.org/dist/latest-v12.x/docs/api/modules.html#modules_all_together.

return packageMainResolve(packageJSONUrl, packageConfig, base);
}
return resolved;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should probably combined into the finalizeResolution function above rather to maintain full equivalence with the LOAD_AS_FILE | LOAD_AS_DIRECTORY checks per the module resolution spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first version I wrote puts this part in finalizeResolution function indeed. But I think finalizeResolution is LOAD_AS_FILE routine, it is called by many other resolve functions as final return call and shouldn't add any resolve functionality. So I move this code pieces after the relative path check to fit this rule:

  1. If X begins with './' or '/' or '../'
    a. LOAD_AS_FILE(Y + X)
    b. LOAD_AS_DIRECTORY(Y + X)`

Copy link
Contributor

Choose a reason for hiding this comment

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

It is ok for finalizeResolution to include the LOAD_AS_FILE | LOAD_AS_DIRECTORY path - the reason for this is that all resolutions eventually end in this check in CommonJS.

Copy link
Contributor Author

@lingsamuel lingsamuel Apr 13, 2020

Choose a reason for hiding this comment

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

Sorry for my late reply.
I don't think it's a good idea...
finalizeResolution is pretty simple now, and keep it simple is better to my mind. Merge package resolve logic to it makes it a bit dirty, the call path does not straight anymore.
But if more people think merge logic is better, I will change it...

Copy link
Contributor

Choose a reason for hiding this comment

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

@lingsamuel let me restate my feedback then. This PR only fixes one case, when there are two other test cases not being covered by this PR that you should make pass as well as part of this fix:

  1. import 'pkg/x' should load /path/to/node_modules/pkg/x/pjson-main.js with package.json main checks
  2. import 'pkg/x' where pkg/package.json contains { "exports": { "./x": "./z" } should load /path/to/node_modules/pkg/z/pjson-main.js

@aduh95
Copy link
Contributor

aduh95 commented Jun 13, 2021

Superseded by #38979.

@aduh95 aduh95 closed this Jun 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Cannot find module" when main file not index.js with experimental-specifier-resolution=node
6 participants