Skip to content

Commit 5397839

Browse files
committed
Implement synchronous instantiation for ModuleJobSync; enhance error handling
1 parent 96575e0 commit 5397839

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎lib/internal/modules/esm/loader.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ class ModuleLoader {
372372
return onImport.traceSync(() => {
373373
const request = { specifier: url, phase: kEvaluationPhase, attributes: kEmptyObject, __proto__: null };
374374
const job = this.getOrCreateModuleJob(undefined, request, kImportInImportedESM);
375-
job.module.instantiate();
375+
job.instantiateSync();
376376
if (job.module.hasAsyncGraph) {
377377
// Hand the job back so the caller can evaluate it through the ordinary
378378
// asynchronous entry point machinery, without re-resolving and re-linking

‎lib/internal/modules/esm/module_job.js‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,16 @@ class ModuleJobSync extends ModuleJobBase {
630630
return PromiseResolve(this.module);
631631
}
632632

633+
instantiateSync() {
634+
try {
635+
this.module.instantiate();
636+
} catch (error) {
637+
decorateErrorStack(error);
638+
handleCJSNamedExportError(error, this.module, this.commonJsDeps);
639+
throw error;
640+
}
641+
}
642+
633643
async run(isEntryPoint = false) {
634644
assert(this.shouldRunModule(this.phase));
635645
const status = this.module.getStatus();
@@ -650,13 +660,7 @@ class ModuleJobSync extends ModuleJobBase {
650660
// or one that was initially require()'d and is now being imported after its instantiation
651661
// failed (e.g. a missing named export). Try finishing the instantiation: if it succeeds,
652662
// proceed to evaluation, otherwise re-throw the instantiation error.
653-
try {
654-
this.module.instantiate();
655-
} catch (error) {
656-
decorateErrorStack(error);
657-
handleCJSNamedExportError(error, this.module, this.commonJsDeps);
658-
throw error;
659-
}
663+
this.instantiateSync();
660664
}
661665
// `status === kInstantiated`: either just instantiated above, or previously instantiated
662666
// but evaluation was deferred (e.g. TLA detected by a prior `runSync()` call)
@@ -703,8 +707,7 @@ class ModuleJobSync extends ModuleJobBase {
703707
assert(this.shouldRunModule(this.phase));
704708
// TODO(joyeecheung): Reject graphs with top-level await _before_ instantiation, so that the
705709
// async graph error supersedes instantiation (mismatch export) errors in the graph.
706-
// TODO(joyeecheung): add the error decoration logic from the async instantiate.
707-
this.module.instantiate();
710+
this.instantiateSync();
708711
// On the deprecated async loader hook worker thread, dependencies linked by an
709712
// earlier import may not be walkable synchronously, so double-check with
710713
// V8 now that the graph is instantiated.

‎test/es-module/test-esm-cjs-named-error.mjs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import '../common/index.mjs';
1+
import { spawnPromisified } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
23
import assert from 'assert';
4+
import { execPath } from 'node:process';
35

46
const fixtureBase = '../fixtures/es-modules/package-cjs-named-error';
57

@@ -75,3 +77,10 @@ await assert.rejects(async () => {
7577
await assert.rejects(async () => {
7678
await import(`${fixtureBase}/escaped-single-quote.mjs`);
7779
}, /import pkg from '\.\/oh'no\.cjs'/, 'should support relative specifiers with escaped single quote');
80+
81+
const entryPoint = fixtures.path('es-modules', 'package-cjs-named-error', 'single-quote.mjs');
82+
const { code, stderr } = await spawnPromisified(execPath, [entryPoint]);
83+
assert.strictEqual(code, 1);
84+
assert.ok(stderr.includes(expectedRelative), 'entry point should show the CommonJS named export hint');
85+
assert.ok(stderr.includes("import { comeOn } from './fail.cjs';"),
86+
'entry point error should include the source import statement');

0 commit comments

Comments
 (0)