Skip to content

Commit 14fdc3b

Browse files
author
Bartek Wrona
committed
Fix dbg() to use lazy-initialized node modules for ESM compatibility
The previous fix moved await import() for node:fs and node:util outside dbg() to module scope. This broke test_dbg because dbg() can be called from --pre-js before those module-scope imports execute. Use lazy initialization instead: declare dbg_node_fs/dbg_node_utils early but leave them undefined. Initialize them in shell.js after fs and utils are loaded (reusing the same imports). dbg() checks if the modules are available and falls back to console.warn if not. This handles all cases: - dbg() from --pre-js (before init): uses console.warn - dbg() after init on Node.js with pthreads: uses fs.writeSync - dbg() in browser/non-node: uses console.warn
1 parent 2b964d5 commit 14fdc3b

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/runtime_debug.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,25 @@ var runtimeDebug = true; // Switch to false at runtime to disable logging at the
99

1010
// Used by XXXXX_DEBUG settings to output debug messages.
1111
#if ENVIRONMENT_MAY_BE_NODE && (PTHREADS || WASM_WORKERS)
12-
// Pre-load debug modules for use in dbg(). These are loaded at module scope
13-
// (inside async function Module()) where await is valid, since dbg() itself
14-
// is a regular function where await cannot be used.
15-
var dbg_fs, dbg_utils;
16-
if (ENVIRONMENT_IS_NODE) {
17-
dbg_fs = {{{ makeNodeImport('node:fs', false) }}};
18-
dbg_utils = {{{ makeNodeImport('node:util', false) }}};
19-
}
12+
// Node modules for dbg() output. Loaded lazily on first use since dbg() can
13+
// be called from --pre-js before module initialization, and await import()
14+
// cannot be used inside the non-async dbg() function.
15+
var dbg_node_fs, dbg_node_utils;
2016
#endif
2117
function dbg(...args) {
2218
if (!runtimeDebug && typeof runtimeDebug != 'undefined') return;
2319
#if ENVIRONMENT_MAY_BE_NODE && (PTHREADS || WASM_WORKERS)
2420
// Avoid using the console for debugging in multi-threaded node applications
2521
// See https://github.com/emscripten-core/emscripten/issues/14804
26-
if (ENVIRONMENT_IS_NODE) {
22+
if (ENVIRONMENT_IS_NODE && dbg_node_fs) {
2723
function stringify(a) {
2824
switch (typeof a) {
29-
case 'object': return dbg_utils.inspect(a);
25+
case 'object': return dbg_node_utils.inspect(a);
3026
case 'undefined': return 'undefined';
3127
}
3228
return a;
3329
}
34-
dbg_fs.writeSync(2, args.map(stringify).join(' ') + '\n');
30+
dbg_node_fs.writeSync(2, args.map(stringify).join(' ') + '\n');
3531
} else
3632
#endif
3733
// TODO(sbc): Make this configurable somehow. Its not always convenient for

src/shell.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,11 @@ if (ENVIRONMENT_IS_NODE) {
347347
var stringify = (a) => typeof a == 'object' ? utils.inspect(a) : a;
348348
defaultPrint = (...args) => fs.writeSync(1, args.map(stringify).join(' ') + '\n');
349349
defaultPrintErr = (...args) => fs.writeSync(2, args.map(stringify).join(' ') + '\n');
350+
#if (ASSERTIONS || RUNTIME_DEBUG || AUTODEBUG) && (PTHREADS || WASM_WORKERS)
351+
// Initialize the lazy-loaded modules for dbg() now that fs/utils are available.
352+
dbg_node_fs = fs;
353+
dbg_node_utils = utils;
354+
#endif
350355
}
351356
{{{ makeModuleReceiveWithVar('out', 'print', 'defaultPrint') }}}
352357
{{{ makeModuleReceiveWithVar('err', 'printErr', 'defaultPrintErr') }}}

0 commit comments

Comments
 (0)