@smoothbricks/nx-plugin 0.4.29 → 0.4.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +83 -15
  2. package/dist/bin/smoo-nx-test-clocks.d.ts +3 -0
  3. package/dist/bin/smoo-nx-test-clocks.d.ts.map +1 -0
  4. package/dist/bin/smoo-nx-test-clocks.js +28 -0
  5. package/dist/bounded-test-policy.d.ts +1 -8
  6. package/dist/bounded-test-policy.d.ts.map +1 -1
  7. package/dist/bounded-test-policy.js +2 -8
  8. package/dist/cargo-closure-input.d.ts +11 -0
  9. package/dist/cargo-closure-input.d.ts.map +1 -1
  10. package/dist/cargo-closure-input.js +16 -6
  11. package/dist/ensure-built-receipt.d.ts +8 -2
  12. package/dist/ensure-built-receipt.d.ts.map +1 -1
  13. package/dist/ensure-built-receipt.js +45 -13
  14. package/dist/ensure-built-witness.d.ts +40 -4
  15. package/dist/ensure-built-witness.d.ts.map +1 -1
  16. package/dist/ensure-built-witness.js +68 -15
  17. package/dist/ensure-built.d.ts.map +1 -1
  18. package/dist/ensure-built.js +11 -3
  19. package/dist/executors/bounded-exec/cpu-pool.d.ts +7 -3
  20. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  21. package/dist/executors/bounded-exec/cpu-pool.js +9 -4
  22. package/dist/executors/bounded-exec/executor.d.ts +1 -1
  23. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  24. package/dist/executors/bounded-exec/executor.js +35 -12
  25. package/dist/executors/bounded-exec/no-space.d.ts +8 -0
  26. package/dist/executors/bounded-exec/no-space.d.ts.map +1 -0
  27. package/dist/executors/bounded-exec/no-space.js +31 -0
  28. package/dist/executors/bounded-exec/ram-temp.d.ts +3 -0
  29. package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -1
  30. package/dist/executors/bounded-exec/ram-temp.js +15 -4
  31. package/dist/executors/bounded-exec/verdict.d.ts +34 -4
  32. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  33. package/dist/executors/bounded-exec/verdict.js +54 -3
  34. package/dist/graph-input-contract.d.ts +4 -0
  35. package/dist/graph-input-contract.d.ts.map +1 -1
  36. package/dist/graph-input-contract.js +17 -4
  37. package/dist/owned-installation.d.ts +14 -0
  38. package/dist/owned-installation.d.ts.map +1 -0
  39. package/dist/owned-installation.js +324 -0
  40. package/dist/producer-receipts.d.ts.map +1 -1
  41. package/dist/producer-receipts.js +15 -10
  42. package/dist/test-backstop.d.ts +9 -0
  43. package/dist/test-backstop.d.ts.map +1 -0
  44. package/dist/test-backstop.js +10 -0
  45. package/dist/test-clock-policy.d.ts +30 -0
  46. package/dist/test-clock-policy.d.ts.map +1 -0
  47. package/dist/test-clock-policy.js +686 -0
  48. package/dist/testing.d.ts.map +1 -1
  49. package/dist/testing.js +8 -1
  50. package/managed/raw/tooling/direnv/developer-links.ts +1 -8
  51. package/managed/raw/tooling/direnv/setup-environment.ts +0 -1
  52. package/package.json +15 -4
  53. package/src/__tests__/cargo-closure-fixture.ts +113 -0
  54. package/src/__tests__/counted-cargo.ts +8 -25
  55. package/src/__tests__/directory-change.ts +27 -0
  56. package/src/__tests__/owned-installation.ts +4 -164
  57. package/src/__tests__/past-tick.ts +12 -0
  58. package/src/bin/smoo-nx-test-clocks.ts +29 -0
  59. package/src/bounded-test-policy.ts +2 -8
  60. package/src/cargo-closure-input.property.test.ts +50 -0
  61. package/src/cargo-closure-input.test.ts +15 -112
  62. package/src/cargo-closure-input.ts +23 -6
  63. package/src/cargo-closure-key.test.ts +239 -0
  64. package/src/cargo-resolve-cache.test.ts +29 -7
  65. package/src/ensure-built-receipt.ts +56 -13
  66. package/src/ensure-built-witness.test.ts +113 -4
  67. package/src/ensure-built-witness.ts +75 -16
  68. package/src/ensure-built.test.ts +74 -2
  69. package/src/ensure-built.ts +13 -2
  70. package/src/executors/bounded-exec/cpu-pool.test.ts +18 -0
  71. package/src/executors/bounded-exec/cpu-pool.ts +10 -4
  72. package/src/executors/bounded-exec/disk-lease.test.ts +47 -13
  73. package/src/executors/bounded-exec/executor-host.test.ts +23 -11
  74. package/src/executors/bounded-exec/executor.test.ts +189 -63
  75. package/src/executors/bounded-exec/executor.ts +36 -11
  76. package/src/executors/bounded-exec/hang-diagnostics.test.ts +14 -8
  77. package/src/executors/bounded-exec/no-space.ts +40 -0
  78. package/src/executors/bounded-exec/ram-temp-host.test.ts +9 -8
  79. package/src/executors/bounded-exec/ram-temp.test.ts +32 -18
  80. package/src/executors/bounded-exec/ram-temp.ts +17 -4
  81. package/src/executors/bounded-exec/verdict.test.ts +125 -25
  82. package/src/executors/bounded-exec/verdict.ts +78 -3
  83. package/src/graph-input-contract.test.ts +8 -0
  84. package/src/graph-input-contract.ts +26 -4
  85. package/src/nextest-extraction.test.ts +114 -4
  86. package/src/nx-daemon-claim.test.ts +5 -3
  87. package/src/nx-daemon-idle-policy.test.ts +22 -18
  88. package/src/nx-daemon-validity.test.ts +5 -13
  89. package/src/owned-installation.ts +174 -0
  90. package/src/producer-receipts.ts +15 -10
  91. package/src/test-backstop.ts +11 -0
  92. package/src/test-clock-policy.test.ts +338 -0
  93. package/src/test-clock-policy.ts +708 -0
  94. package/src/testing.ts +8 -1
  95. package/test-timings.json +3 -0
