@smoothbricks/nx-plugin 0.5.0 → 0.5.2

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 (79) hide show
  1. package/README.md +107 -46
  2. package/dist/bin/smoo-nx-test-clocks.js +2 -2
  3. package/dist/bin/smoo-nx-test-walks.js +6 -9
  4. package/dist/cargo-closure-input.d.ts.map +1 -1
  5. package/dist/cargo-closure-input.js +25 -15
  6. package/dist/ensure-built-witness.d.ts +33 -5
  7. package/dist/ensure-built-witness.d.ts.map +1 -1
  8. package/dist/ensure-built-witness.js +84 -18
  9. package/dist/ensure-built.d.ts.map +1 -1
  10. package/dist/ensure-built.js +33 -14
  11. package/dist/executors/bounded-exec/cpu-pool.d.ts +14 -7
  12. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  13. package/dist/executors/bounded-exec/cpu-pool.js +297 -38
  14. package/dist/executors/bounded-exec/cpu-tokens.d.ts +19 -7
  15. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
  16. package/dist/executors/bounded-exec/cpu-tokens.js +21 -7
  17. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  18. package/dist/executors/bounded-exec/executor.js +12 -6
  19. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  20. package/dist/executors/bounded-exec/run-cpu-pool.js +5 -4
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +4 -0
  23. package/dist/owned-installation.d.ts +6 -5
  24. package/dist/owned-installation.d.ts.map +1 -1
  25. package/dist/owned-installation.js +324 -201
  26. package/dist/producer-receipts.d.ts +10 -0
  27. package/dist/producer-receipts.d.ts.map +1 -1
  28. package/dist/producer-receipts.js +30 -5
  29. package/dist/test-walk-policy.d.ts +8 -0
  30. package/dist/test-walk-policy.d.ts.map +1 -1
  31. package/dist/test-walk-policy.js +34 -0
  32. package/dist/workspace-manifest.d.ts +5 -5
  33. package/dist/workspace-manifest.d.ts.map +1 -1
  34. package/managed/raw/patches/nx@23.2.1.patch +14 -3
  35. package/managed/raw/tooling/direnv/developer-links.ts +37 -10
  36. package/managed/raw/tooling/direnv/devenv.smoo.nix +1 -3
  37. package/managed/raw/tooling/direnv/linked-snapshots.ts +49 -12
  38. package/managed/raw/tooling/direnv/repo-path +1 -1
  39. package/managed/raw/tooling/direnv/setup-environment.ts +2 -0
  40. package/managed/raw/tooling/git-hooks/pre-push.sh +34 -11
  41. package/package.json +3 -3
  42. package/runtime-input.sh +21 -9
  43. package/src/__tests__/cargo-closure-filesets.ts +94 -0
  44. package/src/__tests__/counted-cargo.ts +1 -1
  45. package/src/__tests__/owned-installation.ts +5 -6
  46. package/src/bin/smoo-nx-test-clocks.ts +2 -2
  47. package/src/bin/smoo-nx-test-walks.ts +6 -9
  48. package/src/cargo-closure-filesets.test.ts +63 -0
  49. package/src/cargo-closure-input.test.ts +2 -2
  50. package/src/cargo-closure-input.ts +23 -15
  51. package/src/cargo-closure-key.test.ts +1 -1
  52. package/src/cargo-resolve-cache.test.ts +1 -1
  53. package/src/cross-check-policy.test.ts +53 -12
  54. package/src/ensure-built-witness.test.ts +53 -2
  55. package/src/ensure-built-witness.ts +103 -18
  56. package/src/ensure-built.test.ts +79 -5
  57. package/src/ensure-built.ts +51 -15
  58. package/src/executors/bounded-exec/cpu-pool.test.ts +132 -4
  59. package/src/executors/bounded-exec/cpu-pool.ts +300 -46
  60. package/src/executors/bounded-exec/cpu-tokens.test.ts +60 -15
  61. package/src/executors/bounded-exec/cpu-tokens.ts +34 -11
  62. package/src/executors/bounded-exec/executor.test.ts +72 -4
  63. package/src/executors/bounded-exec/executor.ts +12 -6
  64. package/src/executors/bounded-exec/run-cpu-pool.test.ts +3 -0
  65. package/src/executors/bounded-exec/run-cpu-pool.ts +5 -4
  66. package/src/index.ts +4 -0
  67. package/src/linked-snapshots.nx.test.ts +1 -1
  68. package/src/nextest-partitions.test.ts +1 -1
  69. package/src/nx-daemon-validity.test.ts +51 -0
  70. package/src/owned-installation.test.ts +105 -0
  71. package/src/owned-installation.ts +335 -93
  72. package/src/producer-receipts.test.ts +37 -15
  73. package/src/producer-receipts.ts +29 -5
  74. package/src/runtime-input.test.ts +22 -1
  75. package/src/task-results.test.ts +1 -1
  76. package/src/test-walk-policy.test.ts +35 -0
  77. package/src/test-walk-policy.ts +32 -0
  78. package/src/workspace-manifest.ts +5 -5
  79. package/test-timings.json +94 -92
