@smoothbricks/nx-plugin 0.5.2 → 0.5.4

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 (31) hide show
  1. package/README.md +40 -9
  2. package/dist/cargo-source-hash.d.ts +2 -2
  3. package/dist/cargo-source-hash.d.ts.map +1 -1
  4. package/dist/cargo-source-hash.js +106 -46
  5. package/dist/executors/bounded-exec/cpu-tokens.d.ts +13 -0
  6. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
  7. package/dist/executors/bounded-exec/cpu-tokens.js +9 -9
  8. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  9. package/dist/executors/bounded-exec/executor.js +20 -6
  10. package/dist/executors/bounded-exec/progress.d.ts +8 -3
  11. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/progress.js +100 -10
  13. package/dist/executors/bounded-exec/verdict.d.ts +5 -4
  14. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  15. package/dist/executors/bounded-exec/verdict.js +1 -0
  16. package/dist/testing.js +1 -1
  17. package/managed/raw/patches/nx@23.2.1.patch +208 -45
  18. package/package.json +2 -2
  19. package/src/cargo-source-hash.test.ts +143 -2
  20. package/src/cargo-source-hash.ts +108 -44
  21. package/src/executors/bounded-exec/cpu-tokens.ts +15 -9
  22. package/src/executors/bounded-exec/executor.test.ts +26 -23
  23. package/src/executors/bounded-exec/executor.ts +19 -4
  24. package/src/executors/bounded-exec/go-progress-host.test.ts +171 -0
  25. package/src/executors/bounded-exec/progress.test.ts +73 -0
  26. package/src/executors/bounded-exec/progress.ts +95 -12
  27. package/src/executors/bounded-exec/verdict.ts +5 -4
  28. package/src/nx-daemon-start-never.test.ts +155 -0
  29. package/src/nx-task-output-lease.test.ts +136 -0
  30. package/src/testing.ts +1 -1
  31. package/test-timings.json +92 -91
