From e71e2f3da8d2c265a43843a193f17ab1d8c2889a Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Fri, 6 Sep 2024 10:55:47 -0400 Subject: [PATCH] Revert "feat(core): add shutdown lifecycle hook to node executor" (#27794) Reverts nrwl/nx#27354 This patch is causing issues that are more serious than what it fixes. --- .../node/node-with-require-overrides.ts | 26 ++++++++++++++----- packages/js/src/executors/node/node.impl.ts | 11 +++----- .../js/src/executors/node/patch-require.ts | 23 ---------------- .../js/src/executors/node/patch-sigint.ts | 21 --------------- 4 files changed, 23 insertions(+), 58 deletions(-) delete mode 100644 packages/js/src/executors/node/patch-require.ts delete mode 100644 packages/js/src/executors/node/patch-sigint.ts diff --git a/packages/js/src/executors/node/node-with-require-overrides.ts b/packages/js/src/executors/node/node-with-require-overrides.ts index 704a484d47e59..a67202a792b1d 100644 --- a/packages/js/src/executors/node/node-with-require-overrides.ts +++ b/packages/js/src/executors/node/node-with-require-overrides.ts @@ -1,9 +1,23 @@ +const Module = require('module'); const url = require('node:url'); -const { patchSigint } = require('./patch-sigint'); -const { patchRequire } = require('./patch-require'); - -patchSigint(); -patchRequire(); +const originalLoader = Module._load; const dynamicImport = new Function('specifier', 'return import(specifier)'); -dynamicImport(url.pathToFileURL(process.env.NX_FILE_TO_RUN)); + +const mappings = JSON.parse(process.env.NX_MAPPINGS); +const keys = Object.keys(mappings); +const fileToRun = url.pathToFileURL(process.env.NX_FILE_TO_RUN); + +Module._load = function (request, parent) { + if (!parent) return originalLoader.apply(this, arguments); + const match = keys.find((k) => request === k); + if (match) { + const newArguments = [...arguments]; + newArguments[0] = mappings[match]; + return originalLoader.apply(this, newArguments); + } else { + return originalLoader.apply(this, arguments); + } +}; + +dynamicImport(fileToRun); diff --git a/packages/js/src/executors/node/node.impl.ts b/packages/js/src/executors/node/node.impl.ts index a2e6a732329d7..78b1e2845907f 100644 --- a/packages/js/src/executors/node/node.impl.ts +++ b/packages/js/src/executors/node/node.impl.ts @@ -172,18 +172,13 @@ export async function* nodeExecutor( task.childProcess.stderr.on('data', handleStdErr); task.childProcess.once('exit', (code) => { task.childProcess.off('data', handleStdErr); - if ( - options.watch && - !task.killed && - // SIGINT should exist the process rather than watch for changes. - code !== 130 - ) { + if (options.watch && !task.killed) { logger.info( `NX Process exited with code ${code}, waiting for changes to restart...` ); } - if (!options.watch || code === 130) { - if (code !== 0 && code !== 130) { + if (!options.watch) { + if (code !== 0) { error(new Error(`Process exited with code ${code}`)); } else { done(); diff --git a/packages/js/src/executors/node/patch-require.ts b/packages/js/src/executors/node/patch-require.ts deleted file mode 100644 index 8bec53565137f..0000000000000 --- a/packages/js/src/executors/node/patch-require.ts +++ /dev/null @@ -1,23 +0,0 @@ -const Module = require('node:module'); -const originalLoader = Module._load; - -/** - * Overrides require calls to map buildable workspace libs to their output location. - * This is useful for running programs compiled via TSC/SWC that aren't bundled. - */ -export function patchRequire() { - const mappings = JSON.parse(process.env.NX_MAPPINGS); - const keys = Object.keys(mappings); - - Module._load = function (request, parent) { - if (!parent) return originalLoader.apply(this, arguments); - const match = keys.find((k) => request === k); - if (match) { - const newArguments = [...arguments]; - newArguments[0] = mappings[match]; - return originalLoader.apply(this, newArguments); - } else { - return originalLoader.apply(this, arguments); - } - }; -} diff --git a/packages/js/src/executors/node/patch-sigint.ts b/packages/js/src/executors/node/patch-sigint.ts deleted file mode 100644 index 286887524ddf3..0000000000000 --- a/packages/js/src/executors/node/patch-sigint.ts +++ /dev/null @@ -1,21 +0,0 @@ -const readline = require('node:readline'); - -/** - * Patches the current process so that Ctrl+C is properly handled. - * Without this patch, SIGINT or Ctrl+C does not wait for graceful shutdown and exits immediately. - */ -export function patchSigint() { - readline.emitKeypressEvents(process.stdin); - if (process.stdin.isTTY) process.stdin.setRawMode(true); - process.stdin.on('keypress', async (chunk, key) => { - if (key && key.ctrl && key.name === 'c') { - process.stdin.setRawMode(false); // To ensure nx terminal is not stuck in raw mode - const listeners = process.listeners('SIGINT'); - for (const listener of listeners) { - await listener('SIGINT'); - } - process.exit(130); - } - }); - console.log('To exit the process, press Ctrl+C'); -}