From 30e5c87b226ec804efd176e916947833a9dac151 Mon Sep 17 00:00:00 2001 From: Jonas Lindenskov Nielsen Date: Tue, 22 Feb 2022 20:05:50 +0100 Subject: [PATCH] fix(node): wait for subprocess to exit when --watch=false in node execute executor Fixes #7031. Before this fix, the executor would exit immediately and let the node subprocess continue in the background, if watch was set to false. This commit attempts to fix this problem, by adding event listeners on the subprocess in the node/execute executor if watch is false, such that the nx process continues simply (a)waits until the subprocess exists (successfully or not). Error handling has also been added such that the executor fails if a non-successful status code is returned by the program --- .../src/executors/execute/execute.impl.ts | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/node/src/executors/execute/execute.impl.ts b/packages/node/src/executors/execute/execute.impl.ts index 7944857e5a1df..12831b8a577a2 100644 --- a/packages/node/src/executors/execute/execute.impl.ts +++ b/packages/node/src/executors/execute/execute.impl.ts @@ -68,7 +68,10 @@ export async function* executeExecutor( } } -function runProcess(event: NodeBuildEvent, options: NodeExecuteBuilderOptions) { +async function runProcess( + event: NodeBuildEvent, + options: NodeExecuteBuilderOptions +) { if (subProcess || !event.success) { return; } @@ -76,6 +79,20 @@ function runProcess(event: NodeBuildEvent, options: NodeExecuteBuilderOptions) { subProcess = fork(event.outfile, options.args, { execArgv: getExecArgv(options), }); + + if (!options.watch) { + await new Promise((resolve, reject) => { + subProcess.once('exit', (code) => { + if (code !== 0) { + reject(`Node process exited with unsuccessful code ${code}`); + return; + } + + resolve(); + }); + subProcess.once('error', reject); + }); + } } function getExecArgv(options: NodeExecuteBuilderOptions) { @@ -103,7 +120,7 @@ async function handleBuildEvent( if ((!event.success || options.watch) && subProcess) { await killProcess(); } - runProcess(event, options); + await runProcess(event, options); } async function killProcess() {