@smoothbricks/nx-plugin 0.4.24 → 0.4.26

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 (58) hide show
  1. package/README.md +147 -37
  2. package/dist/bin/smoo-nx-exec.js +3 -14
  3. package/dist/cargo-closure-input.d.ts +13 -15
  4. package/dist/cargo-closure-input.d.ts.map +1 -1
  5. package/dist/cargo-closure-input.js +29 -37
  6. package/dist/cargo-source-hash.d.ts +12 -7
  7. package/dist/cargo-source-hash.d.ts.map +1 -1
  8. package/dist/cargo-source-hash.js +416 -32
  9. package/dist/cargo-workspace.d.ts +7 -7
  10. package/dist/cargo-workspace.js +8 -8
  11. package/dist/ensure-built.d.ts +6 -0
  12. package/dist/ensure-built.d.ts.map +1 -1
  13. package/dist/ensure-built.js +79 -6
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +69 -96
  16. package/dist/managed-assets.d.ts +6 -0
  17. package/dist/managed-assets.d.ts.map +1 -1
  18. package/dist/managed-files/files.d.ts.map +1 -1
  19. package/dist/managed-files/files.js +6 -0
  20. package/dist/manifest-hash.d.ts.map +1 -1
  21. package/dist/manifest-hash.js +6 -2
  22. package/dist/workspace-manifest.d.ts +0 -1
  23. package/dist/workspace-manifest.d.ts.map +1 -1
  24. package/managed/raw/envrc +15 -0
  25. package/managed/raw/git-format-staged.yml +1 -0
  26. package/managed/raw/tooling/direnv/developer-links.ts +43 -11
  27. package/managed/raw/tooling/direnv/devenv.smoo.nix +5 -0
  28. package/managed/raw/tooling/direnv/inherited-devenv.ts +665 -0
  29. package/managed/raw/tooling/direnv/setup-environment.ts +123 -7
  30. package/nextest.toml +3 -37
  31. package/package.json +40 -3
  32. package/src/__tests__/counted-cargo-process.ts +286 -0
  33. package/src/__tests__/counted-cargo.ts +711 -0
  34. package/src/__tests__/fixture-cargo-home.ts +36 -0
  35. package/src/__tests__/fixture-nx-env.ts +93 -3
  36. package/src/bin/smoo-nx-exec.ts +3 -14
  37. package/src/bounded-test-policy.test.ts +0 -1
  38. package/src/cargo-closure-input.test.ts +313 -15
  39. package/src/cargo-closure-input.ts +42 -50
  40. package/src/cargo-resolve-cache.test.ts +975 -0
  41. package/src/cargo-source-hash.test.ts +17 -3
  42. package/src/cargo-source-hash.ts +447 -37
  43. package/src/cargo-workspace.ts +8 -8
  44. package/src/ensure-built.test.ts +122 -20
  45. package/src/ensure-built.ts +82 -5
  46. package/src/index.test.ts +203 -107
  47. package/src/index.ts +86 -115
  48. package/src/managed-assets.ts +12 -0
  49. package/src/managed-files/files.ts +6 -0
  50. package/src/manifest-hash.ts +7 -2
  51. package/src/nextest-partitions.test.ts +150 -0
  52. package/src/nx-cache-bound.test.ts +114 -0
  53. package/src/nx-resident-graph-workers.test.ts +121 -0
  54. package/src/nx-store-resolution.test.ts +98 -0
  55. package/src/task-history-namespace.test.ts +183 -0
  56. package/src/workspace-manifest.ts +0 -1
  57. package/test-timings.json +35 -0
  58. package/src/__tests__/in-process-nx-index.ts +0 -9
