@smoothbricks/nx-plugin 0.5.1 → 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 (53) hide show
  1. package/README.md +55 -11
  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 +19 -5
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +4 -0
  13. package/dist/owned-installation.d.ts +6 -5
  14. package/dist/owned-installation.d.ts.map +1 -1
  15. package/dist/owned-installation.js +324 -201
  16. package/dist/test-walk-policy.d.ts +8 -0
  17. package/dist/test-walk-policy.d.ts.map +1 -1
  18. package/dist/test-walk-policy.js +34 -0
  19. package/dist/workspace-manifest.d.ts +5 -5
  20. package/dist/workspace-manifest.d.ts.map +1 -1
  21. package/managed/raw/patches/nx@23.2.1.patch +14 -3
  22. package/managed/raw/tooling/direnv/developer-links.ts +37 -10
  23. package/managed/raw/tooling/direnv/linked-snapshots.ts +31 -4
  24. package/managed/raw/tooling/direnv/setup-environment.ts +2 -0
  25. package/managed/raw/tooling/git-hooks/pre-push.sh +34 -11
  26. package/package.json +3 -3
  27. package/runtime-input.sh +21 -9
  28. package/src/__tests__/cargo-closure-filesets.ts +94 -0
  29. package/src/__tests__/owned-installation.ts +5 -6
  30. package/src/bin/smoo-nx-test-clocks.ts +2 -2
  31. package/src/bin/smoo-nx-test-walks.ts +6 -9
  32. package/src/cargo-closure-filesets.test.ts +63 -0
  33. package/src/cargo-closure-input.test.ts +1 -1
  34. package/src/cargo-closure-input.ts +23 -15
  35. package/src/cargo-closure-key.test.ts +1 -1
  36. package/src/cargo-resolve-cache.test.ts +1 -1
  37. package/src/cross-check-policy.test.ts +53 -12
  38. package/src/ensure-built-witness.test.ts +53 -2
  39. package/src/ensure-built-witness.ts +103 -18
  40. package/src/ensure-built.test.ts +40 -5
  41. package/src/ensure-built.ts +28 -6
  42. package/src/index.ts +4 -0
  43. package/src/linked-snapshots.nx.test.ts +1 -1
  44. package/src/nextest-partitions.test.ts +1 -1
  45. package/src/nx-daemon-validity.test.ts +51 -0
  46. package/src/owned-installation.test.ts +105 -0
  47. package/src/owned-installation.ts +335 -93
  48. package/src/runtime-input.test.ts +22 -1
  49. package/src/task-results.test.ts +1 -1
  50. package/src/test-walk-policy.test.ts +35 -0
  51. package/src/test-walk-policy.ts +32 -0
  52. package/src/workspace-manifest.ts +5 -5
  53. package/test-timings.json +94 -92
@@ -1,14 +1,13 @@
1
- /** This repository's fixtures own a copy of its Nx installation (`../owned-installation.ts`) in an Nx fixture root. */
1
+ /** This repository's fixtures own a copy of its Nx installation (`../owned-installation.ts`), kept in its tool cache. */
2
2
  import { join } from 'node:path';
3
3
  import { ownedInstallation, type TestFileInstallation } from '../owned-installation.js';
4
- import { nxFixtureRoot } from './fixture-nx-env.js';
5
4
 
6
5
  const repositoryRoot = join(import.meta.dir, '../../../..');
7
6
 
8
7
  /**
9
- * The installation one test file owns, in a fixture root named by `prefix`. Call once per file, at module scope:
10
- * Bun runs many test files in one process, and each must own its copy.
8
+ * The installation one test file owns, kept as `name`. Call once per file, at module scope, with a name no other file
9
+ * uses: Bun runs many test files in one process, and each must own its copy.
11
10
  */
