@smoothbricks/nx-plugin 0.4.5 → 0.4.7

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.
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import { readJsonFile, } from 'nx/src/devkit-exports.js';
6
6
  import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
7
7
  import { parse as parseToml } from 'smol-toml';
8
8
  import { BOUNDED_TEST_KILL_AFTER_MS, BOUNDED_TEST_TIMEOUT_MS } from './bounded-test-policy.js';
9
- import { attributeCargoWorkspacePackages, CARGO_TEST_COMPILE_TARGET, CARGO_TEST_EXCEPTIONS_SUFFIX, CARGO_TEST_TARGET, cargoPackageTestInputs, cargoTestPackageTargetName, exceptionalTestFilter, listCargoWorkspacePackages, nextestConfigRelPath, } from './cargo-workspace.js';
9
+ import { attributeCargoWorkspacePackages, CARGO_TEST_ARCHIVE_FILE, CARGO_TEST_ARCHIVE_TARGET, CARGO_TEST_COMPILE_TARGET, CARGO_TEST_EXCEPTIONS_SUFFIX, CARGO_TEST_TARGET, cargoPackageTestInputs, cargoTestPackageTargetName, createCargoInputsCache, exceptionalTestFilter, listCargoWorkspacePackages, NEXTEST_REPO_CONFIG_PATH, nextestToolConfigArg, } from './cargo-workspace.js';
10
10
  import { CARGO_CROSS_LINT_COMMAND, CARGO_CROSS_LINT_TARGET, CARGO_FETCH_COMMAND, CARGO_FETCH_TARGET, CARGO_FROZEN_PREFIX, CARGO_LINT_CLIPPY_COMMAND, cargoFrozen, } from './cross-check-policy.js';
11
11
  import { isNonSourceDirectory } from './source-directories.js';
