@smoothbricks/nx-plugin 0.4.1 → 0.4.3

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.
@@ -1,7 +1,8 @@
1
1
  import { existsSync, readdirSync, readFileSync } from 'node:fs';
2
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';
3
+ import { dirname, isAbsolute, join, normalize, posix, relative, resolve, sep } from 'node:path';
4
+ import { readNxJson } from 'nx/src/config/nx-json.js';
5
+ import { parse as parseToml, type TomlTable } from 'smol-toml';
5
6
 
6
7
  export interface CargoWorkspacePackage {
7
8
  name: string;
@@ -29,7 +30,7 @@ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWo
29
30
  return [];
30
31
  }
31
32
  const parsed: unknown = parseToml(readFileSync(workspaceTomlPath, 'utf-8'));
32
- if (!isRecord(parsed) || !isRecord(parsed.workspace) || !Array.isArray(parsed.workspace.members)) {
33
+ if (!isRecord(parsed) || !isRecord(parsed.workspace)) {
33
34
  return [];
34
35
  }
35
36
  const excludedDirs = new Set<string>();
@@ -43,7 +44,9 @@ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWo
43
44
  }
44
45
  }
45
46
  const packageDirs = new Set<string>();
46
- for (const member of parsed.workspace.members) {
47
+ if (isRecord(parsed.package)) packageDirs.add('.');
48
+ const members = Array.isArray(parsed.workspace.members) ? parsed.workspace.members : [];
49
+ for (const member of members) {
47
50
  if (typeof member !== 'string' || member.length === 0) {
48
51
  continue;
49
52
  }
@@ -54,12 +57,14 @@ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWo
54
57
  }
55
58
  }
56
59
  const packages: CargoWorkspacePackage[] = [];
60
+ const workspacePathDeps = workspacePathDependencies(parsed, absoluteProjectRoot);
61
+ const external = new Map<string, string>();
57
62
  for (const memberDir of packageDirs) {
58
63
  const crateTomlPath = join(absoluteProjectRoot, memberDir, 'Cargo.toml');
59
64
  if (!existsSync(crateTomlPath)) {
60
65
  continue;
61
66
  }
62
- const crateParsed: unknown = parseToml(readFileSync(crateTomlPath, 'utf-8'));
67
+ const crateParsed: unknown = memberDir === '.' ? parsed : parseToml(readFileSync(crateTomlPath, 'utf-8'));
63
68
  if (!isRecord(crateParsed) || !isRecord(crateParsed.package) || typeof crateParsed.package.name !== 'string') {
64
69
  continue;
65
70
  }
@@ -68,6 +73,36 @@ export function listCargoWorkspacePackages(absoluteProjectRoot: string): CargoWo
68
73
  dir: memberDir,
69
74
  testShards: readTestShards(crateParsed.package, crateTomlPath),
70
75
  });
76
+ // Cargo automatically includes in-workspace path dependencies as members,
77
+ // except explicitly excluded packages.
78
+ const dependencies: string[] = [];
79
+ enqueuePathDependencies(
80
+ crateParsed,
81
+ memberDir,
82
+ workspacePathDeps,
83
+ dependencies,
84
+ external,
85
+ absoluteProjectRoot,
86
+ absoluteProjectRoot,
87
+ );
88
+ if (isRecord(crateParsed.target)) {
89
+ for (const target of Object.values(crateParsed.target)) {
90
+ if (isRecord(target)) {
91
+ enqueuePathDependencies(
92
+ target,
93
+ memberDir,
94
+ workspacePathDeps,
95
+ dependencies,
96
+ external,
97
+ absoluteProjectRoot,
98
+ absoluteProjectRoot,
99
+ );
100
+ }
101
+ }
102
+ }
103
+ for (const dependency of dependencies) {
104
+ if (!isExcludedCargoMember(dependency, excludedDirs)) packageDirs.add(dependency);
105
+ }
71
106
  }
72
107
  packages.sort((left, right) => left.name.localeCompare(right.name));
73
108
  return packages;
