@smoothbricks/nx-plugin 0.3.11 → 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +23 -2
  2. package/dist/bounded-test-policy.d.ts +26 -0
  3. package/dist/bounded-test-policy.d.ts.map +1 -1
  4. package/dist/bounded-test-policy.js +194 -0
  5. package/dist/cargo-workspace.d.ts +67 -0
  6. package/dist/cargo-workspace.d.ts.map +1 -0
  7. package/dist/cargo-workspace.js +215 -0
  8. package/dist/cross-check-policy.d.ts +60 -1
  9. package/dist/cross-check-policy.d.ts.map +1 -1
  10. package/dist/cross-check-policy.js +65 -1
  11. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/executor.js +86 -51
  13. package/dist/executors/bounded-exec/schema.d.ts +13 -0
  14. package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
  15. package/dist/index.d.ts +2 -0
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +344 -57
  18. package/dist/package-target-policy.d.ts +2 -1
  19. package/dist/package-target-policy.d.ts.map +1 -1
  20. package/dist/package-target-policy.js +54 -13
  21. package/dist/workspace-config-policy.js +32 -35
  22. package/nextest.toml +42 -0
  23. package/package.json +2 -1
  24. package/src/bounded-test-policy.test.ts +304 -0
  25. package/src/bounded-test-policy.ts +218 -0
  26. package/src/cargo-workspace.ts +243 -0
  27. package/src/cross-check-policy.test.ts +24 -0
  28. package/src/cross-check-policy.ts +80 -1
  29. package/src/executors/bounded-exec/executor.test.ts +147 -3
  30. package/src/executors/bounded-exec/executor.ts +103 -53
  31. package/src/executors/bounded-exec/schema.json +7 -2
  32. package/src/executors/bounded-exec/schema.ts +13 -0
  33. package/src/index.test.ts +373 -25
  34. package/src/index.ts +390 -65
  35. package/src/package-target-policy.test.ts +50 -0
  36. package/src/package-target-policy.ts +58 -9
  37. package/src/workspace-config-policy.test.ts +15 -30
  38. package/src/workspace-config-policy.ts +32 -39
@@ -146,6 +146,35 @@ function targetExistsInResolvedProject(targetName, resolvedTargets) {
146
146
  function isBuildOutputDependencyPattern(dependency) {
147
147
  return BUILD_OUTPUT_DEPENDENCIES.some((pattern) => pattern === dependency);
148
148
  }
149
+ /**
150
+ * `project:target` in dependsOn is Nx's cross-project dependency syntax. When
151
+ * the prefix names a real workspace project, the entry references that
152
+ * project's target — it is not a legacy colon-named local target, so neither
153
+ * migration nor validation may touch it.
154
+ */
155
+ function isCrossProjectTargetDependency(dependency, projectNames) {
156
+ const split = dependency.lastIndexOf(':');
157
+ return split > 0 && projectNames?.has(dependency.slice(0, split)) === true;
158
+ }
159
+ function projectNamesFromOptions(options) {
160
+ return options.resolvedTargetsByProject ? new Set(options.resolvedTargetsByProject.keys()) : undefined;
161
+ }
162
+ /**
163
+ * The resolved-target map alone can be absent (unit-scale callers), so the
164
+ * workspace's declared package project names back it up: a dependsOn prefix is
165
+ * cross-project if either source knows the name.
166
+ */
167
+ function collectWorkspaceProjectNames(root, options) {
168
+ const names = new Set(projectNamesFromOptions(options) ?? []);
169
+ for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
170
+ const pkg = readJsonObject(packageJsonPath);
171
+ const projectName = pkg ? packageNxProjectName(pkg) : null;
172
+ if (projectName) {
173
+ names.add(projectName);
174
+ }
175
+ }
176
+ return names;
177
+ }
149
178
  function expectedTargetDependencies(targetName) {
150
179
  if (targetName === 'preview')
151
180
  return ['build'];
@@ -437,7 +466,7 @@ function directoryContainsTestFiles(path) {
437
466
  }
438
467
  return false;
439
468
  }
440
- function validateExplicitNxTargets(pkg, packagePath, resolvedProject) {
469
+ function validateExplicitNxTargets(pkg, packagePath, resolvedProject, projectNames) {
441
470
  const issues = validatePlatformTargetDependencies(packagePath, resolvedProject);
442
471
  const nx = recordProperty(pkg, 'nx');
443
472
  const targets = nx ? recordProperty(nx, 'targets') : null;
@@ -455,7 +484,7 @@ function validateExplicitNxTargets(pkg, packagePath, resolvedProject) {
455
484
  if (!isRecord(rawTarget)) {
456
485
  continue;
457
486
  }
458
- issues.push(...validateTargetDependencies(rawTarget, `${packagePath}: nx.targets.${targetName}`, resolvedProjectTargetNames(resolvedProject)));
487
+ issues.push(...validateTargetDependencies(rawTarget, `${packagePath}: nx.targets.${targetName}`, resolvedProjectTargetNames(resolvedProject), projectNames));
459
488
  }
460
489
  return issues;
461
490
  }
@@ -513,7 +542,7 @@ function validatePlatformTargetDependencies(packagePath, resolvedProject) {
513
542
  }
514
543
  return issues;
515
544
  }
