@smoothbricks/nx-plugin 0.5.1 → 0.5.3

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 (60) hide show
  1. package/README.md +74 -13
  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/cargo-source-hash.d.ts +2 -2
  7. package/dist/cargo-source-hash.d.ts.map +1 -1
  8. package/dist/cargo-source-hash.js +106 -46
  9. package/dist/ensure-built-witness.d.ts +33 -5
  10. package/dist/ensure-built-witness.d.ts.map +1 -1
  11. package/dist/ensure-built-witness.js +84 -18
  12. package/dist/ensure-built.d.ts.map +1 -1
  13. package/dist/ensure-built.js +19 -5
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +4 -0
  16. package/dist/owned-installation.d.ts +6 -5
  17. package/dist/owned-installation.d.ts.map +1 -1
  18. package/dist/owned-installation.js +324 -201
  19. package/dist/test-walk-policy.d.ts +8 -0
  20. package/dist/test-walk-policy.d.ts.map +1 -1
  21. package/dist/test-walk-policy.js +34 -0
  22. package/dist/workspace-manifest.d.ts +5 -5
  23. package/dist/workspace-manifest.d.ts.map +1 -1
  24. package/managed/raw/patches/nx@23.2.1.patch +196 -48
  25. package/managed/raw/tooling/direnv/developer-links.ts +37 -10
  26. package/managed/raw/tooling/direnv/linked-snapshots.ts +31 -4
  27. package/managed/raw/tooling/direnv/setup-environment.ts +2 -0
  28. package/managed/raw/tooling/git-hooks/pre-push.sh +34 -11
  29. package/package.json +3 -3
  30. package/runtime-input.sh +21 -9
  31. package/src/__tests__/cargo-closure-filesets.ts +94 -0
  32. package/src/__tests__/owned-installation.ts +5 -6
  33. package/src/bin/smoo-nx-test-clocks.ts +2 -2
  34. package/src/bin/smoo-nx-test-walks.ts +6 -9
  35. package/src/cargo-closure-filesets.test.ts +63 -0
  36. package/src/cargo-closure-input.test.ts +1 -1
  37. package/src/cargo-closure-input.ts +23 -15
  38. package/src/cargo-closure-key.test.ts +1 -1
  39. package/src/cargo-resolve-cache.test.ts +1 -1
  40. package/src/cargo-source-hash.test.ts +143 -2
  41. package/src/cargo-source-hash.ts +108 -44
  42. package/src/cross-check-policy.test.ts +53 -12
  43. package/src/ensure-built-witness.test.ts +53 -2
  44. package/src/ensure-built-witness.ts +103 -18
  45. package/src/ensure-built.test.ts +40 -5
  46. package/src/ensure-built.ts +28 -6
  47. package/src/index.ts +4 -0
  48. package/src/linked-snapshots.nx.test.ts +1 -1
  49. package/src/nextest-partitions.test.ts +1 -1
  50. package/src/nx-daemon-start-never.test.ts +126 -0
  51. package/src/nx-daemon-validity.test.ts +51 -0
  52. package/src/nx-task-output-lease.test.ts +136 -0
  53. package/src/owned-installation.test.ts +105 -0
  54. package/src/owned-installation.ts +335 -93
  55. package/src/runtime-input.test.ts +22 -1
  56. package/src/task-results.test.ts +1 -1
  57. package/src/test-walk-policy.test.ts +35 -0
  58. package/src/test-walk-policy.ts +32 -0
  59. package/src/workspace-manifest.ts +5 -5
  60. package/test-timings.json +95 -92
@@ -1,5 +1,7 @@
1
1
  import { expect, it } from 'bun:test';
2
+ import { spawnSync } from 'node:child_process';
2
3
  import { channel } from 'node:diagnostics_channel';
4
+ import { existsSync, mkdirSync, readdirSync, readlinkSync, realpathSync } from 'node:fs';
3
5
  import { mkdir, readdir, readFile, symlink, writeFile } from 'node:fs/promises';
4
6
  import { join } from 'node:path';
5
7
  import type { Subprocess } from 'bun';
@@ -247,6 +249,140 @@ it('shares ownership across configurations without blocking independent targets,
247
249
  }
248
250
  });
249
251
 
