@smoothbricks/nx-plugin 0.4.27 → 0.4.29

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 (124) hide show
  1. package/README.md +203 -83
  2. package/dist/bounded-test-policy.d.ts +10 -3
  3. package/dist/bounded-test-policy.d.ts.map +1 -1
  4. package/dist/bounded-test-policy.js +19 -25
  5. package/dist/cargo-closure-input.d.ts +12 -0
  6. package/dist/cargo-closure-input.d.ts.map +1 -1
  7. package/dist/cargo-closure-input.js +29 -7
  8. package/dist/cargo-source-hash.d.ts.map +1 -1
  9. package/dist/cargo-source-hash.js +42 -19
  10. package/dist/ensure-built-receipt.d.ts +34 -6
  11. package/dist/ensure-built-receipt.d.ts.map +1 -1
  12. package/dist/ensure-built-receipt.js +265 -159
  13. package/dist/ensure-built-witness.d.ts +27 -2
  14. package/dist/ensure-built-witness.d.ts.map +1 -1
  15. package/dist/ensure-built-witness.js +2176 -15
  16. package/dist/ensure-built.d.ts +99 -1
  17. package/dist/ensure-built.d.ts.map +1 -1
  18. package/dist/ensure-built.js +34 -53
  19. package/dist/executors/bounded-exec/cpu-pool.d.ts +16 -5
  20. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  21. package/dist/executors/bounded-exec/cpu-pool.js +43 -19
  22. package/dist/executors/bounded-exec/executor.d.ts +11 -2
  23. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/executor.js +139 -39
  25. package/dist/executors/bounded-exec/ram-temp.d.ts +17 -11
  26. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  27. package/dist/executors/bounded-exec/ram-temp.js +27 -13
  28. package/dist/generators/bun-test-tracing/generator.js +2 -2
  29. package/dist/graph-input-contract.d.ts +98 -0
  30. package/dist/graph-input-contract.d.ts.map +1 -0
  31. package/dist/graph-input-contract.js +624 -0
  32. package/dist/index.d.ts +12 -5
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +17 -3
  35. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  36. package/dist/managed-files/ci-workflow.js +5 -2
  37. package/dist/managed-files/publish-workflow.d.ts +11 -1
  38. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  39. package/dist/producer-receipts.d.ts +19 -0
  40. package/dist/producer-receipts.d.ts.map +1 -0
  41. package/dist/producer-receipts.js +921 -0
  42. package/dist/testing.d.ts +8 -0
  43. package/dist/testing.d.ts.map +1 -1
  44. package/dist/testing.js +58 -0
  45. package/managed/raw/patches/nx@23.2.1.patch +328 -50
  46. package/managed/raw/tooling/direnv/developer-links.ts +147 -21
  47. package/managed/raw/tooling/direnv/devenv.smoo.nix +7 -7
  48. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +14 -5
  49. package/managed/raw/tooling/direnv/linked-snapshots.ts +206 -40
  50. package/managed/raw/tooling/direnv/repo-path +15 -2
  51. package/managed/raw/tooling/direnv/setup-environment.ts +363 -125
  52. package/managed/templates/github/actions/cache-node-modules/action.yml +9 -0
  53. package/managed/templates/github/actions/setup-devenv/action.yml +63 -7
  54. package/nextest.toml +1 -1
  55. package/package.json +11 -6
  56. package/src/__tests__/fixture-nx-env.ts +7 -0
  57. package/src/__tests__/owned-installation.ts +174 -0
  58. package/src/bin/smoo-nx-bound-failures.test.ts +1 -1
  59. package/src/bounded-test-policy.test.ts +36 -22
  60. package/src/bounded-test-policy.ts +19 -27
  61. package/src/bun-bootstrap.test.ts +1 -1
  62. package/src/cargo-closure-input.test.ts +60 -11
  63. package/src/cargo-closure-input.ts +28 -6
  64. package/src/cargo-resolve-cache.test.ts +140 -25
  65. package/src/cargo-source-hash.test.ts +78 -3
  66. package/src/cargo-source-hash.ts +48 -30
  67. package/src/cross-check-policy.test.ts +2 -2
  68. package/src/ensure-built-receipt.test.ts +111 -16
  69. package/src/ensure-built-receipt.ts +251 -149
  70. package/src/ensure-built-witness.test.ts +197 -13
  71. package/src/ensure-built-witness.ts +187 -15
  72. package/src/ensure-built.test.ts +156 -102
  73. package/src/ensure-built.ts +114 -64
  74. package/src/executors/bounded-exec/cpu-pool.test.ts +116 -32
  75. package/src/executors/bounded-exec/cpu-pool.ts +55 -17
  76. package/src/executors/bounded-exec/cpu-tokens.test.ts +2 -2
  77. package/src/executors/bounded-exec/executor-host.test.ts +51 -41
  78. package/src/executors/bounded-exec/executor.test.ts +155 -35
  79. package/src/executors/bounded-exec/executor.ts +156 -45
  80. package/src/executors/bounded-exec/progress.test.ts +1 -1
  81. package/src/executors/bounded-exec/ram-temp-host.test.ts +46 -17
  82. package/src/executors/bounded-exec/ram-temp.test.ts +65 -3
  83. package/src/executors/bounded-exec/ram-temp.ts +32 -16
  84. package/src/executors/bounded-exec/verdict.test.ts +17 -12
  85. package/src/generators/bounded-test-targets/generator.test.ts +1 -3
  86. package/src/generators/bun-test-tracing/generator.test.ts +2 -2
  87. package/src/generators/bun-test-tracing/generator.ts +2 -2
  88. package/src/graph-input-contract.test.ts +371 -0
  89. package/src/graph-input-contract.ts +605 -0
  90. package/src/index.test.ts +3 -3
  91. package/src/index.ts +24 -3
  92. package/src/linked-snapshots.nx.test.ts +112 -30
  93. package/src/managed-files/ci-workflow.ts +5 -2
  94. package/src/managed-files/publish-workflow.ts +12 -1
  95. package/src/managed-files/repo-path.test.ts +44 -1
  96. package/src/managed-files/setup-devenv-free-disk.test.ts +95 -0
  97. package/src/nextest-code-bound.test.ts +23 -17
  98. package/src/nextest-partitions.test.ts +7 -1
  99. package/src/nx-cache-restore-mtime.test.ts +2 -2
  100. package/src/nx-daemon-claim.test.ts +5 -5
  101. package/src/nx-daemon-idle-cpu.test.ts +0 -1
  102. package/src/nx-daemon-idle-policy.test.ts +56 -0
  103. package/src/nx-daemon-ignore-files.test.ts +2 -2
  104. package/src/nx-daemon-validity.test.ts +31 -9
  105. package/src/nx-dot-input-policy.test.ts +5 -5
  106. package/src/nx-files-archive-racy.test.ts +1 -1
  107. package/src/nx-incomplete-run-summary.test.ts +1 -1
  108. package/src/nx-plugin-load-errors.test.ts +2 -2
  109. package/src/nx-project-graph-errors.test.ts +70 -0
  110. package/src/nx-skipped-task-exit.test.ts +2 -2
  111. package/src/nx-store-resolution.test.ts +7 -25
  112. package/src/nx-task-output-lease.test.ts +3 -3
  113. package/src/nx-use-local.test.ts +7 -5
  114. package/src/producer-receipts.test.ts +164 -0
  115. package/src/producer-receipts.ts +631 -0
  116. package/src/runtime-input.test.ts +1 -1
  117. package/src/serial-group-wiring.test.ts +4 -4
  118. package/src/task-results.test.ts +18 -16
  119. package/src/test-timings.test.ts +2 -2
  120. package/src/testing.test.ts +5 -5
  121. package/src/testing.ts +59 -0
  122. package/src/typecheck-test-policy.test.ts +1 -1
  123. package/src/typescript-resolution.test.ts +76 -0
  124. package/test-timings.json +5 -0
