@smoothbricks/nx-plugin 0.4.28 → 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 (158) hide show
  1. package/README.md +277 -89
  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 +8 -8
  6. package/dist/bounded-test-policy.d.ts.map +1 -1
  7. package/dist/bounded-test-policy.js +18 -30
  8. package/dist/cargo-closure-input.d.ts +23 -0
  9. package/dist/cargo-closure-input.d.ts.map +1 -1
  10. package/dist/cargo-closure-input.js +41 -9
  11. package/dist/cargo-source-hash.d.ts.map +1 -1
  12. package/dist/cargo-source-hash.js +42 -19
  13. package/dist/ensure-built-receipt.d.ts +40 -6
  14. package/dist/ensure-built-receipt.d.ts.map +1 -1
  15. package/dist/ensure-built-receipt.js +297 -159
  16. package/dist/ensure-built-witness.d.ts +65 -4
  17. package/dist/ensure-built-witness.d.ts.map +1 -1
  18. package/dist/ensure-built-witness.js +2237 -23
  19. package/dist/ensure-built.d.ts +99 -1
  20. package/dist/ensure-built.d.ts.map +1 -1
  21. package/dist/ensure-built.js +45 -56
  22. package/dist/executors/bounded-exec/cpu-pool.d.ts +22 -7
  23. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/cpu-pool.js +51 -22
  25. package/dist/executors/bounded-exec/executor.d.ts +12 -3
  26. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  27. package/dist/executors/bounded-exec/executor.js +174 -51
  28. package/dist/executors/bounded-exec/no-space.d.ts +8 -0
  29. package/dist/executors/bounded-exec/no-space.d.ts.map +1 -0
  30. package/dist/executors/bounded-exec/no-space.js +31 -0
  31. package/dist/executors/bounded-exec/ram-temp.d.ts +20 -11
  32. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  33. package/dist/executors/bounded-exec/ram-temp.js +42 -17
  34. package/dist/executors/bounded-exec/verdict.d.ts +34 -4
  35. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  36. package/dist/executors/bounded-exec/verdict.js +54 -3
  37. package/dist/generators/bun-test-tracing/generator.js +2 -2
  38. package/dist/graph-input-contract.d.ts +102 -0
  39. package/dist/graph-input-contract.d.ts.map +1 -0
  40. package/dist/graph-input-contract.js +637 -0
  41. package/dist/index.d.ts +12 -5
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +17 -3
  44. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  45. package/dist/managed-files/ci-workflow.js +5 -2
  46. package/dist/managed-files/publish-workflow.d.ts +11 -1
  47. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  48. package/dist/owned-installation.d.ts +14 -0
  49. package/dist/owned-installation.d.ts.map +1 -0
  50. package/dist/owned-installation.js +324 -0
  51. package/dist/producer-receipts.d.ts +19 -0
  52. package/dist/producer-receipts.d.ts.map +1 -0
  53. package/dist/producer-receipts.js +926 -0
  54. package/dist/test-backstop.d.ts +9 -0
  55. package/dist/test-backstop.d.ts.map +1 -0
  56. package/dist/test-backstop.js +10 -0
  57. package/dist/test-clock-policy.d.ts +30 -0
  58. package/dist/test-clock-policy.d.ts.map +1 -0
  59. package/dist/test-clock-policy.js +686 -0
  60. package/dist/testing.d.ts +8 -0
  61. package/dist/testing.d.ts.map +1 -1
  62. package/dist/testing.js +66 -1
  63. package/managed/raw/patches/nx@23.2.1.patch +328 -50
  64. package/managed/raw/tooling/direnv/developer-links.ts +148 -29
  65. package/managed/raw/tooling/direnv/devenv.smoo.nix +7 -7
  66. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +14 -5
  67. package/managed/raw/tooling/direnv/linked-snapshots.ts +206 -40
  68. package/managed/raw/tooling/direnv/repo-path +15 -2
  69. package/managed/raw/tooling/direnv/setup-environment.ts +179 -115
  70. package/managed/templates/github/actions/cache-node-modules/action.yml +9 -0
  71. package/managed/templates/github/actions/setup-devenv/action.yml +63 -7
  72. package/nextest.toml +1 -1
  73. package/package.json +25 -9
  74. package/src/__tests__/cargo-closure-fixture.ts +113 -0
  75. package/src/__tests__/counted-cargo.ts +8 -25
  76. package/src/__tests__/directory-change.ts +27 -0
  77. package/src/__tests__/fixture-nx-env.ts +7 -0
  78. package/src/__tests__/owned-installation.ts +14 -0
  79. package/src/__tests__/past-tick.ts +12 -0
  80. package/src/bin/smoo-nx-bound-failures.test.ts +1 -1
  81. package/src/bin/smoo-nx-test-clocks.ts +29 -0
  82. package/src/bounded-test-policy.test.ts +36 -22
  83. package/src/bounded-test-policy.ts +18 -32
  84. package/src/bun-bootstrap.test.ts +1 -1
  85. package/src/cargo-closure-input.property.test.ts +50 -0
  86. package/src/cargo-closure-input.test.ts +73 -121
  87. package/src/cargo-closure-input.ts +47 -8
  88. package/src/cargo-closure-key.test.ts +239 -0
  89. package/src/cargo-resolve-cache.test.ts +168 -31
  90. package/src/cargo-source-hash.test.ts +78 -3
  91. package/src/cargo-source-hash.ts +48 -30
  92. package/src/cross-check-policy.test.ts +2 -2
  93. package/src/ensure-built-receipt.test.ts +111 -16
  94. package/src/ensure-built-receipt.ts +294 -149
  95. package/src/ensure-built-witness.test.ts +306 -13
  96. package/src/ensure-built-witness.ts +255 -24
  97. package/src/ensure-built.test.ts +228 -102
  98. package/src/ensure-built.ts +127 -66
  99. package/src/executors/bounded-exec/cpu-pool.test.ts +134 -32
  100. package/src/executors/bounded-exec/cpu-pool.ts +64 -20
  101. package/src/executors/bounded-exec/cpu-tokens.test.ts +2 -2
  102. package/src/executors/bounded-exec/disk-lease.test.ts +47 -13
  103. package/src/executors/bounded-exec/executor-host.test.ts +74 -52
  104. package/src/executors/bounded-exec/executor.test.ts +343 -97
  105. package/src/executors/bounded-exec/executor.ts +192 -56
  106. package/src/executors/bounded-exec/hang-diagnostics.test.ts +14 -8
  107. package/src/executors/bounded-exec/no-space.ts +40 -0
  108. package/src/executors/bounded-exec/progress.test.ts +1 -1
  109. package/src/executors/bounded-exec/ram-temp-host.test.ts +51 -21
  110. package/src/executors/bounded-exec/ram-temp.test.ts +92 -16
  111. package/src/executors/bounded-exec/ram-temp.ts +49 -20
  112. package/src/executors/bounded-exec/verdict.test.ts +127 -22
  113. package/src/executors/bounded-exec/verdict.ts +78 -3
  114. package/src/generators/bounded-test-targets/generator.test.ts +1 -3
  115. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  116. package/src/generators/bun-test-tracing/generator.ts +2 -2
  117. package/src/graph-input-contract.test.ts +379 -0
  118. package/src/graph-input-contract.ts +627 -0
  119. package/src/index.test.ts +3 -3
  120. package/src/index.ts +24 -3
  121. package/src/linked-snapshots.nx.test.ts +112 -30
  122. package/src/managed-files/ci-workflow.ts +5 -2
  123. package/src/managed-files/publish-workflow.ts +12 -1
  124. package/src/managed-files/repo-path.test.ts +44 -1
  125. package/src/managed-files/setup-devenv-free-disk.test.ts +95 -0
  126. package/src/nextest-code-bound.test.ts +23 -17
  127. package/src/nextest-extraction.test.ts +114 -4
  128. package/src/nextest-partitions.test.ts +7 -1
  129. package/src/nx-cache-restore-mtime.test.ts +2 -2
  130. package/src/nx-daemon-claim.test.ts +10 -8
  131. package/src/nx-daemon-idle-cpu.test.ts +0 -1
  132. package/src/nx-daemon-idle-policy.test.ts +61 -1
  133. package/src/nx-daemon-ignore-files.test.ts +2 -2
  134. package/src/nx-daemon-validity.test.ts +36 -22
  135. package/src/nx-dot-input-policy.test.ts +5 -5
  136. package/src/nx-files-archive-racy.test.ts +1 -1
  137. package/src/nx-incomplete-run-summary.test.ts +1 -1
  138. package/src/nx-plugin-load-errors.test.ts +2 -2
  139. package/src/nx-project-graph-errors.test.ts +70 -0
  140. package/src/nx-skipped-task-exit.test.ts +2 -2
  141. package/src/nx-store-resolution.test.ts +7 -25
  142. package/src/nx-task-output-lease.test.ts +3 -3
  143. package/src/nx-use-local.test.ts +7 -5
  144. package/src/owned-installation.ts +174 -0
  145. package/src/producer-receipts.test.ts +164 -0
  146. package/src/producer-receipts.ts +636 -0
  147. package/src/runtime-input.test.ts +1 -1
  148. package/src/serial-group-wiring.test.ts +4 -4
  149. package/src/task-results.test.ts +18 -16
  150. package/src/test-backstop.ts +11 -0
  151. package/src/test-clock-policy.test.ts +338 -0
  152. package/src/test-clock-policy.ts +708 -0
  153. package/src/test-timings.test.ts +2 -2
  154. package/src/testing.test.ts +5 -5
  155. package/src/testing.ts +67 -1
  156. package/src/typecheck-test-policy.test.ts +1 -1
  157. package/src/typescript-resolution.test.ts +76 -0
  158. package/test-timings.json +8 -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
