From d55c3e7f0bbda5dbd188c8b41be0ef655a84df81 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 18 Aug 2025 16:08:59 +0200 Subject: [PATCH] esm: link modules synchronously when no async loader hooks are used When no async loader hooks are registered, perform the linking as synchronously as possible to reduce the chance of races from the the shared module loading cache. PR-URL: https://github.com/nodejs/node/pull/59519 Fixes: https://github.com/nodejs/node/issues/59366 Refs: https://github.com/abejfehr/node-22.18-issue-repro Reviewed-By: Geoffrey Booth Reviewed-By: Filip Skokan Reviewed-By: Chengzhong Wu --- lib/internal/modules/esm/loader.js | 18 +++++++++++++----- lib/internal/modules/esm/module_job.js | 10 +++++----- test/es-module/test-esm-error-cache.js | 4 +++- .../test-esm-import-attributes-errors.js | 4 ++-- .../test-esm-import-attributes-errors.mjs | 4 ++-- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 22868c31da8..78985575beb 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -63,6 +63,8 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => { debug = fn; }); +const { isPromise } = require('internal/util/types'); + /** * @typedef {import('./hooks.js').HooksProxy} HooksProxy * @typedef {import('./module_job.js').ModuleJobBase} ModuleJobBase @@ -582,15 +584,21 @@ class ModuleLoader { /** * Load a module and translate it into a ModuleWrap for ordinary imported ESM. - * This is run asynchronously. + * This may be run asynchronously if there are asynchronous module loader hooks registered. * @param {string} url URL of the module to be translated. * @param {object} loadContext See {@link load} * @param {boolean} isMain Whether the module to be translated is the entry point. - * @returns {Promise} + * @returns {Promise|ModuleWrap} */ - async loadAndTranslate(url, loadContext, isMain) { - const { format, source } = await this.load(url, loadContext); - return this.#translate(url, format, source, isMain); + loadAndTranslate(url, loadContext, isMain) { + const maybePromise = this.load(url, loadContext); + const afterLoad = ({ format, source }) => { + return this.#translate(url, format, source, isMain); + }; + if (isPromise(maybePromise)) { + return maybePromise.then(afterLoad); + } + return afterLoad(maybePromise); } /** diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 2ddfc77e64f..74d6ac12cdd 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -37,6 +37,7 @@ const { }, } = internalBinding('util'); const { decorateErrorStack, kEmptyObject } = require('internal/util'); +const { isPromise } = require('internal/util/types'); const { getSourceMapsSupport, } = require('internal/source_map/source_map_cache'); @@ -135,12 +136,11 @@ class ModuleJob extends ModuleJobBase { this.#loader = loader; // Expose the promise to the ModuleWrap directly for linking below. - if (isForRequireInImportedCJS) { - this.module = moduleOrModulePromise; - assert(this.module instanceof ModuleWrap); - this.modulePromise = PromiseResolve(this.module); - } else { + if (isPromise(moduleOrModulePromise)) { this.modulePromise = moduleOrModulePromise; + } else { + this.module = moduleOrModulePromise; + this.modulePromise = PromiseResolve(moduleOrModulePromise); } // Promise for the list of all dependencyJobs. diff --git a/test/es-module/test-esm-error-cache.js b/test/es-module/test-esm-error-cache.js index d780f1a2216..e39ddb895ac 100644 --- a/test/es-module/test-esm-error-cache.js +++ b/test/es-module/test-esm-error-cache.js @@ -19,7 +19,9 @@ let error; await assert.rejects( () => import(file), (e) => { - assert.strictEqual(error, e); + // The module may be compiled again and a new SyntaxError would be thrown but + // with the same content. + assert.deepStrictEqual(error, e); return true; } ); diff --git a/test/es-module/test-esm-import-attributes-errors.js b/test/es-module/test-esm-import-attributes-errors.js index 63513a51e5a..9789484be98 100644 --- a/test/es-module/test-esm-import-attributes-errors.js +++ b/test/es-module/test-esm-import-attributes-errors.js @@ -28,7 +28,7 @@ async function test() { await rejects( import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }), - { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } ); await rejects( @@ -48,7 +48,7 @@ async function test() { await rejects( import(jsonModuleDataUrl, { with: { foo: 'bar' } }), - { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } ); await rejects( diff --git a/test/es-module/test-esm-import-attributes-errors.mjs b/test/es-module/test-esm-import-attributes-errors.mjs index d7ffc6f92c9..8095662b104 100644 --- a/test/es-module/test-esm-import-attributes-errors.mjs +++ b/test/es-module/test-esm-import-attributes-errors.mjs @@ -23,7 +23,7 @@ await rejects( await rejects( import(jsModuleDataUrl, { with: { type: 'json', other: 'unsupported' } }), - { code: 'ERR_IMPORT_ATTRIBUTE_TYPE_INCOMPATIBLE' } + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } ); await rejects( @@ -43,7 +43,7 @@ await rejects( await rejects( import(jsonModuleDataUrl, { with: { foo: 'bar' } }), - { code: 'ERR_IMPORT_ATTRIBUTE_MISSING' } + { code: 'ERR_IMPORT_ATTRIBUTE_UNSUPPORTED' } ); await rejects(