@@ -1,4 +1,4 @@
1
- import { expect, it } from 'bun:test';
1
+ import { afterAll, expect, it } from 'bun:test';
2
2
  import { execFile, execFileSync } from 'node:child_process';
3
3
  import { appendFile, mkdir, mkdtemp, readFile, realpath, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
@@ -19,6 +19,7 @@ import {
19
19
  withEnvironment,
20
20
  } from './__tests__/counted-cargo.js';
21
21
  import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
22
+ import { testFileInstallation } from './__tests__/owned-installation.js';
22
23
  import { runtimeInputValue } from './__tests__/runtime-input.js';
23
24
  import { CARGO_CLOSURE_INPUT, indexedCargoManifests } from './cargo-closure-input.js';
24
25
  import { hashCargoPathInputs } from './cargo-source-hash.js';
@@ -28,6 +29,10 @@ const repositoryRoot = join(import.meta.dir, '../../..');
28
29
 
29
30
  useFixtureCargoHome();
30
31
 
32
+ /** The Nx installation this file's fixtures share; none may write into it. */
33
+ const installation = testFileInstallation('cargo-closure-installation-');
34
+ afterAll(() => installation.retire());
35
+
31
36
  /**
32
37
  * An Nx workspace that is also the Cargo workspace. `app` reaches `leaf`
33
38
  * through `bridge`, and `external` — outside the Nx workspace — through
@@ -85,7 +90,7 @@ async function closureFixture(
85
90
  await writeFile(join(root, path), text);
86
91
  }
87
92
  execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
88
- await symlink(join(repositoryRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
93
+ await symlink(await installation.nodeModules(), join(workspace, 'node_modules'), 'dir');
89
94
  execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', join(workspace, 'Cargo.toml')], {
90
95
  stdio: 'pipe',
91
96
  });
@@ -150,7 +155,7 @@ it('keys a custom target on exactly its Cargo closure, inside the workspace and
150
155
  } finally {
151
156
  await rm(root, { recursive: true, force: true });
152
157
  }
153
- }, 120_000);
158
+ });
154
159
 
155
160
  it('keys literal compiled-in data inside and outside crate directories without keying comments or unrelated crates', async () => {
156
161
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-compiled-data-')));
@@ -208,7 +213,51 @@ it('keys literal compiled-in data inside and outside crate directories without k
208
213
  } finally {
209
214
  await rm(root, { recursive: true, force: true });
210
215
  }
211
- }, 120_000);
216
+ });
217
+
218
+ it('names no compiled-in build output, so a producer run leaves the closure it was hashed under unchanged', async () => {
219
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-compiled-output-')));
220
+ try {
221
+ const { workspace } = await closureFixture(
222
+ root,
223
+ 'node_modules/\ntarget/\ndist/\n.nx/\ncrates/leaf/src/generated/\n',
224
+ {
225
+ 'workspace/crates/leaf/src/lib.rs': [
226
+ 'pub fn answer() -> u8 { 1 }',
227
+ 'pub const TABLE: &[u8] = include_bytes!("generated/table.bin");',
228
+ 'pub const SKILL: &str = include_str!("../../../skills/SKILL.md");',
229
+ ].join('\n'),
230
+ 'workspace/skills/SKILL.md': 'skill-one',
231
+ },
232
+ );
233
+ // A producer's run: ignored outputs, content-named ones among them, and a generated source including those.
234
+ const generated = join(workspace, 'crates/leaf/src/generated');
235
+ const produce = async (content: string) => {
236
+ await rm(generated, { recursive: true, force: true });
237
+ await mkdir(generated, { recursive: true });
238
+ await writeFile(join(generated, 'table.bin'), content);
239
+ await writeFile(join(generated, `chunk-${content}.js`), content);
240
+ await writeFile(
241
+ join(generated, 'vocabulary.rs'),
242
+ `pub const CHUNK: &str = include_str!("chunk-${content}.js");\n`,
243
+ );
244
+ };
245
+ const closure = async () => {
246
+ resetWorkspaceContext();
247
+ return closureOf(byFile(await graphComputation(workspace)()), 'packages/app');
248
+ };
249
+
250
+ const unproduced = await closure();
251
+ expect(unproduced).toContain('{workspaceRoot}/skills/SKILL.md');
252
+ expect(JSON.stringify(unproduced)).not.toContain('generated/');
253
+ await produce('one');
254
+ expect(await closure()).toEqual(unproduced);
255
+ await produce('two');
256
+ expect(await closure()).toEqual(unproduced);
257
+ } finally {
258
+ await rm(root, { recursive: true, force: true });
259
+ }
260
+ });
212
261
 
213
262
  it('refuses a closure member in an ignored directory, which no fileset can see, instead of hashing the whole workspace', async () => {
214
263
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-ignored-')));
@@ -226,7 +275,7 @@ it('refuses a closure member in an ignored directory, which no fileset can see,
226
275
  } finally {
227
276
  await rm(root, { recursive: true, force: true });
228
277
  }
229
- }, 120_000);
278
+ });
230
279
 
231
280
  it('leaves a closure inside the workspace to Nx: no process runs when its tasks are hashed', async () => {
232
281
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-inferred-')));
@@ -257,7 +306,7 @@ it('leaves a closure inside the workspace to Nx: no process runs when its tasks
257
306
  } finally {
258
307
  await rm(root, { recursive: true, force: true });
259
308
  }
260
- }, 120_000);
309
+ });
261
310
 
262
311
  /** `leaf` as the fixture writes it, and the dependency that draws `unrelated` into every closure reaching `leaf`. */
263
312
  const LEAF_MANIFEST = '[package]\nname="leaf"\nversion="0.1.0"\nedition="2021"\n';
@@ -333,7 +382,7 @@ it('delegates counted Cargo through the caller launcher without re-entering its
333
382
  await fixtureCargo?.release();
334
383
  await rm(root, { recursive: true, force: true });
335
384
  }
336
- }, 30_000);
385
+ });
337
386
 