@@ -0,0 +1,36 @@
1
+ import { afterAll, beforeAll } from 'bun:test';
2
+ import { mkdtemp, rm } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+
6
+ /**
7
+ * Give the calling test file its own empty `CARGO_HOME` from its first test to
8
+ * its last.
9
+ *
10
+ * Use it in files whose Cargo fixtures are offline and path-only. Their Cargo
11
+ * needs nothing from the developer's home, but it still takes that home's
12
+ * package-cache lock. Another Cargo command can hold that lock while it runs;
13
+ * fixture setup, the code under test and its Nx children would otherwise wait
14
+ * on unrelated work.
15
+ *
16
+ * The home goes into `process.env`, which is how a caller hands Cargo its home:
17
+ * node's `child_process` and the plugin's own Cargo children read it at spawn,
18
+ * and `fixtureNxEnv` copies it. `Bun.spawn` does not. Its default environment is
19
+ * the one the process started with, so a `Bun.spawn` that starts Cargo must
20
+ * pass `env` explicitly. A test that needs a particular home (its config.toml,
21
+ * or contention on it) still sets its own `CARGO_HOME` over this one.
22
+ */
23
+ export function useFixtureCargoHome(): void {
24
+ let inherited: string | undefined;
25
+ let home = '';
26
+ beforeAll(async () => {
27
+ inherited = process.env.CARGO_HOME;
28
+ home = await mkdtemp(join(tmpdir(), 'fixture-cargo-home-'));
29
+ process.env.CARGO_HOME = home;
30
+ });
31
+ afterAll(async () => {
32
+ if (inherited === undefined) delete process.env.CARGO_HOME;
33
+ else process.env.CARGO_HOME = inherited;
34
+ await rm(home, { recursive: true, force: true });
35
+ });
36
+ }
@@ -1,4 +1,10 @@
1
- import { join } from 'node:path';
1
+ import { spawn } from 'node:child_process';
2
+ import { existsSync, watch } from 'node:fs';
3
+ import { dirname, join } from 'node:path';
4
+ import { guardEvent } from './counted-cargo.js';
5
+
6
+ const repositoryRoot = join(import.meta.dir, '../../../..');
7
+ const DAEMON_STOP_TIMEOUT_MS = 10_000;
2
8
 
3
9
  /**
4
10
  * The environment of an `nx` a test runs against a fixture workspace.
@@ -7,7 +13,10 @@ import { join } from 'node:path';
7
13
  * `nx run nx-plugin:test`, the test process is itself an Nx task, and what the
8
14
  * outer Nx exported to it (NX_SKIP_NX_CACHE under `--skip-nx-cache`, the
9
15
  * NX_TASK_* identity, output capture) would reconfigure the fixture's Nx: a
10
- * skipped cache turns every second run into a miss.
16
+ * skipped cache turns every second run into a miss. NX_DAEMON goes with them,
17
+ * so an inherited `false` never reaches the fixture: its Nx runs under the
18
+ * daemon Nx would pick on its own, owned by the fixture root and released by
19
+ * `stopFixtureNxDaemon` before the root is deleted.
11
20
  *
12
21
  * FORCE_COLOR, which an outer Nx also exports, is kept set, next to the
13
22
  * NO_COLOR a shell may export. The fixture's runtime inputs inherit both, and
@@ -23,9 +32,90 @@ export function fixtureNxEnv(workspace: string): Record<string, string> {
23
32
  FORCE_COLOR: 'true',
24
33
  NO_COLOR: '1',
25
34
  NX_WORKSPACE_ROOT_PATH: workspace,
26
- NX_DAEMON: 'false',
27
35
  NX_ISOLATE_PLUGINS: 'false',
28
36
  NX_WORKSPACE_DATA_DIRECTORY: join(workspace, '.nx/workspace-data'),
29
37
  NX_CACHE_DIRECTORY: join(workspace, '.nx/cache'),
30
38
  };
31
39
  }
40
+
41
+ /**
42
+ * Stop the Nx daemon a fixture's own `nx` runs started, through Nx's supported
43
+ * `nx daemon --stop`, before the fixture root is deleted. The daemon is told
44
+ * the same root and data directory that started it (`fixtureNxEnv`), so it
45
+ * can only be this fixture's: no other workspace's daemon is reached, and
46
+ * nothing is reset. Nx records a started daemon in `d/server-process.json`
47
+ * under the workspace-data directory, so a root without that record never had
48
+ * one; the record goes when the daemon has shut down, and one that outlives
49
+ * the stop is reported, not buried by the delete that follows.
50
+ */
51
+ export async function stopFixtureNxDaemon(workspace: string): Promise<void> {
52
+ const record = join(workspace, '.nx/workspace-data/d/server-process.json');
53
+ if (!existsSync(record)) return;
54
+ // Subscribe before sending the stop request: an unlink is an event, not a delay to guess.
55
+ const subscription = watch(dirname(record));
56
+ const retirement = new Promise<Error | null>((resolve) => {
57
+ subscription.on('change', () => {
58
+ if (!existsSync(record)) resolve(null);
59
+ });
60
+ subscription.once('error', resolve);
61
+ if (!existsSync(record)) resolve(null);
62
+ });
63
+ let stdout = '';
64
+ let stderr = '';
65
+ try {
66
+ const child = spawn('bun', [join(repositoryRoot, 'node_modules/.bin/nx'), 'daemon', '--stop'], {
67
+ cwd: workspace,
68
+ env: fixtureNxEnv(workspace),
69
+ detached: process.platform !== 'win32',
70
+ stdio: ['ignore', 'pipe', 'pipe'],
71
+ });
72
+ child.stdout.setEncoding('utf8').on('data', (text: string) => {
73
+ stdout += text;
74
+ });
75
+ child.stderr.setEncoding('utf8').on('data', (text: string) => {
76
+ stderr += text;
77
+ });
78
+ let spawnFailure: Error | null = null;
79
+ child.once('error', (error) => {
80
+ spawnFailure = error;
81
+ });
82
+ const ended = new Promise<number | null>((resolve) => {
83
+ child.once('close', (code) => resolve(code));
84
+ });
85
+ const describe = () =>
86
+ `fixture ${workspace}; stop pid ${child.pid ?? 'not started'}, exit ${child.exitCode ?? 'pending'}, ` +
87
+ `signal ${child.signalCode ?? 'none'}; daemon record ${existsSync(record) ? 'present' : 'removed'}\n${stdout}${stderr}`;
88
+ let exitCode: number | null;
89
+ try {
90
+ exitCode = await guardEvent(ended, 'nx daemon --stop to exit', describe);
91
+ } catch (error) {
92
+ if (child.pid !== undefined) {
93
+ try {
94
+ process.kill(process.platform === 'win32' ? child.pid : -child.pid, 'SIGKILL');
95
+ } catch (killError) {
96
+ if (!(killError instanceof Error && 'code' in killError && killError.code === 'ESRCH')) {
97
+ throw new AggregateError(
98
+ [error, killError],
99
+ `could not stop the fixture's failed Nx command\n${describe()}`,
100
+ );
101
+ }
102
+ }
103
+ }
104
+ await ended;
105
+ throw error;
106
+ }
107
+ if (spawnFailure !== null || exitCode !== 0) {
108
+ throw new Error(`nx daemon --stop failed with exit code ${exitCode}\n${describe()}`, { cause: spawnFailure });
109
+ }
110
+ const error = await guardEvent(
111
+ retirement,
112
+ 'Nx daemon record to retire',
113
+ describe,
114
+ undefined,
115
+ DAEMON_STOP_TIMEOUT_MS,
116
+ );
117
+ if (error !== null) throw error;
118
+ } finally {
119
+ subscription.close();
120
+ }
121
+ }
@@ -1,9 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import { existsSync } from 'node:fs';
3
- import { dirname, isAbsolute, join, resolve } from 'node:path';
2
+ import { isAbsolute, resolve } from 'node:path';
4
3
  import { inspect } from 'node:util';
