@smoothbricks/nx-plugin 0.4.4 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -512,6 +512,73 @@ describe('cargo-test reachability policy', () => {
512
512
  }
513
513
  });
514
514
 
515
+ it('treats the workspace nextest archive as a build, never as a runner or a crate', async () => {
516
+ const root = await mkdtemp(join(tmpdir(), 'cargo-reach-archive-'));
517
+ try {
518
+ await writeJson(join(root, 'package.json'), { name: '@scope/root', private: true, workspaces: ['packages/*'] });
519
+ await writeJson(join(root, 'packages/rusty/package.json'), { name: 'rusty', nx: { name: 'rusty' } });
520
+ await mkdir(join(root, 'packages/rusty/crates/core'), { recursive: true });
521
+ await writeFile(join(root, 'packages/rusty/Cargo.toml'), '[workspace]\nmembers = ["crates/core"]\n');
522
+ await writeFile(join(root, 'packages/rusty/crates/core/Cargo.toml'), '[package]\nname = "rusty-core"\n');
523
+
524
+ // `cargo nextest archive` compiles test binaries and packs them. It is
525
+ // the same silent green as `--no-run`: the word "nextest" appears, no
526
+ // test runs. And `cargo-test-archive` is not a crate called "archive".
527
+ const archiveOptions = new Map<string, Record<string, unknown>>([
528
+ [
529
+ 'cargo-test-archive',
530
+ { command: 'cargo --frozen nextest archive --workspace --archive-file target/nextest/archive.tar.zst' },
531
+ ],
532
+ ]);
533
+ const archiveOnly: ResolvedProjectTargets = {
534
+ root: 'packages/rusty',
535
+ targets: new Set(['test', 'cargo-test', 'cargo-test-archive']),
536
+ targetDependencies: new Map([
537
+ ['test', ['cargo-test']],
538
+ ['cargo-test', ['cargo-test-archive']],
539
+ ]),
540
+ targetExecutors: new Map([
541
+ ['test', 'nx:noop'],
542
+ ['cargo-test', 'nx:noop'],
543
+ ['cargo-test-archive', 'nx:run-commands'],
544
+ ]),
545
+ targetOptions: archiveOptions,
546
+ };
547
+ const issues = checkWorkspaceCargoTestReachabilityPolicy(root, {
548
+ resolvedTargetsByProject: new Map([['rusty', archiveOnly]]),
549
+ });
550
+ expect(issues).toHaveLength(1);
551
+ expect(issues[0]?.message).toContain('RUNS tests');
552
+
553
+ const withRunner: ResolvedProjectTargets = {
554
+ ...archiveOnly,
555
+ targets: new Set([...archiveOnly.targets, 'cargo-test-rusty-core']),
556
+ targetDependencies: new Map([
557
+ ['test', ['cargo-test']],
558
+ ['cargo-test', ['cargo-test-rusty-core']],
559
+ ['cargo-test-rusty-core', ['cargo-test-archive']],
560
+ ]),
561
+ targetOptions: new Map([
562
+ ...archiveOptions,
563
+ [
564
+ 'cargo-test-rusty-core',
565
+ {
566
+ command:
567
+ "cargo --frozen nextest run --archive-file target/nextest/archive.tar.zst --workspace-remap . -E 'package(rusty-core)'",
568
+ },
569
+ ],
570
+ ]),
571
+ };
572
+ expect(
573
+ checkWorkspaceCargoTestReachabilityPolicy(root, {
574
+ resolvedTargetsByProject: new Map([['rusty', withRunner]]),
575
+ }),
576
+ ).toEqual([]);
577
+ } finally {
578
+ await rm(root, { recursive: true, force: true });
579
+ }
580
+ });
581
+
515
582
  it('ignores projects with no cargo-test target', async () => {
516
583
  const root = await mkdtemp(join(tmpdir(), 'cargo-reach-ts-'));
517
584
  try {
@@ -207,9 +207,12 @@ export function checkWorkspaceCargoTestReachabilityPolicy(
207
207
 
208
208
  /**
209
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.
210
+ * mode that executes tests. The two workspace-wide preparation targets are
211
+ * deliberately excluded: `cargo-test-compile` is `cargo test --no-run` and
212
+ * `cargo-test-archive` is `cargo nextest archive`. Both prove the binaries
213
+ * build and nothing about them running, and both say "test"/"nextest" in their
214
+ * command — a closure holding only these is the silent green this check exists
215
+ * to catch.
213
216
  */
214
217
  function resolvedTargetRunsTests(project: ResolvedProjectTargets, target: string): boolean {
215
218
  const visiting = new Set<string>();
@@ -219,7 +222,11 @@ function resolvedTargetRunsTests(project: ResolvedProjectTargets, target: string
219
222
  }
220
223
  visiting.add(targetName);
221
224
  const command = commandOf(project, targetName);
222
- const runs = command !== undefined && /\btest\b|\bnextest\b/.test(command) && !command.includes('--no-run');
225
+ const runs =
226
+ command !== undefined &&
227
+ /\btest\b|\bnextest\b/.test(command) &&
228
+ !command.includes('--no-run') &&
229
+ !command.includes('nextest archive');
223
230
  const reached =
224
231
  runs ||
225
232
  (project.targetDependencies?.get(targetName) ?? []).some((dependency) =>
@@ -222,21 +222,49 @@ export interface CargoPackageTestInputsOptions {
222
222
  absoluteProjectRoot: string;
223
223
  memberDir: string;
224
224
  inputRoot?: string;
225
+ /** Shared across every call of one graph computation; see {@link createCargoInputsCache}. */
226
+ cache?: CargoInputsCache;
225
227
  }
226
228
 
227
- export async function cargoPackageTestInputs({
228
- workspaceRoot,
229
- absoluteProjectRoot,
230
- memberDir,
231
- inputRoot = '{projectRoot}',
232
- }: CargoPackageTestInputsOptions): Promise<string[]> {
233
- const manifests = new Map<string, TomlTable>();
234
- const loadManifest = async (path: string): Promise<TomlTable> => {
229
+ /**
230
+ * Memo for one project-graph computation. Every crate's compile, lint and
231
+ * test targets derive the same closure, and every crate re-reads the same
232
+ * manifests and re-walks the same trees for nested `Cargo.toml` files; with
233
+ * three targets over ~45 crates that was ~135 derivations and the dominant
234
+ * cost of loading the graph. The cache lives exactly as long as one
235
+ * `createNodes` call — Nx recomputes the graph when a manifest changes, so
236
+ * nothing here can go stale across runs.
237
+ */
238
+ export interface CargoInputsCache {
239
+ readonly manifests: Map<string, Promise<TomlTable>>;
240
+ readonly nestedCrateDirs: Map<string, string[]>;
241
+ readonly results: Map<string, Promise<string[]>>;
242
+ }
243
+
244
+ export function createCargoInputsCache(): CargoInputsCache {
245
+ return { manifests: new Map(), nestedCrateDirs: new Map(), results: new Map() };
246
+ }
247
+
248
+ export function cargoPackageTestInputs(options: CargoPackageTestInputsOptions): Promise<string[]> {
249
+ const cache = options.cache ?? createCargoInputsCache();
250
+ const key = `${options.absoluteProjectRoot}\0${options.memberDir}\0${options.inputRoot ?? '{projectRoot}'}`;
251
+ const cached = cache.results.get(key);
252
+ if (cached !== undefined) return cached;
253
+ const derived = deriveCargoPackageTestInputs(options, cache);
254
+ cache.results.set(key, derived);
255
+ return derived;
256
+ }
257
+
258
+ async function deriveCargoPackageTestInputs(
259
+ { workspaceRoot, absoluteProjectRoot, memberDir, inputRoot = '{projectRoot}' }: CargoPackageTestInputsOptions,
260
+ cache: CargoInputsCache,
261
+ ): Promise<string[]> {
262
+ const loadManifest = (path: string): Promise<TomlTable> => {
235
263
  const normalized = resolve(path);
236
- const cached = manifests.get(normalized);
264
+ const cached = cache.manifests.get(normalized);
237
265
  if (cached !== undefined) return cached;
238
- const parsed = parseToml(await readFile(normalized, 'utf-8'));
239
- manifests.set(normalized, parsed);
266
+ const parsed = readFile(normalized, 'utf-8').then(parseToml);
267
+ cache.manifests.set(normalized, parsed);
240
268
  return parsed;
241
269
  };
242
270
  const rootManifest = await loadManifest(join(absoluteProjectRoot, 'Cargo.toml'));
@@ -352,21 +380,9 @@ export async function cargoPackageTestInputs({
352
380
  // Cargo packages can compile include_bytes!, headers, schemas and build.rs,
353
381
  // not only Rust sources. Nested packages are separate source owners.
354
382
  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
- }
383
+ for (const child of nestedCrateDirs(absoluteDir, cache)) {
384
+ const childDir = relative(absoluteProjectRoot, child).split(sep).join('/');
385
+ if (!dirs.has(childDir)) exclusions.add(`!${inputPath(child)}/**`);
370
386
  }
371
387
  }
372
388
  for (const source of [...declaredSources].sort()) {
@@ -432,6 +448,30 @@ export async function cargoPackageTestInputs({
432
448
  return [...inputs];
433
449
  }
434
450
 
451
+ /** Every directory beneath `root` that holds its own `Cargo.toml`, the walk stopping at each. */
452
+ function nestedCrateDirs(root: string, cache: CargoInputsCache): string[] {
453
+ const cached = cache.nestedCrateDirs.get(root);
454
+ if (cached !== undefined) return cached;
455
+ const found: string[] = [];
456
+ const scan = [root];
457
+ while (scan.length > 0) {
458
+ const current = scan.pop();
459
+ if (current === undefined || !existsSync(current)) continue;
460
+ for (const entry of readdirSync(current, { withFileTypes: true })) {
461
+ if (!entry.isDirectory()) continue;
462
+ const child = join(current, entry.name);
463
+ if (['target', '.git', 'node_modules', '.nx'].includes(entry.name)) {
464
+ } else if (existsSync(join(child, 'Cargo.toml'))) {
465
+ found.push(child);
466
+ } else {
467
+ scan.push(child);
468
+ }
469
+ }
470
+ }
471
+ cache.nestedCrateDirs.set(root, found);
472
+ return found;
473
+ }
474
+
435
475
  function enqueuePathDependencies(
436
476
  scope: Record<string, unknown>,
437
477
  memberDir: string,
@@ -572,6 +612,23 @@ export const CARGO_TEST_TARGET = 'cargo-test';
572
612
  /** Prerequisite that compiles the workspace; not a per-crate runner. */
573
613
  export const CARGO_TEST_COMPILE_TARGET = 'cargo-test-compile';
574
614
 
615
+ /**
616
+ * The one workspace-wide `cargo nextest archive` every per-crate runner
617
+ * executes from. Named here beside the other cargo target names so the policy
618
+ * that checks a workspace's test graph and the inference that builds it read
619
+ * the same string.
620
+ */
621
+ export const CARGO_TEST_ARCHIVE_TARGET = 'cargo-test-archive';
622
+
623
+ /**
624
+ * Where that archive lands, relative to the cargo workspace root — the cwd both
625
+ * the archive command and every runner already use. `cargo nextest archive`
626
+ * does NOT create this parent directory and fails the whole build when it is
627
+ * missing ("error writing to archive ... No such file or directory"), so the
628
+ * target mkdir's it first.
629
+ */
630
+ export const CARGO_TEST_ARCHIVE_FILE = 'target/nextest/archive.tar.zst';
631
+
575
632
  /**
576
633
  * Tests that nextest.toml singles out are pinned to this suffix instead of
577
634
  * being sharded. Only a sharded crate has one: an unsharded crate runs its
@@ -592,9 +649,17 @@ export function cargoTestPackageTargetName(packageName: string, piece?: string):
592
649
  * Inverse of `cargoTestPackageTargetName`, used by the reachability policy to
593
650
  * check that the per-crate targets cover every workspace member. Pieces of one
594
651
  * crate collapse back to that crate, so a split crate counts as covered once.
652
+ *
653
+ * The two workspace-wide `cargo-test-*` targets are not crates: reading them as
654
+ * one would invent members named "compile" and "archive" and report every real
655
+ * crate's coverage against a set that can never match.
595
656
  */
596
657
  export function packageNameFromCargoTestTarget(targetName: string): string | null {
597
- if (!targetName.startsWith('cargo-test-') || targetName === CARGO_TEST_COMPILE_TARGET) {
658
+ if (
659
+ !targetName.startsWith('cargo-test-') ||
660
+ targetName === CARGO_TEST_COMPILE_TARGET ||
661
+ targetName === CARGO_TEST_ARCHIVE_TARGET
662
+ ) {
598
663
  return null;
599
664
  }
600
665
  const name = targetName
@@ -639,7 +704,30 @@ export function exceptionalTestFilter(nextestConfigPath: string): string | null
639
704
  return unique.length === 0 ? null : unique.map((filter) => `(${filter})`).join(' or ');
640
705
  }
641
706
 
642
- export function nextestConfigRelPath(workspaceRoot: string, projectRoot: string, configAbs: string): string {
707
+ /**
708
+ * The plugin's own nextest settings, as a config layer BENEATH the repository's.
709
+ *
710
+ * `--config-file` replaces `<workspace>/.config/nextest.toml` outright, which
711
+ * made every plugin default unoverridable and — now that per-crate runs execute
712
+ * from an archive — left a repository no way to declare `archive.include` for a
713
+ * cdylib or fixture its tests need. `--tool-config-file` is nextest's mechanism
714
+ * for exactly this: "lower than --config-file in priority but above the default
715
+ * config shipped with nextest". Measured: with only this layer a 1s
716
+ * slow-timeout terminates a 3s test; adding `.config/nextest.toml` with 10s
717
+ * lets the same test pass.
718
+ *
719
+ * The path must be absolute, and an absolute path written into the command text
720
+ * would differ per checkout and split one Nx cache entry per machine. `$PWD`
721
+ * resolves at exec time — both the archive (`nx:run-commands`) and the runners
722
+ * (`bounded-exec`, which spawns with `shell: true`) go through a shell — so the
723
+ * command text stays identical everywhere. Quoted, because a workspace path may
724
+ * contain spaces.
725
+ */
726
+ export function nextestToolConfigArg(workspaceRoot: string, projectRoot: string, configAbs: string): string {
643
727
  const rel = relative(join(workspaceRoot, projectRoot), configAbs);
644
- return rel.length === 0 ? configAbs : rel.split(sep).join('/');
728
+ const path = rel.length === 0 ? configAbs : rel.split(sep).join('/');
729
+ return `--tool-config-file "smoo:$PWD/${path}"`;
645
730
  }
731
+
732
+ /** The repository's own nextest config, which now layers over the plugin's. */
733
+ export const NEXTEST_REPO_CONFIG_PATH = '.config/nextest.toml';