@@ -90,6 +125,7 @@ function isExcludedCargoMember(memberDir: string, excludedDirs: ReadonlySet<stri
90
125
  */
91
126
  function expandCargoMemberDirs(absoluteProjectRoot: string, member: string, requireGlobMatch = true): string[] {
92
127
  const normalized = member.split('\\').join('/').replace(/\/+$/, '');
128
+ if (normalized === '.') return ['.'];
93
129
  const segments = normalized.split('/');
94
130
  if (
95
131
  normalized.length === 0 ||
@@ -170,76 +206,328 @@ export function attributeCargoWorkspacePackages(
170
206
  return attributed;
171
207
  }
172
208
 
173
- export async function cargoPackageTestInputs(
174
- absoluteProjectRoot: string,
175
- memberDir: string,
209
+ /**
210
+ * The named input a workspace must declare when a crate's path dependencies
211
+ * resolve outside the Nx workspace. Nx hashes filesets against the workspace
212
+ * file map only, so a glob pointing above the root — or an absolute path
213
+ * spliced under `{workspaceRoot}` — matches nothing and every cached verdict
214
+ * silently ignores the dependency. A runtime named input can hash any tree.
215
+ */
216
+ export const EXTERNAL_RUST_CRATES_INPUT = 'externalRustCrates';
217
+
218
+ export interface CargoPackageTestInputsOptions {
219
+ /** The Nx workspace root, where `nx.json` declares named inputs. */
220
+ workspaceRoot: string;
221
+ /** The cargo workspace root holding the top-level `Cargo.toml`. */
222
+ absoluteProjectRoot: string;
223
+ memberDir: string;
224
+ inputRoot?: string;
225
+ }
226
+
227
+ export async function cargoPackageTestInputs({
228
+ workspaceRoot,
229
+ absoluteProjectRoot,
230
+ memberDir,
176
231
  inputRoot = '{projectRoot}',
177
- ): Promise<string[]> {
178
- const workspacePathDeps = await workspacePathDependencies(absoluteProjectRoot);
179
- const dirs = new Set<string>([memberDir.split('\\').join('/')]);
180
- const crateParsed: unknown = parseToml(await readFile(join(absoluteProjectRoot, memberDir, 'Cargo.toml'), 'utf-8'));
181
- if (isRecord(crateParsed)) {
182
- for (const tableName of ['dependencies', 'dev-dependencies', 'build-dependencies'] as const) {
183
- const table = crateParsed[tableName];
184
- if (!isRecord(table)) {
185
- continue;
232
+ }: CargoPackageTestInputsOptions): Promise<string[]> {
233
+ const manifests = new Map<string, TomlTable>();
234
+ const loadManifest = async (path: string): Promise<TomlTable> => {
235
+ const normalized = resolve(path);
236
+ const cached = manifests.get(normalized);
237
+ if (cached !== undefined) return cached;
238
+ const parsed = parseToml(await readFile(normalized, 'utf-8'));
239
+ manifests.set(normalized, parsed);
240
+ return parsed;
241
+ };
242
+ const rootManifest = await loadManifest(join(absoluteProjectRoot, 'Cargo.toml'));
243
+ // The closure over in-tree path dependencies, not the direct edge set: a
244
+ // test binary links every crate beneath it, and the chained cargo-test
245
+ // targets order runs without contributing to each other's hash, so a
246
+ // dependency reachable only through another crate would otherwise change
247
+ // nothing in the target that links it.
248
+ const dirs = new Set<string>();
249
+ const external = new Map<string, string>();
250
+ const pending = [normalize(memberDir).split(sep).join('/')];
251
+ const declaredSources = new Set<string>();
252
+ // Workspace patches/replacements can substitute local code for locked sources.
253
+ if (isRecord(rootManifest.patch)) {
254
+ for (const replacements of Object.values(rootManifest.patch)) {
255
+ enqueuePathDependencies(
256
+ { dependencies: replacements },
257
+ '.',
258
+ new Map(),
259
+ pending,
260
+ external,
261
+ absoluteProjectRoot,
262
+ workspaceRoot,
263
+ );
264
+ }
265
+ }
266
+ if (isRecord(rootManifest.replace)) {
267
+ enqueuePathDependencies(
268
+ { dependencies: rootManifest.replace },
269
+ '.',
270
+ new Map(),
271
+ pending,
272
+ external,
273
+ absoluteProjectRoot,
274
+ workspaceRoot,
275
+ );
276
+ }
277
+ while (pending.length > 0) {
278
+ const dir = pending.pop();
279
+ if (dir === undefined || dirs.has(dir)) {
280
+ continue;
281
+ }
282
+ dirs.add(dir);
283
+ const crateTomlPath = join(absoluteProjectRoot, dir, 'Cargo.toml');
284
+ if (!existsSync(crateTomlPath)) {
285
+ continue;
286
+ }
287
+ const crateParsed = await loadManifest(crateTomlPath);
288
+ if (!isRecord(crateParsed)) {
289
+ continue;
290
+ }
291
+ let owner = resolve(absoluteProjectRoot, dir);
292
+ if (isRecord(crateParsed.package) && typeof crateParsed.package.workspace === 'string') {
293
+ owner = resolve(owner, crateParsed.package.workspace);
294
+ if (isOutsideRoot(relative(workspaceRoot, owner))) {
295
+ external.set(`${dir} workspace`, owner);
186
296
  }
187
- for (const [depName, spec] of Object.entries(table)) {
188
- const pathDep = pathDependencyDir(memberDir, depName, spec, workspacePathDeps);
189
- if (pathDep !== null) {
190
- dirs.add(pathDep);
297
+ }
298
+ let workspacePathDeps = new Map<string, string>();
299
+ while (!isOutsideRoot(relative(workspaceRoot, owner))) {
300
+ const manifestPath = join(owner, 'Cargo.toml');
301
+ if (existsSync(manifestPath)) {
302
+ const manifest = await loadManifest(manifestPath);
303
+ if (isRecord(manifest.workspace)) {
304
+ workspacePathDeps = workspacePathDependencies(manifest, owner);
305
+ declaredSources.add(manifestPath);
306
+ break;
307
+ }
308
+ }
309
+ const parent = dirname(owner);
310
+ if (parent === owner) break;
311
+ owner = parent;
312
+ }
313
+ enqueuePathDependencies(crateParsed, dir, workspacePathDeps, pending, external, absoluteProjectRoot, workspaceRoot);
314
+ // All cfg variants can affect a compiled target; do not evaluate Cargo cfg here.
315
+ if (isRecord(crateParsed.target)) {
316
+ for (const target of Object.values(crateParsed.target)) {
317
+ if (isRecord(target)) {
318
+ enqueuePathDependencies(
319
+ target,
320
+ dir,
321
+ workspacePathDeps,
322
+ pending,
323
+ external,
324
+ absoluteProjectRoot,
325
+ workspaceRoot,
326
+ );
327
+ }
328
+ }
329
+ }
330
+ if (isRecord(crateParsed.package) && typeof crateParsed.package.build === 'string') {
331
+ declaredSources.add(resolve(absoluteProjectRoot, dir, crateParsed.package.build));
332
+ }
333
+ for (const targetName of ['lib', 'bin', 'example', 'test', 'bench']) {
334
+ const targets = crateParsed[targetName];
335
+ for (const target of Array.isArray(targets) ? targets : [targets]) {
336
+ if (isRecord(target) && typeof target.path === 'string') {
337
+ declaredSources.add(resolve(absoluteProjectRoot, dir, target.path));
191
338
  }
192
339
  }
193
340
  }
194
341
  }
195
- const inputs: string[] = [`${inputRoot}/Cargo.toml`, `${inputRoot}/Cargo.lock`];
342
+ const inputPath = (absolutePath: string): string => {
343
+ const cargoRelative = relative(absoluteProjectRoot, absolutePath).split(sep).join('/');
344
+ return isOutsideRoot(cargoRelative)
345
+ ? posix.join('{workspaceRoot}', relative(workspaceRoot, absolutePath).split(sep).join('/'))
346
+ : posix.join(inputRoot, cargoRelative);
347
+ };
348
+ const inputs = new Set<string>([`${inputRoot}/Cargo.toml`, `${inputRoot}/Cargo.lock`]);
349
+ const exclusions = new Set<string>();
196
350
  for (const dir of [...dirs].sort()) {
197
- inputs.push(`${inputRoot}/${dir}/**/*.rs`, `${inputRoot}/${dir}/Cargo.toml`);
351
+ const absoluteDir = resolve(absoluteProjectRoot, dir);
352
+ // Cargo packages can compile include_bytes!, headers, schemas and build.rs,
353
+ // not only Rust sources. Nested packages are separate source owners.
354
+ inputs.add(`${inputPath(absoluteDir)}/**/*`);
355
+ const scan = [absoluteDir];
356
+ while (scan.length > 0) {
357
+ const current = scan.pop();
358
+ if (current === undefined || !existsSync(current)) continue;
359
+ for (const entry of readdirSync(current, { withFileTypes: true })) {
360
+ if (!entry.isDirectory()) continue;
361
+ const child = join(current, entry.name);
362
+ if (['target', '.git', 'node_modules', '.nx'].includes(entry.name)) {
363
+ } else if (existsSync(join(child, 'Cargo.toml'))) {
364
+ const childDir = relative(absoluteProjectRoot, child).split(sep).join('/');
365
+ if (!dirs.has(childDir)) exclusions.add(`!${inputPath(child)}/**`);
366
+ } else {
367
+ scan.push(child);
368
+ }
369
+ }
370
+ }
371
+ }
372
+ for (const source of [...declaredSources].sort()) {
373
+ const sourceRelative = relative(workspaceRoot, source);
374
+ if (isOutsideRoot(sourceRelative)) {
375
+ external.set(source, source);
376
+ } else {
377
+ inputs.add(inputPath(source));
378
+ }
379
+ }
380
+ // Cargo/rustup search from the command's working directory upwards. Do not
381
+ // hash unrelated descendant configuration or generated Cargo target caches.
382
+ for (let current = resolve(absoluteProjectRoot); ; current = dirname(current)) {
383
+ for (const config of [
384
+ '.cargo/config',
385
+ '.cargo/config.toml',
386
+ 'rust-toolchain',
387
+ 'rust-toolchain.toml',
388
+ 'rustfmt.toml',
389
+ '.rustfmt.toml',
390
+ 'clippy.toml',
391
+ '.clippy.toml',
392
+ ]) {
393
+ inputs.add(inputPath(join(current, config)));
394
+ if (config === '.cargo/config' || config === '.cargo/config.toml') {
395
+ const configPath = join(current, config);
396
+ if (existsSync(configPath)) {
397
+ const cargoConfig = await loadManifest(configPath);
398
+ if (isRecord(cargoConfig.build) && typeof cargoConfig.build['target-dir'] === 'string') {
399
+ const targetDir = resolve(current, cargoConfig.build['target-dir']);
400
+ if (!isOutsideRoot(relative(workspaceRoot, targetDir))) {
401
+ exclusions.add(`!${inputPath(targetDir)}/**`);
402
+ }
403
+ }
404
+ }
405
+ }
406
+ }
407
+ if (current === resolve(workspaceRoot) || dirname(current) === current) break;
408
+ }
409
+ inputs.add(`${inputRoot}/.config/nextest.toml`);
410
+ for (const generated of ['.git', 'node_modules', '.nx']) {
411
+ inputs.add(`!{workspaceRoot}/**/${generated}/**`);
412
+ }
413
+ inputs.add(`!${inputRoot}/**/target/**`);
414
+ inputs.add('!{workspaceRoot}/**/target/**');
415
+ for (const exclusion of [...exclusions].sort()) inputs.add(exclusion);
416
+ // Arbitrary build-script reads outside package trees, custom output trees,
417
+ // and ambient tools/environment require explicit target or named inputs.
418
+ if (external.size > 0) {
419
+ if (readNxJson(workspaceRoot).namedInputs?.[EXTERNAL_RUST_CRATES_INPUT] === undefined) {
420
+ const listed = [...external]
421
+ .sort(([left], [right]) => left.localeCompare(right))
422
+ .map(([name, dir]) => `${name} -> ${dir}`)
423
+ .join(', ');
424
+ throw new Error(
425
+ `${memberDir}/Cargo.toml references inputs outside the Nx workspace (${listed}) that no fileset can hash; ` +
426
+ `declare namedInputs.${EXTERNAL_RUST_CRATES_INPUT} in ${join(workspaceRoot, 'nx.json')} ` +
427
+ 'with runtime inputs that hash those external sources and configuration',
428
+ );
429
+ }
430
+ inputs.add(EXTERNAL_RUST_CRATES_INPUT);
431
+ }
432
+ return [...inputs];
433
+ }
434
+
435
+ function enqueuePathDependencies(
436
+ scope: Record<string, unknown>,
437
+ memberDir: string,
438
+ workspacePathDeps: Map<string, string>,
439
+ pending: string[],
440
+ external: Map<string, string>,
441
+ absoluteProjectRoot: string,
442
+ workspaceRoot: string,
443
+ ): void {
444
+ for (const tableName of ['dependencies', 'dev-dependencies', 'build-dependencies'] as const) {
445
+ const table = scope[tableName];
446
+ if (!isRecord(table)) {
447
+ continue;
448
+ }
449
+ for (const [depName, spec] of Object.entries(table)) {
450
+ const pathDep = pathDependencyDir(
451
+ memberDir,
452
+ depName,
453
+ spec,
454
+ workspacePathDeps,
455
+ absoluteProjectRoot,
456
+ workspaceRoot,
457
+ );
458
+ if (pathDep === null) {
459
+ continue;
460
+ }
461
+ if (pathDep.external) {
462
+ external.set(depName, pathDep.dir);
463
+ } else {
464
+ pending.push(pathDep.dir);
465
+ }
466
+ }
198
467
  }
199
- inputs.push(`${inputRoot}/**/.cargo/config.toml`, `${inputRoot}/scripts/*.sh`, `!${inputRoot}/**/target/**`);
200
- return inputs;
201
468
  }
202
469
 
203
- async function workspacePathDependencies(absoluteProjectRoot: string): Promise<Map<string, string>> {
204
- const parsed: unknown = parseToml(await readFile(join(absoluteProjectRoot, 'Cargo.toml'), 'utf-8'));
470
+ function workspacePathDependencies(parsed: unknown, root: string): Map<string, string> {
205
471
  const deps = new Map<string, string>();
206
472
  if (!isRecord(parsed) || !isRecord(parsed.workspace) || !isRecord(parsed.workspace.dependencies)) {
207
473
  return deps;
208
474
  }
209
475
  for (const [name, spec] of Object.entries(parsed.workspace.dependencies)) {
210
476
  if (isRecord(spec) && typeof spec.path === 'string') {
211
- deps.set(name, spec.path.split('\\').join('/'));
477
+ const path = spec.path.split('\\').join('/');
478
+ deps.set(name, resolve(root, path));
212
479
  }
213
480
  }
214
481
  return deps;
215
482
  }
216
483
 
484
+ interface PathDependencyDir {
485
+ /** Cargo-workspace-relative directory, or the raw path when external. */
486
+ dir: string;
487
+ /** Resolves outside the Nx workspace root, regardless of spelling. */
488
+ external: boolean;
489
+ }
490
+
491
+ /**
492
+ * Resolve inherited dependencies from their owning workspace and local paths
493
+ * from their member directory. Paths inside Nx remain in the ordinary source
494
+ * closure even when they escape the Cargo workspace; paths outside Nx require
495
+ * an explicit runtime named input.
496
+ */
217
497
  function pathDependencyDir(
218
498
  memberDir: string,
219
499
  depName: string,
220
500
  spec: unknown,
221
501
  workspacePathDeps: Map<string, string>,
222
- ): string | null {
223
- if (spec === undefined || spec === null) {
224
- return null;
225
- }
226
- if (typeof spec === 'string') {
227
- return null;
228
- }
502
+ absoluteProjectRoot: string,
503
+ workspaceRoot: string,
504
+ ): PathDependencyDir | null {
229
505
  if (!isRecord(spec)) {
230
506
  return null;
231
507
  }
508
+ let raw: string;
232
509
  if (spec.workspace === true) {
233
- return workspacePathDeps.get(depName) ?? null;
234
- }
235
- if (typeof spec.path !== 'string') {
236
- return null;
237
- }
238
- const resolved = normalize(join(memberDir, spec.path));
239
- if (resolved.startsWith('..')) {
510
+ const workspacePath = workspacePathDeps.get(depName);
511
+ if (workspacePath === undefined) {
512
+ return null;
513
+ }
514
+ raw = workspacePath;
515
+ } else if (typeof spec.path === 'string') {
516
+ const localPath = spec.path.split('\\').join('/');
517
+ raw =
518
+ isAbsolute(localPath) || posix.isAbsolute(localPath)
519
+ ? localPath
520
+ : posix.join(memberDir.split('\\').join('/'), localPath);
521
+ } else {
240
522
  return null;
241
523
  }
242
- return resolved.split(sep).join(posix.sep);
524
+ const absolute = resolve(absoluteProjectRoot, raw);
525
+ const local = relative(absoluteProjectRoot, absolute).split(sep).join(posix.sep) || '.';
526
+ return { dir: local, external: isOutsideRoot(relative(workspaceRoot, absolute)) };
527
+ }
528
+
529
+ function isOutsideRoot(path: string): boolean {
530
+ return path === '..' || path.startsWith(`..${sep}`) || isAbsolute(path);
243
531
  }
244
532
 
245
533
  function isRecord(value: unknown): value is Record<string, unknown> {
@@ -258,8 +546,7 @@ function isRecord(value: unknown): value is Record<string, unknown> {
258
546
  *
259
547
  * It is declared rather than inferred because the only honest input is how long
260
548
  * the suite takes, which target inference cannot measure. Defaulting to 1 keeps
261
- * small crates on a single target: cowshed-cli's 226 tests run in 6.5s, so
262
- * sharding them would buy nothing and pay a per-target cargo freshness check.
549
+ * small crates on a single target without paying another Cargo freshness check.
263
550
  */
264
551
  function readTestShards(cratePackage: Record<string, unknown>, crateTomlPath: string): number {
265
552
  const metadata = isRecord(cratePackage.metadata) ? cratePackage.metadata : null;
@@ -294,7 +581,7 @@ export const CARGO_TEST_EXCEPTIONS_SUFFIX = 'exceptions';
294
581
 
295
582
  /**
296
583
  * A crate on one target keeps the bare name; a split crate suffixes the piece,
297
- * so `cargo-test-cowshed-core-shard2` still reads as "cowshed-core's tests".
584
+ * so `cargo-test-example-core-shard2` still reads as "example-core's tests".
298
585
  */
299
586
  export function cargoTestPackageTargetName(packageName: string, piece?: string): string {
300
587
  const base = `cargo-test-${packageName}`;
@@ -319,24 +606,10 @@ export function packageNameFromCargoTestTarget(targetName: string): string | nul
319
606
  /**
320
607
  * The tests nextest.toml singles out with an override, as one filterset.
321
608
  *
322
- * An override is the config author saying "this class does not behave like the
323
- * rest of the suite", and both classes declared today break a shard, each in a
324
- * different way:
325
- *
326
- * - a `test-group` is scoped to a single nextest RUN, so letting the hash
327
- * scatter its members across shards silently dissolves it. The real-APFS
328
- * group exists because those tests contend on Disk Arbitration
329
- * machine-wide, where an unscoped `diskutil apfs list -plist` measured 0.7s
330
- * idle against 14.4s while another process is attaching.
331
- * - a raised `slow-timeout` marks a test whose cost is not the suite's cost.
332
- * `lesser_capabilities_fail_to_compile_...` rustc's a fixture that
333
- * `cargo test --no-run` cannot pre-build, so it costs 25.6s on a cold
334
- * target directory against 1.8s warm. Every CI runner is cold, and left in
335
- * a shard that spike lands on whichever 250-odd tests share it.
336
- *
337
- * One target for all of them rather than one each: they sit in different groups
338
- * so they run concurrently, making the combined wall the max of the classes
339
- * rather than their sum.
609
+ * Overrides can encode constraints that must survive sharding: a test group
610
+ * limits concurrency within one nextest run, while a raised slow timeout can
611
+ * identify expensive tests that should not delay an otherwise bounded shard.
612
+ * Keep the configured exceptions in one run so group limits remain effective.
340
613
  *
341
614
  * Deriving this from the config that declares the classes, rather than
342
615
  * restating their filters, means adding an override there is the whole change —
@@ -1,4 +1,7 @@
1
1
  import { describe, expect, it } from 'bun:test';
2
+ import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
2
5
 
3
6
  import {
4
7
  CARGO_CROSS_LINT_COMMAND,
@@ -9,7 +12,6 @@ import {
9
12
  CROSS_CHECK_SCRIPT_NAME,
10
13
  cargoFrozen,
11
14
  DEVENV_CROSS_PROFILE,
12
- withProjectCargoHome,
13
15
  } from './cross-check-policy.js';
14
16
  import { BUILD_OUTPUT_DEPENDENCIES, PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
15
17
 
@@ -74,12 +76,40 @@ describe('Linux cross-check policy', () => {
74
76
  expect(CARGO_CROSS_LINT_COMMAND).toContain('--target ');
75
77
  });
76
78
 
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
- });
79
+ it('preserves Cargo home configuration and its relative paths while linting', async () => {
80
+ const root = await mkdtemp(join(tmpdir(), 'cargo-lint-config-'));
81
+ const home = join(root, 'home');
82
+ const project = join(root, 'project');
83
+ try {
84
+ await mkdir(home);
85
+ await mkdir(join(project, 'src'), { recursive: true });
86
+ await writeFile(join(home, 'config.toml'), '[env]\nCONFIG_DATA = { value = "data.txt", relative = true }\n');
87
+ await writeFile(join(root, 'data.txt'), 'configuration-relative data\n');
88
+ await writeFile(
89
+ join(project, 'Cargo.toml'),
90
+ '[package]\nname = "config-fixture"\nversion = "0.1.0"\nedition = "2021"\n[workspace]\n',
91
+ );
92
+ await writeFile(
93
+ join(project, 'Cargo.lock'),
94
+ 'version = 4\n\n[[package]]\nname = "config-fixture"\nversion = "0.1.0"\n',
95
+ );
96
+ await writeFile(join(project, 'src/lib.rs'), 'pub const DATA: &str = include_str!(env!("CONFIG_DATA"));\n');
97
+ const child = Bun.spawn(['sh', '-c', CARGO_LINT_CLIPPY_COMMAND], {
98
+ cwd: project,
99
+ env: { ...process.env, CARGO_HOME: home, RUSTC_WRAPPER: '', RUSTC_WORKSPACE_WRAPPER: '' },
100
+ stdout: 'pipe',
101
+ stderr: 'pipe',
102
+ });
103
+ const [exitCode, stdout, stderr] = await Promise.all([
104
+ child.exited,
105
+ new Response(child.stdout).text(),
106
+ new Response(child.stderr).text(),
107
+ ]);
108
+ expect({ exitCode, output: stdout + stderr }).toMatchObject({ exitCode: 0 });
109
+ } finally {
110
+ await rm(root, { recursive: true, force: true });
111
+ }
112
+ }, 30_000);
83
113
 
84
114
  it('pins clippy to the committed lockfile and local cache', () => {
85
115
  expect(cargoFrozen('clippy --workspace')).toBe('cargo --frozen clippy --workspace');
@@ -40,14 +40,6 @@ export const CARGO_CROSS_LINT_TARGET = 'cargo-lint-cross';
40
40
 
41
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
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
43
  /**
52
44
  * The prefix `cargoFrozen` writes. Exported so target inference can recognize
53
45
  * the commands whose precondition `CARGO_FETCH_TARGET` supplies without
@@ -119,22 +111,18 @@ export const CARGO_FETCH_COMMAND = 'cargo fetch --locked';
119
111
  * `cargo fmt` is absent on purpose: formatting is target-independent and already
120
112
  * covered by `cargo-lint`, so repeating it here would only cost time.
121
113
  *
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.
114
+ * Keep the caller's CARGO_HOME: it owns registry sources, credentials and global
115
+ * Cargo configuration, including paths relative to that configuration. Relocating
116
+ * or replaying it through --config changes path resolution or precedence.
117
+ * Cargo may serialize registry access on its package-cache lock; these checks
118
+ * are unbounded, and correctness takes priority over overlapping that access.
127
119
  */
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
- ),
120
+ export const CARGO_CROSS_LINT_COMMAND = `${CARGO_CROSS_LINT_GUARD}; ${cargoFrozen(
121
+ `clippy --workspace --all-targets --target ${CARGO_LINUX_TRIPLE} --target-dir target/cargo-lint-cross -- -D warnings`,
133
122
  )}`;
134
123
 
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'),
124
+ export const CARGO_LINT_CLIPPY_COMMAND = cargoFrozen(
125
+ 'clippy --workspace --all-targets --target-dir target/cargo-lint -- -D warnings',
138
126
  );
139
127
 
140
128
  /** Root `package.json` script name, in the repo's `verb:qualifier` style. */
@@ -1,7 +1,7 @@
1
1
  import { afterAll, beforeAll, describe, expect, it } from 'bun:test';
2
2
  import { spawn } from 'node:child_process';
3
3
  import { existsSync } from 'node:fs';
4
- import { chmod, mkdir, mkdtemp, realpath, rm, symlink, writeFile } from 'node:fs/promises';
4
+ import { chmod, mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
5
5
  import { tmpdir } from 'node:os';
6
6
  import { dirname, join } from 'node:path';
7
7
  import { fileURLToPath } from 'node:url';
@@ -280,6 +280,11 @@ describe('smoo-nx-exec', () => {
280
280
  // Realpath because macOS puts the temp directory behind a /private
281
281
  // symlink, and a process's reported cwd is the resolved one.
282
282
  workspace = await realpath(await mkdtemp(join(tmpdir(), 'ensure-built-')));
283
+ // Cowshed's scratch is ignored by its enclosing Git checkout. This is an
284
+ // independent Nx workspace: its own Git boundary keeps the daemon's
285
+ // ignore-aware watcher from dropping all of its source changes.
286
+ const initialized = Bun.spawnSync(['git', 'init', '--quiet', workspace]);
287
+ expect(initialized.exitCode).toBe(0);
283
288
  await mkdir(join(workspace, 'packages', 'app'), { recursive: true });
284
289
  await mkdir(join(workspace, 'packages', 'lib'), { recursive: true });
285
290
  // The repository's own node_modules, so the fixture resolves the same Nx
@@ -433,9 +438,9 @@ describe('smoo-nx-exec', () => {
433
438
 
434
439
  it('runs again when a dependency\u0027s own inputs change', async () => {
435
440
  await writeFile(join(workspace, 'packages', 'lib', 'source.txt'), 'lib changed\n');
436
- const changed = await runBin(workspace, ['app:build', '--', './report'], { NX_VERBOSE_LOGGING: 'true' });
441
+ const changed = await runBin(workspace, ['app:build', '--', './report']);
437
442
  expect(changed.code).toBe(0);
438
- expect(changed.stderr).toContain('lib:build has no cached result');
443
+ expect(await readFile(join(workspace, 'packages', 'lib', 'dist', 'lib.txt'), 'utf-8')).toBe('lib changed\n');
439
444
 
440
445
  // And the graph settles back to silence, which is only reachable if the
441
446
  // dependent-outputs task was hashed too.