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

module: custom --conditions flag option #34637

Closed
wants to merge 10 commits into from

Conversation

guybedford
Copy link
Contributor

@guybedford guybedford commented Aug 5, 2020

This implements a node --conditions=custom flag for supporting custom resolution conditions in conditional exports, as previously discussed in nodejs/modules#537. A short alias variant, -u=custom is also provided, which stands for "user conditions".

Any custom condition names can be provided as repeated flags, but the core node, default, require and import conditions cannot be changed.

In the process a couple of conditional resolution edge ase bugs are fixed as well here.

@nodejs/modules-active-members

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

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. esm Issues and PRs related to the ECMAScript Modules implementation. labels Aug 5, 2020
src/node_options.cc Outdated Show resolved Hide resolved
src/node_options.cc Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Show resolved Hide resolved
doc/api/esm.md Outdated Show resolved Hide resolved
@bmeck
Copy link
Member

bmeck commented Aug 5, 2020

we likely should ban known contextual conditions import and require from coming from the CLI

doc/api/cli.md Outdated Show resolved Hide resolved
doc/api/esm.md Outdated Show resolved Hide resolved
doc/api/esm.md Outdated Show resolved Hide resolved
lib/internal/modules/cjs/loader.js Outdated Show resolved Hide resolved
lib/internal/modules/esm/resolve.js Outdated Show resolved Hide resolved
Co-authored-by: Jordan Harband <ljharb@gmail.com>
Co-authored-by: Jan Olaf Krems <jan.krems@gmail.com>
doc/api/cli.md Outdated Show resolved Hide resolved
@guybedford
Copy link
Contributor Author

we likely should ban known contextual conditions import and require from coming from the CLI

I was thinking about this case and the worst case seems to be having CJS resolve ESM or ESM resolve the CJS path depending on how the ordering works out.

Neither of these seem that bad to me though if users want to do it?

@bmeck
Copy link
Member

bmeck commented Aug 5, 2020

@guybedford I agree it isn't fatal in any way if users want to set those for w/e reason, i just have concerns since we have statements in the docs about being exclusive being invalidated.

@guybedford
Copy link
Contributor Author

@bmeck yes worth thinking about, although the main property behind the exclusive definition was that one or the other will always be matched (so there are no fallbacks when both are set in any environment), but that property is retained.

@ljharb
Copy link
Member

ljharb commented Aug 6, 2020

Given that ESM and CJS are ambiguous, it seems like a very not good idea to allow accidental mis-parsing.

@bmeck
Copy link
Member

bmeck commented Aug 6, 2020

Given that ESM and CJS are ambiguous, it seems like a very not good idea to allow accidental mis-parsing.

The conditions do not affect the static format of a file and even if you set the require condition and target a .mjs file it is still ESM and fails and vice versa if you set the import condition and target a .cjs file it is still seen as CJS.

@ljharb
Copy link
Member

ljharb commented Aug 6, 2020

@bmeck so to confirm, you're saying even if i have a file that contains nothing but console.log('parsed'), there's no way (as a CJS file extension) it can be parsed as a Module, and no way (as an ESM file extension) it can be parsed as a Script?

@bmeck
Copy link
Member

bmeck commented Aug 6, 2020

@ljharb not via this PR, I might be able to fiddle with the old CJS loader to get it to parse wrong with effort; but to my knowledge it is never possible with the ESM loader.

@GeoffreyBooth
Copy link
Member

I might be able to fiddle with the old CJS loader to get it to parse wrong with effort

I'm pretty sure this should be impossible, at least for accidental cases, since we made require of .mjs throw. It should be likewise for require of .js under "type": "module". I'm sure there are hacky ways to circumvent our check like data URIs or extensionless files or something, but for users who aren't trying to trick Node we should already be erroring.

@bmeck
Copy link
Member

bmeck commented Aug 6, 2020

@GeoffreyBooth agreed

@nodejs-github-bot
Copy link
Collaborator

guybedford added a commit that referenced this pull request Aug 11, 2020
PR-URL: #34637
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
@guybedford
Copy link
Contributor Author

Landed in 77a515c.

@guybedford guybedford closed this Aug 11, 2020
MylesBorins pushed a commit that referenced this pull request Aug 17, 2020
PR-URL: #34637
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
@danielleadams danielleadams mentioned this pull request Aug 20, 2020
BethGriggs pushed a commit that referenced this pull request Aug 20, 2020
PR-URL: #34637
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
richardlau added a commit to richardlau/node-1 that referenced this pull request Aug 26, 2020
Old versions of mocha break after nodejs#34637.

This was a bug in mocha, but since this is a widely used module
we can expect ecosystem breakage until modules are updated to
the latest version of mocha. Drop the conflicting `-u` alias --
we can potentially bring it back once modules have been updated.
MylesBorins pushed a commit that referenced this pull request Aug 26, 2020
Old versions of mocha break after #34637.

This was a bug in mocha, but since this is a widely used module
we can expect ecosystem breakage until modules are updated to
the latest version of mocha. Drop the conflicting `-u` alias --
we can potentially bring it back once modules have been updated.

PR-URL: #34935
Refs: mochajs/mocha#4417
Refs: #34637
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Shelley Vohr <codebytere@gmail.com>
MylesBorins pushed a commit that referenced this pull request Aug 26, 2020
Old versions of mocha break after #34637.

This was a bug in mocha, but since this is a widely used module
we can expect ecosystem breakage until modules are updated to
the latest version of mocha. Drop the conflicting `-u` alias --
we can potentially bring it back once modules have been updated.

PR-URL: #34935
Refs: mochajs/mocha#4417
Refs: #34637
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Shelley Vohr <codebytere@gmail.com>
@addaleax
Copy link
Member

If this is backported to v12.x, it should come together with #34935

@guybedford
Copy link
Contributor Author

guybedford commented Sep 27, 2020

Thanks, this likely should be backported, if not for any other reason than to simplify the backporting of subsequent modules PRs.

guybedford added a commit to guybedford/node that referenced this pull request Sep 28, 2020
PR-URL: nodejs#34637
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
guybedford pushed a commit to guybedford/node that referenced this pull request Sep 28, 2020
Old versions of mocha break after nodejs#34637.

This was a bug in mocha, but since this is a widely used module
we can expect ecosystem breakage until modules are updated to
the latest version of mocha. Drop the conflicting `-u` alias --
we can potentially bring it back once modules have been updated.

PR-URL: nodejs#34935
Refs: mochajs/mocha#4417
Refs: nodejs#34637
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Shelley Vohr <codebytere@gmail.com>
@guybedford
Copy link
Contributor Author

This and #34935 are included in the backport PR at #35385.

codebytere pushed a commit that referenced this pull request Oct 1, 2020
PR-URL: #34637
Backport-PR-URL: #35385
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
codebytere pushed a commit that referenced this pull request Oct 1, 2020
Old versions of mocha break after #34637.

This was a bug in mocha, but since this is a widely used module
we can expect ecosystem breakage until modules are updated to
the latest version of mocha. Drop the conflicting `-u` alias --
we can potentially bring it back once modules have been updated.

PR-URL: #34935
Backport-PR-URL: #35385
Refs: mochajs/mocha#4417
Refs: #34637
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Shelley Vohr <codebytere@gmail.com>
@codebytere codebytere mentioned this pull request Oct 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants