Skip to content

Commit 1c508f2

Browse files
JakobJingleheimerGeoffreyBooth
authored andcommitted
WIP: exposed error? (need to remove catches, debugs, etc)
1 parent 26b13b6 commit 1c508f2

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed

lib/internal/modules/esm/hooks.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ const {
6666
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
6767
debug = fn;
6868
});
69+
const { writeFileSync } = require('fs');
70+
71+
6972
let importMetaInitializer;
7073

7174
/**
@@ -125,11 +128,16 @@ class Hooks {
125128
*/
126129
async register(urlOrSpecifier, parentURL, data) {
127130
const moduleLoader = require('internal/process/esm_loader').esmLoader;
131+
// debug('Hooks::register', { urlOrSpecifier, parentURL, data }, moduleLoader.import)
128132
const keyedExports = await moduleLoader.import(
129133
urlOrSpecifier,
130134
parentURL,
131135
kEmptyObject,
132-
);
136+
).catch((err) => {
137+
writeFileSync(1, `Hooks::register err ${err.stack}\n`);
138+
throw err;
139+
});
140+
debug('Hooks::register imported', urlOrSpecifier, keyedExports)
133141
return this.addCustomLoader(urlOrSpecifier, keyedExports, data);
134142
}
135143

lib/internal/modules/esm/loader.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const {
2020
getDefaultConditions,
2121
} = require('internal/modules/esm/utils');
2222
let defaultResolve, defaultLoad, defaultLoadSync, importMetaInitializer;
23+
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
24+
debug = fn;
25+
});
26+
const { writeFileSync } = require('fs');
2327

2428
function newResolveCache() {
2529
const { ResolveCache } = require('internal/modules/esm/module_map');
@@ -167,6 +171,7 @@ class ModuleLoader {
167171
*/
168172
setCustomizations(customizations) {
169173
this.#customizations = customizations;
174+
this.isCustomized = customizations != null;
170175
if (customizations) {
171176
this.allowImportMetaResolve = customizations.allowImportMetaResolve;
172177
} else {
@@ -304,9 +309,17 @@ class ModuleLoader {
304309
* @returns {Promise<ModuleExports>}
305310
*/
306311
async import(specifier, parentURL, importAssertions) {
312+
writeFileSync(1, `ESMLoader::import specifier ${specifier}\n`);
307313
const moduleJob = await this.getModuleJob(specifier, parentURL, importAssertions);
308-
const { module } = await moduleJob.run();
309-
return module.getNamespace();
314+
writeFileSync(1, `ESMLoader::import gotModuleJob ${JSON.stringify(moduleJob)}\n`);
315+
const { module } = await moduleJob.run().catch((err) => {
316+
writeFileSync(1, `ESMLoader::import moduleJob.run caught error ${err.stack}\n`)
317+
throw err;
318+
});
319+
writeFileSync(1, `ESMLoader::import gotModuleJob ${JSON.stringify(module)}\n`);
320+
const namespace = module.getNamespace();
321+
writeFileSync(1, `ESMLoader::import gotModuleJob ${JSON.stringify(namespace)}\n`);
322+
return namespace;
310323
}
311324

312325
/**

lib/internal/modules/esm/module_job.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const {
2727
} = require('internal/source_map/source_map_cache');
2828
const assert = require('internal/assert');
2929
const resolvedPromise = PromiseResolve();
30+
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
31+
debug = fn;
32+
});
33+
const { writeFileSync } = require('fs');
3034

3135
const noop = FunctionPrototype;
3236

@@ -87,8 +91,7 @@ class ModuleJob {
8791
return job.modulePromise;
8892
});
8993

90-
if (promises !== undefined)
91-
await SafePromiseAllReturnVoid(promises);
94+
if (promises !== undefined) await SafePromiseAllReturnVoid(promises);
9295

9396
return SafePromiseAllReturnArrayLike(dependencyJobs);
9497
};
@@ -111,17 +114,21 @@ class ModuleJob {
111114
}
112115

113116
async _instantiate() {
117+
writeFileSync(1, 'ModuleJob::instantiate()\n');
114118
const jobsInGraph = new SafeSet();
115119
const addJobsToDependencyGraph = async (moduleJob) => {
116120
if (jobsInGraph.has(moduleJob)) {
117121
return;
118122
}
119123
jobsInGraph.add(moduleJob);
124+
writeFileSync(1, 'ModuleJob::instantiate() getting linked jobs\n');
120125
const dependencyJobs = await moduleJob.linked;
126+
writeFileSync(1, 'ModuleJob::instantiate() got linked jobs\n');
121127
return SafePromiseAllReturnVoid(dependencyJobs, addJobsToDependencyGraph);
122128
};
123129
await addJobsToDependencyGraph(this);
124130

131+
writeFileSync(1, 'ModuleJob::instantiate() job added to dep graph\n');
125132
try {
126133
if (!hasPausedEntry && this.inspectBrk) {
127134
hasPausedEntry = true;
@@ -131,6 +138,7 @@ class ModuleJob {
131138
this.module.instantiate();
132139
}
133140
} catch (e) {
141+
writeFileSync(1, `ModuleJob::instantiate() error: ${e.stack}\n`);
134142
decorateErrorStack(e);
135143
// TODO(@bcoe): Add source map support to exception that occurs as result
136144
// of missing named export. This is currently not possible because
@@ -210,12 +218,16 @@ class ModuleJob {
210218
}
211219

212220
async run() {
221+
writeFileSync(1, `ModuleJob::run() instantiating: ${this.url}\n`);
213222
await this.instantiate();
223+
writeFileSync(1, `ModuleJob::run() instantiated: ${this.url}\n`);
214224
const timeout = -1;
215225
const breakOnSigint = false;
216226
try {
217227
await this.module.evaluate(timeout, breakOnSigint);
228+
writeFileSync(1, `ModuleJob::run() evaluation complete: ${this.module}\n`);
218229
} catch (e) {
230+
writeFileSync(1, `ModuleJob::run() error: ${e.stack}\n`);
219231
if (e?.name === 'ReferenceError' &&
220232
isCommonJSGlobalLikeNotDefinedError(e.message)) {
221233
e.message += ' in ES module scope';
@@ -237,6 +249,7 @@ class ModuleJob {
237249
'to use the \'.cjs\' file extension.';
238250
}
239251
}
252+
writeFileSync(1, `ModuleJob::run() throwing error: ${e.stack}\n`);
240253
throw e;
241254
}
242255
return { __proto__: null, module: this.module };

lib/internal/modules/esm/utils.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const {
2525
getModuleFromWrap,
2626
} = require('internal/vm/module');
2727
const assert = require('internal/assert');
28+
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
29+
debug = fn;
30+
});
31+
const { writeFileSync } = require('fs');
2832

2933
const callbackMap = new SafeWeakMap();
3034
function setCallbackForWrap(wrap, data) {
@@ -120,12 +124,15 @@ async function initializeHooks() {
120124
cwd = '/';
121125
}
122126

123-
124127
const { Hooks } = require('internal/modules/esm/hooks');
125128
const esmLoader = require('internal/process/esm_loader').esmLoader;
126129

127130
const hooks = new Hooks();
131+
// debug('initializeHooks hooks:', hooks);
132+
// debug('initializeHooks between hooks & esmLoader');
133+
// debug('initializeHooks esmLoader:', esmLoader);
128134
esmLoader.setCustomizations(hooks);
135+
// debug('initializeHooks customization set');
129136

130137
// We need the loader customizations to be set _before_ we start invoking
131138
// `--require`, otherwise loops can happen because a `--require` script
@@ -135,15 +142,22 @@ async function initializeHooks() {
135142
// N.B. This block appears here specifically in order to ensure that
136143
// `--require` calls occur before `--loader` ones do.
137144
loadPreloadModules();
145+
// debug('initializeHooks loaded preload modules');
138146
initializeFrozenIntrinsics();
147+
// debug('initializeHooks Frozen Intrinsics initialized');
139148

140149
const parentURL = pathToFileURL(cwd).href;
150+
// debug('initializeHooks registering hooks relative to', parentURL, customLoaderURLs);
141151
for (let i = 0; i < customLoaderURLs.length; i++) {
142152
await hooks.register(
143153
customLoaderURLs[i],
144154
parentURL,
145-
);
155+
).catch((err) => {
156+
writeFileSync(1, `initializeHooks: Error registering hooks: ${err.message}\n${err.stack}\n`);
157+
throw err;
158+
});
146159
}
160+
debug('initializeHooks finished: hooks', hooks);
147161

148162
return hooks;
149163
}

lib/internal/modules/esm/worker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const {
3131
} = require('internal/modules/esm/shared_constants');
3232
const { initializeHooks } = require('internal/modules/esm/utils');
3333
const { isMarkedAsUntransferable } = require('internal/buffer');
34+
const { writeFileSync } = require('fs');
35+
3436

3537
function transferArrayBuffer(hasError, source) {
3638
if (hasError || source == null) return;
@@ -99,6 +101,7 @@ async function customizedModuleWorker(lock, syncCommPort, errorHandler) {
99101
// be thrown and printed.
100102
hasInitializationError = true;
101103
initializationError = exception;
104+
writeFileSync(1, `Error initializing hooks: ${exception.stack}\n`);
102105
}
103106

104107
syncCommPort.on('message', handleMessage);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ import { describe, it } from 'node:test';
77

88
describe('ESM: loader with syntax error', { concurrency: true }, () => {
99
it('should crash the node process', async () => {
10-
const { code, stderr } = await spawnPromisified(execPath, [
10+
const { code, stderr, stdout } = await spawnPromisified(execPath, [
1111
'--no-warnings',
1212
'--experimental-loader',
1313
fileURL('es-module-loaders', 'syntax-error.mjs').href,
1414
path('print-error-message.js'),
1515
]);
1616

17-
console.log(stderr);
17+
console.log('\n\nstderr', stderr);
18+
console.log('\n\nstdout', stdout);
1819

19-
match(stderr, /SyntaxError \[Error\]:/);
20-
doesNotMatch(stderr, /Bad command or file name/);
21-
notStrictEqual(code, 0);
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);
2225
});
2326
});

0 commit comments

Comments
 (0)