@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
@@ -34,6 +34,11 @@ export function nodeStamp(stats: StampFields): string {
34
34
  return `${stats.dev}:${stats.ino}:${stats.mode}:${stats.size}:${stats.mtimeNs}:${stats.ctimeNs}`;
35
35
  }
36
36
 
37
+ /** The file type and permission bits a {@link nodeStamp} records: what a copy of the node keeps of its stamp. */
38
+ export function stampMode(stamp: string): string {
39
+ return stamp.split(':')[2];
40
+ }
41
+
37
42
  /**
38
43
  * How an observed stamp compares with the recorded one: `same`; `ctime` when device, inode, mode, size and mtime are
39
44
  * kept and only the ctime moved (a same-mode chmod, which Bun's bin linking does to a shared store file on any
@@ -96,10 +101,12 @@ export interface WorkspaceSnapshot {
96
101
  }
97
102
 
98
103
  export interface GraphWitness {
99
- readonly version: 1;
104
+ readonly version: 2;
100
105
  readonly workspaceRoot: string;
101
106
  readonly environment: string;
102
107
  readonly installation: string;
108
+ /** The census behind `installation` read these files in the tick they changed in: {@link GraphInstallationCensus}. */
109
+ readonly installationUnproven: readonly string[];
103
110
  readonly snapshot: WorkspaceSnapshot;
104
111
  readonly projectGraph: ProjectGraph;
105
112
  }