12
12
  import { PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
@@ -93,9 +93,7 @@ function hostPlatformTargetNames(targetNames, hostPlatform) {
93
93
  */
94
94
  function buildOutputTargetNames(targetNames) {
95
95
  return [...new Set(targetNames)]
96
- .filter((name) => BUILD_OUTPUT_TARGET_PATTERN.test(name) &&
97
- !name.startsWith(`${CARGO_TEST_TARGET}-`) &&
98
- !name.startsWith('cargo-lint-'))
96
+ .filter((name) => BUILD_OUTPUT_TARGET_PATTERN.test(name) && !name.startsWith(`${CARGO_TEST_TARGET}-`))
99
97
  .sort();
100
98
  }
101
99
  //#region smoo!n/rust-output-target-inference
@@ -136,6 +134,20 @@ function cargoRuntimeInput(projectRoot, command) {
136
134
  const quotedRoot = `'${projectRoot.replaceAll("'", "'\"'\"'")}'`;
137
135
  return { runtime: `cd ${quotedRoot} && ${command}` };
138
136
  }
137
+ /**
138
+ * Everything a target actually runs, as one string. `nx:run-commands` accepts
139
+ * either spelling and inference asks four questions of the text — does it run
140
+ * frozen cargo, which tool's version keys its cache, which target directories
141
+ * does it write — so the two shapes collapse here once rather than at each ask.
142
+ */
143
+ function targetCommandText(target) {
144
+ const command = target.options?.command;
145
+ const commands = target.options?.commands;
146
+ return [
147
+ typeof command === 'string' ? command : '',
148
+ ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
149
+ ].join('\n');
150
+ }
139
151
  const NAPI_INPUTS = CARGO_OUTPUT_INPUTS;
140
152
  const NAPI_TARGET_CONVENTIONS = {
141
153
  'aarch64-apple-darwin': {
@@ -207,14 +219,20 @@ function createCargoWasmTarget(projectRoot, config, repoRooted) {
207
219
  * which is not a property of the tests. `cargo test --no-run` pays that cost
208
220
  * in its own unbounded target. Cargo reuses incremental compilation locally;
209
221
  * Nx cannot cache this warming action without restoring shared mutable state.
210
- * The bounded runner then uses the warm directory for the suites' own runtime.
211
222
  *
212
223
  * Cargo flocks one `target/` per invocation. Nx must not run two cargo
213
224
  * writers on that directory at once — that is a mutex, not a deadlock, and
214
225
  * the second process sits in "Blocking waiting for file lock" until a
215
226
  * timeout. Inference serializes writers that share the default target dir
216
- * (`napi-debug` after compile, `cargo-test` after `napi-debug`). Clippy uses
217
- * its own `--target-dir` so lint can overlap tests.
227
+ * (`napi-debug` after compile).
228
+ *
229
+ * Clippy writes ONE shared `--target-dir target/cargo-lint` rather than a
230
+ * directory per crate. Per-crate dirs were never about overlap: clippy of a
231
+ * crate is a check build of its whole closure, so 49 crates meant 49 closure
232
+ * rebuilds and 11 GiB of target directories on one repository, and no
233
+ * per-crate cache hit ever repaid that. Sharing the directory costs nothing:
234
+ * cargo's own lock serializes the invocations that use it and each dependency
235
+ * artifact is then compiled once for all of them.
218
236
  */
219
237
  function createCargoTestCompileTarget(projectRoot) {
220
238
  return {
@@ -231,6 +249,41 @@ function createCargoTestCompileTarget(projectRoot) {
231
249
  },
232
250
  };
233
251
  }
252
+ /**
253
+ * The workspace's test binaries, compiled once and packed into one file the
254
+ * per-crate runners execute from.
255
+ *
256
+ * This is the only cargo BUILD in the test graph. Before it, every per-crate
257
+ * target ran `nextest run --workspace -E 'package(X)'`, which is a workspace
258
+ * build with a filter: 32 such tasks on one repository each re-entered cargo,
259
+ * re-resolved features and chained behind one another so the flocked `target/`
260
+ * had a single writer. Reading from an archive removes the build from the run
261
+ * entirely — the runner extracts binaries into its own temporary directory and
262
+ * touches no shared state — which is what lets the runners fan out.
263
+ *
264
+ * Cached, unlike `cargo-test-compile`, because it produces a VALUE: one file,
265
+ * not a mutable build tree Nx would have to restore. `--workspace` matches the
266
+ * clippy selection exactly, so both gates unify features the same way.
267
+ */
268
+ function createCargoTestArchiveTarget(projectRoot, toolConfig) {
269
+ const archiveCommand = (profile) => cargoFrozen(`nextest archive --workspace${profile} --archive-file ${CARGO_TEST_ARCHIVE_FILE} --user-config-file none ${toolConfig}`);
270
+ return {
271
+ executor: 'nx:run-commands',
272
+ cache: true,
273
+ inputs: CARGO_INPUTS,
274
+ outputs: [`{projectRoot}/${CARGO_TEST_ARCHIVE_FILE}`],
275
+ options: {
276
+ commands: [`mkdir -p ${posix.dirname(CARGO_TEST_ARCHIVE_FILE)}`, archiveCommand('')],
277
+ cwd: projectRoot,
278
+ parallel: false,
279
+ },
280
+ configurations: {
281
+ production: {
282
+ commands: [`mkdir -p ${posix.dirname(CARGO_TEST_ARCHIVE_FILE)}`, archiveCommand(' --release')],
283
+ },
284
+ },
285
+ };
286
+ }
234
287
  function createCargoTestTarget(projectRoot) {
235
288
  return {
236
289
  executor: '@smoothbricks/nx-plugin:bounded-exec',
@@ -493,9 +546,26 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
493
546
  workspaceRoot,
494
547
  absoluteProjectRoot: join(workspaceRoot, cargoWorkspaceRoot),
495
548
  memberDir: plan.package.dir,
549
+ cache: cargoWorkspace.inputsCache,
496
550
  })))).flat()),
497
551
  ];
