@smoothbricks/nx-plugin 0.4.29 → 0.4.30

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 (95) hide show
  1. package/README.md +83 -15
  2. package/dist/bin/smoo-nx-test-clocks.d.ts +3 -0
  3. package/dist/bin/smoo-nx-test-clocks.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-test-clocks.js +28 -0
  5. package/dist/bounded-test-policy.d.ts +1 -8
  6. package/dist/bounded-test-policy.d.ts.map +1 -1
  7. package/dist/bounded-test-policy.js +2 -8
  8. package/dist/cargo-closure-input.d.ts +11 -0
  9. package/dist/cargo-closure-input.d.ts.map +1 -1
  10. package/dist/cargo-closure-input.js +16 -6
  11. package/dist/ensure-built-receipt.d.ts +8 -2
  12. package/dist/ensure-built-receipt.d.ts.map +1 -1
  13. package/dist/ensure-built-receipt.js +45 -13
  14. package/dist/ensure-built-witness.d.ts +40 -4
  15. package/dist/ensure-built-witness.d.ts.map +1 -1
  16. package/dist/ensure-built-witness.js +68 -15
  17. package/dist/ensure-built.d.ts.map +1 -1
  18. package/dist/ensure-built.js +11 -3
  19. package/dist/executors/bounded-exec/cpu-pool.d.ts +7 -3
  20. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  21. package/dist/executors/bounded-exec/cpu-pool.js +9 -4
  22. package/dist/executors/bounded-exec/executor.d.ts +1 -1
  23. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/executor.js +35 -12
  25. package/dist/executors/bounded-exec/no-space.d.ts +8 -0
  26. package/dist/executors/bounded-exec/no-space.d.ts.map +1 -0
  27. package/dist/executors/bounded-exec/no-space.js +31 -0
  28. package/dist/executors/bounded-exec/ram-temp.d.ts +3 -0
  29. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  30. package/dist/executors/bounded-exec/ram-temp.js +15 -4
  31. package/dist/executors/bounded-exec/verdict.d.ts +34 -4
  32. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  33. package/dist/executors/bounded-exec/verdict.js +54 -3
  34. package/dist/graph-input-contract.d.ts +4 -0
  35. package/dist/graph-input-contract.d.ts.map +1 -1
  36. package/dist/graph-input-contract.js +17 -4
  37. package/dist/owned-installation.d.ts +14 -0
  38. package/dist/owned-installation.d.ts.map +1 -0
  39. package/dist/owned-installation.js +324 -0
  40. package/dist/producer-receipts.d.ts.map +1 -1
  41. package/dist/producer-receipts.js +15 -10
  42. package/dist/test-backstop.d.ts +9 -0
  43. package/dist/test-backstop.d.ts.map +1 -0
  44. package/dist/test-backstop.js +10 -0
  45. package/dist/test-clock-policy.d.ts +30 -0
  46. package/dist/test-clock-policy.d.ts.map +1 -0
  47. package/dist/test-clock-policy.js +686 -0
  48. package/dist/testing.d.ts.map +1 -1
  49. package/dist/testing.js +8 -1
  50. package/managed/raw/tooling/direnv/developer-links.ts +1 -8
  51. package/managed/raw/tooling/direnv/setup-environment.ts +0 -1
  52. package/package.json +15 -4
  53. package/src/__tests__/cargo-closure-fixture.ts +113 -0
  54. package/src/__tests__/counted-cargo.ts +8 -25
  55. package/src/__tests__/directory-change.ts +27 -0
  56. package/src/__tests__/owned-installation.ts +4 -164
  57. package/src/__tests__/past-tick.ts +12 -0
  58. package/src/bin/smoo-nx-test-clocks.ts +29 -0
  59. package/src/bounded-test-policy.ts +2 -8
  60. package/src/cargo-closure-input.property.test.ts +50 -0
  61. package/src/cargo-closure-input.test.ts +15 -112
  62. package/src/cargo-closure-input.ts +23 -6
  63. package/src/cargo-closure-key.test.ts +239 -0
  64. package/src/cargo-resolve-cache.test.ts +29 -7
  65. package/src/ensure-built-receipt.ts +56 -13
  66. package/src/ensure-built-witness.test.ts +113 -4
  67. package/src/ensure-built-witness.ts +75 -16
  68. package/src/ensure-built.test.ts +74 -2
  69. package/src/ensure-built.ts +13 -2
  70. package/src/executors/bounded-exec/cpu-pool.test.ts +18 -0
  71. package/src/executors/bounded-exec/cpu-pool.ts +10 -4
  72. package/src/executors/bounded-exec/disk-lease.test.ts +47 -13
  73. package/src/executors/bounded-exec/executor-host.test.ts +23 -11
  74. package/src/executors/bounded-exec/executor.test.ts +189 -63
  75. package/src/executors/bounded-exec/executor.ts +36 -11
  76. package/src/executors/bounded-exec/hang-diagnostics.test.ts +14 -8
  77. package/src/executors/bounded-exec/no-space.ts +40 -0
  78. package/src/executors/bounded-exec/ram-temp-host.test.ts +9 -8
  79. package/src/executors/bounded-exec/ram-temp.test.ts +32 -18
  80. package/src/executors/bounded-exec/ram-temp.ts +17 -4
  81. package/src/executors/bounded-exec/verdict.test.ts +125 -25
  82. package/src/executors/bounded-exec/verdict.ts +78 -3
  83. package/src/graph-input-contract.test.ts +8 -0
  84. package/src/graph-input-contract.ts +26 -4
  85. package/src/nextest-extraction.test.ts +114 -4
  86. package/src/nx-daemon-claim.test.ts +5 -3
  87. package/src/nx-daemon-idle-policy.test.ts +22 -18
  88. package/src/nx-daemon-validity.test.ts +5 -13
  89. package/src/owned-installation.ts +174 -0
  90. package/src/producer-receipts.ts +15 -10
  91. package/src/test-backstop.ts +11 -0
  92. package/src/test-clock-policy.test.ts +338 -0
  93. package/src/test-clock-policy.ts +708 -0
  94. package/src/testing.ts +8 -1
  95. package/test-timings.json +3 -0
