@smoothbricks/nx-plugin 0.3.10 → 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 (43) 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/executors/clean-outputs/executor.js +17 -3
  16. package/dist/executors/napi-cross-toolchain/executor.d.ts.map +1 -1
  17. package/dist/executors/napi-cross-toolchain/executor.js +8 -0
  18. package/dist/index.d.ts +2 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +344 -57
  21. package/dist/package-target-policy.d.ts +2 -1
  22. package/dist/package-target-policy.d.ts.map +1 -1
  23. package/dist/package-target-policy.js +54 -13
  24. package/dist/workspace-config-policy.js +32 -35
  25. package/nextest.toml +42 -0
  26. package/package.json +2 -1
  27. package/src/bounded-test-policy.test.ts +304 -0
  28. package/src/bounded-test-policy.ts +218 -0
  29. package/src/cargo-workspace.ts +243 -0
  30. package/src/cross-check-policy.test.ts +24 -0
  31. package/src/cross-check-policy.ts +80 -1
  32. package/src/executors/bounded-exec/executor.test.ts +147 -3
  33. package/src/executors/bounded-exec/executor.ts +103 -53
  34. package/src/executors/bounded-exec/schema.json +7 -2
  35. package/src/executors/bounded-exec/schema.ts +13 -0
  36. package/src/executors/clean-outputs/executor.ts +17 -3
  37. package/src/executors/napi-cross-toolchain/executor.ts +8 -0
  38. package/src/index.test.ts +373 -25
  39. package/src/index.ts +390 -65
  40. package/src/package-target-policy.test.ts +50 -0
  41. package/src/package-target-policy.ts +58 -9
  42. package/src/workspace-config-policy.test.ts +15 -30
  43. package/src/workspace-config-policy.ts +32 -39
@@ -1,5 +1,6 @@
1
1
  import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
2
2
  import { dirname, join } from 'node:path';
3
+ import { CARGO_TEST_TARGET, listCargoWorkspacePackages, packageNameFromCargoTestTarget } from './cargo-workspace.js';
3
4
  import type { PackageTargetPolicyOptions, ResolvedProjectTargets } from './package-target-policy.js';
4
5
 
5
6
  export const BOUNDED_TEST_EXECUTOR = '@smoothbricks/nx-plugin:bounded-exec';