338
387
  it('shares one Cargo child among overlapping graph computations, and an unchanged workspace never asks Cargo again', async () => {
339
388
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-overlap-')));
@@ -388,7 +437,7 @@ it('shares one Cargo child among overlapping graph computations, and an unchange
388
437
  await fixtureCargo?.release();
389
438
  await rm(root, { recursive: true, force: true });
390
439
  }
391
- }, 120_000);
440
+ });
392
441
 
393
442
  it('refuses with a typed error what Cargo refuses, and follows an edit made while a child holds its answer with a resolution after it', async () => {
394
443
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-pending-')));
@@ -454,7 +503,7 @@ it('refuses with a typed error what Cargo refuses, and follows an edit made whil
454
503
  await fixtureCargo?.release();
455
504
  await rm(root, { recursive: true, force: true });
456
505
  }
457
- }, 120_000);
506
+ });
458
507
 
459
508
  it("indexes a source tree's Cargo manifests and none of the runtime state a cowshed keeps in it", async () => {
460
509
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-runtime-state-')));
@@ -477,7 +526,7 @@ it("indexes a source tree's Cargo manifests and none of the runtime state a cows
477
526
  } finally {
478
527
  await rm(root, { recursive: true, force: true });
479
528
  }
480
- }, 120_000);
529
+ });
481
530
 
482
531
  const SOURCE_FILE = 'src/lib.rs';
483
532
 
@@ -551,4 +600,4 @@ it("keeps a cowshed's runtime state from waking the watcher that recomputes the
551
600
  const paths = await watchedPaths(await readFile(join(repositoryRoot, '.nxignore'), 'utf8'));
552
601
  expect(paths).toContain(SOURCE_FILE);
553
602
  expect(paths.filter((path) => path.startsWith('.cowshed'))).toEqual([]);
554
- }, 120_000);
603
+ });
@@ -60,6 +60,8 @@ const GLOB_SYNTAX = /[*?[\]{}()!,]/;
60
60
  */
