@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/src/index.ts CHANGED
@@ -16,14 +16,19 @@ import { BOUNDED_TEST_KILL_AFTER_MS, BOUNDED_TEST_TIMEOUT_MS } from './bounded-t
16
16
  import {
17
17
  type AttributedCargoWorkspacePackage,
18
18
  attributeCargoWorkspacePackages,
19
+ CARGO_TEST_ARCHIVE_FILE,
20
+ CARGO_TEST_ARCHIVE_TARGET,
19
21
  CARGO_TEST_COMPILE_TARGET,
20
22
  CARGO_TEST_EXCEPTIONS_SUFFIX,
21
23
  CARGO_TEST_TARGET,
24
+ type CargoInputsCache,
22
25
  cargoPackageTestInputs,
23
26
  cargoTestPackageTargetName,
27
+ createCargoInputsCache,
24
28
  exceptionalTestFilter,
25
29
  listCargoWorkspacePackages,
26
- nextestConfigRelPath,
30
+ NEXTEST_REPO_CONFIG_PATH,
31
+ nextestToolConfigArg,
27
32
  } from './cargo-workspace.js';
28
33
  import {
29
34
  CARGO_CROSS_LINT_COMMAND,
@@ -134,12 +139,7 @@ function hostPlatformTargetNames(targetNames: Iterable<string>, hostPlatform: Na
134
139
  */
135
140
  function buildOutputTargetNames(targetNames: Iterable<string>): string[] {
136
141
  return [...new Set(targetNames)]
137
- .filter(
138
- (name) =>
139
- BUILD_OUTPUT_TARGET_PATTERN.test(name) &&
140
- !name.startsWith(`${CARGO_TEST_TARGET}-`) &&
141
- !name.startsWith('cargo-lint-'),
142
- )
142
+ .filter((name) => BUILD_OUTPUT_TARGET_PATTERN.test(name) && !name.startsWith(`${CARGO_TEST_TARGET}-`))
143
143
  .sort();
144
144
  }
145
145
 
@@ -182,6 +182,21 @@ function cargoRuntimeInput(projectRoot: string, command: string): { runtime: str
182
182
  const quotedRoot = `'${projectRoot.replaceAll("'", "'\"'\"'")}'`;
183
183
  return { runtime: `cd ${quotedRoot} && ${command}` };
184
184
  }
185
+
186
+ /**
187
+ * Everything a target actually runs, as one string. `nx:run-commands` accepts
188
+ * either spelling and inference asks four questions of the text — does it run
189
+ * frozen cargo, which tool's version keys its cache, which target directories
190
+ * does it write — so the two shapes collapse here once rather than at each ask.
191
+ */
192
+ function targetCommandText(target: TargetConfiguration): string {
193
+ const command: unknown = target.options?.command;
194
+ const commands: unknown = target.options?.commands;
195
+ return [
196
+ typeof command === 'string' ? command : '',
197
+ ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
198
+ ].join('\n');
199
+ }
185
200
  const NAPI_INPUTS = CARGO_OUTPUT_INPUTS;
186
201
 
187
202
  interface NapiTargetConvention extends NapiPlatform {
@@ -276,14 +291,20 @@ function createCargoWasmTarget(
276
291
  * which is not a property of the tests. `cargo test --no-run` pays that cost
277
292
  * in its own unbounded target. Cargo reuses incremental compilation locally;
278
293
  * Nx cannot cache this warming action without restoring shared mutable state.
279
- * The bounded runner then uses the warm directory for the suites' own runtime.
280
294
  *
281
295
  * Cargo flocks one `target/` per invocation. Nx must not run two cargo
282
296
  * writers on that directory at once — that is a mutex, not a deadlock, and
283
297
  * the second process sits in "Blocking waiting for file lock" until a
284
298
  * timeout. Inference serializes writers that share the default target dir
285
- * (`napi-debug` after compile, `cargo-test` after `napi-debug`). Clippy uses
286
- * its own `--target-dir` so lint can overlap tests.
299
+ * (`napi-debug` after compile).
300
+ *
301
+ * Clippy writes ONE shared `--target-dir target/cargo-lint` rather than a
302
+ * directory per crate. Per-crate dirs were never about overlap: clippy of a
303
+ * crate is a check build of its whole closure, so 49 crates meant 49 closure
304
+ * rebuilds and 11 GiB of target directories on one repository, and no
305
+ * per-crate cache hit ever repaid that. Sharing the directory costs nothing:
306
+ * cargo's own lock serializes the invocations that use it and each dependency
307
+ * artifact is then compiled once for all of them.
287
308
  */
288
309
  function createCargoTestCompileTarget(projectRoot: string): TargetConfiguration {
289
310
  return {
@@ -301,6 +322,45 @@ function createCargoTestCompileTarget(projectRoot: string): TargetConfiguration
301
322
  };
302
323
  }
303
324
 
325
+ /**
326
+ * The workspace's test binaries, compiled once and packed into one file the
327
+ * per-crate runners execute from.
328
+ *
329
+ * This is the only cargo BUILD in the test graph. Before it, every per-crate
330
+ * target ran `nextest run --workspace -E 'package(X)'`, which is a workspace
331
+ * build with a filter: 32 such tasks on one repository each re-entered cargo,
332
+ * re-resolved features and chained behind one another so the flocked `target/`
333
+ * had a single writer. Reading from an archive removes the build from the run
334
+ * entirely — the runner extracts binaries into its own temporary directory and
335
+ * touches no shared state — which is what lets the runners fan out.
336
+ *
337
+ * Cached, unlike `cargo-test-compile`, because it produces a VALUE: one file,
338
+ * not a mutable build tree Nx would have to restore. `--workspace` matches the
339
+ * clippy selection exactly, so both gates unify features the same way.
340
+ */
341
+ function createCargoTestArchiveTarget(projectRoot: string, toolConfig: string): TargetConfiguration {
342
+ const archiveCommand = (profile: string): string =>
343
+ cargoFrozen(
344
+ `nextest archive --workspace${profile} --archive-file ${CARGO_TEST_ARCHIVE_FILE} --user-config-file none ${toolConfig}`,
345
+ );
346
+ return {
347
+ executor: 'nx:run-commands',
348
+ cache: true,
349
+ inputs: CARGO_INPUTS,
350
+ outputs: [`{projectRoot}/${CARGO_TEST_ARCHIVE_FILE}`],
351
+ options: {
352
+ commands: [`mkdir -p ${posix.dirname(CARGO_TEST_ARCHIVE_FILE)}`, archiveCommand('')],
353
+ cwd: projectRoot,
354
+ parallel: false,
355
+ },
356
+ configurations: {
357
+ production: {
358
+ commands: [`mkdir -p ${posix.dirname(CARGO_TEST_ARCHIVE_FILE)}`, archiveCommand(' --release')],
359
+ },
360
+ },
361
+ };
362
+ }
363
+
304
364
  function createCargoTestTarget(projectRoot: string): TargetConfiguration {
305
365
  return {
306
366
  executor: '@smoothbricks/nx-plugin:bounded-exec',
@@ -621,13 +681,33 @@ async function createProjectTargets(
621
681
  workspaceRoot,
622
682
  absoluteProjectRoot: join(workspaceRoot, cargoWorkspaceRoot),
623
683
  memberDir: plan.package.dir,
684
+ cache: cargoWorkspace.inputsCache,
624
685
  }),
625
686
  ),
626
687
  )
627
688
  ).flat(),
628
689
  ),
629
690
  ];