498
- targets[CARGO_TEST_COMPILE_TARGET].inputs = workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS;
552
+ // Generated crate sources (wire codecs, vocabularies, fixtures) are
553
+ // gitignored and invisible to nx's file hasher; they reach these targets
554
+ // as the outputs of the targets a repository adds to `dependsOn`. Hashing
555
+ // those outputs here means a repository never has to restate `inputs` to
556
+ // add them - and a restated `inputs` REPLACES this union with the root
557
+ // project's `default`, which owns no crate file, so the archive then
558
+ // cache-hits against edited crates while every per-crate test reruns on
559
+ // the stale binary. That verdict is wrong, not merely slow.
560
+ const workspaceCargoInputs = [
561
+ ...(workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS),
562
+ { dependentTasksOutputFiles: '**/*', transitive: false },
563
+ ];
564
+ targets[CARGO_TEST_COMPILE_TARGET].inputs = workspaceCargoInputs;
565
+ targets[CARGO_TEST_ARCHIVE_TARGET] = createCargoTestArchiveTarget(cargoWorkspaceRoot, nextestToolConfigArg(workspaceRoot, cargoWorkspaceRoot, PLUGIN_NEXTEST_CONFIG));
566
+ // `archive.include` lives in the repository's nextest config, so that file
567
+ // decides what the archive CONTAINS, not merely how a run behaves.
568
+ targets[CARGO_TEST_ARCHIVE_TARGET].inputs = [...workspaceCargoInputs, `{projectRoot}/${NEXTEST_REPO_CONFIG_PATH}`];
499
569
  const aggregateDependencies = cargoWorkspace.packages.flatMap((plan) => plan.pieces.map((piece) => cargoTargetDependency(projectName, {
500
570
  projectName: plan.package.projectName,
501
571
  targetName: piece.targetName,
@@ -504,20 +574,24 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
504
574
  aggregateDependencies.length > 0
505
575
  ? { executor: 'nx:noop', cache: true, outputs: [], dependsOn: aggregateDependencies }
506
576
  : createCargoTestTarget(cargoWorkspaceRoot);
507
- if (cargoWorkspace.packages.length === 0) {
508
- targets['cargo-lint'] = {
509
- executor: 'nx:run-commands',
510
- cache: true,
511
- inputs: CARGO_INPUTS,
512
- outputs: [],
513
- options: {
514
- commands: ['cargo fmt --all --check', CARGO_LINT_CLIPPY_COMMAND],
515
- cwd: cargoWorkspaceRoot,
516
- parallel: false,
517
- },
518
- };
519
- validationTargets.push('cargo-lint');
520
- }
577
+ // One clippy over the whole workspace, in one target dir. Clippy of a crate
578
+ // is a check build of its closure; a target dir per crate compiled every
579
+ // shared dependency once per crate (49 closures, 11 GiB on one repository)
580
+ // and no per-crate cache hit ever repaid that. The workspace inputs are
581
+ // exactly the union of every crate's, so the verdict caches on the same
582
+ // key set the compile does. Non-root packages depend on this target.
583
+ targets['cargo-lint'] = {
584
+ executor: 'nx:run-commands',
585
+ cache: true,
586
+ inputs: workspaceCargoInputs,
587
+ outputs: [],
588
+ options: {
589
+ commands: ['cargo fmt --all --check', CARGO_LINT_CLIPPY_COMMAND],
590
+ cwd: cargoWorkspaceRoot,
591
+ parallel: false,
592
+ },
593
+ };
594
+ validationTargets.push('cargo-lint');
521
595
  // The Linux arm of `cargo-lint`, as its own target. Rationale for the name,
522
596
  // the command and the absent cross test leg lives in ./cross-check-policy.ts.
523
597
  //
@@ -548,7 +622,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
548
622
  targets[CARGO_CROSS_LINT_TARGET] = {
549
623
  executor: 'nx:run-commands',
550
624
  cache: true,
551
- inputs: workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS,
625
+ inputs: workspaceCargoInputs,
552
626
  outputs: [],
553
627
  options: {
554
628
  command: CARGO_CROSS_LINT_COMMAND,
@@ -595,40 +669,19 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
595
669
  dependsOn: packageTargetNames,
596
670
  };
597
671
  }
598
- for (const plan of cargoPackagePlans) {
599
- const name = plan.package.name;
600
- targets[`cargo-lint-${name}`] = {
601
- executor: 'nx:run-commands',
602
- cache: true,
603
- inputs: await cargoPackageTestInputs({
604
- workspaceRoot,
605
- absoluteProjectRoot: join(workspaceRoot, cargoWorkspace.projectRoot),
606
- memberDir: plan.package.dir,
607
- inputRoot: projectRoot === cargoWorkspace.projectRoot
608
- ? '{projectRoot}'
609
- : posix.join('{workspaceRoot}', cargoWorkspace.projectRoot),
610
- }),
611
- outputs: [],
612
- options: {
613
- commands: [
614
- `cargo fmt -p ${name} --check`,
615
- cargoFrozen(`clippy -p ${name} --all-targets --target-dir target/cargo-lint-${name} -- -D warnings`),
616
- ],
617
- cwd: cargoWorkspace.projectRoot,
618
- parallel: false,
619
- },
620
- };
621
- }
622
- const lintPlans = isCargoWorkspaceRoot ? cargoWorkspace.packages : cargoPackagePlans;
623
- if (lintPlans.length > 0) {
672
+ if (!isCargoWorkspaceRoot && cargoPackagePlans.length > 0) {
673
+ // The workspace root runs clippy for every member; a package owning
674
+ // crates validates through that one verdict.
624
675
  targets['cargo-lint'] = {
625
676
  executor: 'nx:noop',
626
677
  cache: true,
627
678
  outputs: [],
628
- dependsOn: lintPlans.map((plan) => cargoTargetDependency(projectName, {
629
- projectName: plan.package.projectName,
630
- targetName: `cargo-lint-${plan.package.name}`,
631
- })),
679
+ dependsOn: [
680
+ cargoTargetDependency(projectName, {
681
+ projectName: cargoWorkspace.rootProjectName,
682
+ targetName: 'cargo-lint',
683
+ }),
684
+ ],
632
685
  };
633
686
  validationTargets.push('cargo-lint');
634
687
  }
@@ -703,12 +756,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
703
756
  if (name === CARGO_FETCH_TARGET) {
704
757
  continue;
705
758
  }
706
- const command = target.options?.command;
707
- const commands = target.options?.commands;
708
- const text = [
709
- typeof command === 'string' ? command : '',
710
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
711
- ].join('\n');
759
+ const text = targetCommandText(target);
712
760
  if (text.includes(CARGO_FROZEN_PREFIX)) {
713
761
  target.dependsOn = [CARGO_FETCH_TARGET, ...(target.dependsOn ?? [])];
714
762
  }
@@ -720,12 +768,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
720
768
  targetName: CARGO_FETCH_TARGET,
721
769
  });
722
770
  for (const target of Object.values(targets)) {
723
- const command = target.options?.command;
724
- const commands = target.options?.commands;
725
- const text = [
726
- typeof command === 'string' ? command : '',
727
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
728
- ].join('\n');
771
+ const text = targetCommandText(target);
729
772
  if (text.includes(CARGO_FROZEN_PREFIX)) {
730
773
  target.dependsOn = [rootFetch, ...(target.dependsOn ?? [])];
731
774
  }
@@ -734,12 +777,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
734
777
  // Cache verdicts and dedicated artifacts, never Cargo's shared mutable build
735
778
  // directories. Resolve tool versions from the command's actual directory.
736
779
  for (const [name, target] of Object.entries(targets)) {
737
- const command = target.options?.command;
738
- const commands = target.options?.commands;
739
- const text = [
740
- typeof command === 'string' ? command : '',
741
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
742
- ].join('\n');
780
+ const text = targetCommandText(target);
743
781
  if (!text.includes(CARGO_FROZEN_PREFIX))
744
782
  continue;
745
783
  target.outputs ??= [];
@@ -748,7 +786,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
748
786
  const cwd = target.options?.cwd;
749
787
  const versions = name.startsWith('cargo-lint')
750
788
  ? ` && cargo clippy -V${name === CARGO_CROSS_LINT_TARGET ? '' : ' && cargo fmt --version'}`
751
- : text.includes('nextest run')
789
+ : text.includes('nextest ')
752
790
  ? ' && cargo nextest --version'
753
791
  : '';
754
792
  target.inputs = [
@@ -758,8 +796,7 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
758
796
  ];
759
797
  }
760
798
  // Cargo flocks the workspace's default target/. Keep every writer out of the
761
- // bounded test window by placing N-API debug builds on the same serialized
762
- // chain as the root compile and per-crate runners.
799
+ // bounded test window by placing N-API debug builds behind the root compile.
763
800
  const cargoTestCompileDependency = targets[CARGO_TEST_COMPILE_TARGET]
764
801
  ? CARGO_TEST_COMPILE_TARGET
765
802
  : isCargoProject && cargoWorkspace
@@ -776,15 +813,18 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
776
813
  }
777
814
  }
778
815
  if (napiDebug && cargoWorkspace) {
779
- const firstCargoTest = cargoPackagePlans.flatMap((plan) => plan.pieces)[0];
780
- const firstTarget = firstCargoTest ? targets[firstCargoTest.targetName] : undefined;
781
- if (firstCargoTest && firstTarget) {
782
- const previous = cargoTargetDependency(projectName, firstCargoTest.previous);
783
- const debugDependencies = napiDebug.dependsOn ?? [];
784
- if (!hasCargoTargetDependency(debugDependencies, previous)) {
785
- napiDebug.dependsOn = [...debugDependencies, previous];
816
+ // A crate whose project also builds the debug cdylib runs its tests after
817
+ // that build: the tests dlopen what `napi-debug` writes into `target/debug`,
818
+ // and an archived test binary still loads it from this tree. The old serial
819
+ // chain gave the same guarantee by accident, to whichever crate happened to
820
+ // be first; this states it for exactly the crates that own the artifact.
821
+ for (const plan of cargoPackagePlans) {
822
+ for (const piece of plan.pieces) {
823
+ const target = targets[piece.targetName];
824
+ if (target && !hasCargoTargetDependency(target.dependsOn ?? [], 'napi-debug')) {
825
+ target.dependsOn = [...(target.dependsOn ?? []), 'napi-debug'];
826
+ }
786
827
  }
787
- firstTarget.dependsOn = (firstTarget.dependsOn ?? []).map((dependency) => hasCargoTargetDependency([dependency], previous) ? 'napi-debug' : dependency);
788
828
  }
789
829
  }
790
830
  else if (napiDebug) {
@@ -863,10 +903,37 @@ async function createProjectTargets(packageJsonPath, workspaceRoot, hostPlatform
863
903
  ],
864
904
  };
865
905
  }
866
- if (hasAnyBuildOutputTarget) {
906
+ // Every cargo build directory this project's own commands write. `clean`
907
+ // cannot learn them from `outputs`: a clippy or nextest target dir is a
908
+ // multi-GB build tree that must never enter the Nx cache (`target/cargo-lint`
909
+ // alone was 11 GiB when each of 49 crates had its own), so the targets
910
+ // deliberately declare `outputs: []`. Reading `--target-dir` back off the
911
+ // commands means the list cannot drift from what actually gets written, and a
912
+ // directory is named relative to the command's own cwd, not the project root.
913
+ const cargoBuildDirs = [
914
+ ...new Set(Object.entries(targets).flatMap(([name, target]) => {
915
+ const cwd = target.options?.cwd;
916
+ const base = typeof cwd === 'string' ? cwd : projectRoot;
917
+ const dirs = [...targetCommandText(target).matchAll(/--target-dir\s+(\S+)/g)].flatMap((match) => match[1] === undefined ? [] : [match[1]]);
918
+ if (name === CARGO_TEST_ARCHIVE_TARGET) {
919
+ dirs.push(posix.dirname(CARGO_TEST_ARCHIVE_FILE));
920
+ }
921
+ return dirs.map((dir) => `{workspaceRoot}/${posix.normalize(posix.join(base, dir))}`);
922
+ })),
923
+ ].sort();
924
+ if (hasAnyBuildOutputTarget || cargoBuildDirs.length > 0) {
925
+ const declaredBuild = declaredTargets.build;
926
+ const declaredBuildOutputs = isRecord(declaredBuild) && Array.isArray(declaredBuild.outputs)
927
+ ? declaredBuild.outputs.filter((output) => typeof output === 'string')
928
+ : null;
867
929
  targets.clean = {
868
930
  executor: '@smoothbricks/nx-plugin:clean-outputs',
869
931
  cache: false,
932
+ // Naming any output REPLACES the executor's default, so the build outputs
933
+ // it would have used are restated here rather than lost.
934
+ ...(cargoBuildDirs.length > 0
935
+ ? { options: { outputs: [...(declaredBuildOutputs ?? ['{projectRoot}/dist']), ...cargoBuildDirs] } }
936
+ : {}),
870
937
  };
871
938
  }
872
939
  if (targets['typecheck-tests']) {
@@ -1288,10 +1355,6 @@ async function resolveCargoWorkspaces(packageJsonPaths, workspaceRoot) {
1288
1355
  dir: posix.join(rootProject.root, pkg.dir),
1289
1356
  })), projects).map((pkg) => ({ ...pkg, dir: posix.relative(rootProject.root, pkg.dir) || '.' }));
1290
1357
  const packages = [];
1291
- let previous = {
1292
- projectName: rootProject.name,
1293
- targetName: CARGO_TEST_COMPILE_TARGET,
1294
- };
1295
1358
  const exceptional = exceptionalTestFilter(PLUGIN_NEXTEST_CONFIG);
1296
1359
  for (const pkg of attributed) {
1297
1360
  const pieces = [];
@@ -1302,25 +1365,26 @@ async function resolveCargoWorkspaces(packageJsonPaths, workspaceRoot) {
1302
1365
  const targetName = cargoTestPackageTargetName(pkg.name, sharded ? `shard${index}` : undefined);
1303
1366
  pieces.push({
1304
1367
  extra: sharded ? ` --partition hash:${index}/${pkg.testShards}` : '',
1305
- previous,
1306
1368
  selector: shardable,
1307
1369
  targetName,
1308
1370
  });
1309
- previous = { projectName: pkg.projectName, targetName };
1310
1371
  }
1311
1372
  if (pin !== null) {
1312
1373
  const targetName = cargoTestPackageTargetName(pkg.name, CARGO_TEST_EXCEPTIONS_SUFFIX);
1313
1374
  pieces.push({
1314
1375
  extra: '',
1315
- previous,
1316
1376
  selector: `package(${pkg.name}) and (${pin})`,
1317
1377
  targetName,
1318
1378
  });
1319
- previous = { projectName: pkg.projectName, targetName };
1320
1379
  }
1321
1380
  packages.push({ package: pkg, pieces });
1322
1381
  }
1323
- workspaces.push({ packages, rootProjectName: rootProject.name, projectRoot: rootProject.root });
1382
+ workspaces.push({
1383
+ packages,
1384
+ rootProjectName: rootProject.name,
1385
+ projectRoot: rootProject.root,
1386
+ inputsCache: createCargoInputsCache(),
1387
+ });
1324
1388
  }
1325
1389
  return workspaces;
1326
1390
  }
