@smoothbricks/nx-plugin 0.4.4 → 0.4.6

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,
@@ -34,6 +39,7 @@ import {
34
39
  CARGO_LINT_CLIPPY_COMMAND,
35
40
  cargoFrozen,
36
41
  } from './cross-check-policy.js';
42
+ import { isNonSourceDirectory } from './source-directories.js';
37
43
  import { PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
38
44
 
39
45
  export { CARGO_TEST_COMPILE_TARGET };
@@ -61,7 +67,7 @@ async function javaScriptSourceFiles(directory: string): Promise<string[]> {
61
67
  const files: string[] = [];
62
68
  for (const entry of await readdir(directory, { withFileTypes: true })) {
63
69
  if (entry.isDirectory()) {
64
- if (entry.name !== 'node_modules' && entry.name !== 'target' && entry.name !== 'dist') {
70
+ if (!isNonSourceDirectory(entry.name)) {
65
71
  files.push(...(await javaScriptSourceFiles(join(directory, entry.name))));
66
72
  }
67
73
  } else if (entry.isFile() && /\.[cm]?[jt]sx?$/.test(entry.name)) {
@@ -133,12 +139,7 @@ function hostPlatformTargetNames(targetNames: Iterable<string>, hostPlatform: Na
133
139
  */
134
140
  function buildOutputTargetNames(targetNames: Iterable<string>): string[] {
135
141
  return [...new Set(targetNames)]
136
- .filter(
137
- (name) =>
138
- BUILD_OUTPUT_TARGET_PATTERN.test(name) &&
139
- !name.startsWith(`${CARGO_TEST_TARGET}-`) &&
140
- !name.startsWith('cargo-lint-'),
141
- )
142
+ .filter((name) => BUILD_OUTPUT_TARGET_PATTERN.test(name) && !name.startsWith(`${CARGO_TEST_TARGET}-`))
142
143
  .sort();
143
144
  }
144
145
 
@@ -181,6 +182,21 @@ function cargoRuntimeInput(projectRoot: string, command: string): { runtime: str
181
182
  const quotedRoot = `'${projectRoot.replaceAll("'", "'\"'\"'")}'`;
182
183
  return { runtime: `cd ${quotedRoot} && ${command}` };
183
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
+ }
184
200
  const NAPI_INPUTS = CARGO_OUTPUT_INPUTS;
185
201
 
186
202
  interface NapiTargetConvention extends NapiPlatform {
@@ -275,14 +291,20 @@ function createCargoWasmTarget(
275
291
  * which is not a property of the tests. `cargo test --no-run` pays that cost
276
292
  * in its own unbounded target. Cargo reuses incremental compilation locally;
277
293
  * Nx cannot cache this warming action without restoring shared mutable state.
278
- * The bounded runner then uses the warm directory for the suites' own runtime.
279
294
  *
280
295
  * Cargo flocks one `target/` per invocation. Nx must not run two cargo
281
296
  * writers on that directory at once — that is a mutex, not a deadlock, and
282
297
  * the second process sits in "Blocking waiting for file lock" until a
283
298
  * timeout. Inference serializes writers that share the default target dir
284
- * (`napi-debug` after compile, `cargo-test` after `napi-debug`). Clippy uses
285
- * 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.
286
308
  */
287
309
  function createCargoTestCompileTarget(projectRoot: string): TargetConfiguration {
288
310
  return {
@@ -300,6 +322,45 @@ function createCargoTestCompileTarget(projectRoot: string): TargetConfiguration
300
322
  };
301
323
  }
302
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
+
303
364
  function createCargoTestTarget(projectRoot: string): TargetConfiguration {
304
365
  return {
305
366
  executor: '@smoothbricks/nx-plugin:bounded-exec',
@@ -620,6 +681,7 @@ async function createProjectTargets(
620
681
  workspaceRoot,
621
682
  absoluteProjectRoot: join(workspaceRoot, cargoWorkspaceRoot),
622
683
  memberDir: plan.package.dir,
684
+ cache: cargoWorkspace.inputsCache,
623
685
  }),
624
686
  ),
625
687
  )
@@ -627,6 +689,16 @@ async function createProjectTargets(
627
689
  ),
628
690
  ];
629
691
  targets[CARGO_TEST_COMPILE_TARGET].inputs = workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS;
692
+ targets[CARGO_TEST_ARCHIVE_TARGET] = createCargoTestArchiveTarget(
693
+ cargoWorkspaceRoot,
694
+ nextestToolConfigArg(workspaceRoot, cargoWorkspaceRoot, PLUGIN_NEXTEST_CONFIG),
695
+ );
696
+ // `archive.include` lives in the repository's nextest config, so that file
697
+ // decides what the archive CONTAINS, not merely how a run behaves.
698
+ targets[CARGO_TEST_ARCHIVE_TARGET].inputs = [
699
+ ...(workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS),
700
+ `{projectRoot}/${NEXTEST_REPO_CONFIG_PATH}`,
701
+ ];
630
702
  const aggregateDependencies = cargoWorkspace.packages.flatMap((plan) =>
631
703
  plan.pieces.map((piece) =>
632
704
  cargoTargetDependency(projectName, {
@@ -639,20 +711,24 @@ async function createProjectTargets(
639
711
  aggregateDependencies.length > 0
640
712
  ? { executor: 'nx:noop', cache: true, outputs: [], dependsOn: aggregateDependencies }
641
713
  : createCargoTestTarget(cargoWorkspaceRoot);
642
- if (cargoWorkspace.packages.length === 0) {
643
- targets['cargo-lint'] = {
644
- executor: 'nx:run-commands',
645
- cache: true,
646
- inputs: CARGO_INPUTS,
647
- outputs: [],
648
- options: {
649
- commands: ['cargo fmt --all --check', CARGO_LINT_CLIPPY_COMMAND],
650
- cwd: cargoWorkspaceRoot,
651
- parallel: false,
652
- },
653
- };
654
- validationTargets.push('cargo-lint');
655
- }
714
+ // One clippy over the whole workspace, in one target dir. Clippy of a crate
715
+ // is a check build of its closure; a target dir per crate compiled every
716
+ // shared dependency once per crate (49 closures, 11 GiB on one repository)
717
+ // and no per-crate cache hit ever repaid that. The workspace inputs are
718
+ // exactly the union of every crate's, so the verdict caches on the same
719
+ // key set the compile does. Non-root packages depend on this target.
720
+ targets['cargo-lint'] = {
721
+ executor: 'nx:run-commands',
722
+ cache: true,
723
+ inputs: workspaceInputs.length > 0 ? workspaceInputs : CARGO_INPUTS,
724
+ outputs: [],
725
+ options: {
726
+ commands: ['cargo fmt --all --check', CARGO_LINT_CLIPPY_COMMAND],
727
+ cwd: cargoWorkspaceRoot,
728
+ parallel: false,
729
+ },
730
+ };
731
+ validationTargets.push('cargo-lint');
656
732
  // The Linux arm of `cargo-lint`, as its own target. Rationale for the name,
657
733
  // the command and the absent cross test leg lives in ./cross-check-policy.ts.
658
734
  //
@@ -737,43 +813,19 @@ async function createProjectTargets(
737
813
  dependsOn: packageTargetNames,
738
814
  };
739
815
  }
740
- for (const plan of cargoPackagePlans) {
741
- const name = plan.package.name;
742
- targets[`cargo-lint-${name}`] = {
743
- executor: 'nx:run-commands',
744
- cache: true,
745
- inputs: await cargoPackageTestInputs({
746
- workspaceRoot,
747
- absoluteProjectRoot: join(workspaceRoot, cargoWorkspace.projectRoot),
748
- memberDir: plan.package.dir,
749
- inputRoot:
750
- projectRoot === cargoWorkspace.projectRoot
751
- ? '{projectRoot}'
752
- : posix.join('{workspaceRoot}', cargoWorkspace.projectRoot),
753
- }),
754
- outputs: [],
755
- options: {
756
- commands: [
757
- `cargo fmt -p ${name} --check`,
758
- cargoFrozen(`clippy -p ${name} --all-targets --target-dir target/cargo-lint-${name} -- -D warnings`),
759
- ],
760
- cwd: cargoWorkspace.projectRoot,
761
- parallel: false,
762
- },
763
- };
764
- }
765
- const lintPlans = isCargoWorkspaceRoot ? cargoWorkspace.packages : cargoPackagePlans;
766
- if (lintPlans.length > 0) {
816
+ if (!isCargoWorkspaceRoot && cargoPackagePlans.length > 0) {
817
+ // The workspace root runs clippy for every member; a package owning
818
+ // crates validates through that one verdict.
767
819
  targets['cargo-lint'] = {
768
820
  executor: 'nx:noop',
769
821
  cache: true,
770
822
  outputs: [],
771
- dependsOn: lintPlans.map((plan) =>
823
+ dependsOn: [
772
824
  cargoTargetDependency(projectName, {
773
- projectName: plan.package.projectName,
774
- targetName: `cargo-lint-${plan.package.name}`,
825
+ projectName: cargoWorkspace.rootProjectName,
826
+ targetName: 'cargo-lint',
775
827
  }),
776
- ),
828
+ ],
777
829
  };
778
830
  validationTargets.push('cargo-lint');
779
831
  }
@@ -852,12 +904,7 @@ async function createProjectTargets(
852
904
  if (name === CARGO_FETCH_TARGET) {
853
905
  continue;
854
906
  }
855
- const command: unknown = target.options?.command;
856
- const commands: unknown = target.options?.commands;
857
- const text = [
858
- typeof command === 'string' ? command : '',
859
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
860
- ].join('\n');
907
+ const text = targetCommandText(target);
861
908
  if (text.includes(CARGO_FROZEN_PREFIX)) {
862
909
  target.dependsOn = [CARGO_FETCH_TARGET, ...(target.dependsOn ?? [])];
863
910
  }
@@ -868,12 +915,7 @@ async function createProjectTargets(
868
915
  targetName: CARGO_FETCH_TARGET,
869
916
  });
870
917
  for (const target of Object.values(targets)) {
871
- const command: unknown = target.options?.command;
872
- const commands: unknown = target.options?.commands;
873
- const text = [
874
- typeof command === 'string' ? command : '',
875
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
876
- ].join('\n');
918
+ const text = targetCommandText(target);
877
919
  if (text.includes(CARGO_FROZEN_PREFIX)) {
878
920
  target.dependsOn = [rootFetch, ...(target.dependsOn ?? [])];
879
921
  }
@@ -883,19 +925,14 @@ async function createProjectTargets(
883
925
  // Cache verdicts and dedicated artifacts, never Cargo's shared mutable build
884
926
  // directories. Resolve tool versions from the command's actual directory.
885
927
  for (const [name, target] of Object.entries(targets)) {
886
- const command: unknown = target.options?.command;
887
- const commands: unknown = target.options?.commands;
888
- const text = [
889
- typeof command === 'string' ? command : '',
890
- ...(Array.isArray(commands) ? commands.filter((entry) => typeof entry === 'string') : []),
891
- ].join('\n');
928
+ const text = targetCommandText(target);
892
929
  if (!text.includes(CARGO_FROZEN_PREFIX)) continue;
893
930
  target.outputs ??= [];
894
931
  if (!target.cache) continue;
895
932
  const cwd: unknown = target.options?.cwd;
896
933
  const versions = name.startsWith('cargo-lint')
897
934
  ? ` && cargo clippy -V${name === CARGO_CROSS_LINT_TARGET ? '' : ' && cargo fmt --version'}`
898
- : text.includes('nextest run')
935
+ : text.includes('nextest ')
899
936
  ? ' && cargo nextest --version'
900
937
  : '';
901
938
  target.inputs = [
@@ -906,8 +943,7 @@ async function createProjectTargets(
906
943
  }
907
944
 
908
945
  // Cargo flocks the workspace's default target/. Keep every writer out of the
909
- // bounded test window by placing N-API debug builds on the same serialized
910
- // chain as the root compile and per-crate runners.
946
+ // bounded test window by placing N-API debug builds behind the root compile.
911
947
  const cargoTestCompileDependency: CargoTargetDependency | null = targets[CARGO_TEST_COMPILE_TARGET]
912
948
  ? CARGO_TEST_COMPILE_TARGET
913
949
  : isCargoProject && cargoWorkspace
@@ -924,17 +960,18 @@ async function createProjectTargets(
924
960
  }
925
961
  }
926
962
  if (napiDebug && cargoWorkspace) {
927
- const firstCargoTest = cargoPackagePlans.flatMap((plan) => plan.pieces)[0];
928
- const firstTarget = firstCargoTest ? targets[firstCargoTest.targetName] : undefined;
929
- if (firstCargoTest && firstTarget) {
930
- const previous = cargoTargetDependency(projectName, firstCargoTest.previous);
931
- const debugDependencies = napiDebug.dependsOn ?? [];
932
- if (!hasCargoTargetDependency(debugDependencies, previous)) {
933
- napiDebug.dependsOn = [...debugDependencies, previous];
963
+ // A crate whose project also builds the debug cdylib runs its tests after
964
+ // that build: the tests dlopen what `napi-debug` writes into `target/debug`,
965
+ // and an archived test binary still loads it from this tree. The old serial
966
+ // chain gave the same guarantee by accident, to whichever crate happened to
967
+ // be first; this states it for exactly the crates that own the artifact.
968
+ for (const plan of cargoPackagePlans) {
969
+ for (const piece of plan.pieces) {
970
+ const target = targets[piece.targetName];
971
+ if (target && !hasCargoTargetDependency(target.dependsOn ?? [], 'napi-debug')) {
972
+ target.dependsOn = [...(target.dependsOn ?? []), 'napi-debug'];
973
+ }
934
974
  }
935
- firstTarget.dependsOn = (firstTarget.dependsOn ?? []).map((dependency) =>
936
- hasCargoTargetDependency([dependency], previous) ? 'napi-debug' : dependency,
937
- );
938
975
  }
939
976
  } else if (napiDebug) {
940
977
  for (const [name, target] of Object.entries(targets)) {
@@ -1021,10 +1058,42 @@ async function createProjectTargets(
1021
1058
  ],
1022
1059
  };
1023
1060
  }
1024
- if (hasAnyBuildOutputTarget) {
1061
+ // Every cargo build directory this project's own commands write. `clean`
1062
+ // cannot learn them from `outputs`: a clippy or nextest target dir is a
1063
+ // multi-GB build tree that must never enter the Nx cache (`target/cargo-lint`
1064
+ // alone was 11 GiB when each of 49 crates had its own), so the targets
1065
+ // deliberately declare `outputs: []`. Reading `--target-dir` back off the
1066
+ // commands means the list cannot drift from what actually gets written, and a
1067
+ // directory is named relative to the command's own cwd, not the project root.
1068
+ const cargoBuildDirs = [
1069
+ ...new Set(
1070
+ Object.entries(targets).flatMap(([name, target]) => {
1071
+ const cwd: unknown = target.options?.cwd;
1072
+ const base = typeof cwd === 'string' ? cwd : projectRoot;
1073
+ const dirs = [...targetCommandText(target).matchAll(/--target-dir\s+(\S+)/g)].flatMap((match) =>
1074
+ match[1] === undefined ? [] : [match[1]],
1075
+ );
1076
+ if (name === CARGO_TEST_ARCHIVE_TARGET) {
1077
+ dirs.push(posix.dirname(CARGO_TEST_ARCHIVE_FILE));
1078
+ }
1079
+ return dirs.map((dir) => `{workspaceRoot}/${posix.normalize(posix.join(base, dir))}`);
1080
+ }),
1081
+ ),
1082
+ ].sort();
1083
+ if (hasAnyBuildOutputTarget || cargoBuildDirs.length > 0) {
1084
+ const declaredBuild = declaredTargets.build;
1085
+ const declaredBuildOutputs =
1086
+ isRecord(declaredBuild) && Array.isArray(declaredBuild.outputs)
1087
+ ? declaredBuild.outputs.filter((output): output is string => typeof output === 'string')
1088
+ : null;
1025
1089
  targets.clean = {
1026
1090
  executor: '@smoothbricks/nx-plugin:clean-outputs',
1027
1091
  cache: false,
1092
+ // Naming any output REPLACES the executor's default, so the build outputs
1093
+ // it would have used are restated here rather than lost.
1094
+ ...(cargoBuildDirs.length > 0
1095
+ ? { options: { outputs: [...(declaredBuildOutputs ?? ['{projectRoot}/dist']), ...cargoBuildDirs] } }
1096
+ : {}),
1028
1097
  };
1029
1098
  }
1030
1099
 
@@ -1500,7 +1569,6 @@ interface CargoTargetRef {
1500
1569
 
1501
1570
  interface CargoTestPiece {
1502
1571
  extra: string;
1503
- previous: CargoTargetRef;
1504
1572
  selector: string;
1505
1573
  targetName: string;
1506
1574
  }
@@ -1514,6 +1582,8 @@ interface CargoWorkspace {
1514
1582
  packages: CargoPackagePlan[];
1515
1583
  rootProjectName: string;
1516
1584
  projectRoot: string;
1585
+ /** One derivation memo for the whole graph computation (see cargoPackageTestInputs). */
1586
+ inputsCache: CargoInputsCache;
1517
1587
  }
1518
1588
 
1519
1589
  type CargoTargetDependency = NonNullable<TargetConfiguration['dependsOn']>[number];
@@ -1558,10 +1628,6 @@ async function resolveCargoWorkspaces(
1558
1628
  projects,
1559
1629
  ).map((pkg) => ({ ...pkg, dir: posix.relative(rootProject.root, pkg.dir) || '.' }));
1560
1630
  const packages: CargoPackagePlan[] = [];
1561
- let previous: CargoTargetRef = {
1562
- projectName: rootProject.name,
1563
- targetName: CARGO_TEST_COMPILE_TARGET,
1564
- };
1565
1631
  const exceptional = exceptionalTestFilter(PLUGIN_NEXTEST_CONFIG);
1566
1632
  for (const pkg of attributed) {
1567
1633
  const pieces: CargoTestPiece[] = [];
@@ -1572,25 +1638,26 @@ async function resolveCargoWorkspaces(
1572
1638
  const targetName = cargoTestPackageTargetName(pkg.name, sharded ? `shard${index}` : undefined);
1573
1639
  pieces.push({
1574
1640
  extra: sharded ? ` --partition hash:${index}/${pkg.testShards}` : '',
1575
- previous,
1576
1641
  selector: shardable,
1577
1642
  targetName,
1578
1643
  });
1579
- previous = { projectName: pkg.projectName, targetName };
1580
1644
  }
1581
1645
  if (pin !== null) {
1582
1646
  const targetName = cargoTestPackageTargetName(pkg.name, CARGO_TEST_EXCEPTIONS_SUFFIX);
1583
1647
  pieces.push({
1584
1648
  extra: '',
1585
- previous,
1586
1649
  selector: `package(${pkg.name}) and (${pin})`,
1587
1650
  targetName,
1588
1651
  });
1589
- previous = { projectName: pkg.projectName, targetName };
1590
1652
  }
1591
1653
  packages.push({ package: pkg, pieces });
1592
1654
  }
1593
- workspaces.push({ packages, rootProjectName: rootProject.name, projectRoot: rootProject.root });
1655
+ workspaces.push({
1656
+ packages,
1657
+ rootProjectName: rootProject.name,
1658
+ projectRoot: rootProject.root,
1659
+ inputsCache: createCargoInputsCache(),
1660
+ });
1594
1661
  }
1595
1662
  return workspaces;
1596
1663
  }
@@ -1599,14 +1666,27 @@ async function resolveCargoWorkspaces(
1599
1666
  * One bounded target per crate; a crate that declares `smoothbricks.test.shards`
1600
1667
  * gets one per shard plus one for the tests nextest.toml singles out.
1601
1668
  *
1602
- * `--workspace -E 'package(X)'` rather than `--package X`. A filterset selects
1603
- * what RUNS; `--package` also re-resolves FEATURES for that crate alone, which
1604
- * fingerprints differently from the `cargo test --workspace --no-run` that
1605
- * `cargo-test-compile` already paid for, so cargo rebuilds the divergent half
1606
- * inside the bounded window. Measured on a hosted 3-core macOS runner that
1607
- * rebuild was 57.9s of a 120s budget — the tests were killed with 264 still to
1608
- * run while nothing was wrong with them. The two forms select the same tests;
1609
- * only the filterset reuses the compile target's artifacts.
1669
+ * Every piece EXECUTES, it never builds: `--archive-file` runs the binaries
1670
+ * `cargo-test-archive` compiled once for the whole workspace, extracted into
1671
+ * the runner's own temporary directory. That is what removed the two costs the
1672
+ * old shape could not avoid. `nextest run --workspace -E 'package(X)'` re-entered
1673
+ * cargo once per crate, and the `-p X` form re-resolved that crate's features so
1674
+ * cargo rebuilt the divergent half inside the bounded window — measured at 57.9s
1675
+ * of a 120s budget on a hosted 3-core macOS runner, killing the run with 264
1676
+ * tests still to go. Neither form is reachable from an archive: nextest rejects
1677
+ * `--workspace` and `-p` beside `--archive-file`, so the filterset is the only
1678
+ * selector left, and it selects the same tests it always did.
1679
+ *
1680
+ * `--workspace-remap .` is not cosmetic. An archive records the ABSOLUTE paths
1681
+ * of the tree that produced it; without the remap, a restored archive hands
1682
+ * every test the producing tree's `CARGO_MANIFEST_DIR` — measured on a moved
1683
+ * tree, tests then read another checkout's fixtures, or a path that no longer
1684
+ * exists. With it, the runner's own cargo workspace root wins, so the archive
1685
+ * is relocatable and one cache entry serves every checkout of the same commit.
1686
+ * nextest also re-points `CARGO_BIN_EXE_<name>`/`NEXTEST_BIN_EXE_<name>` at the
1687
+ * extracted binaries at RUNTIME (measured), but `env!` reads them at COMPILE
1688
+ * time: a test that spawns `env!("CARGO_BIN_EXE_x")` holds the producing tree's
1689
+ * path and must read the runtime variable instead to survive relocation.
1610
1690
  *
1611
1691
  * nextest.toml singles some tests out with an override, and each such class
1612
1692
  * breaks a shard in its own way — a `test-group` is scoped to one nextest RUN
@@ -1627,18 +1707,9 @@ async function resolveCargoWorkspaces(
1627
1707
  * Detecting that regression requires a separate coverage policy rather than
1628
1708
  * making valid empty crates and shards fail execution.
1629
1709
  *
1630
- * Pieces chain rather than fan out, like the crates do: cargo flocks one
1631
- * `target/`, and chaining keeps even the pinned group from overlapping the
1632
- * shards on a machine-wide resource.
1633
- */
1634
-
1635
- /**
1636
- * Root mode keeps `--workspace` and adds `-p` only to make the owning crate
1637
- * explicit in the command Nx attributes to its project. Cargo treats
1638
- * `--workspace -p X` as the full workspace selection, so `-p` is inert for
1639
- * feature resolution; the nextest filterset is what narrows the tests that run.
1640
- * Never drop `--workspace`: `-p X` alone re-resolves features for one package
1641
- * and defeats the compile target's shared artifacts.
1710
+ * Pieces fan out rather than chain. The chain existed because every runner was
1711
+ * a cargo writer on one flocked `target/`; an archive run writes nothing there,
1712
+ * so the only serialization left is the one the machine's own scheduler does.
1642
1713
  */
1643
1714
  async function addCargoTestTargets(
1644
1715
  targets: Record<string, TargetConfiguration>,
@@ -1648,7 +1719,7 @@ async function addCargoTestTargets(
1648
1719
  workspace: CargoWorkspace,
1649
1720
  ): Promise<string[]> {
1650
1721
  const targetNames: string[] = [];
1651
- const configFile = nextestConfigRelPath(workspaceRoot, workspace.projectRoot, PLUGIN_NEXTEST_CONFIG);
1722
+ const toolConfig = nextestToolConfigArg(workspaceRoot, workspace.projectRoot, PLUGIN_NEXTEST_CONFIG);
1652
1723
  for (const plan of workspace.packages) {
1653
1724
  if (plan.package.projectRoot !== projectRoot) {
1654
1725
  continue;
@@ -1658,6 +1729,10 @@ async function addCargoTestTargets(
1658
1729
  // A workspace nextest build unifies features across every selected member.
1659
1730
  // A sibling manifest can therefore change this crate's tests without being
1660
1731
  // a path dependency. Track those manifests, not unrelated sibling sources.
1732
+ //
1733
+ // The repository's own nextest config layers OVER the plugin's tool config,
1734
+ // so it decides timeouts, groups and archive contents: a cached verdict that
1735
+ // ignored it would survive the change that invalidates it.
1661
1736
  const inputs: NonNullable<TargetConfiguration['inputs']> = [
1662
1737
  ...new Set([
1663
1738
  ...(await cargoPackageTestInputs({
@@ -1665,35 +1740,39 @@ async function addCargoTestTargets(
1665
1740
  absoluteProjectRoot: join(workspaceRoot, workspace.projectRoot),
1666
1741
  memberDir: plan.package.dir,
1667
1742
  inputRoot,
1743
+ cache: workspace.inputsCache,
1668
1744
  })),
1669
1745
  ...workspace.packages.map((member) => posix.join(inputRoot, member.package.dir, 'Cargo.toml')),
1746
+ posix.join(inputRoot, NEXTEST_REPO_CONFIG_PATH),
1670
1747
  ]),
1671
1748
  {
1672
1749
  runtime: `bun -e 'console.log(new Bun.CryptoHasher("sha256").update(require("node:fs").readFileSync(process.argv[1])).digest("hex"))' '${PLUGIN_NEXTEST_CONFIG.replaceAll("'", "'\"'\"'")}'`,
1673
1750
  },
1674
1751
  ];
1752
+ const archive = cargoTargetDependency(currentProjectName, {
1753
+ projectName: workspace.rootProjectName,
1754
+ targetName: CARGO_TEST_ARCHIVE_TARGET,
1755
+ });
1675
1756
  for (const piece of plan.pieces) {
1676
1757
  targetNames.push(piece.targetName);
1677
1758
  targets[piece.targetName] = {
1678
1759
  executor: '@smoothbricks/nx-plugin:bounded-exec',
1679
1760
  cache: true,
1680
1761
  inputs,
1681
- dependsOn: [cargoTargetDependency(currentProjectName, piece.previous)],
1762
+ dependsOn: [archive],
1682
1763
  options: {
1683
1764
  command: cargoFrozen(
1684
- `nextest run --workspace${workspace.projectRoot === '.' ? ` -p ${plan.package.name}` : ''} -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none --config-file ${configFile}`,
1765
+ `nextest run --archive-file ${CARGO_TEST_ARCHIVE_FILE} --workspace-remap . -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none ${toolConfig}`,
1685
1766
  ),
1686
1767
  cwd: workspace.projectRoot,
1687
1768
  timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
1688
1769
  killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
1689
1770
  },
1690
- configurations: {
1691
- production: {
1692
- command: cargoFrozen(
1693
- `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}`,
1694
- ),
1695
- },
1696
- },
1771
+ // The archive already holds the profile it was built with, and nextest
1772
+ // rejects `--release` beside `--archive-file`. Nx propagates the
1773
+ // configuration to this target's dependencies, so `:production` builds
1774
+ // the release archive and this command runs exactly what it finds.
1775
+ configurations: { production: {} },
1697
1776
  };
1698
1777
  }
1699
1778
  }
@@ -720,6 +720,22 @@ describe('checkPackageTargetPolicyTree', () => {
720
720
  expect(issues.filter((i) => i.message.includes('test files require'))).toEqual([]);
721
721
  });
722
722
 
723
+ it('does not read test files out of build output or dot-directories', () => {
724
+ addProject(tree, 'lib', 'packages/lib');
725
+ writeJsonFile(tree, 'packages/lib/package.json', {
726
+ name: '@scope/lib',
727
+ nx: { name: 'lib' },
728
+ });
729
+ // Stale emit, cargo's tree and a runtime cache all carry test-shaped files; none is a
730
+ // source the package must wire a test target for.
731
+ tree.write('packages/lib/dist-test/example.test.js', 'export {};\n');
732
+ tree.write('packages/lib/target/debug/build/example.test.ts', 'export {};\n');
733
+ tree.write('packages/lib/.runtime/cache-pin/test/example.test.ts', 'export {};\n');
734
+
735
+ const issues = checkPackageTargetPolicyTree(tree);
736
+ expect(issues.filter((i) => i.message.includes('test files require'))).toEqual([]);
737
+ });
738
+
723
739
  it('accepts wildcard aggregate build dependencies', () => {
724
740
  addProject(tree, 'lib', 'packages/lib');
725
741
  writeJsonFile(tree, 'packages/lib/package.json', {
@@ -4,6 +4,7 @@ import { join } from 'node:path';
4
4
  import { getProjects, readJson, type Tree, updateJson } from 'nx/src/devkit-exports.js';
5
5
 
6
6
  import { boundedTestScriptAlias } from './bounded-test-policy.js';
7
+ import { isNonSourceDirectory } from './source-directories.js';
7
8
  import {
8
9
  BUILD_OUTPUT_DEPENDENCIES,
9
10
  LINUX_PLATFORM_TARGET_GLOBS,
@@ -594,7 +595,7 @@ function directoryContainsTestFiles(path: string): boolean {
594
595
  return false;
595
596
  }
596
597
  for (const entry of readdirSync(path, { withFileTypes: true })) {
597
- if (entry.name === 'node_modules' || entry.name === 'dist' || entry.name === 'coverage' || entry.name === '.git') {
598
+ if (isNonSourceDirectory(entry.name)) {
598
599
  continue;
599
600
  }
600
601
  const entryPath = join(path, entry.name);
@@ -956,7 +957,7 @@ function directoryContainsTestFilesTree(tree: Tree, path: string): boolean {
956
957
  return false;
957
958
  }
958
959
  for (const entry of tree.children(path)) {
959
- if (entry === 'node_modules' || entry === 'dist' || entry === 'coverage' || entry === '.git') {
960
+ if (isNonSourceDirectory(entry)) {
960
961
  continue;
961
962
  }
962
963
  const entryPath = `${path}/${entry}`;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Directories no source walk descends into. Every walker in this plugin runs on
3
+ * project load, so descending into a build output is paid on each graph
4
+ * computation: a `target/` at 50k files or a `dist-test/` left by an old config
5
+ * dominated the load. Anything a build writes is excluded by shape rather than
6
+ * by an enumerated name: every `dist*` variant, cargo's `target`, and any
7
+ * dot-directory (`.runtime`, `.cache`, `.venv`, `.git`, tooling state) — a
8
+ * source tree never starts with a dot.
9
+ */
10
+ export function isNonSourceDirectory(name: string): boolean {
11
+ return (
12
+ name.startsWith('.') ||
13
+ name === 'node_modules' ||
14
+ name === 'target' ||
15
+ name === 'coverage' ||
16
+ name === 'build' ||
17
+ name === '__pycache__' ||
18
+ name === 'dist' ||
19
+ name.startsWith('dist-')
20
+ );
21
+ }