@@ -223,7 +230,7 @@ export function reusableGraph(
223
230
  snapshot: WorkspaceSnapshot,
224
231
  ): ProjectGraph | null {
225
232
  return witness !== null &&
226
- witness.version === 1 &&
233
+ witness.version === 2 &&
227
234
  witness.workspaceRoot === workspaceRoot &&
228
235
  installation !== null &&
229
236
  witness.installation === installation &&
@@ -233,13 +240,15 @@ export function reusableGraph(
233
240
  : null;
234
241
  }
235
242
 
236
- /** What {@link graphInstallationIdentity} binds: resolution edges `[from, name, physical manifest]`, node stamps and
237
- * file hashes, read once.
243
+ /** What {@link graphInstallationIdentity} binds: resolution edges `[from, name, physical manifest]`, node stamps as
244
+ * read and file hashes, read once. `unproven` names the files this census read in the tick they changed in
245
+ * ({@link unproven}); it is not part of the identity, only what a later census that reuses these hashes must re-read.
238
246
  */
239
247
  export interface GraphInstallationCensus {
240
248
  readonly resolutions: readonly (readonly [string, string, string | null])[];
241
249
  readonly nodes: readonly NodeWitness[];
242
250
  readonly files: readonly { readonly path: string; readonly sha256: string }[];
251
+ readonly unproven: readonly string[];
243
252
  }
244
253
 
245
254
  /** Plugins with opaque external graph inputs cannot borrow this proof.
@@ -257,7 +266,59 @@ export function graphInstallationIdentity(
257
266
  localInputs: GraphInputContractProof | null,
258
267
  onUnproved?: (detail: string) => void,
259
268
  ): string | null {
260
- const census = graphInstallationCensus(workspaceRoot, nxJson, resolvePackage, expand, localInputs, onUnproved);
269
+ const census = provenInstallationCensus(workspaceRoot, nxJson, resolvePackage, expand, localInputs, onUnproved);
270
+ return census === null ? null : installationIdentityOf(census, localInputs);
271
+ }
272
+
273
+ /** The identity {@link graphInstallationIdentity} names for a proven census: what it read, never what it reused. */
274
+ export function installationIdentityOf(
275
+ { resolutions, nodes, files }: GraphInstallationCensus,
276
+ localInputs: GraphInputContractProof | null,
277
+ ): string {
278
+ return JSON.stringify({ resolutions, nodes, files, localInputs: localInputs?.identity ?? null });
279
+ }
280
+
281
+ /** The census behind a recorded identity ({@link installationIdentityOf}) and its unproven files, or `undefined` for
282
+ * one that does not parse: a hash source for {@link graphInstallationCensus}, never a proof.
283
+ */
284
+ export function recordedInstallationCensus(
285
+ identity: string,
286
+ unproven: readonly string[],
287
+ ): GraphInstallationCensus | undefined {
288
+ try {
289
+ const recorded = typia.json.isParse<Omit<GraphInstallationCensus, 'unproven'>>(identity);
290
+ return recorded === null
291
+ ? undefined
292
+ : { resolutions: recorded.resolutions, nodes: recorded.nodes, files: recorded.files, unproven };
293
+ } catch (error) {
294
+ if (error instanceof SyntaxError) return undefined;
295
+ throw error;
296
+ }
297
+ }
298
+
299
+ /**
300
+ * A {@link graphInstallationCensus} that no node moved during: every node is read again after the census, and one
301
+ * whose stamp moved fails it, unless only its ctime moved and its bytes are the ones hashed. `previous` is what the
302
+ * census may take hashes from. `null`, with `onUnproved` told why, for a closure that cannot be read or that moved.
303
+ */
304
+ export function provenInstallationCensus(
305
+ workspaceRoot: string,
306
+ nxJson: NxJsonConfiguration,
307
+ resolvePackage: typeof readModulePackageJsonWithoutFallbacks,
308
+ expand: typeof expandOutputs,
309
+ localInputs: GraphInputContractProof | null,
310
+ onUnproved?: (detail: string) => void,
311
+ previous?: GraphInstallationCensus,
312
+ ): GraphInstallationCensus | null {
313
+ const census = graphInstallationCensus(
314
+ workspaceRoot,
315
+ nxJson,
316
+ resolvePackage,
317
+ expand,
318
+ localInputs,
319
+ onUnproved,
320
+ previous,
321
+ );
261
322
  if (census === null) return null;
262
323
  try {
263
324
  // A file whose ctime alone moved is re-hashed: Bun's bin linking on any checkout's install re-chmods a shared
@@ -279,12 +340,22 @@ export function graphInstallationIdentity(
279
340
  }
280
341
  throw error;
281
342
  }
282
- return JSON.stringify({ ...census, localInputs: localInputs?.identity ?? null });
343
+ return census;
283
344
  }
284
345
 
285
- /** One read of the installation closure {@link graphInstallationIdentity} binds, without its final check that no node
346
+ /**
347
+ * One read of the installation closure {@link graphInstallationIdentity} binds, without its final check that no node
286
348
  * moved while it was read: what a caller that needs the closure, not a proof of it, reads. `null`, with `onUnproved`
287
349
  * told why, for a closure that cannot be read.
350
+ *
351
+ * Every census resolves every edge, lists every directory and stats every node; only file bytes are reused. A file
352
+ * takes its hash from `previous` when its stamp (device, inode, mode, size, and mtime and ctime in nanoseconds) is
353
+ * the one `previous` read and `previous` did not read it in the tick it changed in. A write moves a file's ctime to
354
+ * the tick it happens in, so a file written since `previous` stat-ed it, even between that stat and its read, one
355
+ * whose stamp `previous` lacks, and one `previous` recorded unproven are read again; `previous` need not be proven.
356
+ * The key is the stamp, not a cheaper generation (a resolution, a manifest, an install marker): a rebuild of a
357
+ * developer-linked package, this plugin's own `dist`, rewrites its files in place and moves none of those, so a census
358
+ * keyed on them would describe a tree that is no longer there.
288
359
  */
289
360
  export function graphInstallationCensus(
290
361
  workspaceRoot: string,
@@ -293,6 +364,7 @@ export function graphInstallationCensus(
293
364
  expand: typeof expandOutputs,
294
365
  localInputs: GraphInputContractProof | null,
295
366
  onUnproved?: (detail: string) => void,
367
+ previous?: GraphInstallationCensus,
296
368
  ): GraphInstallationCensus | null {
297
369
  const plugins = (nxJson.plugins ?? []).map((plugin) => (typeof plugin === 'string' ? plugin : plugin.plugin));
298
370
  const unproved = plugins.filter(
@@ -302,8 +374,15 @@ export function graphInstallationCensus(
302
374
  onUnproved?.(`Unproved configured graph plugins: ${unproved.join(', ')}`);
303
375
  return null;
304
376
  }
377
+ const start = censusStart();
305
378
  const nodes: NodeWitness[] = [];
306
379
  const files: { path: string; sha256: string }[] = [];
380
+ const unprovenFiles: string[] = [];
381
+ // The stamps `previous` read its files at, those it read in their change tick left out, and the hashes it read.
382
+ const recordedUnproven = new Set(previous?.unproven);
383
+ const recordedStamps = new Map<string, string>();
384
+ for (const { path, stamp } of previous?.nodes ?? []) if (!recordedUnproven.has(path)) recordedStamps.set(path, stamp);
385
+ const recordedHashes = new Map(previous?.files.map(({ path, sha256 }) => [path, sha256]));
307
386
  const buffer = Buffer.allocUnsafe(64 * 1024);
308
387
  const resolutions: [string, string, string | null][] = [];
309
388
  const visited = new Set<string>();
@@ -311,7 +390,8 @@ export function graphInstallationCensus(
311
390
  const expandedRoots = new Set<string>();
312
391
  function walk(path: string, selected?: ReadonlySet<string>): void {
313
392
  const stats = lstatSync(path, { bigint: true });
314
- nodes.push({ path, stamp: nodeStamp(stats) });
393
+ const stamp = nodeStamp(stats);
394
+ nodes.push({ path, stamp });
315
395
  if (stats.isSymbolicLink()) {
316
396
  const physical = realpathSync(path);
317
397
  if (lstatSync(physical).isDirectory() && !declaredDirectoryLinks.has(path)) {
@@ -333,17 +413,22 @@ export function graphInstallationCensus(
333
413
  } else if (!stats.isFile()) {
334
414
  throw new Error(`Unproved graph package node: ${path}`);
335
415
  } else {
336
- const hash = createHash('sha256');
337
- const fd = openSync(path, 'r');
338
- try {
339
- for (let count = readSync(fd, buffer); count > 0; count = readSync(fd, buffer)) {
340
- hash.update(buffer.subarray(0, count));
341
- }
342
- } finally {
343
- closeSync(fd);
416
+ if (unproven(stats, start)) unprovenFiles.push(path);
417
+ const recorded = recordedStamps.get(path) === stamp ? recordedHashes.get(path) : undefined;
418
+ files.push({ path, sha256: recorded ?? fileHash(path) });
419
+ }
420
+ }
421
+ function fileHash(path: string): string {
422
+ const hash = createHash('sha256');
423
+ const fd = openSync(path, 'r');
424
+ try {
425
+ for (let count = readSync(fd, buffer); count > 0; count = readSync(fd, buffer)) {
426
+ hash.update(buffer.subarray(0, count));
344
427
  }
345
- files.push({ path, sha256: hash.digest('hex') });
428
+ } finally {
429
+ closeSync(fd);
346
430
  }
431
+ return hash.digest('hex');
347
432
  }
348
433
  function installedManifest(name: string, from: string): InstalledPackageManifest {
349
434
  try {
@@ -449,7 +534,7 @@ export function graphInstallationCensus(
449
534
  visit(name, workspaceRoot, false);
450
535
  for (const input of localInputs?.packages ?? []) visit(input.name, input.from, false);
451
536
  visit('typescript', workspaceRoot, true);
452
- return { resolutions, nodes, files };
537
+ return { resolutions, nodes, files, unproven: unprovenFiles };
453
538
  } catch (error) {
454
539
  if (systemError(error) && ['EACCES', 'EPERM', 'EIO'].includes(error.code)) throw error;
455
540
  // Installation uncertainty disables only graph reuse. Nx's authoritative
@@ -1,6 +1,6 @@
1
1
  import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'bun:test';
2
2
  import { spawn, spawnSync } from 'node:child_process';
3
- import { chmod, mkdir, mkdtemp, readdir, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
3
+ import { chmod, cp, mkdir, mkdtemp, readdir, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, isAbsolute, join } from 'node:path';
6
6
  import { fileURLToPath } from 'node:url';
@@ -727,8 +727,8 @@ const FIXTURE_NX_JSON = {
727
727
  plugins: ['@smoothbricks/nx-plugin'],
728
728
  };
729
729
 
730
- /** The Nx installation this file's fixtures share: one copy per run of the file, not one per fixture. */
731
- const installation = testFileInstallation('ensure-built-installation-');
730
+ /** The Nx installation this file's fixtures share: one copy kept for this file, not one per fixture. */
731
+ const installation = testFileInstallation('ensure-built-installation');
732
732
 
733
733
  /**
734
734
  * Two projects, `app:build` reading `lib:build`'s outputs through
@@ -834,6 +834,17 @@ describe('smoo-nx-exec with a fresh daemon', () => {
834
834
  async ({ root, workspace, socketDirectory }) => {
835
835
  const builds = join(root, 'lib-builds.log');
836
836
  await writeTwoProjectWorkspace(workspace, builds);
837
+ // The workspace takes its own copy of TypeScript, which the census resolves from the workspace root, so the
838
+ // installation the script below writes into is the fixture's, not the copy this file's fixtures share.
839
+ const shared = await realpath(join(workspace, 'node_modules'));
840
+ await rm(join(workspace, 'node_modules'));
841
+ await mkdir(join(workspace, 'node_modules'));
842
+ for (const entry of await readdir(shared)) {
843
+ if (entry !== 'typescript') await symlink(join(shared, entry), join(workspace, 'node_modules', entry));
844
+ }
845
+ await cp(await realpath(join(shared, 'typescript')), join(workspace, 'node_modules', 'typescript'), {
846
+ recursive: true,
847
+ });
837
848
  const built = await run(
838
849
  'node',
839
850
  nxEntry(workspace),
@@ -850,7 +861,7 @@ describe('smoo-nx-exec with a fresh daemon', () => {
850
861
  import assert from 'node:assert/strict';
851
862
  import { existsSync } from 'node:fs';
852
863
  import { createRequire } from 'node:module';
853
- import { chmod, readFile, rename, rm, stat, writeFile } from 'node:fs/promises';
864
+ import { chmod, readFile, rename, rm, stat, utimes, writeFile } from 'node:fs/promises';
854
865
  import { join } from 'node:path';
855
866
  import { ensureBuilt } from ${JSON.stringify(join(packageRoot, 'dist', 'ensure-built.js'))};
856
867
  const requireNx = createRequire(process.cwd() + '/package.json');
@@ -894,6 +905,17 @@ await writeFile(project, JSON.stringify(config));
894
905
  await check('dependency');
895
906
  await writeFile(project, original);
896
907
  await check('dependency-restored');
908
+ // Every call censuses the installation again, taking the hashes of files whose stamps have not moved from the
909
+ // witness. A same-size rewrite that keeps its file's mtime moves its ctime: the next call reads it and sees the
910
+ // changed installation, and the call after that reuses the graph the changed installation was proved with.
911
+ const installed = join(process.cwd(), 'node_modules/typescript/README.md');
912
+ const readme = await readFile(installed);
913
+ const { atime, mtime } = await stat(installed);
914
+ readme[0] ^= 1;
915
+ await writeFile(installed, readme);
916
+ await utimes(installed, atime, mtime);
917
+ await check('installation');
918
+ await check('installation-reuse');
897
919
  // Save only evidence the real daemon generation actually authorized, outside
898
920
  // the workspace: the later missing-witness RPC is not obliged to replace it.
899
921
  const authoritativeWitness = await readFile(witness, 'utf8');
@@ -937,7 +959,14 @@ await writeFile(${JSON.stringify(resultsPath)}, JSON.stringify(results));
937
959
  disposition: 'hit',
938
960
  witnessExists: true,
939
961
  });
940
- for (const label of ['created', 'deleted', 'dependency', 'dependency-restored', 'unreadable-witness']) {
962
+ for (const label of [
963
+ 'created',
964
+ 'deleted',
965
+ 'dependency',
966
+ 'dependency-restored',
967
+ 'installation',
968
+ 'unreadable-witness',
969
+ ]) {
941
970
  expect(graphChecks.find((check) => check.label === label)?.requests, label).toBeGreaterThan(0);
942
971
  }
943
972
  expect(graphChecks.find(({ label }) => label === 'missing-witness')).toEqual({
@@ -946,6 +975,12 @@ await writeFile(${JSON.stringify(resultsPath)}, JSON.stringify(results));
946
975
  disposition: 'hit',
947
976
  witnessExists: false,
948
977
  });
978
+ expect(graphChecks.find(({ label }) => label === 'installation-reuse')).toEqual({
979
+ label: 'installation-reuse',
980
+ requests: 0,
981
+ disposition: 'hit',
982
+ witnessExists: true,
983
+ });
949
984
  expect(graphChecks.find(({ label }) => label === 'unreadable-witness')?.disposition).toBe('hit');
950
985
  expect(await readFile(builds, 'utf8')).toBe('lib\n');
951
986
  expect(await readFile(join(workspace, 'packages/app/dist/marker.txt'), 'utf8')).toBe('built\nlib\n');
@@ -45,11 +45,14 @@ import {
45
45
  censusStart,
46
46
  type GraphWitness,
47
47
  graphInstallationIdentity,
48
+ installationIdentityOf,
48
49
  movedNodes,
49
50
  nodeStamp,
50
51
  nodesUnchanged,
52
+ provenInstallationCensus,
51
53
  readArtifactWitness,
52
54
  readGraphWitness,
55
+ recordedInstallationCensus,
53
56
  recordedStamp,
54
57
  reusableGraph,
55
58
  snapshotsEqual,
@@ -1065,31 +1068,50 @@ async function currentProjectGraph(
1065
1068
  await daemonClient.updateWorkspaceContext(created, updated, deleted);
1066
1069
  }
1067
1070
  performance.measure('ensureBuilt:inputs', 'ensureBuilt:inputs:start');
1068
- const installation = graphInstallationIdentity(
1071
+ // The witness's census is a hash source: a file whose proven stamp has not moved since it was read is not read again.
1072
+ const recorded =
1073
+ witness === null ? undefined : recordedInstallationCensus(witness.installation, witness.installationUnproven);
1074
+ const localInputs = graphInputContractIdentity(workspaceRoot, nxJson, before);
1075
+ const census = provenInstallationCensus(
1069
1076
  workspaceRoot,
1070
1077
  nxJson,
1071
1078
  packageResolution.readModulePackageJson,
1072
1079
  expandOutputs,
1073
- graphInputContractIdentity(workspaceRoot, nxJson, before),
1080
+ localInputs,
1081
+ undefined,
1082
+ recorded,
1074
1083
  );
1084
+ const installation = census === null ? null : installationIdentityOf(census, localInputs);
1075
1085
  const cached = nodesUnchanged(before.nodes)
1076
1086
  ? reusableGraph(witness, workspaceRoot, environment, installation, before)
1077
1087
  : null;
1078
1088
  if (!forceGraphRpc && cached !== null) return cached;
1079
1089
  const { projectGraph } = await daemonClient.getProjectGraphAndSourceMaps();
1080
1090
  const after = workspaceSnapshot(workspaceRoot, new WorkspaceContext(workspaceRoot, data), before);
1081
- const finalInstallation = graphInstallationIdentity(
1091
+ const finalInputs = graphInputContractIdentity(workspaceRoot, nxJson, after);
1092
+ const finalCensus = provenInstallationCensus(
1082
1093
  workspaceRoot,
1083
1094
  nxJson,
1084
1095
  packageResolution.readModulePackageJson,
1085
1096
  expandOutputs,
1086
- graphInputContractIdentity(workspaceRoot, nxJson, after),
1097
+ finalInputs,
1098
+ undefined,
1099
+ census ?? recorded,
1087
1100
  );
1101
+ const finalInstallation = finalCensus === null ? null : installationIdentityOf(finalCensus, finalInputs);
1088
1102
  // `after` re-hashed every node whose stamp does not prove it (its ctime alone moved, or `before` read it in the tick
1089
1103
  // it changed in), and snapshotsEqual compares those hashes.
1090
1104
  const stableGeneration = snapshotsEqual(before, after) && movedNodes(before.nodes, () => true) === null;
1091
- if (installation !== null && installation === finalInstallation && stableGeneration) {
1092
- const next: GraphWitness = { version: 1, workspaceRoot, environment, installation, snapshot: after, projectGraph };
1105
+ if (installation !== null && finalCensus !== null && installation === finalInstallation && stableGeneration) {
1106
+ const next: GraphWitness = {
1107
+ version: 2,
1108
+ workspaceRoot,
1109
+ environment,
1110
+ installation,
1111
+ installationUnproven: finalCensus.unproven,
1112
+ snapshot: after,
1113
+ projectGraph,
1114
+ };
1093
1115
  writeWitness(witnessPath, next);
1094
1116
  }
1095
1117
  // A write during the await cannot authorize a saved graph. Feed the final
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 () => {
@@ -0,0 +1,126 @@
1
+ import { expect, it } from 'bun:test';
2
+ import { spawn, spawnSync } from 'node:child_process';
3
+ import { mkdir, readFile, symlink, writeFile } from 'node:fs/promises';
4
+ import { createRequire } from 'node:module';
5
+ import { join } from 'node:path';
6
+ import { fixtureNxEnv, withNxFixture } from './__tests__/fixture-nx-env.js';
7
+ import { awaitExit, isRunning, processTable, terminate } from './testing.js';
8
+
9
+ /**
10
+ * Guards the `NX_DAEMON_START=never` hunk of `patches/nx@23.2.1.patch`. A stock client that finds no live daemon
11
+ * starts one, detached into a session of its own, and the record shares it with every client of the checkout. Cowshed
12
+ * ends a job's whole process tree when the job ends (`specs/cowshed/11_shell.md`), so a daemon one job's client
13
+ * started would end with that job while another job's `nx run` still used it. Every cowshed job therefore runs with
14
+ * `NX_DAEMON_START=never` (`specs/cowshed/04_sandbox.md`): its client uses the daemon the record names while that
15
+ * runs, otherwise computes the command without one, and never starts a daemon. A daemon that stops under such a
16
+ * client turns the daemon off for that process alone, and the command continues without it: its successor is its
17
+ * starter's to start, and the stop is no reason to disable the checkout's daemon for anyone else.
18
+ */
19
+
20
+ const repositoryRoot = join(import.meta.dir, '../../..');
21
+ const nxEntry = join(repositoryRoot, 'node_modules', '.bin', 'nx');
22
+ const nxVersion: string = createRequire(join(repositoryRoot, 'package.json'))('nx/package.json').version;
23
+
24
+ const record = (workspace: string) => join(workspace, '.nx/workspace-data/d/server-process.json');
25
+
26
+ async function recordedPid(workspace: string): Promise<number | null> {
27
+ const text = await readFile(record(workspace), 'utf8').catch((error: unknown) => {
28
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return null;
29
+ throw error;
30
+ });
31
+ if (text === null) return null;
32
+ const parsed: unknown = JSON.parse(text);
33
+ if (
34
+ typeof parsed !== 'object' ||
35
+ parsed === null ||
36
+ !('processId' in parsed) ||
37
+ typeof parsed.processId !== 'number'
38
+ ) {
39
+ throw new Error(`the daemon record names no process: ${text}`);
40
+ }
41
+ return parsed.processId;
42
+ }
43
+
44
+ async function fixtureWorkspace(workspace: string): Promise<void> {
45
+ await symlink(join(repositoryRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
46
+ await writeFile(join(workspace, 'nx.json'), '{}\n');
47
+ await writeFile(join(workspace, 'package.json'), JSON.stringify({ name: 'daemon-start-never', private: true }));
48
+ await mkdir(join(workspace, 'p'));
49
+ await writeFile(join(workspace, 'p', 'project.json'), JSON.stringify({ name: 'p' }));
50
+ }
51
+
52
+ /**
53
+ * Run `nx show projects --json`, require it to list the fixture's one project, and return everything it printed. The
54
+ * format is asked for, never inferred: unasked, Nx prints JSON only when it detects an AI agent in the environment
55
+ * (`show/command-object.js`, `isAiAgent`) and a plain name per line otherwise. The list is stdout's last line: a note
56
+ * Nx prints about its daemon goes to stdout before it.
57
+ */
58
+ function showProjects(workspace: string, env: Record<string, string>): string {
59
+ const ran = spawnSync('bun', [nxEntry, 'show', 'projects', '--json'], { cwd: workspace, env, encoding: 'utf8' });
60
+ const output = `${ran.stdout}${ran.stderr}`;
61
+ expect(ran.status, output).toBe(0);
62
+ expect(JSON.parse(ran.stdout.trimEnd().split('\n').at(-1) ?? ''), output).toEqual(['p']);
63
+ return output;
64
+ }
65
+
66
+ it('a client that may never start a daemon uses a running one and otherwise runs without', async () => {
67
+ await withNxFixture('nx-daemon-start-never', async ({ workspace, socketDirectory }) => {
68
+ await fixtureWorkspace(workspace);
69
+ const env = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true' };
70
+ const never = { ...env, NX_DAEMON_START: 'never' };
71
+
72
+ // No daemon runs: the command is computed in the client, and no daemon is started.
73
+ showProjects(workspace, never);
74
+ expect(await recordedPid(workspace)).toBeNull();
75
+
76
+ // Control: the same client without the variable starts one, so the absence above is the variable's doing.
77
+ showProjects(workspace, env);
78
+ const daemon = await recordedPid(workspace);
79
+ if (daemon === null) throw new Error('a client allowed to start a daemon left no record');
80
+ expect(isRunning(daemon)).toBe(true);
81
+
82
+ // A daemon runs: the client uses it, and it is still the one the record names.
83
+ const log = join(workspace, '.nx/workspace-data/d/daemon.log');
84
+ const before = (await readFile(log, 'utf8')).length;
85
+ showProjects(workspace, never);
86
+ expect(await recordedPid(workspace)).toBe(daemon);
87
+ expect((await readFile(log, 'utf8')).length).toBeGreaterThan(before);
88
+
89
+ // A record whose daemon has gone (killed, so it never cleared its record) is no daemon to use and no reason to
90
+ // start one.
91
+ const killed = (await processTable()).filter((entry) => entry.pid === daemon);
92
+ terminate(daemon, 'SIGKILL');
93
+ await awaitExit(killed, 'the killed fixture daemon');
94
+ showProjects(workspace, never);
95
+ expect(await recordedPid(workspace)).toBe(daemon);
96
+ });
97
+ });
98
+
99
+ it('a client that may never start a daemon continues without one that stopped under it, and disables nothing', async () => {
100
+ await withNxFixture('nx-daemon-start-gone', async ({ workspace, socketDirectory }) => {
101
+ await fixtureWorkspace(workspace);
102
+ const never = { ...fixtureNxEnv(workspace, socketDirectory), NX_DAEMON: 'true', NX_DAEMON_START: 'never' };
103
+
104
+ // The record names a running process that serves no socket: a daemon that stopped answering after the client
105
+ // chose to use it.
106
+ const holder = spawn('sleep', ['300'], { stdio: 'ignore' });
107
+ try {
108
+ const pid = holder.pid;
109
+ if (pid === undefined) throw new Error('sleep did not start');
110
+ await mkdir(join(workspace, '.nx/workspace-data/d'), { recursive: true });
111
+ await writeFile(
112
+ record(workspace),
113
+ JSON.stringify({ processId: pid, socketPath: join(socketDirectory, 'gone.sock'), nxVersion }),
114
+ );
115
+
116
+ expect(showProjects(workspace, never)).toContain('NX_DAEMON_START=never: this client never starts a daemon');
117
+ expect(await recordedPid(workspace)).toBe(pid);
118
+ // Not disabled for the checkout: the marker only `nx reset` clears would follow it into every terminal.
119
+ await expect(readFile(join(workspace, '.nx/workspace-data/d/disabled'), 'utf8')).rejects.toMatchObject({
120
+ code: 'ENOENT',
121
+ });
122
+ } finally {
123
+ holder.kill('SIGKILL');
124
+ }
125
+ });
126
+ });
@@ -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-',