@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
@@ -29,13 +29,22 @@ export type RunnerKind =
29
29
  | 'process';
30
30
 
31
31
  /**
32
- * What a runner asks the budget for. Cargo also carries its independent test-thread ceiling; cargo-mutants carries
33
- * how many cargo processes it runs at once, which share its grant.
32
+ * The inherited ceilings, lowered by configuration, of test threads, which take no make jobserver token: libtest's
33
+ * `RUST_TEST_THREADS` and nextest's `NEXTEST_TEST_THREADS`. Null where neither environment names one.
34
+ */
35
+ export interface TestThreadCeilings {
36
+ readonly rust: number | null;
37
+ readonly nextest: number | null;
38
+ }
39
+
40
+ /**
41
+ * What a runner asks the budget for. A cargo build, and a process whose script may start Cargo, carry the test-thread
42
+ * ceilings their sizing keeps; cargo-mutants carries how many cargo processes it runs at once, which share its grant.
34
43
  */
35
44
  export type Demand =
36
- | { kind: 'cargo'; want: number; testThreads: number | null }
45
+ | { kind: 'cargo' | 'process'; want: number; testThreads: TestThreadCeilings }
37
46
  | { kind: 'cargo-mutants'; want: number; jobs: number }
38
- | { kind: Exclude<RunnerKind, 'cargo' | 'cargo-mutants'>; want: number };
47
+ | { kind: Exclude<RunnerKind, 'cargo' | 'cargo-mutants' | 'process'>; want: number };
39
48
 
40
49
  /** Every budgeted command learns its grant here, so a script that starts a runner can size it. */
41
50
  export const CPU_TOKENS_ENV = 'BOUNDED_EXEC_CPU_TOKENS';