61
61
  export interface CargoClosureSource {
62
62
  readonly manifest: string;
63
+ /** The Nx workspace root as the computation names it, which Nx's file index is keyed by. */
64
+ readonly workspaceRoot: string;
63
65
  /** The canonical Nx workspace root. */
64
66
  readonly root: string;
65
67
  readonly cargo: CargoResolve;
@@ -90,7 +92,7 @@ export async function resolveCargoClosureSource(
90
92
  ): Promise<CargoClosureSource> {
91
93
  const [root, index] = await Promise.all([realpath(workspaceRoot), indexed]);
92
94
  const cargo = await readCargoResolve(join(root, manifest), root, index);
93
- return { manifest, root, cargo, indexed: index, compiledFilesCache };
95
+ return { manifest, workspaceRoot, root, cargo, indexed: index, compiledFilesCache };
94
96
  }
95
97
 
96
98
  /**
@@ -112,6 +114,16 @@ export async function resolveCargoClosureSource(
112
114
  * An in-workspace member missing from Nx's file index is a typed refusal,
113
115
  * never a whole-workspace runtime hash. Only outside-workspace members need
114
116
  * the external-only runtime input.
117
+ *
118
+ * A compiled-in file is named only when Nx's file index holds both it and the
119
+ * source that includes it. An ignored one is a build output: no fileset can
120
+ * hash its bytes, which reach a consumer through its producer's dependency
121
+ * outputs instead, so naming it would add nothing to any task's key except
122
+ * the names of whatever output last ran. That state is exactly what a project
123
+ * graph must not read (`globIndexedFiles`): a producer that regenerates
124
+ * content-named files would otherwise move every key of the graph it was
125
+ * hashed under, and the run that compiled a tree would leave its verdict
126
+ * under a key no later run of the same tree computes.
115
127
  */
116
128
  export async function cargoClosureInputs(
117
129
  projectRoot: string,
@@ -140,6 +152,7 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
140
152
  const { root, cargo, indexed } = source;
141
153
  const packageDirectories: string[] = [];
142
154
  const files: string[] = [];
155
+ const compiledIn: { readonly declaring: string; readonly file: string }[] = [];
143
156
  const compiledFiles = new Set<string>();
144
157
  let external = false;
145
158
  try {
@@ -154,10 +167,13 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
154
167
  if (!indexed.has(posix.join(directory, 'Cargo.toml'))) {
155
168
  throw new Error(`the Cargo package ${directory} is not in Nx's file index (an ignored directory)`);
156
169
  }
157
- for (const { file } of compiledInFiles(pkg.directory, source.compiledFilesCache)) {
158
- const path = indexablePath(root, file);
159
- if (path === null) throw new Error(`${directory} compiles ${file}, which Nx's file index cannot hold`);
160
- compiledFiles.add(path);
170
+ for (const compiled of compiledInFiles(pkg.directory, source.compiledFilesCache)) {
171
+ const file = indexablePath(root, compiled.file);
172
+ if (file === null) throw new Error(`${directory} compiles ${compiled.file}, which Nx's file index cannot hold`);
173
+ // The scan starts inside this package's indexable directory, so its sources can always be named.
174
+ const declaring = indexablePath(root, compiled.source);
175
+ if (declaring === null) throw new Error(`${compiled.source} was scanned outside ${directory}`);
176
+ compiledIn.push({ declaring, file });
161
177
  }
162
178
  packageDirectories.push(directory);
163
179
  for (const file of [...pkg.sources, pkg.governingManifest]) {
@@ -166,8 +182,14 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
166
182
  files.push(path);
167
183
  }
168
184
  }
169
- const unglobbable = [...packageDirectories, ...files, ...compiledFiles].find((path) => GLOB_SYNTAX.test(path));
185
+ const compiledPaths = [...new Set(compiledIn.flatMap(({ declaring, file }) => [declaring, file]))];
186
+ const unglobbable = [...packageDirectories, ...files, ...compiledPaths].find((path) => GLOB_SYNTAX.test(path));
170
187
  if (unglobbable !== undefined) throw new Error(`${unglobbable} contains glob syntax, so no fileset can name it`);
188
+ // Literal paths, so each matches exactly itself in the index.
189
+ const held = new Set(compiledPaths.length === 0 ? [] : await globIndexedFiles(source.workspaceRoot, compiledPaths));
190
+ for (const { declaring, file } of compiledIn) {
191
+ if (held.has(declaring) && held.has(file)) compiledFiles.add(file);
192
+ }
171
193
  } catch (error) {
172
194
  throw new CargoClosureInputError(projectRoot, error);
173
195
  }
@@ -1,11 +1,25 @@
1
- import { expect, it } from 'bun:test';
1
+ import { afterAll, expect, it } from 'bun:test';
2
2
  import { execFileSync } from 'node:child_process';
3
- import { appendFile, mkdir, mkdtemp, readFile, realpath, rm, symlink, utimes, writeFile } from 'node:fs/promises';
3
+ import {
4
+ appendFile,
5
+ mkdir,
6
+ mkdtemp,
7
+ readdir,
8
+ readFile,
9
+ realpath,
10
+ rename,
11
+ rm,
12
+ stat,
13
+ symlink,
14
+ utimes,
15
+ writeFile,
16
+ } from 'node:fs/promises';
4
17
  import { tmpdir } from 'node:os';
5
18
  import { delimiter, dirname, join } from 'node:path';
6
19
 
7
20
  import type { CreateNodesResultV2 } from 'nx/src/devkit-exports.js';
8
21
  import { FileLock } from 'nx/src/native/index.js';
22
+ import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory.js';
9
23
  import { resetWorkspaceContext } from 'nx/src/utils/workspace-context.js';
10
24
  import {
11
25
  type CannedCargo,
@@ -20,6 +34,7 @@ import {
20
34
  withEnvironment,
21
35
  } from './__tests__/counted-cargo.js';
22
36
  import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
37
+ import { testFileInstallation } from './__tests__/owned-installation.js';
23
38
  import { CARGO_CLOSURE_INPUT } from './cargo-closure-input.js';
24
39
  import {
25
40
  acquireCargoResolveLock,
@@ -31,6 +46,10 @@ import { createNodesV2 } from './index.js';
31
46
 
32
47
  useFixtureCargoHome();
33
48
 
49
+ /** The Nx installation this file's graph fixtures share; none may write into it. */
50
+ const installation = testFileInstallation('cargo-resolve-installation-');
51
+ afterAll(() => installation.retire());
52
+
34
53
  /*
35
54
  * What Cargo resolution costs, measured by the one number that matters: how many `cargo metadata`
36
55
  * children were actually started. A `cargo` ahead of the real one on PATH reports each child's life
@@ -49,7 +68,6 @@ useFixtureCargoHome();
49
68
 
50
69
  const sourceModule = join(import.meta.dir, 'cargo-source-hash.ts');
51
70
  const cliModule = join(import.meta.dir, 'bin', 'smoo-nx-cargo-hash.ts');
52
- const repositoryRoot = join(import.meta.dir, '../../..');
53
71
 
54
72
  const spawned = async (cargo: CountedCargo): Promise<number> => (await cargo.runs()).spawned;
55
73
 
@@ -81,7 +99,7 @@ function alive(pid: number): boolean {
81
99
  /** How a subprocess ended, with everything it printed. */
82
100
  interface Ended {
83
101
  readonly exitCode: number | null;
84
- readonly signalCode: NodeJS.Signals | null;
102
+ readonly signalCode: Bun.Subprocess['signalCode'];
85
103
  readonly stdout: string;
86
104
  readonly stderr: string;
87
105
  }
@@ -348,7 +366,7 @@ it('concurrent resolutions of one Cargo workspace share one cargo child, and an
348
366
  expect(await members()).toEqual(['after']);
349
367
  expect(await spawned(cargo)).toBe(2);
350
368
  });
351
- }, 20_000);
369
+ });
352
370
 
