Skip to content

Commit 7ae2100

Browse files
JakobJingleheimerGeoffreyBooth
authored andcommitted
WIP
1 parent f0a2a75 commit 7ae2100

File tree

6 files changed

+28
-23
lines changed

6 files changed

+28
-23
lines changed

lib/internal/modules/esm/hooks.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ class HooksProxy {
505505
AtomicsWait(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 0);
506506
const response = this.#worker.receiveMessageSync();
507507
if (response == null || response.message.status === 'exit') { return; }
508+
509+
// ! This line catches initialization errors in the worker thread.
510+
this.#unwrapMessage(response);
508511
}
509512

510513
this.#isReady = true;
@@ -587,8 +590,10 @@ class HooksProxy {
587590
AtomicsWait(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, this.#workerNotificationLastId);
588591
this.#workerNotificationLastId = AtomicsLoad(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION);
589592

593+
writeFileSync(1, `HooksProxy::makeSyncRequest: awoken\n`);
590594
response = this.#worker.receiveMessageSync();
591595
} while (response == null);
596+
writeFileSync(1, `HooksProxy::makeSyncRequest: got response from worker ${JSON.stringify(response)}\n`);
592597
debug('got sync response from worker', { method, args });
593598
if (response.message.status === 'never-settle') {
594599
process.exit(kUnfinishedTopLevelAwait);
@@ -604,6 +609,7 @@ class HooksProxy {
604609
}
605610
const { status, body } = response.message;
606611
if (status === 'error') {
612+
writeFileSync(1, `HooksProxy#unwrapMessage: got error from worker ${body}\n`);
607613
if (body == null || typeof body !== 'object') throw body;
608614
if (body.serializationFailed || body.serialized == null) {
609615
throw ERR_WORKER_UNSERIALIZABLE_ERROR();

lib/internal/modules/esm/loader.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,16 @@ class ModuleLoader {
309309
* @returns {Promise<ModuleExports>}
310310
*/
311311
async import(specifier, parentURL, importAssertions) {
312-
writeFileSync(1, `ESMLoader::import specifier ${specifier}\n`);
312+
// writeFileSync(1, `ESMLoader::import specifier ${specifier}\n`);
313313
const moduleJob = await this.getModuleJob(specifier, parentURL, importAssertions);
314-
writeFileSync(1, `ESMLoader::import gotModuleJob ${JSON.stringify(moduleJob)}\n`);
314+
// writeFileSync(1, `ESMLoader::import gotModuleJob ${JSON.stringify(moduleJob)}\n`);
315315
const { module } = await moduleJob.run().catch((err) => {
316-
writeFileSync(1, `ESMLoader::import moduleJob.run caught error ${err.stack}\n`)
316+
writeFileSync(1, `ESMLoader::import ${specifier} moduleJob.run caught error ${err.stack}\n`)
317317
throw err;
318318
});
319-
writeFileSync(1, `ESMLoader::import gotModuleJob ${JSON.stringify(module)}\n`);
319+
writeFileSync(1, `ESMLoader::import ${specifier} gotModuleJob ${JSON.stringify(module)}\n`);
320320
const namespace = module.getNamespace();
321-
writeFileSync(1, `ESMLoader::import gotModuleJob ${JSON.stringify(namespace)}\n`);
321+
writeFileSync(1, `ESMLoader::import ${specifier} gotModuleJob ${JSON.stringify(namespace)}\n`);
322322
return namespace;
323323
}
324324

lib/internal/modules/esm/module_job.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const resolvedPromise = PromiseResolve();
3030
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
3131
debug = fn;
3232
});
33-
const { writeFileSync } = require('fs');
33+
// const { writeFileSync } = require('fs');
3434

3535
const noop = FunctionPrototype;
3636

@@ -114,21 +114,21 @@ class ModuleJob {
114114
}
115115

116116
async _instantiate() {
117-
writeFileSync(1, 'ModuleJob::instantiate()\n');
117+
// writeFileSync(1, 'ModuleJob::instantiate()\n');
118118
const jobsInGraph = new SafeSet();
119119
const addJobsToDependencyGraph = async (moduleJob) => {
120120
if (jobsInGraph.has(moduleJob)) {
121121
return;
122122
}
123123
jobsInGraph.add(moduleJob);
124-
writeFileSync(1, 'ModuleJob::instantiate() getting linked jobs\n');
124+
// writeFileSync(1, 'ModuleJob::instantiate() getting linked jobs\n');
125125
const dependencyJobs = await moduleJob.linked;
126-
writeFileSync(1, 'ModuleJob::instantiate() got linked jobs\n');
126+
// writeFileSync(1, 'ModuleJob::instantiate() got linked jobs\n');
127127
return SafePromiseAllReturnVoid(dependencyJobs, addJobsToDependencyGraph);
128128
};
129129
await addJobsToDependencyGraph(this);
130130

131-
writeFileSync(1, 'ModuleJob::instantiate() job added to dep graph\n');
131+
// writeFileSync(1, 'ModuleJob::instantiate() job added to dep graph\n');
132132
try {
133133
if (!hasPausedEntry && this.inspectBrk) {
134134
hasPausedEntry = true;
@@ -138,7 +138,7 @@ class ModuleJob {
138138
this.module.instantiate();
139139
}
140140
} catch (e) {
141-
writeFileSync(1, `ModuleJob::instantiate() error: ${e.stack}\n`);
141+
// writeFileSync(1, `ModuleJob::instantiate() error: ${e.stack}\n`);
142142
decorateErrorStack(e);
143143
// TODO(@bcoe): Add source map support to exception that occurs as result
144144
// of missing named export. This is currently not possible because
@@ -218,16 +218,16 @@ class ModuleJob {
218218
}
219219

220220
async run() {
221-
writeFileSync(1, `ModuleJob::run() instantiating: ${this.url}\n`);
221+
// writeFileSync(1, `ModuleJob::run() instantiating: ${this.url}\n`);
222222
await this.instantiate();
223-
writeFileSync(1, `ModuleJob::run() instantiated: ${this.url}\n`);
223+
// writeFileSync(1, `ModuleJob::run() instantiated: ${this.url}\n`);
224224
const timeout = -1;
225225
const breakOnSigint = false;
226226
try {
227227
await this.module.evaluate(timeout, breakOnSigint);
228-
writeFileSync(1, `ModuleJob::run() evaluation complete: ${this.module}\n`);
228+
// writeFileSync(1, `ModuleJob::run() evaluation complete: ${this.module}\n`);
229229
} catch (e) {
230-
writeFileSync(1, `ModuleJob::run() error: ${e.stack}\n`);
230+
// writeFileSync(1, `ModuleJob::run() error: ${e.stack}\n`);
231231
if (e?.name === 'ReferenceError' &&
232232
isCommonJSGlobalLikeNotDefinedError(e.message)) {
233233
e.message += ' in ES module scope';
@@ -249,7 +249,7 @@ class ModuleJob {
249249
'to use the \'.cjs\' file extension.';
250250
}
251251
}
252-
writeFileSync(1, `ModuleJob::run() throwing error: ${e.stack}\n`);
252+
// writeFileSync(1, `ModuleJob::run() throwing error: ${e.stack}\n`);
253253
throw e;
254254
}
255255
return { __proto__: null, module: this.module };

lib/internal/modules/esm/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ async function initializeHooks() {
153153
customLoaderURLs[i],
154154
parentURL,
155155
).catch((err) => {
156-
writeFileSync(1, `initializeHooks: Error registering hooks: ${err.message}\n${err.stack}\n`);
156+
writeFileSync(1, `initializeHooks: Error registering hooks: ${err.stack}\n`);
157157
throw err;
158158
});
159159
}

lib/internal/modules/esm/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
101101
// be thrown and printed.
102102
hasInitializationError = true;
103103
initializationError = exception;
104-
writeFileSync(1, `Error initializing hooks: ${exception.stack}\n`);
104+
writeFileSync(1, `[Worker] Error initializing hooks: ${exception.stack}\n`);
105105
}
106106

107107
syncCommPort.on('message', handleMessage);
108108

109109
if (hasInitializationError) {
110+
writeFileSync(1, `[Worker] posting initialization error: ${initializationError}\n`);
110111
syncCommPort.postMessage(wrapMessage('error', initializationError));
111112
} else {
112113
syncCommPort.postMessage(wrapMessage('success'));

test/es-module/test-esm-loader-with-syntax-error.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ describe('ESM: loader with syntax error', { concurrency: true }, () => {
1717
console.log('\n\nstderr', stderr);
1818
console.log('\n\nstdout', stdout);
1919

20-
match('foo', /bar/);
21-
22-
// match(stderr, /SyntaxError \[Error\]:/);
23-
// doesNotMatch(stderr, /Bad command or file name/); // It should have crashed before this.
24-
// notStrictEqual(code, 0);
20+
match(stderr, /SyntaxError \[Error\]:/);
21+
doesNotMatch(stderr, /Bad command or file name/); // It should have crashed before this.
22+
notStrictEqual(code, 0);
2523
});
2624
});

0 commit comments

Comments
 (0)