252
+ /** One output target of its own, so persistent lock files from other runs never contend. */
253
+ function leaseTask(target: string) {
254
+ return {
255
+ id: `${target}:build`,
256
+ target: { project: `${target}-${crypto.randomUUID()}`, target: 'build' },
257
+ overrides: {},
258
+ outputs: ['app/dist'],
259
+ cache: true,
260
+ };
261
+ }
262
+
263
+ /** The helper pid a held lease's lock file names; the file is the helper's own record. */
264
+ async function leaseHolder(path: string): Promise<number> {
265
+ const record: unknown = JSON.parse(await readFile(path, 'utf8'));
266
+ if (typeof record !== 'object' || record === null || !('pid' in record) || typeof record.pid !== 'number') {
267
+ throw new Error(`lease file ${path} names no helper pid`);
268
+ }
269
+ return record.pid;
270
+ }
271
+
272
+ /** The lock path of the next lease `acquire` asks for, read from the diagnostics channel it publishes before waiting. */
273
+ function nextLeasePath(): Promise<string> {
274
+ const path = Promise.withResolvers<string>();
275
+ const events = channel('nx:task-output-lease');
276
+ const onEvent = (message: unknown) => {
277
+ if (typeof message !== 'object' || message === null || !('phase' in message) || message.phase !== 'waiting') return;
278
+ events.unsubscribe(onEvent);
279
+ if ('paths' in message && Array.isArray(message.paths) && typeof message.paths[0] === 'string')
280
+ path.resolve(message.paths[0]);
281
+ else path.reject(new Error(`waiting event names no lock path: ${JSON.stringify(message)}`));
282
+ };
283
+ events.subscribe(onEvent);
284
+ return path.promise;
285
+ }
286
+
287
+ /** Whether process `pid` is reaped: the parent's exit handler has then run in the same libuv callback. */
288
+ async function reaped(pid: number): Promise<void> {
289
+ for (;;) {
290
+ try {
291
+ process.kill(pid, 0);
292
+ } catch {
293
+ return;
294
+ }
295
+ const turn = Promise.withResolvers<void>();
296
+ setImmediate(turn.resolve);
297
+ await turn.promise;
298
+ }
299
+ }
300
+
301
+ it('wakes a waiter with the slot of a helper that died holding a lease, failing only the holder', async () => {
302
+ const leases = new TaskOutputLeases(1, null, 'output');
303
+ try {
304
+ const heldPath = nextLeasePath();
305
+ const release = await leases.acquire([leaseTask('held')]);
306
+ const helper = await leaseHolder(await heldPath);
307
+ // The one slot is taken, so this waits for it.
308
+ const waiting = leases.acquire([leaseTask('queued')]);
309
+ process.kill(helper, 'SIGKILL');
310
+ await expect(release()).rejects.toThrow('worker exited (code null, signal SIGKILL)');
311
+ // The helper's exit freed its slot: the waiter forks a successor rather than waiting for a release no one can send.
312
+ const releaseQueued = await guardEvent(waiting, "a waiter for a dead helper's slot", () => 'no successor helper');
313
+ await releaseQueued();
314
+ } finally {
315
+ await leases.close();
316
+ }
317
+ });
318
+
319
+ it('retires a helper that replied with an error, so the next acquire takes its slot', async () => {
320
+ const leases = new TaskOutputLeases(1, null, 'output');
321
+ try {
322
+ // The helper cannot open a lock path that is a directory, so it replies with an error and serves no more.
323
+ const blockedPath = nextLeasePath().then((path) => mkdirSync(path, { recursive: true }));
324
+ const refused = leases.acquire([leaseTask('unopenable')]);
325
+ await blockedPath;
326
+ await expect(refused).rejects.toThrow('failed:');
327
+ const release = await guardEvent(
328
+ leases.acquire([leaseTask('after-error')]),
329
+ 'an acquire after a helper replied with an error',
330
+ () => 'the failed helper still holds the only slot',
331
+ );
332
+ await release();
333
+ } finally {
334
+ await leases.close();
335
+ }
336
+ });
337
+
338
+ it('does not hand an idle helper that died to the next acquire', async () => {
339
+ const leases = new TaskOutputLeases(1, null, 'output');
340
+ try {
341
+ const idlePath = nextLeasePath();
342
+ const release = await leases.acquire([leaseTask('idle')]);
343
+ const helper = await leaseHolder(await idlePath);
344
+ await release();
345
+ process.kill(helper, 'SIGKILL');
346
+ await reaped(helper);
347
+ const releaseNext = await guardEvent(
348
+ leases.acquire([leaseTask('after-idle-death')]),
349
+ 'an acquire after the idle helper died',
350
+ () => 'no successor helper',
351
+ );
352
+ await releaseNext();
353
+ } finally {
354
+ await leases.close();
355
+ }
356
+ });
357
+
358
+ /** The descriptors process `pid` holds open on `file`: /proc on Linux, lsof elsewhere, both naming its real path. */
359
+ function openDescriptors(pid: number, file: string): number {
360
+ const path = realpathSync(file);
361
+ const proc = `/proc/${pid}/fd`;
362
+ if (existsSync(proc)) return readdirSync(proc).filter((fd) => readlinkSync(join(proc, fd)) === path).length;
363
+ const listed = spawnSync('lsof', ['-nP', '-p', String(pid), '-Fn'], { encoding: 'utf8' });
364
+ if (listed.status !== 0 && listed.stdout === '') throw new Error(`lsof -p ${pid} failed: ${listed.stderr}`);
365
+ return listed.stdout.split('\n').filter((line) => line === `n${path}`).length;
366
+ }
367
+
368
+ it('keeps one lock descriptor per lock file in a helper across acquires', async () => {
369
+ const leases = new TaskOutputLeases(1, null, 'output');
370
+ const task = leaseTask('repeated');
371
+ try {
372
+ const counts: number[] = [];
373
+ for (let round = 0; round < 5; round++) {
374
+ const pathEvent = nextLeasePath();
375
+ const release = await leases.acquire([task]);
376
+ const path = await pathEvent;
377
+ counts.push(openDescriptors(await leaseHolder(path), path));
378
+ await release();
379
+ }
380
+ expect(counts).toEqual([1, 1, 1, 1, 1]);
381
+ } finally {
382
+ await leases.close();
383
+ }
384
+ });
385
+
250
386
  it('refuses an ancestor-owned target before waiting for its output lease', async () => {
251
387
  await withNxFixture('nx-task-output-loop-', async ({ workspace, socketDirectory }) => {
252
388
  await mkdir(join(workspace, 'app'));
@@ -0,0 +1,105 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
2
+ import { lstat, mkdir, mkdtemp, readdir, readlink, realpath, rm, stat, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { basename, dirname, join } from 'node:path';
5
+
6
+ /** The tool cache a census root keeps its copies in, a real directory as on a fresh CI checkout. */
7
+ const KEPT = join('node_modules', '.cache', 'smoothbricks-nx-plugin', 'owned-installations');
8
+
9
+ let censusRoot = '';
10
+
11
+ /**
12
+ * A census root whose closure is three small packages: Nx, this plugin and the dependency the plugin declares,
13
+ * installed in its own `node_modules` beside a real (not linked) `node_modules/.cache`. A copy kept in that cache lies
14
+ * beneath this `node_modules`, so Node's ancestor lookup from the copy reaches every host package.
15
+ */
16
+ beforeEach(async () => {
17
+ censusRoot = await realpath(await mkdtemp(join(tmpdir(), 'owned-installation-')));
18
+ await writeFile(join(censusRoot, 'package.json'), '{"name":"census-root","private":true}');
19
+ const install = async (name: string, manifest: object) => {
20
+ const root = join(censusRoot, 'node_modules', name);
21
+ await mkdir(root, { recursive: true });
22
+ await writeFile(join(root, 'package.json'), JSON.stringify({ name, version: '1.0.0', ...manifest }));
23
+ await writeFile(join(root, 'index.js'), `module.exports = ${JSON.stringify(name)};\n`);
24
+ };
25
+ await install('nx', {});
26
+ await install('@smoothbricks/nx-plugin', { dependencies: { dependency: '1.0.0' } });
27
+ await install('dependency', {});
28
+ await mkdir(join(censusRoot, KEPT), { recursive: true });
29
+ });
30
+ afterEach(async () => {
31
+ await rm(censusRoot, { recursive: true, force: true });
32
+ });
33
+
34
+ /**
35
+ * Take the copy kept as `name` and retire it in a process of its own, as one run of a test file does: a process
36
+ * resolves each module once, so a take that shares this one's resolutions would not stand for the next run.
37
+ */
38
+ function take(name: string): { readonly modules: string; readonly stderr: string } {
39
+ const run = Bun.spawnSync(
40
+ [
41
+ process.execPath,
42
+ '--preload',
43
+ '@smoothbricks/validation/bun/preload',
44
+ '-e',
45
+ `const { ownedInstallation } = await import(${JSON.stringify(join(import.meta.dir, 'owned-installation.ts'))});
46
+ const installation = ownedInstallation(${JSON.stringify(censusRoot)}, ${JSON.stringify(name)});
47
+ process.stdout.write(await installation.nodeModules());
48
+ await installation.retire();`,
49
+ ],
50
+ { cwd: dirname(import.meta.dir) },
51
+ );
52
+ const stderr = run.stderr.toString();
53
+ expect(run.exitCode, stderr).toBe(0);
54
+ return { modules: run.stdout.toString(), stderr };
55
+ }
56
+
57
+ /** Where `path`, read through every link, lies; asserted to be inside the copy whose `node_modules` is `modules`. */
58
+ async function inCopy(modules: string, path: string): Promise<void> {
59
+ expect(await realpath(path)).toStartWith(`${dirname(modules)}/`);
60
+ }
61
+
62
+ describe('a kept fixture installation', () => {
63
+ it('is made from an empty cache whose ancestors hold the host packages, and resolves nothing outside itself', async () => {
64
+ const { modules, stderr } = take('fresh');
65
+ expect(stderr).toBe('');
66
+ expect(dirname(dirname(modules))).toBe(join(censusRoot, KEPT, 'fresh'));
67
+ await inCopy(modules, join(modules, 'nx'));
68
+ await inCopy(modules, join(modules, '@smoothbricks/nx-plugin'));
69
+ await inCopy(modules, join(await realpath(join(modules, '@smoothbricks/nx-plugin')), '..', '..', 'dependency'));
70
+ // Kept: the next run proves the same files and copies nothing.
71
+ const index = join(modules, 'nx', 'index.js');
72
+ const made = (await stat(index)).ino;
73
+ expect(take('fresh')).toEqual({ modules, stderr: '' });
74
+ expect((await stat(index)).ino).toBe(made);
75
+ });
76
+
77
+ it('makes again, elsewhere, a kept copy whose census would resolve a package from the host, and says why', async () => {
78
+ const first = take('escaping');
79
+ await rm(join(first.modules, 'nx'));
80
+ const again = take('escaping');
81
+ const host = join(censusRoot, 'node_modules', 'nx', 'package.json');
82
+ expect(again.stderr).toContain('failed its proof, copying again');
83
+ expect(again.stderr).toContain(`resolves nx from ${dirname(first.modules)} to ${host}, outside the copy`);
84
+ expect(again.modules).not.toBe(first.modules);
85
+ expect((await lstat(join(again.modules, 'nx'))).isSymbolicLink()).toBe(true);
86
+ await inCopy(again.modules, join(again.modules, 'nx'));
87
+ expect((await readdir(join(censusRoot, KEPT, 'escaping'))).sort()).toEqual(
88
+ ['censuses.json', basename(dirname(again.modules))].sort(),
89
+ );
90
+ expect(await readlink(join(again.modules, 'nx'))).not.toStartWith('/');
91
+ });
92
+
93
+ it('copies again only the package whose host bytes changed, naming it', async () => {
94
+ const { modules } = take('changed');
95
+ const kept = join(modules, '@smoothbricks/nx-plugin', 'index.js');
96
+ const untouched = (await stat(kept)).ino;
97
+ await writeFile(join(censusRoot, 'node_modules', 'nx', 'index.js'), 'module.exports = "changed";\n');
98
+ const again = take('changed');
99
+ expect(again.modules).toBe(modules);
100
+ expect(again.stderr).toContain('1 of 3 packages no longer hold what the host closure holds');
101
+ expect(again.stderr).toContain(join(censusRoot, 'node_modules', 'nx'));
102
+ expect(await Bun.file(join(modules, 'nx', 'index.js')).text()).toBe('module.exports = "changed";\n');
103
+ expect((await stat(kept)).ino).toBe(untouched);
104
+ });
105
+ });