353
371
  it("a failed resolution rejects every sharer with a CargoMetadataError carrying Cargo's stderr, and is not kept for the next call", async () => {
354
372
  await withCannedWorkspace(async ({ cargo, answer, members, workspace, manifest }) => {
@@ -380,7 +398,7 @@ it("a failed resolution rejects every sharer with a CargoMetadataError carrying
380
398
  expect(await members()).toEqual(['recovered']);
381
399
  expect(await spawned(cargo)).toBe(2);
382
400
  });
383
- }, 20_000);
401
+ });
384
402
 
385
403
  it('a Cargo that cannot run, or whose answer is not metadata, is a CargoMetadataError', async () => {
386
404
  await withCannedWorkspace(async ({ cargo, workspace, manifest }) => {
@@ -395,7 +413,7 @@ it('a Cargo that cannot run, or whose answer is not metadata, is a CargoMetadata
395
413
  });
396
414
  expect(await spawned(cargo)).toBe(2);
397
415
  });
398
- }, 20_000);
416
+ });
399
417
 
400
418
  it('source edits, non-Rust edits and a rewrite of the same bytes never run Cargo', async () => {
401
419
  await withCannedWorkspace(async ({ root, cargo, answer, members, workspace }) => {
@@ -421,7 +439,7 @@ it('source edits, non-Rust edits and a rewrite of the same bytes never run Cargo
421
439
  }
422
440
  expect(await spawned(cargo)).toBe(1);
423
441
  });
424
- }, 20_000);
442
+ });
425
443
 
426
444
  interface KeyedFile {
427
445
  readonly name: string;
@@ -481,7 +499,10 @@ for (const file of KEYED_FILES) {
481
499
  expect(await resolvesAs('same bytes')).toEqual(['one']);
482
500
  expect(await spawned(cargo)).toBe(2);
483
501
 
502
+ // Identical size and mtime cannot stand in for the current bytes.
503
+ const before = await stat(join(root, file.path));
484
504
  await put(root, file.path, `${original}# two\n`);
505
+ await utimes(join(root, file.path), before.atime, before.mtime);
485
506
  expect(await resolvesAs('two')).toEqual(['two']);
486
507
  expect(await spawned(cargo)).toBe(3);
487
508
 
@@ -493,9 +514,54 @@ for (const file of KEYED_FILES) {
493
514
  expect(await resolvesAs('settled')).toEqual([file.removable ? 'removed' : 'two']);
494
515
  expect(await spawned(cargo)).toBe(file.removable ? 4 : 3);
495
516
  });
496
- }, 30_000);
517
+ });
497
518
  }
498
519
 
520
+ it('retargeting a configuration symlink with identical bytes still resolves the new canonical input', async () => {
521
+ await withCannedWorkspace(async ({ root, workspace, cargo, answer, members }) => {
522
+ const config = join(workspace, '.cargo/config.toml');
523
+ await put(root, 'config-a.toml', '[net]\noffline=true\n');
524
+ await put(root, 'config-b.toml', '[net]\noffline=true\n');
525
+ await mkdir(dirname(config), { recursive: true });
526
+ await symlink(join(root, 'config-a.toml'), config);
527
+ await answer('first target');
528
+ expect(await members()).toEqual(['first target']);
529
+ expect(await spawned(cargo)).toBe(1);
530
+
531
+ await rm(config);
532
+ await symlink(join(root, 'config-b.toml'), config);
533
+ await answer('second target');
534
+ expect(await members()).toEqual(['second target']);
535
+ expect(await spawned(cargo)).toBe(2);
536
+ await answer('unchanged');
537
+ expect(await members()).toEqual(['second target']);
538
+ expect(await spawned(cargo)).toBe(2);
539
+ });
540
+ });
541
+
542
+ it('a resolution input read failure is not a missing file or a reusable answer', async () => {
543
+ await withCannedWorkspace(async ({ workspace, manifest, cargo, answer }) => {
544
+ // Use the graph caller's inventory so this tests the resolution reader,
545
+ // independently of how the native workspace census treats a symlink loop.
546
+ const indexed = new Set(['Cargo.toml', 'crates/leaf/Cargo.toml']);
547
+ const members = async () => [...(await readCargoResolve(manifest, workspace, indexed)).members];
548
+ await answer('cold');
549
+ expect(await members()).toEqual(['cold']);
550
+ const config = join(workspace, '.cargo/config.toml');
551
+ await mkdir(dirname(config), { recursive: true });
552
+ await symlink('config.toml', config);
553
+ const failure = expectCargoMetadataError(await rejectionOf(members()), manifest);
554
+ expect(failure.message).toContain('ELOOP');
555
+ expect(failure.message).toContain(config);
556
+ expect(await spawned(cargo)).toBe(1);
557
+
558
+ await rm(config);
559
+ await answer('trap');
560
+ expect(await members()).toEqual(['cold']);
561
+ expect(await spawned(cargo)).toBe(1);
562
+ });
563
+ });
564
+
499
565
  it('resolves again exactly once when the cargo on PATH or the rustup selection changes', async () => {
500
566
  await withCannedWorkspace(async ({ root, cargo, answer, members, environment }) => {
501
567
  const resolvesAs = async (name: string, selection: Record<string, string> = {}): Promise<string[]> => {
@@ -527,7 +593,7 @@ it('resolves again exactly once when the cargo on PATH or the rustup selection c
527
593
  expect(await resolvesAs('same cargo', elsewhere)).toEqual(['another cargo']);
528
594
  expect(await spawned(cargo)).toBe(5);
529
595
  });
530
- }, 30_000);
596
+ });
531
597
 
