module: throw error when re-runing errored module jobs

Re-evaluating an errored ESM should lead to rejecting
the rejection again - this is also the case when importing
it twice. In the case of retrying with
require after import, just throw the cached error.

Drive-by: add some debug logs.
PR-URL: https://github.com/nodejs/node/pull/58957
Fixes: https://github.com/nodejs/node/issues/58945
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Joyee Cheung authored and Michaël Zasso committed 2025-07-17 16:57:10 +02:00
1 parent c9ded6ba15
commit 515b581d47
4 files changed
+45 -2

No files matched your search

+4
View File
@@ -43,6 +43,7 @@ const {
kEvaluating,
kEvaluationPhase,
kInstantiated,
kErrored,
kSourcePhase,
throwIfPromiseRejected,
} = internalBinding('module_wrap');
@@ -402,6 +403,9 @@ class ModuleLoader {
mod[kRequiredModuleSymbol] = job.module;
const { namespace } = job.runSync(parent);
return { wrap: job.module, namespace: namespace || job.module.getNamespace() };
} else if (status === kErrored) {
// If the module was previously imported and errored, throw the error.
throw job.module.getError();
}
// When the cached async job have already encountered a linking
// error that gets wrapped into a rejection, but is still later
+8 -2
View File
@@ -323,7 +323,7 @@ class ModuleJob extends ModuleJobBase {
assert(this.module instanceof ModuleWrap);
let status = this.module.getStatus();
debug('ModuleJob.runSync', this.module);
debug('ModuleJob.runSync()', status, this.module);
// FIXME(joyeecheung): this cannot fully handle < kInstantiated. Make the linking
// fully synchronous instead.
if (status === kUninstantiated) {
@@ -358,6 +358,7 @@ class ModuleJob extends ModuleJobBase {
}
async run(isEntryPoint = false) {
debug('ModuleJob.run()', this.module);
assert(this.phase === kEvaluationPhase);
await this.#instantiate();
if (isEntryPoint) {
@@ -461,7 +462,11 @@ class ModuleJobSync extends ModuleJobBase {
assert(this.phase === kEvaluationPhase);
// This path is hit by a require'd module that is imported again.
const status = this.module.getStatus();
if (status > kInstantiated) {
debug('ModuleJobSync.run()', status, this.module);
// If the module was previously required and errored, reject from import() again.
if (status === kErrored) {
throw this.module.getError();
} else if (status > kInstantiated) {
if (this.evaluationPromise) {
await this.evaluationPromise;
}
@@ -482,6 +487,7 @@ class ModuleJobSync extends ModuleJobBase {
}
runSync(parent) {
debug('ModuleJobSync.runSync()', this.module);
assert(this.phase === kEvaluationPhase);
// TODO(joyeecheung): add the error decoration logic from the async instantiate.
this.module.async = this.module.instantiateSync();
@@ -0,0 +1,17 @@
// This tests that after failing to import an ESM that rejects,
// retrying with require() still throws.
'use strict';
const common = require('../common');
const assert = require('assert');
(async () => {
await assert.rejects(import('../fixtures/es-modules/throw-error.mjs'), {
message: 'test',
});
assert.throws(() => {
require('../fixtures/es-modules/throw-error.mjs');
}, {
message: 'test',
});
})().catch(common.mustNotCall());
@@ -0,0 +1,16 @@
// This tests that after failing to require an ESM that throws,
// retrying with import() still rejects.
'use strict';
const common = require('../common');
const assert = require('assert');
assert.throws(() => {
require('../fixtures/es-modules/throw-error.mjs');
}, {
message: 'test',
});
assert.rejects(import('../fixtures/es-modules/throw-error.mjs'), {
message: 'test',
}).catch(common.mustNotCall());