12
- export function testFileInstallation(prefix: string): TestFileInstallation {
13
- return ownedInstallation(repositoryRoot, () => nxFixtureRoot(prefix));
11
+ export function testFileInstallation(name: string): TestFileInstallation {
12
+ return ownedInstallation(repositoryRoot, name);
14
13
  }
@@ -3,8 +3,8 @@ import { findClockOracles, formatClockOracle, TEST_CLOCK_ORACLE } from '../test-
3
3
  import { diffScope, upstreamBase } from '../test-walk-policy.js';
4
4
 
5
5
  // TEST-CLOCK-ORACLE over the current directory's repository, judged on the cases changed since the upstream merge base
6
- // (`upstreamBase`, the base `smoo-nx-test-walks --base` prints for the lint target's runtime input). Every finding
7
- // prints `error[…]` and exits 1. Exit 2 when git cannot answer.
6
+ // (`upstreamBase`, resolved as a run precondition before Nx hashes anything). Every finding prints `error[…]` and
7
+ // exits 1. Exit 2 when git cannot answer.
8
8
  if (process.argv.length > 2) {
9
9
  process.stderr.write('usage: smoo-nx-test-clocks\n');
10
10
  process.exit(2);
@@ -9,12 +9,13 @@ import {
9
9
  } from '../test-walk-policy.js';
10
10
 
11
11
  // TEST-ONE-WALK over the current directory's repository, judged on the cases changed since the upstream merge base
12
- // (`upstreamBase`). `--base` prints only that base, for an Nx runtime input: the verdict depends on it and no file
13
- // hash names it. Refusing kinds print `error[…]` and exit 1; advisory kinds print `warning[…]` for the writer and
14
- // reviewer and leave the exit at 0. Exit 2 when git cannot answer.
12
+ // (`upstreamBase`). The base is repository state, not task data: the plugin resolves it as a run precondition
13
+ // before Nx hashes anything (assertUpstreamBasePrecondition), so it is never carried by a runtime input.
14
+ // Refusing kinds print `error[…]` and exit 1; advisory kinds print `warning[…]` for the writer and reviewer and
15
+ // leave the exit at 0. Exit 2 when git cannot answer.
15
16
  const args = process.argv.slice(2);
16
- if (args.length > 1 || (args.length === 1 && args[0] !== '--base')) {
17
- process.stderr.write('usage: smoo-nx-test-walks [--base]\n');
17
+ if (args.length > 0) {
18
+ process.stderr.write('usage: smoo-nx-test-walks\n');
18
19
  process.exit(2);
19
20
  }
20
21
  const root = process.cwd();
@@ -23,10 +24,6 @@ if (!base.ok) {
23
24
  process.stderr.write(`${TEST_ONE_WALK.id} cannot find the upstream base: ${base.reason}\n`);
24
25
  process.exit(2);
25
26
  }
26
- if (args[0] === '--base') {
27
- process.stdout.write(`${base.value}\n`);
28
- process.exit(0);
29
- }
30
27
  const scope = diffScope(root, base.value);
31
28
  if (!scope.ok) {
32
29
  process.stderr.write(`${TEST_ONE_WALK.id} cannot read the changes since ${base.value}: ${scope.reason}\n`);
@@ -0,0 +1,63 @@
1
+ import { expect, it } from 'bun:test';
2
+ import { realpath } from 'node:fs/promises';
3
+ import { posix } from 'node:path';
4
+ import { expandedFilesets, filesetSource } from './__tests__/cargo-closure-filesets.js';
5
+ import { cargoClosureInputs } from './cargo-closure-input.js';
6
+
7
+ const root = await realpath(import.meta.dir);
8
+
9
+ async function equivalent(directories: readonly string[], files: readonly string[]): Promise<void> {
10
+ const expected = expandedFilesets(directories, [
11
+ 'Cargo.toml',
12
+ ...directories.map((directory) => posix.join(directory, 'src/lib.rs')),
13
+ ...files,
14
+ ]);
15
+ expect(await cargoClosureInputs('', [filesetSource(root, directories, files)])).toEqual(expected);
16
+ }
17
+
18
+ it('preserves ordered inputs for roots, nested packages, prefix siblings, and loose targets', async () => {
19
+ for (const directories of [
20
+ [''],
21
+ ['', 'crates/a', 'crates/a/nested'],
22
+ ['crates/a', 'crates/a-', 'crates/a/nested', 'crates/a./child', 'crates/a0', 'crates/ab'],
23
+ ['crates/z/deep/member', 'crates/a/member', 'crates/z/other', 'crates/a/member'],
24
+ ['a/b', 'a/b/c', 'a/b/c/d', 'a/b-', 'a/b-/d', 'a/b0'],
25
+ ['é/a', 'é/a-b', 'é/a/b', 'a b/c', 'a b/c/d'],
26
+ ]) {
27
+ await equivalent(directories, [
28
+ 'Cargo.toml',
29
+ 'Cargo.lock',
30
+ 'shared.rs',
31
+ 'crates/Cargo.toml',
32
+ 'crates/a/src/lib.rs',
33
+ 'crates/a-/outside.rs',
34
+ 'crates/a0/src/lib.rs',
35
+ 'crates/a/loose.rs',
36
+ 'elsewhere/Cargo.toml',
37
+ ]);
38
+ }
39
+ });
40
+
41
+ it('matches the expansion oracle over generated directory trees and repeated source ownership', async () => {
42
+ let state = 0x9e3779b9;
43
+ const next = (bound: number): number => {
44
+ state ^= state << 13;
45
+ state ^= state >>> 17;
46
+ state ^= state << 5;
47
+ return (state >>> 0) % bound;
48
+ };
49
+ const names = ['a', 'a-', 'a.', 'a0', 'b', 'z'];
50
+ for (let trial = 0; trial < 256; trial += 1) {
51
+ const path = (): string => Array.from({ length: next(4) }, () => names[next(names.length)]).join('/');
52
+ const directories = Array.from({ length: 1 + next(32) }, path);
53
+ const files = Array.from({ length: 1 + next(8) }, () =>
54
+ posix.join(path(), next(2) === 0 ? 'Cargo.toml' : 'target.rs'),
55
+ );
56
+ await equivalent(directories, files);
57
+ }
58
+ });
59
+
60
+ it('reduces a large sibling closure to the same few ordered filesets', async () => {
61
+ const directories = Array.from({ length: 1024 }, (_, index) => `crates/package-${index}`);
62
+ await equivalent(directories, ['shared.rs']);
63
+ });
@@ -31,7 +31,7 @@ const repositoryRoot = join(import.meta.dir, '../../..');
31
31
  useFixtureCargoHome();
32
32
 
33
33
  /** The Nx installation this file's fixtures share; none may write into it. */
34
- const installation = testFileInstallation('cargo-closure-installation-');
34
+ const installation = testFileInstallation('cargo-closure-installation');
35
35
  afterAll(() => installation.retire());
36
36
 
37
37
  it('keys a custom target on exactly its Cargo closure, inside the workspace and out', async () => {
@@ -150,8 +150,8 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
150
150
  const closureArguments = `--closure ${shellWord(projectRoot)} ${shellWord(source.manifest)}`;
151
151
 
152
152
  const { root, cargo, indexed } = source;
153
- const packageDirectories: string[] = [];
154
- const files: string[] = [];
153
+ const packageDirectories = new Set<string>();
154
+ const files = new Set<string>();
155
155
  const compiledIn: CompiledInclusion[] = [];
156
156
  let compiledFiles: readonly string[] = [];
157
157
  let external = false;
@@ -175,11 +175,11 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
175
175
  if (declaring === null) throw new Error(`${compiled.source} was scanned outside ${directory}`);
176
176
  compiledIn.push({ declaring, file });
177
177
  }
178
- packageDirectories.push(directory);
178
+ packageDirectories.add(directory);
179
179
  for (const file of [...pkg.sources, pkg.governingManifest]) {
180
180
  const path = indexablePath(root, file);
181
181
  if (path === null) throw new Error(`${directory} compiles ${file}, which Nx's file index cannot hold`);
182
- files.push(path);
182
+ files.add(path);
183
183
  }
184
184
  }
185
185
  const compiledPaths = [...new Set(compiledIn.flatMap(({ declaring, file }) => [declaring, file]))];
@@ -192,23 +192,25 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
192
192
  throw new CargoClosureInputError(projectRoot, error);
193
193
  }
194
194
 
195
- // A package nested in another member's directory is already covered by it.
196
- const directories = [...new Set(packageDirectories)]
195
+ // Query the directory set by path ancestors, not by expanding all pairs.
196
+ // Removing nested members leaves an antichain with the same covered files.
197
+ const directories = [...packageDirectories]
197
198
  .sort()
198
- .filter((directory, index, sorted) => !sorted.slice(0, index).some((outer) => contains(outer, directory)));
199
+ .filter((directory) => !covered(packageDirectories, parent(directory)));
199
200
  const ancestors = new Set<string>();
200
201
  for (const directory of directories) {
201
- for (let ancestor = parent(directory); ancestor !== null; ancestor = parent(ancestor)) {
202
- const candidate = ancestor;
203
- if (!directories.some((outer) => contains(outer, candidate))) ancestors.add(candidate);
202
+ // An ancestor of an outermost member cannot be inside another member.
203
+ // Once this walk joins an earlier walk, all remaining ancestors are held.
204
+ for (let ancestor = parent(directory); ancestor !== null && !ancestors.has(ancestor); ancestor = parent(ancestor)) {
205
+ ancestors.add(ancestor);
204
206
  }
205
207
  }
206
208
  // Target sources outside every package directory, and governing manifests
207
209
  // that are not already one of the ancestors' manifests.
208
210
  const loose = new Set(
209
- files.filter(
211
+ [...files].filter(
210
212
  (file) =>
211
- !directories.some((directory) => contains(directory, file)) &&
213
+ !covered(packageDirectories, file) &&
212
214
  !(posix.basename(file) === 'Cargo.toml' && ancestors.has(parent(file) ?? '')),
213
215
  ),
214
216
  );
@@ -218,7 +220,9 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
218
220
  for (const directory of directories) {
219
221
  const key = parent(directory);
220
222
  if (key === null) continue;
221
- siblings.set(key, [...(siblings.get(key) ?? []), posix.basename(directory)]);
223
+ const names = siblings.get(key);
224
+ if (names === undefined) siblings.set(key, [posix.basename(directory)]);
225
+ else names.push(posix.basename(directory));
222
226
  }
223
227
  const packageGlobs = directories.includes('')
224
228
  ? ['']
@@ -275,6 +279,10 @@ function parent(path: string): string | null {
275
279
  return directory === '.' ? '' : directory;
276
280
  }
277
281
 
278
- function contains(directory: string, path: string): boolean {
279
- return directory === '' || path === directory || path.startsWith(`${directory}/`);
282
+ /** Membership in the union of package subtrees, bounded by path depth rather than package count. */
283
+ function covered(directories: ReadonlySet<string>, path: string | null): boolean {
284
+ for (let candidate = path; candidate !== null; candidate = parent(candidate)) {
285
+ if (directories.has(candidate)) return true;
286
+ }
287
+ return false;
280
288
  }
@@ -30,7 +30,7 @@ import { isRecord } from './type-guards.js';
30
30
  useFixtureCargoHome();
31
31
 
32
32
  /** The Nx installation this file's fixtures share; none may write into it. */
33
- const installation = testFileInstallation('cargo-closure-key-installation-');
33
+ const installation = testFileInstallation('cargo-closure-key-installation');
34
34
  afterAll(() => installation.retire());
35
35
 
36
36
  /** The ignored directory the producer owns, inside `leaf`, which every closure of `app` reaches. */
@@ -48,7 +48,7 @@ import { createNodesV2 } from './index.js';
48
48
  useFixtureCargoHome();
49
49
 
50
50
  /** The Nx installation this file's graph fixtures share; none may write into it. */
51
- const installation = testFileInstallation('cargo-resolve-installation-');
51
+ const installation = testFileInstallation('cargo-resolve-installation');
52
52
  afterAll(() => installation.retire());
53
53
 
54
54
  /*
@@ -220,13 +220,23 @@ describe('Linux cross-check policy', () => {
220
220
 
221
221
  /**
222
222
  * The managed pre-push hook, run for real in a fixture repository whose `tooling/direnv/repo-path` puts stand-ins
223
- * first on PATH: `uname` says Darwin, `smoo` passes the denylist, and `nx` prints what a cross run printed and exits
224
- * with its status. The hook must call a miss a miss and every other Nx failure what it is.
223
+ * first on PATH: `uname` says Darwin, `smoo` passes the denylist, `direnv` enters the checkout's environment, and `nx`
224
+ * prints what a cross run printed and exits with its status, refusing to run in any environment but the checkout's.
225
+ * The hook must ask in the checkout's environment and call a miss a miss and every other failure what it is.
225
226
  */
226
227
  describe('managed pre-push cross probe', () => {
227
228
  const hook = join(import.meta.dir, '../managed/raw/tooling/git-hooks/pre-push.sh');
229
+ /** The SDK the checkout's environment names; the caller's shell names another. */
230
+ const CHECKOUT_SDK = '/checkout/MacOSX.sdk';
228
231
 
229
- async function push(nxOutput: string, nxStatus: number): Promise<{ status: number | null; stderr: string }> {
232
+ interface Push {
233
+ readonly nxOutput: string;
234
+ readonly nxStatus: number;
235
+ /** Whether direnv is on the hook's PATH. */
236
+ readonly direnv?: boolean;
237
+ }
238
+
239
+ async function push({ nxOutput, nxStatus, direnv = true }: Push): Promise<{ status: number | null; stderr: string }> {
230
240
  const root = await mkdtemp(join(tmpdir(), 'pre-push-cross-'));
231
241
  try {
232
242
  const bin = join(root, 'bin');
@@ -238,14 +248,29 @@ describe('managed pre-push cross probe', () => {
238
248
  };
239
249
  await stub('uname', 'echo Darwin');
240
250
  await stub('smoo', 'exit 0');
241
- await stub('nx', `printf '%s\\n' "$NX_STUB_OUTPUT"; exit ${nxStatus}`);
242
- await writeFile(join(root, 'repo', 'tooling', 'direnv', 'repo-path'), `#!/bin/sh\necho "${bin}:$PATH"\n`);
251
+ // Nx keys on the environment it runs in: outside the checkout's, it is not the gate's run.
252
+ await stub(
253
+ 'nx',
254
+ `[ "$SDKROOT" = '${CHECKOUT_SDK}' ] || { echo "nx stub: keyed on the caller's SDKROOT $SDKROOT" >&2; exit 1; }\nprintf '%s\\n' "$NX_STUB_OUTPUT"; exit ${nxStatus}`,
255
+ );
256
+ if (direnv) {
257
+ // The checkout's .envrc: `direnv exec <checkout>` enters the environment that checkout declares.
258
+ await stub(
259
+ 'direnv',
260
+ `[ "$1" = exec ] && [ "$2" = "$PWD" ] || { echo "direnv stub: unexpected $*" >&2; exit 64; }\nexport SDKROOT='${CHECKOUT_SDK}'\nshift 2\nexec "$@"`,
261
+ );
262
+ }
263
+ // Without direnv, PATH holds the stand-ins alone: no host direnv is found, and no coreutils either, as on a
264
+ // Nix-built Linux host whose /usr/bin and /bin hold none. The refusal must still be said.
265
+ const path = direnv ? `${bin}:$PATH` : bin;
266
+ await writeFile(join(root, 'repo', 'tooling', 'direnv', 'repo-path'), `#!/bin/sh\necho "${path}"\n`);
243
267
  await chmod(join(root, 'repo', 'tooling', 'direnv', 'repo-path'), 0o755);
244
268
  await writeFile(join(root, 'repo', 'tooling', 'direnv', 'ttsc-env.sh'), '');
245
269
  expect(spawnSync('git', ['init', '-q', join(root, 'repo')]).status).toBe(0);
246
270
  const run = spawnSync('bash', [hook, 'origin'], {
247
271
  cwd: join(root, 'repo'),
248
- env: { ...process.env, NX_STUB_OUTPUT: nxOutput },
272
+ // The caller's shell carries another directory's SDK, as a shell inside a parent directory's devenv does.
273
+ env: { ...process.env, NX_STUB_OUTPUT: nxOutput, SDKROOT: '/nix/store/caller-apple-sdk/MacOSX.sdk' },
249
274
  input: '',
250
275
  encoding: 'utf8',
251
276
  });
@@ -255,21 +280,37 @@ describe('managed pre-push cross probe', () => {
255
280
  }
256
281
  }
257
282
 
258
- it('passes a hit', async () => {
259
- expect((await push('Successfully ran target cargo-lint-cross', 0)).status).toBe(0);
283
+ it("passes a hit, asked in the checkout's environment rather than the caller's", async () => {
284
+ const { status, stderr } = await push({ nxOutput: 'Successfully ran target cargo-lint-cross', nxStatus: 0 });
285
+ expect(stderr).not.toContain("keyed on the caller's SDKROOT");
286
+ expect(status).toBe(0);
260
287
  });
261
288
 
262
289
  it('names a miss when the cross target refused to compile', async () => {
263
- const { status, stderr } = await push(CARGO_CROSS_LINT_REFUSAL, 1);
290
+ const { status, stderr } = await push({ nxOutput: CARGO_CROSS_LINT_REFUSAL, nxStatus: 1 });
264
291
  expect(status).toBe(1);
265
292
  expect(stderr).toContain('NOT cached for this tree');
266
293
  expect(stderr).toContain('bun run check:linux');
267
294
  });
268
295
 
269
- it('names any other Nx failure as that, never as a cache miss', async () => {
270
- const { status, stderr } = await push('NX NX_WORKSPACE_ROOT_PATH names another workspace', 1);
296
+ it('names any other failure as that, never as a cache miss', async () => {
297
+ const { status, stderr } = await push({
298
+ nxOutput: 'NX NX_WORKSPACE_ROOT_PATH names another workspace',
299
+ nxStatus: 1,
300
+ });
301
+ expect(status).toBe(1);
302
+ expect(stderr).toContain('failed (exit 1) before it reached');
303
+ expect(stderr).not.toContain('NOT cached');
304
+ });
305
+
306
+ it("refuses, naming direnv, when it cannot enter the checkout's environment", async () => {
307
+ const { status, stderr } = await push({
308
+ nxOutput: 'Successfully ran target cargo-lint-cross',
309
+ nxStatus: 0,
310
+ direnv: false,
311
+ });
271
312
  expect(status).toBe(1);
272
- expect(stderr).toContain('Nx failed (exit 1) before the Linux cross-compile check reached');
313
+ expect(stderr).toContain('direnv is not on PATH');
273
314
  expect(stderr).not.toContain('NOT cached');
274
315
  });
275
316
  });
@@ -22,15 +22,20 @@ import { expandOutputs, WorkspaceContext } from 'nx/src/native';
22
22
  import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory';
23
23
  import type { readModulePackageJsonWithoutFallbacks } from 'nx/src/utils/package-json';
24
24
  import typia from 'typia';
25
+ import { pastTick } from './__tests__/past-tick.js';
25
26
  import {
26
27
  censusStart,
28
+ type GraphInstallationCensus,
27
29
  type GraphWitness,
28
30
  graphInstallationIdentity,
31
+ installationIdentityOf,
29
32
  movedNodes,
30
33
  type NodeWitness,
31
34
  nodeStamp,
32
35
  nodesUnchanged,
36
+ provenInstallationCensus,
33
37
  readGraphWitness,
38
+ recordedInstallationCensus,
34
39
  recordedStamp,
35
40
  reusableGraph,
36
41
  SAME_TICK_NS,
@@ -68,10 +73,11 @@ afterEach(async () => {
68
73
 
69
74
  function witness(): GraphWitness {
70
75
  return {
71
- version: 1,
76
+ version: 2,
72
77
  workspaceRoot: root,
73
78
  environment: 'env',
74
79
  installation: 'installed',
80
+ installationUnproven: [],
75
81
  snapshot: snapshot(),
76
82
  projectGraph: graph,
77
83
  };
@@ -151,7 +157,7 @@ describe('authoritative graph witness', () => {
151
157
  } finally {
152
158
  await chmod(path, 0o600);
153
159
  }
154
- await writeFile(path, JSON.stringify({ ...witness(), version: 2 }));
160
+ await writeFile(path, JSON.stringify({ ...witness(), version: 1 }));
155
161
  expect(readGraphWitness(path)).toBeNull();
156
162
  });
157
163
  });
@@ -339,6 +345,51 @@ describe('installed graph input identity', () => {
339
345
  original.files.find(({ path }) => path === output)?.sha256,
340
346
  );
341
347
  });
348
+
349
+ it('takes only a proven, unmoved file’s hash from a previous census and reads every other file again', async () => {
350
+ const { plugin, resolver } = await packages();
351
+ const output = join(plugin, 'dist', 'index.js');
352
+ const census = (previous?: GraphInstallationCensus) =>
353
+ typia.assert<GraphInstallationCensus>(
354
+ provenInstallationCensus(
355
+ root,
356
+ { plugins: ['@smoothbricks/nx-plugin'] },
357
+ resolver,
358
+ expandOutputs,
359
+ null,
360
+ undefined,
361
+ previous,
362
+ ),
363
+ );
364
+ const hashOf = ({ files }: GraphInstallationCensus) => files.find(({ path }) => path === output)?.sha256;
365
+ const bytes = (text: string) => createHash('sha256').update(text).digest('hex');
366
+ await pastTick();
367
+ const first = census();
368
+ expect(first.unproven).toEqual([]);
369
+ // A recorded hash that is not the file's: a census that takes it did not read the file.
370
+ const recorded = {
371
+ ...first,
372
+ files: first.files.map((file) => (file.path === output ? { ...file, sha256: 'recorded' } : file)),
373
+ };
374
+ expect(hashOf(census(recorded))).toBe('recorded');
375
+ // The witness keeps the census as its identity and its unproven files beside it.
376
+ expect(recordedInstallationCensus(installationIdentityOf(recorded, null), [])).toEqual(recorded);
377
+ expect(hashOf(census({ ...recorded, unproven: [output] }))).toBe(bytes('exports.value = 1;'));
378
+ // Only its ctime moved: a same-mode chmod, as Bun's bin linking makes.
379
+ chmodSync(output, lstatSync(output).mode & 0o7777);
380
+ expect(hashOf(census(recorded))).toBe(bytes('exports.value = 1;'));
381
+ // A same-size rewrite that keeps its mtime moves its ctime: read again.
382
+ const before = await stat(output);
383
+ await writeFile(output, 'exports.value = 2;');
384
+ await utimes(output, before.atime, before.mtime);
385
+ expect(hashOf(census(recorded))).toBe(bytes('exports.value = 2;'));
386
+ // Dated ahead of the census, as a change in its tick is: recorded unproven, and read again by the next census.
387
+ const ahead = new Date(Date.now() + 60_000);
388
+ await utimes(output, ahead, ahead);
389
+ const dated = census();
390
+ expect(dated.unproven).toEqual([output]);
391
+ expect(hashOf(census({ ...dated, files: recorded.files }))).toBe(bytes('exports.value = 2;'));
392
+ });
342
393
  it('proves a declared dist link by its physical output tree and rejects changed public bytes', async () => {
343
394
  const { plugin, resolver } = await packages();
344
395
  const published = join(root, 'published-build-volume');
@@ -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