nx 24.0.0 → 24.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/src/ai/clone-ai-config-repo.js +49 -12
  2. package/dist/src/command-line/migrate/migrate-commits.d.ts +17 -7
  3. package/dist/src/command-line/migrate/migrate-commits.js +57 -17
  4. package/dist/src/command-line/migrate/run/clean-retry.js +4 -2
  5. package/dist/src/command-line/migrate/run/orchestrator.js +19 -13
  6. package/dist/src/daemon/client/client.d.ts +4 -0
  7. package/dist/src/daemon/client/client.js +67 -12
  8. package/dist/src/daemon/server/handle-process-in-background.js +21 -9
  9. package/dist/src/daemon/server/native-js-loader.d.ts +1 -0
  10. package/dist/src/daemon/server/native-js-loader.js +21 -0
  11. package/dist/src/daemon/server/project-graph-incremental-recomputation.js +4 -8
  12. package/dist/src/daemon/server/server.d.ts +2 -1
  13. package/dist/src/daemon/server/server.js +16 -3
  14. package/dist/src/daemon/server/start.d.ts +1 -0
  15. package/dist/src/daemon/server/start.js +2 -0
  16. package/dist/src/executors/run-commands/run-commands.impl.d.ts +1 -1
  17. package/dist/src/executors/run-commands/run-commands.impl.js +14 -8
  18. package/dist/src/executors/run-commands/running-tasks.d.ts +1 -1
  19. package/dist/src/executors/run-commands/running-tasks.js +126 -77
  20. package/dist/src/executors/run-commands/schema.json +1 -1
  21. package/dist/src/native/index.d.ts +1 -1
  22. package/dist/src/native/nx.wasm32-wasi.debug.wasm +0 -0
  23. package/dist/src/native/nx.wasm32-wasi.wasm +0 -0
  24. package/dist/src/plugins/js/lock-file/pnpm-parser.js +2 -1
  25. package/dist/src/tasks-runner/init-tasks-runner.js +103 -61
  26. package/dist/src/tasks-runner/life-cycles/static-run-many-terminal-output-life-cycle.js +5 -2
  27. package/dist/src/tasks-runner/life-cycles/static-run-one-terminal-output-life-cycle.js +4 -1
  28. package/dist/src/tasks-runner/life-cycles/summary-terminal-output-life-cycle.js +5 -1
  29. package/dist/src/tasks-runner/pseudo-terminal.d.ts +1 -1
  30. package/dist/src/tasks-runner/pseudo-terminal.js +2 -2
  31. package/dist/src/tasks-runner/readiness/probes.js +2 -2
  32. package/dist/src/tasks-runner/readiness/ready-when.d.ts +22 -1
  33. package/dist/src/tasks-runner/readiness/ready-when.js +25 -2
  34. package/dist/src/tasks-runner/run-command.d.ts +6 -2
  35. package/dist/src/tasks-runner/run-command.js +14 -25
  36. package/dist/src/tasks-runner/run-outcome.d.ts +16 -0
  37. package/dist/src/tasks-runner/run-outcome.js +39 -0
  38. package/dist/src/tasks-runner/running-tasks/running-task.d.ts +1 -1
  39. package/dist/src/tasks-runner/task-orchestrator.d.ts +4 -2
  40. package/dist/src/tasks-runner/task-orchestrator.js +92 -21
  41. package/dist/src/tasks-runner/tasks-schedule.js +4 -5
  42. package/dist/src/utils/child-process.js +1 -1
  43. package/dist/src/utils/find-matching-projects.js +9 -6
  44. package/dist/src/utils/git-utils.d.ts +6 -4
  45. package/dist/src/utils/git-utils.js +43 -6
  46. package/dist/src/utils/output.d.ts +4 -3
  47. package/dist/src/utils/output.js +4 -3
  48. package/dist/src/utils/package-manager.js +5 -0
  49. package/package.json +11 -11
  50. package/schemas/nx-schema.json +6 -0
  51. package/schemas/project-schema.json +6 -0
@@ -36,7 +36,6 @@ const collectedUpdatedFiles = new Map();
36
36
  const collectedDeletedFiles = new Map();
37
37
  const projectGraphRecomputationListeners = new Set();
38
38
  let storedWorkspaceConfigHash;
39
- let knownExternalNodes = {};
40
39
  let fileChangeCounter = 0;
