@smoothbricks/nx-plugin 0.5.5 → 0.5.7

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 (58) hide show
  1. package/README.md +59 -11
  2. package/dist/ensure-built-receipt.d.ts +2 -2
  3. package/dist/ensure-built-receipt.d.ts.map +1 -1
  4. package/dist/ensure-built-receipt.js +7 -2
  5. package/dist/ensure-built-witness.d.ts +12 -1
  6. package/dist/ensure-built-witness.d.ts.map +1 -1
  7. package/dist/ensure-built-witness.js +32 -0
  8. package/dist/ensure-built.d.ts +2 -2
  9. package/dist/ensure-built.d.ts.map +1 -1
  10. package/dist/ensure-built.js +27 -5
  11. package/dist/executors/all-verdicts/executor.d.ts +35 -0
  12. package/dist/executors/all-verdicts/executor.d.ts.map +1 -0
  13. package/dist/executors/all-verdicts/executor.js +133 -0
  14. package/dist/executors/all-verdicts/schema.d.ts +10 -0
  15. package/dist/executors/all-verdicts/schema.d.ts.map +1 -0
  16. package/dist/executors/all-verdicts/schema.js +2 -0
  17. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  18. package/dist/executors/bounded-exec/executor.js +48 -11
  19. package/dist/executors/bounded-exec/progress.d.ts +1 -3
  20. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  21. package/dist/executors/bounded-exec/progress.js +1 -2
  22. package/dist/executors/bounded-exec/runner-hook.d.ts +2 -0
  23. package/dist/executors/bounded-exec/runner-hook.d.ts.map +1 -0
  24. package/dist/executors/bounded-exec/runner-hook.js +301 -0
  25. package/dist/executors/bounded-exec/runner-lifecycle.d.ts +18 -0
  26. package/dist/executors/bounded-exec/runner-lifecycle.d.ts.map +1 -0
  27. package/dist/executors/bounded-exec/runner-lifecycle.js +92 -0
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +4 -1
  30. package/dist/producer-receipts.d.ts +24 -1
  31. package/dist/producer-receipts.d.ts.map +1 -1
  32. package/dist/producer-receipts.js +91 -60
  33. package/executors.json +5 -0
  34. package/managed/raw/git-format-staged.yml +16 -3
  35. package/managed/raw/patches/nx@23.2.1.patch +397 -33
  36. package/package.json +2 -2
  37. package/src/ensure-built-receipt.ts +8 -2
  38. package/src/ensure-built-witness.ts +29 -1
  39. package/src/ensure-built.ts +39 -7
  40. package/src/executors/all-verdicts/executor.test.ts +135 -0
  41. package/src/executors/all-verdicts/executor.ts +164 -0
  42. package/src/executors/all-verdicts/schema.json +22 -0
  43. package/src/executors/all-verdicts/schema.ts +10 -0
  44. package/src/executors/bounded-exec/cargo-progress-host.test.ts +332 -0
  45. package/src/executors/bounded-exec/executor.test.ts +89 -42
  46. package/src/executors/bounded-exec/executor.ts +45 -11
  47. package/src/executors/bounded-exec/progress.test.ts +1 -14
  48. package/src/executors/bounded-exec/progress.ts +2 -5
  49. package/src/executors/bounded-exec/runner-hook.test.ts +193 -0
  50. package/src/executors/bounded-exec/runner-hook.ts +292 -0
  51. package/src/executors/bounded-exec/runner-lifecycle.test.ts +82 -0
  52. package/src/executors/bounded-exec/runner-lifecycle.ts +102 -0
  53. package/src/index.test.ts +7 -5
  54. package/src/index.ts +4 -1
  55. package/src/nx-daemon-start-never.test.ts +137 -0
  56. package/src/producer-receipts.test.ts +99 -0
  57. package/src/producer-receipts.ts +111 -58
  58. package/test-timings.json +101 -96
@@ -2,6 +2,8 @@ import { spawn } from 'node:child_process';
2
2
  import { accessSync, constants } from 'node:fs';
3
3
  import { availableParallelism, userInfo } from 'node:os';
4
4
  import { delimiter, isAbsolute, join, resolve as resolvePath } from 'node:path';
5
+ import { createInterface } from 'node:readline';
6
+ import { Duplex } from 'node:stream';
5
7
  import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory.js';
6
8
  import { CPU_POOL_ENV, createCpuPool, readInheritedCpuPool, takeParentCpuTokens, } from './cpu-pool.js';
7
9
  import { CPU_TOKENS_ENV, gatewayCpuBudget, runnerDemand, sizeRunner, } from './cpu-tokens.js';
@@ -9,6 +11,7 @@ import { captureHangDiagnostics, HANG_CAPTURE_MS } from './hang-diagnostics.js';
9
11
  import { createNoSpaceWatch } from './no-space.js';
10
12
  import { createRunProgress, withProgressReport } from './progress.js';
11
13
  import { COWSHED_EXECUTABLE_ENV, createHostCommands, describeRamTempError, RAM_TEMP_CAPACITY_BYTES, RamTempVolume, ramTempPaths, } from './ram-temp.js';