532
598
  it('a discovered manifest outside the workspace, edited while the first resolution holds, is never answered from before the edit', async () => {
533
599
  await withCannedWorkspace(async ({ root, cargo, answer, members, workspace, manifest }) => {
@@ -550,7 +616,7 @@ it('a discovered manifest outside the workspace, edited while the first resoluti
550
616
  expect(await members()).toEqual(['after']);
551
617
  expect(await spawned(cargo)).toBe(2);
552
618
  });
553
- }, 20_000);
619
+ });
554
620
 
555
621
  it('an edit made while Cargo resolves is answered by a resolution that follows the first, never beside it', async () => {
556
622
  await withCannedWorkspace(async ({ cargo, answer, members, workspace, manifest }) => {
@@ -576,7 +642,7 @@ it('an edit made while Cargo resolves is answered by a resolution that follows t
576
642
  expect(await members()).toEqual(['after']);
577
643
  expect(await spawned(cargo)).toBe(2);
578
644
  });
579
- }, 20_000);
645
+ });
580
646
 
581
647
  it('an answer Cargo read after an edit is never kept for the workspace as it was before the edit', async () => {
582
648
  await withCannedWorkspace(async ({ cargo, answer, members, workspace, manifest }) => {
@@ -598,7 +664,7 @@ it('an answer Cargo read after an edit is never kept for the workspace as it was
598
664
  await answer('before');
599
665
  expect(await members()).toEqual(['before']);
600
666
  });
601
- }, 20_000);
667
+ });
602
668
 
603
669
  it('a process that has never resolved an unchanged workspace reads the answer an earlier one left, without Cargo', async () => {
604
670
  await withCannedWorkspace(async (fixture) => {
@@ -626,7 +692,7 @@ it('a process that has never resolved an unchanged workspace reads the answer an
626
692
  expect(await freshResolver(fixture, 'edited again').result).toEqual(edited);
627
693
  expect(await spawned(cargo)).toBe(2);
628
694
  });
629
- }, 60_000);
695
+ });
630
696
 
631
697
  it('fresh processes that ask at once for a workspace nobody has resolved share one cargo child', async () => {
632
698
  await withCannedWorkspace(async (fixture) => {
@@ -645,7 +711,7 @@ it('fresh processes that ask at once for a workspace nobody has resolved share o
645
711
  expect(results[0]).toMatchObject({ members: ['cold'] });
646
712
  expect(await spawned(cargo)).toBe(1);
647
713
  });
648
- }, 60_000);
714
+ });
649
715
 
650
716
  it('a worker that exits on SIGTERM while Cargo resolves leaves no cargo child behind', async () => {
651
717
  await withCannedWorkspace(async ({ root, workspace, manifest, cargo, answer, environment }) => {
@@ -673,7 +739,7 @@ it('a worker that exits on SIGTERM while Cargo resolves leaves no cargo child be
673
739
  // The child's end is the end of its connection to the fixture, killed before it answered.
674
740
  await cargo.waitForDisconnects([pid]);
675
741
  });
676
- }, 20_000);
742
+ });
677
743
 
678
744
  it("a worker that exits with callers waiting on two resolutions leaves none of Cargo's processes behind, and the next process resolves normally", async () => {
679
745
  await withCannedWorkspace(async (fixture) => {
@@ -730,7 +796,7 @@ it("a worker that exits with callers waiting on two resolutions leaves none of C
730
796
  expect(await freshResolver(fixture, 'again').result).toEqual(next);
731
797
  expect(await spawned(cargo)).toBe(3);
732
798
  });
733
- }, 60_000);
799
+ });
734
800
 
735
801
  // A standalone `smoo-nx-cargo-hash` has no handler of its own, so a signal would end it on the spot,
736
802
  // before any exit hook could take the Cargo it started along. The module forwards the signal to
@@ -754,7 +820,7 @@ for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP'] as const) {
754
820
  expect(ended.signalCode, proc.describe()).toBe(signal);
755
821
  await cargo.waitForDisconnects(running);
756
822
  });
757
- }, 30_000);
823
+ });
758
824
  }
759
825
 
760
826
  /* ---- The same, with the real Cargo, through the project graph and the hash command ---- */
@@ -808,7 +874,7 @@ async function graphFixture(root: string): Promise<string> {
808
874
  };
809
875
  for (const [path, text] of Object.entries(files)) await put(root, path, text);
810
876
  execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
811
- await symlink(join(repositoryRoot, 'node_modules'), join(workspace, 'node_modules'), 'dir');
877
+ await symlink(await installation.nodeModules(), join(workspace, 'node_modules'), 'dir');
812
878
  relock(workspace);
813
879
  return workspace;
814
880
  }
@@ -912,7 +978,7 @@ it('a project graph over an unchanged Cargo workspace runs Cargo once, however o
912
978
  expect(byFile(await graph())).toEqual(edited);
913
979
  expect(await spawned(cargo)).toBe(expected + 1);
914
980
  });
915
- }, 120_000);
981
+ });
916
982
 
