-
Notifications
You must be signed in to change notification settings - Fork 30k
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
esm: resolve main from package.json when importing from directory #32612
Conversation
test/fixtures/es-module-specifiers/package-with-exports/exports_main.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
@guybedford I think this is meant to specifically fix a bug in the |
There was a problem hiding this 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; | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- If X begins with './' or '/' or '../'
a. LOAD_AS_FILE(Y + X)
b. LOAD_AS_DIRECTORY(Y + X)`
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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:
import 'pkg/x'
should load/path/to/node_modules/pkg/x/pjson-main.js
with package.json main checksimport 'pkg/x'
wherepkg/package.json
contains{ "exports": { "./x": "./z" }
should load/path/to/node_modules/pkg/z/pjson-main.js
8ae28ff
to
2935f72
Compare
Superseded by #38979. |
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesFixes #32103
Ref: My issue comment