+ });
@@ -6,6 +6,7 @@ import { join } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
7
7
  import type { TargetConfiguration } from 'nx/src/devkit-exports.js';
8
8
  import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
9
+ import { testFileInstallation } from './__tests__/owned-installation.js';
9
10
  import { BOUNDED_TEST_IDLE_TIMEOUT_MS, BOUNDED_TEST_KILL_AFTER_MS } from './bounded-test-policy.js';
10
11
  import type { NxTaskContext } from './executors/bounded-exec/executor.js';
11
12
  import { hostLeaseProcesses } from './executors/bounded-exec/ram-temp.js';
@@ -64,6 +65,8 @@ let ending = false;
64
65
  let bodyDone: Promise<void> = Promise.resolve();
65
66
  /** A failed barrier keeps the fixture, even if a later cleanup attempt succeeds. */
66
67
  let barrierFailure: string | undefined;
68
+ /** The Nx installation the fixture links; it must leave it as it was made. */
69
+ const installation = testFileInstallation('smoo-nextest-partitions-installation-');
67
70
 
68
71
  beforeAll(
69
72
  caseBody(async () => {
@@ -108,7 +111,7 @@ ${expected.map((name) => ` #[test] fn ${name}() { record("${name}");${serial.
108
111
  }
109
112
  `,
110
113
  );
111
- await symlink(join(import.meta.dir, '../../../node_modules'), join(root, 'node_modules'), 'dir');
114
+ await symlink(await installation.nodeModules(), join(root, 'node_modules'), 'dir');
112
115
  await run(root, ['git', 'init', '--quiet']);
113
116
  await run(root, ['cargo', 'generate-lockfile', '--offline']);
114
117
  const [, infer] = createNodesV2ForPlatform('darwin', 'arm64');
@@ -206,6 +209,9 @@ afterAll(async () => {
206
209
  await rm(root, { recursive: true, force: true });
207
210
  });
208
211
 
212
+ // After the fixture's own teardown, so every process that could read the installation has ended.
213
+ afterAll(() => installation.retire());
214
+
209
215
  function fixture(): SharedFixture {
210
216
  if (shared === undefined) {
211
217
  throw new Error('the shared Cargo fixture was not built: its beforeAll did not complete');
@@ -127,7 +127,7 @@ it('restores a locally cached output newer than the output it replaces', async (
127
127
  expect(restored.log).toContain('[local cache]');
128
128
  expectRestoredNewer(restored, fromB, 'A');
129
129
  });
130
- }, 60_000);
130
+ });
131
131
 
132
132
  it('restores a remotely cached output newer than the output it replaces', async () => {
133
133
  // The self-hosted remote cache protocol Nx's native HTTP cache speaks: GET and PUT of an
@@ -167,4 +167,4 @@ it('restores a remotely cached output newer than the output it replaces', async
167
167
  expect(restored.log).toContain('[remote cache]');
168
168
  expectRestoredNewer(restored, fromB, 'A');
169
169
  });
170
- }, 60_000);
170
+ });
@@ -177,7 +177,7 @@ describe('a second daemon started for a workspace a daemon serves', () => {
177
177
  });