@@ -0,0 +1,171 @@
1
+ import { afterEach, describe, expect, it, jest } from 'bun:test';
2
+ import { chmod, mkdir, rm, writeFile } from 'node:fs/promises';
3
+ import { createServer, type Socket } from 'node:net';
4
+ import { join } from 'node:path';
5
+ import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from '../../bounded-test-policy.js';
6
+ import { ownedSocketDirectory } from '../../testing.js';
7
+ import { createProcessTreeKiller, runBoundedExec } from './executor.js';
8
+
9
+ const roots: string[] = [];
10
+
11
+ /** Real Go build/run events; only the executor's clock is controlled, never compiler speed. */
12
+ describe('Go test progress host proofs', () => {
13
+ afterEach(async () => {
14
+ jest.useRealTimers();
15
+ await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
16
+ });
17
+
18
+ it('excludes held initial linking and inter-package linking from test idle time', async () => {
19
+ const root = await goFixture();
20
+ for (const name of ['first', 'second']) {
21
+ await mkdir(join(root, name));
22
+ await writeFile(
23
+ join(root, name, 'ready_test.go'),
24
+ `package ${name}\nimport "testing"\nfunc TestReady(t *testing.T) {}\n`,
25
+ );
26
+ }
27
+ const endpoint = join(root, 'runner.sock');
28
+ const wrapper = join(root, 'tool.cjs');
29
+ await writeFile(
30
+ wrapper,
31
+ [
32
+ '#!/usr/bin/env node',
33
+ "const { spawnSync } = require('node:child_process');",
34
+ "const { connect } = require('node:net');",
35
+ "const { basename } = require('node:path');",
36
+ 'const [tool, ...args] = process.argv.slice(2);',
37
+ 'function run() {',
38
+ " const result = spawnSync(tool, args, { stdio: 'inherit' });",
39
+ ' if (result.error) throw result.error;',
40
+ ' if (result.signal) process.kill(process.pid, result.signal);',
41
+ ' else process.exit(result.status ?? 1);',
42
+ '}',
43
+ "if (basename(tool) !== 'link' || args.some(arg => arg.startsWith('-V'))) run();",
44
+ 'else {',
45
+ ` const socket = connect(${JSON.stringify(endpoint)}, () => socket.write('link-ready\\n'));`,
46
+ " socket.once('error', error => { throw error; });",
47
+ " let answer = '';",
48
+ " socket.on('data', data => { answer += data.toString(); });",
49
+ " socket.once('end', () => {",
50
+ " if (answer !== 'go\\n') throw new Error('unexpected link release');",
51
+ ' socket.end(); run();',
52
+ ' });',
53
+ '}',
54
+ '',
55
+ ].join('\n'),
56
+ );
57
+ await chmod(wrapper, 0o700);
58
+ const sockets = new Set<Socket>();
59
+ const exchanges: Promise<unknown>[] = [];
60
+ const firstFinished = Promise.withResolvers<boolean>();
61
+ let links = 0;
62
+ const server = createServer((socket) => {
63
+ sockets.add(socket);
64
+ socket.once('close', () => sockets.delete(socket));
65
+ exchanges.push(
66
+ new Promise<void>((resolve, reject) => {
67
+ socket.once('error', reject);
68
+ let request = '';
69
+ const received = (data: Buffer) => {
70
+ request += data.toString();
71
+ if (!request.includes('\n')) return;
72
+ socket.off('data', received);
73
+ void (async () => {
74
+ expect(request).toBe('link-ready\n');
75
+ links += 1;
76
+ if (links === 2) expect(await firstFinished.promise).toBe(true);
77
+ // The linker is held at a real IPC barrier while virtual idle time crosses the
78
+ // unchanged production bound twice. No test binary exists for this package yet.
79
+ jest.advanceTimersByTime(2 * BOUNDED_TEST_IDLE_TIMEOUT_MS);
80
+ socket.end('go\n');
81
+ resolve();
82
+ })().catch(reject);
83
+ };
84
+ socket.on('data', received);
85
+ }).catch((error: unknown) => error),
86
+ );
87
+ });
88
+ await new Promise<void>((resolve) => server.listen(endpoint, resolve));
89
+ const stdout = process.stdout.write;
90
+ const stderr = process.stderr.write;
91
+ let output = '';
92
+ process.stdout.write = (chunk) => {
93
+ output += chunk.toString();
94
+ if (output.includes('"Action":"pass","Package":"example/first","Elapsed":')) firstFinished.resolve(true);
95
+ return true;
96
+ };
97
+ process.stderr.write = () => true;
98
+ jest.useFakeTimers();
99
+ try {
100
+ const command = ['first', 'second']
101
+ .map((name) => `go test -p=1 -count=1 -toolexec=${JSON.stringify(wrapper)} ./${name}`)
102
+ .join(' && ');
103
+ const result = await runBoundedExec(
104
+ { command, idleTimeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS, killAfterMs: 0 },
105
+ { root },
106
+ createProcessTreeKiller(),
107
+ null,
108
+ null,
109
+ );
110
+ firstFinished.resolve(false);
111
+ const exchangeErrors = (await Promise.all(exchanges)).filter((error) => error !== undefined);
112
+ expect(result.success, result.terminalOutput).toBe(true);
113
+ expect(exchangeErrors).toEqual([]);
114
+ expect(links).toBe(2);
115
+ expect(result.terminalOutput).not.toContain('Command made no progress');
116
+ } finally {
117
+ firstFinished.resolve(false);
118
+ jest.useRealTimers();
119
+ process.stdout.write = stdout;
120
+ process.stderr.write = stderr;
121
+ for (const socket of sockets) socket.destroy();
122
+ await new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())));
123
+ }
124
+ });
125
+
126
+ it('ends a real hanging Go test when its unchanged active-test budget expires', async () => {
127
+ const root = await goFixture();
128
+ await writeFile(
129
+ join(root, 'hang_test.go'),
130
+ 'package example\nimport ("runtime"; "testing")\nfunc TestHang(t *testing.T) { for { runtime.Gosched() } }\n',
131
+ );
132
+ const stdout = process.stdout.write;
133
+ const stderr = process.stderr.write;
134
+ let output = '';
135
+ let advanced = false;
136
+ process.stdout.write = (chunk) => {
137
+ output += chunk.toString();
138
+ if (!advanced && output.includes('"Action":"run","Package":"example","Test":"TestHang"')) {
139
+ advanced = true;
140
+ queueMicrotask(() => jest.advanceTimersByTime(BOUNDED_TEST_IDLE_TIMEOUT_MS));
141
+ }
142
+ return true;
143
+ };
144
+ process.stderr.write = () => true;
145
+ jest.useFakeTimers();
146
+ try {
147
+ const result = await runBoundedExec(
148
+ { command: 'go test -p=1 -count=1 .', idleTimeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS, killAfterMs: 0 },
149
+ { root },
150
+ createProcessTreeKiller(),
151
+ null,
152
+ null,
153
+ );
154
+ expect(advanced).toBe(true);
155
+ expect(result.success).toBe(false);
156
+ expect(result.terminalOutput).toContain(`no test finished for ${BOUNDED_TEST_IDLE_TIMEOUT_MS}ms`);
157
+ expect(result.terminalOutput).toContain('still running: example TestHang');
158
+ } finally {
159
+ jest.useRealTimers();
160
+ process.stdout.write = stdout;
161
+ process.stderr.write = stderr;
162
+ }
163
+ });
164
+ });
165
+
166
+ async function goFixture(): Promise<string> {
167
+ const root = await ownedSocketDirectory('go-progress-', 'runner.sock');
168
+ roots.push(root);
169
+ await writeFile(join(root, 'go.mod'), 'module example\n\ngo 1.24\n');
170
+ return root;
171
+ }
@@ -3,6 +3,69 @@ import { describe, expect, it } from 'bun:test';
3
3
  import { createRunProgress, withProgressReport } from './progress.js';
