@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
@@ -3,9 +3,12 @@ import { execFileSync } from 'node:child_process';
3
3
  import { mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
4
4
  import { tmpdir } from 'node:os';
5
5
  import { dirname, join } from 'node:path';
6
- import { fixtureNxEnv } from './__tests__/fixture-nx-env.js';
6
+ import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
7
+ import { fixtureNxEnv, stopFixtureNxDaemon } from './__tests__/fixture-nx-env.js';
7
8
  import { hashCargoPathInputs } from './cargo-source-hash.js';
8
9
 
10
+ useFixtureCargoHome();
11
+
9
12
  it.each(['app', '.'])('invalidates transitive and inherited Cargo inputs with Nx rooted at %s', async (nxDirectory) => {
10
13
  const root = await mkdtemp(join(tmpdir(), 'cargo-hash-'));
11
14
  const app = join(root, 'app');
@@ -153,7 +156,14 @@ it('include-workspace CLI accepts an optional manifest and refuses unsupported a
153
156
  const { workspace, leafSource } = await workspaceCargoFixture(root);
154
157
  const bin = join(import.meta.dir, '../dist/bin/smoo-nx-cargo-hash.js');
155
158
  const run = async (args: string[]) => {
156
- const child = Bun.spawn(['bun', bin, ...args], { cwd: workspace, stdout: 'pipe', stderr: 'pipe' });
159
+ // Bun.spawn's default environment is the one this process started with,
160
+ // not process.env, so pass it: the file's own CARGO_HOME must reach the CLI.
161
+ const child = Bun.spawn(['bun', bin, ...args], {
162
+ cwd: workspace,
163
+ env: process.env,
164
+ stdout: 'pipe',
165
+ stderr: 'pipe',
166
+ });
157
167
  const [exitCode, stdout, stderr] = await Promise.all([
158
168
  child.exited,
159
169
  new Response(child.stdout).text(),
@@ -374,7 +384,11 @@ await appendFile(${JSON.stringify(executions)}, result);
374
384
  expect(await readFile(output, 'utf8')).toBe('12\n');
375
385
  expect(await readFile(executions, 'utf8')).toBe('11\n12\n');
376
386
  } finally {
377
- await rm(root, { recursive: true, force: true });
387
+ try {
388
+ await stopFixtureNxDaemon(join(root, 'workspace'));
389
+ } finally {
390
+ await rm(root, { recursive: true, force: true });
391
+ }
378
392
  }
379
393
  },
380
394
  120_000,
@@ -1,8 +1,29 @@
1
- import { execFile, execFileSync, spawnSync } from 'node:child_process';
2
- import { createHash } from 'node:crypto';
3
- import { realpathSync, statSync } from 'node:fs';
4
- import { lstat, readdir, readFile, readlink, realpath } from 'node:fs/promises';
5
- import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
1
+ import { type ChildProcess, execFileSync, spawn, spawnSync } from 'node:child_process';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
+ import { channel } from 'node:diagnostics_channel';
4
+ import { constants, realpathSync, statSync } from 'node:fs';
5
+ import {
6
+ access,
7
+ lstat,
8
+ mkdir,
9
+ readdir,
10
+ readFile,
11
+ readlink,
12
+ realpath,
13
+ rename,
14
+ rm,
15
+ stat,
16
+ writeFile,
17
+ } from 'node:fs/promises';
18
+ import { homedir } from 'node:os';
19
+ import { delimiter, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
20
+ import native from 'nx/src/native/index.js';
21
+ import { workspaceDataDirectoryForWorkspace } from 'nx/src/utils/cache-directory.js';
22
+ import { CARGO_TOOLCHAIN_PIN_INPUTS } from './cargo-toolchain-policy.js';
23
+
24
+ const { FileLock, IS_WASM, WorkspaceContext } = native;
25
+
26
+ const cargoResolveEvents = channel('@smoothbricks/nx-plugin:cargo-resolve');
6
27
 
7
28
  /** The fields of `cargo metadata --format-version 1` this module reads. */
8
29
  interface CargoMetadata {
@@ -11,6 +32,8 @@ interface CargoMetadata {
11
32
  source: string | null;
12
33
  manifest_path: string;
13
34
  targets: { src_path: string }[];
35
+ /** Added by the cache after Cargo identifies each local package's governing workspace. */
36
+ governing_manifest_path?: string;
14
37
  }[];
15
38
  resolve: { nodes: { id: string; deps: { pkg: string }[] }[] } | null;
16
39
  workspace_members: string[];
@@ -43,6 +66,9 @@ function parseCargoMetadata(json: string): CargoMetadata {
43
66
  targets: list(member(pkg, 'targets'), 'package targets').map((target) => ({
44
67
  src_path: text(member(target, 'src_path'), 'target src_path'),
45
68
  })),
69
+ ...(member(pkg, 'governing_manifest_path') === undefined
70
+ ? {}
71
+ : { governing_manifest_path: text(member(pkg, 'governing_manifest_path'), 'governing manifest') }),
46
72
  })),
47
73
  resolve:
48
74
  resolveGraph === null
@@ -69,6 +95,7 @@ export interface LocalCargoPackage {
69
95
  readonly manifest: string;
70
96
  /** Every target's source file: library, binaries, tests, build script. A target may live outside `directory`. */
71
97
  readonly sources: readonly string[];
98
+ readonly governingManifest: string;
72
99
  }
73
100
 
74
101
  /** What locked, offline `cargo metadata` reports about one Cargo workspace's mutable packages. */
@@ -92,6 +119,59 @@ export interface CargoPathInputsOptions {
92
119
  closure?: string;
93
120
  }
94
121
 
122
+ /**
123
+ * Nx workers handle termination by exiting, so their exit hook kills owned
124
+ * Cargo groups. Standalone callers need signal forwarding after detaching
125
+ * Cargo from the terminal; handlers are installed only where the caller has
126
+ * not already chosen a signal disposition, and removed after the last child.
127
+ * Uncatchable SIGKILL cannot execute either hook.
128
+ */
129
+ const cargoChildren = new Set<ChildProcess>();
130
+
131
+ function killCargoChild(child: ChildProcess): void {
132
+ if (process.platform === 'win32' || child.pid === undefined) child.kill('SIGKILL');
133
+ else {
134
+ try {
135
+ process.kill(-child.pid, 'SIGKILL');
136
+ } catch (error) {
137
+ if (!(error instanceof Error && 'code' in error && error.code === 'ESRCH')) throw error;
138
+ }
139
+ }
140
+ }
141
+
142
+ function killCargoChildren(): void {
143
+ for (const child of cargoChildren) killCargoChild(child);
144
+ }
145
+
146
+ type CargoSignal = 'SIGINT' | 'SIGTERM' | 'SIGHUP';
147
+ const forwardedSignals = new Set<CargoSignal>();
148
+ const cargoSignalHandlers: Record<CargoSignal, () => void> = {
149
+ SIGINT: () => forwardCargoSignal('SIGINT'),
150
+ SIGTERM: () => forwardCargoSignal('SIGTERM'),
151
+ SIGHUP: () => forwardCargoSignal('SIGHUP'),
152
+ };
153
+
154
+ function removeCargoSignalHandlers(): void {
155
+ for (const signal of forwardedSignals) process.removeListener(signal, cargoSignalHandlers[signal]);
156
+ forwardedSignals.clear();
157
+ }
158
+
159
+ function forwardCargoSignal(signal: CargoSignal): void {
160
+ killCargoChildren();
161
+ removeCargoSignalHandlers();
162
+ process.kill(process.pid, signal);
163
+ }
164
+
165
+ function ownCargoChildren(): void {
166
+ process.on('exit', killCargoChildren);
167
+ for (const signal of Object.keys(cargoSignalHandlers)) {
168
+ if (signal !== 'SIGINT' && signal !== 'SIGTERM' && signal !== 'SIGHUP') continue;
169
+ if (process.listenerCount(signal) !== 0) continue;
170
+ process.on(signal, cargoSignalHandlers[signal]);
171
+ forwardedSignals.add(signal);
172
+ }
173
+ }
174
+
95
175
  /**
96
176
  * Cargo's stdout, or its failure with Cargo's stderr in the message. Not
97
177
  * `promisify` from `node:util`: importing that module makes Node probe color
@@ -101,11 +181,61 @@ export interface CargoPathInputsOptions {
101
181
  * every run.
102
182
  */
103
183
  function runCargo(args: readonly string[], cwd: string): Promise<string> {
104
- // The executor form: the workspace compiles against es2022, which has no `Promise.withResolvers`.
184
+ // spawn forwards detached groups; execFile does not. The public ES2022
185
+ // library API requires the constructor form, not Promise.withResolvers.
105
186
  return new Promise((settle, reject) => {
106
- execFile('cargo', args, { cwd, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }, (error, stdout) =>
107
- error === null ? settle(stdout) : reject(error),
108
- );
187
+ const child = spawn('cargo', args, {
188
+ cwd,
189
+ detached: process.platform !== 'win32',
190
+ stdio: ['ignore', 'pipe', 'pipe'],
191
+ });
192
+ let stdout = '';
193
+ let stderr = '';
194
+ let stdoutBytes = 0;
195
+ let stderrBytes = 0;
196
+ let failure: Error | undefined;
197
+ const fail = (error: Error): void => {
198
+ failure ??= error;
199
+ killCargoChild(child);
200
+ };
201
+ child.stdout.setEncoding('utf8');
202
+ child.stderr.setEncoding('utf8');
203
+ child.stdout.on('data', (chunk: string) => {
204
+ if (failure !== undefined) return;
205
+ stdoutBytes += Buffer.byteLength(chunk);
206
+ if (stdoutBytes > 64 * 1024 * 1024) {
207
+ fail(new Error('Cargo stdout exceeded 64 MiB'));
208
+ return;
209
+ }
210
+ stdout += chunk;
211
+ });
212
+ child.stderr.on('data', (chunk: string) => {
213
+ if (failure !== undefined) return;
214
+ stderrBytes += Buffer.byteLength(chunk);
215
+ if (stderrBytes > 64 * 1024 * 1024) {
216
+ fail(new Error('Cargo stderr exceeded 64 MiB'));
217
+ return;
218
+ }
219
+ stderr += chunk;
220
+ });
221
+ child.on('error', (error) => {
222
+ failure ??= error;
223
+ });
224
+ child.stdout.on('error', fail);
225
+ child.stderr.on('error', fail);
226
+ child.on('close', (code, signal) => {
227
+ cargoChildren.delete(child);
228
+ if (cargoChildren.size === 0) {
229
+ process.removeListener('exit', killCargoChildren);
230
+ removeCargoSignalHandlers();
231
+ }
232
+ if (failure !== undefined) reject(failure);
233
+ else if (code !== 0) {
234
+ reject(new Error(`cargo ${args.join(' ')} failed (${signal ?? code}): ${stderr.trim()}`));
235
+ } else settle(stdout);
236
+ });
237
+ if (cargoChildren.size === 0) ownCargoChildren();
238
+ cargoChildren.add(child);
109
239
  });
110
240
  }
111
241
 
@@ -148,50 +278,330 @@ export const CARGO_ANCESTOR_INPUTS = ['Cargo.toml', '.cargo/config', '.cargo/con
148
278
  // on stderr ("Blocking waiting for file lock on package cache") a
149
279
  // timing-dependent number of times when Nx hashes many tasks at once, which
150
280
  // made one unchanged tree produce four different task hashes per run. Cargo's
151
- // stderr is kept for the failure path only, where execFile attaches it to the
281
+ // stderr is kept for the failure path only, where runCargo includes it in the
152
282
  // rejected error. Git runs under the same rule.
153
283
  const CHILD_STDIO: ['ignore', 'pipe', 'pipe'] = ['ignore', 'pipe', 'pipe'];
154
284
 
155
- /** Locked, offline `cargo metadata` for the workspace `manifestPath` names, run from `cwd`. */
156
- export async function readCargoResolve(manifestPath: string, cwd: string): Promise<CargoResolve> {
157
- const stdout = await runCargo(
158
- ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', resolve(manifestPath)],
159
- cwd,
160
- );
161
- const metadata = parseCargoMetadata(stdout);
162
- const local = await Promise.all(
163
- metadata.packages
164
- .filter((pkg) => pkg.source === null)
165
- .map(async (pkg): Promise<LocalCargoPackage> => {
166
- const manifest = await realpath(pkg.manifest_path);
167
- const sources = await Promise.all(pkg.targets.map((target) => realpath(target.src_path)));
168
- return { id: pkg.id, directory: dirname(manifest), manifest, sources };
169
- }),
285
+ /** A Cargo resolution refusal: no stale closure or runtime whole-workspace substitute is safe. */
286
+ export class CargoMetadataError extends Error {
287
+ readonly manifestPath: string;
288
+
289
+ constructor(manifestPath: string, cause: unknown) {
290
+ const detail =
291
+ cause instanceof Error
292
+ ? `${cause.name}: ${cause.message}`
293
+ : typeof cause === 'object' && cause !== null && 'message' in cause
294
+ ? String(cause.message)
295
+ : String(cause);
296
+ super(`Locked offline Cargo resolution failed for ${manifestPath}: ${detail}`, { cause });
297
+ this.name = 'CargoMetadataError';
298
+ this.manifestPath = manifestPath;
299
+ }
300
+ }
301
+
302
+ interface ResolveCache {
303
+ readonly key: string;
304
+ readonly cargo: CargoResolve;
305
+ }
306
+ interface ResolveState {
307
+ ready: Promise<void>;
308
+ cached: ResolveCache | null;
309
+ pending?: { readonly key: string; readonly promise: Promise<CargoResolve> };
310
+ }
311
+ const resolves = new Map<string, ResolveState>();
312
+ const RESOLVE_CACHE_SCHEMA = 'cargo-resolve-v1';
313
+
314
+ /**
315
+ * The full manifest SET, not merely the last closure: adding a glob workspace
316
+ * member must invalidate resolution even when Cargo.lock did not change.
317
+ * Graph callers supply Nx's already-current index; standalone hash callers
318
+ * take a fresh native snapshot. File contents below are read directly so an
319
+ * edit within Nx's mtime granularity cannot reuse a stale resolution.
320
+ */
321
+ function resolutionFiles(
322
+ root: string,
323
+ manifest: string,
324
+ cargo: CargoResolve | null,
325
+ indexed: ReadonlySet<string>,
326
+ ): ReadonlySet<string> {
327
+ const paths = new Set<string>([manifest]);
328
+ for (const file of indexed) paths.add(resolve(root, file));
329
+ for (const pkg of cargo?.local ?? []) {
330
+ paths.add(pkg.manifest);
331
+ paths.add(pkg.governingManifest);
332
+ }
333
+ if (cargo !== null) paths.add(join(cargo.root, 'Cargo.toml'));
334
+ const directories = new Set<string>([root]);
335
+ for (const file of paths) directories.add(dirname(file));
336
+ for (const directory of directories) {
337
+ paths.add(join(directory, 'Cargo.lock'));
338
+ for (let ancestor = directory; ; ancestor = dirname(ancestor)) {
339
+ paths.add(join(ancestor, '.cargo/config'));
340
+ paths.add(join(ancestor, '.cargo/config.toml'));
341
+ paths.add(join(ancestor, 'rust-toolchain'));
342
+ paths.add(join(ancestor, 'rust-toolchain.toml'));
343
+ paths.add(join(ancestor, 'Cargo.toml'));
344
+ if (dirname(ancestor) === ancestor) break;
345
+ }
346
+ }
347
+ for (const pin of CARGO_TOOLCHAIN_PIN_INPUTS) paths.add(pin.replace('{workspaceRoot}', root));
348
+ const cargoHome = resolve(process.env.CARGO_HOME ?? join(homedir(), '.cargo'));
349
+ paths.add(join(cargoHome, 'config'));
350
+ paths.add(join(cargoHome, 'config.toml'));
351
+ return paths;
352
+ }
353
+
354
+ /** Toolchain selection can change even while the declared pin's bytes do not. */
355
+ async function cargoExecutable(): Promise<string> {
356
+ for (const directory of (process.env.PATH ?? '').split(delimiter)) {
357
+ const executable = resolve(directory, process.platform === 'win32' ? 'cargo.exe' : 'cargo');
358
+ try {
359
+ await access(executable, constants.X_OK);
360
+ return await realpath(executable);
361
+ } catch (error) {
362
+ if (
363
+ !(
364
+ error instanceof Error &&
365
+ 'code' in error &&
366
+ (error.code === 'ENOENT' || error.code === 'EACCES' || error.code === 'ENOTDIR')
367
+ )
368
+ )
369
+ throw error;
370
+ }
371
+ }
372
+ return '<cargo-not-on-PATH>';
373
+ }
374
+
375
+ async function resolutionKey(
376
+ root: string,
377
+ manifest: string,
378
+ cargo: CargoResolve | null,
379
+ indexed: ReadonlySet<string>,
380
+ ): Promise<string> {
381
+ const cargoHome = resolve(process.env.CARGO_HOME ?? join(homedir(), '.cargo'));
382
+ const executable = await cargoExecutable();
383
+ const hash = createHash('sha256').update(
384
+ `${RESOLVE_CACHE_SCHEMA}\0${root}\0${manifest}\0${cargoHome}\0${executable}\0${process.env.RUSTUP_TOOLCHAIN ?? ''}\0${process.env.RUSTUP_HOME ?? ''}\0`,
170
385
  );
386
+ for (const file of [...resolutionFiles(root, manifest, cargo, indexed)].sort()) {
387
+ hash.update(file).update('\0');
388
+ try {
389
+ const [bytes, canonical] = await Promise.all([readFile(file), realpath(file)]);
390
+ hash.update(`${canonical}\0${bytes.length}\0`).update(bytes);
391
+ } catch (error) {
392
+ if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT')) throw error;
393
+ hash.update('missing');
394
+ }
395
+ hash.update('\0');
396
+ }
397
+ return hash.digest('hex');
398
+ }
399
+
400
+ function cargoFromMetadata(metadata: CargoMetadata): CargoResolve {
171
401
  return {
172
- root: await realpath(metadata.workspace_root),
402
+ root: metadata.workspace_root,
173
403
  members: new Set(metadata.workspace_members),
174
- local,
404
+ local: metadata.packages
405
+ .filter((pkg) => pkg.source === null)
406
+ .map((pkg) => {
407
+ if (pkg.governing_manifest_path === undefined) throw new Error('Cargo cache omitted a governing manifest');
408
+ return {
409
+ id: pkg.id,
410
+ directory: dirname(pkg.manifest_path),
411
+ manifest: pkg.manifest_path,
412
+ sources: pkg.targets.map((target) => target.src_path),
413
+ governingManifest: pkg.governing_manifest_path,
414
+ };
415
+ }),
175
416
  edges:
176
417
  metadata.resolve === null
177
418
  ? null
178
- : new Map(metadata.resolve.nodes.map((node) => [node.id, node.deps.map((dependency) => dependency.pkg)])),
419
+ : new Map(metadata.resolve.nodes.map((node) => [node.id, node.deps.map((dep) => dep.pkg)])),
179
420
  };
180
421
  }
181
422
 
423
+ async function loadResolveCache(file: string): Promise<ResolveCache | null> {
424
+ let content: string;
425
+ try {
426
+ content = await readFile(file, 'utf8');
427
+ } catch (error) {
428
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return null;
429
+ throw error;
430
+ }
431
+ try {
432
+ const newline = content.indexOf('\n');
433
+ const key = content.slice(0, newline);
434
+ if (newline === -1 || !/^[a-f0-9]{64}$/.test(key)) throw new Error('invalid content-key header');
435
+ const metadata = parseCargoMetadata(content.slice(newline + 1));
436
+ return { key, cargo: cargoFromMetadata(metadata) };
437
+ } catch (error) {
438
+ process.stderr.write(
439
+ `@smoothbricks/nx-plugin: discarding corrupt Cargo resolution cache ${file}: ${error instanceof Error ? error.message : String(error)}\n`,
440
+ );
441
+ return null;
442
+ }
443
+ }
444
+
182
445
  /**
183
- * The canonical root manifest of the Cargo workspace that governs `pkg`.
184
- * Metadata's `workspace_root` describes only the invoking workspace, which is
185
- * the answer for its own members. Any other path package asks Cargo, which
186
- * resolves an explicit `package.workspace` as well as the ancestor search.
446
+ * One content-keyed resolution per Cargo workspace, across concurrent graph
447
+ * calls and workers. Nx's kernel FileLock releases even if its worker dies;
448
+ * in-process callers join before taking that lock, so a synchronous lock()
449
+ * cannot block the continuation that owns it. A changed key queues behind
450
+ * the old child instead of receiving its stale answer.
187
451
  */
188
- export async function governingManifest(cargo: CargoResolve, pkg: LocalCargoPackage, cwd: string): Promise<string> {
189
- if (cargo.members.has(pkg.id)) return realpath(join(cargo.root, 'Cargo.toml'));
452
+ export async function readCargoResolve(
453
+ manifestPath: string,
454
+ cwd: string,
455
+ indexed?: ReadonlySet<string>,
456
+ ): Promise<CargoResolve> {
457
+ let manifest = resolve(manifestPath);
458
+ try {
459
+ const root = await realpath(cwd);
460
+ manifest = await realpath(manifest);
461
+ const index =
462
+ indexed ?? new Set(new WorkspaceContext(root, workspaceDataDirectoryForWorkspace(root)).glob(['**/Cargo.toml']));
463
+ const identity = createHash('sha256').update(`${RESOLVE_CACHE_SCHEMA}\0${root}\0${manifest}`).digest('hex');
464
+ const directory = join(workspaceDataDirectoryForWorkspace(root), 'cargo-resolve', identity);
465
+ const file = join(directory, 'entry.cache');
466
+ let state = resolves.get(identity);
467
+ if (state === undefined) {
468
+ const created: ResolveState = { ready: Promise.resolve(), cached: null };
469
+ created.ready = loadResolveCache(file).then((cached) => {
470
+ created.cached = cached;
471
+ });
472
+ resolves.set(identity, created);
473
+ state = created;
474
+ try {
475
+ await created.ready;
476
+ } catch (error) {
477
+ resolves.delete(identity);
478
+ throw error;
479
+ }
480
+ } else await state.ready;
481
+ for (let joined = 0; joined <= 3; joined += 1) {
482
+ const key = await resolutionKey(root, manifest, state.cached?.cargo ?? null, index);
483
+ const pending = state.pending;
484
+ if (pending !== undefined) {
485
+ if (joined === 3) break;
486
+ if (cargoResolveEvents.hasSubscribers) cargoResolveEvents.publish({ phase: 'join', manifest });
487
+ try {
488
+ await pending.promise;
489
+ } catch (error) {
490
+ // Sharers of a refused key receive its refusal; a changed key still
491
+ // needs its own resolution after the old flight releases the lock.
492
+ if (pending.key === key) throw error;
493
+ }
494
+ // Re-key every joiner with the discovered paths, including an external
495
+ // manifest edited after the flight's last validation, before publication.
496
+ continue;
497
+ }
498
+ if (state.cached?.key === key) return state.cached.cargo;
499
+ if (joined === 3) break;
500
+ const active = state;
501
+ const promise = resolveAndCache(root, manifest, index, directory, file, active);
502
+ active.pending = { key, promise };
503
+ try {
504
+ return await promise;
505
+ } finally {
506
+ if (active.pending?.promise === promise) delete active.pending;
507
+ }
508
+ }
509
+ throw new Error('Cargo resolution inputs kept changing across three consecutive in-flight queries');
510
+ } catch (error) {
511
+ if (error instanceof CargoMetadataError) throw error;
512
+ throw new CargoMetadataError(manifest, error);
513
+ }
514
+ }
515
+
516
+ async function resolveAndCache(
517
+ root: string,
518
+ manifest: string,
519
+ indexed: ReadonlySet<string>,
520
+ directory: string,
521
+ file: string,
522
+ state: ResolveState,
523
+ ): Promise<CargoResolve> {
524
+ if (IS_WASM)
525
+ throw new Error('Cargo resolution requires the native Nx file lock; a WASM Nx host cannot own Cargo children');
526
+ await mkdir(directory, { recursive: true });
527
+ const lockFile = join(directory, 'resolve.lock');
528
+ const lock = new FileLock(lockFile);
529
+ while (lock.check()) await lock.wait();
530
+ // Nx's lock() is synchronous: losing this inter-process check/acquire race
531
+ // can block for one foreign flight. Only the in-process leader reaches it.
532
+ lock.lock();
533
+ try {
534
+ // Another worker may have populated it while this worker waited.
535
+ state.cached = await loadResolveCache(file);
536
+ let inputs = state.cached?.cargo ?? null;
537
+ for (let attempt = 0; attempt < 3; attempt += 1) {
538
+ const before = await resolutionKey(root, manifest, inputs, indexed);
539
+ if (state.cached?.key === before) return state.cached.cargo;
540
+ const knownFiles = resolutionFiles(root, manifest, inputs, indexed);
541
+ // A filesystem timestamp, not a sandbox's wall clock: a newly
542
+ // discovered external input edited during this flight requires replay.
543
+ await writeFile(lockFile, '');
544
+ const started = (await stat(lockFile)).mtimeMs;
545
+ const metadata = await resolveCargoMetadata(manifest, root);
546
+ const cargo = cargoFromMetadata(metadata);
547
+ // Capture the published key before validation. Reading it afterwards
548
+ // could pair a post-edit key with Cargo's pre-edit answer.
549
+ const key = await resolutionKey(root, manifest, cargo, indexed);
550
+ let changed = before !== (await resolutionKey(root, manifest, inputs, indexed));
551
+ for (const added of resolutionFiles(root, manifest, cargo, indexed)) {
552
+ if (knownFiles.has(added)) continue;
553
+ try {
554
+ if ((await stat(added)).mtimeMs >= started) changed = true;
555
+ } catch (error) {
556
+ if (!(error instanceof Error && 'code' in error && error.code === 'ENOENT')) throw error;
557
+ }
558
+ }
559
+ if (changed) {
560
+ inputs = cargo;
561
+ continue;
562
+ }
563
+ const temporary = `${file}.${process.pid}.${randomUUID()}.tmp`;
564
+ try {
565
+ await writeFile(temporary, `${key}\n${JSON.stringify(metadata)}\n`);
566
+ await rename(temporary, file);
567
+ } finally {
568
+ await rm(temporary, { force: true });
569
+ }
570
+ state.cached = { key, cargo };
571
+ return cargo;
572
+ }
573
+ throw new Error('Cargo resolution inputs kept changing during three consecutive locked offline queries');
574
+ } finally {
575
+ lock.unlock();
576
+ }
577
+ }
578
+
579
+ async function resolveCargoMetadata(manifestPath: string, cwd: string): Promise<CargoMetadata> {
190
580
  const stdout = await runCargo(
191
- ['locate-project', '--workspace', '--manifest-path', pkg.manifest, '--message-format', 'plain'],
581
+ ['metadata', '--format-version', '1', '--locked', '--offline', '--manifest-path', manifestPath],
192
582
  cwd,
193
583
  );
194
- return realpath(stdout.trim());
584
+ const metadata = parseCargoMetadata(stdout);
585
+ metadata.workspace_root = await realpath(metadata.workspace_root);
586
+ const members = new Set(metadata.workspace_members);
587
+ // Sequential: the metadata flight owns every Cargo process it starts,
588
+ // including the one-time governing-workspace lookups for path dependencies.
589
+ for (const pkg of metadata.packages) {
590
+ if (pkg.source !== null) continue;
591
+ pkg.manifest_path = await realpath(pkg.manifest_path);
592
+ for (const target of pkg.targets) target.src_path = await realpath(target.src_path);
593
+ pkg.governing_manifest_path = members.has(pkg.id)
594
+ ? join(metadata.workspace_root, 'Cargo.toml')
595
+ : await realpath(
596
+ (
597
+ await runCargo(
598
+ ['locate-project', '--workspace', '--manifest-path', pkg.manifest_path, '--message-format', 'plain'],
599
+ cwd,
600
+ )
601
+ ).trim(),
602
+ );
603
+ }
604
+ return metadata;
195
605
  }
196
606
 
197
607
  export async function hashCargoPathInputs(
@@ -227,7 +637,7 @@ export async function hashCargoPathInputs(
227
637
  }
228
638
  directories.add(directory);
229
639
  files.add(pkg.manifest);
230
- files.add(await governingManifest(cargo, pkg, root));
640
+ files.add(pkg.governingManifest);
231
641
  packageSources.push(...pkg.sources);
232
642
  // A member can inherit edition, lint policy, dependencies and
233
643
  // profiles from a workspace above its package directory. Those manifests
@@ -719,9 +719,9 @@ export function listCargoCrossTestTargets(absoluteProjectRoot: string): CargoCro
719
719
  }
720
720
 
721
721
  /**
722
- * Tests that nextest.toml singles out are pinned to this suffix instead of
723
- * being sharded. Only a sharded crate has one: an unsharded crate runs its
724
- * whole suite in a single nextest process, which is all the pin restores.
722
+ * Tests that nextest.toml singles out use this suffix before their shard name.
723
+ * The class is hash-partitioned by the same declared count as the ordinary
724
+ * shards, so it never sits in one run that outgrows the target's bound.
725
725
  */
726
726
  export const CARGO_TEST_EXCEPTIONS_SUFFIX = 'exceptions';
727
727
 
@@ -753,17 +753,17 @@ export function packageNameFromCargoTestTarget(targetName: string): string | nul
753
753
  }
754
754
  const name = targetName
755
755
  .slice('cargo-test-'.length)
756
- .replace(new RegExp(`-(shard[1-9][0-9]*|${CARGO_TEST_EXCEPTIONS_SUFFIX})$`), '');
756
+ .replace(new RegExp(`-(shard[1-9][0-9]*|${CARGO_TEST_EXCEPTIONS_SUFFIX}-shard[1-9][0-9]*)$`), '');
757
757
  return name.length === 0 ? null : name;
758
758
  }
759
759
 
760
760
  /**
761
761
  * The tests nextest.toml singles out with an override, as one filterset.
762
762
  *
763
- * Overrides can encode constraints that must survive sharding: a test group
764
- * limits concurrency within one nextest run, while a raised slow timeout can
765
- * identify expensive tests that should not delay an otherwise bounded shard.
766
- * Keep the configured exceptions in one run so group limits remain effective.
763
+ * An override can give a test a bound of its own: a raised slow timeout
764
+ * identifies an expensive test that should not delay an otherwise bounded
765
+ * shard. Partition those tests separately, so every target keeps its time
766
+ * bound and each class runs beside the ordinary shards.
767
767
  *
768
768
  * Deriving this from the config that declares the classes, rather than
769
769
  * restating their filters, means adding an override there is the whole change —