178
178
  });
179
179
  });
180
- }, 60_000);
180
+ });
181
181
 
182
182
  it('exits and leaves the serving daemon alone when both use one socket directory', async () => {
183
183
  await withNxFixture('nx-daemon-claim-shared-', async ({ socketDirectory, workspace }) => {
@@ -187,7 +187,7 @@ describe('a second daemon started for a workspace a daemon serves', () => {
187
187
  await secondDaemonDefers(workspace, env, env);
188
188
  });
189
189
  });
190
- }, 60_000);
190
+ });
191
191
  });
192
192
 
193
193
  it('replaces a daemon whose socket was removed, which no client could reach', async () => {
@@ -204,7 +204,7 @@ it('replaces a daemon whose socket was removed, which no client could reach', as
204
204
  const replacement = await daemonRecord(workspace);
205
205
  expect(replacement?.processId).not.toBe(lost.processId);
206
206
  });
207
- }, 60_000);
207
+ });
208
208
 
209
209
  it('keeps serving clients whose socket directory differs from the one it bound', async () => {
210
210
  await withNxFixture('nx-daemon-claim-env-', async ({ socketDirectory, workspace }) => {
@@ -229,7 +229,7 @@ it('keeps serving clients whose socket directory differs from the one it bound',
229
229
  });
230
230
  });
231
231
  });
