esm: fix missed renaming in ModuleJob.runSync

https://redirect.github.com/nodejs/node/pull/59675 missed a case
when renaming .async to .hasAsyncGraph. This fixes that and add
a test that would previously crash with the missed rename.

PR-URL: https://github.com/nodejs/node/pull/59724
Refs: https://github.com/nodejs/node/pull/59675
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
Joyee Cheung 2025-09-04 12:01:32 +02:00 committed by GitHub
parent 2258f22672
commit 3ffc3d73ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 40 additions and 1 deletions

View File

@ -335,7 +335,7 @@ class ModuleJob extends ModuleJobBase {
const parentFilename = urlToFilename(parent?.filename);
this.module.hasAsyncGraph ??= this.module.isGraphAsync();
if (this.module.async && !getOptionValue('--experimental-print-required-tla')) {
if (this.module.hasAsyncGraph && !getOptionValue('--experimental-print-required-tla')) {
throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename);
}
if (status === kInstantiated) {

View File

@ -0,0 +1,21 @@
'use strict';
// This tests that in the require() in imported CJS can retry loading an ESM with TLA
// twice and get the correct error both times.
require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const fixtures = require('../common/fixtures');
const assert = require('assert');
spawnSyncAndAssert(
process.execPath,
['--import', fixtures.fileURL('es-modules', 'import-require-tla-twice', 'hook.js'),
fixtures.path('es-modules', 'import-require-tla-twice', 'require-tla.js'),
],
{
stdout(output) {
const matches = output.matchAll(/e\.code === ERR_REQUIRE_ASYNC_MODULE true/g);
assert.strictEqual([...matches].length, 2);
}
}
);

View File

@ -0,0 +1,6 @@
const { registerHooks } = require('module');
registerHooks({
load(url, context, nextLoad) {
return nextLoad(url, context);
}
});

View File

@ -0,0 +1,11 @@
try {
require('./tla.mjs');
} catch (e) {
console.log('e.code === ERR_REQUIRE_ASYNC_MODULE', e.code === 'ERR_REQUIRE_ASYNC_MODULE');
}
try {
require('./tla.mjs');
} catch (e) {
console.log('e.code === ERR_REQUIRE_ASYNC_MODULE', e.code === 'ERR_REQUIRE_ASYNC_MODULE');
}

View File

@ -0,0 +1 @@
await Promise.resolve('1');