4
4
 
5
5
  describe('run progress', () => {
6
+ it('observes Go package execution and test completions, not build output or heartbeats', () => {
7
+ const progress = createRunProgress();
8
+ expect(
9
+ progress.scan('stdout', '{"Action":"build-output","ImportPath":"example/first","Output":"linking\\n"}\n'),
10
+ ).toBe(false);
11
+ expect(progress.inFlight()).toEqual([]);
12
+ expect(progress.scan('stdout', '{"Action":"start","Package":"example/first"}\n')).toBe(false);
13
+ expect(progress.inFlight()).toEqual(['example/first']);
14
+ expect(progress.scan('stdout', '{"Action":"run","Package":"example/first","Test":"TestReady"}\n')).toBe(false);
15
+ expect(progress.inFlight()).toEqual(['example/first TestReady']);
16
+ expect(progress.scan('stdout', '{"Action":"output","Package":"example/first","Output":"...\\n"}\n')).toBe(false);
17
+ expect(progress.scan('stdout', '{"Action":"pass","Package":"example/first","Test":"TestReady"}\n')).toBe(true);
18
+ expect(progress.scan('stdout', '{"Action":"pass","Package":"example/first"}\n')).toBe(true);
19
+ expect(progress.inFlight()).toEqual([]);
20
+ expect(
21
+ progress.scan('stdout', '{"Action":"build-output","ImportPath":"example/second","Output":"compiling\\n"}\n'),
22
+ ).toBe(false);
23
+ expect(progress.scan('stdout', '{"Action":"start","Package":"example/second"}\n')).toBe(false);
24
+ expect(progress.scan('stdout', '{"Action":"run","Package":"example/second","Test":"TestHang"}\n')).toBe(false);
25
+ expect(progress.inFlight()).toEqual(['example/second TestHang']);
26
+ });
27
+
28
+ it('counts only active Go package intervals, including startup and parallel packages', () => {
29
+ const progress = createRunProgress('go test ./...');
30
+ expect(progress.isTesting()).toBe(false);
31
+ progress.scan('stdout', '{"Action":"start","Package":"first"}\n{"Action":"start","Package":"second"}\n');
32
+ expect(progress.isTesting()).toBe(true);
33
+ expect(progress.inFlight()).toEqual(['first', 'second']);
34
+ progress.scan('stdout', '{"Action":"pass","Package":"first"}\n');
35
+ expect(progress.isTesting()).toBe(true);
36
+ expect(progress.inFlight()).toEqual(['second']);
37
+ progress.scan('stdout', '{"Action":"fail","Package":"second"}\n');
38
+ expect(progress.isTesting()).toBe(false);
39
+ expect(progress.inFlight()).toEqual([]);
40
+ });
41
+
42
+ it('buffers split Go events and does not mistake test output for lifecycle events', () => {
43
+ const progress = createRunProgress('go test ./...');
44
+ expect(progress.scan('stdout', '{"Action":"sta')).toBe(false);
45
+ expect(progress.isTesting()).toBe(false);
46
+ expect(progress.scan('stdout', 'rt","Package":"example"}\n')).toBe(false);
47
+ expect(progress.isTesting()).toBe(true);
48
+ expect(progress.scan('stdout', '{"Action":"run","Package":"example","Test":"TestHang"}\n')).toBe(false);
49
+ expect(progress.scan('stdout', '{"Action":"pause","Package":"example","Test":"TestHang"}\n')).toBe(false);
50
+ expect(progress.inFlight()).toEqual(['example TestHang']);
51
+ expect(progress.scan('stdout', '{"Action":"cont","Package":"example","Test":"TestHang"}\n')).toBe(false);
52
+ expect(progress.scan('stdout', '{"Action":"output","Package":"example","Output":"test fake ... ok\\n"}\n')).toBe(
53
+ false,
54
+ );
55
+ expect(progress.scan('stderr', '...\n')).toBe(false);
56
+ expect(progress.scan('stderr', '{invalid JSON}\n')).toBe(false);
57
+ expect(progress.scan('stdout', '{"Action":"skip","Package":"example","Test":"TestHang"}\n')).toBe(true);
58
+ expect(progress.inFlight()).toEqual(['example']);
59
+ 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
+ });
68
+
6
69
  it('counts a nextest status line with its run counter as a finished test, and a SLOW line as none', () => {
7
70
  const progress = createRunProgress();
8
71
  expect(progress.scan('stderr', ' SLOW [> 1.000s] (───) nt tests::hangs\n')).toBe(false);
@@ -60,6 +123,16 @@ describe('run progress', () => {
60
123
  });
61
124
 
62
125
  describe('progress report', () => {
126
+ it('asks Go for its package execution events without changing its test selection', () => {
127
+ expect(withProgressReport('go test -count=1 ./hts/... ./host/...')).toBe(
128
+ 'go test -json -count=1 ./hts/... ./host/...',
129
+ );
130
+ expect(withProgressReport('go test -json -count=1 ./...')).toBe('go test -json -count=1 ./...');
131
+ expect(withProgressReport('go test -json=false ./first && go test -count=1 ./second')).toBe(
132
+ 'go test -json ./first && go test -json -count=1 ./second',
133
+ );
134
+ });
135
+
63
136
  it('asks a lone bun test for dots, keeping the runtime the target named', () => {
64
137
  expect(withProgressReport('bun test --timeout=0 src')).toBe('bun test --dots --timeout=0 src');
65
138
  expect(withProgressReport('../x/.runtime/bun test a')).toBe('../x/.runtime/bun test --dots a');
@@ -1,4 +1,6 @@
1
- import { BUN_TEST, NEXTEST_RUN } from './verdict.js';
1
+ import typia from 'typia';
2
+ import { invocationSpan, withInvocationFlag } from './cpu-tokens.js';
3
+ import { BUN_TEST, GO_TEST, NEXTEST_RUN } from './verdict.js';
2
4
 
3
5
  /**
4
6
  * What a run's output says about its forward motion: whether a test finished, and which tests the runner said
@@ -17,14 +19,19 @@ import { BUN_TEST, NEXTEST_RUN } from './verdict.js';
17
19
  * - bun: a line of dots, one per test finished (`--dots`, which `withProgressReport` asks for: bun prints nothing
18
20
  * per passing test when it believes an agent runs it), or a `(pass)`/`(fail)`/`(skip)`/`(todo)` line. bun names
19
21
  * no test before it finishes, so a bun run names nothing in flight.
22
+ * - go: structured `go test -json` package `start` follows the package's build dependencies; package `pass`,
23
+ * `fail`, or `skip` ends that execution interval. Test terminals re-arm the bound, but `output` and
24
+ * `build-output` never do. Initial and inter-package builds are outside an active test interval.
20
25
  *
21
- * A command that reports no tests makes progress only by exiting.
26
+ * A command without a recognised execution lifecycle remains bounded from spawn.
22
27
  */
23
28
  export interface RunProgress {
24
- /** One chunk of one of the command's output streams; true when it reports at least one finished test. */
29
+ /** One chunk of one of the command's output streams; true when it reports a finished test or Go package. */
25
30
  scan(stream: OutputStream, chunk: string): boolean;
26
31
  /** The tests the runner reported running and has not reported finished, in the order it first named them. */
27
32
  inFlight(): string[];
33
+ /** Whether test idle time is currently chargeable; unknown runner lifecycles stay chargeable. */
34
+ isTesting(): boolean;
28
35
  }
29
36
 
30
37
  export type OutputStream = 'stdout' | 'stderr';
@@ -43,21 +50,77 @@ const RUNNING = [
43
50
  ];
44
51
  const DOTS = /^\.+$/;
45
52
 
53
+ type GoTestEvent =
54
+ | { Action: 'start'; Package: string }
55
+ | { Action: 'run' | 'pause' | 'cont'; Package: string; Test: string }
56
+ | { Action: 'pass' | 'fail' | 'skip' | 'bench'; Package: string; Test?: string }
57
+ | { Action: 'output'; Package: string; Output: string; Test?: string };
58
+ const isGoTestEvent = typia.createIs<GoTestEvent>();
59
+
60
+ function goTestEvent(text: string): GoTestEvent | null {
61
+ if (!text.startsWith('{')) return null;
62
+ try {
63
+ const event: unknown = JSON.parse(text);
64
+ return isGoTestEvent(event) ? event : null;
65
+ } catch (error) {
66
+ if (error instanceof SyntaxError) return null;
67
+ throw error;
68
+ }
69
+ }
70
+
71
+ /** Defer only a direct Go command/chain; an unknown shell wrapper or another runner keeps its existing bound. */
72
+ function onlyGoTests(command: string): boolean {
73
+ let rest = command.trim();
74
+ while (rest.search(GO_TEST) === 0) {
75
+ rest = rest.slice(invocationSpan(rest, GO_TEST).argsEnd).trimStart();
76
+ if (rest === '') return true;
77
+ const separator = /^(?:&&|\|\||;)\s*/.exec(rest);
78
+ if (separator === null) return false;
79
+ rest = rest.slice(separator[0].length);
80
+ }
81
+ return false;
82
+ }
83
+
46
84
  /** A stream's unfinished last line, and how many of its dots were already counted. */
47
85
  interface LineState {
48
86
  partial: string;
49
87
  countedDots: number;
50
88
  }
51
89
 
52
- export function createRunProgress(): RunProgress {
90
+ export function createRunProgress(command = ''): RunProgress {
91
+ const goLifecycle = onlyGoTests(command);
53
92
  const streams: Record<OutputStream, LineState> = {
54
93
  stdout: { partial: '', countedDots: 0 },
55
94
  stderr: { partial: '', countedDots: 0 },
56
95
  };
57
96
  const running = new Set<string>();
97
+ const packages = new Map<string, Set<string>>();
58
98
 
59
99
  // One complete line, ANSI stripped, that is not a line of dots: true when it names a finished test.
60
100
  const finishedLine = (text: string): boolean => {
101
+ const event = goTestEvent(text);
102
+ if (event !== null) {
103
+ switch (event.Action) {
104
+ case 'start':
105
+ packages.set(event.Package, new Set());
106
+ return false;
107
+ case 'run':
108
+ packages.get(event.Package)?.add(event.Test);
109
+ return false;
110
+ case 'pass':
111
+ case 'fail':
112
+ case 'skip':
113
+ case 'bench':
114
+ if (event.Test === undefined) packages.delete(event.Package);
115
+ else packages.get(event.Package)?.delete(event.Test);
116
+ return true;
117
+ case 'pause':
118
+ case 'cont':
119
+ case 'output':
120
+ return false;
121
+ }
122
+ }
123
+ if (goLifecycle) return false;
61
124
  for (const pattern of FINISHED) {
62
125
  const name = pattern.exec(text)?.[1];
63
126
  if (name !== undefined) {
@@ -86,20 +149,28 @@ export function createRunProgress(): RunProgress {
86
149
  for (const raw of lines) {
87
150
  const text = raw.replace(ANSI, '').trimEnd();
88
151
  if (DOTS.test(text)) {
89
- progressed ||= text.length > counted;
152
+ if (!goLifecycle && DOTS.test(text)) {
153
+ progressed ||= text.length > counted;
154
+ }
90
155
  } else if (finishedLine(text)) {
91
156
  progressed = true;
92
157
  }
93
158
  counted = 0;
94
159
  }
95
160
  const tail = rest.replace(ANSI, '');
96
- const dots = DOTS.test(tail) ? tail.length : 0;
161
+ const dots = !goLifecycle && DOTS.test(tail) ? tail.length : 0;
97
162
  progressed ||= dots > counted;
98
163
  state.partial = rest;
99
164
  state.countedDots = dots;
100
165
  return progressed;
101
166
  },
102
- inFlight: () => [...running],
167
+ inFlight: () => [
168
+ ...running,
169
+ ...[...packages].flatMap(([name, tests]) =>
170
+ tests.size === 0 ? [name] : [...tests].map((test) => `${name} ${test}`),
171
+ ),
172
+ ],
173
+ isTesting: () => !goLifecycle || packages.size > 0,
103
174
  };
104
175
  }
105
176
 
@@ -109,10 +180,22 @@ export function createRunProgress(): RunProgress {
109
180
  * unchanged.
110
181
  */
111
182
  export function withProgressReport(command: string): string {
112
- const bun = command.match(BUN_TEST)?.length ?? 0;
113
- const nextest = command.match(NEXTEST_RUN)?.length ?? 0;
114
- if (bun !== 1 || nextest !== 0 || /(?:^|\s)(?:--dots|--reporter[=\s]dots)(?=\s|$)/.test(command)) {
115
- return command;
183
+ let reported = command;
184
+ for (const match of [...command.matchAll(new RegExp(GO_TEST, 'g'))].reverse()) {
185
+ reported =
186
+ reported.slice(0, match.index) +
187
+ withInvocationFlag(
188
+ reported.slice(match.index),
189
+ GO_TEST,
190
+ /(?<![\w-])-json(?:=(?:true|false))?(?=\s|$)/g,
191
+ '-json',
192
+ true,
193
+ );
194
+ }
195
+ const bun = reported.match(BUN_TEST)?.length ?? 0;
196
+ const nextest = reported.match(NEXTEST_RUN)?.length ?? 0;
197
+ if (bun !== 1 || nextest !== 0 || /(?:^|\s)(?:--dots|--reporter[=\s]dots)(?=\s|$)/.test(reported)) {
198
+ return reported;
116
199
  }
117
- return command.replace(BUN_TEST, (_match, runtime: string) => `${runtime} test --dots`);
200
+ return reported.replace(BUN_TEST, (_match, runtime: string) => `${runtime} test --dots`);
118
201
  }
@@ -8,10 +8,10 @@ import { join } from 'node:path';
8
8
  * - `bound`: the run hit a wall-clock bound and nothing else failed. Either the command
9
9
  * outlived `timeoutMs` (`total`), or every failing test in the runner's report failed on
10
10
  * its runner's per-test timeout (`test`). A loaded host produces exactly these.
11
- * - `wedged`: `idleTimeoutMs` fired: no test finished for that long. A machine that is only slow still finishes
12
- * tests, so this is a hang, not a slow host, and the fix is the opposite one, so it is never a `bound`. It names
13
- * the tests still running: those the runner said were running and never said finished, and those its report says
14
- * it aborted when the bound stopped it.
11
+ * - `wedged`: `idleTimeoutMs` fired: no test finished for that long during a chargeable test interval. This proves
12
+ * the suite's completion bound was exceeded, not that every process stopped: an expensive active fixture can
13
+ * exceed it too. The verdict names tests reported running but never finished and tests the report says were
14
+ * aborted; diagnostics distinguish a stalled wait from a progressing but oversized lifecycle.
15
15
  * - `no-space`: the run failed and its output said a write found no space (ENOSPC). Whatever its tests then reported,
16
16
  * they failed on the disk, not on their own: the verdict names the line that said so and, when the task held one,
17
17
  * its RAM temp volume as it stood when that line arrived.
@@ -274,6 +274,7 @@ const NEXTEST_REPORT_CONFIG_FILE = 'nextest-report.toml';
274
274
  */
275
275
  export const BUN_TEST = /(?<=^|[\s;&|()])((?:[^\s;&|()<>'"`\\=]*\/)?bun)\s+test(?=\s|$)/g;
276
276
  export const NEXTEST_RUN = /(?<=^|[\s;&|()-])nextest\s+run(?=\s|$)/g;
277
+ export const GO_TEST = /(?<=^|[\s;&|()])go\s+test(?=\s|$)/;
277
278
 
278
279
  /**
279
280
  * `command` with its test runner asked to write a JUnit report into `directory`, the report
@@ -0,0 +1,155 @@
1
+ import { expect, it } from 'bun:test';
2
+ import { spawn, spawnSync } from 'node:child_process';
3
+ import { mkdir, readFile, symlink, writeFile } from 'node:fs/promises';
4
+ import { createRequire } from 'node:module';
5
+ import { join } from 'node:path';
6
+ import { fixtureNxEnv, withNxFixture } from './__tests__/fixture-nx-env.js';
7
+ import { awaitExit, isRunning, processTable, terminate } from './testing.js';
8
+
9
+ /**
10
+ * Guards the `NX_DAEMON_START=never` hunks of `patches/nx@23.2.1.patch`. A stock client that finds no live daemon
11
+ * starts one, detached into a session of its own, and the record shares it with every client of the checkout. Cowshed
12
+ * ends a job's whole process tree when the job ends (`specs/cowshed/11_shell.md`), so a daemon one job's client
13
+ * started would end with that job while another job's `nx run` still used it. Every cowshed job therefore runs with
14
+ * `NX_DAEMON_START=never` (`specs/cowshed/04_sandbox.md`): its client uses the daemon the record names while that
15
+ * runs, otherwise computes the command without one, and never starts a daemon. A daemon that stops under such a
16
+ * client turns the daemon off for that process alone, and the command continues without it: its successor is its
17
+ * starter's to start, and the stop is no reason to disable the checkout's daemon for anyone else.
18
+ */
19
+
20
+ const repositoryRoot = join(import.meta.dir, '../../..');
21
+ const nxEntry = join(repositoryRoot, 'node_modules', '.bin', 'nx');
22
+ const nxVersion: string = createRequire(join(repositoryRoot, 'package.json'))('nx/package.json').version;
23
+
24
+ const record = (workspace: string) => join(workspace, '.nx/workspace-data/d/server-process.json');
25
+
26
+ async function recordedPid(workspace: string): Promise<number | null> {
27
+ const text = await readFile(record(workspace), 'utf8').catch((error: unknown) => {
28
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return null;
29
+ throw error;
30
+ });
31
+ if (text === null) return null;
32
+ const parsed: unknown = JSON.parse(text);
33
+ if (
34
+ typeof parsed !== 'object' ||
35
+ parsed === null ||
36
+ !('processId' in parsed) ||
37
+ typeof parsed.processId !== 'number'
38
+ ) {
39
+ throw new Error(`the daemon record names no process: ${text}`);
40
+ }
41
+ return parsed.processId;
42
+ }
43
+
44
+ async function fixtureWorkspace(workspace: string): Promise<void> {
45
+ await symlink(join(repositoryRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
46
+ await writeFile(join(workspace, 'nx.json'), '{}\n');
47
+ await writeFile(join(workspace, 'package.json'), JSON.stringify({ name: 'daemon-start-never', private: true }));
48
+ await mkdir(join(workspace, 'p'));
49
+ await writeFile(join(workspace, 'p', 'project.json'), JSON.stringify({ name: 'p' }));
50
+ }
51
+
52
+ /**
53
+ * Run `nx show projects --json`, require it to list the fixture's one project, and return everything it printed. The
54
+ * format is asked for, never inferred: unasked, Nx prints JSON only when it detects an AI agent in the environment
55
+ * (`show/command-object.js`, `isAiAgent`) and a plain name per line otherwise. The list is stdout's last line: a note
56
+ * Nx prints about its daemon goes to stdout before it.
57
+ */
58
+ function showProjects(workspace: string, env: Record<string, string>): string {
59
+ const ran = spawnSync('bun', [nxEntry, 'show', 'projects', '--json'], { cwd: workspace, env, encoding: 'utf8' });
60
+ const output = `${ran.stdout}${ran.stderr}`;
61
+ expect(ran.status, output).toBe(0);
62
+ expect(JSON.parse(ran.stdout.trimEnd().split('\n').at(-1) ?? ''), output).toEqual(['p']);
63
+ return output;
64
+ }
65
+
66
+ it('a client that may never start a daemon uses a running one and otherwise runs without', async () => {
67
+ await withNxFixture('nx-daemon-start-never', async ({ workspace, socketDirectory }) => {
68
+ await fixtureWorkspace(workspace);
69
+ const env = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' };
70
+ const never = { ...env, NX_DAEMON_START: 'never' };
71
+
72
+ // No daemon runs: the command is computed in the client, and no daemon is started.
73
+ showProjects(workspace, never);
74
+ expect(await recordedPid(workspace)).toBeNull();
75
+
76
+ // Control: the same client without the variable starts one, so the absence above is the variable's doing.
77
+ showProjects(workspace, env);
78
+ const daemon = await recordedPid(workspace);
79
+ if (daemon === null) throw new Error('a client allowed to start a daemon left no record');
80
+ expect(isRunning(daemon)).toBe(true);
81
+
82
+ // A daemon runs: the client uses it, and it is still the one the record names.
83
+ const log = join(workspace, '.nx/workspace-data/d/daemon.log');
84
+ const before = (await readFile(log, 'utf8')).length;
85
+ showProjects(workspace, never);
86
+ expect(await recordedPid(workspace)).toBe(daemon);
87
+ expect((await readFile(log, 'utf8')).length).toBeGreaterThan(before);
88
+
89
+ // A record whose daemon has gone (killed, so it never cleared its record) is no daemon to use and no reason to
90
+ // start one.
91
+ const killed = (await processTable()).filter((entry) => entry.pid === daemon);
92
+ terminate(daemon, 'SIGKILL');
93
+ await awaitExit(killed, 'the killed fixture daemon');
94
+ showProjects(workspace, never);
95
+ expect(await recordedPid(workspace)).toBe(daemon);
96
+ });
97
+ });
98
+
99
+ it('a client that may never start a daemon continues without one that stopped under it, and disables nothing', async () => {
100
+ await withNxFixture('nx-daemon-start-gone', async ({ workspace, socketDirectory }) => {
101
+ await fixtureWorkspace(workspace);
102
+ const never = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true', NX_DAEMON_START: 'never' };
103
+
104
+ // The record names a running process that serves no socket: a daemon that stopped answering after the client
105
+ // chose to use it.
106
+ const holder = spawn('sleep', ['300'], { stdio: 'ignore' });
107
+ try {
108
+ const pid = holder.pid;
109
+ if (pid === undefined) throw new Error('sleep did not start');
110
+ await mkdir(join(workspace, '.nx/workspace-data/d'), { recursive: true });
111
+ await writeFile(
112
+ record(workspace),
113
+ JSON.stringify({ processId: pid, socketPath: join(socketDirectory, 'gone.sock'), nxVersion }),
114
+ );
115
+
116
+ expect(showProjects(workspace, never)).toContain('NX_DAEMON_START=never: this client never starts a daemon');
117
+ expect(await recordedPid(workspace)).toBe(pid);
118
+ // Not disabled for the checkout: the marker only `nx reset` clears would follow it into every terminal.
119
+ await expect(readFile(join(workspace, '.nx/workspace-data/d/disabled'), 'utf8')).rejects.toMatchObject({
120
+ code: 'ENOENT',
121
+ });
122
+ } finally {
123
+ holder.kill('SIGKILL');
124
+ }
125
+ });
126
+ });
127
+
128
+ /**
129
+ * An explicit `nx daemon --start` calls the daemon start directly, past the implicit-start check above, so under the
130
+ * job's rule it is refused as policy: a non-zero exit naming `NX_DAEMON_START=never` and the fixture escape, and no
131
+ * daemon. The same command without the variable, which is what a test's named fixture escape runs, starts one.
132
+ */
133
+ it('refuses an explicit daemon start under the job rule, and starts one without it', async () => {
134
+ await withNxFixture('nx-daemon-start-explicit', async ({ workspace, socketDirectory }) => {
135
+ await fixtureWorkspace(workspace);
136
+ const env = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' };
137
+
138
+ const refused = spawnSync('bun', [nxEntry, 'daemon', '--start'], {
139
+ cwd: workspace,
140
+ env: { ...env, NX_DAEMON_START: 'never' },
141
+ encoding: 'utf8',
142
+ });
143
+ const said = `${refused.stdout}${refused.stderr}`;
144
+ expect(refused.status, said).toBe(1);
145
+ expect(said).toContain('NX_DAEMON_START=never forbids this client starting a daemon');
146
+ expect(said).toContain('FIXTURE_STARTS_ITS_OWN_DAEMON');
147
+ expect(await recordedPid(workspace)).toBeNull();
148
+
149
+ const started = spawnSync('bun', [nxEntry, 'daemon', '--start'], { cwd: workspace, env, encoding: 'utf8' });
150
+ expect(started.status, `${started.stdout}${started.stderr}`).toBe(0);
151
+ const daemon = await recordedPid(workspace);
152
+ if (daemon === null) throw new Error('an explicit start without the variable left no daemon record');
153
+ expect(isRunning(daemon)).toBe(true);
154
+ });
155
+ });