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

loader: warn when the user imports a module with a thenable namespace #20951

Closed
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ const defaultResolve = require('internal/modules/esm/default_resolve');
const createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
const translators = require('internal/modules/esm/translators');
const { SafeWeakSet } = require('internal/safe_globals');

const FunctionBind = Function.call.bind(Function.prototype.bind);

const debug = require('util').debuglog('esm');

const thenableNamespaces = new SafeWeakSet();
const thenableWarning = (x) => `The namespace of ${x} is thenable. \
See https://mdn.io/thenable for more information.`;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure how obvious this is to users - perhaps The module ${x} cannot be resolved through a dynamic import because it has a "then" method.

Copy link
Member Author

Choose a reason for hiding this comment

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

the first paragraph of the linked article explains it pretty well, do you feel strongly about adding more detail here?

Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably be updated now to reflect the new warning status - The namespace of ${x} exports an invalid thenable, resulting in top-level imports of this module never resolving..


/* A Loader instance is used as the main entry point for loading ES modules.
* Currently, this is a singleton -- there is only one used for loading
* the main module and everything in its dependency graph. */
Expand Down Expand Up @@ -73,7 +78,14 @@ class Loader {
async import(specifier, parent) {
const job = await this.getModuleJob(specifier, parent);
const module = await job.run();
Copy link
Contributor

Choose a reason for hiding this comment

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

Job.run is only called for top-level imports, so if you provide the warning in the implementation of job.run you shouldn't need to separately cache the errors with a weak map.

Copy link
Member Author

Choose a reason for hiding this comment

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

i put it here to catch the entry file and dynamic imports, do you think i should go for all imports?

Copy link
Contributor

Choose a reason for hiding this comment

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

@devsnek I don't think we should do all imports, but as far as I'm aware, job.run will be called iff it is an entry file or dynamic import?

return module.namespace();
const m = module.namespace();
if (typeof m.then === 'function' && m.then.length === 0 &&
!thenableNamespaces.has(m) &&
(!parent || !/node_modules/.test(parent))) {
process.emitWarning(thenableWarning(module.url), 'loader');
thenableNamespaces.add(m);
}
return m;
}

hook({ resolve, dynamicInstantiate }) {
Expand Down
1 change: 1 addition & 0 deletions lib/internal/safe_globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ const makeSafe = (unsafe, safe) => {

exports.SafeMap = makeSafe(Map, class SafeMap extends Map {});
exports.SafeSet = makeSafe(Set, class SafeSet extends Set {});
exports.SafeWeakSet = makeSafe(WeakSet, class SafeWeakSet extends WeakSet {});
exports.SafePromise = makeSafe(Promise, class SafePromise extends Promise {});
15 changes: 15 additions & 0 deletions test/es-module/test-thenable-warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

// Flags: --experimental-modules

const common = require('../common');
const assert = require('assert');

common.crashOnUnhandledRejection();

const re = /^The namespace of .+? is thenable\. See https:\/\/mdn\.io\/thenable for more information\.$/;
process.on('warning', common.mustCall((warning) => {
assert(re.test(warning.message));
}));

import('../fixtures/es-modules/thenable.mjs');
1 change: 1 addition & 0 deletions test/fixtures/es-modules/thenable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function then() {}