5
4
 
6
- import { describeMiss, ensureBuilt, parseTargetSelector } from '../ensure-built.js';
5
+ import { describeMiss, ensureBuilt, findNxWorkspaceRoot, parseTargetSelector } from '../ensure-built.js';
7
6
 
8
7
  const USAGE = 'usage: smoo-nx-exec <project:target[:configuration]> [--workspace-root <dir>] [-- <binary> [args...]]';
9
8
 
@@ -43,17 +42,7 @@ function tokenize(args: readonly string[]): string[] {
43
42
  }
44
43
 
45
44
  function findWorkspaceRoot(from: string): string {
46
- let directory = from;
47
- for (;;) {
48
- if (existsSync(join(directory, 'nx.json'))) {
49
- return directory;
50
- }
51
- const parent = dirname(directory);
52
- if (parent === directory) {
53
- return usageError(`no nx.json at or above ${from}; pass --workspace-root`);
54
- }
55
- directory = parent;
56
- }
45
+ return findNxWorkspaceRoot(from) ?? usageError(`no nx.json at or above ${from}; pass --workspace-root`);
57
46
  }
58
47
 
59
48
  const argv = process.argv.slice(2);
@@ -297,7 +297,6 @@ describe('bounded test target policy', () => {
297
297
  executor: 'nx:run-commands',
298
298
  options: {
299
299
  commands: ['bun test test/', 'cd go && go test ./...'],
300
- parallel: false,
301
300
  cwd: 'packages/schema',
302
301
  },
303
302
  };
@@ -1,23 +1,43 @@
1
1
  import { expect, it } from 'bun:test';
2
2
  import { execFileSync } from 'node:child_process';
3
- import { mkdir, mkdtemp, readFile, realpath, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises';
3
+ import { appendFile, mkdir, mkdtemp, readFile, realpath, rm, stat, symlink, utimes, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, join } from 'node:path';
6
6
 
7
+ import type { CreateNodesResultV2 } from 'nx/src/devkit-exports.js';
8
+ import { Watcher } from 'nx/src/native/index.js';
7
9
  import { resetWorkspaceContext } from 'nx/src/utils/workspace-context.js';
8
- import { fixtureNxEnv } from './__tests__/fixture-nx-env.js';
9
- import { CARGO_CLOSURE_INPUT } from './cargo-closure-input.js';
10
+ import {
11
+ aggregateOf,
12
+ type CountedCargo,
13
+ countedCargo,
14
+ expectGraphRefusal,
15
+ guardEvent,
16
+ rejectionOf,
17
+ waitForCargoJoins,
18
+ withEnvironment,
19
+ } from './__tests__/counted-cargo.js';
20
+ import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
21
+ import { fixtureNxEnv, stopFixtureNxDaemon } from './__tests__/fixture-nx-env.js';
22
+ import { CARGO_CLOSURE_INPUT, indexedCargoManifests } from './cargo-closure-input.js';
10
23
  import { createNodesV2 } from './index.js';
11
24
 
12
25
  const repositoryRoot = join(import.meta.dir, '../../..');
13
26
 
27
+ useFixtureCargoHome();
28
+
14
29
  /**
15
30
  * An Nx workspace that is also the Cargo workspace. `app` reaches `leaf`
16
31
  * through `bridge`, and `external` — outside the Nx workspace — through
17
32
  * `bridge` too. `unrelated` is a member nothing depends on. `app` records
18
33
  * every real compile in `executions.log`, so a cache hit is an unchanged log.
34
+ * `extraFiles` (paths relative to `root`) are written with the rest, last.
19
35
  */
20
- async function closureFixture(root: string, gitignore = 'node_modules/\ntarget/\ndist/\n.nx/\n') {
36
+ async function closureFixture(
37
+ root: string,
38
+ gitignore = 'node_modules/\ntarget/\ndist/\n.nx/\n',
39
+ extraFiles: Record<string, string> = {},
40
+ ) {
21
41
  const workspace = join(root, 'workspace');
22
42
  const executions = join(root, 'executions.log');
23
43
  const output = join(workspace, 'packages/app/dist/result.txt');
@@ -29,6 +49,9 @@ async function closureFixture(root: string, gitignore = 'node_modules/\ntarget/\
29
49
  plugins: ['@smoothbricks/nx-plugin'],
30
50
  namedInputs: { externalRustCrates: [] },
31
51
  }),
52
+ // Exercise the real linked-source path: Node's strip-only loader must
53
+ // import the plugin when the workspace selects development exports.
54
+ 'workspace/tsconfig.base.json': JSON.stringify({ compilerOptions: { customConditions: ['development'] } }),
32
55
  'workspace/Cargo.toml':
33
56
  '[workspace]\nmembers=["packages/app","crates/bridge","crates/leaf","crates/unrelated"]\nresolver="2"\n',
34
57
  'workspace/packages/app/package.json': JSON.stringify({
@@ -66,6 +89,7 @@ await appendFile(${JSON.stringify(executions)}, result);
66
89
  'workspace/crates/unrelated/src/lib.rs': 'pub fn unrelated() -> u8 { 3 }\n',
67
90
  'external/Cargo.toml': '[package]\nname="external"\nversion="0.1.0"\nedition="2021"\n[workspace]\n',
68
91
  'external/src/lib.rs': 'pub fn answer() -> u8 { 10 }\n',
92
+ ...extraFiles,
69
93
  };
70
94
  for (const [path, text] of Object.entries(files)) {
71
95
  await mkdir(dirname(join(root, path)), { recursive: true });
@@ -77,9 +101,9 @@ await appendFile(${JSON.stringify(executions)}, result);
77
101
  stdio: 'pipe',
78
102
  });
79
103
 
80
- /** `nx run app:compile` in a fresh process, as CI runs it; the built plugin is the one Nx loads. */
104
+ /** A fresh Node CLI and normal daemon, loading the linked plugin through development exports. */
81
105
  async function compile(): Promise<void> {
82
- const child = Bun.spawn(['bun', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:compile'], {
106
+ const child = Bun.spawn(['node', join(repositoryRoot, 'node_modules/.bin/nx'), 'run', 'app:compile'], {
83
107
  cwd: workspace,
84
108
  env: fixtureNxEnv(workspace),
85
109
  stdout: 'pipe',
@@ -110,6 +134,15 @@ await appendFile(${JSON.stringify(executions)}, result);
110
134
  return { workspace, compile, edit, log };
111
135
  }
112
136
 
137
+ /** Release the fixture's own Nx daemon through Nx, then delete the whole fixture root. */
138
+ async function removeFixture(root: string): Promise<void> {
139
+ try {
140
+ await stopFixtureNxDaemon(join(root, 'workspace'));
141
+ } finally {
142
+ await rm(root, { recursive: true, force: true });
143
+ }
144
+ }
145
+
113
146
  it('keys a custom target on exactly its Cargo closure, inside the workspace and out', async () => {
114
147
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-')));
115
148
  try {
@@ -134,21 +167,25 @@ it('keys a custom target on exactly its Cargo closure, inside the workspace and
134
167
  await compile();
135
168
  expect(await log()).toBe('11\n12\n22\n');
136
169
  } finally {
137
- await rm(root, { recursive: true, force: true });
170
+ await removeFixture(root);
138
171
  }
139
172
  }, 120_000);
140
173
 
141
- it('hashes a closure member in an ignored directory, which no fileset can see', async () => {
174
+ it('refuses a closure member in an ignored directory, which no fileset can see, instead of hashing the whole workspace', async () => {
142
175
  const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-ignored-')));
143
176
  try {
144
- const { compile, edit, log } = await closureFixture(root, 'node_modules/\ntarget/\ndist/\n.nx/\ncrates/leaf/\n');
145
- await compile();
146
- expect(await log()).toBe('11\n');
147
- await edit('workspace/crates/leaf/src/lib.rs', 'pub fn answer() -> u8 { 2 }\n');
148
- await compile();
149
- expect(await log()).toBe('11\n12\n');
177
+ const { workspace } = await closureFixture(root, 'node_modules/\ntarget/\ndist/\n.nx/\ncrates/leaf/\n');
178
+ resetWorkspaceContext();
179
+ const failure = aggregateOf(await rejectionOf(graphComputation(workspace)()));
180
+ const refusals = failure.errors.flatMap(([, error]) => (error.name === 'CargoClosureInputError' ? [error] : []));
181
+ expect(refusals.length).toBeGreaterThan(0);
182
+ for (const error of refusals) expect(error.message).toContain('crates/leaf');
183
+ // `app` reaches the ignored crate through `bridge`: its closure is what no fileset can name.
184
+ expect(refusals.map((error) => Reflect.get(error, 'projectRoot'))).toContain('packages/app');
185
+ // No runtime hash of the whole workspace stands in for it.
186
+ expect(JSON.stringify(failure.partialResults)).not.toContain('--include-workspace');
150
187
  } finally {
151
- await rm(root, { recursive: true, force: true });
188
+ await removeFixture(root);
152
189
  }
153
190
  }, 120_000);
154
191
 
@@ -182,3 +219,264 @@ it('leaves a closure inside the workspace to Nx: no process runs when its tasks
182
219
  await rm(root, { recursive: true, force: true });
183
220
  }
184
221
  }, 120_000);
222
+
223
+ /** `leaf` as the fixture writes it, and the dependency that draws `unrelated` into every closure reaching `leaf`. */
224
+ const LEAF_MANIFEST = '[package]\nname="leaf"\nversion="0.1.0"\nedition="2021"\n';
225
+ const LEAF_LINK = '[dependencies]\nunrelated={path="../unrelated"}\n';
226
+
227
+ /** The fileset the plugin names for the closure members `crates` (a name or a `{a,b}` set) under `crates/`. */
228
+ const crateFiles = (crates: string) =>
229
+ `{workspaceRoot}/crates/${crates}/**/{*.rs,Cargo.toml,.cargo/config,.cargo/config.toml}`;
230
+
231
+ /** One project-graph computation over the fixture: the plugin's `createNodes`, as Nx runs it for each recomputation. */
232
+ function graphComputation(workspace: string) {
233
+ const [, infer] = createNodesV2;
234
+ return async () =>
235
+ infer(['package.json', 'packages/app/package.json', 'crates/leaf/package.json'], undefined, {
236
+ workspaceRoot: workspace,
237
+ nxJsonConfiguration: { namedInputs: { externalRustCrates: [] } },
238
+ });
239
+ }
240
+
241
+ /** The computation's nodes in file order: the plugin pushes them as their projects finish. */
242
+ function byFile(graph: CreateNodesResultV2): CreateNodesResultV2 {
243
+ return [...graph].sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0));
244
+ }
245
+
246
+ /** The `cargoClosure` definition the computation gave the project at `projectRoot`. */
247
+ function closureOf(graph: CreateNodesResultV2, projectRoot: string) {
248
+ return (
249
+ graph.find(([file]) => dirname(file) === projectRoot)?.[1].projects?.[projectRoot]?.namedInputs?.[
250
+ CARGO_CLOSURE_INPUT
251
+ ] ?? []
252
+ );
253
+ }
254
+
255
+ /** Turn `leaf`'s dependency on `unrelated` on or off, and bring Cargo.lock in line with the manifests. */
256
+ function leafControls(workspace: string, edit: (path: string, text: string) => Promise<void>) {
257
+ return {
258
+ linkLeaf: (linked: boolean) => edit('workspace/crates/leaf/Cargo.toml', LEAF_MANIFEST + (linked ? LEAF_LINK : '')),
259
+ relock: () =>
260
+ execFileSync('cargo', ['generate-lockfile', '--offline', '--manifest-path', join(workspace, 'Cargo.toml')], {
261
+ stdio: 'pipe',
262
+ }),
263
+ };
264
+ }
265
+
266
+ it('shares one Cargo child among overlapping graph computations, and an unchanged workspace never asks Cargo again', async () => {
267
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-overlap-')));
268
+ let fixtureCargo: CountedCargo | undefined;
269
+ try {
270
+ const { workspace, edit } = await closureFixture(root);
271
+ const { linkLeaf, relock } = leafControls(workspace, edit);
272
+ const cargo = await countedCargo(root);
273
+ fixtureCargo = cargo;
274
+ const graph = graphComputation(workspace);
275
+ resetWorkspaceContext();
276
+ await withEnvironment(cargo.environment, async () => {
277
+ // Watcher events start computations while earlier ones still run: here sixteen at once, over a
278
+ // workspace nobody has resolved, each Cargo child holding its answer so that a child per
279
+ // computation would certainly overlap. One child answers all of them.
280
+ await cargo.beforeAnswer.close();
281
+ const joins = waitForCargoJoins(join(workspace, 'Cargo.toml'), 15);
282
+ let overlapping: CreateNodesResultV2[];
283
+ try {
284
+ const computations = Array.from({ length: 16 }, graph);
285
+ await cargo.waitForRuns((runs) => runs.read === 1, 'the shared Cargo answer');
286
+ await joins.joined;
287
+ await cargo.beforeAnswer.open();
288
+ overlapping = await Promise.all(computations);
289
+ } finally {
290
+ joins.dispose();
291
+ }
292
+ const overlapped = await cargo.runs();
293
+ expect(overlapped.spawned).toBe(1);
294
+ expect(overlapped.peak).toBe(1);
295
+
296
+ const baseline = byFile(await graph());
297
+ for (const computed of overlapping) expect(byFile(computed)).toEqual(baseline);
298
+ expect(closureOf(baseline, 'packages/app')).toContain(crateFiles('{bridge,leaf}'));
299
+ expect(closureOf(baseline, 'packages/app')).not.toContain(crateFiles('{bridge,leaf,unrelated}'));
300
+
301
+ // Nothing Cargo reads has changed since, so no computation asks it again, however often it runs.
302
+ expect(byFile(await graph())).toEqual(baseline);
303
+ expect((await cargo.runs()).spawned).toBe(1);
304
+
305
+ // An edit to what Cargo reads is seen by the next computation, and costs one child.
306
+ await linkLeaf(true);
307
+ relock();
308
+ const edited = byFile(await graph());
309
+ expect(closureOf(edited, 'packages/app')).toContain(crateFiles('{bridge,leaf,unrelated}'));
310
+ expect(byFile(await graph())).toEqual(edited);
311
+ const settled = await cargo.runs();
312
+ expect(settled.spawned).toBe(2);
313
+ expect(settled.peak).toBe(1);
314
+ });
315
+ } finally {
316
+ await fixtureCargo?.release();
317
+ await rm(root, { recursive: true, force: true });
318
+ }
319
+ }, 120_000);
320
+
321
+ 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 () => {
322
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-pending-')));
323
+ let fixtureCargo: CountedCargo | undefined;
324
+ try {
325
+ const { workspace, edit } = await closureFixture(root);
326
+ const { linkLeaf, relock } = leafControls(workspace, edit);
327
+ const cargo = await countedCargo(root);
328
+ fixtureCargo = cargo;
329
+ const graph = graphComputation(workspace);
330
+ const baselineLock = await readFile(join(workspace, 'Cargo.lock'), 'utf8');
331
+ resetWorkspaceContext();
332
+ await withEnvironment(cargo.environment, async () => {
333
+ const baseline = byFile(await graph());
334
+
335
+ // Cargo refuses a Cargo.lock the manifests have outgrown (`--locked`): the computation fails,
336
+ // whole, with Cargo's reason. No runtime hash stands in for the closure, and the refusal is
337
+ // not kept for the next computation.
338
+ await linkLeaf(true);
339
+ expectGraphRefusal(await rejectionOf(graph()), join(workspace, 'Cargo.toml'));
340
+ relock();
341
+ const repaired = byFile(await graph());
342
+ expect(closureOf(repaired, 'packages/app')).toContain(crateFiles('{bridge,leaf,unrelated}'));
343
+ expect(JSON.stringify(repaired)).not.toContain('--include-workspace');
344
+ expect((await cargo.runs()).spawned).toBe(3);
345
+
346
+ // A computation that asks while a child holds its answer waits for that child. One that asks
347
+ // after an edit made since the child read the workspace does not take that answer for the edited
348
+ // workspace's: a resolution follows the first, never beside it.
349
+ await cargo.beforeAnswer.close();
350
+ await linkLeaf(false);
351
+ await writeFile(join(workspace, 'Cargo.lock'), baselineLock);
352
+ const before = await cargo.runs();
353
+ const early = graph();
354
+ await cargo.waitForRuns((runs) => runs.read > before.read, 'the running Cargo child to read the workspace');
355
+ // Use content the successful cache has never seen; restoring the repaired
356
+ // content would correctly reuse that entry after discarding the held answer.
357
+ await edit(
358
+ 'workspace/crates/leaf/Cargo.toml',
359
+ (LEAF_MANIFEST + LEAF_LINK).replace('version="0.1.0"', 'version="0.2.0"'),
360
+ );
361
+ relock();
362
+ const joins = waitForCargoJoins(join(workspace, 'Cargo.toml'), 1);
363
+ const late = graph();
364
+ try {
365
+ await joins.joined;
366
+ } finally {
367
+ joins.dispose();
368
+ }
369
+ await cargo.beforeAnswer.open();
370
+ expect(byFile(await late)).toEqual(repaired);
371
+ // The early computation began before the edit: it saw the workspace on either side of it.
372
+ expect([baseline, repaired]).toContainEqual(byFile(await early));
373
+ const shared = await cargo.runs();
374
+ expect(shared.spawned - before.spawned).toBe(2);
375
+ expect(shared.peak).toBe(1);
376
+
377
+ // Nothing is pending once both have settled, and nothing has changed since.
378
+ expect(byFile(await graph())).toEqual(repaired);
379
+ expect((await cargo.runs()).spawned).toBe(shared.spawned);
380
+ });
381
+ } finally {
382
+ await fixtureCargo?.release();
383
+ await rm(root, { recursive: true, force: true });
384
+ }
385
+ }, 120_000);
386
+
387
+ it("indexes a source tree's Cargo manifests and none of the runtime state a cowshed keeps in it", async () => {
388
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-runtime-state-')));
389
+ try {
390
+ const { workspace } = await closureFixture(root, undefined, {
391
+ 'workspace/.nxignore': await readFile(join(repositoryRoot, '.nxignore'), 'utf8'),
392
+ 'workspace/.cowshed/cache/nx/workspace-data/d/daemon.log': 'daemon log\n',
393
+ 'workspace/.cowshed/job/1/stdout.log': 'job output\n',
394
+ 'workspace/.cowshed/cache/cargo/registry/Cargo.toml':
395
+ '[package]\nname="cached"\nversion="0.1.0"\nedition="2021"\n',
396
+ });
397
+ resetWorkspaceContext();
398
+ expect([...(await indexedCargoManifests(workspace))].sort()).toEqual([
399
+ 'Cargo.toml',
400
+ 'crates/bridge/Cargo.toml',
401
+ 'crates/leaf/Cargo.toml',
402
+ 'crates/unrelated/Cargo.toml',
403
+ 'packages/app/Cargo.toml',
404
+ ]);
405
+ } finally {
406
+ await rm(root, { recursive: true, force: true });
407
+ }
408
+ }, 120_000);
409
+
410
+ const SOURCE_FILE = 'src/lib.rs';
411
+
412
+ /**
413
+ * Every path the native watcher reports for a fresh workspace under `nxignore`
414
+ * while a cowshed writes its runtime state (the daemon's own log, job output,
415
+ * a supervisor's pid file) beside a source file that changes every round. This is
416
+ * the watcher Nx's daemon subscribes to the workspace with: it recomputes the
417
+ * project graph for each batch of events it reports, with no filter of its
418
+ * own. Each round advances on its unique source file's native callback, not
419
+ * on a guessed subscription or debounce delay.
420
+ */
421
+ async function watchedPaths(nxignore: string): Promise<string[]> {
422
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'cargo-closure-watch-')));
423
+ const reported = new Set<string>();
424
+ const failures: string[] = [];
425
+ let watcher: Watcher | undefined;
426
+ let pending: { path: string; resolve(): void; reject(error: Error): void } | undefined;
427
+ try {
428
+ execFileSync('git', ['init', '--quiet', root], { stdio: 'pipe' });
429
+ await writeFile(join(root, '.nxignore'), nxignore);
430
+ await mkdir(join(root, 'src'), { recursive: true });
431
+ await mkdir(join(root, '.cowshed/cache/nx/workspace-data/d'), { recursive: true });
432
+ await mkdir(join(root, '.cowshed/run'), { recursive: true });
433
+ watcher = new Watcher(root);
434
+ // Native watch() registers the filesystem subscription synchronously.
435
+ watcher.watch((error, events) => {
436
+ if (error !== null) {
437
+ failures.push(error);
438
+ pending?.reject(new Error(error));
439
+ }
440
+ for (const event of events) {
441
+ reported.add(event.path);
442
+ if (event.path === pending?.path) {
443
+ pending.resolve();
444
+ pending = undefined;
445
+ }
446
+ }
447
+ });
448
+ for (let round = 0; round < 5; round += 1) {
449
+ const source = round === 0 ? SOURCE_FILE : `src/round-${round}.rs`;
450
+ const observed = new Promise<void>((resolve, reject) => {
451
+ pending = { path: source, resolve, reject };
452
+ });
453
+ const writes = (async () => {
454
+ await appendFile(join(root, '.cowshed/cache/nx/workspace-data/d/daemon.log'), `round ${round}\n`);
455
+ await mkdir(join(root, '.cowshed/job', String(round)), { recursive: true });
456
+ await writeFile(join(root, '.cowshed/job', String(round), 'stdout.log'), `round ${round}\n`);
457
+ await writeFile(join(root, '.cowshed/run/supervisor.pid'), `${round}\n`);
458
+ await writeFile(join(root, source), `pub fn round() -> u8 { ${round} }\n`);
459
+ })();
460
+ await guardEvent(Promise.all([observed, writes]), `native watcher callback for ${source}`, () =>
461
+ JSON.stringify({ pending: pending?.path, reported: [...reported], failures }),
462
+ );
463
+ for (const event of watcher.forceFlushPending()) reported.add(event.path);
464
+ }
465
+ expect(failures).toEqual([]);
466
+ return [...reported].sort();
467
+ } finally {
468
+ await watcher?.stop();
469
+ await rm(root, { recursive: true, force: true });
470
+ }
471
+ }
472
+
473
+ it("keeps a cowshed's runtime state from waking the watcher that recomputes the project graph", async () => {
474
+ // With nothing ignored, the watcher does report runtime state: that is what
475
+ // makes the daemon's own log writes feed its next recomputation.
476
+ expect((await watchedPaths('')).filter((path) => path.startsWith('.cowshed'))).not.toEqual([]);
477
+
478
+ // With the repository's own .nxignore it reports the source file alone.
479
+ const paths = await watchedPaths(await readFile(join(repositoryRoot, '.nxignore'), 'utf8'));
480
+ expect(paths).toContain(SOURCE_FILE);
481
+ expect(paths.filter((path) => path.startsWith('.cowshed'))).toEqual([]);
482
+ }, 120_000);