630
- targets[CARGO_TEST_COMPILE_TARGET].inputs = workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS;
691
+ // Generated crate sources (wire codecs, vocabularies, fixtures) are
692
+ // gitignored and invisible to nx's file hasher; they reach these targets
693
+ // as the outputs of the targets a repository adds to `dependsOn`. Hashing
694
+ // those outputs here means a repository never has to restate `inputs` to
695
+ // add them - and a restated `inputs` REPLACES this union with the root
696
+ // project's `default`, which owns no crate file, so the archive then
697
+ // cache-hits against edited crates while every per-crate test reruns on
698
+ // the stale binary. That verdict is wrong, not merely slow.
699
+ const workspaceCargoInputs: TargetConfiguration['inputs'] = [
700
+ ...(workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS),
701
+ { dependentTasksOutputFiles: '**/*', transitive: false },
702
+ ];
703
+ targets[CARGO_TEST_COMPILE_TARGET].inputs = workspaceCargoInputs;
704
+ targets[CARGO_TEST_ARCHIVE_TARGET] = createCargoTestArchiveTarget(
705
+ cargoWorkspaceRoot,
706
+ nextestToolConfigArg(workspaceRoot, cargoWorkspaceRoot, PLUGIN_NEXTEST_CONFIG),
707
+ );
708
+ // `archive.include` lives in the repository's nextest config, so that file
709
+ // decides what the archive CONTAINS, not merely how a run behaves.
710
+ targets[CARGO_TEST_ARCHIVE_TARGET].inputs = [...workspaceCargoInputs, `{projectRoot}/${NEXTEST_REPO_CONFIG_PATH}`];
631
711
  const aggregateDependencies = cargoWorkspace.packages.flatMap((plan) =>
632
712
  plan.pieces.map((piece) =>
633
713
  cargoTargetDependency(projectName, {
@@ -640,20 +720,24 @@ async function createProjectTargets(
640
720
  aggregateDependencies.length > 0
641
721
  ? { executor: 'nx:noop', cache: true, outputs: [], dependsOn: aggregateDependencies }
642
722
  : createCargoTestTarget(cargoWorkspaceRoot);
643
- if (cargoWorkspace.packages.length === 0) {
644
- targets['cargo-lint'] = {
645
- executor: 'nx:run-commands',
646
- cache: true,
647
- inputs: CARGO_INPUTS,
648
- outputs: [],
649
- options: {
650
- commands: ['cargo fmt --all --check', CARGO_LINT_CLIPPY_COMMAND],
651
- cwd: cargoWorkspaceRoot,
652
- parallel: false,
653
- },
654
- };
655
- validationTargets.push('cargo-lint');
656
- }
723
+ // One clippy over the whole workspace, in one target dir. Clippy of a crate
724
+ // is a check build of its closure; a target dir per crate compiled every
725
+ // shared dependency once per crate (49 closures, 11 GiB on one repository)
726
+ // and no per-crate cache hit ever repaid that. The workspace inputs are
727
+ // exactly the union of every crate's, so the verdict caches on the same
728
+ // key set the compile does. Non-root packages depend on this target.
729
+ targets['cargo-lint'] = {
730
+ executor: 'nx:run-commands',
731
+ cache: true,
732
+ inputs: workspaceCargoInputs,
733
+ outputs: [],
734
+ options: {
735
+ commands: ['cargo fmt --all --check', CARGO_LINT_CLIPPY_COMMAND],
736
+ cwd: cargoWorkspaceRoot,
737
+ parallel: false,
738
+ },
739
+ };
740
+ validationTargets.push('cargo-lint');
657
741
  // The Linux arm of `cargo-lint`, as its own target. Rationale for the name,
658
742
  // the command and the absent cross test leg lives in ./cross-check-policy.ts.
659
743
  //
@@ -684,7 +768,7 @@ async function createProjectTargets(
684
768
  targets[CARGO_CROSS_LINT_TARGET] = {
685
769
  executor: 'nx:run-commands',
686
770
  cache: true,
687
- inputs: workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS,
771
+ inputs: workspaceCargoInputs,
688
772
  outputs: [],
689
773
  options: {
690
774
  command: CARGO_CROSS_LINT_COMMAND,
@@ -738,43 +822,19 @@ async function createProjectTargets(
738
822
  dependsOn: packageTargetNames,
739
823
  };
740
824
  }
741
- for (const plan of cargoPackagePlans) {
742
- const name = plan.package.name;
743
- targets[`cargo-lint-${name}`] = {
744
- executor: 'nx:run-commands',
745
- cache: true,
746
- inputs: await cargoPackageTestInputs({
747
- workspaceRoot,
748
- absoluteProjectRoot: join(workspaceRoot, cargoWorkspace.projectRoot),
749
- memberDir: plan.package.dir,
750
- inputRoot:
751
- projectRoot === cargoWorkspace.projectRoot
752
- ? '{projectRoot}'
753
- : posix.join('{workspaceRoot}', cargoWorkspace.projectRoot),
754
- }),
755
- outputs: [],
756
- options: {
757
- commands: [
758
- `cargo fmt -p ${name} --check`,
759
- cargoFrozen(`clippy -p ${name} --all-targets --target-dir target/cargo-lint-${name} -- -D warnings`),
760
- ],
761
- cwd: cargoWorkspace.projectRoot,
762
- parallel: false,
763
- },
764
- };
765
- }
766
- const lintPlans = isCargoWorkspaceRoot ? cargoWorkspace.packages : cargoPackagePlans;
767
- if (lintPlans.length > 0) {
825
+ if (!isCargoWorkspaceRoot && cargoPackagePlans.length > 0) {
826
+ // The workspace root runs clippy for every member; a package owning
827
+ // crates validates through that one verdict.
768
828
  targets['cargo-lint'] = {
769
829
  executor: 'nx:noop',
770
830
  cache: true,
771
831
  outputs: [],
772
- dependsOn: lintPlans.map((plan) =>
832
+ dependsOn: [
773
833
  cargoTargetDependency(projectName, {
774
- projectName: plan.package.projectName,
775
- targetName: `cargo-lint-${plan.package.name}`,
834
+ projectName: cargoWorkspace.rootProjectName,
835
+ targetName: 'cargo-lint',
776
836
  }),
777
- ),
837
+ ],
778
838
  };
779
839
  validationTargets.push('cargo-lint');
780
840
  }
@@ -853,12 +913,7 @@ async function createProjectTargets(
853
913
  if (name === CARGO_FETCH_TARGET) {
854
914
  continue;
855
915
  }
856
- const command: unknown = target.options?.command;
857
- const commands: unknown = target.options?.commands;
858
- const text = [
859
- typeof command === 'string' ? command : '',
860
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
861
- ].join('\n');
916
+ const text = targetCommandText(target);
862
917
  if (text.includes(CARGO_FROZEN_PREFIX)) {
863
918
  target.dependsOn = [CARGO_FETCH_TARGET, ...(target.dependsOn ?? [])];
864
919
  }
@@ -869,12 +924,7 @@ async function createProjectTargets(
869
924
  targetName: CARGO_FETCH_TARGET,
870
925
  });
871
926
  for (const target of Object.values(targets)) {
872
- const command: unknown = target.options?.command;
873
- const commands: unknown = target.options?.commands;
874
- const text = [
875
- typeof command === 'string' ? command : '',
876
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
877
- ].join('\n');
927
+ const text = targetCommandText(target);
878
928
  if (text.includes(CARGO_FROZEN_PREFIX)) {
879
929
  target.dependsOn = [rootFetch, ...(target.dependsOn ?? [])];
880
930
  }
@@ -884,19 +934,14 @@ async function createProjectTargets(
884
934
  // Cache verdicts and dedicated artifacts, never Cargo's shared mutable build
885
935
  // directories. Resolve tool versions from the command's actual directory.
886
936
  for (const [name, target] of Object.entries(targets)) {
887
- const command: unknown = target.options?.command;
888
- const commands: unknown = target.options?.commands;
889
- const text = [
890
- typeof command === 'string' ? command : '',
891
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
892
- ].join('\n');
937
+ const text = targetCommandText(target);
893
938
  if (!text.includes(CARGO_FROZEN_PREFIX)) continue;
894
939
  target.outputs ??= [];
895
940
  if (!target.cache) continue;
896
941
  const cwd: unknown = target.options?.cwd;
897
942
  const versions = name.startsWith('cargo-lint')
898
943
  ? ` && cargo clippy -V${name === CARGO_CROSS_LINT_TARGET ? '' : ' && cargo fmt --version'}`
899
- : text.includes('nextest run')
944
+ : text.includes('nextest ')
900
945
  ? ' && cargo nextest --version'
901
946
  : '';
902
947
  target.inputs = [
@@ -907,8 +952,7 @@ async function createProjectTargets(
907
952
  }
908
953
 
909
954
  // Cargo flocks the workspace's default target/. Keep every writer out of the
910
- // bounded test window by placing N-API debug builds on the same serialized
911
- // chain as the root compile and per-crate runners.
955
+ // bounded test window by placing N-API debug builds behind the root compile.
912
956
  const cargoTestCompileDependency: CargoTargetDependency | null = targets[CARGO_TEST_COMPILE_TARGET]
913
957
  ? CARGO_TEST_COMPILE_TARGET
914
958
  : isCargoProject && cargoWorkspace
@@ -925,17 +969,18 @@ async function createProjectTargets(
925
969
  }
926
970
  }
927
971
  if (napiDebug && cargoWorkspace) {
928
- const firstCargoTest = cargoPackagePlans.flatMap((plan) => plan.pieces)[0];
929
- const firstTarget = firstCargoTest ? targets[firstCargoTest.targetName] : undefined;
930
- if (firstCargoTest && firstTarget) {
931
- const previous = cargoTargetDependency(projectName, firstCargoTest.previous);
932
- const debugDependencies = napiDebug.dependsOn ?? [];
933
- if (!hasCargoTargetDependency(debugDependencies, previous)) {
934
- napiDebug.dependsOn = [...debugDependencies, previous];
972
+ // A crate whose project also builds the debug cdylib runs its tests after
973
+ // that build: the tests dlopen what `napi-debug` writes into `target/debug`,
974
+ // and an archived test binary still loads it from this tree. The old serial
975
+ // chain gave the same guarantee by accident, to whichever crate happened to
976
+ // be first; this states it for exactly the crates that own the artifact.
977
+ for (const plan of cargoPackagePlans) {
978
+ for (const piece of plan.pieces) {
979
+ const target = targets[piece.targetName];
980
+ if (target && !hasCargoTargetDependency(target.dependsOn ?? [], 'napi-debug')) {
981
+ target.dependsOn = [...(target.dependsOn ?? []), 'napi-debug'];
982
+ }
935
983
  }
936
- firstTarget.dependsOn = (firstTarget.dependsOn ?? []).map((dependency) =>
937
- hasCargoTargetDependency([dependency], previous) ? 'napi-debug' : dependency,
938
- );
939
984
  }
940
985
  } else if (napiDebug) {
941
986
  for (const [name, target] of Object.entries(targets)) {
@@ -1022,10 +1067,42 @@ async function createProjectTargets(
1022
1067
  ],
1023
1068
  };
1024
1069
  }
1025
- if (hasAnyBuildOutputTarget) {
1070
+ // Every cargo build directory this project's own commands write. `clean`
1071
+ // cannot learn them from `outputs`: a clippy or nextest target dir is a
1072
+ // multi-GB build tree that must never enter the Nx cache (`target/cargo-lint`
1073
+ // alone was 11 GiB when each of 49 crates had its own), so the targets
1074
+ // deliberately declare `outputs: []`. Reading `--target-dir` back off the
1075
+ // commands means the list cannot drift from what actually gets written, and a
1076
+ // directory is named relative to the command's own cwd, not the project root.
1077
+ const cargoBuildDirs = [
1078
+ ...new Set(
1079
+ Object.entries(targets).flatMap(([name, target]) => {
1080
+ const cwd: unknown = target.options?.cwd;
1081
+ const base = typeof cwd === 'string' ? cwd : projectRoot;
1082
+ const dirs = [...targetCommandText(target).matchAll(/--target-dir\s+(\S+)/g)].flatMap((match) =>
1083
+ match[1] === undefined ? [] : [match[1]],
1084
+ );
1085
+ if (name === CARGO_TEST_ARCHIVE_TARGET) {
1086
+ dirs.push(posix.dirname(CARGO_TEST_ARCHIVE_FILE));
1087
+ }
1088
+ return dirs.map((dir) => `{workspaceRoot}/${posix.normalize(posix.join(base, dir))}`);
1089
+ }),
1090
+ ),
1091
+ ].sort();
1092
+ if (hasAnyBuildOutputTarget || cargoBuildDirs.length > 0) {
1093
+ const declaredBuild = declaredTargets.build;
1094
+ const declaredBuildOutputs =
1095
+ isRecord(declaredBuild) && Array.isArray(declaredBuild.outputs)
1096
+ ? declaredBuild.outputs.filter((output): output is string => typeof output === 'string')
1097
+ : null;
1026
1098
  targets.clean = {
1027
1099
  executor: '@smoothbricks/nx-plugin:clean-outputs',
1028
1100
  cache: false,
1101
+ // Naming any output REPLACES the executor's default, so the build outputs
1102
+ // it would have used are restated here rather than lost.
1103
+ ...(cargoBuildDirs.length > 0
1104
+ ? { options: { outputs: [...(declaredBuildOutputs ?? ['{projectRoot}/dist']), ...cargoBuildDirs] } }
1105
+ : {}),
1029
1106
  };
1030
1107
  }
1031
1108
 
@@ -1501,7 +1578,6 @@ interface CargoTargetRef {
1501
1578
 
1502
1579
  interface CargoTestPiece {
1503
1580
  extra: string;
1504
- previous: CargoTargetRef;
1505
1581
  selector: string;
1506
1582
  targetName: string;
1507
1583
  }
@@ -1515,6 +1591,8 @@ interface CargoWorkspace {
1515
1591
  packages: CargoPackagePlan[];
1516
1592
  rootProjectName: string;
1517
1593
  projectRoot: string;
1594
+ /** One derivation memo for the whole graph computation (see cargoPackageTestInputs). */
1595
+ inputsCache: CargoInputsCache;
1518
1596
  }
1519
1597
 
1520
1598
  type CargoTargetDependency = NonNullable<TargetConfiguration['dependsOn']>[number];
@@ -1559,10 +1637,6 @@ async function resolveCargoWorkspaces(
1559
1637
  projects,
1560
1638
  ).map((pkg) => ({ ...pkg, dir: posix.relative(rootProject.root, pkg.dir) || '.' }));
1561
1639
  const packages: CargoPackagePlan[] = [];
1562
- let previous: CargoTargetRef = {
1563
- projectName: rootProject.name,
1564
- targetName: CARGO_TEST_COMPILE_TARGET,
1565
- };
1566
1640
  const exceptional = exceptionalTestFilter(PLUGIN_NEXTEST_CONFIG);
1567
1641
  for (const pkg of attributed) {
1568
1642
  const pieces: CargoTestPiece[] = [];
@@ -1573,25 +1647,26 @@ async function resolveCargoWorkspaces(
1573
1647
  const targetName = cargoTestPackageTargetName(pkg.name, sharded ? `shard${index}` : undefined);
1574
1648
  pieces.push({
1575
1649
  extra: sharded ? ` --partition hash:${index}/${pkg.testShards}` : '',
1576
- previous,
1577
1650
  selector: shardable,
1578
1651
  targetName,
1579
1652
  });
1580
- previous = { projectName: pkg.projectName, targetName };
1581
1653
  }
1582
1654
  if (pin !== null) {
1583
1655
  const targetName = cargoTestPackageTargetName(pkg.name, CARGO_TEST_EXCEPTIONS_SUFFIX);
1584
1656
  pieces.push({
1585
1657
  extra: '',
1586
- previous,
1587
1658
  selector: `package(${pkg.name}) and (${pin})`,
1588
1659
  targetName,
1589
1660
  });
1590
- previous = { projectName: pkg.projectName, targetName };
1591
1661
  }
1592
1662
  packages.push({ package: pkg, pieces });
1593
1663
  }
1594
- workspaces.push({ packages, rootProjectName: rootProject.name, projectRoot: rootProject.root });
1664
+ workspaces.push({
1665
+ packages,
1666
+ rootProjectName: rootProject.name,
1667
+ projectRoot: rootProject.root,
1668
+ inputsCache: createCargoInputsCache(),
1669
+ });
1595
1670
  }
1596
1671
  return workspaces;
1597
1672
  }
@@ -1600,14 +1675,27 @@ async function resolveCargoWorkspaces(
1600
1675
  * One bounded target per crate; a crate that declares `smoothbricks.test.shards`
1601
1676
  * gets one per shard plus one for the tests nextest.toml singles out.
1602
1677
  *
1603
- * `--workspace -E 'package(X)'` rather than `--package X`. A filterset selects
1604
- * what RUNS; `--package` also re-resolves FEATURES for that crate alone, which
1605
- * fingerprints differently from the `cargo test --workspace --no-run` that
1606
- * `cargo-test-compile` already paid for, so cargo rebuilds the divergent half
1607
- * inside the bounded window. Measured on a hosted 3-core macOS runner that
1608
- * rebuild was 57.9s of a 120s budget — the tests were killed with 264 still to
1609
- * run while nothing was wrong with them. The two forms select the same tests;
1610
- * only the filterset reuses the compile target's artifacts.
1678
+ * Every piece EXECUTES, it never builds: `--archive-file` runs the binaries
1679
+ * `cargo-test-archive` compiled once for the whole workspace, extracted into
1680
+ * the runner's own temporary directory. That is what removed the two costs the
1681
+ * old shape could not avoid. `nextest run --workspace -E 'package(X)'` re-entered
1682
+ * cargo once per crate, and the `-p X` form re-resolved that crate's features so
1683
+ * cargo rebuilt the divergent half inside the bounded window — measured at 57.9s
1684
+ * of a 120s budget on a hosted 3-core macOS runner, killing the run with 264
1685
+ * tests still to go. Neither form is reachable from an archive: nextest rejects
1686
+ * `--workspace` and `-p` beside `--archive-file`, so the filterset is the only
1687
+ * selector left, and it selects the same tests it always did.
1688
+ *
1689
+ * `--workspace-remap .` is not cosmetic. An archive records the ABSOLUTE paths
1690
+ * of the tree that produced it; without the remap, a restored archive hands
1691
+ * every test the producing tree's `CARGO_MANIFEST_DIR` — measured on a moved
1692
+ * tree, tests then read another checkout's fixtures, or a path that no longer
1693
+ * exists. With it, the runner's own cargo workspace root wins, so the archive
1694
+ * is relocatable and one cache entry serves every checkout of the same commit.
1695
+ * nextest also re-points `CARGO_BIN_EXE_<name>`/`NEXTEST_BIN_EXE_<name>` at the
1696
+ * extracted binaries at RUNTIME (measured), but `env!` reads them at COMPILE
1697
+ * time: a test that spawns `env!("CARGO_BIN_EXE_x")` holds the producing tree's
1698
+ * path and must read the runtime variable instead to survive relocation.
1611
1699
  *
1612
1700
  * nextest.toml singles some tests out with an override, and each such class
1613
1701
  * breaks a shard in its own way — a `test-group` is scoped to one nextest RUN
@@ -1628,18 +1716,9 @@ async function resolveCargoWorkspaces(
1628
1716
  * Detecting that regression requires a separate coverage policy rather than
1629
1717
  * making valid empty crates and shards fail execution.
1630
1718
  *
1631
- * Pieces chain rather than fan out, like the crates do: cargo flocks one
1632
- * `target/`, and chaining keeps even the pinned group from overlapping the
1633
- * shards on a machine-wide resource.
1634
- */
1635
-
1636
- /**
1637
- * Root mode keeps `--workspace` and adds `-p` only to make the owning crate
1638
- * explicit in the command Nx attributes to its project. Cargo treats
1639
- * `--workspace -p X` as the full workspace selection, so `-p` is inert for
1640
- * feature resolution; the nextest filterset is what narrows the tests that run.
1641
- * Never drop `--workspace`: `-p X` alone re-resolves features for one package
1642
- * and defeats the compile target's shared artifacts.
1719
+ * Pieces fan out rather than chain. The chain existed because every runner was
1720
+ * a cargo writer on one flocked `target/`; an archive run writes nothing there,
1721
+ * so the only serialization left is the one the machine's own scheduler does.
1643
1722
  */
1644
1723
  async function addCargoTestTargets(
1645
1724
  targets: Record<string, TargetConfiguration>,
@@ -1649,7 +1728,7 @@ async function addCargoTestTargets(
1649
1728
  workspace: CargoWorkspace,
1650
1729
  ): Promise<string[]> {
1651
1730
  const targetNames: string[] = [];
1652
- const configFile = nextestConfigRelPath(workspaceRoot, workspace.projectRoot, PLUGIN_NEXTEST_CONFIG);
1731
+ const toolConfig = nextestToolConfigArg(workspaceRoot, workspace.projectRoot, PLUGIN_NEXTEST_CONFIG);
1653
1732
  for (const plan of workspace.packages) {
1654
1733
  if (plan.package.projectRoot !== projectRoot) {
1655
1734
  continue;
@@ -1659,6 +1738,10 @@ async function addCargoTestTargets(
1659
1738
  // A workspace nextest build unifies features across every selected member.
1660
1739
  // A sibling manifest can therefore change this crate's tests without being
1661
1740
  // a path dependency. Track those manifests, not unrelated sibling sources.
1741
+ //
1742
+ // The repository's own nextest config layers OVER the plugin's tool config,
1743
+ // so it decides timeouts, groups and archive contents: a cached verdict that
1744
+ // ignored it would survive the change that invalidates it.
1662
1745
  const inputs: NonNullable<TargetConfiguration['inputs']> = [
1663
1746
  ...new Set([
1664
1747
  ...(await cargoPackageTestInputs({
@@ -1666,35 +1749,39 @@ async function addCargoTestTargets(
1666
1749
  absoluteProjectRoot: join(workspaceRoot, workspace.projectRoot),
1667
1750
  memberDir: plan.package.dir,
1668
1751
  inputRoot,
1752
+ cache: workspace.inputsCache,
1669
1753
  })),
1670
1754
  ...workspace.packages.map((member) => posix.join(inputRoot, member.package.dir, 'Cargo.toml')),
1755
+ posix.join(inputRoot, NEXTEST_REPO_CONFIG_PATH),
1671
1756
  ]),
1672
1757
  {
1673
1758
  runtime: `bun -e 'console.log(new Bun.CryptoHasher("sha256").update(require("node:fs").readFileSync(process.argv[1])).digest("hex"))' '${PLUGIN_NEXTEST_CONFIG.replaceAll("'", "'\"'\"'")}'`,
1674
1759
  },
1675
1760
  ];
1761
+ const archive = cargoTargetDependency(currentProjectName, {
1762
+ projectName: workspace.rootProjectName,
1763
+ targetName: CARGO_TEST_ARCHIVE_TARGET,
1764
+ });
1676
1765
  for (const piece of plan.pieces) {
1677
1766
  targetNames.push(piece.targetName);
1678
1767
  targets[piece.targetName] = {
1679
1768
  executor: '@smoothbricks/nx-plugin:bounded-exec',
1680
1769
  cache: true,
1681
1770
  inputs,
1682
- dependsOn: [cargoTargetDependency(currentProjectName, piece.previous)],
1771
+ dependsOn: [archive],
1683
1772
  options: {
1684
1773
  command: cargoFrozen(
1685
- `nextest run --workspace${workspace.projectRoot === '.' ? ` -p ${plan.package.name}` : ''} -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none --config-file ${configFile}`,
1774
+ `nextest run --archive-file ${CARGO_TEST_ARCHIVE_FILE} --workspace-remap . -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none ${toolConfig}`,
1686
1775
  ),
1687
1776
  cwd: workspace.projectRoot,
1688
1777
  timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
1689
1778
  killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
1690
1779
  },
1691
- configurations: {
1692
- production: {
1693
- command: cargoFrozen(
1694
- `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}`,
1695
- ),
1696
- },
1697
- },
1780
+ // The archive already holds the profile it was built with, and nextest
1781
+ // rejects `--release` beside `--archive-file`. Nx propagates the
1782
+ // configuration to this target's dependencies, so `:production` builds
1783
+ // the release archive and this command runs exactly what it finds.
1784
+ configurations: { production: {} },
1698
1785
  };
1699
1786
  }
1700
1787
  }