@@ -1328,14 +1392,27 @@ async function resolveCargoWorkspaces(packageJsonPaths, workspaceRoot) {
1328
1392
  * One bounded target per crate; a crate that declares `smoothbricks.test.shards`
1329
1393
  * gets one per shard plus one for the tests nextest.toml singles out.
1330
1394
  *
1331
- * `--workspace -E 'package(X)'` rather than `--package X`. A filterset selects
1332
- * what RUNS; `--package` also re-resolves FEATURES for that crate alone, which
1333
- * fingerprints differently from the `cargo test --workspace --no-run` that
1334
- * `cargo-test-compile` already paid for, so cargo rebuilds the divergent half
1335
- * inside the bounded window. Measured on a hosted 3-core macOS runner that
1336
- * rebuild was 57.9s of a 120s budget — the tests were killed with 264 still to
1337
- * run while nothing was wrong with them. The two forms select the same tests;
1338
- * only the filterset reuses the compile target's artifacts.
1395
+ * Every piece EXECUTES, it never builds: `--archive-file` runs the binaries
1396
+ * `cargo-test-archive` compiled once for the whole workspace, extracted into
1397
+ * the runner's own temporary directory. That is what removed the two costs the
1398
+ * old shape could not avoid. `nextest run --workspace -E 'package(X)'` re-entered
1399
+ * cargo once per crate, and the `-p X` form re-resolved that crate's features so
1400
+ * cargo rebuilt the divergent half inside the bounded window — measured at 57.9s
1401
+ * of a 120s budget on a hosted 3-core macOS runner, killing the run with 264
1402
+ * tests still to go. Neither form is reachable from an archive: nextest rejects
1403
+ * `--workspace` and `-p` beside `--archive-file`, so the filterset is the only
1404
+ * selector left, and it selects the same tests it always did.
1405
+ *
1406
+ * `--workspace-remap .` is not cosmetic. An archive records the ABSOLUTE paths
1407
+ * of the tree that produced it; without the remap, a restored archive hands
1408
+ * every test the producing tree's `CARGO_MANIFEST_DIR` — measured on a moved
1409
+ * tree, tests then read another checkout's fixtures, or a path that no longer
1410
+ * exists. With it, the runner's own cargo workspace root wins, so the archive
1411
+ * is relocatable and one cache entry serves every checkout of the same commit.
1412
+ * nextest also re-points `CARGO_BIN_EXE_<name>`/`NEXTEST_BIN_EXE_<name>` at the
1413
+ * extracted binaries at RUNTIME (measured), but `env!` reads them at COMPILE
1414
+ * time: a test that spawns `env!("CARGO_BIN_EXE_x")` holds the producing tree's
1415
+ * path and must read the runtime variable instead to survive relocation.
1339
1416
  *
1340
1417
  * nextest.toml singles some tests out with an override, and each such class
1341
1418
  * breaks a shard in its own way — a `test-group` is scoped to one nextest RUN
@@ -1356,21 +1433,13 @@ async function resolveCargoWorkspaces(packageJsonPaths, workspaceRoot) {
1356
1433
  * Detecting that regression requires a separate coverage policy rather than
1357
1434
  * making valid empty crates and shards fail execution.
1358
1435
  *
1359
- * Pieces chain rather than fan out, like the crates do: cargo flocks one
1360
- * `target/`, and chaining keeps even the pinned group from overlapping the
1361
- * shards on a machine-wide resource.
1362
- */
1363
- /**
1364
- * Root mode keeps `--workspace` and adds `-p` only to make the owning crate
1365
- * explicit in the command Nx attributes to its project. Cargo treats
1366
- * `--workspace -p X` as the full workspace selection, so `-p` is inert for
1367
- * feature resolution; the nextest filterset is what narrows the tests that run.
1368
- * Never drop `--workspace`: `-p X` alone re-resolves features for one package
1369
- * and defeats the compile target's shared artifacts.
1436
+ * Pieces fan out rather than chain. The chain existed because every runner was
1437
+ * a cargo writer on one flocked `target/`; an archive run writes nothing there,
1438
+ * so the only serialization left is the one the machine's own scheduler does.
1370
1439
  */
1371
1440
  async function addCargoTestTargets(targets, currentProjectName, projectRoot, workspaceRoot, workspace) {
1372
1441
  const targetNames = [];
1373
- const configFile = nextestConfigRelPath(workspaceRoot, workspace.projectRoot, PLUGIN_NEXTEST_CONFIG);
1442
+ const toolConfig = nextestToolConfigArg(workspaceRoot, workspace.projectRoot, PLUGIN_NEXTEST_CONFIG);
1374
1443
  for (const plan of workspace.packages) {
1375
1444
  if (plan.package.projectRoot !== projectRoot) {
1376
1445
  continue;
@@ -1379,6 +1448,10 @@ async function addCargoTestTargets(targets, currentProjectName, projectRoot, wor
1379
1448
  // A workspace nextest build unifies features across every selected member.
1380
1449
  // A sibling manifest can therefore change this crate's tests without being
1381
1450
  // a path dependency. Track those manifests, not unrelated sibling sources.
1451
+ //
1452
+ // The repository's own nextest config layers OVER the plugin's tool config,
1453
+ // so it decides timeouts, groups and archive contents: a cached verdict that
1454
+ // ignored it would survive the change that invalidates it.
1382
1455
  const inputs = [
1383
1456
  ...new Set([
1384
1457
  ...(await cargoPackageTestInputs({
@@ -1386,31 +1459,37 @@ async function addCargoTestTargets(targets, currentProjectName, projectRoot, wor
1386
1459
  absoluteProjectRoot: join(workspaceRoot, workspace.projectRoot),
1387
1460
  memberDir: plan.package.dir,
1388
1461
  inputRoot,
1462
+ cache: workspace.inputsCache,
1389
1463
  })),
1390
1464
  ...workspace.packages.map((member) => posix.join(inputRoot, member.package.dir, 'Cargo.toml')),
1465
+ posix.join(inputRoot, NEXTEST_REPO_CONFIG_PATH),
1391
1466
  ]),
1392
1467
  {
1393
1468
  runtime: `bun -e 'console.log(new Bun.CryptoHasher("sha256").update(require("node:fs").readFileSync(process.argv[1])).digest("hex"))' '${PLUGIN_NEXTEST_CONFIG.replaceAll("'", "'\"'\"'")}'`,
1394
1469
  },
1395
1470
  ];
1471
+ const archive = cargoTargetDependency(currentProjectName, {
1472
+ projectName: workspace.rootProjectName,
1473
+ targetName: CARGO_TEST_ARCHIVE_TARGET,
1474
+ });
1396
1475
  for (const piece of plan.pieces) {
1397
1476
  targetNames.push(piece.targetName);
1398
1477
  targets[piece.targetName] = {
1399
1478
  executor: '@smoothbricks/nx-plugin:bounded-exec',
1400
1479
  cache: true,
1401
1480
  inputs,
1402
- dependsOn: [cargoTargetDependency(currentProjectName, piece.previous)],
1481
+ dependsOn: [archive],
1403
1482
  options: {
1404
- command: cargoFrozen(`nextest run --workspace${workspace.projectRoot === '.' ? ` -p ${plan.package.name}` : ''} -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none --config-file ${configFile}`),
1483
+ command: cargoFrozen(`nextest run --archive-file ${CARGO_TEST_ARCHIVE_FILE} --workspace-remap . -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none ${toolConfig}`),
1405
1484
  cwd: workspace.projectRoot,
1406
1485
  timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
1407
1486
  killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
1408
1487
  },
1409
- configurations: {
1410
- production: {
1411
- command: cargoFrozen(`nextest run --workspace${workspace.projectRoot === '.' ? ` -p ${plan.package.name}` : ''} --release -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none --config-file ${configFile}`),
1412
- },
1413
- },
1488
+ // The archive already holds the profile it was built with, and nextest
1489
+ // rejects `--release` beside `--archive-file`. Nx propagates the
1490
+ // configuration to this target's dependencies, so `:production` builds
1491
+ // the release archive and this command runs exactly what it finds.
1492
+ configurations: { production: {} },
1414
1493
  };
1415
1494
  }
1416
1495
  }
package/nextest.toml CHANGED
@@ -34,8 +34,14 @@ slow-timeout = { period = "180s", terminate-after = 1 }
34
34
  # test joins the group by being named for what it touches. A bare `real_*` filter
35
35
  # would be wrong here — unrelated `real_*` tests exist in columine-arrow,
36
36
  # cowshed-cli and cowshed-gateway that touch no disk image at all.
37
+ #
38
+ # The `@tool:smoo:` prefix is not decoration: this file is passed as
39
+ # `--tool-config-file smoo:...` so a repository's own `.config/nextest.toml`
40
+ # can override it, and nextest REFUSES a tool config whose groups are not
41
+ # namespaced ("test groups must start with '@tool:<tool-name>:'") precisely so
42
+ # a tool cannot collide with a repository's group of the same name.
37
43
  [test-groups]
38
- real-apfs = { max-threads = 1 }
44
+ "@tool:smoo:real-apfs" = { max-threads = 1 }
39
45
 
40
46
  # The full-lifecycle real-APFS tests cannot meet the default deadline, and
41
47
  # `specs/cowshed/08_testing.md` already contains the arithmetic that proves it.
@@ -72,8 +78,8 @@ real-apfs = { max-threads = 1 }
72
78
  [[profile.default.overrides]]
73
79
  filter = 'test(/real_apfs_.*_substrate_lifecycle/)'
74
80
  slow-timeout = { period = "60s", terminate-after = 1 }
75
- test-group = 'real-apfs'
81
+ test-group = '@tool:smoo:real-apfs'
76
82
 
77
83
  [[profile.default.overrides]]
78
84
  filter = 'test(/real_apfs_/)'
79
- test-group = 'real-apfs'
85
+ test-group = '@tool:smoo:real-apfs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.4.5",
3
+ "version": "0.4.7",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -512,6 +512,73 @@ describe('cargo-test reachability policy', () => {
512
512
  }
513
513
  });
514
514
 
515
+ it('treats the workspace nextest archive as a build, never as a runner or a crate', async () => {
516
+ const root = await mkdtemp(join(tmpdir(), 'cargo-reach-archive-'));
517
+ try {
518
+ await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
519
+ await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
520
+ await mkdir(join(root, 'packages/rusty/crates/core'), { recursive: true });
521
+ await writeFile(join(root, 'packages/rusty/Cargo.toml'), '[workspace]\nmembers = ["crates/core"]\n');
522
+ await writeFile(join(root, 'packages/rusty/crates/core/Cargo.toml'), '[package]\nname = "rusty-core"\n');
523
+
524
+ // `cargo nextest archive` compiles test binaries and packs them. It is
525
+ // the same silent green as `--no-run`: the word "nextest" appears, no
526
+ // test runs. And `cargo-test-archive` is not a crate called "archive".
527
+ const archiveOptions = new Map<string, Record<string, unknown>>([
528
+ [
529
+ 'cargo-test-archive',
530
+ { command: 'cargo --frozen nextest archive --workspace --archive-file target/nextest/archive.tar.zst' },
531
+ ],
532
+ ]);
533
+ const archiveOnly: ResolvedProjectTargets = {
534
+ root: 'packages/rusty',
535
+ targets: new Set(['test', 'cargo-test', 'cargo-test-archive']),
536
+ targetDependencies: new Map([
537
+ ['test', ['cargo-test']],
538
+ ['cargo-test', ['cargo-test-archive']],
539
+ ]),
540
+ targetExecutors: new Map([
541
+ ['test', 'nx:noop'],
542
+ ['cargo-test', 'nx:noop'],
543
+ ['cargo-test-archive', 'nx:run-commands'],
544
+ ]),
545
+ targetOptions: archiveOptions,
546
+ };
547
+ const issues = checkWorkspaceCargoTestReachabilityPolicy(root, {
548
+ resolvedTargetsByProject: new Map([['rusty', archiveOnly]]),
549
+ });
550
+ expect(issues).toHaveLength(1);
551
+ expect(issues[0]?.message).toContain('RUNS tests');
552
+
553
+ const withRunner: ResolvedProjectTargets = {
554
+ ...archiveOnly,
555
+ targets: new Set([...archiveOnly.targets, 'cargo-test-rusty-core']),
556
+ targetDependencies: new Map([
557
+ ['test', ['cargo-test']],
558
+ ['cargo-test', ['cargo-test-rusty-core']],
559
+ ['cargo-test-rusty-core', ['cargo-test-archive']],
560
+ ]),
561
+ targetOptions: new Map([
562
+ ...archiveOptions,
563
+ [
564
+ 'cargo-test-rusty-core',
565
+ {
566
+ command:
567
+ "cargo --frozen nextest run --archive-file target/nextest/archive.tar.zst --workspace-remap . -E 'package(rusty-core)'",
568
+ },
569
+ ],
570
+ ]),
571
+ };
572
+ expect(
573
+ checkWorkspaceCargoTestReachabilityPolicy(root, {
574
+ resolvedTargetsByProject: new Map([['rusty', withRunner]]),
575
+ }),
576
+ ).toEqual([]);
577
+ } finally {
578
+ await rm(root, { recursive: true, force: true });
579
+ }
580
+ });
581
+
515
582
  it('ignores projects with no cargo-test target', async () => {
516
583
  const root = await mkdtemp(join(tmpdir(), 'cargo-reach-ts-'));
517
584
  try {