@@ -1,5 +1,17 @@
1
1
  import { afterEach, describe, expect, it } from 'bun:test';
2
- import { chmodSync, existsSync, mkdirSync, readdirSync, rmSync, statSync } from 'node:fs';
2
+ import {
3
+ chmodSync,
4
+ closeSync,
5
+ constants,
6
+ existsSync,
7
+ mkdirSync,
8
+ openSync,
9
+ readdirSync,
10
+ readSync,
11
+ rmSync,
12
+ statSync,
13
+ writeSync,
14
+ } from 'node:fs';
3
15
  import { writeFile } from 'node:fs/promises';
4
16
  import { connect, type Socket } from 'node:net';
5
17
  import { dirname, join } from 'node:path';
@@ -82,11 +94,16 @@ const QUEUED: PoolReply = { ok: true, lease: 'queued' };
82
94
  const pools: CpuPool[] = [];
83
95
  const sockets: Socket[] = [];
84
96
  const directories: string[] = [];
97
+ // Descriptors a test opened on a pool's jobserver fifo, as a make-protocol client does.
98
+ const descriptors: number[] = [];
85
99
 
86
100
  afterEach(async () => {
87
101
  for (const socket of sockets.splice(0)) {
88
102
  socket.destroy();
89
103
  }
104
+ for (const fd of descriptors.splice(0)) {
105
+ closeSync(fd);
106
+ }
90
107
  for (const pool of pools.splice(0)) {
91
108
  // Every pool a test leaves open is closed here, and its outcome is asserted, not dropped.
92
109
  expect(await pool.close()).toEqual({ ok: true });
@@ -220,8 +237,59 @@ async function untilLine(stream: ReadableStream<Uint8Array>, marker: string): Pr
220
237
  reader.releaseLock();
221
238
  }
222
239
 
240
+ /** The fifo a pool's environment names for Cargo and make, which name the same one. */
241
+ function jobserverFifo(pool: CpuPool): string {
242
+ const flags = pool.environment.CARGO_MAKEFLAGS ?? '';
243
+ expect(pool.environment.MAKEFLAGS).toBe(flags);
244
+ const named = /^-j --jobserver-auth=fifo:(\/\S+)$/.exec(flags)?.[1];
245
+ if (named === undefined) {
246
+ throw new Error(`the pool names no jobserver fifo: ${JSON.stringify(pool.environment)}`);
247
+ }
248
+ return named;
249
+ }
250
+
251
+ /**
252
+ * A make-protocol client of `fifo`, opened read-write as Cargo opens it, and non-blocking here so that `take` reads
253
+ * what the fifo holds now and an empty fifo reads 0.
254
+ */
255
+ function jobserverClient(fifo: string): { take(most: number): number; give(count: number): void } {
256
+ const fd = openSync(fifo, constants.O_RDWR | constants.O_NONBLOCK);
257
+ descriptors.push(fd);
258
+ return {
259
+ take(most) {
260
+ try {
261
+ return readSync(fd, Buffer.alloc(most), 0, most, null);
262
+ } catch (error) {
263
+ if (error instanceof Error && 'code' in error && error.code === 'EAGAIN') return 0;
264
+ throw error;
265
+ }
266
+ },
267
+ give(count) {
268
+ expect(writeSync(fd, Buffer.alloc(count, '+'))).toBe(count);
269
+ },
270
+ };
271
+ }
272
+
273
+ /** Wait for `count` token bytes in `fifo` and take them, as a blocked Cargo does; resolves to the count it took. */
274
+ async function awaitTokens(fifo: string, count: number): Promise<number> {
275
+ const fd = openSync(fifo, constants.O_RDONLY | constants.O_NONBLOCK);
276
+ const reader = Bun.file(fd).stream().getReader();
277
+ let taken = 0;
278
+ try {
279
+ while (taken < count) {
280
+ const chunk = await reader.read();
281
+ if (chunk.done) break;
282
+ taken += chunk.value.byteLength;
283
+ }
284
+ } finally {
285
+ await reader.cancel();
286
+ closeSync(fd);
287
+ }
288
+ return taken;
289
+ }
290
+
223
291
  describe('the parent-held CPU pool', () => {
224
- it('serves a Cargo allocation as its own live child pool without competing with the occupied ancestor', async () => {
292
+ it('serves an allocation as its own live child pool without competing with the occupied ancestor', async () => {
225
293
  const { parent, pool } = await openPool(4);
226
294
  const cargo = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 4, children: true }));
227
295
  expect(await cargo.next()).toEqual(QUEUED);
@@ -382,7 +450,7 @@ describe('the parent-held CPU pool', () => {
382
450
  }
383
451
  });
384
452
 
385
- it('names the endpoint and count it holds, in private owned paths, and removes them on close', async () => {
453
+ it('names its endpoint, count and jobserver fifo in private owned paths, and removes them on close', async () => {
386
454
  const made = await createCpuPool(3, NEVER, countedWaits());
387
455
  if (!made.ok) {
388
456
  throw new Error(made.error);
@@ -396,8 +464,17 @@ describe('the parent-held CPU pool', () => {
396
464
  expect(made.pool.environment[CPU_TOKENS_ENV]).toBe('3');
397
465
  expect(statSync(dirname(inherited.socket)).mode & 0o777).toBe(0o700);
398
466
  expect(statSync(inherited.socket).mode & 0o777).toBe(0o600);
467
+ // The jobserver fifo lies beside the socket, as private, and holds the pool's tokens but the implicit first.
468
+ const fifo = jobserverFifo(made.pool);
469
+ expect(dirname(fifo)).toBe(dirname(inherited.socket));
470
+ expect(statSync(fifo).isFIFO()).toBe(true);
471
+ expect(statSync(fifo).mode & 0o777).toBe(0o600);
472
+ const cargo = jobserverClient(fifo);
473
+ expect(cargo.take(3)).toBe(2);
474
+ cargo.give(2);
399
475
  expect(await made.pool.close()).toEqual({ ok: true });
400
476
  expect(await made.pool.close()).toEqual({ ok: true });
477
+ expect(existsSync(fifo)).toBe(false);
401
478
  expect(existsSync(dirname(inherited.socket))).toBe(false);
402
479
  });
403
480
 
@@ -408,6 +485,8 @@ describe('the parent-held CPU pool', () => {
408
485
  '{"op":"other","want":1}',
409
486
  '{"op":"cpu-pool","want":0}',
410
487
  '{"op":"cpu-pool","want":1.5}',
488
+ // A grant never grows, so `grow` is a field like any other this pool does not read.
489
+ '{"op":"cpu-pool","want":1,"grow":true}',
411
490
  'x'.repeat(5000),
412
491
  'x'.repeat(1024 * 1024),
413
492
  ];
@@ -428,7 +507,7 @@ describe('the parent-held CPU pool', () => {
428
507
  // A wait holds no token: the whole pool is still there to grant.
429
508
  expect(await grantedFor(parent, 4)).toBe(4);
430
509
 
431
- // Cargo's launcher is served a child pool; the test it runs reports its waits there, to the same run.
510
+ // A child is served a pool of its own; the test it runs reports its waits there, to the same run.
432
511
  const cargo = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 2, children: true }));
433
512
  expect(await cargo.next()).toEqual(QUEUED);
434
513
  const granted = await cargo.next();
@@ -553,4 +632,53 @@ describe('the parent-held CPU pool', () => {
553
632
  expect(cpuPoolDirectories()).toEqual(before);
554
633
  await expect(createCpuPool(0, NEVER, countedWaits())).rejects.toBeInstanceOf(RangeError);
555
634
  });
635
+
636
+ it("lends a 1-token pool's implicit slot to a socket child, since its fifo holds no byte", async () => {
637
+ const { pool, parent } = await openPool(1);
638
+ expect(jobserverClient(jobserverFifo(pool)).take(1)).toBe(0);
639
+ // The holder is not using its own token while it waits on this child: lending it is what avoids the deadlock.
640
+ const first = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 2 }));
641
+ expect(await first.next()).toEqual(QUEUED);
642
+ expect(await first.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
643
+ const second = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 1 }));
644
+ expect(await second.next()).toEqual(QUEUED);
645
+ first.socket.destroy();
646
+ expect(await second.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
647
+ });
648
+
649
+ it('grants no byte a fifo reader holds, and wakes its queued head when the reader writes it back', async () => {
650
+ const { pool, parent } = await openPool(2);
651
+ const cargo = jobserverClient(jobserverFifo(pool));
652
+ // Cargo takes the fifo's one byte: only the implicit slot is left to lend.
653
+ expect(cargo.take(1)).toBe(1);
654
+ const first = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 2 }));
655
+ expect(await first.next()).toEqual(QUEUED);
656
+ expect(await first.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
657
+ const second = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 1 }));
658
+ expect(await second.next()).toEqual(QUEUED);
659
+ const third = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 1 }));
660
+ expect(await third.next()).toEqual(QUEUED);
661
+ // No socket child releases anything: only the fifo turning readable can wake the head, and it takes the byte.
662
+ cargo.give(1);
663
+ expect(await second.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
664
+ // The implicit slot goes back with the first child, to the next in line.
665
+ first.socket.destroy();
666
+ expect(await third.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
667
+ });
668
+
669
+ it("writes a released child's bytes back to the fifo, where Cargo and the next child both find them", async () => {
670
+ const { pool, parent } = await openPool(3);
671
+ const fifo = jobserverFifo(pool);
672
+ const cargo = jobserverClient(fifo);
673
+ const holder = await takeOrThrow(parent, 3);
674
+ expect(holder.tokens).toBe(3);
675
+ // The grant took both bytes and the implicit slot: the fifo is empty while it holds them.
676
+ expect(cargo.take(2)).toBe(0);
677
+ holder.release();
678
+ await holder.closed;
679
+ // The pool hears the close after the child does; a waiting reader takes the two bytes it writes back.
680
+ expect(await awaitTokens(fifo, 2)).toBe(2);
681
+ cargo.give(2);
682
+ expect(await grantedFor(parent, 3)).toBe(3);
683
+ });
556
684
  });