41
40
  let recomputationGeneration = 0;
42
41
  // The graph the settled cached promise serves, with the generation its
@@ -341,7 +340,7 @@ function computeWorkspaceConfigHash(projectsConfigurations) {
341
340
  .map(([projectName, projectConfig]) => `${projectName}:${JSON.stringify(projectConfig)}`);
342
341
  return (0, file_hasher_1.hashArray)(projectConfigurationStrings);
343
342
  }
344
- async function processCollectedUpdatedAndDeletedFiles({ projects, externalNodes, projectRootMap }, updatedFileHashes, deletedFiles) {
343
+ async function processCollectedUpdatedAndDeletedFiles({ projects, projectRootMap }, updatedFileHashes, deletedFiles) {
345
344
  try {
346
345
  const configHash = computeWorkspaceConfigHash(projects);
347
346
  // Config changed → can't incrementally update; refetch the file map
@@ -350,7 +349,7 @@ async function processCollectedUpdatedAndDeletedFiles({ projects, externalNodes,
350
349
  // can't clobber a faster newer one's already-committed state.
351
350
  if (configHash !== storedWorkspaceConfigHash) {
352
351
  const fresh = await (0, retrieve_workspace_files_1.retrieveWorkspaceFiles)(workspace_root_1.workspaceRoot, projectRootMap);
353
- return { fileMap: fresh, configHash, knownExternalNodes: externalNodes };
352
+ return { fileMap: fresh, configHash };
354
353
  }
355
354
  // Config unchanged → patch the existing file map in place.
356
355
  if (exports.fileMapWithFiles) {
@@ -477,9 +476,6 @@ async function processFilesAndCreateAndSerializeProjectGraph(separatedPlugins) {
477
476
  // can't clobber a newer compute's write.
478
477
  exports.fileMapWithFiles = fileMapUpdate.fileMap;
479
478
  storedWorkspaceConfigHash = fileMapUpdate.configHash;
480
- if (fileMapUpdate.knownExternalNodes) {
481
- knownExternalNodes = fileMapUpdate.knownExternalNodes;
482
- }
483
479
  // Drain only after committing — a stale compute that returns at the
484
480
  // staleness check above must leave its snapshot in `collected*` so the
485
481
  // newer compute still sees those files. Removing them earlier would
@@ -604,12 +600,12 @@ function copyFileMap(m) {
604
600
  }
605
601
  return c;
606
602
  }
607
- async function createAndSerializeProjectGraph({ projects, sourceMaps, }) {
603
+ async function createAndSerializeProjectGraph({ projects, externalNodes, sourceMaps, }) {
608
604
  try {
609
605
  perf_hooks_1.performance.mark('create-project-graph-start');
610
606
  const fileMap = copyFileMap(exports.fileMapWithFiles.fileMap);
611
607
  const rustReferences = exports.fileMapWithFiles.rustReferences;
612
- const { projectGraph, projectFileMapCache } = await (0, build_project_graph_1.buildProjectGraphUsingProjectFileMap)(projects, knownExternalNodes, fileMap, rustReferences, exports.currentProjectFileMapCache || (0, nx_deps_cache_1.readFileMapCache)(), await (0, get_plugins_1.getPlugins)((0, nx_json_1.readNxJson)(workspace_root_1.workspaceRoot)), sourceMaps);
608
+ const { projectGraph, projectFileMapCache } = await (0, build_project_graph_1.buildProjectGraphUsingProjectFileMap)(projects, externalNodes, fileMap, rustReferences, exports.currentProjectFileMapCache || (0, nx_deps_cache_1.readFileMapCache)(), await (0, get_plugins_1.getPlugins)((0, nx_json_1.readNxJson)(workspace_root_1.workspaceRoot)), sourceMaps);
613
609
  exports.currentProjectFileMapCache = projectFileMapCache;
614
610
  exports.currentProjectGraph = projectGraph;
615
611
  exports.currentSourceMaps = sourceMaps;
@@ -1,5 +1,6 @@
1
1
  import { Server, Socket } from 'net';
2
2
  import '../../utils/perf-logging';
3
+ import { respondWithErrorAndExit } from './shutdown-utils';
3
4
  export type HandlerResult = {
4
5
  description: string;
5
6
  error?: any;
@@ -7,5 +8,5 @@ export type HandlerResult = {
7
8
  };
8
9
  export declare const openSockets: Set<Socket>;
9
10
  export declare function handleMessage(socket: Socket, data: Buffer): Promise<void>;
10
- export declare function handleResult(socket: Socket, type: string, hrFn: () => Promise<HandlerResult>, mode: 'json' | 'v8'): Promise<void>;
11
+ export declare function handleResult(socket: Socket, type: string, hrFn: () => Promise<HandlerResult>, mode: 'json' | 'v8', respondToError?: typeof respondWithErrorAndExit): Promise<void>;
11
12
  export declare function startServer(): Promise<Server>;
@@ -155,7 +155,9 @@ async function handleMessage(socket, data) {
155
155
  await handleResult(socket, 'HASH_TASKS_UPFRONT', () => (0, handle_hash_tasks_1.handleHashTasksUpfront)(payload), mode);
156
156
  }
157
157
  else if (payload.type === 'PROCESS_IN_BACKGROUND') {
158
- await handleResult(socket, 'PROCESS_IN_BACKGROUND', () => (0, handle_process_in_background_1.handleProcessInBackground)(payload), mode);
158
+ // The client names the module to run, so its failure says nothing about
159
+ // the daemon's health. Respond, but stay alive for other clients.
160
+ await handleResult(socket, 'PROCESS_IN_BACKGROUND', () => (0, handle_process_in_background_1.handleProcessInBackground)(payload), mode, shutdown_utils_1.respondWithError);
159
161
  }
160
162
  else if (payload.type === 'RECORD_OUTPUTS_HASH_BATCH') {
161
163
  await handleResult(socket, 'RECORD_OUTPUTS_HASH_BATCH', () => (0, handle_outputs_tracking_1.handleRecordOutputsHashBatch)(payload), mode);
@@ -168,6 +170,17 @@ async function handleMessage(socket, data) {
168
170
  }
169
171
  else if (payload.type === 'REGISTER_FILE_WATCHER') {
170
172
  file_watcher_sockets_1.registeredFileWatcherSockets.push({ socket, config: payload.config });
173
+ // A client must not announce readiness until this socket is subscribed.
174
+ // Older clients do not request an acknowledgement and expect only changes.
175
+ if (payload.registrationId) {
176
+ await handleResult(socket, 'REGISTER_FILE_WATCHER', async () => ({
177
+ description: 'File watcher registered',
178
+ response: {
179
+ type: 'FILE_WATCHER_REGISTERED',
180
+ registrationId: payload.registrationId,
181
+ },
182
+ }), mode);
183
+ }
171
184
  }
172
185
  else if ((0, register_project_graph_listener_1.isRegisterProjectGraphListenerMessage)(payload)) {
173
186
  project_graph_listener_sockets_1.registeredProjectGraphListenerSockets.push(socket);
@@ -245,7 +258,7 @@ async function handleMessage(socket, data) {
245
258
  await (0, shutdown_utils_1.respondWithErrorAndExit)(socket, `Invalid payload from the client`, new Error(`Unsupported payload sent to daemon server: ${(0, consume_messages_from_socket_1.describeMessage)(unparsedPayload, { maxBytes: 200 })}`));
246
259
  }
247
260
  }
248
- async function handleResult(socket, type, hrFn, mode) {
261
+ async function handleResult(socket, type, hrFn, mode, respondToError = shutdown_utils_1.respondWithErrorAndExit) {
249
262
  let hr;
250
263
  const startMark = new Date();
251
264
  try {
@@ -256,7 +269,7 @@ async function handleResult(socket, type, hrFn, mode) {
256
269
  }
257
270
  const doneHandlingMark = new Date();
258
271
  if (hr.error) {
259
- await (0, shutdown_utils_1.respondWithErrorAndExit)(socket, hr.description, hr.error);
272
+ await respondToError(socket, hr.description, hr.error);
260
273
  }
261
274
  else {
262
275
  logger_1.serverLogger.log(`Serializing response for ${type} message in ${mode} mode`);
@@ -1 +1,2 @@
1
1
  import '../../utils/enable-compile-cache';
2
+ import './native-js-loader';
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  // Must be the first import — see enable-compile-cache.ts.
5
5
  require("../../utils/enable-compile-cache");
6
+ // Captures Node's .js loader before user code can hook it, see native-js-loader.ts.
7
+ require("./native-js-loader");
6
8
  const output_1 = require("../../utils/output");
7
9
  const server_1 = require("./server");
8
10
  const process = tslib_1.__importStar(require("process"));
@@ -56,7 +56,7 @@ export default function (options: RunCommandsOptions, context: ExecutorContext):
56
56
  success: boolean;
57
57
  terminalOutput: string;
58
58
  }>;
59
- export declare function runCommands(options: RunCommandsOptions, context: ExecutorContext, task?: Pick<Task, 'id' | 'ultracache'>, baseEnv?: NodeJS.ProcessEnv): Promise<NoopChildProcess | ParallelRunningTasks | import("../../tasks-runner/pseudo-terminal").PseudoTtyProcess | SeriallyRunningTasks>;
59
+ export declare function runCommands(options: RunCommandsOptions, context: ExecutorContext, task?: Pick<Task, 'id' | 'ultracache' | 'continuous'>, baseEnv?: NodeJS.ProcessEnv): Promise<NoopChildProcess | ParallelRunningTasks | import("../../tasks-runner/pseudo-terminal").PseudoTtyProcess | SeriallyRunningTasks>;
60
60
  /** The options `interpolateArgsIntoCommand` reads, from the task's options. */
61
61
  export declare function normalizeArgs(options: RunCommandsOptions): Pick<NormalizedRunCommandsOptions, 'args' | 'parsedArgs' | '__unparsed__' | 'unknownOptions' | 'unparsedCommandArgs'>;
62
62
  export declare function interpolateArgsIntoCommand(command: string, opts: Pick<NormalizedRunCommandsOptions, 'args' | 'parsedArgs' | '__unparsed__' | 'unknownOptions' | 'unparsedCommandArgs'>, forwardAllArgs: boolean, env?: () => NodeJS.ProcessEnv): string;
@@ -68,20 +68,26 @@ async function runCommands(options, context, task, baseEnv) {
68
68
  // The orchestrator's in-process path passes the task with a stub context,
69
69
  // so prefer it. `runExecutor` can name a target the task graph does not
70
70
  // hold, hence the project lookup; `convert-nx-executor` has neither graph,
71
- // and only then is the config unknowable, which counts as tracked.
71
+ // and only then is the config unknowable, which counts as tracked and
72
+ // discrete.
72
73
  const resolvedTaskId = task?.id ??
73
74
  (0, utils_1.createTaskId)(context.projectName, context.targetName, context.configurationName);
75
+ // `projectGraph` is typed as present, but `convert-nx-executor` passes
76
+ // null for both graphs.
77
+ const targetConfiguration = context.projectName && context.targetName
78
+ ? context.projectGraph?.nodes?.[context.projectName]?.data.targets?.[context.targetName]
79
+ : undefined;
74
80
  const resolvedTask = task ??
75
81
  context.taskGraph?.tasks?.[resolvedTaskId] ?? {
76
82
  id: resolvedTaskId,
77
- // Both graphs are typed as present, but `convert-nx-executor` passes
78
- // null for each.
79
- ultracache: context.projectName && context.targetName
80
- ? context.projectGraph?.nodes?.[context.projectName]?.data
81
- .targets?.[context.targetName]?.ultracache
82
- : undefined,
83
+ ultracache: targetConfiguration?.ultracache,
84
+ continuous: targetConfiguration?.continuous,
83
85
  };
84
- const runningTask = isSingleCommandAndCanUsePseudoTerminal
86
+ // The pseudo terminal runner ignores readyWhen, which keeps a continuous
87
+ // task running past the match. A discrete task must end there.
88
+ const usePtyRunner = isSingleCommandAndCanUsePseudoTerminal &&
89
+ (!normalized.readyWhenStatus.length || resolvedTask.continuous === true);
90
+ const runningTask = usePtyRunner
85
91
  ? await (0, running_tasks_1.runSingleCommandWithPseudoTerminal)(normalized, context, resolvedTask, baseEnv)
86
92
  : options.parallel
87
93
  ? new running_tasks_1.ParallelRunningTasks(normalized, context, resolvedTask, baseEnv)
@@ -45,7 +45,7 @@ export declare class SeriallyRunningTasks implements RunningTask {
45
45
  terminalOutput: string;
46
46
  }>;
47
47
  onExit(cb: (code: number, terminalOutput: string) => void): void;
48
- onOutput(cb: (terminalOutput: string) => void): void;
48
+ onOutput(cb: (terminalOutput: string, isCommandHeader?: boolean) => void): void;
49
49
  send(message: Serializable): void;
50
50
  kill(signal?: NodeJS.Signals): void | Promise<void>;
51
51
  private run;
@@ -41,6 +41,14 @@ function streamChunk(chunk, streamOutput, target = process.stdout) {
41
41
  function commandExitedNonZeroWarning(command) {
42
42
  return `Warning: command "${command}" exited with non-zero status code`;
43
43
  }
44
+ function commandsExitedBeforeReadyWarning(commands, readyWhenStatus) {
45
+ const exited = commands.length === 1 ? `command "${commands[0].command}"` : 'all commands';
46
+ const missing = readyWhenStatus
47
+ .filter(({ found }) => !found)
48
+ .map(({ stringToMatch }) => `"${stringToMatch}"`)
49
+ .join(', ');
50
+ return `Warning: ${exited} exited before printing the readyWhen text ${missing}`;
51
+ }
44
52
  class ParallelRunningTasks {
45
53
  constructor(options, context, task, baseEnv) {
46
54
  this.exitCallbacks = [];
@@ -94,19 +102,32 @@ class ParallelRunningTasks {
94
102
  }
95
103
  async run() {
96
104
  if (this.readyWhenStatus.length) {
97
- let { childProcess, result: { code, terminalOutput }, } = await Promise.race(this.childProcesses.map((childProcess) => new Promise((res) => {
98
- childProcess.onOutput((terminalOutput) => {
99
- for (const cb of this.outputCallbacks) {
100
- cb(terminalOutput);
101
- }
102
- });
103
- childProcess.onExit((code, terminalOutput) => {
104
- res({
105
- childProcess,
106
- result: { code, terminalOutput },
105
+ const settled = await Promise.race([
106
+ ...this.childProcesses.map((childProcess) => new Promise((res) => {
107
+ childProcess.onOutput((terminalOutput) => {
108
+ for (const cb of this.outputCallbacks) {
109
+ cb(terminalOutput);
110
+ }
107
111
  });
108
- });
109
- })));
112
+ childProcess.onExit((code, terminalOutput) => {
113
+ res({
114
+ childProcess,
115
+ result: { code, terminalOutput },
116
+ });
117
+ });
118
+ })),
119
+ Promise.all(this.childProcesses.map((childProcess) => childProcess.outputWhenClosed())).then((outputs) => ({ outputsOfExitedCommands: outputs })),
120
+ ]);
121
+ if ('outputsOfExitedCommands' in settled) {
122
+ const warning = commandsExitedBeforeReadyWarning(this.childProcesses, this.readyWhenStatus);
123
+ for (const cb of this.outputCallbacks) {
124
+ cb(warning);
125
+ }
126
+ streamChunk(warning, this.streamOutput, process.stderr);
127
+ this.emitExit(1, settled.outputsOfExitedCommands.join('\r\n') + warning);
128
+ return;
129
+ }
130
+ let { childProcess, result: { code, terminalOutput }, } = settled;
110
131
  if (code !== 0) {
111
132
  const warning = commandExitedNonZeroWarning(childProcess.command);
112
133
  terminalOutput += warning;
@@ -231,9 +252,9 @@ class SeriallyRunningTasks {
231
252
  for (const c of options.commands) {
232
253
  const childProcess = await this.createProcess(c, options.color, calculateCwd(options.cwd, context), options.processEnv ?? options.env ?? {}, this.task, options.usePty, options.streamOutput, options.tty, options.envFile, this.baseEnv);
233
254
  this.currentProcess = childProcess;
234
- childProcess.onOutput((output) => {
255
+ childProcess.onOutput((output, isCommandHeader) => {
235
256
  for (const cb of this.outputCallbacks) {
236
- cb(output);
257
+ cb(output, isCommandHeader);
237
258
  }
238
259
  });
239
260
  let { code, terminalOutput } = await childProcess.getResults();
@@ -270,6 +291,45 @@ class SeriallyRunningTasks {
270
291
  }
271
292
  }
272
293
  exports.SeriallyRunningTasks = SeriallyRunningTasks;
294
+ // Running children share one process listener per event instead of adding one
295
+ // each. The listeners are removed once the last child exits: a signal handler
296
+ // left behind would stop a forked executor from exiting on that signal.
297
+ const runningNodeProcesses = new Set();
298
+ function killRunningNodeProcessesOnExit() {
299
+ for (const runningNodeProcess of runningNodeProcesses) {
300
+ runningNodeProcess.killOnExit();
301
+ }
302
+ }
303
+ function terminateRunningNodeProcesses() {
304
+ for (const runningNodeProcess of runningNodeProcesses) {
305
+ runningNodeProcess.kill('SIGTERM');
306
+ }
307
+ }
308
+ function trackRunningNodeProcess(runningNodeProcess) {
309
+ if (runningNodeProcesses.size === 0) {
310
+ process.on('exit', killRunningNodeProcessesOnExit);
311
+ // In the direct path, detached children don't get OS SIGINT (own process
312
+ // group via setsid); the orchestrator's cleanup() sends SIGTERM via
313
+ // killProcessTreeGraceful. Signal handlers here are only needed in the
314
+ // forked path where there's no orchestrator to dispatch signals.
315
+ if (process.env.NX_FORKED_TASK_EXECUTOR) {
316
+ process.on('SIGINT', terminateRunningNodeProcesses);
317
+ process.on('SIGTERM', terminateRunningNodeProcesses);
318
+ process.on('SIGHUP', terminateRunningNodeProcesses);
319
+ }
320
+ }
321
+ runningNodeProcesses.add(runningNodeProcess);
322
+ }
323
+ function untrackRunningNodeProcess(runningNodeProcess) {
324
+ if (!runningNodeProcesses.delete(runningNodeProcess) ||
325
+ runningNodeProcesses.size > 0) {
326
+ return;
327
+ }
328
+ process.removeListener('exit', killRunningNodeProcessesOnExit);
329
+ process.removeListener('SIGINT', terminateRunningNodeProcesses);
330
+ process.removeListener('SIGTERM', terminateRunningNodeProcesses);
331
+ process.removeListener('SIGHUP', terminateRunningNodeProcesses);
332
+ }
273
333
  class RunningNodeProcess {
274
334
  /** This class's writes, funnelled through {@link streamChunk}. */
275
335
  stream(chunk, target = process.stdout) {
@@ -338,6 +398,10 @@ class RunningNodeProcess {
338
398
  send(message) {
339
399
  this.childProcess.send(message);
340
400
  }
401
+ async outputWhenClosed() {
402
+ await this.closedPromise;
403
+ return this.terminalOutputChunks.join('');
404
+ }
341
405
  emitExit(code, terminalOutput) {
342
406
  if (this.exited)
343
407
  return;
@@ -365,46 +429,30 @@ class RunningNodeProcess {
365
429
  ]));
366
430
  return this.killPromise;
367
431
  }
432
+ // Last resort on process exit, so it has to be sync. A graceful kill in
433
+ // progress tracks cleanup subprocesses that this must not interfere with.
434
+ killOnExit() {
435
+ if (this.childProcess.pid && !this.killPromise) {
436
+ (0, native_1.killProcessTree)(this.childProcess.pid);
437
+ }
438
+ }
368
439
  triggerOutputListeners(output) {
369
440
  for (const cb of this.outputCallbacks) {
370
441
  cb(output);
371
442
  }
372
443
  }
373
444
  addListeners(commandConfig) {
374
- // Named handlers so they can be removed when the child exits.
375
- // Otherwise each RunningNodeProcess leaks process listeners; with
376
- // many run-commands tasks this triggers MaxListenersExceededWarning.
377
- const isForked = !!process.env.NX_FORKED_TASK_EXECUTOR;
378
- const onProcessExit = () => {
379
- if (this.childProcess.pid && !this.killPromise) {
380
- (0, native_1.killProcessTree)(this.childProcess.pid);
381
- }
382
- };
383
- const onSigInt = () => {
384
- this.kill('SIGTERM');
385
- };
386
- const onSigTerm = () => {
387
- this.kill('SIGTERM');
388
- };
389
- const onSigHup = () => {
390
- this.kill('SIGTERM');
391
- };
392
- const removeProcessListeners = () => {
393
- process.removeListener('exit', onProcessExit);
394
- if (isForked) {
395
- process.removeListener('SIGINT', onSigInt);
396
- process.removeListener('SIGTERM', onSigTerm);
397
- process.removeListener('SIGHUP', onSigHup);
398
- }
399
- };
400
445
  this.childProcess.stdout.setEncoding('utf8');
401
446
  this.childProcess.stderr.setEncoding('utf8');
447
+ // Separate matchers, so a ready string never spans stdout and stderr
448
+ const stdoutReady = readyWhenMatcher(this.readyWhenStatus);
449
+ const stderrReady = readyWhenMatcher(this.readyWhenStatus);
402
450
  this.childProcess.stdout.on('data', (data) => {
403
451
  const output = addColorAndPrefix(data, commandConfig);
404
452
  this.terminalOutputChunks.push(output);
405
453
  this.triggerOutputListeners(output);
406
454
  this.stream(output);
407
- if (this.readyWhenStatus.length && isReady(this.readyWhenStatus, data)) {
455
+ if (this.readyWhenStatus.length && stdoutReady(data)) {
408
456
  for (const cb of this.exitCallbacks) {
409
457
  cb(0, this.terminalOutputChunks.join(''));
410
458
  }
@@ -415,9 +463,9 @@ class RunningNodeProcess {
415
463
  this.terminalOutputChunks.push(output);
416
464
  this.triggerOutputListeners(output);
417
465
  this.stream(output, process.stderr);
418
- if (this.readyWhenStatus.length && isReady(this.readyWhenStatus, err)) {
466
+ if (this.readyWhenStatus.length && stderrReady(err)) {
419
467
  for (const cb of this.exitCallbacks) {
420
- cb(1, this.terminalOutputChunks.join(''));
468
+ cb(0, this.terminalOutputChunks.join(''));
421
469
  }
422
470
  }
423
471
  });
@@ -427,39 +475,26 @@ class RunningNodeProcess {
427
475
  this.stream(output, process.stderr);
428
476
  const terminalOutput = this.terminalOutputChunks.join('');
429
477
  this.terminalOutputChunks = [];
430
- removeProcessListeners();
478
+ untrackRunningNodeProcess(this);
431
479
  this.emitExit(1, terminalOutput);
432
480
  });
433
481
  this.childProcess.on('exit', (code, signal) => {
434
482
  if (code === null) {
435
483
  code = (0, exit_codes_1.signalToCode)(signal);
436
484
  }
437
- removeProcessListeners();
485
+ untrackRunningNodeProcess(this);
486
+ if (code === 0 &&
487
+ this.readyWhenStatus.length &&
488
+ !isReady(this.readyWhenStatus)) {
489
+ // A parallel companion may still print the readyWhen text. Keep the
490
+ // output for ParallelRunningTasks, which decides once all have closed.
491
+ return;
492
+ }
438
493
  const terminalOutput = this.terminalOutputChunks.join('');
439
494
  this.terminalOutputChunks = [];
440
- if (!this.readyWhenStatus.length || isReady(this.readyWhenStatus)) {
441
- this.emitExit(code, terminalOutput);
442
- }
443
- else if (code !== 0) {
444
- // Process crashed before readyWhen matched; surface the failure so
445
- // awaiters unblock. A clean exit (code 0) is left as-is — a parallel
446
- // companion command may still produce the readyWhen output.
447
- this.emitExit(code, terminalOutput);
448
- }
495
+ this.emitExit(code, terminalOutput);
449
496
  });
450
- // Terminate any task processes on exit (sync, last resort).
451
- // Skip if graceful kill is already in progress — it tracks cleanup
452
- // subprocesses and we must not interfere with them.
453
- process.on('exit', onProcessExit);
454
- // In the direct path, detached children don't get OS SIGINT (own process
455
- // group via setsid); the orchestrator's cleanup() sends SIGTERM via
456
- // killProcessTreeGraceful. Signal handlers here are only needed in the
457
- // forked path where there's no orchestrator to dispatch signals.
458
- if (isForked) {
459
- process.on('SIGINT', onSigInt);
460
- process.on('SIGTERM', onSigTerm);
461
- process.on('SIGHUP', onSigHup);
462
- }
497
+ trackRunningNodeProcess(this);
463
498
  }
464
499
  }
465
500
  async function runSingleCommandWithPseudoTerminal(normalized, context, task, baseEnv) {
@@ -543,17 +578,31 @@ function processEnv(color, cwd, envOptionFromExecutor, envFile, baseEnv = proces
543
578
  delete res.NX_PREFIX_OUTPUT;
544
579
  return res;
545
580
  }
546
- function isReady(readyWhenStatus = [], data) {
547
- if (data) {
548
- for (const readyWhenElement of readyWhenStatus) {
549
- if (data.toString().indexOf(readyWhenElement.stringToMatch) > -1) {
550
- readyWhenElement.found = true;
551
- break;
552
- }
553
- }
554
- }
581
+ function isReady(readyWhenStatus) {
555
582
  return readyWhenStatus.every((readyWhenElement) => readyWhenElement.found);
556
583
  }
584
+ // The output as printed, kept across chunks, matches strings that hold escape
585
+ // sequences. LogMatcher, absent from the WASM binding, matches text with
586
+ // escapes inside it.
587
+ function readyWhenMatcher(readyWhenStatus) {
588
+ const matchers = readyWhenStatus.map((status) => ({
589
+ status,
590
+ matcher: native_1.IS_WASM ? null : new native_1.LogMatcher([status.stringToMatch]),
591
+ tail: '',
592
+ }));
593
+ return (chunk) => {
594
+ for (const entry of matchers) {
595
+ const { status, matcher } = entry;
596
+ if (status.found)
597
+ continue;
598
+ const printed = entry.tail + chunk;
599
+ status.found =
600
+ printed.includes(status.stringToMatch) || !!matcher?.feed(chunk);
601
+ entry.tail = printed.slice(Math.max(0, printed.length - status.stringToMatch.length + 1));
602
+ }
603
+ return isReady(readyWhenStatus);
604
+ };
605
+ }
557
606
  function loadEnvVarsFile(path, env = {}) {
558
607
  (0, task_env_1.unloadDotEnvFile)(path, env);
559
608
  const result = (0, task_env_1.loadAndExpandDotEnvFile)(path, env);
@@ -578,7 +627,7 @@ function registerProcessListener(runningTask, pseudoTerminal) {
578
627
  }
579
628
  });
580
629
  // Terminate any task processes on exit (sync, last resort).
581
- // The per-child exit handlers and PseudoTerminal.shutdown() use the
630
+ // killRunningNodeProcessesOnExit and PseudoTerminal.shutdown() use the
582
631
  // sync killProcessTree for this path. We call kill() here as a
583
632
  // best-effort fallback — it returns a Promise but on 'exit' only
584
633
  // synchronous work runs, so the initial signal is sent but the
@@ -123,7 +123,7 @@
123
123
  "x-priority": "important"
124
124
  },
125
125
  "readyWhen": {
126
- "description": "String or array of strings to appear in `stdout` or `stderr` that indicate that the task is done. When running multiple commands, this option can only be used when `parallel` is set to `true`. If not specified, the task is done when all the child processes complete.",
126
+ "description": "String or array of strings to appear in `stdout` or `stderr` that indicate that the task is done. Unless the target is `continuous`, the task fails if every command exits before printing them. When running multiple commands, this option can only be used when `parallel` is set to `true`. If not specified, the task is done when all the child processes complete.",
127
127
  "oneOf": [
128
128
  { "type": "string" },
129
129
  { "type": "array", "items": { "type": "string" } }
@@ -65,7 +65,7 @@ export declare class ChildProcess {
65
65
  getPid(): number
66
66
  kill(signal?: NodeJS.Signals | number): void
67
67
  onExit(callback: (message: string) => void): void
68
- onOutput(callback: (message: string) => void): void
68
+ onOutput(callback: (message: string, isCommandHeader: boolean) => void): void
69
69
  cleanup(): void
70
70
  }
71
71
 
Binary file
@@ -438,7 +438,8 @@ function getDependencies(data, keyMap, isV5, ctx) {
438
438
  Object.keys(section).forEach((name) => {
439
439
  const versionRange = section[name];
440
440
  const version = parseBaseVersion(findVersion(versionRange, name, isV5), isV5);
441
- const target = ctx.externalNodes[`npm:${name}@${version}`] ||
441
+ const target = keyMap.get(versionRange)?.values().next().value ||
442
+ ctx.externalNodes[`npm:${name}@${version}`] ||
442
443
  ctx.externalNodes[`npm:${name}`];
443
444
  if (target) {
444
445
  const dep = {