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: fix specifier resolution algorithm #30574

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function shouldUseESMLoader(mainPath) {
const userLoader = getOptionValue('--experimental-loader');
if (userLoader)
return true;
const esModuleSpecifierResolution =
getOptionValue('--es-module-specifier-resolution');
Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect. One thing to note is that this flag is experimental. I just posted an issue about this here - #30600.

Since this is now the primary entry for this flag, we should probably move this check up to the top of the function, and also include a warning like:

process.emitWarning(
    'The --es-module-specifier-resolution flag is experimental.',
    'ExperimentalWarning', undefined);

Would help to add this stuff while we're working on this area anyway, but if you'd rather it be done separately that is fine too just thought I'd mention as it's important for users not to overlook that any of these flags might be removed in future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the explanation. I've noticed that #30600 is not only about specifier-resolution but also related to other experimental flags, so perhaps the fix about it should be raised in another PR rather than merged to this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I don't understand it wrong, I'll try to work on #30600 and create another PR to fix it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure if you're able to work on that it would be great, just mentioning as otherwise it can be useful to do these things while we're there.

if (esModuleSpecifierResolution === 'node')
return true;
// Determine the module format of the main
if (mainPath && mainPath.endsWith('.mjs'))
return true;
Expand Down
22 changes: 22 additions & 0 deletions test/es-module/test-esm-specifiers.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Flags: --es-module-specifier-resolution=node
import { mustNotCall } from '../common/index.mjs';
import assert from 'assert';
import path from 'path';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';

// commonJS index.js
import commonjs from '../fixtures/es-module-specifiers/package-type-commonjs';
Expand Down Expand Up @@ -33,3 +36,22 @@ async function main() {
}

main().catch(mustNotCall);

// Test path from command line arguments
[
'package-type-commonjs',
'package-type-module',
'/',
'/index',
].forEach((item) => {
const modulePath = path.join(
fileURLToPath(import.meta.url),
'../../fixtures/es-module-specifiers',
item,
);
spawn(process.execPath,
['--es-module-specifier-resolution=node', modulePath],
{ stdio: 'inherit' }).on('exit', (code) => {
assert.strictEqual(code, 0);
});
});