917
983
  it('a Cargo workspace Cargo refuses fails the project graph with a CargoMetadataError, and no runtime hash stands in', async () => {
918
984
  await withGraphWorkspace(async ({ workspace, cargo, graph }) => {
@@ -937,7 +1003,56 @@ it('a Cargo workspace Cargo refuses fails the project graph with a CargoMetadata
937
1003
  expect(byFile(await graph())).toEqual(repaired);
938
1004
  expect(await spawned(cargo)).toBe(4);
939
1005
  });
940
- }, 120_000);
1006
+ });
1007
+
1008
+ for (const replaced of [false, true]) {
1009
+ it(`cache publication records a ${replaced ? 'rebound' : 'missing'} parent with the original filesystem cause`, async () => {
1010
+ await withGraphWorkspace(async ({ workspace, cargo }) => {
1011
+ const manifest = join(workspace, 'Cargo.toml');
1012
+ await cargo.beforeAnswer.close();
1013
+ const refused = rejectionOf(readCargoResolve(manifest, workspace));
1014
+ await cargo.waitForRuns((runs) => runs.read === 1, 'real locked/offline Cargo to read before cache publication');
1015
+ const namespace = join(workspaceDataDirectoryForWorkspace(workspace), 'cargo-resolve');
1016
+ const entries = await readdir(namespace);
1017
+ expect(entries).toHaveLength(1);
1018
+ const [entry] = entries;
1019
+ if (entry === undefined) throw new Error('the real resolver created no owned cache namespace');
1020
+ const directory = join(namespace, entry);
1021
+ const before = await stat(directory, { bigint: true });
1022
+ let atOpen = before;
1023
+ if (replaced) {
1024
+ await rename(directory, `${directory}.retired`);
1025
+ await mkdir(directory);
1026
+ atOpen = await stat(directory, { bigint: true });
1027
+ expect(atOpen.ino).not.toBe(before.ino);
1028
+ // Force publication to report the replacement rather than silently sign under a new namespace.
1029
+ await mkdir(join(directory, 'entry.cache'));
1030
+ } else {
1031
+ await rm(directory, { recursive: true });
1032
+ }
1033
+ await cargo.beforeAnswer.open();
1034
+ const failure = expectCargoMetadataError(await refused, manifest);
1035
+ expect(failure.message).toContain(directory);
1036
+ expect(failure.message).toContain('entry.cache.');
1037
+ expect(failure.message).toContain(
1038
+ `"parentBeforeLock":${JSON.stringify({ state: 'present', dev: before.dev.toString(), ino: before.ino.toString() })}`,
1039
+ );
1040
+ if (replaced) {
1041
+ const expected = JSON.stringify({ state: 'present', dev: atOpen.dev.toString(), ino: atOpen.ino.toString() });
1042
+ expect(failure.message).toContain(`"parentAtTmpOpen":${expected}`);
1043
+ expect(failure.message).toContain(`"parentAfterFailure":${expected}`);
1044
+ } else {
1045
+ expect(failure.message).toContain('"parentAtTmpOpen":{"state":"unavailable","code":"ENOENT"');
1046
+ expect(failure.message).toContain('"parentAfterFailure":{"state":"unavailable","code":"ENOENT"');
1047
+ }
1048
+ const publication = failure.cause;
1049
+ expect(publication).toBeInstanceOf(Error);
1050
+ if (!(publication instanceof Error)) throw new Error('cache publication lost its structured cause');
1051
+ expect(publication.cause).toHaveProperty('code', replaced ? 'EISDIR' : 'ENOENT');
1052
+ expect(await spawned(cargo)).toBe(1);
1053
+ });
1054
+ });
1055
+ }
941
1056
 
942
1057
  it('fresh smoo-nx-cargo-hash processes over an unchanged Cargo workspace run Cargo once between them', async () => {
943
1058
  await withGraphWorkspace(async ({ root, workspace, cargo }) => {
@@ -982,7 +1097,7 @@ it('fresh smoo-nx-cargo-hash processes over an unchanged Cargo workspace run Car
982
1097
  expect(await hash()).toBe(manifestEdited);
983
1098
  expect(await spawned(cargo)).toBe(2);
984
1099
  });
985
- }, 120_000);
1100
+ });
986
1101
 
987
1102
  it('a Cargo child that never answers is killed and named at the resolve bound', async () => {
988
1103
  await withCannedWorkspace(async ({ cargo, answer, workspace, manifest }) => {
@@ -995,7 +1110,7 @@ it('a Cargo child that never answers is killed and named at the resolve bound',
995
1110
  await cargo.beforeAnswer.open();
996
1111
  }
997
1112
  });
998
- }, 20_000);
1113
+ });
999
1114
 
1000
1115
  it('a held resolve lock times out with its name and elapsed wait and leaves no lock child', async () => {
1001
1116
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-resolve-lock-')));
@@ -1023,7 +1138,7 @@ it('a held resolve lock times out with its name and elapsed wait and leaves no l
1023
1138
  } finally {
1024
1139
  await rm(root, { recursive: true, force: true });
1025
1140
  }
1026
- }, 20_000);
1141
+ });
1027
1142
 
1028
1143
  it('a successful resolve lock lease holds the kernel lock until its child is released and reaped', async () => {
1029
1144
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-resolve-lease-')));
@@ -1041,4 +1156,4 @@ it('a successful resolve lock lease holds the kernel lock until its child is rel
1041
1156
  } finally {
1042
1157
  await rm(root, { recursive: true, force: true });
1043
1158
  }
1044
- }, 20_000);
1159
+ });
@@ -1,11 +1,11 @@
1
1
  import { expect, it } from 'bun:test';
2
2
  import { execFileSync } from 'node:child_process';