@@ -83,6 +84,9 @@ export function applyWorkspaceBoundedTestTargetPolicy(root: string, options: Pac
83
84
  if (checkBoundedTestTargetPolicy(packageJson, { projectName, projectJson, resolvedProject })) {
84
85
  continue;
85
86
  }
87
+ if (!boundableTestTarget(packageJson, projectJson)) {
88
+ continue;
89
+ }
86
90
  const beforePackageJson = JSON.stringify(packageJson);
87
91
  const beforeProjectJson = JSON.stringify(projectJson);
88
92
  applyBoundedTestTargetPolicy(packageJson, { projectName, projectJson });
@@ -131,6 +135,193 @@ export function checkWorkspaceBoundedTestTargetPolicy(
131
135
  return issues;
132
136
  }
133
137
 
138
+ /**
139
+ * A cargo workspace's `test` aggregate MUST reach `cargo-test`, and `cargo-test`
140
+ * must actually RUN tests.
141
+ *
142
+ * Inference wires `test` to `cargo-test` (see `index.ts`), but two things unwire
143
+ * it silently and both leave the target green:
144
+ *
145
+ * 1. `targetDefaults.test.dependsOn` in `nx.json` REPLACES an inferred
146
+ * `dependsOn` rather than merging, and a package-local `nx.targets.test`
147
+ * replaces it outright. `nx test <project>` then executes nothing.
148
+ * 2. `dependsOn` REPLACES rather than unions at every merge layer, so a
149
+ * package-local `nx.targets['cargo-test'].dependsOn` that omits the inferred
150
+ * runner leg leaves a target that still "reaches" its dependencies while no
151
+ * test binary is ever run. Reachability alone cannot see this, because
152
+ * `cargo-test-compile` is `--no-run`. (Inference itself no longer skips a
153
+ * declared cargo key, so the executor and options survive the declaration —
154
+ * see `index.ts`.)
155
+ *
156
+ * So this checks three things: `test` reaches `cargo-test`; some target in
157
+ * `cargo-test`'s closure actually executes tests rather than only compiling them;
158
+ * and when the run is split into per-crate `cargo-test-<package>` children, their
159
+ * union equals the workspace member set. A missing child is silent once
160
+ * `--package` actually filters: the remaining targets stay green and the
161
+ * uncovered member never runs.
162
+ */
163
+ export function checkWorkspaceCargoTestReachabilityPolicy(
164
+ root: string,
165
+ options: PackageTargetPolicyOptions = {},
166
+ ): BoundedTestPolicyIssue[] {
167
+ const issues: BoundedTestPolicyIssue[] = [];
168
+ for (const packageJsonPath of listWorkspacePackageJsonPaths(root)) {
169
+ const packageJson = readPackageJson(packageJsonPath);
170
+ const projectJsonPath = projectJsonPathForPackageJson(packageJsonPath);
171
+ const projectJson = existsSync(projectJsonPath) ? readProjectJson(projectJsonPath) : undefined;
172
+ const projectName = packageProjectName(packageJson, projectJson);
173
+ if (!projectName) {
174
+ continue;
175
+ }
176
+ const resolvedProject = resolvedProjectFor(options, projectName);
177
+ // Without a resolved graph there is nothing to judge, and without a
178
+ // cargo-test target this is not a cargo workspace.
179
+ if (!resolvedProject?.targets?.has(CARGO_TEST_TARGET) || !resolvedProject.targets.has('test')) {
180
+ continue;
181
+ }
182
+ const scope = projectJson ? 'targets' : 'nx.targets';
183
+ const path = projectJson ? projectJsonPath : packageJsonPath;
184
+ if (!resolvedTargetReaches(resolvedProject, 'test', CARGO_TEST_TARGET)) {
185
+ issues.push({
186
+ path,
187
+ message:
188
+ `${scope}.test must depend on ${CARGO_TEST_TARGET} ` +
189
+ 'so the Rust tests run in the test aggregate; nx.json targetDefaults replaces the inferred dependsOn',
190
+ });
191
+ } else if (!resolvedTargetRunsTests(resolvedProject, CARGO_TEST_TARGET)) {
192
+ issues.push({
193
+ path,
194
+ message:
195
+ `${scope}.${CARGO_TEST_TARGET} reaches no target that RUNS tests — only ones that compile them. ` +
196
+ 'A declared dependsOn replaces the inferred one; spread it with "..." instead of omitting the runner leg',
197
+ });
198
+ } else {
199
+ const coverage = cargoTestMemberCoverageIssue(root, path, scope, resolvedProject);
200
+ if (coverage !== undefined) {
201
+ issues.push(coverage);
202
+ }
203
+ }
204
+ }
205
+ return issues;
206
+ }
207
+
208
+ /**
209
+ * True when `target`, or something in its dependency closure, invokes cargo in a
210
+ * mode that executes tests. `cargo-test-compile` is deliberately excluded: it is
211
+ * `cargo test --no-run`, so it proves the binaries build and nothing about them
212
+ * running.
213
+ */
214
+ function resolvedTargetRunsTests(project: ResolvedProjectTargets, target: string): boolean {
215
+ const visiting = new Set<string>();
216
+ const visit = (targetName: string): boolean => {
217
+ if (visiting.has(targetName) || !project.targets.has(targetName)) {
218
+ return false;
219
+ }
220
+ visiting.add(targetName);
221
+ const command = commandOf(project, targetName);
222
+ const runs = command !== undefined && /\btest\b|\bnextest\b/.test(command) && !command.includes('--no-run');
223
+ const reached =
224
+ runs ||
225
+ (project.targetDependencies?.get(targetName) ?? []).some((dependency) =>
226
+ matchingResolvedTargets(dependency, project.targets).some(visit),
227
+ );
228
+ visiting.delete(targetName);
229
+ return reached;
230
+ };
231
+ return visit(target);
232
+ }
233
+
234
+ /**
235
+ * Per-crate nextest targets that `cargo-test` actually reaches. `cargo-test-compile`
236
+ * is excluded: it is not a runner. Members come from the same Cargo.toml walk
237
+ * inference uses; the child list is the graph, not a hand-written crate list.
238
+ */
239
+ function cargoTestPackageChildren(project: ResolvedProjectTargets): string[] {
240
+ const packages = new Set<string>();
241
+ const visiting = new Set<string>();
242
+ const visit = (targetName: string): void => {
243
+ if (visiting.has(targetName) || !project.targets.has(targetName)) {
244
+ return;
245
+ }
246
+ visiting.add(targetName);
247
+ const pkg = packageNameFromCargoTestTarget(targetName);
248
+ if (pkg !== null) {
249
+ packages.add(pkg);
250
+ }
251
+ for (const dependency of project.targetDependencies?.get(targetName) ?? []) {
252
+ for (const matched of matchingResolvedTargets(dependency, project.targets)) {
253
+ visit(matched);
254
+ }
255
+ }
256
+ };
257
+ visit(CARGO_TEST_TARGET);
258
+ return [...packages].sort((left, right) => left.localeCompare(right));
259
+ }
260
+
261
+ function cargoTestMemberCoverageIssue(
262
+ workspaceRoot: string,
263
+ path: string,
264
+ scope: string,
265
+ project: ResolvedProjectTargets,
266
+ ): BoundedTestPolicyIssue | undefined {
267
+ if (typeof project.root !== 'string' || project.root.length === 0) {
268
+ return undefined;
269
+ }
270
+ const members = listCargoWorkspacePackages(join(workspaceRoot, project.root)).map((pkg) => pkg.name);
271
+ if (members.length === 0) {
272
+ return undefined;
273
+ }
274
+ const children = cargoTestPackageChildren(project);
275
+ if (children.length === 0) {
276
+ return undefined;
277
+ }
278
+ const memberSet = new Set(members);
279
+ const childSet = new Set(children);
280
+ const missing = members.filter((name) => !childSet.has(name));
281
+ const extra = children.filter((name) => !memberSet.has(name));
282
+ if (missing.length === 0 && extra.length === 0) {
283
+ return undefined;
284
+ }
285
+ const details = [
286
+ ...(missing.length > 0 ? [`missing ${missing.join(', ')}`] : []),
287
+ ...(extra.length > 0 ? [`extra ${extra.join(', ')}`] : []),
288
+ ];
289
+ return {
290
+ path,
291
+ message:
292
+ `${scope}.${CARGO_TEST_TARGET} per-crate children must equal workspace members that have tests ` +
293
+ `(${members.join(', ')}); ${details.join('; ')}`,
294
+ };
295
+ }
296
+
297
+ function commandOf(project: ResolvedProjectTargets, targetName: string): string | undefined {
298
+ const options = project.targetOptions?.get(targetName);
299
+ if (options === undefined || options === null) {
300
+ return undefined;
301
+ }
302
+ const command = (options as Record<string, unknown>).command;
303
+ return typeof command === 'string' ? command : undefined;
304
+ }
305
+
306
+ function resolvedTargetReaches(project: ResolvedProjectTargets, from: string, goal: string): boolean {
307
+ const visiting = new Set<string>();
308
+ const visit = (targetName: string): boolean => {
309
+ if (targetName === goal) {
310
+ return true;
311
+ }
312
+ if (visiting.has(targetName) || !project.targets.has(targetName)) {
313
+ return false;
314
+ }
315
+ visiting.add(targetName);
316
+ const reached = (project.targetDependencies?.get(targetName) ?? []).some((dependency) =>
317
+ matchingResolvedTargets(dependency, project.targets).some(visit),
318
+ );
319
+ visiting.delete(targetName);
320
+ return reached;
321
+ };
322
+ return visit(from);
323
+ }
324
+
134
325
  export function checkBoundedTestTargetPolicy(
135
326
  packageJson: BoundedTestPolicyPackageJson,
136
327
  options: ApplyBoundedTestTargetPolicyOptions,
@@ -157,6 +348,33 @@ export function checkBoundedTestTargetPolicy(
157
348
  return resolvedAggregateTestIsBounded(options.resolvedProject);
158
349
  }
159
350
 
351
+ /**
352
+ * Apply may only rewrite a test target it can express faithfully as one
353
+ * bounded `bun test` command. A `commands` array (a multi-runner gate) would
354
+ * be flattened to the default command — deleting the very checks the target
355
+ * exists to run — and a non-bun runner (cargo, go) would inherit wall-clock
356
+ * bounds scaled for bun suites. Those targets are left in place for the check
357
+ * to report, so restructuring them stays a human decision.
358
+ */
359
+ function boundableTestTarget(
360
+ packageJson: BoundedTestPolicyPackageJson,
361
+ projectJson: BoundedTestPolicyProjectJson | undefined,
362
+ ): boolean {
363
+ const target = projectJson?.targets?.test ?? packageJson.nx?.targets?.test;
364
+ if (!isRecord(target)) {
365
+ return true;
366
+ }
367
+ if (isNoopAggregateTarget(target)) {
368
+ return true;
369
+ }
370
+ const targetOptions = isRecord(target.options) ? target.options : undefined;
371
+ if (targetOptions && Array.isArray(targetOptions.commands)) {
372
+ return false;
373
+ }
374
+ const command = resolveTargetCommand(target);
375
+ return command === null || BUN_TEST_PREFIX.test(command);
376
+ }
377
+
160
378
  function isNoopAggregateTarget(target: Record<string, unknown>): boolean {
161
379
  if (target.executor !== undefined && target.executor !== 'nx:noop') {
162
380
  return false;
@@ -0,0 +1,243 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { join, normalize, posix, relative, sep } from 'node:path';
4
+ import { parse as parseToml } from 'smol-toml';
5
+
6
+ export interface CargoWorkspacePackage {
7
+ name: string;
8
+ dir: string;
9
+ /**
10
+ * How many bounded Nx targets this crate's tests are split across. See
11
+ * `readTestShards` for why this is declared per crate rather than inferred.
12
+ */
13
+ testShards: number;
14
+ }
15
+
16
+ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWorkspacePackage[] {
17
+ const workspaceTomlPath = join(absoluteProjectRoot, 'Cargo.toml');
18
+ if (!existsSync(workspaceTomlPath)) {
19
+ return [];
20
+ }
21
+ const parsed: unknown = parseToml(readFileSync(workspaceTomlPath, 'utf-8'));
22
+ if (!isRecord(parsed) || !isRecord(parsed.workspace) || !Array.isArray(parsed.workspace.members)) {
23
+ return [];
24
+ }
25
+ const packages: CargoWorkspacePackage[] = [];
26
+ for (const member of parsed.workspace.members) {
27
+ if (typeof member !== 'string' || member.length === 0) {
28
+ continue;
29
+ }
30
+ const crateTomlPath = join(absoluteProjectRoot, member, 'Cargo.toml');
31
+ if (!existsSync(crateTomlPath)) {
32
+ continue;
33
+ }
34
+ const crateParsed: unknown = parseToml(readFileSync(crateTomlPath, 'utf-8'));
35
+ if (!isRecord(crateParsed) || !isRecord(crateParsed.package) || typeof crateParsed.package.name !== 'string') {
36
+ continue;
37
+ }
38
+ packages.push({
39
+ name: crateParsed.package.name,
40
+ dir: member.split('\\').join('/'),
41
+ testShards: readTestShards(crateParsed.package, crateTomlPath),
42
+ });
43
+ }
44
+ packages.sort((left, right) => left.name.localeCompare(right.name));
45
+ return packages;
46
+ }
47
+
48
+ export async function cargoPackageTestInputs(absoluteProjectRoot: string, memberDir: string): Promise<string[]> {
49
+ const workspacePathDeps = await workspacePathDependencies(absoluteProjectRoot);
50
+ const dirs = new Set<string>([memberDir.split('\\').join('/')]);
51
+ const crateParsed: unknown = parseToml(await readFile(join(absoluteProjectRoot, memberDir, 'Cargo.toml'), 'utf-8'));
52
+ if (isRecord(crateParsed)) {
53
+ for (const tableName of ['dependencies', 'dev-dependencies', 'build-dependencies'] as const) {
54
+ const table = crateParsed[tableName];
55
+ if (!isRecord(table)) {
56
+ continue;
57
+ }
58
+ for (const [depName, spec] of Object.entries(table)) {
59
+ const pathDep = pathDependencyDir(memberDir, depName, spec, workspacePathDeps);
60
+ if (pathDep !== null) {
61
+ dirs.add(pathDep);
62
+ }
63
+ }
64
+ }
65
+ }
66
+ const inputs: string[] = ['{projectRoot}/Cargo.toml', '{projectRoot}/Cargo.lock'];
67
+ for (const dir of [...dirs].sort()) {
68
+ inputs.push(`{projectRoot}/${dir}/**/*.rs`, `{projectRoot}/${dir}/Cargo.toml`);
69
+ }
70
+ inputs.push('{projectRoot}/**/.cargo/config.toml', '{projectRoot}/scripts/*.sh', '!{projectRoot}/**/target/**');
71
+ return inputs;
72
+ }
73
+
74
+ async function workspacePathDependencies(absoluteProjectRoot: string): Promise<Map<string, string>> {
75
+ const parsed: unknown = parseToml(await readFile(join(absoluteProjectRoot, 'Cargo.toml'), 'utf-8'));
76
+ const deps = new Map<string, string>();
77
+ if (!isRecord(parsed) || !isRecord(parsed.workspace) || !isRecord(parsed.workspace.dependencies)) {
78
+ return deps;
79
+ }
80
+ for (const [name, spec] of Object.entries(parsed.workspace.dependencies)) {
81
+ if (isRecord(spec) && typeof spec.path === 'string') {
82
+ deps.set(name, spec.path.split('\\').join('/'));
83
+ }
84
+ }
85
+ return deps;
86
+ }
87
+
88
+ function pathDependencyDir(
89
+ memberDir: string,
90
+ depName: string,
91
+ spec: unknown,
92
+ workspacePathDeps: Map<string, string>,
93
+ ): string | null {
94
+ if (spec === undefined || spec === null) {
95
+ return null;
96
+ }
97
+ if (typeof spec === 'string') {
98
+ return null;
99
+ }
100
+ if (!isRecord(spec)) {
101
+ return null;
102
+ }
103
+ if (spec.workspace === true) {
104
+ return workspacePathDeps.get(depName) ?? null;
105
+ }
106
+ if (typeof spec.path !== 'string') {
107
+ return null;
108
+ }
109
+ const resolved = normalize(join(memberDir, spec.path));
110
+ if (resolved.startsWith('..')) {
111
+ return null;
112
+ }
113
+ return resolved.split(sep).join(posix.sep);
114
+ }
115
+
116
+ function isRecord(value: unknown): value is Record<string, unknown> {
117
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
118
+ }
119
+
120
+ /**
121
+ * `[package.metadata.smoothbricks.test] shards = N` splits a crate's tests
122
+ * across N bounded targets, each running `--partition hash:i/N`.
123
+ *
124
+ * The COUNT is declared and the MEMBERSHIP is derived: nextest hashes each test
125
+ * name into a shard, so a new test — or a whole new test binary — is absorbed
126
+ * with no edit here and no list to forget to update. A stale count can only
127
+ * make a target slow, never make a test disappear, because the shards remain an
128
+ * exact partition of whatever the crate currently contains.
129
+ *
130
+ * It is declared rather than inferred because the only honest input is how long
131
+ * the suite takes, which target inference cannot measure. Defaulting to 1 keeps
132
+ * small crates on a single target: cowshed-cli's 226 tests run in 6.5s, so
133
+ * sharding them would buy nothing and pay a per-target cargo freshness check.
134
+ */
135
+ function readTestShards(cratePackage: Record<string, unknown>, crateTomlPath: string): number {
136
+ const metadata = isRecord(cratePackage.metadata) ? cratePackage.metadata : null;
137
+ const smoothbricks = metadata && isRecord(metadata.smoothbricks) ? metadata.smoothbricks : null;
138
+ const test = smoothbricks && isRecord(smoothbricks.test) ? smoothbricks.test : null;
139
+ const shards = test?.shards;
140
+ if (shards === undefined) {
141
+ return 1;
142
+ }
143
+ if (typeof shards !== 'number' || !Number.isInteger(shards) || shards < 1) {
144
+ throw new Error(`${crateTomlPath}: smoothbricks.test.shards must be an integer >= 1, got ${String(shards)}`);
145
+ }
146
+ return shards;
147
+ }
148
+
149
+ /**
150
+ * The aggregate that actually runs a cargo workspace's tests. Named here because
151
+ * both target inference and the policy that verifies `test` reaches it need the
152
+ * same string, and a drifted copy would make the policy vacuous.
153
+ */
154
+ export const CARGO_TEST_TARGET = 'cargo-test';
155
+
156
+ /** Prerequisite that compiles the workspace; not a per-crate runner. */
157
+ export const CARGO_TEST_COMPILE_TARGET = 'cargo-test-compile';
158
+
159
+ /**
160
+ * Tests that nextest.toml singles out are pinned to this suffix instead of
161
+ * being sharded. Only a sharded crate has one: an unsharded crate runs its
162
+ * whole suite in a single nextest process, which is all the pin restores.
163
+ */
164
+ export const CARGO_TEST_EXCEPTIONS_SUFFIX = 'exceptions';
165
+
166
+ /**
167
+ * A crate on one target keeps the bare name; a split crate suffixes the piece,
168
+ * so `cargo-test-cowshed-core-shard2` still reads as "cowshed-core's tests".
169
+ */
170
+ export function cargoTestPackageTargetName(packageName: string, piece?: string): string {
171
+ const base = `cargo-test-${packageName}`;
172
+ return piece === undefined ? base : `${base}-${piece}`;
173
+ }
174
+
175
+ /**
176
+ * Inverse of `cargoTestPackageTargetName`, used by the reachability policy to
177
+ * check that the per-crate targets cover every workspace member. Pieces of one
178
+ * crate collapse back to that crate, so a split crate counts as covered once.
179
+ */
180
+ export function packageNameFromCargoTestTarget(targetName: string): string | null {
181
+ if (!targetName.startsWith('cargo-test-') || targetName === CARGO_TEST_COMPILE_TARGET) {
182
+ return null;
183
+ }
184
+ const name = targetName
185
+ .slice('cargo-test-'.length)
186
+ .replace(new RegExp(`-(shard[1-9][0-9]*|${CARGO_TEST_EXCEPTIONS_SUFFIX})$`), '');
187
+ return name.length === 0 ? null : name;
188
+ }
189
+
190
+ /**
191
+ * The tests nextest.toml singles out with an override, as one filterset.
192
+ *
193
+ * An override is the config author saying "this class does not behave like the
194
+ * rest of the suite", and both classes declared today break a shard, each in a
195
+ * different way:
196
+ *
197
+ * - a `test-group` is scoped to a single nextest RUN, so letting the hash
198
+ * scatter its members across shards silently dissolves it. The real-APFS
199
+ * group exists because those tests contend on Disk Arbitration
200
+ * machine-wide, where an unscoped `diskutil apfs list -plist` measured 0.7s
201
+ * idle against 14.4s while another process is attaching.
202
+ * - a raised `slow-timeout` marks a test whose cost is not the suite's cost.
203
+ * `lesser_capabilities_fail_to_compile_...` rustc's a fixture that
204
+ * `cargo test --no-run` cannot pre-build, so it costs 25.6s on a cold
205
+ * target directory against 1.8s warm. Every CI runner is cold, and left in
206
+ * a shard that spike lands on whichever 250-odd tests share it.
207
+ *
208
+ * One target for all of them rather than one each: they sit in different groups
209
+ * so they run concurrently, making the combined wall the max of the classes
210
+ * rather than their sum.
211
+ *
212
+ * Deriving this from the config that declares the classes, rather than
213
+ * restating their filters, means adding an override there is the whole change —
214
+ * the pin follows and cannot drift from what it protects.
215
+ *
216
+ * Returns null when nothing is singled out, the "nothing to pin" case.
217
+ */
218
+ export function exceptionalTestFilter(nextestConfigPath: string): string | null {
219
+ const parsed: unknown = parseToml(readFileSync(nextestConfigPath, 'utf-8'));
220
+ if (!isRecord(parsed)) {
221
+ return null;
222
+ }
223
+ const profiles = isRecord(parsed.profile) ? parsed.profile : {};
224
+ const filters: string[] = [];
225
+ for (const profile of Object.values(profiles)) {
226
+ const overrides = isRecord(profile) ? profile.overrides : undefined;
227
+ if (!Array.isArray(overrides)) {
228
+ continue;
229
+ }
230
+ for (const override of overrides) {
231
+ if (isRecord(override) && typeof override.filter === 'string' && override.filter.length > 0) {
232
+ filters.push(override.filter);
233
+ }
234
+ }
235
+ }
236
+ const unique = [...new Set(filters)];
237
+ return unique.length === 0 ? null : unique.map((filter) => `(${filter})`).join(' or ');
238
+ }
239
+
240
+ export function nextestConfigRelPath(workspaceRoot: string, projectRoot: string, configAbs: string): string {
241
+ const rel = relative(join(workspaceRoot, projectRoot), configAbs);
242
+ return rel.length === 0 ? configAbs : rel.split(sep).join('/');
243
+ }
@@ -3,10 +3,13 @@ import { describe, expect, it } from 'bun:test';
3
3
  import {
4
4
  CARGO_CROSS_LINT_COMMAND,
5
5
  CARGO_CROSS_LINT_TARGET,
6
+ CARGO_LINT_CLIPPY_COMMAND,
6
7
  CARGO_LINUX_TRIPLE,
7
8
  CROSS_CHECK_SCRIPT_COMMAND,
8
9
  CROSS_CHECK_SCRIPT_NAME,
10
+ cargoFrozen,
9
11
  DEVENV_CROSS_PROFILE,
12
+ withProjectCargoHome,
10
13
  } from './cross-check-policy.js';
11
14
  import { BUILD_OUTPUT_DEPENDENCIES, PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
12
15
 
@@ -56,6 +59,14 @@ describe('Linux cross-check policy', () => {
56
59
  expect(CARGO_CROSS_LINT_COMMAND).toContain(`--target ${CARGO_LINUX_TRIPLE}`);
57
60
  });
58
61
 
62
+ it('fails closed on Darwin without the linux-cross compiler instead of compiling', () => {
63
+ // Without this guard a Darwin cache miss sits in ring's cc-rs looking for
64
+ // x86_64-linux-gnu-gcc. The linux-cross profile exports
65
+ // CC_x86_64_unknown_linux_gnu; Linux CI is already the target.
66
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('CC_x86_64_unknown_linux_gnu');
67
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('uname -s');
68
+ });
69
+
59
70
  it('carries the triple explicitly so a host lint cannot pass for a cross one', () => {
60
71
  // The devenv profile deliberately exports no CARGO_BUILD_TARGET. Were the
61
72
  // triple ambient instead, running this target outside the cross shell would
@@ -63,6 +74,19 @@ describe('Linux cross-check policy', () => {
63
74
  expect(CARGO_CROSS_LINT_COMMAND).toContain('--target ');
64
75
  });
65
76
 
77
+ it('gives each project its own CARGO_HOME so parallel clippy does not flock ~/.cargo', () => {
78
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('CARGO_HOME="$PWD/target/cargo-lint-cross-home"');
79
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('ln -sfn "$host_cargo_home/registry"');
80
+ expect(CARGO_LINT_CLIPPY_COMMAND).toContain('CARGO_HOME="$PWD/target/cargo-lint-home"');
81
+ expect(withProjectCargoHome('target/h', 'cargo clippy')).toContain('CARGO_HOME="$PWD/target/h" cargo clippy');
82
+ });
83
+
84
+ it('pins clippy to the committed lockfile and local cache', () => {
85
+ expect(cargoFrozen('clippy --workspace')).toBe('cargo --frozen clippy --workspace');
86
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('cargo --frozen clippy');
87
+ expect(CARGO_LINT_CLIPPY_COMMAND).toContain('cargo --frozen clippy');
88
+ });
89
+
66
90
  it('drives the Nx target through the cross profile from the root script', () => {
67
91
  expect(CROSS_CHECK_SCRIPT_NAME).toBe('check:linux');
68
92
  // Activates the profile...
@@ -38,6 +38,69 @@ export const DEVENV_CROSS_PROFILE = 'linux-cross';
38
38
  */
39
39
  export const CARGO_CROSS_LINT_TARGET = 'cargo-lint-cross';
40
40
 
41
+ const CARGO_CROSS_LINT_GUARD = `[ -n "\${CC_x86_64_unknown_linux_gnu:-}" ] || [ "$(uname -s)" = Linux ] || { echo 'cargo-lint-cross needs the linux-cross C toolchain; run: bun run check:linux' >&2; exit 2; }`;
42
+
43
+ /**
44
+ * Point this cargo at a project-local home so parallel Nx cargos do not
45
+ * exclusive-lock `~/.cargo/.package-cache`. Downloaded crates stay shared.
46
+ */
47
+ export function withProjectCargoHome(homeRel: string, command: string): string {
48
+ return `host_cargo_home="\${CARGO_HOME:-$HOME/.cargo}"; mkdir -p ${homeRel}; if [ -d "$host_cargo_home/registry" ]; then ln -sfn "$host_cargo_home/registry" ${homeRel}/registry; fi; if [ -d "$host_cargo_home/git" ]; then ln -sfn "$host_cargo_home/git" ${homeRel}/git; fi; CARGO_HOME="$PWD/${homeRel}" ${command}`;
49
+ }
50
+
51
+ /**
52
+ * The prefix `cargoFrozen` writes. Exported so target inference can recognize
53
+ * the commands whose precondition `CARGO_FETCH_TARGET` supplies without
54
+ * re-spelling the flag; a second copy would silently stop matching.
55
+ */
56
+ export const CARGO_FROZEN_PREFIX = 'cargo --frozen ';
57
+
58
+ /**
59
+ * `--frozen` is cargo's `--locked` + `--offline`. Cargo.lock and the registry
60
+ * cache are inputs: inferred cargo must not rewrite the lockfile or fetch.
61
+ * The flag sits on `cargo` so nextest sees a cargo-level option, not its own.
62
+ *
63
+ * The registry cache being an INPUT is a precondition, and offline cargo
64
+ * enforces it at RESOLUTION, before it selects a single target: an offline
65
+ * registry source only reports packages already downloaded, so one absent
66
+ * member of the lockfile's graph makes the whole workspace unresolvable with
67
+ * `no matching package named <dep> found`. The failure therefore has nothing to
68
+ * do with which targets a command builds — `build`, `check`, `test --lib` and
69
+ * `test --no-run` all fail identically on a dependency only a bench needs — and
70
+ * narrowing a command's target selection cannot cure it.
71
+ *
72
+ * Dev- and bench-only dependencies are the exposed class. A build downloads the
73
+ * normal dependency graph, so on a runner that builds before it tests those are
74
+ * warm by accident; nothing downloads a dev-dependency until something compiles
75
+ * a test target, and by then `--frozen` refuses to. `CARGO_FETCH_TARGET` below
76
+ * turns that accident into a stated dependency.
77
+ */
78
+ export function cargoFrozen(args: string): string {
79
+ return `${CARGO_FROZEN_PREFIX}${args}`;
80
+ }
81
+
82
+ /**
83
+ * Nx target name. Downloads one Cargo workspace's locked dependency graph so
84
+ * every `cargoFrozen` command in that workspace can resolve offline. No colons,
85
+ * per the repo's target-naming rule, and outside the `-js`/`-napi`/`-wasm` and
86
+ * platform-suffix families so no aggregate sweeps it up.
87
+ */
88
+ export const CARGO_FETCH_TARGET = 'cargo-fetch';
89
+
90
+ /**
91
+ * `--locked` rather than bare `cargo fetch`: this downloads exactly the graph
92
+ * Cargo.lock pins and fails loudly on a stale lockfile. Without it a fetch could
93
+ * re-resolve and hand `--frozen` a cache built from a different graph than the
94
+ * lockfile names, which is the guarantee `--frozen` exists to give.
95
+ *
96
+ * `fetch` and nothing else, because fetching is the only act that needs the
97
+ * network; every compile downstream stays frozen.
98
+ *
99
+ * No `--target`: with no triple named cargo fetches every target's
100
+ * dependencies, so one run also covers `CARGO_CROSS_LINT_TARGET`'s Linux triple.
101
+ */
102
+ export const CARGO_FETCH_COMMAND = 'cargo fetch --locked';
103
+
41
104
  /**
42
105
  * Clippy rather than `cargo check`, and `-D warnings`, because that is exactly
43
106
  * what CI's `lint` runs; a weaker local command would pass where CI fails.
@@ -55,8 +118,24 @@ export const CARGO_CROSS_LINT_TARGET = 'cargo-lint-cross';
55
118
  *
56
119
  * `cargo fmt` is absent on purpose: formatting is target-independent and already
57
120
  * covered by `cargo-lint`, so repeating it here would only cost time.
121
+ *
122
+ * `CARGO_HOME` is per-project (`$PWD/target/cargo-lint-cross-home`). Cargo's
123
+ * package-cache flock lives in CARGO_HOME; three workspaces otherwise serialize
124
+ * on `~/.cargo/.package-cache` even with distinct `--target-dir`. Registry and
125
+ * git are linked to the host home so crates are not re-fetched. `--frozen`
126
+ * keeps that shared registry read-only: lockfile and cache are inputs.
58
127
  */
59
- export const CARGO_CROSS_LINT_COMMAND = `cargo clippy --workspace --all-targets --target ${CARGO_LINUX_TRIPLE} -- -D warnings`;
128
+ export const CARGO_CROSS_LINT_COMMAND = `${CARGO_CROSS_LINT_GUARD}; ${withProjectCargoHome(
129
+ 'target/cargo-lint-cross-home',
130
+ cargoFrozen(
131
+ `clippy --workspace --all-targets --target ${CARGO_LINUX_TRIPLE} --target-dir target/cargo-lint-cross -- -D warnings`,
132
+ ),
133
+ )}`;
134
+
135
+ export const CARGO_LINT_CLIPPY_COMMAND = withProjectCargoHome(
136
+ 'target/cargo-lint-home',
137
+ cargoFrozen('clippy --workspace --all-targets --target-dir target/cargo-lint -- -D warnings'),
138
+ );
60
139
 
61
140
  /** Root `package.json` script name, in the repo's `verb:qualifier` style. */
62
141
  export const CROSS_CHECK_SCRIPT_NAME = 'check:linux';