@@ -1,6 +1,6 @@
1
1
  import { afterAll, expect, it } from 'bun:test';
2
2
  import { execFile, execFileSync } from 'node:child_process';
3
- import { appendFile, mkdir, mkdtemp, readFile, realpath, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises';
3
+ import { appendFile, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, join } from 'node:path';
6
6
  import { promisify } from 'node:util';
@@ -8,6 +8,7 @@ import { promisify } from 'node:util';
8
8
  import type { CreateNodesResultV2 } from 'nx/src/devkit-exports.js';
9
9
  import { Watcher, WorkspaceContext } from 'nx/src/native/index.js';
10
10
  import { globWithWorkspaceContextSync, resetWorkspaceContext } from 'nx/src/utils/workspace-context.js';
11
+ import { byFile, closureFixture, closureOf, graphComputation } from './__tests__/cargo-closure-fixture.js';
11
12
  import {
12
13
  aggregateOf,
13
14
  type CountedCargo,
@@ -33,89 +34,10 @@ useFixtureCargoHome();
33
34
  const installation = testFileInstallation('cargo-closure-installation-');
34
35
  afterAll(() => installation.retire());
35
36
 
36
- /**
37
- * An Nx workspace that is also the Cargo workspace. `app` reaches `leaf`
38
- * through `bridge`, and `external` — outside the Nx workspace — through
39
- * `bridge` too. `unrelated` is a member nothing depends on. `app` declares a
40
- * custom cached target keyed on its closure.
41
- * `extraFiles` (paths relative to `root`) are written with the rest, last.
42
- */
43
- async function closureFixture(
44
- root: string,
45
- gitignore = 'node_modules/\ntarget/\ndist/\n.nx/\n',
46
- extraFiles: Record<string, string> = {},
47
- ) {
48
- const workspace = join(root, 'workspace');
49
- const files: Record<string, string> = {
50
- 'workspace/.gitignore': gitignore,
51
- 'workspace/package.json':
52
- '{"name":"cargo-closure-fixture","private":true,"workspaces":["packages/*","crates/*"]}\n',
53
- 'workspace/nx.json': JSON.stringify({
54
- plugins: ['@smoothbricks/nx-plugin'],
55
- namedInputs: { externalRustCrates: [] },
56
- }),
57
- 'workspace/Cargo.toml':
58
- '[workspace]\nmembers=["packages/app","crates/bridge","crates/leaf","crates/unrelated"]\nresolver="2"\n',
59
- 'workspace/packages/app/package.json': JSON.stringify({
60
- name: 'app',
61
- private: true,
62
- nx: {
63
- targets: {
64
- compile: {
65
- executor: 'nx:run-commands',
66
- cache: true,
67
- inputs: [CARGO_CLOSURE_INPUT],
68
- options: { command: 'cargo build --quiet --locked --offline --manifest-path packages/app/Cargo.toml' },
69
- },
70
- },
71
- },
72
- }),
73
- 'workspace/packages/app/Cargo.toml':
74
- '[package]\nname="app"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nbridge={path="../../crates/bridge"}\n',
75
- 'workspace/packages/app/src/main.rs': 'fn main() { println!("{}", bridge::answer()); }\n',
76
- 'workspace/crates/bridge/Cargo.toml':
77
- '[package]\nname="bridge"\nversion="0.1.0"\nedition="2021"\n[dependencies]\nleaf={path="../leaf"}\nexternal={path="../../../external"}\n',
78
- 'workspace/crates/bridge/src/lib.rs': 'pub fn answer() -> u8 { leaf::answer() + external::answer() }\n',
79
- 'workspace/crates/leaf/package.json': '{"name":"leaf","private":true}\n',
80
- 'workspace/crates/leaf/Cargo.toml': '[package]\nname="leaf"\nversion="0.1.0"\nedition="2021"\n',
81
- 'workspace/crates/leaf/src/lib.rs': 'pub fn answer() -> u8 { 1 }\n',
82
- 'workspace/crates/unrelated/Cargo.toml': '[package]\nname="unrelated"\nversion="0.1.0"\nedition="2021"\n',
83
- 'workspace/crates/unrelated/src/lib.rs': 'pub fn unrelated() -> u8 { 3 }\n',
84
- 'external/Cargo.toml': '[package]\nname="external"\nversion="0.1.0"\nedition="2021"\n[workspace]\n',
85
- 'external/src/lib.rs': 'pub fn answer() -> u8 { 10 }\n',
86
- ...extraFiles,
87
- };
88
- for (const [path, text] of Object.entries(files)) {
89
- await mkdir(dirname(join(root, path)), { recursive: true });
90
- await writeFile(join(root, path), text);
91
- }
92
- execFileSync('git', ['init', '--quiet', workspace], { stdio: 'pipe' });
93
- await symlink(await installation.nodeModules(), join(workspace, 'node_modules'), 'dir');
94
- execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', join(workspace, 'Cargo.toml')], {
95
- stdio: 'pipe',
96
- });
97
-
98
- /**
99
- * Rewrite a fixture file as an edit Nx can see. Nx keeps each file's hash
100
- * beside its mtime in whole seconds and reuses the hash until that second
101
- * changes, so a rewrite inside the second the file was last hashed in is
102
- * invisible to every fileset input. A fast host runs this whole fixture in
103
- * about a second; the edit is dated at least a second past the old mtime.
104
- */
105
- async function edit(path: string, text: string): Promise<void> {
106
- const file = join(root, path);
107
- const { mtimeMs } = await stat(file);
108
- await writeFile(file, text);
109
- const edited = new Date(Math.max(Date.now(), mtimeMs + 1000));
110
- await utimes(file, edited, edited);
111
- }
112
- return { workspace, edit };
113
- }
114
-
115
37
  it('keys a custom target on exactly its Cargo closure, inside the workspace and out', async () => {
116
38
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-')));
117
39
  try {
118
- const { workspace, edit } = await closureFixture(root);
40
+ const { workspace, edit } = await closureFixture(installation, root);
119
41
  resetWorkspaceContext();
120
42
  const closure = closureOf(byFile(await graphComputation(workspace)()), 'packages/app');
121
43
  const filesets = closure.flatMap((input) => (typeof input === 'string' ? [input] : []));
@@ -160,7 +82,7 @@ it('keys a custom target on exactly its Cargo closure, inside the workspace and
160
82
  it('keys literal compiled-in data inside and outside crate directories without keying comments or unrelated crates', async () => {
161
83
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-compiled-data-')));
162
84
  try {
163
- const { workspace, edit } = await closureFixture(root, undefined, {
85
+ const { workspace, edit } = await closureFixture(installation, root, undefined, {
164
86
  'workspace/packages/app/src/main.rs':
165
87
  'const LOCAL: &str = include_str!("data/local.txt");\nfn main() { println!("{}", bridge::answer()); }\n',
166
88
  'workspace/packages/app/src/data/local.txt': 'local-one',
@@ -219,6 +141,7 @@ it('names no compiled-in build output, so a producer run leaves the closure it w
219
141
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-compiled-output-')));
220
142
  try {
221
143
  const { workspace } = await closureFixture(
144
+ installation,
222
145
  root,
223
146
  'node_modules/\ntarget/\ndist/\n.nx/\ncrates/leaf/src/generated/\n',
224
147
  {
@@ -262,7 +185,11 @@ it('names no compiled-in build output, so a producer run leaves the closure it w
262
185
  it('refuses a closure member in an ignored directory, which no fileset can see, instead of hashing the whole workspace', async () => {
263
186
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-ignored-')));
264
187
  try {
265
- const { workspace } = await closureFixture(root, 'node_modules/\ntarget/\ndist/\n.nx/\ncrates/leaf/\n');
188
+ const { workspace } = await closureFixture(
189
+ installation,
190
+ root,
191
+ 'node_modules/\ntarget/\ndist/\n.nx/\ncrates/leaf/\n',
192
+ );
266
193
  resetWorkspaceContext();
267
194
  const failure = aggregateOf(await rejectionOf(graphComputation(workspace)()));
268
195
  const refusals = failure.errors.flatMap(([, error]) => (error.name === 'CargoClosureInputError' ? [error] : []));
@@ -280,7 +207,7 @@ it('refuses a closure member in an ignored directory, which no fileset can see,
280
207
  it('leaves a closure inside the workspace to Nx: no process runs when its tasks are hashed', async () => {
281
208
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-inferred-')));
282
209
  try {
283
- const { workspace } = await closureFixture(root);
210
+ const { workspace } = await closureFixture(installation, root);
284
211
  resetWorkspaceContext();
285
212
  const [, infer] = createNodesV2;
286
213
  const inferred = await infer(['package.json', 'packages/app/package.json', 'crates/leaf/package.json'], undefined, {
@@ -316,30 +243,6 @@ const LEAF_LINK = '[dependencies]\nunrelated={path="../unrelated"}\n';
316
243
  const crateFiles = (crates: string) =>
317
244
  `{workspaceRoot}/crates/${crates}/**/{*.rs,Cargo.toml,.cargo/config,.cargo/config.toml}`;
318
245
 
319
- /** One project-graph computation over the fixture: the plugin's `createNodes`, as Nx runs it for each recomputation. */
320
- function graphComputation(workspace: string) {
321
- const [, infer] = createNodesV2;
322
- return async () =>
323
- infer(['package.json', 'packages/app/package.json', 'crates/leaf/package.json'], undefined, {
324
- workspaceRoot: workspace,
325
- nxJsonConfiguration: { namedInputs: { externalRustCrates: [] } },
326
- });
327
- }
328
-
329
- /** The computation's nodes in file order: the plugin pushes them as their projects finish. */
330
- function byFile(graph: CreateNodesResultV2): CreateNodesResultV2 {
331
- return [...graph].sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0));
332
- }
333
-
334
- /** The `cargoClosure` definition the computation gave the project at `projectRoot`. */
335
- function closureOf(graph: CreateNodesResultV2, projectRoot: string) {
336
- return (
337
- graph.find(([file]) => dirname(file) === projectRoot)?.[1].projects?.[projectRoot]?.namedInputs?.[
338
- CARGO_CLOSURE_INPUT
339
- ] ?? []
340
- );
341
- }
342
-
343
246
  /** Turn `leaf`'s dependency on `unrelated` on or off, and bring Cargo.lock in line with the manifests. */
344
247
  function leafControls(workspace: string, edit: (path: string, text: string) => Promise<void>) {
345
248
  return {
@@ -355,7 +258,7 @@ it('delegates counted Cargo through the caller launcher without re-entering its
355
258
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-counted-delegate-')));
356
259
  let fixtureCargo: CountedCargo | undefined;
357
260
  try {
358
- const { workspace } = await closureFixture(root);
261
+ const { workspace } = await closureFixture(installation, root);
359
262
  const manifest = join(workspace, 'Cargo.toml');
360
263
  const args = ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', manifest];
361
264
  const expected = execFileSync('cargo', args, { encoding: 'utf8' });
@@ -388,7 +291,7 @@ it('shares one Cargo child among overlapping graph computations, and an unchange
388
291
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-overlap-')));
389
292
  let fixtureCargo: CountedCargo | undefined;
390
293
  try {
391
- const { workspace, edit } = await closureFixture(root);
294
+ const { workspace, edit } = await closureFixture(installation, root);
392
295
  const { linkLeaf, relock } = leafControls(workspace, edit);
393
296
  const cargo = await countedCargo(root);
394
297
  fixtureCargo = cargo;
@@ -443,7 +346,7 @@ it('refuses with a typed error what Cargo refuses, and follows an edit made whil
443
346
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-pending-')));
444
347
  let fixtureCargo: CountedCargo | undefined;
445
348
  try {
446
- const { workspace, edit } = await closureFixture(root);
349
+ const { workspace, edit } = await closureFixture(installation, root);
447
350
  const { linkLeaf, relock } = leafControls(workspace, edit);
448
351
  const cargo = await countedCargo(root);
449
352
  fixtureCargo = cargo;
@@ -508,7 +411,7 @@ it('refuses with a typed error what Cargo refuses, and follows an edit made whil
508
411
  it("indexes a source tree's Cargo manifests and none of the runtime state a cowshed keeps in it", async () => {
509
412
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-runtime-state-')));
510
413
  try {
511
- const { workspace } = await closureFixture(root, undefined, {
414
+ const { workspace } = await closureFixture(installation, root, undefined, {
512
415
  'workspace/.nxignore': await readFile(join(repositoryRoot, '.nxignore'), 'utf8'),
513
416
  'workspace/.cowshed/cache/nx/workspace-data/d/daemon.log': 'daemon log\n',
514
417
  'workspace/.cowshed/job/1/stdout.log': 'job output\n',
@@ -152,8 +152,8 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
152
152
  const { root, cargo, indexed } = source;
153
153
  const packageDirectories: string[] = [];
154
154
  const files: string[] = [];
155
- const compiledIn: { readonly declaring: string; readonly file: string }[] = [];
156
- const compiledFiles = new Set<string>();
155
+ const compiledIn: CompiledInclusion[] = [];
156
+ let compiledFiles: readonly string[] = [];
157
157
  let external = false;
158
158
  try {
159
159
  const members = await dependencyClosure(cargo, join(root, projectRoot));
@@ -187,9 +187,7 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
187
187
  if (unglobbable !== undefined) throw new Error(`${unglobbable} contains glob syntax, so no fileset can name it`);
188
188
  // Literal paths, so each matches exactly itself in the index.
189
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
- }
190
+ compiledFiles = indexedCompiledFiles(compiledIn, held);
193
191
  } catch (error) {
194
192
  throw new CargoClosureInputError(projectRoot, error);
195
193
  }
@@ -233,12 +231,31 @@ async function workspaceClosureInputs(projectRoot: string, source: CargoClosureS
233
231
  ...packageGlobs.map((glob) => anchored(glob, PACKAGE_FILES)),
234
232
  ...packageGlobs.map((glob) => `!${anchored(glob, SKIPPED_TREES)}`),
235
233
  ...[...ancestors].sort().map((ancestor) => anchored(ancestor, ANCESTOR_FILES)),
236
- ...[...compiledFiles].sort().map((file) => anchored(file)),
234
+ ...compiledFiles.map((file) => anchored(file)),
237
235
  ...[...loose].sort().map((file) => anchored(file)),
238
236
  ...(external ? [runtimeInput(`${CARGO_HASH_COMMAND} ${closureArguments}`)] : []),
239
237
  ];
240
238
  }
241
239
 
240
+ /** A compiled-in file and the source whose `include_str!`/`include_bytes!` names it, relative to the workspace root. */
241
+ export interface CompiledInclusion {
242
+ readonly declaring: string;
243
+ readonly file: string;
244
+ }
245
+
246
+ /**
247
+ * The compiled-in files a `cargoClosure` names, sorted: each one Nx's file index holds together with the source that
248
+ * includes it. `held` is the index's answer for exactly those paths. A file it does not hold is a build output or
249
+ * ignored, and naming it would key the graph on whatever last wrote it rather than on the tree.
250
+ */
251
+ export function indexedCompiledFiles(compiledIn: readonly CompiledInclusion[], held: ReadonlySet<string>): string[] {
252
+ const named = new Set<string>();
253
+ for (const { declaring, file } of compiledIn) {
254
+ if (held.has(declaring) && held.has(file)) named.add(file);
255
+ }
256
+ return [...named].sort();
257
+ }
258
+
242
259
  /**
243
260
  * `path` relative to the workspace root, `/`-separated, when it is somewhere
244
261
  * Nx's file index can hold: inside the workspace and not under `node_modules`.
@@ -0,0 +1,239 @@
1
+ import { afterAll, expect, it } from 'bun:test';
2
+ import { mkdir, mkdtemp, readdir, readFile, realpath, rm, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+
6
+ import fc from 'fast-check';
7
+ import { WorkspaceContext } from 'nx/src/native/index.js';
8
+ import { resetWorkspaceContext } from 'nx/src/utils/workspace-context.js';
9
+ import { byFile, closureFixture, closureOf, graphComputation } from './__tests__/cargo-closure-fixture.js';
10
+ import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
11
+ import { fixtureNxEnv, withNxFixture } from './__tests__/fixture-nx-env.js';
12
+ import { testFileInstallation } from './__tests__/owned-installation.js';
13
+ import { runtimeInputValue } from './__tests__/runtime-input.js';
14
+ import { CARGO_CLOSURE_INPUT } from './cargo-closure-input.js';
15
+ import { isRecord } from './type-guards.js';
16
+
17
+ /**
18
+ * The pre-push hook's key is the gate's key at the same tree (test-hardening #5).
19
+ *
20
+ * `check:linux` (the gate) writes its verdict under the key Nx computed from a project graph it
21
+ * built before its own tasks ran; the pre-push probe (the hook) recomputes the graph afterwards
22
+ * and reads under that key. A consumer's code generator, run by the gate, rewrites ignored,
23
+ * content-named chunk files and the generated `.rs` sources that `include_str!` them. When the
24
+ * Cargo closure named those files, the generator's own output moved the input list, so every
25
+ * first push of a gated tree was refused (1f5c3a58e). The properties below hold the key to a
26
+ * function of Nx's file index: no history of producer runs moves it, and the real Nx hook run
27
+ * reads exactly the hash the real gate run wrote.
28
+ */
29
+
30
+ useFixtureCargoHome();
31
+
32
+ /** The Nx installation this file's fixtures share; none may write into it. */
33
+ const installation = testFileInstallation('cargo-closure-key-installation-');
34
+ afterAll(() => installation.retire());
35
+
36
+ /** The ignored directory the producer owns, inside `leaf`, which every closure of `app` reaches. */
37
+ const GENERATED = 'crates/leaf/src/generated';
38
+ const GITIGNORE = `node_modules/\ntarget/\ndist/\n.nx/\n.runs\n${GENERATED}/\n`;
39
+
40
+ /** `leaf` compiles an indexed file in, and the generated sources by `include!`, as a consumer's tracked sources do. */
41
+ const LEAF_FILES = {
42
+ 'workspace/crates/leaf/src/lib.rs': [
43
+ 'pub fn answer() -> u8 { 1 }',
44
+ 'pub const SKILL: &str = include_str!("../../../skills/SKILL.md");',
45
+ 'include!("generated/vocabulary.rs");',
46
+ ].join('\n'),
47
+ 'workspace/skills/SKILL.md': 'skill-one',
48
+ };
49
+
50
+ /** One producer run: the chunk contents it emits, whether it clears the previous run's files, and where. */
51
+ interface ProducerRun {
52
+ readonly chunks: readonly string[];
53
+ readonly clean: boolean;
54
+ readonly nested: boolean;
55
+ }
56
+
57
+ const producerRun: fc.Arbitrary<ProducerRun> = fc.record({
58
+ chunks: fc.uniqueArray(fc.string({ minLength: 1, maxLength: 12 }), { minLength: 1, maxLength: 3 }),
59
+ clean: fc.boolean(),
60
+ nested: fc.boolean(),
61
+ });
62
+
63
+ /** A content-named chunk, as a bundler names it: the name moves whenever the content does. */
64
+ const chunkName = (content: string) => `chunk-${Bun.hash(content).toString(36)}.js`;
65
+
66
+ /** Write one run's ignored outputs: a table, its chunks, and the generated source including each. */
67
+ async function produce(workspace: string, { chunks, clean, nested }: ProducerRun): Promise<void> {
68
+ const generated = join(workspace, GENERATED);
69
+ if (clean) await rm(generated, { recursive: true, force: true });
70
+ const directory = nested ? join(generated, 'shared') : generated;
71
+ await mkdir(directory, { recursive: true });
72
+ await writeFile(join(generated, 'table.bin'), chunks.join('\0'));
73
+ const includes = chunks.map((content, index) => {
74
+ const name = chunkName(content);
75
+ return { name: `${nested ? 'shared/' : ''}${name}`, content, index };
76
+ });
77
+ for (const { name, content } of includes) await writeFile(join(generated, name), content);
78
+ await writeFile(
79
+ join(generated, 'vocabulary.rs'),
80
+ [
81
+ 'pub const TABLE: &[u8] = include_bytes!("table.bin");',
82
+ ...includes.map(({ name, index }) => `pub const CHUNK_${index}: &str = include_str!("${name}");`),
83
+ ].join('\n'),
84
+ );
85
+ }
86
+
87
+ let contexts = 0;
88
+
89
+ /**
90
+ * `app`'s Cargo-closure key as a task hashes it: the input list the project graph names, the
91
+ * bytes Nx's file index hashes for its filesets, and the stdout of its runtime entry. A fresh
92
+ * graph computation and a fresh file context each time, as each `nx` process starts with.
93
+ */
94
+ async function closureKey(workspace: string): Promise<string> {
95
+ resetWorkspaceContext();
96
+ const inputs = closureOf(byFile(await graphComputation(workspace)()), 'packages/app');
97
+ // Every fileset the closure names is workspace-anchored; `!` entries exclude.
98
+ const filesets = inputs.flatMap((input) => (typeof input === 'string' ? [input] : []));
99
+ for (const input of filesets) expect(input).toMatch(/^!?\{workspaceRoot\}\//);
100
+ const anchored = (input: string) => input.replace(/^!?\{workspaceRoot\}\//, '');
101
+ const positive = filesets.filter((input) => !input.startsWith('!')).map(anchored);
102
+ const negative = filesets.filter((input) => input.startsWith('!')).map(anchored);
103
+ contexts += 1;
104
+ const context = new WorkspaceContext(workspace, join(workspace, `.nx/closure-key-${contexts}`));
105
+ const runtime = await Promise.all(
106
+ inputs.flatMap((input) =>
107
+ typeof input === 'object' && 'runtime' in input ? [runtimeInputValue(input.runtime, workspace)] : [],
108
+ ),
109
+ );
110
+ return JSON.stringify({ inputs, files: context.hashFilesMatchingGlob(positive, negative), runtime });
111
+ }
112
+
113
+ it('no history of producer runs over ignored compiled-in files moves the Cargo closure key', async () => {
114
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-key-')));
115
+ try {
116
+ const { workspace, edit } = await closureFixture(installation, root, GITIGNORE, LEAF_FILES);
117
+ const gate = await closureKey(workspace);
118
+ // The key really reaches leaf's compiled-in data, so an unmoved key is not an empty one.
119
+ expect(gate).toContain('{workspaceRoot}/skills/SKILL.md');
120
+ expect(gate).not.toContain('generated/');
121
+
122
+ await fc.assert(
123
+ fc.asyncProperty(fc.array(producerRun, { minLength: 1, maxLength: 4 }), async (history) => {
124
+ await rm(join(workspace, GENERATED), { recursive: true, force: true });
125
+ for (const run of history) await produce(workspace, run);
126
+ expect(await closureKey(workspace)).toBe(gate);
127
+ }),
128
+ { numRuns: 15 },
129
+ );
130
+
131
+ // And an edit Nx's index sees still moves it: the property is not a key that never moves.
132
+ await edit('workspace/skills/SKILL.md', 'skill-two');
133
+ expect(await closureKey(workspace)).not.toBe(gate);
134
+ } finally {
135
+ await rm(root, { recursive: true, force: true });
136
+ }
137
+ });
138
+
139
+ const RECEIPT_ENV = 'COWSHED_NX_TASK_RECEIPT_ID';
140
+ const GATE_RECEIPT = '0123456789abcdef0123456789abcdef';
141
+ const HOOK_RECEIPT = 'fedcba9876543210fedcba9876543210';
142
+
143
+ /**
144
+ * The gate's task: a producer like a consumer's runtime-vocabulary generator. Each execution writes a chunk
145
+ * named for a new run number and the generated source including it, replacing the last run's,
146
+ * and counts itself in `.runs`, which no input names.
147
+ */
148
+ const PRODUCER = [
149
+ 'set -e',
150
+ `d=${GENERATED}`,
151
+ 'n=$(( $(cat .runs 2>/dev/null || echo 0) + 1 ))',
152
+ 'echo "$n" > .runs',
153
+ 'rm -rf "$d"',
154
+ 'mkdir -p "$d"',
155
+ `printf 'chunk %s' "$n" > "$d/chunk-$n.js"`,
156
+ `printf 'pub const CHUNK: &str = include_str!("chunk-%s.js");\\n' "$n" > "$d/vocabulary.rs"`,
157
+ ].join('\n');
158
+
159
+ /** The task hash and cache status the plugin's hint recorded for the one task a receipt's run ran. */
160
+ async function recorded(workspace: string, receipt: string): Promise<{ hash: string; status: string }> {
161
+ const namespace = join(workspace, '.nx/cache/.cowshed-task-results', receipt);
162
+ const files = await readdir(namespace);
163
+ expect(files).toHaveLength(1);
164
+ const [file] = files;
165
+ if (file === undefined) throw new Error(`no hint in ${namespace}`);
166
+ const hint: unknown = JSON.parse(await readFile(join(namespace, file), 'utf8'));
167
+ const tasks = isRecord(hint) && Array.isArray(hint.tasks) ? hint.tasks : [];
168
+ expect(tasks).toHaveLength(1);
169
+ const [entry] = tasks;
170
+ const task = isRecord(entry) && isRecord(entry.task) ? entry.task : undefined;
171
+ expect(task?.id).toBe('app:gate');
172
+ const hash = task?.hash;
173
+ const status = isRecord(entry) ? entry.status : undefined;
174
+ if (typeof hash !== 'string' || typeof status !== 'string') throw new Error(`malformed hint ${namespace}/${file}`);
175
+ return { hash, status };
176
+ }
177
+
178
+ it('the hook run reads the key the gate run wrote, after the gate rewrote compiled-in outputs', async () => {
179
+ await withNxFixture(
180
+ 'cargo-closure-key',
181
+ async ({ root, workspace, socketDirectory }) => {
182
+ await closureFixture(installation, root, GITIGNORE, {
183
+ ...LEAF_FILES,
184
+ 'workspace/produce.sh': `${PRODUCER}\n`,
185
+ 'workspace/packages/app/package.json': JSON.stringify({
186
+ name: 'app',
187
+ private: true,
188
+ nx: {
189
+ targets: {
190
+ gate: {
191
+ executor: 'nx:run-commands',
192
+ cache: true,
193
+ inputs: [CARGO_CLOSURE_INPUT],
194
+ options: { command: 'sh produce.sh', cwd: '{workspaceRoot}' },
195
+ },
196
+ },
197
+ },
198
+ }),
199
+ });
200
+ const { [RECEIPT_ENV]: _inherited, ...env } = fixtureNxEnv(workspace, socketDirectory);
201
+ const nx = async (receipt: string) => {
202
+ const argv = [
203
+ process.execPath,
204
+ join(workspace, 'node_modules/.bin/nx'),
205
+ 'run',
206
+ 'app:gate',
207
+ '--outputStyle=static',
208
+ ];
209
+ const child = Bun.spawn(argv, {
210
+ cwd: workspace,
211
+ env: { ...env, [RECEIPT_ENV]: receipt },
212
+ stdout: 'pipe',
213
+ stderr: 'pipe',
214
+ });
215
+ const [exitCode, stdout, stderr] = await Promise.all([
216
+ child.exited,
217
+ new Response(child.stdout).text(),
218
+ new Response(child.stderr).text(),
219
+ ]);
220
+ return { exitCode, log: `${stdout}${stderr}` };
221
+ };
222
+
223
+ const gate = await nx(GATE_RECEIPT);
224
+ expect(gate.exitCode, gate.log).toBe(0);
225
+ expect(await readFile(join(workspace, '.runs'), 'utf8')).toBe('1\n');
226
+ // The gate's own execution left a compiled-in file its graph never saw.
227
+ expect(await readdir(join(workspace, GENERATED))).toEqual(['chunk-1.js', 'vocabulary.rs']);
228
+
229
+ const hook = await nx(HOOK_RECEIPT);
230
+ expect(hook.exitCode, hook.log).toBe(0);
231
+ const [gateTask, hookTask] = [await recorded(workspace, GATE_RECEIPT), await recorded(workspace, HOOK_RECEIPT)];
232
+ expect(gateTask.status).toBe('success');
233
+ // The hook computed the gate's hash and read its entry (`-kept-existing`: the task declares no outputs).
234
+ expect(hookTask).toEqual({ hash: gateTask.hash, status: 'local-cache-kept-existing' });
235
+ expect(await readFile(join(workspace, '.runs'), 'utf8')).toBe('1\n');
236
+ },
237
+ 'workspace',
238
+ );
239
+ });
@@ -1,4 +1,4 @@
1
- import { afterAll, expect, it } from 'bun:test';
1
+ import { afterAll, expect, it, jest } from 'bun:test';
2
2
  import { execFileSync } from 'node:child_process';
3
3
  import {
4
4
  appendFile,
@@ -35,6 +35,7 @@ import {
35
35
  } from './__tests__/counted-cargo.js';
36
36
  import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
37
37
  import { testFileInstallation } from './__tests__/owned-installation.js';
38
+ import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from './bounded-test-policy.js';
38
39
  import { CARGO_CLOSURE_INPUT } from './cargo-closure-input.js';
39
40
  import {
40
41
  acquireCargoResolveLock,
@@ -1104,8 +1105,18 @@ it('a Cargo child that never answers is killed and named at the resolve bound',
1104
1105
  await answer('before');
1105
1106
  await cargo.beforeAnswer.close();
1106
1107
  try {
1107
- const refused = await rejectionOf(readCargoResolve(manifest, workspace, undefined, 100));
1108
- expect(String(refused)).toContain('did not finish within 100ms');
1108
+ // The resolve bound is a timer in this process, started only once the lock is held and Cargo is spawned. The
1109
+ // clock stays stopped until the canned Cargo reports that it read the workspace and is held at the closed
1110
+ // answer latch; only then is the bound advanced, so it expires on a child that can never answer.
1111
+ jest.useFakeTimers();
1112
+ try {
1113
+ const refusal = rejectionOf(readCargoResolve(manifest, workspace, undefined, 100));
1114
+ await cargo.waitForRuns((runs) => runs.read >= 1, 'the canned Cargo to read the workspace and be held');
1115
+ jest.advanceTimersByTime(100);
1116
+ expect(String(await refusal)).toContain('did not finish within 100ms');
1117
+ } finally {
1118
+ jest.useRealTimers();
1119
+ }
1109
1120
  } finally {
1110
1121
  await cargo.beforeAnswer.open();
1111
1122
  }
@@ -1119,15 +1130,24 @@ it('a held resolve lock times out with its name and elapsed wait and leaves no l
1119
1130
  const holder = new FileLock(lockFile);
1120
1131
  holder.lock();
1121
1132
  try {
1122
- const started = performance.now();
1123
- const refused = await rejectionOf(acquireCargoResolveLock(lockFile, 100));
1133
+ // The lock's bound is a timer in this process, and the waiter cannot take the lock while the holder keeps it.
1134
+ // The clock stays stopped while the waiter queues; advancing it by the bound is what ends the wait, and the
1135
+ // refusal must then name the lock, the wait and the reaped waiter while the holder still holds the lock.
1136
+ jest.useFakeTimers();
1137
+ let refused: unknown;
1138
+ try {
1139
+ const attempt = rejectionOf(acquireCargoResolveLock(lockFile, 100));
1140
+ jest.advanceTimersByTime(100);
1141
+ refused = await attempt;
1142
+ } finally {
1143
+ jest.useRealTimers();
1144
+ }
1124
1145
  if (!(refused instanceof CargoResolveLockError)) throw new Error(`unexpected lock refusal: ${refused}`);
1125
1146
  expect(refused.name).toBe('CargoResolveLockError');
1126
1147
  expect(refused.message).toContain(lockFile);
1127
1148
  expect(refused.message).toContain(`waited ${refused.waitedMs}ms`);
1128
1149
  expect(refused.message).toContain('bound 100ms');
1129
1150
  expect(refused.waitedMs).toBeGreaterThanOrEqual(100);
1130
- expect(performance.now() - started).toBeLessThan(1_100);
1131
1151
  if (refused.waiterPid === undefined) throw new Error('the lock refusal named no spawned waiter');
1132
1152
  expect(refused.message).toContain(`reaped lock waiter pid ${refused.waiterPid}`);
1133
1153
  expect(alive(refused.waiterPid)).toBe(false);
@@ -1144,7 +1164,9 @@ it('a successful resolve lock lease holds the kernel lock until its child is rel
1144
1164
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-resolve-lease-')));
1145
1165
  try {
1146
1166
  const lockFile = join(root, 'resolve.lock');
1147
- const lease = await acquireCargoResolveLock(lockFile, 5_000);
1167
+ // The lock is granted by the child's `locked` line and released by the child's exit, so I/O decides this case; the
1168
+ // bound is the leg backstop, which an uncontended lock never reaches.
1169
+ const lease = await acquireCargoResolveLock(lockFile, BOUNDED_TEST_IDLE_TIMEOUT_MS);
1148
1170
  try {
1149
1171
  expect(alive(lease.pid)).toBe(true);
1150
1172
  expect(new FileLock(lockFile).check()).toBe(true);