14
+ import { createRunnerLifecycle, withRunnerLifecycle } from './runner-lifecycle.js';
12
15
  import { clearTaskDirectory, describeNoSpace, JUNIT_FILE, JUNIT_REPORT_ENV, judgeOperationalFailure, judgeRun, readJunitReport, taskDirectory, withJunitReport, writeRecord, } from './verdict.js';
13
16
  const DEFAULT_KILL_AFTER_MS = 10_000;
14
17
  const REAP_AFTER_FORCE_KILL_MS = 2_000;
@@ -327,13 +330,18 @@ export async function runBoundedExec(options, context, killer, tempVolume, budge
327
330
  ...sized.env,
328
331
  ...(pool?.environment ?? {}),
329
332
  };
333
+ const hooked = process.platform === 'win32' ? null : withRunnerLifecycle(command);
334
+ const lifecycle = hooked === null ? null : createRunnerLifecycle(hooked.runners);
335
+ let lifecycleFailure = null;
336
+ const isTesting = () => lifecycleFailure !== null || (lifecycle?.isTesting() ?? true);
330
337
  const startedAt = Date.now();
331
338
  let child;
332
339
  try {
333
- child = spawn(command, [], {
340
+ child = spawn(hooked?.command ?? command, [], {
334
341
  cwd,
335
342
  env: mergeEnv(lease, env),
336
343
  shell: true,
344
+ stdio: hooked === null ? 'pipe' : ['pipe', 'pipe', 'pipe', 'pipe'],
337
345
  detached: process.platform !== 'win32',
338
346
  windowsHide: true,
339
347
  });
@@ -405,7 +413,7 @@ export async function runBoundedExec(options, context, killer, tempVolume, budge
405
413
  idleTimer = setTimeout(() => expire({ kind: 'idle', limitMs, tests: progress.inFlight() }, queuedMs), leftMs);
406
414
  };
407
415
  const armIdleTimer = () => {
408
- if (idleTimeoutMs === undefined || state.settled || state.expiry !== null || !progress.isTesting()) {
416
+ if (idleTimeoutMs === undefined || state.settled || state.expiry !== null || !isTesting()) {
409
417
  return;
410
418
  }
411
419
  idleLeftMs = idleTimeoutMs;
@@ -438,7 +446,7 @@ export async function runBoundedExec(options, context, killer, tempVolume, budge
438
446
  const waitedMs = Date.now() - queuedSinceMs;
439
447
  queuedMs += waitedMs;
440
448
  queuedSinceMs = null;
441
- if (!progress.isTesting()) {
449
+ if (!isTesting()) {
442
450
  appendStderr(`bounded-exec: host CPU wait ended after ${waitedMs}ms; no test runner is active\n`);
443
451
  return;
444
452
  }
@@ -449,10 +457,8 @@ export async function runBoundedExec(options, context, killer, tempVolume, budge
449
457
  // Only the CHILD's own output is read for progress. The diagnostics below go
450
458
  // through appendStderr as well, and re-arming on those would let a teardown
451
459
  // message extend the very bound that just fired.
452
- const scanProgress = (stream, text) => {
453
- const wasTesting = progress.isTesting();
454
- const finished = progress.scan(stream, text);
455
- if (!progress.isTesting()) {
460
+ const updateIdle = (finished, wasTesting) => {
461
+ if (!isTesting()) {
456
462
  clearTimeout(idleTimer);
457
463
  cancelCapture();
458
464
  idleCapture = undefined;
@@ -461,6 +467,37 @@ export async function runBoundedExec(options, context, killer, tempVolume, budge
461
467
  armIdleTimer();
462
468
  }
463
469
  };
470
+ const scanProgress = (stream, text) => {
471
+ updateIdle(progress.scan(stream, text), isTesting());
472
+ };
473
+ if (lifecycle !== null) {
474
+ const channel = child.stdio[3];
475
+ if (!(channel instanceof Duplex))
476
+ throw new Error('runner lifecycle requires a duplex child descriptor');
477
+ const failLifecycle = (detail, wasTesting = isTesting()) => {
478
+ if (lifecycleFailure !== null || state.expiry !== null)
479
+ return;
480
+ lifecycleFailure = detail;
481
+ appendStderr(`bounded-exec: runner lifecycle failed: ${detail}\n`);
482
+ updateIdle(false, wasTesting);
483
+ };
484
+ createInterface({ input: channel }).on('line', (line) => {
485
+ const wasTesting = isTesting();
486
+ const event = lifecycle.accept(line);
487
+ if (event === null) {
488
+ failLifecycle(lifecycle.error() ?? 'invalid lifecycle event', wasTesting);
489
+ return;
490
+ }
491
+ updateIdle(false, wasTesting);
492
+ // Tool startup and native lifecycle-bearing stdout wait for their phase acknowledgement.
493
+ channel.write(`${JSON.stringify(event)}\n`);
494
+ });
495
+ channel.on('error', (error) => failLifecycle(error.message));
496
+ channel.on('end', () => {
497
+ if (!lifecycle.finished())
498
+ failLifecycle('supervisor channel ended without an owned terminal event');
499
+ });
500
+ }
464
501
  child.stdout?.on('data', (chunk) => {
465
502
  const text = chunk.toString();
466
503
  scanProgress('stdout', text);
@@ -518,7 +555,7 @@ export async function runBoundedExec(options, context, killer, tempVolume, budge
518
555
  state.settled = true;
519
556
  resolve({ code: 1, signal: null });
520
557
  });
521
- child.once('exit', (code, signal) => {
558
+ child.once(hooked === null ? 'exit' : 'close', (code, signal) => {
522
559
  state.settled = true;
523
560
  resolve({ code, signal });
524
561
  });
@@ -632,9 +669,9 @@ export async function runBoundedExec(options, context, killer, tempVolume, budge
632
669
  }
633
670
  }
634
671
  await releaseLease();
635
- // A run whose sub-pool did not close cleanly fails even when the command exited 0, and whatever bound also fired:
636
- // its verdict is an ordinary failure, never a bound.
637
- const operationalFailure = state.poolCloseError !== null;
672
+ // Broken resource ownership or a lost native lifecycle fails even if a shell branch later exited 0:
673
+ // its verdict is an ordinary operational failure, never a successful or silently unbounded run.
674
+ const operationalFailure = state.poolCloseError !== null || lifecycleFailure !== null;
638
675
  await recordRun(code, state.expiry, Date.now() - startedAt, operationalFailure, seen);
639
676
  return {
640
677
  success: !operationalFailure && state.expiry === null && code === 0,
@@ -19,15 +19,13 @@
19
19
  * `fail`, or `skip` ends that execution interval. Test terminals re-arm the bound, but `output` and
20
20
  * `build-output` never do. Initial and inter-package builds are outside an active test interval.
21
21
  *
22
- * A command without a recognised execution lifecycle remains bounded from spawn.
22
+ * Execution phases are owned by the private supervisor channel in runner-lifecycle.ts, never these text reports.
23
23
  */
24
24
  export interface RunProgress {
25
25
  /** One chunk of one of the command's output streams; true when it reports a finished test or Go package. */
26
26
  scan(stream: OutputStream, chunk: string): boolean;
27
27
  /** The tests the runner reported running and has not reported finished, in the order it first named them. */
28
28
  inFlight(): string[];
29
- /** Whether test idle time is currently chargeable; unknown runner lifecycles stay chargeable. */
30
- isTesting(): boolean;
31
29
  }
32
30
  export type OutputStream = 'stdout' | 'stderr';
33
31
  export declare function createRunProgress(command?: string): RunProgress;
@@ -1 +1 @@
1
- {"version":3,"file":"progress.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/progress.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,WAAW,WAAW;IAC1B,4GAA4G;IAC5G,IAAI,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACnD,6GAA6G;IAC7G,QAAQ,IAAI,MAAM,EAAE,CAAC;IACrB,iGAAiG;IACjG,SAAS,IAAI,OAAO,CAAC;CACtB;AAED,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAqD/C,wBAAgB,iBAAiB,CAAC,OAAO,SAAK,GAAG,WAAW,CAqF3D;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAmB1D"}
1
+ {"version":3,"file":"progress.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/progress.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,WAAW,WAAW;IAC1B,4GAA4G;IAC5G,IAAI,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACnD,6GAA6G;IAC7G,QAAQ,IAAI,MAAM,EAAE,CAAC;CACtB;AAED,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAqD/C,wBAAgB,iBAAiB,CAAC,OAAO,SAAK,GAAG,WAAW,CAoF3D;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAmB1D"}
@@ -48,7 +48,7 @@ function goTestEvent(text) {
48
48
  throw error;
49
49
  }
50
50
  }
51
- /** Defer only a direct Go command/chain; an unknown shell wrapper or another runner keeps its existing bound. */
51
+ /** A direct Go chain reports tests only through JSON; other commands may also carry text runner reports. */
52
52
  function onlyGoTests(command) {
53
53
  let rest = command.trim();
54
54
  while (rest.search(GO_TEST) === 0) {
@@ -145,7 +145,6 @@ export function createRunProgress(command = '') {
145
145
  ...running,
146
146
  ...[...packages].flatMap(([name, tests]) => tests.size === 0 ? [name] : [...tests].map((test) => `${name} ${test}`)),
147
147
  ],
148
- isTesting: () => !goLifecycle || packages.size > 0,
149
148
  };
150
149
  }
151
150
  /**
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=runner-hook.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runner-hook.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/runner-hook.ts"],"names":[],"mappings":""}
@@ -0,0 +1,301 @@
1
+ /**
2
+ * Standalone supervisor: <runtime> runner-hook.js <id> <go|cargo> <executable> [args...].
3
+ * The parent requests native JSON output and supplies a private duplex socket on fd 3.
4
+ * Each newline JSON {id,phase} must be echoed exactly after applying the watchdog state.
5
+ * Build is acknowledged before spawn; each native transition is acknowledged before its
6
+ * stdout record is forwarded. Terminal follows child close and drained output, then its
7
+ * acknowledgement closes the channel. The tool receives only descriptors 0, 1, and 2.
8
+ */
9
+ import { spawn } from 'node:child_process';
10
+ import { Socket } from 'node:net';
11
+ function failure(context, error) {
12
+ return new Error(`${context}: ${error instanceof Error ? error.message : String(error)}`);
13
+ }
14
+ // This executable inherits lib es2022; Promise.withResolvers requires es2024.
15
+ function write(stream, bytes) {
16
+ return new Promise((resolve) => {
17
+ stream.write(bytes, (error) => resolve(error ?? null));
18
+ });
19
+ }
20
+ /** Only native stdout records change the lifecycle; arbitrary runner text never does. */
21
+ function nativePhase(runner) {
22
+ const packages = new Set();
23
+ let cargoActive = false;
24
+ return (line) => {
25
+ if (cargoActive || line[0] !== 123)
26
+ return null;
27
+ let event;
28
+ try {
29
+ event = JSON.parse(line.toString('utf8'));
30
+ }
31
+ catch (error) {
32
+ if (error instanceof SyntaxError)
33
+ return null;
34
+ throw error;
35
+ }
36
+ if (typeof event !== 'object' || event === null)
37
+ return null;
38
+ if (runner === 'cargo') {
39
+ if ('reason' in event &&
40
+ event.reason === 'build-finished' &&
41
+ 'success' in event &&
42
+ typeof event.success === 'boolean') {
43
+ // Even a failed build has left compilation. Nothing emitted by a test, doctest,
44
+ // or custom harness can put Cargo back into build or finish this invocation.
45
+ cargoActive = true;
46
+ return 'active';
47
+ }
48
+ return null;
49
+ }
50
+ if (!('Action' in event) || !('Package' in event) || typeof event.Package !== 'string' || 'Test' in event) {
51
+ return null;
52
+ }
53
+ switch (event.Action) {
54
+ case 'start':
55
+ packages.add(event.Package);
56
+ break;
57
+ case 'pass':
58
+ case 'fail':
59
+ case 'skip':
60
+ packages.delete(event.Package);
61
+ break;
62
+ default:
63
+ return null;
64
+ }
65
+ return packages.size === 0 ? 'build' : 'active';
66
+ };
67
+ }
68
+ /** One outstanding phase, acknowledged only after the executor applies its watchdog state. */
69
+ function lifecycle(id, failed) {
70
+ const socket = new Socket({ fd: 3, readable: true, writable: true });
71
+ let pending = null;
72
+ let received = '';
73
+ let error = null;
74
+ let closing = false;
75
+ let phase = null;
76
+ const fail = (cause) => {
77
+ if (error !== null)
78
+ return;
79
+ error = cause;
80
+ const waiting = pending;
81
+ pending = null;
82
+ waiting?.resolve(cause);
83
+ failed(cause);
84
+ socket.destroy();
85
+ };
86
+ socket.on('error', (cause) => fail(failure('lifecycle channel failed', cause)));
87
+ socket.on('end', () => {
88
+ if (!closing)
89
+ fail(new Error('lifecycle channel ended before terminal acknowledgement'));
90
+ });
91
+ socket.on('close', () => {
92
+ if (!closing && error === null)
93
+ fail(new Error('lifecycle channel closed before terminal acknowledgement'));
94
+ });
95
+ socket.on('data', (chunk) => {
96
+ received += chunk.toString('utf8');
97
+ if (pending === null || !pending.message.startsWith(received)) {
98
+ fail(new Error(`unexpected lifecycle acknowledgement: ${JSON.stringify(received)}`));
99
+ }
100
+ else if (received === pending.message) {
101
+ const waiting = pending;
102
+ pending = null;
103
+ received = '';
104
+ waiting.resolve(null);
105
+ }
106
+ });
107
+ return {
108
+ async announce(next) {
109
+ if (error !== null)
110
+ return error;
111
+ if (phase === next)
112
+ return null;
113
+ if (pending !== null)
114
+ throw new Error('concurrent lifecycle announcements');
115
+ const result = await new Promise((resolve) => {
116
+ const message = `${JSON.stringify({ id, phase: next })}\n`;
117
+ pending = { message, resolve };
118
+ socket.write(message, (cause) => {
119
+ if (cause)
120
+ fail(failure('writing lifecycle phase failed', cause));
121
+ });
122
+ });
123
+ if (result === null)
124
+ phase = next;
125
+ return result;
126
+ },
127
+ async close() {
128
+ closing = true;
129
+ if (socket.destroyed)
130
+ return;
131
+ await new Promise((resolve) => {
132
+ socket.once('close', resolve);
133
+ // The shell retains this endpoint for later commands: shutdown/end would poison
134
+ // every inherited reference. Terminal ACK drained our writes; close only ours.
135
+ socket.destroy();
136
+ });
137
+ },
138
+ };
139
+ }
140
+ /** Await each destination write, bounding queued output and preserving its original bytes. */
141
+ async function forward(source, target) {
142
+ try {
143
+ for await (const chunk of source) {
144
+ const bytes = chunk;
145
+ if (!Buffer.isBuffer(bytes))
146
+ throw new Error('child output was not a byte stream');
147
+ const error = await write(target, bytes);
148
+ if (error !== null)
149
+ return error;
150
+ }
151
+ return null;
152
+ }
153
+ catch (error) {
154
+ return failure('reading child output failed', error);
155
+ }
156
+ }
157
+ /** Buffer only possible JSON records; ACK their transition before releasing any of their bytes. */
158
+ async function forwardStdout(source, inspect) {
159
+ let pending = [];
160
+ let start = true;
161
+ let json = false;
162
+ const record = async (line) => {
163
+ const error = await inspect(line);
164
+ return error ?? (await write(process.stdout, line));
165
+ };
166
+ try {
167
+ for await (const chunk of source) {
168
+ const bytes = chunk;
169
+ if (!Buffer.isBuffer(bytes))
170
+ throw new Error('child stdout was not a byte stream');
171
+ let offset = 0;
172
+ while (offset < bytes.length) {
173
+ if (start)
174
+ json = bytes[offset] === 123;
175
+ const newline = bytes.indexOf(10, offset);
176
+ const end = newline === -1 ? bytes.length : newline + 1;
177
+ const piece = bytes.subarray(offset, end);
178
+ let error = null;
179
+ if (json && newline === -1) {
180
+ pending.push(piece);
181
+ }
182
+ else if (json) {
183
+ let line = piece;
184
+ if (pending.length !== 0) {
185
+ pending.push(piece);
186
+ line = Buffer.concat(pending);
187
+ pending = [];
188
+ }
189
+ error = await record(line);
190
+ }
191
+ else {
192
+ error = await write(process.stdout, piece);
193
+ }
194
+ if (error !== null)
195
+ return error;
196
+ start = newline !== -1;
197
+ offset = end;
198
+ }
199
+ }
200
+ return pending.length === 0 ? null : await record(Buffer.concat(pending));
201
+ }
202
+ catch (error) {
203
+ return failure('reading child stdout failed', error);
204
+ }
205
+ }
206
+ /** argv: invocation ID, runner kind, original executable, then its unchanged arguments. */
207
+ async function main([idText, runner, executable, ...args]) {
208
+ const id = Number(idText);
209
+ if (idText === undefined ||
210
+ !/^\d+$/.test(idText) ||
211
+ !Number.isSafeInteger(id) ||
212
+ (runner !== 'go' && runner !== 'cargo') ||
213
+ executable === undefined ||
214
+ executable === '') {
215
+ process.stderr.write('runner-hook: usage: <invocation-id> <go|cargo> <executable> [arguments...]\n');
216
+ process.exitCode = 2;
217
+ return;
218
+ }
219
+ let child = null;
220
+ let failed = false;
221
+ let interrupted = null;
222
+ const fail = (error) => {
223
+ if (failed)
224
+ return;
225
+ failed = true;
226
+ process.exitCode = 1;
227
+ process.stderr.write(`runner-hook: ${error.message}\n`);
228
+ child?.kill('SIGKILL');
229
+ };
230
+ const forwardedSignals = ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT'];
231
+ const signals = forwardedSignals.map((name) => {
232
+ const listener = () => {
233
+ interrupted = name;
234
+ child?.kill(name);
235
+ };
236
+ process.on(name, listener);
237
+ return { name, listener };
238
+ });
239
+ process.stdout.on('error', fail);
240
+ process.stderr.on('error', fail);
241
+ let channel = null;
242
+ let code = null;
243
+ let exitSignal = null;
244
+ try {
245
+ channel = lifecycle(id, fail);
246
+ const ready = await channel.announce('build');
247
+ if (ready !== null)
248
+ fail(ready);
249
+ if (!failed && interrupted === null) {
250
+ // Descriptor 3 belongs only to this supervisor, never the tool or its descendants.
251
+ const running = spawn(executable, args, { stdio: ['inherit', 'pipe', 'pipe'] });
252
+ child = running;
253
+ const closed = new Promise((resolve) => {
254
+ running.once('close', (status, reason) => {
255
+ code = status;
256
+ exitSignal = reason;
257
+ resolve();
258
+ });
259
+ });
260
+ running.on('error', (error) => fail(failure(`could not run ${JSON.stringify(executable)}`, error)));
261
+ const scan = nativePhase(runner);
262
+ const control = channel;
263
+ const stdout = forwardStdout(running.stdout, async (line) => {
264
+ const next = scan(line);
265
+ return next === null ? null : await control.announce(next);
266
+ }).then((error) => {
267
+ if (error !== null)
268
+ fail(failure('forwarding stdout failed', error));
269
+ });
270
+ const stderr = forward(running.stderr, process.stderr).then((error) => {
271
+ if (error !== null)
272
+ fail(failure('forwarding stderr failed', error));
273
+ });
274
+ await Promise.all([closed, stdout, stderr]);
275
+ }
276
+ else {
277
+ exitSignal = interrupted;
278
+ }
279
+ }
280
+ catch (error) {
281
+ fail(failure('supervisor failed', error));
282
+ }
283
+ // Write callbacks complete only once downstream has accepted every forwarded byte.
284
+ // Flush diagnostics too, before terminal and before re-raising a child's signal.
285
+ for (const error of await Promise.all([write(process.stdout, ''), write(process.stderr, '')])) {
286
+ if (error !== null)
287
+ fail(failure('draining supervisor output failed', error));
288
+ }
289
+ if (channel !== null) {
290
+ const error = await channel.announce('terminal');
291
+ if (error !== null)
292
+ fail(error);
293
+ await channel.close();
294
+ }
295
+ for (const { name, listener } of signals)
296
+ process.off(name, listener);
297
+ process.exitCode = failed ? 1 : (code ?? 1);
298
+ if (!failed && exitSignal !== null)
299
+ process.kill(process.pid, exitSignal);
300
+ }
301
+ await main(process.argv.slice(2));
@@ -0,0 +1,18 @@
1
+ export type RunnerPhase = 'build' | 'active' | 'terminal';
2
+ export interface RunnerPhaseEvent {
3
+ id: number;
4
+ phase: RunnerPhase;
5
+ }
6
+ /** Only complete, direct chains have a known lifecycle. Shell wrappers remain bounded from spawn. */
7
+ export declare function withRunnerLifecycle(command: string): {
8
+ command: string;
9
+ runners: number;
10
+ } | null;
11
+ /** A private supervisor channel, not runner output, grants authority to pause the test clock. */
12
+ export declare function createRunnerLifecycle(runners: number): {
13
+ isTesting: () => boolean;
14
+ error: () => string | null;
15
+ finished: () => boolean;
16
+ accept(line: string): RunnerPhaseEvent | null;
17
+ };
18
+ //# sourceMappingURL=runner-lifecycle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runner-lifecycle.d.ts","sourceRoot":"","sources":["../../../src/executors/bounded-exec/runner-lifecycle.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,WAAW,GAAG,OAAO,GAAG,QAAQ,GAAG,UAAU,CAAC;AAC1D,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,WAAW,CAAC;CACpB;AAED,qGAAqG;AACrG,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAyChG;AAED,iGAAiG;AACjG,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM;IAKjD,SAAS,QAAM,OAAO;IACtB,KAAK,QAAM,MAAM,GAAG,IAAI;IACxB,QAAQ,QAAM,OAAO;IACrB,MAAM,OAAO,MAAM,GAAG,gBAAgB,GAAG,IAAI;EAmChD"}
@@ -0,0 +1,92 @@
1
+ import { fileURLToPath } from 'node:url';
2
+ import { shellWord } from '../../runtime-input.js';
3
+ import { invocationSpan, withInvocationFlag } from './cpu-tokens.js';
4
+ import { GO_TEST } from './verdict.js';
5
+ const CARGO_TEST = /^cargo(?:\s+(?:-\S+|\+\S+))*\s+test(?=\s|$)/;
6
+ const MESSAGE_FORMAT = /(?<![\w-])--message-format(?:=|\s+)(?:"[^"]*"|'[^']*'|[^\s]+)/g;
7
+ /** Only complete, direct chains have a known lifecycle. Shell wrappers remain bounded from spawn. */
8
+ export function withRunnerLifecycle(command) {
9
+ // Redirections could replace the private descriptor; newlines/backticks/escapes need a real shell parser.
10
+ if (/[<>\n\r`\\]/.test(command))
11
+ return null;
12
+ let rest = command.trim();
13
+ const parts = [];
14
+ let runners = 0;
15
+ const hook = fileURLToPath(new URL(import.meta.url.endsWith('.ts') ? './runner-hook.ts' : './runner-hook.js', import.meta.url));
16
+ while (rest !== '') {
17
+ const kind = rest.search(GO_TEST) === 0 ? 'go' : CARGO_TEST.test(rest) ? 'cargo' : null;
18
+ if (kind === null)
19
+ return null;
20
+ const invocation = kind === 'go' ? GO_TEST : CARGO_TEST;
21
+ const { argsEnd } = invocationSpan(rest, invocation);
22
+ let runner = rest.slice(0, argsEnd).trimEnd();
23
+ if (kind === 'cargo') {
24
+ // Only Cargo's own arguments, never those after the libtest separator, select build reporting.
25
+ const separator = /\s--(?:\s|$)/.exec(runner);
26
+ const end = separator?.index ?? runner.length;
27
+ runner =
28
+ withInvocationFlag(runner.slice(0, end), invocation, MESSAGE_FORMAT, '--message-format=json-render-diagnostics', true) + runner.slice(end);
29
+ }
30
+ // The hook owns an inherited duplex descriptor, which Bun's net.Socket({fd}) does not adopt.
31
+ const runtime = process.versions.bun === undefined ? process.execPath : 'node';
32
+ parts.push(`${shellWord(runtime)} ${shellWord(hook)} ${runners} ${kind} ${runner}`);
33
+ runners += 1;
34
+ rest = rest.slice(argsEnd).trimStart();
35
+ if (rest === '')
36
+ break;
37
+ const separator = /^(?:&&|\|\||;)\s*/.exec(rest);
38
+ if (separator === null)
39
+ return null;
40
+ parts.push(separator[0].trim());
41
+ rest = rest.slice(separator[0].length);
42
+ if (rest === '')
43
+ return null;
44
+ }
45
+ return runners === 0 ? null : { command: parts.join(' '), runners };
46
+ }
47
+ /** A private supervisor channel, not runner output, grants authority to pause the test clock. */
48
+ export function createRunnerLifecycle(runners) {
49
+ let current = 0;
50
+ let phase = 'active'; // Shell and supervisor startup are bounded until their build declaration.
51
+ let failure = null;
52
+ return {
53
+ isTesting: () => failure !== null || phase === 'active' || (phase === 'terminal' && current < runners - 1),
54
+ error: () => failure,
55
+ finished: () => phase === 'terminal' && failure === null,
56
+ accept(line) {
57
+ if (failure !== null)
58
+ return null;
59
+ let event;
60
+ try {
61
+ event = JSON.parse(line);
62
+ }
63
+ catch (error) {
64
+ if (!(error instanceof SyntaxError))
65
+ throw error;
66
+ failure = 'invalid JSON on the runner lifecycle channel';
67
+ return null;
68
+ }
69
+ if (typeof event !== 'object' ||
70
+ event === null ||
71
+ !('id' in event) ||
72
+ !('phase' in event) ||
73
+ typeof event.id !== 'number' ||
74
+ !Number.isSafeInteger(event.id) ||
75
+ event.id < current ||
76
+ event.id >= runners ||
77
+ (event.id === current && phase === 'terminal') ||
78
+ (event.phase !== 'build' && event.phase !== 'active' && event.phase !== 'terminal')) {
79
+ failure = 'invalid or retired invocation on the runner lifecycle channel';
80
+ return null;
81
+ }
82
+ // && and || may skip invocations. A new supervisor can advance only after the previous one ended.
83
+ if (event.id !== current && phase !== 'terminal') {
84
+ failure = 'overlapping invocations on a serial runner lifecycle channel';
85
+ return null;
86
+ }
87
+ current = event.id;
88
+ phase = event.phase;
89
+ return { id: current, phase };
90
+ },
91
+ };
92
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EAEtB,KAAK,mBAAmB,EACzB,MAAM,0BAA0B,CAAC;AAkBlC,OAAO,EAKL,yBAAyB,EAiB1B,MAAM,sBAAsB,CAAC;AAsB9B,OAAO,EACL,KAAK,uBAAuB,EAG7B,MAAM,wBAAwB,CAAC;AAchC,OAAO,EAAE,yBAAyB,EAAE,CAAC;AAErC,gEAAgE;AAChE,eAAO,MAAM,aAAa,WAAW,CAAC;AACtC,uEAAuE;AACvE,eAAO,MAAM,mBAAmB,iBAAiB,CAAC;AAmyBlD;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,mBAAmB,EAC3B,WAAW,EAAE,MAAM,EACnB,SAAS,UAAsB,GAC9B,MAAM,EAAE,CAKV;AAkID,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,YAAY,EAAE,MAAM,GAAG,aAAa,CAEvG;AAED,eAAO,MAAM,aAAa,EAAE,aAG3B,CAAC;AAeF;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,EAAE,iBAAiB,CAAC,uBAAuB,CAOxE,CAAC;AACF,eAAO,MAAM,kBAAkB,EAAE,kBAAkB,CAAC,uBAAuB,CAI1E,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAMA,OAAO,EAGL,KAAK,aAAa,EAClB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EAEtB,KAAK,mBAAmB,EACzB,MAAM,0BAA0B,CAAC;AAkBlC,OAAO,EAKL,yBAAyB,EAiB1B,MAAM,sBAAsB,CAAC;AAuB9B,OAAO,EACL,KAAK,uBAAuB,EAG7B,MAAM,wBAAwB,CAAC;AAchC,OAAO,EAAE,yBAAyB,EAAE,CAAC;AAErC,gEAAgE;AAChE,eAAO,MAAM,aAAa,WAAW,CAAC;AACtC,uEAAuE;AACvE,eAAO,MAAM,mBAAmB,iBAAiB,CAAC;AAmyBlD;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,mBAAmB,EAC3B,WAAW,EAAE,MAAM,EACnB,SAAS,UAAsB,GAC9B,MAAM,EAAE,CAKV;AAkID,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,YAAY,EAAE,MAAM,GAAG,aAAa,CAEvG;AAED,eAAO,MAAM,aAAa,EAAE,aAG3B,CAAC;AAeF;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,EAAE,iBAAiB,CAAC,uBAAuB,CAOxE,CAAC;AACF,eAAO,MAAM,kBAAkB,EAAE,kBAAkB,CAAC,uBAAuB,CAI1E,CAAC"}
package/dist/index.js CHANGED
@@ -12,6 +12,7 @@ import { CARGO_CLOSURE_INPUT, cargoClosureInputs, indexedCargoManifests, resolve
12
12
  import { CARGO_TOOLCHAIN_NAMED_INPUT, CARGO_TOOLCHAIN_PIN_INPUTS, cargoRuntimeInput, } from './cargo-toolchain-policy.js';
13
13
  import { attributeCargoWorkspacePackages, CARGO_TEST_ARCHIVE_FILE, CARGO_TEST_ARCHIVE_TARGET, CARGO_TEST_COMPILE_TARGET, CARGO_TEST_EXCEPTIONS_SUFFIX, CARGO_TEST_TARGET, cargoPackageTestInputs, cargoTestGroupTargetName, cargoTestPackageTargetName, createCargoInputsCache, exceptionalTestFilter, listCargoCrossTestTargets, listCargoWorkspacePackages, NEXTEST_REPO_CONFIG_PATH, nextestToolConfigArg, PLUGIN_NEXTEST_CONFIG_PATH, serialTestGroups, } from './cargo-workspace.js';
14
14
  import { CARGO_CROSS_LINT_COMMAND, CARGO_CROSS_LINT_TARGET, CARGO_FETCH_COMMAND, CARGO_FETCH_TARGET, CARGO_LINT_CLIPPY_COMMAND, cargoCrossTestArchiveFile, cargoCrossTestArchiveTargetName, cargoCrossTestTargetName, cargoFrozen, runsFrozenCargo, } from './cross-check-policy.js';
15
+ import { ALL_VERDICTS_EXECUTOR } from './executors/all-verdicts/schema.js';
15
16
  import { closeRunCpuPool, serveRunCpuPool } from './executors/bounded-exec/run-cpu-pool.js';
16
17
  import { hashVersionlessCrateManifests } from './manifest-hash.js';
17
18
  import { EXTRACTED_BINARIES_METADATA, EXTRACTED_CARGO_METADATA, EXTRACTED_TARGET_DIR, NEXTEST_EXTRACT_BIN, } from './nextest-extraction.js';
@@ -1594,9 +1595,11 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
1594
1595
  validationTargets.push(SHARD_TIMINGS_TARGET);
1595
1596
  }
1596
1597
  const { commands: lintCommands, inputs: lintProgramInputs, eslintFileSet, } = projectLintCommands(projectRoot, workspaceRoot, eslintSources);
1598
+ // Every lint tool reaches its own verdict in one run: run-commands would end the
1599
+ // other tools once one fails, and their findings would surface a run later.
1597
1600
  if (validationTargets.length > 0 || lintCommands.length > 0) {
1598
1601
  targets.lint = {
1599
- executor: lintCommands.length > 0 ? 'nx:run-commands' : 'nx:noop',
1602
+ executor: lintCommands.length > 0 ? ALL_VERDICTS_EXECUTOR : 'nx:noop',
1600
1603
  ...(eslintFileSet === null ? {} : { metadata: { eslintFileSet } }),
1601
1604
  cache: true,
1602
1605
  dependsOn: validationTargets,
@@ -1,7 +1,12 @@
1
+ import type { NxJsonConfiguration } from 'nx/src/config/nx-json';
1
2
  import type { Task, TaskGraph } from 'nx/src/config/task-graph';
3
+ import type { expandOutputs } from 'nx/src/native';
2
4
  import type { PostTasksExecutionContext, PreTasksExecutionContext } from 'nx/src/project-graph/plugins/public-api';
3
- import { type EnsureBuiltProducerReceipt, type EnsureBuiltReceipt } from './ensure-built.js';
5
+ import type { readModulePackageJsonWithoutFallbacks } from 'nx/src/utils/package-json';
6
+ import { type EnsureBuiltProducerReceipt, type EnsureBuiltProducerTool, type EnsureBuiltReceipt } from './ensure-built.js';
4
7
  import { type ReceiptEvidence } from './ensure-built-receipt.js';
8
+ import { type NodeWitness } from './ensure-built-witness.js';
9
+ import { type GraphInputContractProof } from './graph-input-contract.js';
5
10
  export interface ProducerReceiptsOptions {
6
11
  readonly producerReceipts?: {
7
12
  readonly targets: readonly string[];
@@ -17,6 +22,24 @@ export interface ProducerReceiptsOptions {
17
22
  * is no source or input of the run that publishes it.
18
23
  */
19
24
  export declare const PRODUCER_RECORDS: string;
25
+ /**
26
+ * The installed closure of Nx, this plugin, TypeScript and the local graph plugins as one producer tool: every node of
27
+ * the installation census, and the module namespace each of its resolutions searched. The census is not proved by a
28
+ * stat pass of its own: each of its nodes is stat-ed again as a tool node, a file whose stamp moved since the census
29
+ * read it is read again, and the capture's stability check refuses a tool node that moves before publication.
30
+ *
31
+ * Its value is a function of the closure's node stamps (device, inode, mode, size, mtime and ctime), not of the moment
32
+ * it is taken. So every node is stat-ed on every call, and that is what makes reuse safe: a file whose stamp is the one
33
+ * the census recorded at `record` read it at keeps that census's hash, so an unchanged closure reads no file bytes,
34
+ * while a file rewritten in place, as a developer-linked package's rebuilt `dist` is with no lockfile or
35
+ * `node_modules` change, moves its ctime and is read again. The tool's nodes take the hash this census read
36
+ * ({@link censusFileHashes}) instead of reading each file a second time: a write landing between two reads could make
37
+ * them disagree.
38
+ */
39
+ export declare function installedClosureTool(root: string, nxJson: NxJsonConfiguration, localInputs: GraphInputContractProof | null, record: string, resolvePackage: typeof readModulePackageJsonWithoutFallbacks, expand: typeof expandOutputs, census: {
40
+ readonly start: bigint;
41
+ readonly observed: NodeWitness[];
42
+ }): ReceiptEvidence<EnsureBuiltProducerTool>;
20
43
  export declare function completedProducerTasks(graph: TaskGraph, results: PostTasksExecutionContext['taskResults']): ReceiptEvidence<readonly Task[]>;
21
44
  export declare function producerEnvironmentIdentity(inputs: EnsureBuiltProducerReceipt['inputs']): string;
22
45
  export declare function preTasksExecution(options: ProducerReceiptsOptions | undefined, context: PreTasksExecutionContext): Promise<void>;