232
- }, 60_000);
232
+ });
233
233
 
234
234
  it('replaces a record whose pid now belongs to an unrelated live process', async () => {
235
235
  await withNxFixture('nx-daemon-claim-reused-', async ({ socketDirectory, workspace }) => {
@@ -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 {
@@ -274,4 +276,4 @@ it('replaces a record whose pid now belongs to an unrelated live process', async
274
276
  await unrelatedExited;
275
277
  }
276
278
  });
277
- }, 60_000);
279
+ });
@@ -77,5 +77,4 @@ it.skipIf(process.env.NX_DAEMON_CPU_PROOF !== 'true')(
77
77
  expect(wallSeconds).toBeGreaterThanOrEqual(30);
78
78
  });
79
79
  },
80
- 120_000,
81
80
  );
@@ -1,10 +1,23 @@
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
7
  import { fixtureNxEnv, withNxFixture } from './__tests__/fixture-nx-env.js';
8
+ import { nxDaemonProcesses } from './testing.js';
9
+
10
+ // A graph plugin that asks its daemon for files, as globIndexedFiles does: from an isolated worker Nx routes the glob
11
+ // through the worker's own daemon client.
12
+ const DAEMON_GLOB_PLUGIN = `const { globWithWorkspaceContext } = require('nx/src/utils/workspace-context');
13
+ const { workspaceRoot } = require('nx/src/utils/workspace-root');
14
+ exports.createNodesV2 = ['**/project.json', async () => {
15
+ await globWithWorkspaceContext(workspaceRoot, ['**/project.json']);
16
+ return [];
17
+ }];
18
+ `;
19
+ // The daemon logs its open connection count on every connect and close.
20
+ const OPEN_CONNECTIONS = /Number of open connections: (\d+)$/;
8
21
 
