From c02af136b062574081d355a768ceddd3b0bb1903 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Wed, 13 Jul 2022 21:45:45 -0400 Subject: [PATCH] Attempt workaround for node regression (#1838) * attempt workaround for node regression * lint-fix * fix --- dist-raw/runmain-hack.js | 9 +++++++++ src/bin.ts | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 dist-raw/runmain-hack.js diff --git a/dist-raw/runmain-hack.js b/dist-raw/runmain-hack.js new file mode 100644 index 000000000..6c0688e14 --- /dev/null +++ b/dist-raw/runmain-hack.js @@ -0,0 +1,9 @@ +const {pathToFileURL} = require('url'); + +// Hack to avoid Module.runMain on node 18.6.0 +// Keeping it simple for now, isolated in this file. +// Could theoretically probe `getFormat` impl to determine if `import()` or `Module._load()` is best +// Note that I attempted a try-catch around `Module._load`, but it poisons some sort of cache such that subsequent `import()` is impossible. +exports.run = function(entryPointPath) { + import(pathToFileURL(entryPointPath)); +} diff --git a/src/bin.ts b/src/bin.ts index fb3208c48..f470a2c83 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -639,7 +639,15 @@ function phase4(payload: BootstrapState) { // Execute the main contents (either eval, script or piped). if (executeEntrypoint) { - Module.runMain(); + if ( + payload.isInChildProcess && + versionGteLt(process.versions.node, '18.6.0') + ) { + // HACK workaround node regression + require('../dist-raw/runmain-hack.js').run(entryPointPath); + } else { + Module.runMain(); + } } else { // Note: eval and repl may both run, but never with stdin. // If stdin runs, eval and repl will not.