516
- function validateTargetDependencies(target, label, resolvedTargets) {
545
+ function validateTargetDependencies(target, label, resolvedTargets, projectNames) {
517
546
  if (!Array.isArray(target.dependsOn)) {
518
547
  return [];
519
548
  }
@@ -522,6 +551,9 @@ function validateTargetDependencies(target, label, resolvedTargets) {
522
551
  if (typeof dependency !== 'string') {
523
552
  continue;
524
553
  }
554
+ if (isCrossProjectTargetDependency(dependency, projectNames)) {
555
+ continue;
556
+ }
525
557
  if (dependency.includes(':')) {
526
558
  issues.push({
527
559
  path: label.split(':')[0]?.trim() ?? label,
@@ -760,7 +792,9 @@ function validateTestEntrypointPresenceTree(tree, packagePath, pkg) {
760
792
  export function checkPackageTargetPolicyTree(tree, options = {}) {
761
793
  const issues = [];
762
794
  const workspaceNames = collectWorkspaceNamesTree(tree);
763
- for (const [projectName, config] of getProjects(tree)) {
795
+ const projects = getProjects(tree);
796
+ const projectNames = new Set([...(projectNamesFromOptions(options) ?? []), ...projects.keys()]);
797
+ for (const [projectName, config] of projects) {
764
798
  const pkgPath = `${config.root}/package.json`;
765
799
  if (!tree.exists(pkgPath)) {
766
800
  continue;
@@ -769,7 +803,7 @@ export function checkPackageTargetPolicyTree(tree, options = {}) {
769
803
  const packagePath = config.root;
770
804
  const resolvedProject = options.resolvedTargetsByProject?.get(projectName);
771
805
  const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
772
- issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedProject));
806
+ issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedProject, projectNames));
773
807
  issues.push(...validateTestEntrypointPresenceTree(tree, packagePath, pkg));
774
808
  issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets }));
775
809
  }
@@ -783,7 +817,9 @@ export function checkPackageTargetPolicyTree(tree, options = {}) {
783
817
  export function applyPackageTargetPolicyTree(tree, options = {}) {
784
818
  let changed = false;
785
819
  const workspaceNames = collectWorkspaceNamesTree(tree);
786
- for (const [projectName, config] of getProjects(tree)) {
820
+ const projects = getProjects(tree);
821
+ const projectNames = new Set([...(projectNamesFromOptions(options) ?? []), ...projects.keys()]);
822
+ for (const [projectName, config] of projects) {
787
823
  const pkgPath = `${config.root}/package.json`;
788
824
  if (!tree.exists(pkgPath)) {
789
825
  continue;
@@ -793,7 +829,7 @@ export function applyPackageTargetPolicyTree(tree, options = {}) {
793
829
  const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
794
830
  updateJson(tree, pkgPath, (pkg) => {
795
831
  let packageChanged = migratePackageColonTargets(pkg, resolvedTargets);
796
- packageChanged = rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets) || packageChanged;
832
+ packageChanged = rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets, projectNames) || packageChanged;
797
833
  packageChanged = removePackageColonTargets(pkg) || packageChanged;
798
834
  packageChanged = removeRedundantNoopBuildTarget(pkg, resolvedProject) || packageChanged;
799
835
  packageChanged =
@@ -821,7 +857,7 @@ function removePackageColonTargets(pkg) {
821
857
  }
822
858
  return changed;
823
859
  }
824
- function rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets) {
860
+ function rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets, projectNames) {
825
861
  const nx = recordProperty(pkg, 'nx');
826
862
  const targets = nx ? recordProperty(nx, 'targets') : null;
827
863
  if (!targets) {
@@ -838,6 +874,9 @@ function rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets) {
838
874
  if (typeof dependency !== 'string' || !dependency.includes(':')) {
839
875
  return dependency;
840
876
  }
877
+ if (isCrossProjectTargetDependency(dependency, projectNames)) {
878
+ return dependency;
879
+ }
841
880
  const next = scriptAliases.get(dependency) ?? replacementTargetName(dependency, null, resolvedTargets);
842
881
  if (!next) {
843
882
  return dependency;
@@ -931,12 +970,12 @@ function isResolvedSelfRecursiveScript(scriptName, alias, projectName, resolvedT
931
970
  resolvedProjectTargetExecutor(resolvedTargets, scriptName) === 'nx:run-script' &&
932
971
  resolvedProjectTargetScript(resolvedTargets, scriptName) === scriptName);
933
972
  }
934
- export function checkPackageTargets(pkg, packagePath, resolvedTargets) {
935
- return [...validateExplicitNxTargets(pkg, packagePath, resolvedTargets)];
973
+ export function checkPackageTargets(pkg, packagePath, resolvedTargets, projectNames) {
974
+ return [...validateExplicitNxTargets(pkg, packagePath, resolvedTargets, projectNames)];
936
975
  }
937
976
  export function applyPackageTargets(pkg, packagePath, workspaceNames, options = {}) {
938
977
  let changed = migratePackageColonTargets(pkg, options.resolvedTargets);
939
- changed = rewriteColonTargetDependenciesInPackage(pkg, options.resolvedTargets) || changed;
978
+ changed = rewriteColonTargetDependenciesInPackage(pkg, options.resolvedTargets, options.projectNames) || changed;
940
979
  changed = removePackageColonTargets(pkg) || changed;
941
980
  changed = applyPackageScriptPolicyForPkg(pkg, packagePath, workspaceNames, options) || changed;
942
981
  return changed;
@@ -944,6 +983,7 @@ export function applyPackageTargets(pkg, packagePath, workspaceNames, options =
944
983
  export function checkPackageTargetPolicy(root, options = {}) {
945
984
  const issues = [];
946
985
  const workspaceNames = getWorkspacePackageNames(root);
986
+ const projectNames = collectWorkspaceProjectNames(root, options);
947
987
  for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
948
988
  const pkg = readJsonObject(packageJsonPath);
949
989
  if (!pkg) {
@@ -952,7 +992,7 @@ export function checkPackageTargetPolicy(root, options = {}) {
952
992
  const packagePath = packagePathFromJsonPath(root, packageJsonPath);
953
993
  const projectName = packageNxProjectName(pkg);
954
994
  const resolvedProject = projectName ? options.resolvedTargetsByProject?.get(projectName) : undefined;
955
- issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedProject));
995
+ issues.push(...validateExplicitNxTargets(pkg, packagePath, resolvedProject, projectNames));
956
996
  issues.push(...validateTestEntrypointPresence(root, packagePath, pkg));
957
997
  issues.push(...validatePackageScriptPolicy(pkg, packagePath, workspaceNames, { resolvedTargets: resolvedProject }));
958
998
  }
@@ -961,6 +1001,7 @@ export function checkPackageTargetPolicy(root, options = {}) {
961
1001
  export function applyPackageTargetPolicy(root, options = {}) {
962
1002
  let changed = false;
963
1003
  const workspaceNames = getWorkspacePackageNames(root);
1004
+ const projectNames = collectWorkspaceProjectNames(root, options);
964
1005
  for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
965
1006
  const pkg = readJsonObject(packageJsonPath);
966
1007
  if (!pkg) {
@@ -971,7 +1012,7 @@ export function applyPackageTargetPolicy(root, options = {}) {
971
1012
  const resolvedProject = projectName ? options.resolvedTargetsByProject?.get(projectName) : undefined;
972
1013
  const resolvedTargets = resolvedProjectTargetNames(resolvedProject);
973
1014
  let packageChanged = migratePackageColonTargets(pkg, resolvedTargets);
974
- packageChanged = rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets) || packageChanged;
1015
+ packageChanged = rewriteColonTargetDependenciesInPackage(pkg, resolvedTargets, projectNames) || packageChanged;
975
1016
  packageChanged = removePackageColonTargets(pkg) || packageChanged;
976
1017
  packageChanged = removeRedundantNoopBuildTarget(pkg, resolvedProject) || packageChanged;
977
1018
  packageChanged =
@@ -156,35 +156,35 @@ function validateBuildTargetDefault(nxJson, nxJsonPath, issues) {
156
156
  if (build?.cache !== true) {
157
157
  issues.push({ path: nxJsonPath, message: 'targetDefaults.build.cache must be true' });
158
158
  }
159
- // dependsOn is owned by the inference plugin: the inferred aggregate carries
160
- // the output families PLUS this host's platform-suffixed binary targets, and
161
- // Nx gives targetDefaults precedence over inferred values — a static list
162
- // here would clobber the host-aware edge on every project.
163
- if (build !== null && 'dependsOn' in build) {
164
- issues.push({
165
- path: nxJsonPath,
166
- message: 'targetDefaults.build.dependsOn must not be set; @smoothbricks/nx-plugin infers build dependencies',
167
- });
168
- }
169
- const outputs = build?.outputs;
170
- if (!isValidBuildOutputs(outputs)) {
171
- issues.push({
172
- path: nxJsonPath,
173
- message: 'targetDefaults.build.outputs must contain "{projectRoot}/dist" and only "{projectRoot}/dist-<suffix>" siblings (e.g. "{projectRoot}/dist-node")',
174
- });
159
+ // dependsOn and outputs are both owned by the inference plugin, and Nx gives
160
+ // targetDefaults precedence over inferred values, so anything set here lands
161
+ // on every project regardless of what inference knows about that project.
162
+ //
163
+ // dependsOn: the inferred aggregate carries the output families PLUS this
164
+ // host's platform-suffixed binary targets; a static list here would clobber
165
+ // the host-aware edge.
166
+ //
167
+ // outputs: `build` is an `nx:noop` aggregate that runs no command and writes
168
+ // no file — every artifact under dist belongs to the concrete target that
169
+ // emitted it. A workspace-wide `{projectRoot}/dist` makes the aggregate claim
170
+ // its children's trees, which caches those bytes a second time under the
171
+ // aggregate's hash and, worse, makes `github-ci nx-run-many
172
+ // --collect-outputs` attribute to `build` every file that happens to sit
173
+ // under dist: artifacts of platform targets the collect deliberately
174
+ // excluded, and stale artifacts of a target that did not run at all. The
175
+ // publish workflow then applies two collected trees that both claim the same
176
+ // platform binary and `apply-outputs` rejects the overlap. A project whose
177
+ // `build` genuinely emits files declares its own outputs in package.json,
178
+ // where the claim is scoped to the project that can honour it.
179
+ for (const property of ['dependsOn', 'outputs']) {
180
+ if (build !== null && property in build) {
181
+ issues.push({
182
+ path: nxJsonPath,
183
+ message: `targetDefaults.build.${property} must not be set; @smoothbricks/nx-plugin infers the build aggregate`,
184
+ });
185
+ }
175
186
  }
176
187
  }
177
- /**
178
- * Build outputs must include the canonical dist tree; additional project-root
179
- * dist siblings (dist-node, dist-test, …) are legitimate secondary outputs a
180
- * repo may aggregate into `build` — the policy pins outputs under the project
181
- * root's dist family, not the exact array.
182
- */
183
- function isValidBuildOutputs(outputs) {
184
- return (Array.isArray(outputs) &&
185
- outputs.includes('{projectRoot}/dist') &&
186
- outputs.every((output) => typeof output === 'string' && /^\{projectRoot\}\/dist(-[\w.-]+)?$/.test(output)));
187
- }
188
188
  function validateCleanTargetDefault(nxJson, nxJsonPath, issues) {
189
189
  const targetDefaults = recordProperty(nxJson, 'targetDefaults');
190
190
  const clean = targetDefaults ? recordProperty(targetDefaults, 'clean') : null;
@@ -235,14 +235,11 @@ function applyBuildTargetDefault(nxJson) {
235
235
  const targetDefaults = getOrCreateRecord(nxJson, 'targetDefaults');
236
236
  const build = getOrCreateRecord(targetDefaults, 'build');
237
237
  let changed = setBooleanProperty(build, 'cache', true);
238
- // Reset only invalid outputs: valid extra dist siblings (dist-node, …) are a
239
- // repo decision the fixer must not clobber.
240
- if (!isValidBuildOutputs(build.outputs)) {
241
- changed = setStringArrayProperty(build, 'outputs', ['{projectRoot}/dist']) || changed;
242
- }
243
- if ('dependsOn' in build) {
244
- delete build.dependsOn;
245
- changed = true;
238
+ for (const property of ['dependsOn', 'outputs']) {
239
+ if (property in build) {
240
+ delete build[property];
241
+ changed = true;
242
+ }
246
243
  }
247
244
  return changed;
248
245
  }
package/nextest.toml ADDED
@@ -0,0 +1,42 @@
1
+ # Injected by @smoothbricks/nx-plugin as `--config-file` on inferred cargo-test
2
+ # package targets. `period` matches bun's `--timeout=30000`; `terminate-after = 1`
3
+ # kills at that bound instead of warning then waiting another period.
4
+ [profile.default]
5
+ slow-timeout = { period = "30s", terminate-after = 1 }
6
+
7
+ # Compile-fail tests compile fixture crates (or rustc a negative contract) as
8
+ # their body. `cargo test --no-run` cannot pre-build those; they are generated
9
+ # at test time. Filterset matches the class by test name rather than listing
10
+ # today's two tests (`compile_fail_cases`, `lesser_capabilities_fail_to_compile_*`).
11
+ #
12
+ # 180s is provisional: measure on an idle machine with `--test-threads=1`.
13
+ # An unbounded run phase is how a hung test becomes a hung lane.
14
+ [[profile.default.overrides]]
15
+ filter = 'test(/compile_fail|fail_to_compile/)'
16
+ slow-timeout = { period = "180s", terminate-after = 1 }
17
+
18
+ # Tests that drive the host's REAL APFS stack — creating, attaching, mounting and
19
+ # detaching actual disk images — contend on a single machine-wide serialization
20
+ # point: Disk Arbitration and the APFS driver. Measured on this host, a read-only
21
+ # `diskutil apfs list -plist` costs 0.7s idle at 33 attached images but up to
22
+ # 14.4s while another process is attaching/detaching. Run in parallel these tests
23
+ # manufacture exactly that contention for each other, so each one's wall time
24
+ # becomes a function of how many siblings happen to be mid-attach — a test whose
25
+ # verdict depends on that is measuring the scheduler, not the code.
26
+ #
27
+ # `max-threads = 1` makes the group a mutex across the WHOLE run (groups are
28
+ # run-wide, not per-binary, so the lib unit tests and the apfs_integration binary
29
+ # serialize against each other as intended). The other ~830 tests stay parallel.
30
+ #
31
+ # This bounds contention; it does NOT grant more time. The per-test deadline above
32
+ # is unchanged and each of these tests is expected to fit it on its own merits.
33
+ # Membership is by name: `real_apfs_*` is the class marker, so a new real-APFS
34
+ # test joins the group by being named for what it touches. A bare `real_*` filter
35
+ # would be wrong here — unrelated `real_*` tests exist in columine-arrow,
36
+ # cowshed-cli and cowshed-gateway that touch no disk image at all.
37
+ [test-groups]
38
+ real-apfs = { max-threads = 1 }
39
+
40
+ [[profile.default.overrides]]
41
+ filter = 'test(/real_apfs_/)'
42
+ test-group = 'real-apfs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -75,6 +75,7 @@
75
75
  "files": [
76
76
  "dist",
77
77
  "src",
78
+ "nextest.toml",
78
79
  "README.md",
79
80
  "loader.js",
80
81
  "generators.json",
@@ -15,6 +15,7 @@ import {
15
15
  boundedTestScriptAlias,
16
16
  checkBoundedTestTargetPolicy,
17
17
  checkWorkspaceBoundedTestTargetPolicy,
18
+ checkWorkspaceCargoTestReachabilityPolicy,
18
19
  ensureBunTestTimeoutFlag,
19
20
  resolveTestCommand,
20
21
  } from './bounded-test-policy.js';
@@ -263,6 +264,48 @@ describe('bounded test target policy', () => {
263
264
  }
264
265
  });
265
266
 
267
+ it('leaves multi-command and non-bun test targets for the check to report', async () => {
268
+ const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-policy-'));
269
+ try {
270
+ await writeJson(join(root, 'package.json'), {
271
+ name: '@scope/root',
272
+ private: true,
273
+ workspaces: ['packages/*'],
274
+ });
275
+ const corpusTarget = {
276
+ executor: 'nx:run-commands',
277
+ options: {
278
+ commands: ['bun test test/', 'cd go && go test ./...'],
279
+ parallel: false,
280
+ cwd: 'packages/schema',
281
+ },
282
+ };
283
+ const cargoTarget = {
284
+ executor: 'nx:run-commands',
285
+ options: { command: 'cargo test --workspace', cwd: 'packages/native' },
286
+ };
287
+ await writeJson(join(root, 'packages/schema/package.json'), {
288
+ name: '@scope/schema',
289
+ nx: { name: 'schema', targets: { test: corpusTarget } },
290
+ });
291
+ await writeJson(join(root, 'packages/native/package.json'), {
292
+ name: '@scope/native',
293
+ nx: { name: 'native', targets: { test: cargoTarget } },
294
+ });
295
+
296
+ expect(applyWorkspaceBoundedTestTargetPolicy(root)).toBe(false);
297
+
298
+ const schema = JSON.parse(await readFile(join(root, 'packages/schema/package.json'), 'utf8'));
299
+ expect(schema.nx.targets.test).toEqual(corpusTarget);
300
+ const native = JSON.parse(await readFile(join(root, 'packages/native/package.json'), 'utf8'));
301
+ expect(native.nx.targets.test).toEqual(cargoTarget);
302
+ // Still reported: apply refusing to flatten does not silently bless them.
303
+ expect(checkWorkspaceBoundedTestTargetPolicy(root).length).toBe(2);
304
+ } finally {
305
+ await rm(root, { recursive: true, force: true });
306
+ }
307
+ });
308
+
266
309
  it('preserves a resolved bounded aggregate during workspace updates', async () => {
267
310
  const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-policy-'));
268
311
  try {
@@ -340,6 +383,267 @@ describe('bounded test target policy', () => {
340
383
  });
341
384
  });
342
385
 
386
+ describe('cargo-test reachability policy', () => {
387
+ it('flags a cargo workspace whose test aggregate never reaches cargo-test', async () => {
388
+ const root = await mkdtemp(join(tmpdir(), 'cargo-reach-'));
389
+ try {
390
+ await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
391
+ await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
392
+
393
+ // The shape that shipped: nx.json targetDefaults replaced the inferred
394
+ // dependsOn: [cargo-test] with [^build, build], so `nx test rusty` was a
395
+ // no-op that ran nothing and reported success.
396
+ const clobbered: ResolvedProjectTargets = {
397
+ root: 'packages/rusty',
398
+ targets: new Set(['test', 'cargo-test', 'build']),
399
+ targetDependencies: new Map([['test', ['^build', 'build']]]),
400
+ targetExecutors: new Map([
401
+ ['test', 'nx:noop'],
402
+ ['cargo-test', 'nx:noop'],
403
+ ['build', 'nx:run-commands'],
404
+ ]),
405
+ // cargo-test is the runner for a single-crate workspace.
406
+ targetOptions: new Map([['cargo-test', { command: 'cargo --frozen test --workspace' }]]),
407
+ };
408
+ const issues = checkWorkspaceCargoTestReachabilityPolicy(root, {
409
+ resolvedTargetsByProject: new Map([['rusty', clobbered]]),
410
+ });
411
+ expect(issues).toHaveLength(1);
412
+ expect(issues[0]?.message).toContain('cargo-test');
413
+
414
+ // Transitive reachability counts: cowshed reaches it through napi-test.
415
+ const transitive: ResolvedProjectTargets = {
416
+ ...clobbered,
417
+ targets: new Set(['test', 'napi-test', 'cargo-test', 'build']),
418
+ targetDependencies: new Map([
419
+ ['test', ['napi-test']],
420
+ ['napi-test', ['cargo-test']],
421
+ ]),
422
+ };
423
+ expect(
424
+ checkWorkspaceCargoTestReachabilityPolicy(root, {
425
+ resolvedTargetsByProject: new Map([['rusty', transitive]]),
426
+ }),
427
+ ).toEqual([]);
428
+ } finally {
429
+ await rm(root, { recursive: true, force: true });
430
+ }
431
+ });
432
+
433
+ it('flags a cargo-test that reaches only compile targets, never a runner', async () => {
434
+ const root = await mkdtemp(join(tmpdir(), 'cargo-reach-noop-'));
435
+ try {
436
+ await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
437
+ await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
438
+
439
+ // The shape a `dependsOn` that omits the runner leg produces: `test`
440
+ // still reaches cargo-test, and cargo-test still reaches real work — but
441
+ // the only cargo invocation in the closure is `--no-run`, so no test is
442
+ // ever executed and the target is green. Reachability alone cannot see
443
+ // this. Hoisted so the runner fixture below extends a total map instead
444
+ // of spreading an optional one.
445
+ const compileOnlyOptions = new Map<string, Record<string, unknown>>([
446
+ ['cargo-test-compile', { command: 'cargo --frozen test --workspace --no-run' }],
447
+ ['cargo-wasm', { command: 'just wasm' }],
448
+ ]);
449
+ const compileOnly: ResolvedProjectTargets = {
450
+ root: 'packages/rusty',
451
+ targets: new Set(['test', 'cargo-test', 'cargo-test-compile', 'cargo-wasm']),
452
+ targetDependencies: new Map([
453
+ ['test', ['cargo-test']],
454
+ ['cargo-test', ['cargo-test-compile', 'cargo-wasm']],
455
+ ]),
456
+ targetExecutors: new Map([
457
+ ['test', 'nx:noop'],
458
+ ['cargo-test', 'nx:noop'],
459
+ ['cargo-test-compile', 'nx:run-commands'],
460
+ ['cargo-wasm', 'nx:run-commands'],
461
+ ]),
462
+ targetOptions: compileOnlyOptions,
463
+ };
464
+ const issues = checkWorkspaceCargoTestReachabilityPolicy(root, {
465
+ resolvedTargetsByProject: new Map([['rusty', compileOnly]]),
466
+ });
467
+ expect(issues).toHaveLength(1);
468
+ expect(issues[0]?.message).toContain('RUNS tests');
469
+
470
+ // Restoring a real runner clears it.
471
+ const withRunner: ResolvedProjectTargets = {
472
+ ...compileOnly,
473
+ targets: new Set([...compileOnly.targets, 'cargo-test-rusty-core']),
474
+ targetDependencies: new Map([
475
+ ['test', ['cargo-test']],
476
+ ['cargo-test', ['cargo-test-rusty-core']],
477
+ ['cargo-test-rusty-core', ['cargo-test-compile']],
478
+ ]),
479
+ targetOptions: new Map([
480
+ ...compileOnlyOptions,
481
+ ['cargo-test-rusty-core', { command: 'cargo --frozen nextest run --package rusty-core' }],
482
+ ]),
483
+ };
484
+ expect(
485
+ checkWorkspaceCargoTestReachabilityPolicy(root, {
486
+ resolvedTargetsByProject: new Map([['rusty', withRunner]]),
487
+ }),
488
+ ).toEqual([]);
489
+ } finally {
490
+ await rm(root, { recursive: true, force: true });
491
+ }
492
+ });
493
+
494
+ it('ignores projects with no cargo-test target', async () => {
495
+ const root = await mkdtemp(join(tmpdir(), 'cargo-reach-ts-'));
496
+ try {
497
+ await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
498
+ await writeJson(join(root, 'packages/tsonly/package.json'), { name: 'tsonly', nx: { name: 'tsonly' } });
499
+ const tsOnly: ResolvedProjectTargets = {
500
+ root: 'packages/tsonly',
501
+ targets: new Set(['test', 'build']),
502
+ targetDependencies: new Map([['test', ['build']]]),
503
+ targetExecutors: new Map([['test', BOUNDED_TEST_EXECUTOR]]),
504
+ targetOptions: new Map(),
505
+ };
506
+ expect(
507
+ checkWorkspaceCargoTestReachabilityPolicy(root, {
508
+ resolvedTargetsByProject: new Map([['tsonly', tsOnly]]),
509
+ }),
510
+ ).toEqual([]);
511
+ } finally {
512
+ await rm(root, { recursive: true, force: true });
513
+ }
514
+ });
515
+
516
+ it('flags per-crate cargo-test children that do not cover every workspace member', async () => {
517
+ const root = await mkdtemp(join(tmpdir(), 'cargo-reach-union-'));
518
+ try {
519
+ await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
520
+ await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
521
+ await mkdir(join(root, 'packages/rusty/crates/core'), { recursive: true });
522
+ await mkdir(join(root, 'packages/rusty/crates/cli'), { recursive: true });
523
+ await writeFile(
524
+ join(root, 'packages/rusty/Cargo.toml'),
525
+ '[workspace]\nmembers = ["crates/core", "crates/cli"]\n',
526
+ );
527
+ await writeFile(join(root, 'packages/rusty/crates/core/Cargo.toml'), '[package]\nname = "rusty-core"\n');
528
+ await writeFile(join(root, 'packages/rusty/crates/cli/Cargo.toml'), '[package]\nname = "rusty-cli"\n');
529
+
530
+ const runnerOptions = new Map<string, Record<string, unknown>>([
531
+ ['cargo-test-compile', { command: 'cargo --frozen test --workspace --no-run' }],
532
+ ['cargo-test-rusty-core', { command: 'cargo --frozen nextest run --package rusty-core' }],
533
+ ['cargo-test-rusty-cli', { command: 'cargo --frozen nextest run --package rusty-cli' }],
534
+ ]);
535
+ const missingChild: ResolvedProjectTargets = {
536
+ root: 'packages/rusty',
537
+ targets: new Set(['test', 'cargo-test', 'cargo-test-compile', 'cargo-test-rusty-core']),
538
+ targetDependencies: new Map([
539
+ ['test', ['cargo-test']],
540
+ ['cargo-test', ['cargo-test-rusty-core']],
541
+ ['cargo-test-rusty-core', ['cargo-test-compile']],
542
+ ]),
543
+ targetExecutors: new Map([
544
+ ['test', 'nx:noop'],
545
+ ['cargo-test', 'nx:noop'],
546
+ ['cargo-test-compile', 'nx:run-commands'],
547
+ ['cargo-test-rusty-core', BOUNDED_TEST_EXECUTOR],
548
+ ]),
549
+ targetOptions: runnerOptions,
550
+ };
551
+ const issues = checkWorkspaceCargoTestReachabilityPolicy(root, {
552
+ resolvedTargetsByProject: new Map([['rusty', missingChild]]),
553
+ });
554
+ expect(issues).toHaveLength(1);
555
+ expect(issues[0]?.message).toContain('rusty-cli');
556
+
557
+ const covered: ResolvedProjectTargets = {
558
+ ...missingChild,
559
+ targets: new Set([...missingChild.targets, 'cargo-test-rusty-cli']),
560
+ targetDependencies: new Map([
561
+ ['test', ['cargo-test']],
562
+ ['cargo-test', ['cargo-test-rusty-core', 'cargo-test-rusty-cli']],
563
+ ['cargo-test-rusty-core', ['cargo-test-compile']],
564
+ ['cargo-test-rusty-cli', ['cargo-test-rusty-core']],
565
+ ]),
566
+ targetExecutors: new Map([
567
+ ...(missingChild.targetExecutors ?? []),
568
+ ['cargo-test-rusty-cli', BOUNDED_TEST_EXECUTOR],
569
+ ]),
570
+ };
571
+ expect(
572
+ checkWorkspaceCargoTestReachabilityPolicy(root, {
573
+ resolvedTargetsByProject: new Map([['rusty', covered]]),
574
+ }),
575
+ ).toEqual([]);
576
+
577
+ await writeFile(join(root, 'packages/rusty/Cargo.toml'), '[workspace]\nmembers = ["crates/core"]\n');
578
+ const extra = checkWorkspaceCargoTestReachabilityPolicy(root, {
579
+ resolvedTargetsByProject: new Map([['rusty', covered]]),
580
+ });
581
+ expect(extra).toHaveLength(1);
582
+ expect(extra[0]?.message).toContain('rusty-cli');
583
+ } finally {
584
+ await rm(root, { recursive: true, force: true });
585
+ }
586
+ });
587
+
588
+ it('counts every shard of a sharded crate as covering that crate exactly once', async () => {
589
+ const root = await mkdtemp(join(tmpdir(), 'cargo-reach-shard-'));
590
+ try {
591
+ await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
592
+ await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
593
+ await mkdir(join(root, 'packages/rusty/crates/core'), { recursive: true });
594
+ await writeFile(join(root, 'packages/rusty/Cargo.toml'), '[workspace]\nmembers = ["crates/core"]\n');
595
+ await writeFile(
596
+ join(root, 'packages/rusty/crates/core/Cargo.toml'),
597
+ '[package]\nname = "rusty-core"\n\n[package.metadata.smoothbricks.test]\nshards = 2\n',
598
+ );
599
+
600
+ const sharded: ResolvedProjectTargets = {
601
+ root: 'packages/rusty',
602
+ targets: new Set([
603
+ 'test',
604
+ 'cargo-test',
605
+ 'cargo-test-compile',
606
+ 'cargo-test-rusty-core-shard1',
607
+ 'cargo-test-rusty-core-shard2',
608
+ ]),
609
+ targetDependencies: new Map([
610
+ ['test', ['cargo-test']],
611
+ ['cargo-test', ['cargo-test-rusty-core-shard1', 'cargo-test-rusty-core-shard2']],
612
+ ['cargo-test-rusty-core-shard1', ['cargo-test-compile']],
613
+ ['cargo-test-rusty-core-shard2', ['cargo-test-rusty-core-shard1']],
614
+ ]),
615
+ targetExecutors: new Map([
616
+ ['test', 'nx:noop'],
617
+ ['cargo-test', 'nx:noop'],
618
+ ['cargo-test-compile', 'nx:run-commands'],
619
+ ['cargo-test-rusty-core-shard1', BOUNDED_TEST_EXECUTOR],
620
+ ['cargo-test-rusty-core-shard2', BOUNDED_TEST_EXECUTOR],
621
+ ]),
622
+ targetOptions: new Map<string, Record<string, unknown>>([
623
+ ['cargo-test-compile', { command: 'cargo --frozen test --workspace --no-run' }],
624
+ [
625
+ 'cargo-test-rusty-core-shard1',
626
+ { command: "cargo --frozen nextest run --workspace -E 'package(rusty-core)' --partition hash:1/2" },
627
+ ],
628
+ [
629
+ 'cargo-test-rusty-core-shard2',
630
+ { command: "cargo --frozen nextest run --workspace -E 'package(rusty-core)' --partition hash:2/2" },
631
+ ],
632
+ ]),
633
+ };
634
+ // Shards collapse to their crate: two targets cover one member without
635
+ // reading as an extra member the workspace does not have.
636
+ expect(
637
+ checkWorkspaceCargoTestReachabilityPolicy(root, {
638
+ resolvedTargetsByProject: new Map([['rusty', sharded]]),
639
+ }),
640
+ ).toEqual([]);
641
+ } finally {
642
+ await rm(root, { recursive: true, force: true });
643
+ }
644
+ });
645
+ });
646
+
343
647
  function resolvedAggregateProject(): ResolvedProjectTargets {
344
648
  return {
345
649
  root: 'packages/cowshed',