9
22
  it('serves real daemon requests with the installed five-minute inactivity policy', async () => {
10
23
  const require = createRequire(import.meta.url);
@@ -34,6 +47,53 @@ it('serves real daemon requests with the installed five-minute inactivity policy
34
47
  });
35
48
  });
36
49
 
50
+ it('lets a resident plugin worker ask its daemon for files without holding the daemon open', async () => {
51
+ const repositoryRoot = join(import.meta.dir, '../../..');
52
+ const nxEntry = join(repositoryRoot, 'node_modules/.bin/nx');
53
+ await withNxFixture('nx-worker-connection-', async ({ workspace, socketDirectory }) => {
54
+ await symlink(join(repositoryRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
55
+ await writeFile(join(workspace, 'nx.json'), JSON.stringify({ plugins: ['./daemon-glob-plugin.js'] }));
56
+ await writeFile(join(workspace, 'package.json'), JSON.stringify({ name: 'worker-connection', private: true }));
57
+ await writeFile(join(workspace, 'daemon-glob-plugin.js'), DAEMON_GLOB_PLUGIN);
58
+ await mkdir(join(workspace, 'p'));
59
+ await writeFile(join(workspace, 'p/project.json'), JSON.stringify({ name: 'p' }));
60
+ const env = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true', NX_ISOLATE_PLUGINS: 'true' };
61
+ const requested = spawnSync('node', [nxEntry, 'show', 'projects', '--json'], {
62
+ cwd: workspace,
63
+ env,
64
+ encoding: 'utf8',
65
+ });
66
+ expect(requested.status, requested.stdout + requested.stderr).toBe(0);
67
+ expect(JSON.parse(requested.stdout)).toEqual(['p']);
68
+ const logPath = join(workspace, '.nx/workspace-data/d/daemon.log');
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.
72
+ let open: string | undefined;
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();
91
+ }
92
+ const workers = (await nxDaemonProcesses(workspace)).filter(({ command }) => command.includes('plugin-worker'));
93
+ expect(workers.length, 'the resident plugin worker that asked the daemon for files').toBeGreaterThan(0);
94
+ });
95
+ });
96
+
37
97
  it('syntax-checks every JavaScript file patched in the actual installed Nx product', () => {
38
98
  const installedNxRoot = dirname(createRequire(import.meta.url).resolve('nx/package.json'));
39
99
  const patch = readFileSync(new URL('../../../patches/nx@23.2.1.patch', import.meta.url), 'utf8');
@@ -154,7 +154,7 @@ it('keeps serving when a watched ignore file is rewritten with the bytes the dae
154
154
  await client.end();
155
155
  }
156
156
  });
157
- }, 60_000);
157
+ });
158
158
 
159
159
  /** A workspace whose context walk takes long enough that a write right after `watch()` lands before it. */
160
160
  async function largeWorkspace(workspace: string): Promise<void> {
@@ -207,4 +207,4 @@ it('stops when an ignore file changes after the watcher read it and before the d
207
207
  }
208
208
  }
209
209
  });
210
- }, 60_000);
210
+ });
@@ -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
 
@@ -575,7 +568,7 @@ it('awaits an answerable daemon after a closed PING without another socket-direc
575
568
  await closed.promise;
576
569
  }
577
570
  });
578
- }, 45_000);
571
+ });
579
572
 
580
573
  /** Valid lockfiles keep the real graph usable while the watcher judges byte changes. */
581
574
  async function lockContents(
@@ -665,7 +658,7 @@ for (const lockfile of LOCKFILES) {
665
658
  },
666
659
  FIXTURE_WORKSPACE,
667
660
  );
668
- }, 180_000);
661
+ });
669
662
  }
670
663
 
671
664
  it('does not serve a request sent on a connection opened before a lockfile change', async () => {
@@ -703,7 +696,7 @@ it('does not serve a request sent on a connection opened before a lockfile chang
703
696
  },
704
697
  FIXTURE_WORKSPACE,
705
698
  );
706
- }, 180_000);
699
+ });
707
700
 
