@smoothbricks/nx-plugin 0.5.5 → 0.5.6

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 (43) hide show
  1. package/README.md +50 -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/bounded-exec/executor.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/executor.js +48 -11
  13. package/dist/executors/bounded-exec/progress.d.ts +1 -3
  14. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  15. package/dist/executors/bounded-exec/progress.js +1 -2
  16. package/dist/executors/bounded-exec/runner-hook.d.ts +2 -0
  17. package/dist/executors/bounded-exec/runner-hook.d.ts.map +1 -0
  18. package/dist/executors/bounded-exec/runner-hook.js +301 -0
  19. package/dist/executors/bounded-exec/runner-lifecycle.d.ts +18 -0
  20. package/dist/executors/bounded-exec/runner-lifecycle.d.ts.map +1 -0
  21. package/dist/executors/bounded-exec/runner-lifecycle.js +92 -0
  22. package/dist/producer-receipts.d.ts +24 -1
  23. package/dist/producer-receipts.d.ts.map +1 -1
  24. package/dist/producer-receipts.js +91 -60
  25. package/managed/raw/git-format-staged.yml +16 -3
  26. package/managed/raw/patches/nx@23.2.1.patch +397 -33
  27. package/package.json +2 -2
  28. package/src/ensure-built-receipt.ts +8 -2
  29. package/src/ensure-built-witness.ts +29 -1
  30. package/src/ensure-built.ts +39 -7
  31. package/src/executors/bounded-exec/cargo-progress-host.test.ts +332 -0
  32. package/src/executors/bounded-exec/executor.test.ts +89 -42
  33. package/src/executors/bounded-exec/executor.ts +45 -11
  34. package/src/executors/bounded-exec/progress.test.ts +1 -14
  35. package/src/executors/bounded-exec/progress.ts +2 -5
  36. package/src/executors/bounded-exec/runner-hook.test.ts +193 -0
  37. package/src/executors/bounded-exec/runner-hook.ts +292 -0
  38. package/src/executors/bounded-exec/runner-lifecycle.test.ts +82 -0
  39. package/src/executors/bounded-exec/runner-lifecycle.ts +102 -0
  40. package/src/nx-daemon-start-never.test.ts +137 -0
  41. package/src/producer-receipts.test.ts +99 -0
  42. package/src/producer-receipts.ts +111 -58
  43. package/test-timings.json +4 -0
