Skip to content

Commit 8b02670

Browse files
authored
fix(core): pass-through ipc messages to and from task processes (#11744)
1 parent 9d6e53b commit 8b02670

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

packages/nx/src/tasks-runner/forked-process-task-runner.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync, writeFileSync } from 'fs';
22
import * as dotenv from 'dotenv';
3-
import { ChildProcess, fork } from 'child_process';
3+
import { ChildProcess, fork, Serializable } from 'child_process';
44
import { workspaceRoot } from '../utils/workspace-root';
55
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
66
import { output } from '../utils/output';
@@ -30,7 +30,7 @@ export class ForkedProcessTaskRunner {
3030
private processes = new Set<ChildProcess>();
3131

3232
constructor(private readonly options: DefaultTasksRunnerOptions) {
33-
this.setupOnProcessExitListener();
33+
this.setupProcessEventListeners();
3434
}
3535

3636
// TODO: vsavkin delegate terminal output printing
@@ -79,6 +79,14 @@ export class ForkedProcessTaskRunner {
7979
switch (message.type) {
8080
case BatchMessageType.Complete: {
8181
res(message.results);
82+
break;
83+
}
84+
case BatchMessageType.Tasks: {
85+
break;
86+
}
87+
default: {
88+
// Re-emit any non-batch messages from the task process
89+
process.send(message);
8290
}
8391
}
8492
});
@@ -126,6 +134,12 @@ export class ForkedProcessTaskRunner {
126134
),
127135
});
128136
this.processes.add(p);
137+
138+
// Re-emit any messages from the task process
139+
p.on('message', (message) => {
140+
process.send(message);
141+
});
142+
129143
let out = [];
130144
let outWithErr = [];
131145
p.stdout.on('data', (chunk) => {
@@ -199,6 +213,12 @@ export class ForkedProcessTaskRunner {
199213
),
200214
});
201215
this.processes.add(p);
216+
217+
// Re-emit any messages from the task process
218+
p.on('message', (message) => {
219+
process.send(message);
220+
});
221+
202222
p.on('exit', (code, signal) => {
203223
if (code === null) code = this.signalToCode(signal);
204224
// we didn't print any output as we were running the command
@@ -369,7 +389,15 @@ export class ForkedProcessTaskRunner {
369389
return 128;
370390
}
371391

372-
private setupOnProcessExitListener() {
392+
private setupProcessEventListeners() {
393+
// When the nx process gets a message, it will be sent into the task's process
394+
process.on('message', (message: Serializable) => {
395+
this.processes.forEach((p) => {
396+
p.send(message);
397+
});
398+
});
399+
400+
// Terminate any task processes on exit
373401
process.on('SIGINT', () => {
374402
this.processes.forEach((p) => {
375403
p.kill('SIGTERM');

0 commit comments

Comments
 (0)