708
701
  it('stops a daemon at the next request after an ignored lockfile changes, then its successor within its safety poll with no request', async () => {
709
702
  await withNxFixture(
@@ -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],
@@ -753,7 +745,7 @@ it('stops a daemon at the next request after an ignored lockfile changes, then i
753
745
  },
754
746
  FIXTURE_WORKSPACE,
755
747
  );
756
- }, 180_000);
748
+ });
757
749
 
758
750
  it('stops a daemon, refusing its next request, once the installed nx version changes in place', async () => {
759
751
  await withNxFixture(
@@ -781,7 +773,7 @@ it('stops a daemon, refusing its next request, once the installed nx version cha
781
773
  },
782
774
  FIXTURE_WORKSPACE,
783
775
  );
784
- }, 180_000);
776
+ });
785
777
 
786
778
  it('stops a daemon once the workspace nx link points at a different version', async () => {
787
779
  await withNxFixture(
@@ -806,7 +798,7 @@ it('stops a daemon once the workspace nx link points at a different version', as
806
798
  },
807
799
  FIXTURE_WORKSPACE,
808
800
  );
809
- }, 180_000);
801
+ });
810
802
 
811
803
  it('leaves a successor record and socket in place when another process takes the record', async () => {
812
804
  await withNxFixture(
@@ -840,7 +832,7 @@ it('leaves a successor record and socket in place when another process takes the
840
832
  },
841
833
  FIXTURE_WORKSPACE,
842
834
  );
843
- }, 180_000);
835
+ });
844
836
 
845
837
  it("leaves a replacement socket in place when the daemon's own socket path is replaced", async () => {
846
838
  await withNxFixture(
@@ -863,7 +855,7 @@ it("leaves a replacement socket in place when the daemon's own socket path is re
863
855
  },
864
856
  FIXTURE_WORKSPACE,
865
857
  );
866
- }, 180_000);
858
+ });
867
859
 
868
860
  it('stops a daemon whose own socket path is removed with nothing taking its place', async () => {
869
861
  await withNxFixture(
@@ -878,7 +870,29 @@ it('stops a daemon whose own socket path is removed with nothing taking its plac
878
870
  },
879
871
  FIXTURE_WORKSPACE,
880
872
  );
881
- }, 180_000);
873
+ });
874
+
875
+ it('stops a daemon whose workspace files are all deleted before it reads the emptied workspace', async () => {
876
+ await withNxFixture(
877
+ 'nx-daemon-validity-emptied-',
878
+ async ({ socketDirectory, workspace }) => {
879
+ await fixtureWorkspace(workspace);
880
+ const env = fixtureNxEnv(workspace, socketDirectory);
881
+ const daemon = await startDaemon(workspace, env);
882
+ expect(JSON.parse(nx(workspace, { ...env, NX_DAEMON: 'true' }, 'show', 'projects', '--json'))).toEqual(['p']);
883
+ // Every tracked file goes, as when a checkout is removed or unmounted under its daemon; the daemon's own record
884
+ // under .nx stays, so only the workspace check can stop it. Reading the emptied file map instead would block
885
+ // the daemon's only thread for good, where no SIGTERM handler runs.
886
+ await rm(join(workspace, 'p'), { recursive: true });
887
+ await unlink(join(workspace, 'package.json'));
888
+ await unlink(join(workspace, 'nx.json'));
889
+ await awaitExit([daemon.entry], `daemon ${daemon.record.processId} after every workspace file was deleted`);
890
+ const log = await readFile(join(workspace, '.nx', 'workspace-data', 'd', 'daemon.log'), 'utf8');
891
+ expect(log).toMatch(/Server stopped because: "its workspace \S+ has no nx\.json any more"/);
892
+ },
893
+ FIXTURE_WORKSPACE,
894
+ );
895
+ });
882
896
 
