-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.`; | ||
|
||
/* 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. */ | ||
|
@@ -73,7 +78,14 @@ class Loader { | |
async import(specifier, parent) { | ||
const job = await this.getModuleJob(specifier, parent); | ||
const module = await job.run(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
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 }) { | ||
|
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'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export function then() {} |
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'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.
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 paragraph of the linked article explains it pretty well, do you feel strongly about adding more detail here?
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.
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.
.