3
- import { mkdir, mkdtemp, readFile, realpath, rename, rm, symlink, writeFile } from 'node:fs/promises';
3
+ import { mkdir, mkdtemp, readFile, realpath, rename, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises';
4
4
  import { homedir, tmpdir } from 'node:os';
5
- import { dirname, join } from 'node:path';
5
+ import { dirname, join, relative } from 'node:path';
6
6
  import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
7
7
  import { runtimeInputValue } from './__tests__/runtime-input.js';
8
- import { hashCargoPathInputs } from './cargo-source-hash.js';
8
+ import { hashCargoPathInputs, readCargoResolve } from './cargo-source-hash.js';
9
9
 
10
10
  useFixtureCargoHome();
11
11
 
@@ -256,6 +256,81 @@ it('include-workspace hashes in-workspace transitive source edits without changi
256
256
  }
257
257
  });
258
258
 
259
+ it.each(['external', 'workspace/node_modules/external'])(
260
+ 'follows same-size same-mtime dependency edits in %s and hashes the newly reached source bytes',
261
+ async (externalDirectory) => {
262
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-hash-boundary-')));
263
+ const workspace = join(root, 'workspace');
264
+ const manifest = join(workspace, 'Cargo.toml');
265
+ const external = join(root, externalDirectory);
266
+ const externalManifest = join(external, 'Cargo.toml');
267
+ async function put(file: string, text: string): Promise<void> {
268
+ await mkdir(dirname(file), { recursive: true });
269
+ await writeFile(file, text);
270
+ }
271
+ const dependencyManifest = (name: string) =>
272
+ '[package]\nname="external"\nversion="0.1.0"\nedition="2021"\n[workspace]\n' +
273
+ `[dependencies]\nside={path=${JSON.stringify(relative(external, join(root, name)))}}\n`;
274
+ const sideManifest = '[package]\nname="side"\nversion="0.1.0"\nedition="2021"\n[workspace]\n';
275
+ const timestamp = new Date(1_700_000_000_000);
276
+ try {
277
+ // A nested installed workspace must not be auto-enrolled as a member of this one.
278
+ await put(
279
+ manifest,
280
+ '[package]\nname="app"\nversion="0.1.0"\nedition="2021"\n[workspace]\nexclude=["node_modules/external"]\n' +
281
+ `[dependencies]\nexternal={path=${JSON.stringify(relative(workspace, external))}}\n`,
282
+ );
283
+ await put(join(workspace, 'src/lib.rs'), 'pub fn app() {}\n');
284
+ await put(externalManifest, dependencyManifest('left'));
285
+ await put(join(external, 'src/lib.rs'), 'pub fn external() {}\n');
286
+ for (const side of ['left', 'rght']) {
287
+ await put(join(root, side, 'Cargo.toml'), sideManifest);
288
+ await put(join(root, side, 'src/lib.rs'), 'pub fn side() -> u8 { 1 }\n');
289
+ }
290
+ execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
291
+ await put(join(workspace, '.gitignore'), 'node_modules/\n.nx/\n');
292
+ execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', manifest], { stdio: 'pipe' });
293
+ await utimes(externalManifest, timestamp, timestamp);
294
+ const before = await stat(externalManifest);
295
+ const lock = await readFile(join(workspace, 'Cargo.lock'), 'utf8');
296
+ const initialResolve = await readCargoResolve(manifest, workspace);
297
+ expect(initialResolve.local.map((pkg) => pkg.manifest)).toContain(join(root, 'left/Cargo.toml'));
298
+ const initial = await hashCargoPathInputs(manifest, workspace);
299
+
300
+ // Both packages have the same name/version, so the locked graph stays
301
+ // valid. Only the dependency manifest's bytes identify its new location.
302
+ await writeFile(externalManifest, dependencyManifest('rght'));
303
+ await utimes(externalManifest, timestamp, timestamp);
304
+ const after = await stat(externalManifest);
305
+ expect(after.size).toBe(before.size);
306
+ expect(after.mtimeMs).toBe(before.mtimeMs);
307
+ const changedResolve = await readCargoResolve(manifest, workspace);
308
+ const manifests = changedResolve.local.map((pkg) => pkg.manifest);
309
+ expect(manifests).toContain(join(root, 'rght/Cargo.toml'));
310
+ expect(manifests).not.toContain(join(root, 'left/Cargo.toml'));
311
+ expect(await readFile(join(workspace, 'Cargo.lock'), 'utf8')).toBe(lock);
312
+ const changed = await hashCargoPathInputs(manifest, workspace);
313
+ expect(changed).not.toBe(initial);
314
+
315
+ await writeFile(join(root, 'left/src/lib.rs'), 'pub fn side() -> u8 { 2 }\n');
316
+ expect(await hashCargoPathInputs(manifest, workspace)).toBe(changed);
317
+ const source = join(root, 'rght/src/lib.rs');
318
+ const sourceBefore = await stat(source);
319
+ await writeFile(source, 'pub fn side() -> u8 { 2 }\n');
320
+ await utimes(source, sourceBefore.atime, sourceBefore.mtime);
321
+ const sourceChanged = await hashCargoPathInputs(manifest, workspace);
322
+ expect(sourceChanged).not.toBe(changed);
323
+
324
+ await rm(join(root, 'rght/Cargo.toml'));
325
+ await expect(hashCargoPathInputs(manifest, workspace)).rejects.toThrow('Locked offline Cargo resolution failed');
326
+ await put(join(root, 'rght/Cargo.toml'), sideManifest);
327
+ expect(await hashCargoPathInputs(manifest, workspace)).toBe(sourceChanged);
328
+ } finally {
329
+ await rm(root, { recursive: true, force: true });
330
+ }
331
+ },
332
+ );
333
+
259
334
  it('include-workspace CLI accepts an optional manifest and refuses unsupported arguments', async () => {
260
335
  const root = await mkdtemp(join(tmpdir(), 'cargo-hash-cli-'));
261
336
  try {