883
897
  it('keeps an idle daemon off the lockfiles, record, and socket between its safety polls', async () => {
884
898
  await withNxFixture(
@@ -922,4 +936,4 @@ it('keeps an idle daemon off the lockfiles, record, and socket between its safet
922
936
  },
923
937
  FIXTURE_WORKSPACE,
924
938
  );
925
- }, 120_000);
939
+ });
@@ -167,7 +167,7 @@ it('indexes every tracked dot input and none of the untracked dot state', async
167
167
  expect(after).toEqual(before.filter((path) => tracked.includes(path)));
168
168
  expect(after).toContain('.github/workflows/ci.yml');
169
169
  expect(after).toContain('.cargo/config.toml');
170
- }, 120_000);
170
+ });
171
171
 
172
172
  it('keeps the watcher reporting tracked dot inputs and silent about untracked dot state', async () => {
173
173
  const nxignore = await readFile(join(repositoryRoot, '.nxignore'), 'utf8');
@@ -179,7 +179,7 @@ it('keeps the watcher reporting tracked dot inputs and silent about untracked do
179
179
  const current = await withWorkspace(nxignore, reportedAfterWrites);
180
180
  expect(current).toContain(TRACKED_DOT_WRITE);
181
181
  expect(current.filter((path) => RUNTIME_DOT_STATE.includes(path))).toEqual([]);
182
- }, 120_000);
182
+ });
183
183
 
184
184
  it('fails the lint when a tracked dot path lacks its re-include line, and passes with it', async () => {
185
185
  const nxignore = await readFile(join(repositoryRoot, '.nxignore'), 'utf8');
@@ -203,7 +203,7 @@ it('fails the lint when a tracked dot path lacks its re-include line, and passes
203
203
  const checkout = runLint(repositoryRoot);
204
204
  expect(checkout.stderr).toBe('');
205
205
  expect(checkout.status).toBe(0);
206
- }, 120_000);
206
+ });
207
207
 
208
208
  it('rejects a newly tracked dot input until its explicit negation is added', async () => {
209
209
  const nxignore = await readFile(join(repositoryRoot, '.nxignore'), 'utf8');
@@ -217,7 +217,7 @@ it('rejects a newly tracked dot input until its explicit negation is added', asy
217
217
  const included = runLint(root);
218
218
  expect(included.status, included.stderr).toBe(0);
219
219
  });
220
- }, 120_000);
220
+ });
221
221
 
222
222
  it('preserves tracked input hashes and the graph while dot staging never causes graph passes', async () => {
223
223
  const nxignore = await readFile(join(repositoryRoot, '.nxignore'), 'utf8');
@@ -350,4 +350,4 @@ it('preserves tracked input hashes and the graph while dot staging never causes
350
350
  await ended;
351
351
  }
352
352
  });
353
- }, 120_000);
353
+ });
@@ -94,4 +94,4 @@ it('hashes an input rewritten in the second its archived hash was taken', async
94
94
  expect(skewed.log).not.toContain('[local cache]');
95
95
  expect(skewed.result).toBe('E');
96
96
  });
97
- }, 60_000);
97
+ });
@@ -67,4 +67,4 @@ it('does not report success for a run the orchestrator abandoned part way throug
67
67
  expect(output).toContain('Tasks the run ended without finishing:\n\n- p:second');
68
68
  }
69
69
  });
70
- }, 60_000);
70
+ });
@@ -41,7 +41,7 @@ it('names the duplicate project and both roots when local plugin resolution fail
41
41
  expect(output).toContain('- packages/second');
42
42
  expect(output).not.toContain('fixture-plugin-executed');
43
43
  });
44
- }, 60_000);
44
+ });
45
45
 
46
46
  for (const fixture of [
47
47
  {
@@ -116,5 +116,5 @@ throw failure;
116
116
  for (const message of fixture.messages) expect(output).toContain(message);
117
117
  if (fixture.repeated !== undefined) expect(output.split(fixture.repeated).length - 1).toBe(2);
118
118
  });
119
- }, 60_000);
119
+ });
120
120
  }