Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): combine serial and parallel execution + forward sigint to child process #13885

Merged
merged 3 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/nx-misc/src/extras.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ describe('Extra Nx Misc Tests', () => {
runCLI(`run ${mylib}:error`);
fail('Should error if process errors');
} catch (e) {
expect(e.stdout.toString()).toContain(
'Something went wrong in run-commands - Command failed: exit 1'
expect(e.stderr.toString()).toContain(
'command "exit 1" exited with non-zero status code'
);
}
});
Expand Down
30 changes: 17 additions & 13 deletions packages/nx/src/executors/run-commands/run-commands.impl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exec, execSync } from 'child_process';
import { exec } from 'child_process';
import * as path from 'path';
import * as yargsParser from 'yargs-parser';
import { env as appendLocalEnv } from 'npm-run-path';
Expand Down Expand Up @@ -176,12 +176,20 @@ async function runSerially(
context: ExecutorContext
) {
for (const c of options.commands) {
createSyncProcess(
c.command,
const success = await createProcess(
c,
undefined,
options.color,
calculateCwd(options.cwd, context)
);
if (!success) {
process.stderr.write(
`Warning: run-commands command "${c.command}" exited with non-zero status code`
);
return false;
}
}

return true;
}

Expand All @@ -205,9 +213,14 @@ function createProcess(
/**
* Ensure the child process is killed when the parent exits
*/
const processExitListener = () => childProcess.kill();
const processExitListener = (signal?: number | NodeJS.Signals) => () =>
childProcess.kill(signal);

process.on('exit', processExitListener);
process.on('SIGTERM', processExitListener);
process.on('SIGINT', processExitListener);
process.on('SIGQUIT', processExitListener);

childProcess.stdout.on('data', (data) => {
process.stdout.write(addColorAndPrefix(data, commandConfig));
if (readyWhen && data.toString().indexOf(readyWhen) > -1) {
Expand Down Expand Up @@ -253,15 +266,6 @@ function addColorAndPrefix(
return out;
}

function createSyncProcess(command: string, color: boolean, cwd: string) {
execSync(command, {
env: processEnv(color),
stdio: ['inherit', 'inherit', 'inherit'],
maxBuffer: LARGE_BUFFER,
cwd,
});
}

function calculateCwd(
cwd: string | undefined,
context: ExecutorContext
Expand Down