@smoothbricks/nx-plugin 0.5.4 → 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.
- package/README.md +81 -16
- package/dist/ensure-built-receipt.d.ts +2 -2
- package/dist/ensure-built-receipt.d.ts.map +1 -1
- package/dist/ensure-built-receipt.js +7 -2
- package/dist/ensure-built-witness.d.ts +12 -1
- package/dist/ensure-built-witness.d.ts.map +1 -1
- package/dist/ensure-built-witness.js +32 -0
- package/dist/ensure-built.d.ts +2 -2
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +27 -5
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +48 -11
- package/dist/executors/bounded-exec/progress.d.ts +1 -3
- package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
- package/dist/executors/bounded-exec/progress.js +1 -2
- package/dist/executors/bounded-exec/runner-hook.d.ts +2 -0
- package/dist/executors/bounded-exec/runner-hook.d.ts.map +1 -0
- package/dist/executors/bounded-exec/runner-hook.js +301 -0
- package/dist/executors/bounded-exec/runner-lifecycle.d.ts +18 -0
- package/dist/executors/bounded-exec/runner-lifecycle.d.ts.map +1 -0
- package/dist/executors/bounded-exec/runner-lifecycle.js +92 -0
- package/dist/producer-receipts.d.ts +24 -1
- package/dist/producer-receipts.d.ts.map +1 -1
- package/dist/producer-receipts.js +91 -60
- package/dist/runtime-input-policy.d.ts +60 -0
- package/dist/runtime-input-policy.d.ts.map +1 -0
- package/dist/runtime-input-policy.js +379 -0
- package/managed/raw/git-format-staged.yml +16 -3
- package/managed/raw/patches/nx@23.2.1.patch +397 -33
- package/package.json +7 -2
- package/src/ensure-built-receipt.ts +8 -2
- package/src/ensure-built-witness.ts +29 -1
- package/src/ensure-built.ts +39 -7
- package/src/executors/bounded-exec/cargo-progress-host.test.ts +332 -0
- package/src/executors/bounded-exec/executor.test.ts +89 -42
- package/src/executors/bounded-exec/executor.ts +45 -11
- package/src/executors/bounded-exec/progress.test.ts +1 -14
- package/src/executors/bounded-exec/progress.ts +2 -5
- package/src/executors/bounded-exec/runner-hook.test.ts +193 -0
- package/src/executors/bounded-exec/runner-hook.ts +292 -0
- package/src/executors/bounded-exec/runner-lifecycle.test.ts +82 -0
- package/src/executors/bounded-exec/runner-lifecycle.ts +102 -0
- package/src/nx-daemon-start-never.test.ts +137 -0
- package/src/producer-receipts.test.ts +99 -0
- package/src/producer-receipts.ts +111 -58
- package/src/runtime-input-policy.ts +395 -0
- package/src/runtime-input.test.ts +160 -3
- package/test-timings.json +4 -0
|
@@ -0,0 +1,292 @@
|
|
|
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 { type ChildProcess, spawn } from 'node:child_process';
|
|
10
|
+
import { Socket } from 'node:net';
|
|
11
|
+
import type { Readable, Writable } from 'node:stream';
|
|
12
|
+
|
|
13
|
+
type Runner = 'go' | 'cargo';
|
|
14
|
+
type Phase = 'build' | 'active' | 'terminal';
|
|
15
|
+
|
|
16
|
+
interface Lifecycle {
|
|
17
|
+
announce(phase: Phase): Promise<Error | null>;
|
|
18
|
+
close(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function failure(context: string, error: unknown): Error {
|
|
22
|
+
return new Error(`${context}: ${error instanceof Error ? error.message : String(error)}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// This executable inherits lib es2022; Promise.withResolvers requires es2024.
|
|
26
|
+
function write(stream: Writable, bytes: Buffer | string): Promise<Error | null> {
|
|
27
|
+
return new Promise((resolve) => {
|
|
28
|
+
stream.write(bytes, (error) => resolve(error ?? null));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Only native stdout records change the lifecycle; arbitrary runner text never does. */
|
|
33
|
+
function nativePhase(runner: Runner): (line: Buffer) => Phase | null {
|
|
34
|
+
const packages = new Set<string>();
|
|
35
|
+
let cargoActive = false;
|
|
36
|
+
return (line) => {
|
|
37
|
+
if (cargoActive || line[0] !== 123) return null;
|
|
38
|
+
let event: unknown;
|
|
39
|
+
try {
|
|
40
|
+
event = JSON.parse(line.toString('utf8'));
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error instanceof SyntaxError) return null;
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
if (typeof event !== 'object' || event === null) return null;
|
|
46
|
+
if (runner === 'cargo') {
|
|
47
|
+
if (
|
|
48
|
+
'reason' in event &&
|
|
49
|
+
event.reason === 'build-finished' &&
|
|
50
|
+
'success' in event &&
|
|
51
|
+
typeof event.success === 'boolean'
|
|
52
|
+
) {
|
|
53
|
+
// Even a failed build has left compilation. Nothing emitted by a test, doctest,
|
|
54
|
+
// or custom harness can put Cargo back into build or finish this invocation.
|
|
55
|
+
cargoActive = true;
|
|
56
|
+
return 'active';
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (!('Action' in event) || !('Package' in event) || typeof event.Package !== 'string' || 'Test' in event) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
switch (event.Action) {
|
|
64
|
+
case 'start':
|
|
65
|
+
packages.add(event.Package);
|
|
66
|
+
break;
|
|
67
|
+
case 'pass':
|
|
68
|
+
case 'fail':
|
|
69
|
+
case 'skip':
|
|
70
|
+
packages.delete(event.Package);
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
return packages.size === 0 ? 'build' : 'active';
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** One outstanding phase, acknowledged only after the executor applies its watchdog state. */
|
|
80
|
+
function lifecycle(id: number, failed: (error: Error) => void): Lifecycle {
|
|
81
|
+
const socket = new Socket({ fd: 3, readable: true, writable: true });
|
|
82
|
+
let pending: { message: string; resolve: (error: Error | null) => void } | null = null;
|
|
83
|
+
let received = '';
|
|
84
|
+
let error: Error | null = null;
|
|
85
|
+
let closing = false;
|
|
86
|
+
let phase: Phase | null = null;
|
|
87
|
+
const fail = (cause: Error): void => {
|
|
88
|
+
if (error !== null) return;
|
|
89
|
+
error = cause;
|
|
90
|
+
const waiting = pending;
|
|
91
|
+
pending = null;
|
|
92
|
+
waiting?.resolve(cause);
|
|
93
|
+
failed(cause);
|
|
94
|
+
socket.destroy();
|
|
95
|
+
};
|
|
96
|
+
socket.on('error', (cause) => fail(failure('lifecycle channel failed', cause)));
|
|
97
|
+
socket.on('end', () => {
|
|
98
|
+
if (!closing) fail(new Error('lifecycle channel ended before terminal acknowledgement'));
|
|
99
|
+
});
|
|
100
|
+
socket.on('close', () => {
|
|
101
|
+
if (!closing && error === null) fail(new Error('lifecycle channel closed before terminal acknowledgement'));
|
|
102
|
+
});
|
|
103
|
+
socket.on('data', (chunk: Buffer) => {
|
|
104
|
+
received += chunk.toString('utf8');
|
|
105
|
+
if (pending === null || !pending.message.startsWith(received)) {
|
|
106
|
+
fail(new Error(`unexpected lifecycle acknowledgement: ${JSON.stringify(received)}`));
|
|
107
|
+
} else if (received === pending.message) {
|
|
108
|
+
const waiting = pending;
|
|
109
|
+
pending = null;
|
|
110
|
+
received = '';
|
|
111
|
+
waiting.resolve(null);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return {
|
|
115
|
+
async announce(next: Phase): Promise<Error | null> {
|
|
116
|
+
if (error !== null) return error;
|
|
117
|
+
if (phase === next) return null;
|
|
118
|
+
if (pending !== null) throw new Error('concurrent lifecycle announcements');
|
|
119
|
+
const result = await new Promise<Error | null>((resolve) => {
|
|
120
|
+
const message = `${JSON.stringify({ id, phase: next })}\n`;
|
|
121
|
+
pending = { message, resolve };
|
|
122
|
+
socket.write(message, (cause) => {
|
|
123
|
+
if (cause) fail(failure('writing lifecycle phase failed', cause));
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
if (result === null) phase = next;
|
|
127
|
+
return result;
|
|
128
|
+
},
|
|
129
|
+
async close(): Promise<void> {
|
|
130
|
+
closing = true;
|
|
131
|
+
if (socket.destroyed) return;
|
|
132
|
+
await new Promise<void>((resolve) => {
|
|
133
|
+
socket.once('close', resolve);
|
|
134
|
+
// The shell retains this endpoint for later commands: shutdown/end would poison
|
|
135
|
+
// every inherited reference. Terminal ACK drained our writes; close only ours.
|
|
136
|
+
socket.destroy();
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Await each destination write, bounding queued output and preserving its original bytes. */
|
|
143
|
+
async function forward(source: Readable, target: Writable): Promise<Error | null> {
|
|
144
|
+
try {
|
|
145
|
+
for await (const chunk of source) {
|
|
146
|
+
const bytes: unknown = chunk;
|
|
147
|
+
if (!Buffer.isBuffer(bytes)) throw new Error('child output was not a byte stream');
|
|
148
|
+
const error = await write(target, bytes);
|
|
149
|
+
if (error !== null) return error;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
} catch (error) {
|
|
153
|
+
return failure('reading child output failed', error);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Buffer only possible JSON records; ACK their transition before releasing any of their bytes. */
|
|
158
|
+
async function forwardStdout(
|
|
159
|
+
source: Readable,
|
|
160
|
+
inspect: (line: Buffer) => Promise<Error | null>,
|
|
161
|
+
): Promise<Error | null> {
|
|
162
|
+
let pending: Buffer[] = [];
|
|
163
|
+
let start = true;
|
|
164
|
+
let json = false;
|
|
165
|
+
const record = async (line: Buffer): Promise<Error | null> => {
|
|
166
|
+
const error = await inspect(line);
|
|
167
|
+
return error ?? (await write(process.stdout, line));
|
|
168
|
+
};
|
|
169
|
+
try {
|
|
170
|
+
for await (const chunk of source) {
|
|
171
|
+
const bytes: unknown = chunk;
|
|
172
|
+
if (!Buffer.isBuffer(bytes)) throw new Error('child stdout was not a byte stream');
|
|
173
|
+
let offset = 0;
|
|
174
|
+
while (offset < bytes.length) {
|
|
175
|
+
if (start) json = bytes[offset] === 123;
|
|
176
|
+
const newline = bytes.indexOf(10, offset);
|
|
177
|
+
const end = newline === -1 ? bytes.length : newline + 1;
|
|
178
|
+
const piece = bytes.subarray(offset, end);
|
|
179
|
+
let error: Error | null = null;
|
|
180
|
+
if (json && newline === -1) {
|
|
181
|
+
pending.push(piece);
|
|
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
|
+
} else {
|
|
191
|
+
error = await write(process.stdout, piece);
|
|
192
|
+
}
|
|
193
|
+
if (error !== null) return error;
|
|
194
|
+
start = newline !== -1;
|
|
195
|
+
offset = end;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return pending.length === 0 ? null : await record(Buffer.concat(pending));
|
|
199
|
+
} catch (error) {
|
|
200
|
+
return failure('reading child stdout failed', error);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** argv: invocation ID, runner kind, original executable, then its unchanged arguments. */
|
|
205
|
+
async function main([idText, runner, executable, ...args]: readonly string[]): Promise<void> {
|
|
206
|
+
const id = Number(idText);
|
|
207
|
+
if (
|
|
208
|
+
idText === undefined ||
|
|
209
|
+
!/^\d+$/.test(idText) ||
|
|
210
|
+
!Number.isSafeInteger(id) ||
|
|
211
|
+
(runner !== 'go' && runner !== 'cargo') ||
|
|
212
|
+
executable === undefined ||
|
|
213
|
+
executable === ''
|
|
214
|
+
) {
|
|
215
|
+
process.stderr.write('runner-hook: usage: <invocation-id> <go|cargo> <executable> [arguments...]\n');
|
|
216
|
+
process.exitCode = 2;
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
let child: ChildProcess | null = null;
|
|
220
|
+
let failed = false;
|
|
221
|
+
let interrupted: NodeJS.Signals | null = null;
|
|
222
|
+
const fail = (error: Error): void => {
|
|
223
|
+
if (failed) return;
|
|
224
|
+
failed = true;
|
|
225
|
+
process.exitCode = 1;
|
|
226
|
+
process.stderr.write(`runner-hook: ${error.message}\n`);
|
|
227
|
+
child?.kill('SIGKILL');
|
|
228
|
+
};
|
|
229
|
+
const forwardedSignals: NodeJS.Signals[] = ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT'];
|
|
230
|
+
const signals = forwardedSignals.map((name) => {
|
|
231
|
+
const listener = (): void => {
|
|
232
|
+
interrupted = name;
|
|
233
|
+
child?.kill(name);
|
|
234
|
+
};
|
|
235
|
+
process.on(name, listener);
|
|
236
|
+
return { name, listener };
|
|
237
|
+
});
|
|
238
|
+
process.stdout.on('error', fail);
|
|
239
|
+
process.stderr.on('error', fail);
|
|
240
|
+
let channel: Lifecycle | null = null;
|
|
241
|
+
let code: number | null = null;
|
|
242
|
+
let exitSignal: NodeJS.Signals | null = null;
|
|
243
|
+
try {
|
|
244
|
+
channel = lifecycle(id, fail);
|
|
245
|
+
const ready = await channel.announce('build');
|
|
246
|
+
if (ready !== null) fail(ready);
|
|
247
|
+
if (!failed && interrupted === null) {
|
|
248
|
+
// Descriptor 3 belongs only to this supervisor, never the tool or its descendants.
|
|
249
|
+
const running = spawn(executable, args, { stdio: ['inherit', 'pipe', 'pipe'] });
|
|
250
|
+
child = running;
|
|
251
|
+
const closed = new Promise<void>((resolve) => {
|
|
252
|
+
running.once('close', (status, reason) => {
|
|
253
|
+
code = status;
|
|
254
|
+
exitSignal = reason;
|
|
255
|
+
resolve();
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
running.on('error', (error) => fail(failure(`could not run ${JSON.stringify(executable)}`, error)));
|
|
259
|
+
const scan = nativePhase(runner);
|
|
260
|
+
const control = channel;
|
|
261
|
+
const stdout = forwardStdout(running.stdout, async (line) => {
|
|
262
|
+
const next = scan(line);
|
|
263
|
+
return next === null ? null : await control.announce(next);
|
|
264
|
+
}).then((error) => {
|
|
265
|
+
if (error !== null) fail(failure('forwarding stdout failed', error));
|
|
266
|
+
});
|
|
267
|
+
const stderr = forward(running.stderr, process.stderr).then((error) => {
|
|
268
|
+
if (error !== null) fail(failure('forwarding stderr failed', error));
|
|
269
|
+
});
|
|
270
|
+
await Promise.all([closed, stdout, stderr]);
|
|
271
|
+
} else {
|
|
272
|
+
exitSignal = interrupted;
|
|
273
|
+
}
|
|
274
|
+
} catch (error) {
|
|
275
|
+
fail(failure('supervisor failed', error));
|
|
276
|
+
}
|
|
277
|
+
// Write callbacks complete only once downstream has accepted every forwarded byte.
|
|
278
|
+
// Flush diagnostics too, before terminal and before re-raising a child's signal.
|
|
279
|
+
for (const error of await Promise.all([write(process.stdout, ''), write(process.stderr, '')])) {
|
|
280
|
+
if (error !== null) fail(failure('draining supervisor output failed', error));
|
|
281
|
+
}
|
|
282
|
+
if (channel !== null) {
|
|
283
|
+
const error = await channel.announce('terminal');
|
|
284
|
+
if (error !== null) fail(error);
|
|
285
|
+
await channel.close();
|
|
286
|
+
}
|
|
287
|
+
for (const { name, listener } of signals) process.off(name, listener);
|
|
288
|
+
process.exitCode = failed ? 1 : (code ?? 1);
|
|
289
|
+
if (!failed && exitSignal !== null) process.kill(process.pid, exitSignal);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
await main(process.argv.slice(2));
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { createRunnerLifecycle, withRunnerLifecycle } from './runner-lifecycle.js';
|
|
3
|
+
|
|
4
|
+
const event = (id: number, phase: string): string => JSON.stringify({ id, phase });
|
|
5
|
+
|
|
6
|
+
describe('private runner lifecycle', () => {
|
|
7
|
+
it('bounds startup and active execution, never build intervals, through the final owned close', () => {
|
|
8
|
+
const lifecycle = createRunnerLifecycle(2);
|
|
9
|
+
expect(lifecycle.isTesting()).toBe(true);
|
|
10
|
+
lifecycle.accept(event(0, 'build'));
|
|
11
|
+
expect(lifecycle.isTesting()).toBe(false);
|
|
12
|
+
lifecycle.accept(event(0, 'active'));
|
|
13
|
+
expect(lifecycle.isTesting()).toBe(true);
|
|
14
|
+
lifecycle.accept(event(0, 'build'));
|
|
15
|
+
expect(lifecycle.isTesting()).toBe(false);
|
|
16
|
+
lifecycle.accept(event(0, 'active'));
|
|
17
|
+
lifecycle.accept(event(0, 'terminal'));
|
|
18
|
+
expect(lifecycle.isTesting()).toBe(true);
|
|
19
|
+
lifecycle.accept(event(1, 'build'));
|
|
20
|
+
expect(lifecycle.isTesting()).toBe(false);
|
|
21
|
+
lifecycle.accept(event(1, 'active'));
|
|
22
|
+
expect(lifecycle.isTesting()).toBe(true);
|
|
23
|
+
lifecycle.accept(event(1, 'terminal'));
|
|
24
|
+
expect(lifecycle.isTesting()).toBe(false);
|
|
25
|
+
expect(lifecycle.error()).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('allows shell short-circuiting but refuses overlapping or resurrected invocations', () => {
|
|
29
|
+
const lifecycle = createRunnerLifecycle(3);
|
|
30
|
+
lifecycle.accept(event(0, 'build'));
|
|
31
|
+
lifecycle.accept(event(0, 'terminal'));
|
|
32
|
+
expect(lifecycle.accept(event(2, 'build'))).toEqual({ id: 2, phase: 'build' });
|
|
33
|
+
expect(lifecycle.accept(event(1, 'build'))).toBeNull();
|
|
34
|
+
expect(lifecycle.isTesting()).toBe(true);
|
|
35
|
+
expect(lifecycle.error()).not.toBeNull();
|
|
36
|
+
for (const lines of [
|
|
37
|
+
[event(0, 'terminal'), event(0, 'build')],
|
|
38
|
+
[event(0, 'build'), event(1, 'build')],
|
|
39
|
+
['test result: ok'],
|
|
40
|
+
[event(0, 'heartbeat')],
|
|
41
|
+
]) {
|
|
42
|
+
const invalid = createRunnerLifecycle(2);
|
|
43
|
+
for (const line of lines) invalid.accept(line);
|
|
44
|
+
expect(invalid.error()).not.toBeNull();
|
|
45
|
+
expect(invalid.isTesting()).toBe(true);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('native runner hooks', () => {
|
|
51
|
+
it('preserves runner selection, toolchains, separators, and harness arguments', () => {
|
|
52
|
+
const hooked = withRunnerLifecycle(
|
|
53
|
+
'cargo +stable --frozen test --message-format=human --lib selected -- --nocapture && go test -json ./second',
|
|
54
|
+
);
|
|
55
|
+
expect(hooked?.runners).toBe(2);
|
|
56
|
+
expect(hooked?.command).toContain(
|
|
57
|
+
'0 cargo cargo +stable --frozen test --message-format=json-render-diagnostics --lib selected -- --nocapture && ',
|
|
58
|
+
);
|
|
59
|
+
expect(hooked?.command).toContain('1 go go test -json ./second');
|
|
60
|
+
expect(withRunnerLifecycle('cargo test a || cargo test b; go test -json ./...')?.runners).toBe(3);
|
|
61
|
+
expect(withRunnerLifecycle('cargo test -- --message-format=human')?.command).toContain(
|
|
62
|
+
'cargo test --message-format=json-render-diagnostics -- --message-format=human',
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('retains spawn accounting for Bun, nextest, mixed wrappers and unknown shell syntax', () => {
|
|
67
|
+
for (const command of [
|
|
68
|
+
'bun test src',
|
|
69
|
+
'cargo nextest run',
|
|
70
|
+
'cargo build && cargo test',
|
|
71
|
+
'go test ./... && bun test src',
|
|
72
|
+
'env FOO=bar cargo test',
|
|
73
|
+
'cargo test | cat',
|
|
74
|
+
'cargo test > output',
|
|
75
|
+
'cargo test 3>&1',
|
|
76
|
+
'cargo test &&',
|
|
77
|
+
'cargo test\ngo test',
|
|
78
|
+
'sh -c "cargo test"',
|
|
79
|
+
])
|
|
80
|
+
expect(withRunnerLifecycle(command)).toBeNull();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
|
|
6
|
+
const CARGO_TEST = /^cargo(?:\s+(?:-\S+|\+\S+))*\s+test(?=\s|$)/;
|
|
7
|
+
const MESSAGE_FORMAT = /(?<![\w-])--message-format(?:=|\s+)(?:"[^"]*"|'[^']*'|[^\s]+)/g;
|
|
8
|
+
export type RunnerPhase = 'build' | 'active' | 'terminal';
|
|
9
|
+
export interface RunnerPhaseEvent {
|
|
10
|
+
id: number;
|
|
11
|
+
phase: RunnerPhase;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Only complete, direct chains have a known lifecycle. Shell wrappers remain bounded from spawn. */
|
|
15
|
+
export function withRunnerLifecycle(command: string): { command: string; runners: number } | null {
|
|
16
|
+
// Redirections could replace the private descriptor; newlines/backticks/escapes need a real shell parser.
|
|
17
|
+
if (/[<>\n\r`\\]/.test(command)) return null;
|
|
18
|
+
let rest = command.trim();
|
|
19
|
+
const parts: string[] = [];
|
|
20
|
+
let runners = 0;
|
|
21
|
+
const hook = fileURLToPath(
|
|
22
|
+
new URL(import.meta.url.endsWith('.ts') ? './runner-hook.ts' : './runner-hook.js', import.meta.url),
|
|
23
|
+
);
|
|
24
|
+
while (rest !== '') {
|
|
25
|
+
const kind = rest.search(GO_TEST) === 0 ? 'go' : CARGO_TEST.test(rest) ? 'cargo' : null;
|
|
26
|
+
if (kind === null) return null;
|
|
27
|
+
const invocation = kind === 'go' ? GO_TEST : CARGO_TEST;
|
|
28
|
+
const { argsEnd } = invocationSpan(rest, invocation);
|
|
29
|
+
let runner = rest.slice(0, argsEnd).trimEnd();
|
|
30
|
+
if (kind === 'cargo') {
|
|
31
|
+
// Only Cargo's own arguments, never those after the libtest separator, select build reporting.
|
|
32
|
+
const separator = /\s--(?:\s|$)/.exec(runner);
|
|
33
|
+
const end = separator?.index ?? runner.length;
|
|
34
|
+
runner =
|
|
35
|
+
withInvocationFlag(
|
|
36
|
+
runner.slice(0, end),
|
|
37
|
+
invocation,
|
|
38
|
+
MESSAGE_FORMAT,
|
|
39
|
+
'--message-format=json-render-diagnostics',
|
|
40
|
+
true,
|
|
41
|
+
) + runner.slice(end);
|
|
42
|
+
}
|
|
43
|
+
// The hook owns an inherited duplex descriptor, which Bun's net.Socket({fd}) does not adopt.
|
|
44
|
+
const runtime = process.versions.bun === undefined ? process.execPath : 'node';
|
|
45
|
+
parts.push(`${shellWord(runtime)} ${shellWord(hook)} ${runners} ${kind} ${runner}`);
|
|
46
|
+
runners += 1;
|
|
47
|
+
rest = rest.slice(argsEnd).trimStart();
|
|
48
|
+
if (rest === '') break;
|
|
49
|
+
const separator = /^(?:&&|\|\||;)\s*/.exec(rest);
|
|
50
|
+
if (separator === null) return null;
|
|
51
|
+
parts.push(separator[0].trim());
|
|
52
|
+
rest = rest.slice(separator[0].length);
|
|
53
|
+
if (rest === '') return null;
|
|
54
|
+
}
|
|
55
|
+
return runners === 0 ? null : { command: parts.join(' '), runners };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A private supervisor channel, not runner output, grants authority to pause the test clock. */
|
|
59
|
+
export function createRunnerLifecycle(runners: number) {
|
|
60
|
+
let current = 0;
|
|
61
|
+
let phase: RunnerPhase = 'active'; // Shell and supervisor startup are bounded until their build declaration.
|
|
62
|
+
let failure: string | null = null;
|
|
63
|
+
return {
|
|
64
|
+
isTesting: (): boolean => failure !== null || phase === 'active' || (phase === 'terminal' && current < runners - 1),
|
|
65
|
+
error: (): string | null => failure,
|
|
66
|
+
finished: (): boolean => phase === 'terminal' && failure === null,
|
|
67
|
+
accept(line: string): RunnerPhaseEvent | null {
|
|
68
|
+
if (failure !== null) return null;
|
|
69
|
+
let event: unknown;
|
|
70
|
+
try {
|
|
71
|
+
event = JSON.parse(line);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
74
|
+
failure = 'invalid JSON on the runner lifecycle channel';
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
if (
|
|
78
|
+
typeof event !== 'object' ||
|
|
79
|
+
event === null ||
|
|
80
|
+
!('id' in event) ||
|
|
81
|
+
!('phase' in event) ||
|
|
82
|
+
typeof event.id !== 'number' ||
|
|
83
|
+
!Number.isSafeInteger(event.id) ||
|
|
84
|
+
event.id < current ||
|
|
85
|
+
event.id >= runners ||
|
|
86
|
+
(event.id === current && phase === 'terminal') ||
|
|
87
|
+
(event.phase !== 'build' && event.phase !== 'active' && event.phase !== 'terminal')
|
|
88
|
+
) {
|
|
89
|
+
failure = 'invalid or retired invocation on the runner lifecycle channel';
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
// && and || may skip invocations. A new supervisor can advance only after the previous one ended.
|
|
93
|
+
if (event.id !== current && phase !== 'terminal') {
|
|
94
|
+
failure = 'overlapping invocations on a serial runner lifecycle channel';
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
current = event.id;
|
|
98
|
+
phase = event.phase;
|
|
99
|
+
return { id: current, phase };
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -125,6 +125,143 @@ it('a client that may never start a daemon continues without one that stopped un
|
|
|
125
125
|
});
|
|
126
126
|
});
|
|
127
127
|
|
|
128
|
+
const taskOutput = (project: string) => `${project}/dist/${project}.txt`;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Two cacheable tasks: `p:build` runs `first` and writes its output, and `q:build` copies it. `q:build`'s inputs are
|
|
132
|
+
* `p:build`'s outputs, so it is hashed only once `p:build` has finished, and whatever `first` did to the daemon is
|
|
133
|
+
* behind every daemon request after it: that hash, recording outputs, the post-run hooks.
|
|
134
|
+
*/
|
|
135
|
+
async function twoTaskWorkspace(workspace: string, first: string): Promise<void> {
|
|
136
|
+
await fixtureWorkspace(workspace);
|
|
137
|
+
const build = (command: string) => ({ command, outputs: ['{projectRoot}/dist'], cache: true });
|
|
138
|
+
await writeFile(
|
|
139
|
+
join(workspace, 'p', 'project.json'),
|
|
140
|
+
JSON.stringify({
|
|
141
|
+
name: 'p',
|
|
142
|
+
targets: { build: build(`${first}; mkdir -p p/dist && echo p > ${taskOutput('p')}`) },
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
await mkdir(join(workspace, 'q'));
|
|
146
|
+
await writeFile(
|
|
147
|
+
join(workspace, 'q', 'project.json'),
|
|
148
|
+
JSON.stringify({
|
|
149
|
+
name: 'q',
|
|
150
|
+
implicitDependencies: ['p'],
|
|
151
|
+
targets: {
|
|
152
|
+
build: {
|
|
153
|
+
...build(`mkdir -p q/dist && cp ${taskOutput('p')} ${taskOutput('q')}`),
|
|
154
|
+
dependsOn: ['^build'],
|
|
155
|
+
inputs: ['default', { dependentTasksOutputFiles: '**/*.txt' }],
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
}),
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Start the fixture's daemon with a client allowed to, under `runtime`, and return the pid its record names. */
|
|
163
|
+
async function startDaemon(workspace: string, env: Record<string, string>, runtime: string): Promise<number> {
|
|
164
|
+
const ran = spawnSync(runtime, [nxEntry, 'show', 'projects', '--json'], { cwd: workspace, env, encoding: 'utf8' });
|
|
165
|
+
expect(ran.status, `${ran.stdout}${ran.stderr}`).toBe(0);
|
|
166
|
+
const daemon = await recordedPid(workspace);
|
|
167
|
+
if (daemon === null) throw new Error('a client allowed to start a daemon left no record');
|
|
168
|
+
return daemon;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** `nx run-many -t build` under the job rule; requires it to succeed with `q:build` built from `p:build`. */
|
|
172
|
+
async function runBothUnderNever(workspace: string, env: Record<string, string>, runtime: string): Promise<string> {
|
|
173
|
+
const run = spawnSync(runtime, [nxEntry, 'run-many', '-t', 'build', '--outputStyle=static'], {
|
|
174
|
+
cwd: workspace,
|
|
175
|
+
env: { ...env, NX_DAEMON_START: 'never' },
|
|
176
|
+
encoding: 'utf8',
|
|
177
|
+
});
|
|
178
|
+
const printed = `${run.stdout}${run.stderr}`;
|
|
179
|
+
expect(run.status, printed).toBe(0);
|
|
180
|
+
expect(await readFile(join(workspace, taskOutput('q')), 'utf8')).toBe('p\n');
|
|
181
|
+
// Not disabled for the checkout: the marker only `nx reset` clears would follow it into every terminal.
|
|
182
|
+
await expect(readFile(join(workspace, '.nx/workspace-data/d/disabled'), 'utf8')).rejects.toMatchObject({
|
|
183
|
+
code: 'ENOENT',
|
|
184
|
+
});
|
|
185
|
+
return printed;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The daemon stops while a run uses it, after the graph came from it: `p:build` kills it, and every daemon request
|
|
190
|
+
* after that goes to a daemon that is gone. Each continues in the client, the run finishes, and nothing is disabled
|
|
191
|
+
* or started.
|
|
192
|
+
*/
|
|
193
|
+
it('a run whose daemon stops under it continues without one, and disables nothing', async () => {
|
|
194
|
+
await withNxFixture('nx-daemon-start-midrun', async ({ workspace, socketDirectory }) => {
|
|
195
|
+
await twoTaskWorkspace(
|
|
196
|
+
workspace,
|
|
197
|
+
'kill -9 $STOP_DAEMON; while kill -0 $STOP_DAEMON 2>/dev/null; do sleep 0.05; done',
|
|
198
|
+
);
|
|
199
|
+
const env = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' };
|
|
200
|
+
const daemon = await startDaemon(workspace, env, 'bun');
|
|
201
|
+
|
|
202
|
+
const printed = await runBothUnderNever(workspace, { ...env, STOP_DAEMON: String(daemon) }, 'bun');
|
|
203
|
+
expect(isRunning(daemon)).toBe(false);
|
|
204
|
+
expect(printed).toContain(
|
|
205
|
+
'NX_DAEMON_START=never: this client never starts a daemon. Continuing without the daemon.',
|
|
206
|
+
);
|
|
207
|
+
// No successor: the record still names the daemon the task killed.
|
|
208
|
+
expect(await recordedPid(workspace)).toBe(daemon);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The daemon restarts itself while a run uses it: `p:build` writes a lockfile, so the next request finds the daemon
|
|
214
|
+
* outdated, and it starts its successor and ends the request's socket. The client waits for that successor and
|
|
215
|
+
* resumes on it, as the job rule allows: its starter is the restarting daemon, not the client. The run finishes.
|
|
216
|
+
*/
|
|
217
|
+
it('a run whose daemon restarts itself under it finishes', async () => {
|
|
218
|
+
await withNxFixture('nx-daemon-start-restart', async ({ workspace, socketDirectory }) => {
|
|
219
|
+
// A lockfile Nx parses: an unparsable one fails the successor's graph, which is a different failure.
|
|
220
|
+
const lockfile = JSON.stringify({
|
|
221
|
+
name: 'daemon-start-never',
|
|
222
|
+
lockfileVersion: 3,
|
|
223
|
+
requires: true,
|
|
224
|
+
packages: { '': { name: 'daemon-start-never' } },
|
|
225
|
+
});
|
|
226
|
+
await twoTaskWorkspace(workspace, `echo '${lockfile}' > package-lock.json`);
|
|
227
|
+
const env = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' };
|
|
228
|
+
await startDaemon(workspace, env, 'node');
|
|
229
|
+
|
|
230
|
+
await runBothUnderNever(workspace, env, 'node');
|
|
231
|
+
expect(await readFile(join(workspace, '.nx/workspace-data/d/daemon.log'), 'utf8')).toContain('Restarting daemon');
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A restarting daemon opens the log twice for its successor's stdout and stderr. Left to garbage collection, Node >=26
|
|
237
|
+
* turns each collected `FileHandle` into a fatal `ERR_INVALID_STATE`, which ended a restarting daemon in a job with
|
|
238
|
+
* the socket its clients were still writing to. Collected deliberately here, right after the restart's spawn, so the
|
|
239
|
+
* descriptors' lifetime decides the exit rather than when a collection happens to run.
|
|
240
|
+
*/
|
|
241
|
+
it('a restarting daemon closes the log descriptors it hands its successor', async () => {
|
|
242
|
+
await withNxFixture('nx-daemon-restart-descriptors', async ({ workspace, socketDirectory }) => {
|
|
243
|
+
await fixtureWorkspace(workspace);
|
|
244
|
+
const shutdownUtils = join(repositoryRoot, 'node_modules/nx/dist/src/daemon/server/shutdown-utils.js');
|
|
245
|
+
const restart = `const { setTimeout: sleep } = require('node:timers/promises');
|
|
246
|
+
require(${JSON.stringify(shutdownUtils)}).startNewDaemonInBackground().then(async () => {
|
|
247
|
+
for (let round = 0; round < 3; round++) { gc(); await sleep(50); }
|
|
248
|
+
})`;
|
|
249
|
+
const ran = spawnSync('node', ['--expose-gc', '-e', restart], {
|
|
250
|
+
cwd: workspace,
|
|
251
|
+
env: { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' },
|
|
252
|
+
encoding: 'utf8',
|
|
253
|
+
});
|
|
254
|
+
expect(ran.status, `${ran.stdout}${ran.stderr}`).toBe(0);
|
|
255
|
+
expect(ran.stderr).not.toContain('ERR_INVALID_STATE');
|
|
256
|
+
// The successor is the fixture's to stop: wait for the record it writes once it serves.
|
|
257
|
+
const deadline = Date.now() + 60_000;
|
|
258
|
+
while ((await recordedPid(workspace)) === null) {
|
|
259
|
+
if (Date.now() > deadline) throw new Error('the restarted daemon wrote no record within 60 s');
|
|
260
|
+
await Bun.sleep(100);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
128
265
|
/**
|
|
129
266
|
* An explicit `nx daemon --start` calls the daemon start directly, past the implicit-start check above, so under the
|
|
130
267
|
* job's rule it is refused as policy: a non-zero exit naming `NX_DAEMON_START=never` and the fixture escape, and no
|