@@ -1,11 +1,14 @@
1
- import { describe, expect, it } from 'bun:test';
2
- import { spawnSync } from 'node:child_process';
1
+ import { afterEach, describe, expect, it, jest } from 'bun:test';
2
+ import { spawn, spawnSync } from 'node:child_process';
3
+ import { once } from 'node:events';
3
4
  import { mkdir, mkdtemp, readdir, realpath, rm, stat, utimes, writeFile } from 'node:fs/promises';
4
5
  import { tmpdir } from 'node:os';
5
6
  import { join } from 'node:path';
7
+ import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from './bounded-test-policy.js';
6
8
  import {
7
9
  EXTRACTED_BINARIES_METADATA,
8
10
  EXTRACTED_CARGO_METADATA,
11
+ EXTRACTED_TARGET_DIR,
9
12
  ensureNextestArchiveExtracted,
10
13
  NEXTEST_EXTRACTION_DIRECTORY,
11
14
  type NextestArchiveExtractor,
@@ -22,9 +25,15 @@ const unpack: NextestArchiveExtractor = async (_archive, destination) => {
22
25
  await writeFile(join(destination, EXTRACTED_CARGO_METADATA), '{}');
23
26
  };
24
27
 
28
+ /**
29
+ * The lock's timing. `staleAfterMs` is the bound the first case is about; `heartbeatMs` and `pollMs` are intervals, not
30
+ * bounds. The heartbeat must stay below `staleAfterMs` for a live holder, and neither case reaches the poll.
31
+ */
25
32
  const FAST = { heartbeatMs: 20, staleAfterMs: 400, pollMs: 10 };
26
33
 
27
34
  describe('ensureNextestArchiveExtracted', () => {
35
+ afterEach(() => jest.useRealTimers());
36
+
28
37
  it('takes over a lock its holder stopped refreshing instead of waiting on it forever', async () => {
29
38
  const root = await mkdtemp(join(tmpdir(), 'nextest-extraction-'));
30
39
  try {
@@ -35,6 +44,9 @@ describe('ensureNextestArchiveExtracted', () => {
35
44
  // lock behind, and a pid in it that may since belong to anyone.
36
45
  await mkdir(paths.root, { recursive: true });
37
46
  await writeFile(paths.lock, `${process.pid} abandoned\n`);
47
+ // Dated on the fake clock, which moves only when advanced: the lock's age the product reads is exactly this minute.
48
+ // No holder refreshes it, so it is stale at any host speed and the case never reaches the poll.
49
+ jest.useFakeTimers();
38
50
  const minuteAgo = new Date(Date.now() - 60_000);
39
51
  await utimes(paths.lock, minuteAgo, minuteAgo);
40
52
 
@@ -48,7 +60,7 @@ describe('ensureNextestArchiveExtracted', () => {
48
60
  );
49
61
 
50
62
  expect(extraction).toMatchObject({ kind: 'extracted', directory: paths.directory });
51
- expect(events).toEqual([{ kind: 'stale-lock', holder: `pid ${process.pid}`, ageMs: expect.any(Number) }]);
63
+ expect(events).toEqual([{ kind: 'stale-lock', holder: `pid ${process.pid}`, ageMs: 60_000 }]);
52
64
  expect(await readdir(paths.root)).toEqual([paths.directory.slice(paths.root.length + 1)]);
53
65
  } finally {
54
66
  await rm(root, { recursive: true, force: true });
@@ -70,7 +82,9 @@ describe('ensureNextestArchiveExtracted', () => {
70
82
  await writeFile(archive, 'rebuilt archive bytes');
71
83
  };
72
84
 
73
- await expect(ensureNextestArchiveExtracted(archive, paths.root, rebuiltMidway, () => {}, FAST)).rejects.toThrow(
85
+ // Nothing else holds the lock, so no staleness bound can decide this case: it takes the leg's backstop.
86
+ const timing = { ...FAST, staleAfterMs: BOUNDED_TEST_IDLE_TIMEOUT_MS };
87
+ await expect(ensureNextestArchiveExtracted(archive, paths.root, rebuiltMidway, () => {}, timing)).rejects.toThrow(
74
88
  /changed while it was being extracted/,
75
89
  );
76
90
  // No directory under the old key, no staging left over, no lock held.
@@ -104,3 +118,99 @@ describe('nextestExtractionRoot', () => {
104
118
  }
105
119
  });
106
120
  });
121
+
122
+ describe('smoo-nx-nextest-extract', () => {
123
+ /** One shard's run of the real helper, as a runner's `$(...)` makes it: own TMPDIR, the archive in the checkout. */
124
+ async function shard(root: string, n: number) {
125
+ const tmp = join(root, `tmp-${n}`);
126
+ await mkdir(tmp);
127
+ const child = spawn(
128
+ process.execPath,
129
+ [join(import.meta.dir, 'bin/smoo-nx-nextest-extract.ts'), 'archive.tar.zst'],
130
+ {
131
+ cwd: root,
132
+ env: { ...process.env, TMPDIR: tmp },
133
+ stdio: ['ignore', 'pipe', 'pipe'],
134
+ },
135
+ );
136
+ let stdout = '';
137
+ let stderr = '';
138
+ child.stdout.on('data', (chunk: Buffer) => {
139
+ stdout += chunk.toString();
140
+ });
141
+ child.stderr.on('data', (chunk: Buffer) => {
142
+ stderr += chunk.toString();
143
+ });
144
+ const [code]: unknown[] = await once(child, 'close');
145
+ return { code, stdout, stderr, tmp };
146
+ }
147
+
148
+ it('unpacks an archive once for four concurrent shards, in the build tree and in none of their temp directories', async () => {
149
+ // A downstream land gate: shards that each ran `nextest run --archive-file` unpacked the whole 1.97 GiB archive into
150
+ // their own TMPDIR, a lease on one 8 GiB RAM volume, and four of them filled it.
151
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'nextest-extract-shards-')));
152
+ try {
153
+ await writeFile(
154
+ join(root, 'Cargo.toml'),
155
+ '[package]\nname = "shard-probe"\nversion = "0.1.0"\nedition = "2021"\n',
156
+ );
157
+ await mkdir(join(root, 'src'));
158
+ await writeFile(join(root, 'src/lib.rs'), '#[test]\nfn adds() {\n assert_eq!(1 + 1, 2);\n}\n');
159
+ expect(spawnSync('cargo', ['generate-lockfile', '--offline'], { cwd: root }).status).toBe(0);
160
+ const archived = spawnSync(
161
+ 'cargo',
162
+ ['--frozen', 'nextest', 'archive', '--archive-file', 'archive.tar.zst', '--user-config-file', 'none'],
163
+ { cwd: root, encoding: 'utf8' },
164
+ );
165
+ expect({ status: archived.status, stderr: archived.status === 0 ? '' : archived.stderr }).toEqual({
166
+ status: 0,
167
+ stderr: '',
168
+ });
169
+
170
+ const shards = await Promise.all([1, 2, 3, 4].map((n) => shard(root, n)));
171
+
172
+ const extractions = join(root, 'target', NEXTEST_EXTRACTION_DIRECTORY);
173
+ const key = await nextestArchiveKey(join(root, 'archive.tar.zst'));
174
+ const directory = join(extractions, key);
175
+ expect(shards.map(({ code, stdout }) => ({ code, stdout }))).toEqual(
176
+ Array.from({ length: 4 }, () => ({ code: 0, stdout: `${directory}\n` })),
177
+ );
178
+ // One shard unpacked it; the others waited on it or found it done.
179
+ expect(shards.filter(({ stderr }) => stderr.includes(': extracted archive.tar.zst ')).length).toBe(1);
180
+ // One extraction under the archive's hash, nothing staged or locked beside it, and no shard's TMPDIR touched.
181
+ expect(await readdir(extractions)).toEqual([key]);
182
+ for (const { tmp } of shards) {
183
+ expect({ tmp, entries: await readdir(tmp) }).toEqual({ tmp, entries: [] });
184
+ }
185
+ // A shard runs its tests from that one extraction.
186
+ const ran = spawnSync(
187
+ 'cargo',
188
+ [
189
+ '--frozen',
190
+ 'nextest',
191
+ 'run',
192
+ '--binaries-metadata',
193
+ join(directory, EXTRACTED_BINARIES_METADATA),
194
+ '--cargo-metadata',
195
+ join(directory, EXTRACTED_CARGO_METADATA),
196
+ '--target-dir-remap',
197
+ join(directory, EXTRACTED_TARGET_DIR),
198
+ '--workspace-remap',
199
+ '.',
200
+ '--user-config-file',
201
+ 'none',
202
+ // A gate forces colour (FORCE_COLOR, CARGO_TERM_COLOR), which puts escape codes inside the summary line.
203
+ '--color',
204
+ 'never',
205
+ ],
206
+ { cwd: root, encoding: 'utf8' },
207
+ );
208
+ expect({ status: ran.status, passed: ran.stderr.includes('1 test run: 1 passed') }).toEqual({
209
+ status: 0,
210
+ passed: true,
211
+ });
212
+ } finally {
213
+ await rm(root, { recursive: true, force: true });
214
+ }
215
+ });
216
+ });
@@ -263,10 +263,12 @@ it('replaces a record whose pid now belongs to an unrelated live process', async
263
263
  const crashed = new Date(Date.now() - 10 * 60_000);
264
264
  await utimes(record, crashed, crashed);
265
265
 
266
- const started = Date.now();
267
266
  expect(JSON.parse(nx(workspace, env, 'show', 'projects', '--json'))).toEqual(['p']);
268
- // Well inside the minute a client waits for a daemon, which a deferral to the stale record would use up.
269
- expect(Date.now() - started).toBeLessThan(30_000);
267
+ // A deferral to the stale record would wait out the client's whole budget for a daemon that never answers, and the
268
+ // client logs that exhaustion. A client that started its own daemon instead never logs it.
269
+ const clientLog = await readFile(join(workspace, '.nx/workspace-data/d/daemon.log'), 'utf8');
270
+ expect(clientLog).toContain('[Client] Starting new daemon server in background');
271
+ expect(clientLog).not.toContain('[Client] Server not available after');
270
272
  expect((await daemonRecord(workspace))?.processId).not.toBe(unrelated.pid);
271
273
  expect(isRunning(unrelated.pid)).toBe(true);
272
274
  } finally {
@@ -1,10 +1,9 @@
1
1
  import { expect, it } from 'bun:test';
2
2
  import { spawnSync } from 'node:child_process';
3
- import { readFileSync } from 'node:fs';
3
+ import { watch as fsWatch, readFileSync } from 'node:fs';
4
4
  import { mkdir, readFile, symlink, writeFile } from 'node:fs/promises';
5
5
  import { createRequire } from 'node:module';
6
6
  import { dirname, join } from 'node:path';
7
- import { setTimeout as sleep } from 'node:timers/promises';
8
7
  import { fixtureNxEnv, withNxFixture } from './__tests__/fixture-nx-env.js';
9
8
  import { nxDaemonProcesses } from './testing.js';
10
9
 
@@ -19,8 +18,6 @@ exports.createNodesV2 = ['**/project.json', async () => {
19
18
  `;
20
19
  // The daemon logs its open connection count on every connect and close.
21
20
  const OPEN_CONNECTIONS = /Number of open connections: (\d+)$/;
22
- // How long the daemon may take to see a closed connection: the aftermath of a reply, not an idle interval.
23
- const CLOSE_SEEN_MS = 10_000;
24
21
 
25
22
  it('serves real daemon requests with the installed five-minute inactivity policy', async () => {
26
23
  const require = createRequire(import.meta.url);
@@ -69,22 +66,29 @@ it('lets a resident plugin worker ask its daemon for files without holding the d
69
66
  expect(requested.status, requested.stdout + requested.stderr).toBe(0);
70
67
  expect(JSON.parse(requested.stdout)).toEqual(['p']);
71
68
  const logPath = join(workspace, '.nx/workspace-data/d/daemon.log');
72
- const deadline = Date.now() + CLOSE_SEEN_MS;
69
+ // The daemon logs its open connection count on every connect and close, so the wait ends on that log. A connection
70
+ // the worker kept would keep the daemon from its five-minute idle stop for as long as the worker lives, and the daemon
71
+ // keeps a worker with graph hooks for as long as it lives.
73
72
  let open: string | undefined;
74
- for (;;) {
75
- const log = await readFile(logPath, 'utf8');
76
- expect(log).toContain('Handled json message GLOB');
77
- open = log
78
- .split('\n')
79
- .map((line) => OPEN_CONNECTIONS.exec(line)?.[1])
80
- .filter((count) => count !== undefined)
81
- .at(-1);
82
- if (open === '0' || Date.now() > deadline) break;
83
- await sleep(50);
73
+ let notify: (() => void) | undefined;
74
+ const watcher = fsWatch(dirname(logPath), () => notify?.());
75
+ try {
76
+ while (open !== '0') {
77
+ // Armed before the read, so a count written while the read is in flight still wakes the next wait.
78
+ const changed = Promise.withResolvers<void>();
79
+ notify = () => changed.resolve();
80
+ const log = await readFile(logPath, 'utf8');
81
+ expect(log).toContain('Handled json message GLOB');
82
+ open = log
83
+ .split('\n')
84
+ .map((line) => OPEN_CONNECTIONS.exec(line)?.[1])
85
+ .filter((count) => count !== undefined)
86
+ .at(-1);
87
+ if (open !== '0') await changed.promise;
88
+ }
89
+ } finally {
90
+ watcher.close();
84
91
  }
85
- // A connection the worker kept would keep the daemon from its five-minute idle stop for as long as the worker
86
- // lives, and the daemon keeps a worker with graph hooks for as long as it lives.
87
- expect(open, `open daemon connections ${CLOSE_SEEN_MS}ms after the only client exited`).toBe('0');
88
92
  const workers = (await nxDaemonProcesses(workspace)).filter(({ command }) => command.includes('plugin-worker'));
89
93
  expect(workers.length, 'the resident plugin worker that asked the daemon for files').toBeGreaterThan(0);
90
94
  });
@@ -81,7 +81,6 @@ interface Replacement {
81
81
  /** Runs once the directory watch is open. */
82
82
  readonly action?: () => Promise<void>;
83
83
  readonly what: string;
84
- readonly timeoutMs?: number;
85
84
  }
86
85
 
87
86
  /** One line per event the watch client saw: its kind, then the tab-separated details. */
@@ -322,7 +321,6 @@ async function watchUntil<T>(
322
321
  what: string,
323
322
  check: () => Promise<T | null>,
324
323
  action?: () => Promise<void>,
325
- timeoutMs = 30_000,
326
324
  ): Promise<T> {
327
325
  await mkdir(directory, { recursive: true });
328
326
  let notify: (() => void) | undefined;
@@ -334,7 +332,7 @@ async function watchUntil<T>(
334
332
  notify = () => changed.resolve();
335
333
  const found = await check();
336
334
  if (found !== null) return found;
337
- await guardEvent(changed.promise, what, () => `no change in ${directory}`, undefined, timeoutMs);
335
+ await guardEvent(changed.promise, what, () => `no change in ${directory}`);
338
336
  }
339
337
  } finally {
340
338
  watcher.close();
@@ -381,12 +379,8 @@ async function untilServing(record: DaemonRecord, workspace: string): Promise<vo
381
379
  }
382
380
  };
383
381
  try {
384
- await guardEvent(
385
- serving(),
386
- `the daemon on ${socketPath} to answer PING`,
387
- () => daemonDiagnostics(workspace, record.processId),
388
- undefined,
389
- 30_000,
382
+ await guardEvent(serving(), `the daemon on ${socketPath} to answer PING`, () =>
383
+ daemonDiagnostics(workspace, record.processId),
390
384
  );
391
385
  } finally {
392
386
  waiting.abort();
@@ -409,7 +403,7 @@ async function startDaemon(workspace: string, env: Record<string, string>): Prom
409
403
  * Runs `action`, then resolves with the record that names a process other than `previous`. The record directory is
410
404
  * watched before `action` runs, so a record written at any point is seen.
411
405
  */
412
- function replacementAfter({ workspace, previous, action, what, timeoutMs }: Replacement): Promise<DaemonRecord> {
406
+ function replacementAfter({ workspace, previous, action, what }: Replacement): Promise<DaemonRecord> {
413
407
  return watchUntil(
414
408
  dirname(recordPath(workspace)),
415
409
  what,
@@ -418,7 +412,6 @@ function replacementAfter({ workspace, previous, action, what, timeoutMs }: Repl
418
412
  return record !== null && record.processId !== previous ? record : null;
419
413
  },
420
414
  action,
421
- timeoutMs,
422
415
  );
423
416
  }
424
417
 
@@ -744,7 +737,6 @@ it('stops a daemon at the next request after an ignored lockfile changes, then i
744
737
  previous: successor.record.processId,
745
738
  action: () => writeFile(lock, 'lock three\n'),
746
739
  what: 'a replacement of the successor within the 5 s safety poll after an ignored lockfile change',
747
- timeoutMs: 20_000,
748
740
  });
749
741
  await awaitExit(
750
742
  [successor.entry],
@@ -900,7 +892,7 @@ it('stops a daemon whose workspace files are all deleted before it reads the emp
900
892
  },
901
893
  FIXTURE_WORKSPACE,
902
894
  );
903
- }, 180_000);
895
+ });
904
896
 
905
897
  it('keeps an idle daemon off the lockfiles, record, and socket between its safety polls', async () => {
906
898
  await withNxFixture(
@@ -0,0 +1,174 @@
1
+ /**
2
+ * A fixture's own copy of a workspace's Nx installation, and the check that nothing wrote into it.
3
+ *
4
+ * The graph witness binds node stamps (ctime included) of every file in the closure of Nx, this plugin and
5
+ * TypeScript (README "Ancestor directories bind..."). Linked from a workspace, that closure is the host's: on CI
6
+ * its files are hardlinks in the shared Bun store, and any other task that installs, restores or cleans changes their
7
+ * link count, so their ctime, so a fixture's installation identity, between two calls of one test. Where this plugin
8
+ * is a developer link, the closure is also a live checkout that its own author edits and rebuilds while a consumer's
9
+ * test reads it. A fixture must not take an input it does not own.
10
+ *
11
+ * So the closure is copied, exactly as the witness's own census resolves it from the workspace that names it: each
12
+ * package once, with the census's own exclusions (`node_modules`, `.git`, `.nx`), each dependency edge relinked beside
13
+ * its copy as Bun's isolated layout does, the top-level packages' bins in `.bin`. Every copy is a new inode, cloned
14
+ * where the volume allows and written otherwise, so no link count is shared with anything outside its owner.
15
+ *
16
+ * A test file makes one copy, on first use, shared by its fixtures; retiring it fails, naming the changed nodes, if
17
+ * any fixture wrote into it.
18
+ */
19
+ import { constants } from 'node:fs';
20
+ import { cp, mkdir, readFile, rm, symlink } from 'node:fs/promises';
21
+ import { basename, dirname, join, relative } from 'node:path';
22
+ import { expandOutputs } from 'nx/src/native';
23
+ import { readModulePackageJson } from 'nx/src/utils/package-json';
24
+ import typia from 'typia';
25
+ import {
26
+ type GraphInstallationCensus,
27
+ graphInstallationCensus,
28
+ graphInstallationIdentity,
29
+ } from './ensure-built-witness.js';
30
+
31
+ /** The configuration the census resolves: Nx, this plugin and TypeScript. */
32
+ const CENSUS_NX_JSON = { plugins: ['@smoothbricks/nx-plugin'] };
33
+
34
+ /** An installation made under `root`, and its identity when the copy was made. */
35
+ interface OwnedInstallation {
36
+ readonly root: string;
37
+ readonly identity: string;
38
+ }
39
+
40
+ /** The graph witness's installation identity resolved from `root`; an unproved installation fails loudly. */
41
+ function installationIdentity(root: string): string {
42
+ let unproved = 'no detail';
43
+ const identity = graphInstallationIdentity(
44
+ root,
45
+ CENSUS_NX_JSON,
46
+ readModulePackageJson,
47
+ expandOutputs,
48
+ null,
49
+ (detail) => {
50
+ unproved = detail;
51
+ },
52
+ );
53
+ if (identity === null) throw new Error(`the Nx installation resolved from ${root} is unproved: ${unproved}`);
54
+ return identity;
55
+ }
56
+
57
+ /** Copy the installation closure `censusRoot` resolves into `root/node_modules`; `root` is an empty directory the
58
+ * caller owns.
59
+ *
60
+ * Only the closure is read from `censusRoot`, never its proof: that installation is the host's, whose shared store
61
+ * other tasks relink while the census reads it, which changes node stamps and would make every test that copies it
62
+ * fail for the host's work rather than its own. The copy, which the fixture owns, is what gets proved.
63
+ */
64
+ async function makeOwnedInstallation(censusRoot: string, root: string): Promise<OwnedInstallation> {
65
+ let unread = 'no detail';
66
+ const census = graphInstallationCensus(
67
+ censusRoot,
68
+ CENSUS_NX_JSON,
69
+ readModulePackageJson,
70
+ expandOutputs,
71
+ null,
72
+ (detail) => {
73
+ unread = detail;
74
+ },
75
+ );
76
+ if (census === null) throw new Error(`the Nx installation closure of ${censusRoot} cannot be read: ${unread}`);
77
+ const { resolutions } = census;
78
+ const modules = join(root, 'node_modules');
79
+ // Each source package root's copy, inside a store directory whose node_modules also holds its dependency links.
80
+ const copies = new Map<
81
+ string,
82
+ { readonly store: string; readonly copy: string; readonly bin: string | Record<string, string> | undefined }
83
+ >();
84
+ for (const [, , manifest] of resolutions) {
85
+ if (manifest === null || copies.has(dirname(manifest))) continue;
86
+ const { name, bin } = typia.json.assertParse<{ name: string; bin?: string | Record<string, string> }>(
87
+ await readFile(manifest, 'utf8'),
88
+ );
89
+ const store = join(modules, '.store', String(copies.size), 'node_modules');
90
+ copies.set(dirname(manifest), { store, copy: join(store, name), bin });
91
+ }
92
+ const copyOf = (source: string) => {
93
+ const copied = copies.get(source);
94
+ if (copied === undefined) throw new Error(`${source} resolves dependencies but is not in the census`);
95
+ return copied;
96
+ };
97
+ await Promise.all(
98
+ [...copies].map(([source, { copy }]) =>
99
+ cp(source, copy, {
100
+ recursive: true,
101
+ verbatimSymlinks: true,
102
+ mode: constants.COPYFILE_FICLONE,
103
+ filter: (path) => path === source || !['node_modules', '.git', '.nx'].includes(basename(path)),
104
+ }),
105
+ ),
106
+ );
107
+ for (const [from, name, manifest] of resolutions) {
108
+ if (manifest === null) continue;
109
+ const link = join(from === censusRoot ? modules : copyOf(from).store, name);
110
+ await mkdir(dirname(link), { recursive: true });
111
+ await symlink(relative(dirname(link), copyOf(dirname(manifest)).copy), link, 'dir');
112
+ if (from !== censusRoot) continue;
113
+ const { bin } = copyOf(dirname(manifest));
114
+ const commands = typeof bin === 'string' ? { [basename(name)]: bin } : (bin ?? {});
115
+ for (const [command, path] of Object.entries(commands)) {
116
+ await mkdir(join(modules, '.bin'), { recursive: true });
117
+ await symlink(relative(join(modules, '.bin'), join(link, path)), join(modules, '.bin', command));
118
+ }
119
+ }
120
+ return { root, identity: installationIdentity(root) };
121
+ }
122
+
123
+ /**
124
+ * Remove an installation, failing, with the changed nodes named, if its identity is no longer the one it was made
125
+ * with: the proof that its sharers only read it.
126
+ */
127
+ async function retireOwnedInstallation({ root, identity }: OwnedInstallation): Promise<void> {
128
+ try {
129
+ const retired = installationIdentity(root);
130
+ if (retired === identity) return;
131
+ const entries = (census: string) => {
132
+ const { nodes, files } = typia.json.assertParse<GraphInstallationCensus>(census);
133
+ return new Set([
134
+ ...nodes.map(({ path, stamp }) => `${path} ${stamp}`),
135
+ ...files.map(({ path, sha256 }) => `${path} ${sha256}`),
136
+ ]);
137
+ };
138
+ const made = entries(identity);
139
+ const now = entries(retired);
140
+ const changed = [
141
+ ...[...made].filter((entry) => !now.has(entry)).map((entry) => `- ${entry}`),
142
+ ...[...now].filter((entry) => !made.has(entry)).map((entry) => `+ ${entry}`),
143
+ ];
144
+ throw new Error(`a fixture changed the installation it shares, in ${root}:\n${changed.slice(0, 20).join('\n')}`);
145
+ } finally {
146
+ await rm(root, { recursive: true, force: true });
147
+ }
148
+ }
149
+
150
+ /** One test file's installation: made on first use and shared by its fixtures, retired by the file's `afterAll`. */
151
+ export interface TestFileInstallation {
152
+ /** The `node_modules` a fixture workspace links. */
153
+ readonly nodeModules: () => Promise<string>;
154
+ /** Fails if a fixture wrote into the copy; a file that never used it has nothing to retire. */
155
+ readonly retire: () => Promise<void>;
156
+ }
157
+
158
+ /**
159
+ * The installation one test file owns: a copy of the closure `censusRoot` (the workspace whose Nx, plugin and
160
+ * TypeScript the fixtures stand for) resolves, made in the empty directory `makeRoot` creates on first use. Call once
161
+ * per file, at module scope: Bun runs many test files in one process, and each must own its copy.
162
+ */
163
+ export function ownedInstallation(censusRoot: string, makeRoot: () => Promise<string>): TestFileInstallation {
164
+ let made: Promise<OwnedInstallation> | undefined;
165
+ return {
166
+ nodeModules: async () => {
167
+ made ??= makeRoot().then((root) => makeOwnedInstallation(censusRoot, root));
168
+ return join((await made).root, 'node_modules');
169
+ },
170
+ retire: async () => {
171
+ if (made !== undefined) await retireOwnedInstallation(await made);
172
+ },
173
+ };
174
+ }
@@ -35,10 +35,10 @@ import {
35
35
  receiptNode,
36
36
  } from './ensure-built-receipt.js';
37
37
  import {
38
+ censusStart,
38
39
  graphInstallationIdentity,
39
40
  movedNodes,
40
41
  type NodeWitness,
41
- nodesUnchanged,
42
42
  workspaceSnapshot,
43
43
  } from './ensure-built-witness.js';
44
44
  import { graphInputContractIdentity } from './graph-input-contract.js';
@@ -125,7 +125,8 @@ function same(left: unknown, right: unknown): boolean {
125
125
  return JSON.stringify(left) === JSON.stringify(right);
126
126
  }
127
127
  /**
128
- * Whether a tool node whose ctime alone moved still reads as `tools` recorded it: the same bytes, link text,
128
+ * Whether a tool node whose stamp does not prove it (its ctime alone moved, or it was read in the tick it changed in,
129
+ * recordedStamp) still reads as `tools` recorded it: the same bytes, link text,
129
130
  * membership and mode. Bun's global store is shared by every checkout, and linking a package's bin on any of their
130
131
  * installs re-chmods the store file to the mode it already has; that moves the ctime of a node these tools witness
131
132
  * without changing it, and is no reason to refuse the run.
@@ -134,7 +135,7 @@ function recordedTool(tools: readonly EnsureBuiltProducerTool[]): (path: string)
134
135
  const recorded = new Map(tools.flatMap((tool) => tool.nodes.map((node) => [node.path, node] as const)));
135
136
  return (path) => {
136
137
  const node = recorded.get(path);
137
- return node !== undefined && same(receiptNode(path, path, []), node);
138
+ return node !== undefined && same(receiptNode(path, path), node);
138
139
  };
139
140
  }
140
141
  function missing(paths: readonly string[]): boolean {
@@ -205,6 +206,7 @@ function toolsFor(
205
206
  const installation = typia.json.isParse<Installation>(identity);
206
207
  if (installation === null) return { ok: false, detail: 'Invalid installed module closure evidence' };
207
208
  const observed: NodeWitness[] = [];
209
+ const census = { start: censusStart(), observed };
208
210
  const paths = new Set(installation.nodes.map(({ path }) => path));
209
211
  const absent = new Set<string>();
210
212
  function namespace(path: string): void {
@@ -232,7 +234,7 @@ function toolsFor(
232
234
  else absent.add(candidate);
233
235
  }
234
236
  }
235
- const nodes = [...paths].sort().map((path) => receiptNode(path, path, observed));
237
+ const nodes = [...paths].sort().map((path) => receiptNode(path, path, census));
236
238
  const module: EnsureBuiltProducerTool = {
237
239
  kind: 'module',
238
240
  name: 'installed-nx-plugin-closure',
@@ -264,7 +266,7 @@ function toolsFor(
264
266
  }
265
267
  if (path === undefined) return { ok: false, detail: `Unbound producer executable ${name}` };
266
268
  const physicalPath = realpathSync(path);
267
- const entries = [...new Set([path, physicalPath])].map((entry) => receiptNode(entry, entry, observed));
269
+ const entries = [...new Set([path, physicalPath])].map((entry) => receiptNode(entry, entry, census));
268
270
  tools.push({
269
271
  kind: 'executable',
270
272
  name,
@@ -291,16 +293,18 @@ function toolsFor(
291
293
  function verifyTools(tools: readonly EnsureBuiltProducerTool[]): ReceiptEvidence<readonly NodeWitness[]> {
292
294
  try {
293
295
  const observed: NodeWitness[] = [];
296
+ const census = { start: censusStart(), observed };
294
297
  for (const tool of tools) {
295
298
  if (realpathSync(tool.path) !== tool.physicalPath || !missing(tool.missing))
296
299
  return { ok: false, detail: `Producer tool alias/namespace changed: ${tool.name}` };
297
- const nodes = tool.nodes.map((node) => receiptNode(node.path, node.path, observed));
300
+ const nodes = tool.nodes.map((node) => receiptNode(node.path, node.path, census));
298
301
  if (!same(nodes, tool.nodes) || sha(nodes) !== tool.sha256)
299
302
  return { ok: false, detail: `Producer tool/module bytes changed: ${tool.name}` };
300
303
  }
301
- return nodesUnchanged(observed)
304
+ const moved = movedNodes(observed, recordedTool(tools));
305
+ return moved === null
302
306
  ? { ok: true, value: observed }
303
- : { ok: false, detail: 'Producer tool nodes changed during readonly validation' };
307
+ : { ok: false, detail: `Producer tool nodes changed during readonly validation: ${moved}` };
304
308
  } catch (error) {
305
309
  if (error instanceof Error) return { ok: false, detail: `Cannot validate producer tools: ${error.message}` };
306
310
  throw error;
@@ -620,8 +624,9 @@ function validatedReceipt(root: string, target: string): ReceiptEvidence<EnsureB
620
624
  ? 'Public product bytes differ from actual producer receipt'
621
625
  : census.reason.detail,
622
626
  };
623
- if (!nodesUnchanged(tools.value))
624
- return { ok: false, detail: 'Producer tool namespace changed during independent product validation' };
627
+ const moved = movedNodes(tools.value, recordedTool(receipt.producer.tools));
628
+ if (moved !== null)
629
+ return { ok: false, detail: `Producer tool namespace changed during independent product validation: ${moved}` };
625
630
  return { ok: true, value: receipt };
626
631
  } catch (error) {
627
632
  if (error instanceof Error)
@@ -0,0 +1,11 @@
1
+ // A leaf module with no imports: tests' child processes on plain bun load testing.ts, which reads this bound, so
2
+ // nothing here may pull in a module that needs the typia transform (bounded-test-policy.ts does).
3
+
4
+ /**
5
+ * A test target's one bound: the `idleTimeoutMs` with which no test may go unfinished, the hang backstop of
6
+ * specs/cowshed/08_testing.md. It is no wall-clock ceiling: a suite that keeps finishing tests runs as long as it
7
+ * needs. It is the longest any single test may run, so it stays above every slow-timeout class in the plugin's
8
+ * nextest.toml; a bun leg has no per-test deadline of its own (`BUN_TEST_TIMEOUT_FLAG`).
9
+ */
10
+ export const BOUNDED_TEST_IDLE_TIMEOUT_MS = 180_000;
11
+ export const BOUNDED_TEST_KILL_AFTER_MS = 10_000;