@@ -138,7 +147,15 @@ export function runnerDemand(
138
147
  const want = Math.max(1, Math.min(requested, ceiling ?? requested));
139
148
  switch (kind) {
140
149
  case 'cargo':
141
- return { kind, want, testThreads: inheritedCeiling(inherited.RUST_TEST_THREADS, configured?.RUST_TEST_THREADS) };
150
+ case 'process':
151
+ return {
152
+ kind,
153
+ want,
154
+ testThreads: {
155
+ rust: inheritedCeiling(inherited.RUST_TEST_THREADS, configured?.RUST_TEST_THREADS),
156
+ nextest: inheritedCeiling(inherited.NEXTEST_TEST_THREADS, configured?.NEXTEST_TEST_THREADS),
157
+ },
158
+ };
142
159
  case 'cargo-mutants':
143
160
  return { kind, want, jobs };
144
161
  default:
@@ -259,8 +276,12 @@ function withSizedFlag(command: string, invocation: RegExp, flag: RegExp, sized:
259
276
  }
260
277
 
261
278
  /**
262
- * Size the runner to the lesser of its bounded demand and grant, retaining Cargo's test-thread ceiling.
263
- * Rewrite named flags so they cannot outrank that ceiling. `CPU_TOKENS_ENV` still reports the actual held grant.
279
+ * Size the runner to the lesser of its bounded demand and grant, keeping the test-thread ceilings. Rewrite named flags
280
+ * so they cannot outrank that size. `CPU_TOKENS_ENV` still reports the actual held grant.
281
+ *
282
+ * Cargo's rustc count is the pool's: the executor's sub-pool exports its jobserver fifo, so no `CARGO_BUILD_JOBS`
283
+ * comes from the grant (specs/cowshed/05_gateway.md, "The jobserver fifo"). Test threads take no jobserver token, so a
284
+ * cargo build's test threads are its size, and a process's, whose script may start Cargo, are its whole held grant.
264
285
  */
265
286
  export function sizeRunner(
266
287
  demand: Demand,
@@ -279,14 +300,18 @@ export function sizeRunner(
279
300
  case 'bun-parallel':
280
301
  return { command: command.replace(BUN_PARALLEL, `--parallel=${count}`), env: { [CPU_TOKENS_ENV]: held } };
281
302
  case 'cargo':
303
+ case 'process': {
304
+ const threads = demand.kind === 'cargo' ? runnerCount : tokens;
305
+ const { rust, nextest } = demand.testThreads;
282
306
  return {
283
307
  command,
284
308
  env: {
285
309
  [CPU_TOKENS_ENV]: held,
286
- CARGO_BUILD_JOBS: count,
287
- RUST_TEST_THREADS: String(Math.min(runnerCount, demand.testThreads ?? runnerCount)),
310
+ RUST_TEST_THREADS: String(Math.min(threads, rust ?? threads)),
311
+ NEXTEST_TEST_THREADS: String(Math.min(threads, nextest ?? threads)),
288
312
  },
289
313
  };
314
+ }
290
315
  case 'stryker':
291
316
  case 'vitest':
292
317
  case 'go-test': {
@@ -313,8 +338,6 @@ export function sizeRunner(
313
338
  },
314
339
  };
315
340
  }
316
- case 'process':
317
- return { command, env: { [CPU_TOKENS_ENV]: held } };
318
341
  }
319
342
  }
320
343
 
@@ -1,4 +1,4 @@
1
- import { afterEach, describe, expect, it, jest } from 'bun:test';
1
+ import { afterAll, afterEach, beforeAll, describe, expect, it, jest } from 'bun:test';
2
2
  import { spawnSync } from 'node:child_process';
3
3
  import { access, mkdir, mkdtemp, open, readFile, rm, writeFile } from 'node:fs/promises';
4
4
  import { createServer } from 'node:net';
@@ -8,7 +8,7 @@ import { onProgressClock } from '../../__tests__/progress-clock.js';
8
8
  import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from '../../bounded-test-policy.js';
9
9
  import { ownedSocketDirectory } from '../../testing.js';
10
10
  import { CPU_POOL_ENV, type CpuPool, createCpuPool, type InheritedCpuPool, readInheritedCpuPool } from './cpu-pool.js';
11
- import { CPU_TOKENS_ENV, type CpuBudget, parseInheritedTokens, type TokenGrant } from './cpu-tokens.js';
11
+ import { CPU_TOKENS_ENV, type CpuBudget, type TokenGrant } from './cpu-tokens.js';
12
12
  import {
13
13
  type BoundedExecContext,
14
14
  type BoundedExecResult,
@@ -28,7 +28,26 @@ const ownedProcessGroups = new Set<number>();
28
28
  const originalStdoutWrite = process.stdout.write;
29
29
  const originalStderrWrite = process.stderr.write;
30
30
 
31
- const nextestWant = Math.min(8, parseInheritedTokens(process.env.NEXTEST_TEST_THREADS) ?? 8);
31
+ /** What the nextest cases declare as `parallelism`, and so what they ask: the suite runs under no inherited ceiling. */
32
+ const nextestWant = 8;
33
+
34
+ /**
35
+ * Every sizing variable a holder exports to its command (specs/cowshed/05_gateway.md, "Sized to the grant", "Cargo reads
36
+ * its holder's jobserver"). This suite's own process runs under them when a gate's `bounded-exec` started it, and the
37
+ * executor reads them as inherited ceilings and pool beside the ones its cases construct, so the suite clears them
38
+ * (specs/cowshed/08_testing.md: fixtures clear every inherited sizing variable before they set their own).
39
+ */
40
+ const INHERITED_SIZING_ENV = [
41
+ CPU_POOL_ENV,
42
+ CPU_TOKENS_ENV,
43
+ 'NEXTEST_TEST_THREADS',
44
+ 'RUST_TEST_THREADS',
45
+ 'CARGO_BUILD_JOBS',
46
+ 'CARGO_MAKEFLAGS',
47
+ 'MAKEFLAGS',
48
+ 'MFLAGS',
49
+ ] as const;
50
+ const inheritedSizing = INHERITED_SIZING_ENV.map((name) => [name, process.env[name]] as const);
32
51
 
33
52
  /**
34
53
  * Prints the budget variables it sees, then what a grandchild sees: a command's environment reaches its descendants,
@@ -41,6 +60,19 @@ console.log(execFileSync('node', ['-p', read]).toString().trim());
41
60
  `;
42
61
 
43
62
  describe('@smoothbricks/nx-plugin:bounded-exec', () => {
63
+ beforeAll(() => {
64
+ for (const name of INHERITED_SIZING_ENV) {
65
+ delete process.env[name];
66
+ }
67
+ });
68
+
69
+ afterAll(() => {
70
+ for (const [name, value] of inheritedSizing) {
71
+ if (value === undefined) delete process.env[name];
72
+ else process.env[name] = value;
73
+ }
74
+ });
75
+
44
76
  afterEach(async () => {
45
77
  process.stdout.write = originalStdoutWrite;
46
78
  process.stderr.write = originalStderrWrite;
@@ -931,9 +963,11 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
931
963
  return heldGrant(tokens, released);
932
964
  },
933
965
  };
966
+ // Cargo's ceiling bounds its ask; its rustc count is then the sub-pool's jobserver fifo, and its test threads
967
+ // are sized under their own ceiling, never to the target's 64.
934
968
  for (const scenario of [
935
969
  { runner: 'nextest run', variable: 'NEXTEST_TEST_THREADS', cap: 6 },
936
- { runner: 'cargo test', variable: 'CARGO_BUILD_JOBS', cap: 4 },
970
+ { runner: 'cargo test', variable: 'RUST_TEST_THREADS', cap: 4 },
937
971
  ]) {
938
972
  const command = `node -e "console.log('cap=' + process.env.${scenario.variable} + ' held=' + process.env.BOUNDED_EXEC_CPU_TOKENS)" # ${scenario.runner}`;
939
973
  const result = await runBoundedExec(
@@ -964,6 +998,40 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
964
998
  }
965
999
  });
966
1000
 
1001
+ it("names the run's own jobserver fifo to its command, never one the target's env names", async () => {
1002
+ const workspace = await createWorkspace();
1003
+ const released: number[] = [];
1004
+ const injected = '-j --jobserver-auth=fifo:/injected';
1005
+ const command = `node -e "console.log(JSON.stringify([process.env.CARGO_MAKEFLAGS, process.env.MAKEFLAGS, process.env.${CPU_POOL_ENV}]))"`;
1006
+ const result = await runBoundedExec(
1007
+ {
1008
+ command,
1009
+ timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
1010
+ env: { CARGO_MAKEFLAGS: injected, MAKEFLAGS: injected, MFLAGS: injected },
1011
+ },
1012
+ workspace.context,
1013
+ createProcessTreeKiller(),
1014
+ null,
1015
+ {
1016
+ async take() {
1017
+ return heldGrant(2, released);
1018
+ },
1019
+ },
1020
+ );
1021
+ expect(result.success).toBe(true);
1022
+ for (const name of ['CARGO_MAKEFLAGS', 'MAKEFLAGS', 'MFLAGS']) {
1023
+ expect(result.terminalOutput).toContain(
1024
+ `cowshed: ${name} in the target's env is ignored: the CPU budget sets it`,
1025
+ );
1026
+ }
1027
+ // Cargo and make both read the fifo beside the sub-pool's socket that this run serves its descendants.
1028
+ expect(result.terminalOutput).toMatch(
1029
+ /^\["-j --jobserver-auth=fifo:(\/\S+)\/j","-j --jobserver-auth=fifo:\1\/j","\1\/p\.sock"\]$/m,
1030
+ );
1031
+ expect(result.terminalOutput).not.toContain('/injected');
1032
+ expect(released).toEqual([2]);
1033
+ });
1034
+
967
1035
  it('starts the runner once granted, sized to its grant, and returns the tokens when it exits', async () => {
968
1036
  const workspace = await createWorkspace();
969
1037
  const asked: { want: number; checkout: string; command: string }[] = [];
@@ -64,8 +64,14 @@ const EXIT_CODE_BY_SIGNAL: Partial<Record<NodeJS.Signals, number>> = {
64
64
  SIGTERM: 143,
65
65
  };
66
66
 
67
- /** The variables the CPU budget sets for the command it grants: no target's environment may set them. */
68
- const BUDGET_ENV = [CPU_TOKENS_ENV, CPU_POOL_ENV];
67
+ /** The pool descriptor: only a grant may give it, so neither the target's nor the inherited environment passes it on. */
68
+ const POOL_ENV = [CPU_TOKENS_ENV, CPU_POOL_ENV];
69
+ /**
70
+ * The variables the CPU budget sets for the command it grants: no target's environment may set them. A jobserver the
71
+ * executor itself inherits, a parent make's, still reaches an ungranted command by Cargo's own rule; a granted one's
72
+ * sub-pool names its own fifo over it.
73
+ */
74
+ const BUDGET_ENV = [...POOL_ENV, 'CARGO_MAKEFLAGS', 'MAKEFLAGS', 'MFLAGS'];
69
75
 
70
76
  export interface BoundedExecContext {
71
77
  root: string;
@@ -859,14 +865,14 @@ function buildCommand(options: BoundedExecOptions): string {
859
865
 
860
866
  /** The lease's directory is the command's TMPDIR unless the target names one itself. */
861
867
  function mergeEnv(lease: RamTempLease | null, env: Record<string, string>): NodeJS.ProcessEnv {
862
- const inherited = withoutBudgetEnv(process.env);
868
+ const inherited = withoutPoolEnv(process.env);
863
869
  return lease === null ? { ...inherited, ...env } : { ...inherited, TMPDIR: lease.directory, ...env };
864
870
  }
865
871
 
866
- /** The process's environment without the CPU budget's variables: a pool and its count only a grant may give. */
867
- function withoutBudgetEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
872
+ /** The process's environment without the CPU pool's descriptor: a pool and its count only a grant may give. */
873
+ function withoutPoolEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
868
874
  const kept = { ...env };
869
- for (const name of BUDGET_ENV) {
875
+ for (const name of POOL_ENV) {
870
876
  delete kept[name];
871
877
  }
872
878
  return kept;
@@ -31,6 +31,9 @@ describe('a run without a gateway', () => {
31
31
  throw new Error(`the run published no pool: ${JSON.stringify(env)}`);
32
32
  }
33
33
  expect(pool.tokens).toBe(4);
34
+ // A Cargo that Nx's own `nx:run-commands` starts reads the same count through the run's jobserver fifo.
35
+ expect(env.CARGO_MAKEFLAGS).toMatch(/^-j --jobserver-auth=fifo:\/\S+$/);
36
+ expect(env.MAKEFLAGS).toBe(env.CARGO_MAKEFLAGS);
34
37
  // Nightly 477's shape: two mutation sweeps, each asking for the whole host. The first holds the run's cores;
35
38
  // the second queues for them instead of starting a second host's worth of workers beside it.
36
39
  const first = await takeParentCpuTokens(pool, 31);
@@ -12,12 +12,13 @@ import { GATEWAY_SOCKET } from './gateway-lease.js';
12
12
  * Every runner sizes itself to the whole host unless something budgets it, and without a gateway nothing did: under
13
13
  * `NX_PARALLEL=100%` Nx starts every target side by side and each one's runner takes the whole machine. The run
14
14
  * therefore serves its own pool of one token per core, as a gateway-granted executor serves its children, and every
15
- * task inherits it: `bounded-exec` and the `cargo` launcher take from it as from any parent pool, FIFO, the head
16
- * taking `min(want, free)` once one token is free, so the runners of one run together stay within the cores.
15
+ * task inherits it: `bounded-exec` takes from it as from any parent pool, FIFO, the head taking `min(want, free)` once
16
+ * one token is free, and a Cargo that Nx's own `nx:run-commands` starts reads its jobserver fifo through the
17
+ * `CARGO_MAKEFLAGS` the pool exports, so the runners of one run together stay within the cores.
17
18
  *
18
19
  * The run serves it only when nothing else budgets it: it inherits no pool, and the gateway's control socket does
19
- * not exist. Only `ENOENT` proves the gateway absent, as for the launcher; a socket that exists but does not answer
20
- * may be a live, saturated gateway, and a run budgeting itself beside it would count the host twice.
20
+ * not exist. Only `ENOENT` proves the gateway absent; a socket that exists but does not answer may be a live,
21
+ * saturated gateway, and a run budgeting itself beside it would count the host twice.
21
22
  */
22
23
 
23
24
  /** The pools this process serves, by Nx run id, until that run's `postTasksExecution`. */
package/src/index.ts CHANGED
@@ -83,6 +83,7 @@ import { runtimeInput, shellWord } from './runtime-input.js';
83
83
  import { isNonSourceDirectory } from './source-directories.js';
84
84
  import { postTasksExecution as postTaskResults, preTasksExecution as preTaskResults } from './task-results.js';
85
85
  import { SHARD_TIMINGS_EXECUTOR, SHARD_TIMINGS_TARGET, timingsShardedSuite } from './test-timings.js';
86
+ import { assertUpstreamBasePrecondition } from './test-walk-policy.js';
86
87
  import {
87
88
  isBuildOutputTargetName,
88
89
  isTestRunnerTargetName,
@@ -1076,6 +1077,9 @@ function isBuildOutputPackageJson(packageJsonPath: string): boolean {
1076
1077
  * (`run-cpu-pool.ts`), and closes it when the run ends.
1077
1078
  */
1078
1079
  export const preTasksExecution: PreTasksExecution<ProducerReceiptsOptions> = async (options, context) => {
1080
+ // Before any task is hashed: a workspace that declares the test-walk check
1081
+ // must be able to name its upstream base, or the run fails with that reason.
1082
+ assertUpstreamBasePrecondition(context.workspaceRoot);
1079
1083
  preTaskResults(options, context);
1080
1084
  await preProducerReceipts(options, context);
1081
1085
  await serveRunCpuPool(context);
@@ -14,7 +14,7 @@ import { testFileInstallation } from './__tests__/owned-installation.js';
14
14
  import { managedAssetsRoot } from './managed-assets.js';
15
15
 
16
16
  /** The Nx and the plugin product the provider and the consumer link, one copy this file owns; none may write into it. */
17
- const installation = testFileInstallation('linked-snapshot-installation-');
17
+ const installation = testFileInstallation('linked-snapshot-installation');
18
18
  const SNAPSHOTS = join(managedAssetsRoot, 'raw/tooling/direnv/linked-snapshots.ts');
19
19
  const DECLARATION = { targets: ['linked-api:build'], artifactModules: [] };
20
20
 
@@ -66,7 +66,7 @@ let bodyDone: Promise<void> = Promise.resolve();
66
66
  /** A failed barrier keeps the fixture, even if a later cleanup attempt succeeds. */
67
67
  let barrierFailure: string | undefined;
68
68
  /** The Nx installation the fixture links; it must leave it as it was made. */
69
- const installation = testFileInstallation('smoo-nextest-partitions-installation-');
69
+ const installation = testFileInstallation('smoo-nextest-partitions-installation');
70
70
 
71
71
  beforeAll(
72
72
  caseBody(async () => {
@@ -698,6 +698,57 @@ it('does not serve a request sent on a connection opened before a lockfile chang
698
698
  );
699
699
  });
700
700
 
701
+ it('stops a kept daemon on a lockfile change instead of spawning a successor its keeper does not own', async () => {
702
+ await withNxFixture(
703
+ 'nx-daemon-validity-kept-',
704
+ async ({ socketDirectory, workspace }) => {
705
+ await fixtureWorkspace(workspace);
706
+ const lock = join(workspace, 'yarn.lock');
707
+ await writeFile(lock, 'lock one\n');
708
+ const env = fixtureNxEnv(workspace, socketDirectory);
709
+ // As cowshed's keeper job starts it: the daemon's own entry in the foreground, kept.
710
+ const kept = spawn('node', [daemonStart, '--kept'], { cwd: workspace, env, stdio: ['ignore', 'pipe', 'pipe'] });
711
+ let log = '';
712
+ kept.stdout.setEncoding('utf8').on('data', (text: string) => {
713
+ log += text;
714
+ });
715
+ kept.stderr.setEncoding('utf8').on('data', (text: string) => {
716
+ log += text;
717
+ });
718
+ const exited = Promise.withResolvers<number | null>();
719
+ kept.once('exit', (code) => exited.resolve(code));
720
+ try {
721
+ const record = await watchUntil(
722
+ dirname(recordPath(workspace)),
723
+ 'the kept daemon to write its record',
724
+ async () => {
725
+ const current = await daemonRecord(workspace);
726
+ return current !== null && current.processId === kept.pid ? current : null;
727
+ },
728
+ async () => {},
729
+ );
730
+ const daemon = await readyDaemon(workspace, record);
731
+ await writeFile(lock, 'lock two\n');
732
+ assertNotServed(
733
+ await requestOnce(daemon.record.socketPath, ping(workspace), 'a PING sent after the lockfile changed'),
734
+ 'a PING sent to the kept daemon after the lockfile changed',
735
+ );
736
+ await guardEvent(exited.promise, 'the kept daemon to exit after the lockfile changed', () => log);
737
+ // The decision is in the daemon's own log, written before it exits: it stopped, and started nothing.
738
+ expect(log, 'the kept daemon did not say it left its successor to its keeper').toContain(
739
+ 'Stopping: the process that started this daemon (--kept) starts its successor',
740
+ );
741
+ expect(log, 'the kept daemon spawned a successor of its own').not.toContain('Restarting daemon');
742
+ expect(log, 'the kept daemon spawned a successor of its own').not.toContain('Started new daemon process');
743
+ } finally {
744
+ if (kept.exitCode === null && kept.signalCode === null && kept.pid !== undefined) terminate(kept.pid);
745
+ await exited.promise;
746
+ }
747
+ },
748
+ FIXTURE_WORKSPACE,
749
+ );
750
+ });
751
+
701
752
  it('stops a daemon at the next request after an ignored lockfile changes, then its successor within its safety poll with no request', async () => {
702
753
  await withNxFixture(
703
754
  'nx-daemon-validity-ignored-',
@@ -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
+ });