@@ -2,6 +2,8 @@ import { type ChildProcess, 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
 
7
9
  import {
@@ -34,6 +36,7 @@ import {
34
36
  RamTempVolume,
35
37
  ramTempPaths,
36
38
  } from './ram-temp.js';
39
+ import { createRunnerLifecycle, withRunnerLifecycle } from './runner-lifecycle.js';
37
40
  import type { BoundedExecOptions } from './schema.js';
38
41
  import {
39
42
  type BoundedExecRecord,
@@ -458,13 +461,18 @@ export async function runBoundedExec(
458
461
  ...sized.env,
459
462
  ...(pool?.environment ?? {}),
460
463
  };
464
+ const hooked = process.platform === 'win32' ? null : withRunnerLifecycle(command);
465
+ const lifecycle = hooked === null ? null : createRunnerLifecycle(hooked.runners);
466
+ let lifecycleFailure: string | null = null;
467
+ const isTesting = (): boolean => lifecycleFailure !== null || (lifecycle?.isTesting() ?? true);
461
468
  const startedAt = Date.now();
462
469
  let child: ChildProcess;
463
470
  try {
464
- child = spawn(command, [], {
471
+ child = spawn(hooked?.command ?? command, [], {
465
472
  cwd,
466
473
  env: mergeEnv(lease, env),
467
474
  shell: true,
475
+ stdio: hooked === null ? 'pipe' : ['pipe', 'pipe', 'pipe', 'pipe'],
468
476
  detached: process.platform !== 'win32',
469
477
  windowsHide: true,
470
478
  });
@@ -542,7 +550,7 @@ export async function runBoundedExec(
542
550
  idleTimer = setTimeout(() => expire({ kind: 'idle', limitMs, tests: progress.inFlight() }, queuedMs), leftMs);
543
551
  };
544
552
  const armIdleTimer = (): void => {
545
- if (idleTimeoutMs === undefined || state.settled || state.expiry !== null || !progress.isTesting()) {
553
+ if (idleTimeoutMs === undefined || state.settled || state.expiry !== null || !isTesting()) {
546
554
  return;
547
555
  }
548
556
  idleLeftMs = idleTimeoutMs;
@@ -577,7 +585,7 @@ export async function runBoundedExec(
577
585
  const waitedMs = Date.now() - queuedSinceMs;
578
586
  queuedMs += waitedMs;
579
587
  queuedSinceMs = null;
580
- if (!progress.isTesting()) {
588
+ if (!isTesting()) {
581
589
  appendStderr(`bounded-exec: host CPU wait ended after ${waitedMs}ms; no test runner is active\n`);
582
590
  return;
583
591
  }
@@ -591,10 +599,8 @@ export async function runBoundedExec(
591
599
  // Only the CHILD's own output is read for progress. The diagnostics below go
592
600
  // through appendStderr as well, and re-arming on those would let a teardown
593
601
  // message extend the very bound that just fired.
594
- const scanProgress = (stream: 'stdout' | 'stderr', text: string): void => {
595
- const wasTesting = progress.isTesting();
596
- const finished = progress.scan(stream, text);
597
- if (!progress.isTesting()) {
602
+ const updateIdle = (finished: boolean, wasTesting: boolean): void => {
603
+ if (!isTesting()) {
598
604
  clearTimeout(idleTimer);
599
605
  cancelCapture();
600
606
  idleCapture = undefined;
@@ -602,6 +608,34 @@ export async function runBoundedExec(
602
608
  armIdleTimer();
603
609
  }
604
610
  };
611
+ const scanProgress = (stream: 'stdout' | 'stderr', text: string): void => {
612
+ updateIdle(progress.scan(stream, text), isTesting());
613
+ };
614
+ if (lifecycle !== null) {
615
+ const channel = child.stdio[3];
616
+ if (!(channel instanceof Duplex)) throw new Error('runner lifecycle requires a duplex child descriptor');
617
+ const failLifecycle = (detail: string, wasTesting = isTesting()): void => {
618
+ if (lifecycleFailure !== null || state.expiry !== null) return;
619
+ lifecycleFailure = detail;
620
+ appendStderr(`bounded-exec: runner lifecycle failed: ${detail}\n`);
621
+ updateIdle(false, wasTesting);
622
+ };
623
+ createInterface({ input: channel }).on('line', (line) => {
624
+ const wasTesting = isTesting();
625
+ const event = lifecycle.accept(line);
626
+ if (event === null) {
627
+ failLifecycle(lifecycle.error() ?? 'invalid lifecycle event', wasTesting);
628
+ return;
629
+ }
630
+ updateIdle(false, wasTesting);
631
+ // Tool startup and native lifecycle-bearing stdout wait for their phase acknowledgement.
632
+ channel.write(`${JSON.stringify(event)}\n`);
633
+ });
634
+ channel.on('error', (error) => failLifecycle(error.message));
635
+ channel.on('end', () => {
636
+ if (!lifecycle.finished()) failLifecycle('supervisor channel ended without an owned terminal event');
637
+ });
638
+ }
605
639
  child.stdout?.on('data', (chunk: Buffer | string) => {
606
640
  const text = chunk.toString();
607
641
  scanProgress('stdout', text);
@@ -661,7 +695,7 @@ export async function runBoundedExec(
661
695
  state.settled = true;
662
696
  resolve({ code: 1, signal: null });
663
697
  });
664
- child.once('exit', (code, signal) => {
698
+ child.once(hooked === null ? 'exit' : 'close', (code: number | null, signal: NodeJS.Signals | null) => {
665
699
  state.settled = true;
666
700
  resolve({ code, signal });
667
701
  });
@@ -806,9 +840,9 @@ export async function runBoundedExec(
806
840
  }
807
841
  await releaseLease();
808
842
 
809
- // A run whose sub-pool did not close cleanly fails even when the command exited 0, and whatever bound also fired:
810
- // its verdict is an ordinary failure, never a bound.
811
- const operationalFailure = state.poolCloseError !== null;
843
+ // Broken resource ownership or a lost native lifecycle fails even if a shell branch later exited 0:
844
+ // its verdict is an ordinary operational failure, never a successful or silently unbounded run.
845
+ const operationalFailure = state.poolCloseError !== null || lifecycleFailure !== null;
812
846
  await recordRun(code, state.expiry, Date.now() - startedAt, operationalFailure, seen);
813
847
 
814
848
  return {
@@ -25,26 +25,20 @@ describe('run progress', () => {
25
25
  expect(progress.inFlight()).toEqual(['example/second TestHang']);
26
26
  });
27
27
 
28
- it('counts only active Go package intervals, including startup and parallel packages', () => {
28
+ it('tracks Go package startup and parallel packages until their terminal reports', () => {
29
29
  const progress = createRunProgress('go test ./...');
30
- expect(progress.isTesting()).toBe(false);
31
30
  progress.scan('stdout', '{"Action":"start","Package":"first"}\n{"Action":"start","Package":"second"}\n');
32
- expect(progress.isTesting()).toBe(true);
33
31
  expect(progress.inFlight()).toEqual(['first', 'second']);
34
32
  progress.scan('stdout', '{"Action":"pass","Package":"first"}\n');
35
- expect(progress.isTesting()).toBe(true);
36
33
  expect(progress.inFlight()).toEqual(['second']);
37
34
  progress.scan('stdout', '{"Action":"fail","Package":"second"}\n');
38
- expect(progress.isTesting()).toBe(false);
39
35
  expect(progress.inFlight()).toEqual([]);
40
36
  });
41
37
 
42
38
  it('buffers split Go events and does not mistake test output for lifecycle events', () => {
43
39
  const progress = createRunProgress('go test ./...');
44
40
  expect(progress.scan('stdout', '{"Action":"sta')).toBe(false);
45
- expect(progress.isTesting()).toBe(false);
46
41
  expect(progress.scan('stdout', 'rt","Package":"example"}\n')).toBe(false);
47
- expect(progress.isTesting()).toBe(true);
48
42
  expect(progress.scan('stdout', '{"Action":"run","Package":"example","Test":"TestHang"}\n')).toBe(false);
49
43
  expect(progress.scan('stdout', '{"Action":"pause","Package":"example","Test":"TestHang"}\n')).toBe(false);
50
44
  expect(progress.inFlight()).toEqual(['example TestHang']);
@@ -57,13 +51,6 @@ describe('run progress', () => {
57
51
  expect(progress.scan('stdout', '{"Action":"skip","Package":"example","Test":"TestHang"}\n')).toBe(true);
58
52
  expect(progress.inFlight()).toEqual(['example']);
59
53
  expect(progress.scan('stdout', '{"Action":"pass","Package":"example"}\n')).toBe(true);
60
- expect(progress.isTesting()).toBe(false);
61
- });
62
-
63
- it('keeps the existing watchdog for commands without a complete Go lifecycle contract', () => {
64
- for (const command of ['bun test src', 'cargo nextest run', 'cargo test', 'go test ./... && bun test src']) {
65
- expect(createRunProgress(command).isTesting()).toBe(true);
66
- }
67
54
  });
68
55
 
69
56
  it('counts a nextest status line with its run counter as a finished test, and a SLOW line as none', () => {
@@ -23,15 +23,13 @@ import { BUN_TEST, GO_TEST, NEXTEST_RUN } from './verdict.js';
23
23
  * `fail`, or `skip` ends that execution interval. Test terminals re-arm the bound, but `output` and
24
24
  * `build-output` never do. Initial and inter-package builds are outside an active test interval.
25
25
  *
26
- * A command without a recognised execution lifecycle remains bounded from spawn.
26
+ * Execution phases are owned by the private supervisor channel in runner-lifecycle.ts, never these text reports.
27
27
  */
28
28
  export interface RunProgress {
29
29
  /** One chunk of one of the command's output streams; true when it reports a finished test or Go package. */
30
30
  scan(stream: OutputStream, chunk: string): boolean;
31
31
  /** The tests the runner reported running and has not reported finished, in the order it first named them. */
32
32
  inFlight(): string[];
33
- /** Whether test idle time is currently chargeable; unknown runner lifecycles stay chargeable. */
34
- isTesting(): boolean;
35
33
  }
36
34
 
37
35
  export type OutputStream = 'stdout' | 'stderr';
@@ -68,7 +66,7 @@ function goTestEvent(text: string): GoTestEvent | null {
68
66
  }
69
67
  }
70
68
 
71
- /** Defer only a direct Go command/chain; an unknown shell wrapper or another runner keeps its existing bound. */
69
+ /** A direct Go chain reports tests only through JSON; other commands may also carry text runner reports. */
72
70
  function onlyGoTests(command: string): boolean {
73
71
  let rest = command.trim();
74
72
  while (rest.search(GO_TEST) === 0) {
@@ -170,7 +168,6 @@ export function createRunProgress(command = ''): RunProgress {
170
168
  tests.size === 0 ? [name] : [...tests].map((test) => `${name} ${test}`),
171
169
  ),
172
170
  ],
173
- isTesting: () => !goLifecycle || packages.size > 0,
174
171
  };
175
172
  }
176
173
 
@@ -0,0 +1,193 @@
1
+ import { afterEach, describe, expect, it } from 'bun:test';
2
+ import { type ChildProcess, spawn } from 'node:child_process';
3
+ import { createInterface } from 'node:readline';
4
+ import { Duplex, type Readable } from 'node:stream';
5
+ import { fileURLToPath } from 'node:url';
6
+
7
+ const hook = fileURLToPath(new URL('./runner-hook.ts', import.meta.url));
8
+ const children = new Set<ChildProcess>();
9
+
10
+ afterEach(() => {
11
+ for (const child of children) child.kill('SIGKILL');
12
+ children.clear();
13
+ });
14
+
15
+ async function collect(stream: Readable): Promise<Buffer> {
16
+ const chunks: Buffer[] = [];
17
+ for await (const chunk of stream) {
18
+ const bytes: unknown = chunk;
19
+ if (!Buffer.isBuffer(bytes)) throw new Error('expected subprocess bytes');
20
+ chunks.push(bytes);
21
+ }
22
+ return Buffer.concat(chunks);
23
+ }
24
+
25
+ async function runHook(
26
+ runner: 'cargo' | 'go',
27
+ executable: string,
28
+ args: string[],
29
+ acknowledge: (line: string) => string = (line) => `${line}\n`,
30
+ ) {
31
+ // Node's standalone TypeScript loader needs no project preloader or validation transform.
32
+ const child = spawn('node', [hook, '7', runner, executable, ...args], {
33
+ stdio: ['ignore', 'pipe', 'pipe', 'pipe'],
34
+ });
35
+ children.add(child);
36
+ const closed = Promise.withResolvers<{ code: number | null; signal: NodeJS.Signals | null }>();
37
+ child.once('error', closed.reject);
38
+ child.once('close', (code, signal) => {
39
+ children.delete(child);
40
+ closed.resolve({ code, signal });
41
+ });
42
+ const control = child.stdio[3];
43
+ if (!(control instanceof Duplex) || child.stdout === null || child.stderr === null) {
44
+ throw new Error('runner-hook fixture needs stdout, stderr, and duplex descriptor 3');
45
+ }
46
+ const phases: unknown[] = [];
47
+ const protocol = async (): Promise<void> => {
48
+ try {
49
+ for await (const line of createInterface({ input: control })) {
50
+ const message: unknown = JSON.parse(line);
51
+ phases.push(message);
52
+ control.write(acknowledge(line));
53
+ }
54
+ } finally {
55
+ control.destroy();
56
+ }
57
+ };
58
+ const [status, stdout, stderr] = await Promise.all([
59
+ closed.promise,
60
+ collect(child.stdout),
61
+ collect(child.stderr),
62
+ protocol(),
63
+ ]);
64
+ return { ...status, stdout, stderr, phases };
65
+ }
66
+
67
+ /** A real process emitting exact protocol fragments, not a parser imported into the test process. */
68
+ function emit(stdout: readonly string[], stderr = '', ending = 'process.exitCode = 0;'): string {
69
+ return [
70
+ "const { writeSync } = require('node:fs');",
71
+ `writeSync(2, ${JSON.stringify(stderr)});`,
72
+ `for (const fragment of ${JSON.stringify(stdout)}) writeSync(1, fragment);`,
73
+ ending,
74
+ ].join('\n');
75
+ }
76
+
77
+ describe('standalone native runner hook', () => {
78
+ it('accepts Cargo build-finished(false) once, ignores compiler/text/nested terminals, and preserves bytes and status', async () => {
79
+ const stdout = [
80
+ '{"reason":"compiler-artifact","fresh":true}\n',
81
+ '{"reason":"build-finished","success":"true"}\n',
82
+ '{not JSON}\n',
83
+ '{"reason":"build-fin',
84
+ 'ished","success":false}\n',
85
+ 'test result: ok. 1 passed; 0 failed\n',
86
+ '{"Action":"pass","Package":"nested"}\n',
87
+ '{"reason":"build-finished","success":true}\n',
88
+ 'custom harness teardown without newline',
89
+ ];
90
+ const stderr = '{"reason":"build-finished","success":true}\ncompiler diagnostic\n';
91
+ const program =
92
+ "require('node:fs').writeSync(1, Buffer.from([0, 255, 128, 10]));\n" +
93
+ emit(stdout, stderr, 'process.exitCode = 37;');
94
+ const result = await runHook('cargo', process.execPath, ['-e', program]);
95
+ expect(result.code).toBe(37);
96
+ expect(result.signal).toBeNull();
97
+ expect(result.stdout).toEqual(Buffer.concat([Buffer.from([0, 255, 128, 10]), Buffer.from(stdout.join(''))]));
98
+ expect(result.stderr).toEqual(Buffer.from(stderr));
99
+ expect(result.phases).toEqual([
100
+ { id: 7, phase: 'build' },
101
+ { id: 7, phase: 'active' },
102
+ { id: 7, phase: 'terminal' },
103
+ ]);
104
+ });
105
+
106
+ it('tracks overlapping Go packages, not test terminals, stderr, build output, or nested output', async () => {
107
+ const stdout = [
108
+ '{"Action":"build-output","ImportPath":"one","Output":"linking\\n"}\n',
109
+ '{"Action":"sta',
110
+ 'rt","Package":"one"}\n',
111
+ '{"Action":"start","Package":"two"}\n',
112
+ '{"Action":"pass","Package":"one","Test":"TestDone"}\n',
113
+ '{"Action":"output","Package":"one","Output":"{\\"Action\\":\\"pass\\",\\"Package\\":\\"two\\"}\\n"}\n',
114
+ '{"Action":"fail","Package":"one"}\n',
115
+ '{"Action":"skip","Package":"two"}\n',
116
+ '{"Action":"start","Package":"three"}\n',
117
+ '{"Action":"pass","Package":"three"}',
118
+ ];
119
+ const stderr = '{"Action":"start","Package":"stderr-is-not-native"}\n';
120
+ const result = await runHook('go', process.execPath, ['-e', emit(stdout, stderr)]);
121
+ expect(result.code).toBe(0);
122
+ expect(result.stdout).toEqual(Buffer.from(stdout.join('')));
123
+ expect(result.stderr).toEqual(Buffer.from(stderr));
124
+ expect(result.phases).toEqual([
125
+ { id: 7, phase: 'build' },
126
+ { id: 7, phase: 'active' },
127
+ { id: 7, phase: 'build' },
128
+ { id: 7, phase: 'active' },
129
+ { id: 7, phase: 'build' },
130
+ { id: 7, phase: 'terminal' },
131
+ ]);
132
+ });
133
+
134
+ it('preserves a child termination signal after draining output and acknowledging terminal', async () => {
135
+ const stdout = ['{"reason":"build-finished","success":true}\n', 'last stdout\n'];
136
+ const result = await runHook('cargo', process.execPath, [
137
+ '-e',
138
+ emit(stdout, 'last stderr\n', "process.kill(process.pid, 'SIGTERM');"),
139
+ ]);
140
+ expect(result.code).toBeNull();
141
+ expect(result.signal).toBe('SIGTERM');
142
+ expect(result.stdout).toEqual(Buffer.from(stdout.join('')));
143
+ expect(result.stderr.toString()).toBe('last stderr\n');
144
+ expect(result.phases).toEqual([
145
+ { id: 7, phase: 'build' },
146
+ { id: 7, phase: 'active' },
147
+ { id: 7, phase: 'terminal' },
148
+ ]);
149
+ });
150
+
151
+ it('reports spawn failure, acknowledges terminal, and exits nonzero', async () => {
152
+ const result = await runHook('cargo', `/runner-hook-missing-executable-${process.pid}`, []);
153
+ expect(result.code).toBe(1);
154
+ expect(result.signal).toBeNull();
155
+ expect(result.stdout.length).toBe(0);
156
+ expect(result.stderr.toString()).toContain('runner-hook:');
157
+ expect(result.stderr.toString()).toContain('runner-hook-missing-executable-');
158
+ expect(result.phases).toEqual([
159
+ { id: 7, phase: 'build' },
160
+ { id: 7, phase: 'terminal' },
161
+ ]);
162
+ });
163
+
164
+ it('does not start the child until its initial build phase is acknowledged correctly', async () => {
165
+ const result = await runHook(
166
+ 'cargo',
167
+ process.execPath,
168
+ ['-e', emit(['child must not run\n'])],
169
+ () => '{"id":8,"phase":"build"}\n',
170
+ );
171
+ expect(result.code).toBe(1);
172
+ expect(result.stdout.length).toBe(0);
173
+ expect(result.stderr.toString()).toContain('unexpected lifecycle acknowledgement');
174
+ expect(result.phases).toEqual([{ id: 7, phase: 'build' }]);
175
+ });
176
+
177
+ it('withholds the active record and subsequent stdout when its acknowledgement is invalid', async () => {
178
+ const compiler = '{"reason":"compiler-artifact","fresh":true}\n';
179
+ const result = await runHook(
180
+ 'cargo',
181
+ process.execPath,
182
+ ['-e', emit([compiler, '{"reason":"build-finished","success":true}\n', 'fixture ready\n'])],
183
+ (line) => (line.includes('"phase":"active"') ? '{"id":8,"phase":"active"}\n' : `${line}\n`),
184
+ );
185
+ expect(result.code).toBe(1);
186
+ expect(result.stdout.toString()).toBe(compiler);
187
+ expect(result.stderr.toString()).toContain('unexpected lifecycle acknowledgement');
188
+ expect(result.phases).toEqual([
189
+ { id: 7, phase: 'build' },
190
+ { id: 7, phase: 'active' },
191
+ ]);
192
+ });
193
+ });