@smoothbricks/nx-plugin 0.4.24 → 0.4.26
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 +147 -37
- package/dist/bin/smoo-nx-exec.js +3 -14
- package/dist/cargo-closure-input.d.ts +13 -15
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +29 -37
- package/dist/cargo-source-hash.d.ts +12 -7
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +416 -32
- package/dist/cargo-workspace.d.ts +7 -7
- package/dist/cargo-workspace.js +8 -8
- package/dist/ensure-built.d.ts +6 -0
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +79 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +69 -96
- package/dist/managed-assets.d.ts +6 -0
- package/dist/managed-assets.d.ts.map +1 -1
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +6 -0
- package/dist/manifest-hash.d.ts.map +1 -1
- package/dist/manifest-hash.js +6 -2
- package/dist/workspace-manifest.d.ts +0 -1
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/managed/raw/envrc +15 -0
- package/managed/raw/git-format-staged.yml +1 -0
- package/managed/raw/tooling/direnv/developer-links.ts +43 -11
- package/managed/raw/tooling/direnv/devenv.smoo.nix +5 -0
- package/managed/raw/tooling/direnv/inherited-devenv.ts +665 -0
- package/managed/raw/tooling/direnv/setup-environment.ts +123 -7
- package/nextest.toml +3 -37
- package/package.json +40 -3
- package/src/__tests__/counted-cargo-process.ts +286 -0
- package/src/__tests__/counted-cargo.ts +711 -0
- package/src/__tests__/fixture-cargo-home.ts +36 -0
- package/src/__tests__/fixture-nx-env.ts +93 -3
- package/src/bin/smoo-nx-exec.ts +3 -14
- package/src/bounded-test-policy.test.ts +0 -1
- package/src/cargo-closure-input.test.ts +313 -15
- package/src/cargo-closure-input.ts +42 -50
- package/src/cargo-resolve-cache.test.ts +975 -0
- package/src/cargo-source-hash.test.ts +17 -3
- package/src/cargo-source-hash.ts +447 -37
- package/src/cargo-workspace.ts +8 -8
- package/src/ensure-built.test.ts +122 -20
- package/src/ensure-built.ts +82 -5
- package/src/index.test.ts +203 -107
- package/src/index.ts +86 -115
- package/src/managed-assets.ts +12 -0
- package/src/managed-files/files.ts +6 -0
- package/src/manifest-hash.ts +7 -2
- package/src/nextest-partitions.test.ts +150 -0
- package/src/nx-cache-bound.test.ts +114 -0
- package/src/nx-resident-graph-workers.test.ts +121 -0
- package/src/nx-store-resolution.test.ts +98 -0
- package/src/task-history-namespace.test.ts +183 -0
- package/src/workspace-manifest.ts +0 -1
- package/test-timings.json +35 -0
- package/src/__tests__/in-process-nx-index.ts +0 -9
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
import { expect } from 'bun:test';
|
|
2
|
+
import { subscribe, unsubscribe } from 'node:diagnostics_channel';
|
|
3
|
+
import { realpathSync } from 'node:fs';
|
|
4
|
+
import { chmod, mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
5
|
+
import { createServer, type Server, type Socket } from 'node:net';
|
|
6
|
+
import { delimiter, join } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
|
|
9
|
+
import type { LogSchema, OpContextBinding } from '@smoothbricks/lmao';
|
|
10
|
+
import { type QueryableSpan, querySpan } from '@smoothbricks/lmao/testing';
|
|
11
|
+
import { useTestSpan } from '@smoothbricks/lmao/testing/bun';
|
|
12
|
+
import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
|
|
13
|
+
import { CargoMetadataError } from '../cargo-source-hash.js';
|
|
14
|
+
import { BROKER_HOST, encodeOrder, type ProcessReport, parseReport, readLines } from './counted-cargo-process.js';
|
|
15
|
+
|
|
16
|
+
/** What one fixture's `cargo metadata` children have done so far, in the order they did it. */
|
|
17
|
+
export interface CargoMetadataRuns {
|
|
18
|
+
/** `cargo metadata` children started. */
|
|
19
|
+
readonly spawned: number;
|
|
20
|
+
/** Those whose Cargo has read the workspace, whether or not the answer has been handed over yet. */
|
|
21
|
+
readonly read: number;
|
|
22
|
+
/** The most that were alive at once; a child killed before it answered never counts as ended. */
|
|
23
|
+
readonly peak: number;
|
|
24
|
+
/** Each child's pid, in the order they started. */
|
|
25
|
+
readonly pids: readonly number[];
|
|
26
|
+
/** Processes the children left running behind them (see `leaveGrandchildren`). */
|
|
27
|
+
readonly grandchildren: readonly number[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** One step of a `cargo metadata` child's life, as the broker received it. */
|
|
31
|
+
export interface CargoMetadataEvent {
|
|
32
|
+
readonly kind: 'start' | 'read' | 'end' | 'grandchild';
|
|
33
|
+
/** The child's pid; for `grandchild`, the pid of the process the child left behind. */
|
|
34
|
+
readonly pid: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The events are in the order things happened, so a running count over them is the true concurrency. */
|
|
38
|
+
export function cargoMetadataRuns(events: readonly CargoMetadataEvent[]): CargoMetadataRuns {
|
|
39
|
+
const pids: number[] = [];
|
|
40
|
+
const grandchildren: number[] = [];
|
|
41
|
+
let read = 0;
|
|
42
|
+
let alive = 0;
|
|
43
|
+
let peak = 0;
|
|
44
|
+
for (const { kind, pid } of events) {
|
|
45
|
+
switch (kind) {
|
|
46
|
+
case 'start':
|
|
47
|
+
pids.push(pid);
|
|
48
|
+
alive += 1;
|
|
49
|
+
peak = Math.max(peak, alive);
|
|
50
|
+
break;
|
|
51
|
+
case 'read':
|
|
52
|
+
read += 1;
|
|
53
|
+
break;
|
|
54
|
+
case 'end':
|
|
55
|
+
alive -= 1;
|
|
56
|
+
break;
|
|
57
|
+
case 'grandchild':
|
|
58
|
+
grandchildren.push(pid);
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return { spawned: pids.length, read, peak, pids, grandchildren };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* How long an awaited event may take before its guard says what is still open: inside the tests'
|
|
67
|
+
* own 20 s budgets, so the report reaches the test before the runner's timeout does. A guard only
|
|
68
|
+
* ever fails a wait; nothing advances on it.
|
|
69
|
+
*/
|
|
70
|
+
const GUARD_MS = 15_000;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The guard over the real Cargo: above the slowest real resolution seen behind a contended package
|
|
74
|
+
* cache (21 s), below Bun's 30 s default test timeout, which those tests run under or exceed.
|
|
75
|
+
*/
|
|
76
|
+
const REAL_CARGO_GUARD_MS = 25_000;
|
|
77
|
+
|
|
78
|
+
/** The test spans still open under `span`, itself included: the work a hung test is still inside. */
|
|
79
|
+
function openSpans<T extends LogSchema>(span: QueryableSpan<T>, open: string[]): string[] {
|
|
80
|
+
// Row 1 holds a span's completion; its timestamp stays 0 until the span ends.
|
|
81
|
+
if (span.buffer.timestamp[1] === 0n) open.push(span.name);
|
|
82
|
+
for (const child of span.children) openSpans(child, open);
|
|
83
|
+
return open;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* `operation`, settling exactly as it does, unless no event settles it within `timeoutMs`: then it
|
|
88
|
+
* rejects with `what`, the test spans still open and `diagnostics()`. The test span is taken now, so
|
|
89
|
+
* call this from the test's own async context. `signal` disarms the guard; the result still follows
|
|
90
|
+
* `operation`.
|
|
91
|
+
*/
|
|
92
|
+
export function guardEvent<T>(
|
|
93
|
+
operation: Promise<T>,
|
|
94
|
+
what: string,
|
|
95
|
+
diagnostics: () => string | Promise<string>,
|
|
96
|
+
signal?: AbortSignal,
|
|
97
|
+
timeoutMs = GUARD_MS,
|
|
98
|
+
): Promise<T> {
|
|
99
|
+
let spans: () => string;
|
|
100
|
+
try {
|
|
101
|
+
const test = querySpan(useTestSpan<OpContextBinding>().buffer);
|
|
102
|
+
spans = () => {
|
|
103
|
+
try {
|
|
104
|
+
return `open test spans: [${openSpans(test, []).join(', ')}]`;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
return `open test spans unreadable: ${error instanceof Error ? error.message : String(error)}`;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
} catch (error) {
|
|
110
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
111
|
+
spans = () => `no test span to report: ${reason}`;
|
|
112
|
+
}
|
|
113
|
+
return new Promise<T>((resolve, reject) => {
|
|
114
|
+
const report = async (): Promise<void> => {
|
|
115
|
+
let details: string;
|
|
116
|
+
try {
|
|
117
|
+
details = await diagnostics();
|
|
118
|
+
} catch (error) {
|
|
119
|
+
details = `diagnostics failed: ${error instanceof Error ? (error.stack ?? error.message) : String(error)}`;
|
|
120
|
+
}
|
|
121
|
+
reject(new Error(`${what}: no event within ${timeoutMs} ms\n${spans()}\n${details}`));
|
|
122
|
+
};
|
|
123
|
+
// Kept referenced: when nothing else is left to wake the process, the report still comes.
|
|
124
|
+
const timer = setTimeout(() => void report(), timeoutMs);
|
|
125
|
+
const disarm = () => clearTimeout(timer);
|
|
126
|
+
if (signal?.aborted) disarm();
|
|
127
|
+
else signal?.addEventListener('abort', disarm, { once: true });
|
|
128
|
+
operation.then(resolve, reject).finally(() => {
|
|
129
|
+
disarm();
|
|
130
|
+
signal?.removeEventListener('abort', disarm);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Where `readCargoResolve` says a call joined a resolution already in flight. */
|
|
136
|
+
const CARGO_RESOLVE_CHANNEL = '@smoothbricks/nx-plugin:cargo-resolve';
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Counts the calls that join a resolution of `manifest` already in flight in this process, from now:
|
|
140
|
+
* subscribe before the calls start. `joined` resolves once `count` have joined. `dispose` stops
|
|
141
|
+
* counting and disarms the guard; a `joined` not yet reached then never settles.
|
|
142
|
+
*/
|
|
143
|
+
export function waitForCargoJoins(manifest: string, count: number): { joined: Promise<void>; dispose(): void } {
|
|
144
|
+
const canonical = realpathSync(manifest);
|
|
145
|
+
const elsewhere: string[] = [];
|
|
146
|
+
const malformed: string[] = [];
|
|
147
|
+
const abort = new AbortController();
|
|
148
|
+
let seen = 0;
|
|
149
|
+
let listener: ((message: unknown) => void) | null = null;
|
|
150
|
+
const stop = () => {
|
|
151
|
+
if (listener !== null) unsubscribe(CARGO_RESOLVE_CHANNEL, listener);
|
|
152
|
+
listener = null;
|
|
153
|
+
};
|
|
154
|
+
const counted = new Promise<void>((resolve) => {
|
|
155
|
+
const onMessage = (message: unknown) => {
|
|
156
|
+
if (typeof message !== 'object' || message === null || !('phase' in message) || !('manifest' in message)) {
|
|
157
|
+
malformed.push(String(message));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (message.phase !== 'join') return;
|
|
161
|
+
if (message.manifest !== canonical) {
|
|
162
|
+
elsewhere.push(String(message.manifest));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
seen += 1;
|
|
166
|
+
if (seen === count) {
|
|
167
|
+
stop();
|
|
168
|
+
resolve();
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
listener = onMessage;
|
|
172
|
+
subscribe(CARGO_RESOLVE_CHANNEL, onMessage);
|
|
173
|
+
});
|
|
174
|
+
const joined = guardEvent(
|
|
175
|
+
counted,
|
|
176
|
+
`waiting for ${count} calls to join the resolution of ${canonical}`,
|
|
177
|
+
() =>
|
|
178
|
+
`saw ${seen} of ${count} joins on ${CARGO_RESOLVE_CHANNEL}; ` +
|
|
179
|
+
`joins of other manifests: [${elsewhere.join(', ')}]; malformed messages: [${malformed.join(', ')}]`,
|
|
180
|
+
abort.signal,
|
|
181
|
+
);
|
|
182
|
+
return {
|
|
183
|
+
joined,
|
|
184
|
+
dispose: () => {
|
|
185
|
+
stop();
|
|
186
|
+
abort.abort();
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** A gate a test closes to hold every `cargo metadata` child at one point of its life, and opens to let them go on. */
|
|
192
|
+
export interface Latch {
|
|
193
|
+
close(): Promise<void>;
|
|
194
|
+
open(): Promise<void>;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export interface CountedCargo {
|
|
198
|
+
/** The directory holding the `cargo` that goes ahead of the real one. */
|
|
199
|
+
readonly bin: string;
|
|
200
|
+
/**
|
|
201
|
+
* What a process under test needs: this Cargo ahead of everything else on PATH, and the Nx
|
|
202
|
+
* workspace-data directory (where Cargo resolutions are kept between processes) inside the
|
|
203
|
+
* fixture, so the persistent cache is the fixture's own and is deleted with it, whatever
|
|
204
|
+
* `NX_WORKSPACE_DATA_DIRECTORY` the test run itself inherited.
|
|
205
|
+
*/
|
|
206
|
+
readonly environment: Readonly<Record<string, string>>;
|
|
207
|
+
/** Hold every child before it reads the workspace: an edit made now is one its answer includes. */
|
|
208
|
+
readonly beforeRead: Latch;
|
|
209
|
+
/** Hold every child after it read the workspace: an edit made now is one its answer predates. */
|
|
210
|
+
readonly beforeAnswer: Latch;
|
|
211
|
+
/** While set, a child that has read answers with this on stderr and exit code 101 instead. */
|
|
212
|
+
failWith(stderr: string): Promise<void>;
|
|
213
|
+
recover(): Promise<void>;
|
|
214
|
+
/** Whether every child leaves a background process running behind it, as a real Cargo does with rustc. */
|
|
215
|
+
leaveGrandchildren(leave: boolean): Promise<void>;
|
|
216
|
+
runs(): Promise<CargoMetadataRuns>;
|
|
217
|
+
/**
|
|
218
|
+
* The first runs `predicate` holds for: now, or after whichever child's report makes it hold. A
|
|
219
|
+
* child counts as `read`, and its grandchild among `grandchildren`, only once that grandchild is
|
|
220
|
+
* running and connected, so a predicate over both sees them together.
|
|
221
|
+
*/
|
|
222
|
+
waitForRuns(predicate: (runs: CargoMetadataRuns) => boolean, what: string): Promise<CargoMetadataRuns>;
|
|
223
|
+
/**
|
|
224
|
+
* Resolves once every one of `pids` (children and grandchildren) has died: its connection to the
|
|
225
|
+
* fixture closed without the child having finished. Rejects for a pid that never connected, or one
|
|
226
|
+
* that ended normally or was let go by `release`, since none of those died.
|
|
227
|
+
*/
|
|
228
|
+
waitForDisconnects(pids: readonly number[]): Promise<void>;
|
|
229
|
+
/**
|
|
230
|
+
* Open every latch, end every grandchild, stop accepting children, and wait until every process
|
|
231
|
+
* the fixture started has gone, so nothing outlives it. Idempotent.
|
|
232
|
+
*/
|
|
233
|
+
release(): Promise<void>;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface CannedCargo extends CountedCargo {
|
|
237
|
+
/** What `cargo metadata` prints from now on: this document, as JSON. */
|
|
238
|
+
answerWith(metadata: unknown): Promise<void>;
|
|
239
|
+
/** What `cargo metadata` prints from now on: this text, whatever it is. */
|
|
240
|
+
answerWithText(text: string): Promise<void>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** How a process's connection to the fixture ended. */
|
|
244
|
+
type Departure =
|
|
245
|
+
/** A child that finished: it said `end` before hanging up. */
|
|
246
|
+
| 'ended'
|
|
247
|
+
/** A grandchild the fixture let go on `release`. */
|
|
248
|
+
| 'released'
|
|
249
|
+
/** Died: hung up without finishing, killed or crashed (its stderr says which). */
|
|
250
|
+
| 'vanished';
|
|
251
|
+
|
|
252
|
+
/** Where a `cargo metadata` child is in its life: each step is taken when its gate lets it. */
|
|
253
|
+
type ChildPhase =
|
|
254
|
+
| 'before-read'
|
|
255
|
+
| 'reading'
|
|
256
|
+
/** It has read; the grandchild it named has not connected yet. */
|
|
257
|
+
| 'awaiting-grandchild'
|
|
258
|
+
| 'before-answer'
|
|
259
|
+
| 'answering'
|
|
260
|
+
| 'ended';
|
|
261
|
+
|
|
262
|
+
interface MetadataChild {
|
|
263
|
+
readonly role: 'metadata';
|
|
264
|
+
readonly pid: number;
|
|
265
|
+
readonly socket: Socket;
|
|
266
|
+
phase: ChildPhase;
|
|
267
|
+
grandchild: number | null;
|
|
268
|
+
status: number | null;
|
|
269
|
+
departure: Departure | null;
|
|
270
|
+
error: string | null;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
interface Grandchild {
|
|
274
|
+
readonly role: 'grandchild';
|
|
275
|
+
readonly pid: number;
|
|
276
|
+
readonly parent: number;
|
|
277
|
+
readonly socket: Socket;
|
|
278
|
+
released: boolean;
|
|
279
|
+
departure: Departure | null;
|
|
280
|
+
error: string | null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
type Registered = MetadataChild | Grandchild;
|
|
284
|
+
|
|
285
|
+
const quote = (value: string): string => `'${value.replaceAll("'", `'"'"'`)}'`;
|
|
286
|
+
|
|
287
|
+
const PROGRAM = fileURLToPath(new URL('./counted-cargo-process.ts', import.meta.url));
|
|
288
|
+
|
|
289
|
+
function listen(server: Server): Promise<number> {
|
|
290
|
+
return new Promise((resolve, reject) => {
|
|
291
|
+
server.once('error', reject);
|
|
292
|
+
server.listen(0, BROKER_HOST, () => {
|
|
293
|
+
server.off('error', reject);
|
|
294
|
+
const address = server.address();
|
|
295
|
+
if (address === null || typeof address === 'string') reject(new Error(`the broker has no port: ${address}`));
|
|
296
|
+
else resolve(address.port);
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* A `cargo` ahead of the real one on PATH, and the broker in this process it reports to. Every
|
|
303
|
+
* subcommand but `metadata` runs the real Cargo (`forward` is the command line that does). A
|
|
304
|
+
* `metadata` child is `counted-cargo-process.ts` under the same pid and process group; it reports
|
|
305
|
+
* `start` as it begins, `read` once it has its answer, and `end` after the answer has been handed
|
|
306
|
+
* over, so the broker orders each child's life against every other's. Its answer is the real
|
|
307
|
+
* `cargo metadata`'s while `canned` is null, else `canned`, as it is when the child reads.
|
|
308
|
+
*/
|
|
309
|
+
async function shim(
|
|
310
|
+
root: string,
|
|
311
|
+
forward: string,
|
|
312
|
+
initial: string | null,
|
|
313
|
+
/** How long the fixture's waits may take: longer over the real Cargo. */
|
|
314
|
+
guardMs: number,
|
|
315
|
+
): Promise<{ cargo: CountedCargo; answer(text: string): void }> {
|
|
316
|
+
const real = Bun.which('cargo');
|
|
317
|
+
if (real === null) throw new Error('cargo is not on PATH');
|
|
318
|
+
const directory = join(root, 'counted-cargo');
|
|
319
|
+
const bin = join(directory, 'bin');
|
|
320
|
+
|
|
321
|
+
const events: CargoMetadataEvent[] = [];
|
|
322
|
+
const processes = new Map<number, Registered>();
|
|
323
|
+
const connections = new Set<Socket>();
|
|
324
|
+
const listeners = new Set<() => void>();
|
|
325
|
+
const faults: string[] = [];
|
|
326
|
+
const torn: string[] = [];
|
|
327
|
+
const closed = { read: false, answer: false };
|
|
328
|
+
let canned = initial;
|
|
329
|
+
let failure: string | null = null;
|
|
330
|
+
let leave = false;
|
|
331
|
+
let releasing = false;
|
|
332
|
+
let listening = true;
|
|
333
|
+
let released: Promise<void> | null = null;
|
|
334
|
+
|
|
335
|
+
const changed = () => {
|
|
336
|
+
for (const listener of [...listeners]) listener();
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/** Settles with the first verdict `judge` gives: now, or after a later broker event; a fault fails it. */
|
|
340
|
+
const verdict = <T>(judge: () => T | Error | null): Promise<T> =>
|
|
341
|
+
new Promise((resolve, reject) => {
|
|
342
|
+
const listener = () => {
|
|
343
|
+
const result = faults.length > 0 ? new Error(`the counted Cargo broke: ${faults.join('; ')}`) : judge();
|
|
344
|
+
if (result === null) return;
|
|
345
|
+
listeners.delete(listener);
|
|
346
|
+
if (result instanceof Error) reject(result);
|
|
347
|
+
else resolve(result);
|
|
348
|
+
};
|
|
349
|
+
listeners.add(listener);
|
|
350
|
+
listener();
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
/** Takes every step `child` may take now, in order; a child that has gone takes none. */
|
|
354
|
+
const advance = (child: MetadataChild) => {
|
|
355
|
+
if (child.departure !== null) return;
|
|
356
|
+
if (child.phase === 'before-read' && !closed.read) {
|
|
357
|
+
child.phase = 'reading';
|
|
358
|
+
child.socket.write(encodeOrder({ kind: 'read', grandchild: leave, canned }));
|
|
359
|
+
}
|
|
360
|
+
if (child.phase === 'awaiting-grandchild') {
|
|
361
|
+
const left = child.grandchild === null ? undefined : processes.get(child.grandchild);
|
|
362
|
+
if (child.grandchild !== null && left?.role !== 'grandchild') return;
|
|
363
|
+
if (child.grandchild !== null) events.push({ kind: 'grandchild', pid: child.grandchild });
|
|
364
|
+
events.push({ kind: 'read', pid: child.pid });
|
|
365
|
+
child.phase = 'before-answer';
|
|
366
|
+
}
|
|
367
|
+
if (child.phase === 'before-answer' && !closed.answer) {
|
|
368
|
+
child.phase = 'answering';
|
|
369
|
+
child.socket.write(encodeOrder({ kind: 'answer', failure }));
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
const advanceAll = () => {
|
|
373
|
+
for (const record of processes.values()) if (record.role === 'metadata') advance(record);
|
|
374
|
+
changed();
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/** What `report` makes of the connection that sent it: the process it registers, or the one it was. */
|
|
378
|
+
const receive = (self: Registered | null, report: ProcessReport, socket: Socket): Registered | string => {
|
|
379
|
+
if (self === null) {
|
|
380
|
+
if (report.kind !== 'start' && report.kind !== 'grandchild') return `first report was ${report.kind}`;
|
|
381
|
+
if (processes.has(report.pid)) return `pid ${report.pid} registered twice`;
|
|
382
|
+
if (report.kind === 'start') {
|
|
383
|
+
const child: MetadataChild = {
|
|
384
|
+
role: 'metadata',
|
|
385
|
+
pid: report.pid,
|
|
386
|
+
socket,
|
|
387
|
+
phase: 'before-read',
|
|
388
|
+
grandchild: null,
|
|
389
|
+
status: null,
|
|
390
|
+
departure: null,
|
|
391
|
+
error: null,
|
|
392
|
+
};
|
|
393
|
+
processes.set(child.pid, child);
|
|
394
|
+
events.push({ kind: 'start', pid: child.pid });
|
|
395
|
+
advance(child);
|
|
396
|
+
return child;
|
|
397
|
+
}
|
|
398
|
+
const grandchild: Grandchild = {
|
|
399
|
+
role: 'grandchild',
|
|
400
|
+
pid: report.pid,
|
|
401
|
+
parent: report.parent,
|
|
402
|
+
socket,
|
|
403
|
+
released: releasing,
|
|
404
|
+
departure: null,
|
|
405
|
+
error: null,
|
|
406
|
+
};
|
|
407
|
+
processes.set(grandchild.pid, grandchild);
|
|
408
|
+
if (releasing) socket.end();
|
|
409
|
+
const parent = processes.get(report.parent);
|
|
410
|
+
if (parent?.role === 'metadata') advance(parent);
|
|
411
|
+
return grandchild;
|
|
412
|
+
}
|
|
413
|
+
if (self.role === 'grandchild') return `grandchild ${self.pid} reported ${report.kind}`;
|
|
414
|
+
if (report.kind === 'read' && self.phase === 'reading') {
|
|
415
|
+
self.grandchild = report.grandchild;
|
|
416
|
+
self.phase = 'awaiting-grandchild';
|
|
417
|
+
advance(self);
|
|
418
|
+
return self;
|
|
419
|
+
}
|
|
420
|
+
if (report.kind === 'end' && self.phase === 'answering') {
|
|
421
|
+
self.phase = 'ended';
|
|
422
|
+
self.status = report.status;
|
|
423
|
+
events.push({ kind: 'end', pid: self.pid });
|
|
424
|
+
return self;
|
|
425
|
+
}
|
|
426
|
+
return `child ${self.pid} reported ${report.kind} while ${self.phase}`;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
const server = createServer((socket) => {
|
|
430
|
+
connections.add(socket);
|
|
431
|
+
let self: Registered | null = null;
|
|
432
|
+
const unterminated = readLines(socket, (line) => {
|
|
433
|
+
const report = parseReport(line);
|
|
434
|
+
const outcome = report instanceof Error ? report.message : receive(self, report, socket);
|
|
435
|
+
if (typeof outcome === 'string') {
|
|
436
|
+
faults.push(outcome);
|
|
437
|
+
socket.destroy();
|
|
438
|
+
} else {
|
|
439
|
+
self = outcome;
|
|
440
|
+
}
|
|
441
|
+
changed();
|
|
442
|
+
});
|
|
443
|
+
socket.on('error', (error) => {
|
|
444
|
+
// A process killed while the broker writes to it resets the connection; its close says the rest.
|
|
445
|
+
if (self !== null) self.error = error.message;
|
|
446
|
+
});
|
|
447
|
+
socket.on('close', () => {
|
|
448
|
+
connections.delete(socket);
|
|
449
|
+
// A process that died mid-write leaves part of a line: evidence of how it died, not a broken broker.
|
|
450
|
+
const tail = unterminated();
|
|
451
|
+
if (tail !== '') torn.push(`${self === null ? 'unregistered' : `${self.role} ${self.pid}`}: ${tail}`);
|
|
452
|
+
if (self !== null) {
|
|
453
|
+
if (self.role === 'metadata') self.departure = self.phase === 'ended' ? 'ended' : 'vanished';
|
|
454
|
+
else self.departure = self.released ? 'released' : 'vanished';
|
|
455
|
+
}
|
|
456
|
+
changed();
|
|
457
|
+
});
|
|
458
|
+
});
|
|
459
|
+
server.on('close', () => {
|
|
460
|
+
listening = false;
|
|
461
|
+
changed();
|
|
462
|
+
});
|
|
463
|
+
const port = await listen(server);
|
|
464
|
+
// Only the processes it serves keep the test process alive, never the listener itself.
|
|
465
|
+
server.unref();
|
|
466
|
+
|
|
467
|
+
/** Everything the broker knows, for a guard that ran out of patience. */
|
|
468
|
+
const describe = async (): Promise<string> => {
|
|
469
|
+
const lines = [
|
|
470
|
+
`counted Cargo broker ${BROKER_HOST}:${port}: ${listening ? 'listening' : 'closed'}${releasing ? ', releasing' : ''}`,
|
|
471
|
+
`gates: before-read ${closed.read ? 'closed' : 'open'}, before-answer ${closed.answer ? 'closed' : 'open'}; ` +
|
|
472
|
+
`failing: ${failure !== null}; leaving grandchildren: ${leave}`,
|
|
473
|
+
`runs: ${JSON.stringify(cargoMetadataRuns(events))}`,
|
|
474
|
+
`connections open: ${connections.size}`,
|
|
475
|
+
];
|
|
476
|
+
for (const record of processes.values()) {
|
|
477
|
+
const state = record.departure === null ? 'connected' : `gone (${record.departure})`;
|
|
478
|
+
const socket = record.error === null ? '' : `, socket error: ${record.error}`;
|
|
479
|
+
if (record.role === 'metadata') {
|
|
480
|
+
lines.push(
|
|
481
|
+
`metadata child ${record.pid}: ${record.phase}, ${state}, grandchild ${record.grandchild ?? 'none'}, ` +
|
|
482
|
+
`status ${record.status ?? 'none'}${socket}`,
|
|
483
|
+
);
|
|
484
|
+
} else {
|
|
485
|
+
let log: string;
|
|
486
|
+
try {
|
|
487
|
+
log = await readFile(join(directory, `grandchild-of-${record.parent}.log`), 'utf8');
|
|
488
|
+
} catch (error) {
|
|
489
|
+
log = `unreadable: ${error instanceof Error ? error.message : String(error)}`;
|
|
490
|
+
}
|
|
491
|
+
lines.push(`grandchild ${record.pid} of ${record.parent}: ${state}${socket}; log: ${JSON.stringify(log)}`);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (faults.length > 0) lines.push(`faults: ${faults.join('; ')}`);
|
|
495
|
+
if (torn.length > 0) lines.push(`partial reports from processes that died: ${torn.join('; ')}`);
|
|
496
|
+
return lines.join('\n');
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
/** SIGKILL `target` (a pid, or a negated process-group id); a target already gone is no failure. */
|
|
500
|
+
const kill = (target: number, failures: string[]) => {
|
|
501
|
+
try {
|
|
502
|
+
process.kill(target, 'SIGKILL');
|
|
503
|
+
} catch (error) {
|
|
504
|
+
if (!(error instanceof Error && 'code' in error && error.code === 'ESRCH')) {
|
|
505
|
+
failures.push(`kill ${target}: ${error instanceof Error ? error.message : String(error)}`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
const drain = async (): Promise<void> => {
|
|
511
|
+
// Gates first, so every held child answers; then the grandchildren, which live until let go.
|
|
512
|
+
releasing = true;
|
|
513
|
+
closed.read = false;
|
|
514
|
+
closed.answer = false;
|
|
515
|
+
server.close();
|
|
516
|
+
for (const record of processes.values()) {
|
|
517
|
+
if (record.role === 'grandchild' && record.departure === null) {
|
|
518
|
+
record.released = true;
|
|
519
|
+
record.socket.end();
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
advanceAll();
|
|
523
|
+
try {
|
|
524
|
+
await guardEvent(
|
|
525
|
+
verdict(() => (connections.size === 0 ? true : null)),
|
|
526
|
+
'releasing the counted Cargo',
|
|
527
|
+
describe,
|
|
528
|
+
undefined,
|
|
529
|
+
guardMs,
|
|
530
|
+
);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
// A process still connected is still running, so its pid is still its own: kill it and, for a
|
|
533
|
+
// child, the process group it leads (`readCargoResolve` starts each one detached), with whatever
|
|
534
|
+
// real Cargo or grandchild is in it. Nothing the fixture started outlives a failed release.
|
|
535
|
+
const failures: string[] = [];
|
|
536
|
+
const killed: string[] = [];
|
|
537
|
+
for (const record of processes.values()) {
|
|
538
|
+
if (record.departure !== null) continue;
|
|
539
|
+
if (record.role === 'metadata') kill(-record.pid, failures);
|
|
540
|
+
kill(record.pid, failures);
|
|
541
|
+
killed.push(`${record.role} ${record.pid}`);
|
|
542
|
+
}
|
|
543
|
+
for (const socket of connections) socket.destroy();
|
|
544
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
545
|
+
throw new Error(
|
|
546
|
+
`${message}\nkilled: [${killed.join(', ')}]${failures.length > 0 ? `; kill failures: ${failures.join('; ')}` : ''}`,
|
|
547
|
+
{ cause: error },
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
await mkdir(bin, { recursive: true });
|
|
553
|
+
await writeFile(
|
|
554
|
+
join(bin, 'cargo'),
|
|
555
|
+
[
|
|
556
|
+
'#!/bin/sh',
|
|
557
|
+
`real=${quote(real)}`,
|
|
558
|
+
`if [ "$1" != metadata ]; then ${forward}; fi`,
|
|
559
|
+
`exec ${quote(process.execPath)} ${quote(PROGRAM)} metadata ${port} "$real" ${quote(directory)} "$@"`,
|
|
560
|
+
'',
|
|
561
|
+
].join('\n'),
|
|
562
|
+
);
|
|
563
|
+
await chmod(join(bin, 'cargo'), 0o755);
|
|
564
|
+
|
|
565
|
+
const latch = (gate: 'read' | 'answer'): Latch => ({
|
|
566
|
+
close: async () => {
|
|
567
|
+
closed[gate] = true;
|
|
568
|
+
},
|
|
569
|
+
open: async () => {
|
|
570
|
+
closed[gate] = false;
|
|
571
|
+
advanceAll();
|
|
572
|
+
},
|
|
573
|
+
});
|
|
574
|
+
const cargo: CountedCargo = {
|
|
575
|
+
bin,
|
|
576
|
+
environment: {
|
|
577
|
+
PATH: `${bin}${delimiter}${process.env.PATH ?? ''}`,
|
|
578
|
+
NX_WORKSPACE_DATA_DIRECTORY: join(root, '.nx/workspace-data'),
|
|
579
|
+
},
|
|
580
|
+
beforeRead: latch('read'),
|
|
581
|
+
beforeAnswer: latch('answer'),
|
|
582
|
+
failWith: async (stderr) => {
|
|
583
|
+
failure = stderr;
|
|
584
|
+
},
|
|
585
|
+
recover: async () => {
|
|
586
|
+
failure = null;
|
|
587
|
+
},
|
|
588
|
+
leaveGrandchildren: async (value) => {
|
|
589
|
+
leave = value;
|
|
590
|
+
},
|
|
591
|
+
runs: async () => cargoMetadataRuns(events),
|
|
592
|
+
waitForRuns: (predicate, what) =>
|
|
593
|
+
guardEvent(
|
|
594
|
+
verdict(() => {
|
|
595
|
+
const runs = cargoMetadataRuns(events);
|
|
596
|
+
return predicate(runs) ? runs : null;
|
|
597
|
+
}),
|
|
598
|
+
`waiting for ${what}`,
|
|
599
|
+
describe,
|
|
600
|
+
undefined,
|
|
601
|
+
guardMs,
|
|
602
|
+
),
|
|
603
|
+
waitForDisconnects: async (pids) => {
|
|
604
|
+
await guardEvent(
|
|
605
|
+
verdict(() => {
|
|
606
|
+
const unknown = pids.filter((pid) => !processes.has(pid));
|
|
607
|
+
if (unknown.length > 0) return new Error(`never connected to the counted Cargo: ${unknown.join(', ')}`);
|
|
608
|
+
const records = pids.flatMap((pid) => processes.get(pid) ?? []);
|
|
609
|
+
const survived = records.filter((record) => record.departure !== null && record.departure !== 'vanished');
|
|
610
|
+
if (survived.length > 0) {
|
|
611
|
+
const which = survived.map((record) => `${record.role} ${record.pid} (${record.departure})`);
|
|
612
|
+
return new Error(`expected to die, but finished: ${which.join(', ')}`);
|
|
613
|
+
}
|
|
614
|
+
return records.every((record) => record.departure === 'vanished') ? true : null;
|
|
615
|
+
}),
|
|
616
|
+
`waiting for ${pids.join(', ')} to die`,
|
|
617
|
+
describe,
|
|
618
|
+
undefined,
|
|
619
|
+
guardMs,
|
|
620
|
+
);
|
|
621
|
+
},
|
|
622
|
+
release: () => {
|
|
623
|
+
released ??= drain();
|
|
624
|
+
return released;
|
|
625
|
+
},
|
|
626
|
+
};
|
|
627
|
+
return {
|
|
628
|
+
cargo,
|
|
629
|
+
answer: (text) => {
|
|
630
|
+
canned = text;
|
|
631
|
+
},
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/** The real Cargo, counted: `metadata` prints what the real `cargo metadata` prints. */
|
|
636
|
+
export async function countedCargo(root: string): Promise<CountedCargo> {
|
|
637
|
+
const { cargo } = await shim(root, 'exec "$real" "$@"', null, REAL_CARGO_GUARD_MS);
|
|
638
|
+
return cargo;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* A Cargo whose `metadata` prints whatever the test says, whatever is on disk: the cache's own behavior,
|
|
643
|
+
* alone. Its other subcommands run the real Cargo under no rustup selection, which the canned answers do
|
|
644
|
+
* not depend on.
|
|
645
|
+
*/
|
|
646
|
+
export async function cannedCargo(root: string): Promise<CannedCargo> {
|
|
647
|
+
const { cargo, answer } = await shim(root, 'exec env -u RUSTUP_TOOLCHAIN -u RUSTUP_HOME "$real" "$@"', '', GUARD_MS);
|
|
648
|
+
return {
|
|
649
|
+
...cargo,
|
|
650
|
+
answerWith: async (document) => answer(JSON.stringify(document)),
|
|
651
|
+
answerWithText: async (text) => answer(text),
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/** `body` with `overrides` in this process's environment, which the module's Cargo children inherit. */
|
|
656
|
+
export async function withEnvironment<T>(
|
|
657
|
+
overrides: Readonly<Record<string, string>>,
|
|
658
|
+
body: () => Promise<T>,
|
|
659
|
+
): Promise<T> {
|
|
660
|
+
const saved = Object.keys(overrides).map((key): [string, string | undefined] => [key, process.env[key]]);
|
|
661
|
+
Object.assign(process.env, overrides);
|
|
662
|
+
try {
|
|
663
|
+
return await body();
|
|
664
|
+
} finally {
|
|
665
|
+
for (const [key, value] of saved) {
|
|
666
|
+
if (value === undefined) delete process.env[key];
|
|
667
|
+
else process.env[key] = value;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/** The error `promise` rejects with; a promise that resolves fails the test. */
|
|
673
|
+
export async function rejectionOf(promise: Promise<unknown>): Promise<unknown> {
|
|
674
|
+
try {
|
|
675
|
+
await promise;
|
|
676
|
+
} catch (error) {
|
|
677
|
+
return error;
|
|
678
|
+
}
|
|
679
|
+
throw new Error('expected a rejection, but the promise resolved');
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/** The typed Cargo refusal, including the requested manifest and the original failure. */
|
|
683
|
+
export function expectCargoMetadataError(error: unknown, manifestPath: string): CargoMetadataError {
|
|
684
|
+
expect(error).toBeInstanceOf(CargoMetadataError);
|
|
685
|
+
if (!(error instanceof CargoMetadataError)) throw new Error(`expected a CargoMetadataError, got ${String(error)}`);
|
|
686
|
+
expect(error.name).toBe('CargoMetadataError');
|
|
687
|
+
expect(error.manifestPath).toBe(manifestPath);
|
|
688
|
+
expect(error.cause).toBeInstanceOf(Error);
|
|
689
|
+
return error;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** `failure` is the whole refusal of a project-graph computation: Nx's aggregate of the per-file errors. */
|
|
693
|
+
export function aggregateOf(failure: unknown): AggregateCreateNodesError {
|
|
694
|
+
if (!(failure instanceof AggregateCreateNodesError)) {
|
|
695
|
+
throw new Error(`expected the graph computation to fail as an aggregate, got ${String(failure)}`);
|
|
696
|
+
}
|
|
697
|
+
return failure;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* `failure` is a project-graph computation refused for want of a Cargo resolution: an aggregate whose
|
|
702
|
+
* every error is the typed `CargoMetadataError` for the workspace manifest `manifestPath`, and which
|
|
703
|
+
* carries no partial result a runtime hash of the whole workspace could have filled.
|
|
704
|
+
*/
|
|
705
|
+
export function expectGraphRefusal(failure: unknown, manifestPath: string): AggregateCreateNodesError {
|
|
706
|
+
const aggregate = aggregateOf(failure);
|
|
707
|
+
expect(aggregate.errors.length).toBeGreaterThan(0);
|
|
708
|
+
for (const [, error] of aggregate.errors) expectCargoMetadataError(error, manifestPath);
|
|
709
|
+
expect(JSON.stringify(aggregate.partialResults)).not.toContain('--include-workspace');
|
|
710
|
+
return aggregate;
|
|
711
|
+
}
|