@smoothbricks/nx-plugin 0.4.24 → 0.4.26

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 (58) hide show
  1. package/README.md +147 -37
  2. package/dist/bin/smoo-nx-exec.js +3 -14
  3. package/dist/cargo-closure-input.d.ts +13 -15
  4. package/dist/cargo-closure-input.d.ts.map +1 -1
  5. package/dist/cargo-closure-input.js +29 -37
  6. package/dist/cargo-source-hash.d.ts +12 -7
  7. package/dist/cargo-source-hash.d.ts.map +1 -1
  8. package/dist/cargo-source-hash.js +416 -32
  9. package/dist/cargo-workspace.d.ts +7 -7
  10. package/dist/cargo-workspace.js +8 -8
  11. package/dist/ensure-built.d.ts +6 -0
  12. package/dist/ensure-built.d.ts.map +1 -1
  13. package/dist/ensure-built.js +79 -6
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +69 -96
  16. package/dist/managed-assets.d.ts +6 -0
  17. package/dist/managed-assets.d.ts.map +1 -1
  18. package/dist/managed-files/files.d.ts.map +1 -1
  19. package/dist/managed-files/files.js +6 -0
  20. package/dist/manifest-hash.d.ts.map +1 -1
  21. package/dist/manifest-hash.js +6 -2
  22. package/dist/workspace-manifest.d.ts +0 -1
  23. package/dist/workspace-manifest.d.ts.map +1 -1
  24. package/managed/raw/envrc +15 -0
  25. package/managed/raw/git-format-staged.yml +1 -0
  26. package/managed/raw/tooling/direnv/developer-links.ts +43 -11
  27. package/managed/raw/tooling/direnv/devenv.smoo.nix +5 -0
  28. package/managed/raw/tooling/direnv/inherited-devenv.ts +665 -0
  29. package/managed/raw/tooling/direnv/setup-environment.ts +123 -7
  30. package/nextest.toml +3 -37
  31. package/package.json +40 -3
  32. package/src/__tests__/counted-cargo-process.ts +286 -0
  33. package/src/__tests__/counted-cargo.ts +711 -0
  34. package/src/__tests__/fixture-cargo-home.ts +36 -0
  35. package/src/__tests__/fixture-nx-env.ts +93 -3
  36. package/src/bin/smoo-nx-exec.ts +3 -14
  37. package/src/bounded-test-policy.test.ts +0 -1
  38. package/src/cargo-closure-input.test.ts +313 -15
  39. package/src/cargo-closure-input.ts +42 -50
  40. package/src/cargo-resolve-cache.test.ts +975 -0
  41. package/src/cargo-source-hash.test.ts +17 -3
  42. package/src/cargo-source-hash.ts +447 -37
  43. package/src/cargo-workspace.ts +8 -8
  44. package/src/ensure-built.test.ts +122 -20
  45. package/src/ensure-built.ts +82 -5
  46. package/src/index.test.ts +203 -107
  47. package/src/index.ts +86 -115
  48. package/src/managed-assets.ts +12 -0
  49. package/src/managed-files/files.ts +6 -0
  50. package/src/manifest-hash.ts +7 -2
  51. package/src/nextest-partitions.test.ts +150 -0
  52. package/src/nx-cache-bound.test.ts +114 -0
  53. package/src/nx-resident-graph-workers.test.ts +121 -0
  54. package/src/nx-store-resolution.test.ts +98 -0
  55. package/src/task-history-namespace.test.ts +183 -0
  56. package/src/workspace-manifest.ts +0 -1
  57. package/test-timings.json +35 -0
  58. package/src/__tests__/in-process-nx-index.ts +0 -9
package/src/index.ts CHANGED
@@ -433,6 +433,19 @@ function cargoRuntimeInput(projectRoot: string, command: string): { runtime: str
433
433
  return { runtime: `cd ${quotedRoot} && ${command}` };
434
434
  }
435
435
 
436
+ /**
437
+ * One shell command that runs `steps` in order and stops at the first failure.
438
+ *
439
+ * For a step that CONSUMES what an earlier step produces — the archive needs
440
+ * its directory, `wasm-bindgen` reads the `.wasm` that `cargo build` writes.
441
+ * The order lives in the command because that is the only place a
442
+ * producer-consumer pair inside one task can state it: `nx:run-commands`'
443
+ * `commands` array is a set of independent commands that Nx runs side by side.
444
+ */
445
+ function sequence(steps: readonly string[]): string {
446
+ return steps.join(' && ');
447
+ }
448
+
436
449
  /**
437
450
  * Everything a target actually runs, as one string. `nx:run-commands` accepts
438
451
  * either spelling and inference asks four questions of the text — does it run
@@ -567,14 +580,15 @@ function createCargoWasmTarget(
567
580
  ): TargetConfiguration {
568
581
  const outputDirectory = repoRooted ? posix.join(projectRoot, config.outputDirectory) : config.outputDirectory;
569
582
  const cargoSelection = repoRooted ? `-p ${config.cargoPackage}` : `--manifest-path ${config.manifestPath}`;
570
- const commands = (profile: CargoProfile): string[] => [
571
- cargoFrozen(`build${CARGO_PROFILE_FLAG[profile]} --target wasm32-unknown-unknown ${cargoSelection}`),
572
- ...config.targets.map(
573
- ({ bindgenTarget, outputName }) =>
574
- `wasm-bindgen --target ${bindgenTarget} --out-dir ${outputDirectory}/${outputName} ` +
575
- `${cargoTargetDirectory}/wasm32-unknown-unknown/${CARGO_PROFILE_OUTPUT_DIR[profile]}/${config.libraryName}.wasm`,
576
- ),
577
- ];
583
+ const buildCommand = (profile: CargoProfile): string =>
584
+ sequence([
585
+ cargoFrozen(`build${CARGO_PROFILE_FLAG[profile]} --target wasm32-unknown-unknown ${cargoSelection}`),
586
+ ...config.targets.map(
587
+ ({ bindgenTarget, outputName }) =>
588
+ `wasm-bindgen --target ${bindgenTarget} --out-dir ${outputDirectory}/${outputName} ` +
589
+ `${cargoTargetDirectory}/wasm32-unknown-unknown/${CARGO_PROFILE_OUTPUT_DIR[profile]}/${config.libraryName}.wasm`,
590
+ ),
591
+ ]);
578
592
  return {
579
593
  executor: 'nx:run-commands',
580
594
  cache: true,
@@ -582,11 +596,10 @@ function createCargoWasmTarget(
582
596
  inputs: repoRooted ? REPO_ROOT_CARGO_OUTPUT_INPUTS : CARGO_OUTPUT_INPUTS,
583
597
  outputs: [`{projectRoot}/${config.outputDirectory}`],
584
598
  options: {
585
- commands: commands('dev'),
599
+ command: buildCommand('dev'),
586
600
  cwd: repoRooted ? '.' : projectRoot,
587
- parallel: false,
588
601
  },
589
- configurations: { [RELEASE_CONFIGURATION]: { commands: commands('release') } },
602
+ configurations: { [RELEASE_CONFIGURATION]: { command: buildCommand('release') } },
590
603
  };
591
604
  }
592
605
 
@@ -597,11 +610,11 @@ function createCargoWasmTarget(
597
610
  * in its own unbounded target. Cargo reuses incremental compilation locally;
598
611
  * Nx cannot cache this warming action without restoring shared mutable state.
599
612
  *
600
- * Cargo flocks one `target/` per invocation. Nx must not run two cargo
601
- * writers on that directory at once — that is a mutex, not a deadlock, and
602
- * the second process sits in "Blocking waiting for file lock" until a
603
- * timeout. Inference serializes writers that share the default target dir
604
- * (`napi-debug` after compile).
613
+ * Cargo flocks one `target/` per invocation and owns that lock: a second cargo
614
+ * process on the same directory waits inside cargo ("Blocking waiting for file
615
+ * lock"). Inference adds no edge to keep writers apart — an edge says one
616
+ * target consumes another's output, and none of these do. These are unbounded
617
+ * `nx:run-commands` targets, so a wait is not charged against a test budget.
605
618
  *
606
619
  * Clippy runs ONCE over the whole workspace, in that same `target/`, rather
607
620
  * than once per crate. Clippy of a crate is a check build of its whole
@@ -645,6 +658,9 @@ function createCargoTestCompileTarget(projectRoot: string): TargetConfiguration
645
658
  * Cached, unlike `cargo-test-compile`, because it produces a VALUE: one file,
646
659
  * not a mutable build tree Nx would have to restore. `--workspace` matches the
647
660
  * clippy selection exactly, so both gates unify features the same way.
661
+ *
662
+ * The directory and the archive are ONE command: nextest does not create the
663
+ * archive's parent, so the archive step consumes what the `mkdir` produced.
648
664
  */
649
665
  function createCargoTestArchiveTarget(projectRoot: string, toolConfig: string): TargetConfiguration {
650
666
  return {
@@ -653,14 +669,13 @@ function createCargoTestArchiveTarget(projectRoot: string, toolConfig: string):
653
669
  inputs: CARGO_INPUTS,
654
670
  outputs: [`{projectRoot}/${CARGO_TEST_ARCHIVE_FILE}`],
655
671
  options: {
656
- commands: [
672
+ command: sequence([
657
673
  `mkdir -p ${posix.dirname(CARGO_TEST_ARCHIVE_FILE)}`,
658
674
  cargoFrozen(
659
675
  `nextest archive --workspace --archive-file ${CARGO_TEST_ARCHIVE_FILE} --user-config-file none ${toolConfig}`,
660
676
  ),
661
- ],
677
+ ]),
662
678
  cwd: projectRoot,
663
- parallel: false,
664
679
  },
665
680
  };
666
681
  }
@@ -727,13 +742,12 @@ function createCargoCrossTestTargets(
727
742
  outputs: [`{projectRoot}/${archiveFile}`],
728
743
  dependsOn: [CARGO_FETCH_TARGET],
729
744
  options: {
730
- commands: [
745
+ command: sequence([
731
746
  `mkdir -p ${posix.dirname(archiveFile)}`,
732
747
  `cargo-nextest nextest archive --workspace --target ${triple} --frozen ` +
733
748
  `--archive-file ${archiveFile} --user-config-file none ${toolConfig}`,
734
- ],
749
+ ]),
735
750
  cwd: projectRoot,
736
- parallel: false,
737
751
  // `CARGO` is cargo's own driver variable and the only seam nextest
738
752
  // offers: it shells out to `$CARGO metadata` and
739
753
  // `$CARGO test --no-run --target <triple>`, so the declared script
@@ -894,9 +908,6 @@ function createNodesHandler(hostPlatform: NapiPlatform | null): CreateNodesHandl
894
908
  }
895
909
  }),
896
910
  );
897
- // A project that failed before reaching its closure never awaited Cargo;
898
- // no process this call started may outlive it.
899
- await Promise.all(cargoWorkspaces.map((workspace) => workspace.closureSource));
900
911
 
901
912
  if (errors.length > 0) {
902
913
  throw new AggregateCreateNodesError(errors, results);
@@ -1209,13 +1220,12 @@ async function createProjectTargets(
1209
1220
  // inferred list here, once when Nx merges the same declaration over this
1210
1221
  // result — duplicating the very edges the spread exists to add.
1211
1222
  //
1212
- // `dependsOn` REPLACES rather than unions. These lists are not a bag of
1213
- // independent wishes; they are the serialization chain that keeps two cargo
1214
- // writers off one flocked `target/` (see CARGO_TEST_COMPILE_TARGET), and their
1215
- // ORDER carries the invariant. A package that must re-route the chain — build
1216
- // its artifact ahead of the test run, or drop `cargo-test-compile` because it
1217
- // compiles differently — can only say so by replacing the list; a union would
1218
- // make every inferred edge unremovable and a corrected order unrepresentable.
1223
+ // `dependsOn` REPLACES rather than unions. These lists state what each target
1224
+ // consumes — the fetched registry, the archive, the debug addon — and a
1225
+ // package that must re-route one (build its artifact ahead of the test run,
1226
+ // or drop `cargo-test-compile` because it compiles differently) can only say
1227
+ // so by replacing the list; a union would make every inferred edge
1228
+ // unremovable and a corrected order unrepresentable.
1219
1229
  // Additive intent already has a spelling (`"..."` above), so a plugin-local
1220
1230
  // additive key would be a second convention beside a working one.
1221
1231
  //
@@ -1307,7 +1317,6 @@ async function createProjectTargets(
1307
1317
  options: {
1308
1318
  commands: ['cargo fmt --all --check', CARGO_LINT_CLIPPY_COMMAND],
1309
1319
  cwd: cargoWorkspaceRoot,
1310
- parallel: false,
1311
1320
  },
1312
1321
  };
1313
1322
  validationTargets.push('cargo-lint');
@@ -1448,8 +1457,8 @@ async function createProjectTargets(
1448
1457
  // Derived from the command text rather than a curated list of target names:
1449
1458
  // the fact that makes a target need this is that it runs frozen cargo, so a
1450
1459
  // frozen target added later is covered without anyone remembering to. The edge
1451
- // goes on EVERY frozen target, not just the head of the serialization chain,
1452
- // so re-routing that chain cannot strip a surviving target's precondition.
1460
+ // goes on EVERY frozen target, so re-routing one target's `dependsOn` cannot
1461
+ // strip another's precondition.
1453
1462
  // Package-rooted workspaces fetch beside their package.json; members of a
1454
1463
  // repository-root workspace all depend on the root project's one fetch.
1455
1464
  //
@@ -1472,6 +1481,8 @@ async function createProjectTargets(
1472
1481
  // there is nothing for Nx to restore in its place.
1473
1482
  cache: false,
1474
1483
  options: {
1484
+ // One fetch per manifest, independent of the others: Cargo's own
1485
+ // package-cache lock arbitrates the CARGO_HOME they share.
1475
1486
  commands: [...frozenCargoManifests]
1476
1487
  .sort()
1477
1488
  .map((manifestPath) =>
@@ -1480,9 +1491,6 @@ async function createProjectTargets(
1480
1491
  : `${CARGO_FETCH_COMMAND} --manifest-path ${manifestPath}`,
1481
1492
  ),
1482
1493
  cwd: projectRoot,
1483
- // Two fetches into one CARGO_HOME contend on its package-cache flock;
1484
- // serializing here spends the wait in Nx instead of inside cargo.
1485
- parallel: false,
1486
1494
  },
1487
1495
  };
1488
1496
  for (const [name, target] of Object.entries(targets)) {
@@ -1527,8 +1535,8 @@ async function createProjectTargets(
1527
1535
  ];
1528
1536
  }
1529
1537
 
1530
- // Cargo flocks the workspace's default target/. Keep every writer out of the
1531
- // bounded test window by placing N-API debug builds behind the root compile.
1538
+ // `build` warms the test executables' compile; in a nested package that names
1539
+ // the root workspace's one compile.
1532
1540
  const cargoTestCompileDependency: CargoTargetDependency | null = targets[CARGO_TEST_COMPILE_TARGET]
1533
1541
  ? CARGO_TEST_COMPILE_TARGET
1534
1542
  : isCargoProject && cargoWorkspace
@@ -1538,18 +1546,10 @@ async function createProjectTargets(
1538
1546
  })
1539
1547
  : null;
1540
1548
  const napiDebug = targets['napi-debug'];
1541
- if (napiDebug && cargoTestCompileDependency) {
1542
- const dependsOn = napiDebug.dependsOn ?? [];
1543
- if (!hasCargoTargetDependency(dependsOn, cargoTestCompileDependency)) {
1544
- napiDebug.dependsOn = [...dependsOn, cargoTestCompileDependency];
1545
- }
1546
- }
1547
1549
  if (napiDebug && cargoWorkspace) {
1548
1550
  // A crate whose project also builds the debug cdylib runs its tests after
1549
1551
  // that build: the tests dlopen what `napi-debug` writes into `target/debug`,
1550
- // and an archived test binary still loads it from this tree. The old serial
1551
- // chain gave the same guarantee by accident, to whichever crate happened to
1552
- // be first; this states it for exactly the crates that own the artifact.
1552
+ // and an archived test binary still loads it from this tree.
1553
1553
  for (const plan of cargoPackagePlans) {
1554
1554
  for (const piece of plan.pieces) {
1555
1555
  const target = targets[piece.targetName];
@@ -1565,9 +1565,7 @@ async function createProjectTargets(
1565
1565
  }
1566
1566
  const dependsOn = target.dependsOn ?? [];
1567
1567
  if (dependsOn.includes(CARGO_TEST_COMPILE_TARGET) && !dependsOn.includes('napi-debug')) {
1568
- target.dependsOn = dependsOn.map((dependency) =>
1569
- dependency === CARGO_TEST_COMPILE_TARGET ? 'napi-debug' : dependency,
1570
- );
1568
+ target.dependsOn = [...dependsOn, 'napi-debug'];
1571
1569
  }
1572
1570
  }
1573
1571
  const cargoTest = targets[CARGO_TEST_TARGET];
@@ -1583,36 +1581,12 @@ async function createProjectTargets(
1583
1581
  }
1584
1582
  }
1585
1583
 
1586
- // `build` lists cargo-test-compile beside the host's platform binaries, and
1587
- // every one of those is a `napi build` — another cargo writer on the
1588
- // workspace's default `target/`, which cargo flocks.
1589
- //
1590
- // Only `cargo-napi` gets the ordering edge. A platform-suffixed target may NOT
1591
- // have one: `validatePlatformTargetDependencies` (package-target-policy.ts:674)
1592
- // walks each platform target's dependency closure and refuses any member of a
1593
- // different family, because the publish flow builds and collects one platform
1594
- // at a time — a `*-macos` collect that reached a familyless sibling would drag
1595
- // in work the macOS runner has no business doing. `cargo-napi` carries no
1596
- // platform suffix, so it is not scanned and the edge is legal there.
1597
- //
1598
- // The platform binaries therefore stay Nx siblings of cargo-test-compile and
1599
- // serialize on cargo's flock instead of on a graph edge. That is the same
1600
- // arrangement `cli-<arch>-<os>` and `napi-<arch>-<os>` already have with each
1601
- // other — both `napi build` on one target dir — so this adds a third
1602
- // participant to an existing wait, not a new hazard. The flock makes them wait;
1603
- // it does not corrupt anything, and these are unbounded `nx:run-commands`
1604
- // targets, so the wait is not charged against a test budget.
1605
- const cargoWriterNames = hostPlatformTargetNames(
1584
+ // The inferring host's own platform binaries join the aggregate `build`;
1585
+ // publish still owns foreign platforms.
1586
+ const hostPlatformBinaries = hostPlatformTargetNames(
1606
1587
  [...Object.keys(declaredTargets), ...Object.keys(targets)],
1607
1588
  hostPlatform,
1608
1589
  );
1609
- const cargoNapi = targets['cargo-napi'];
1610
- if (cargoNapi && cargoTestCompileDependency) {
1611
- const dependsOn = cargoNapi.dependsOn ?? [];
1612
- if (!hasCargoTargetDependency(dependsOn, cargoTestCompileDependency)) {
1613
- cargoNapi.dependsOn = [...dependsOn, cargoTestCompileDependency];
1614
- }
1615
- }
1616
1590
 
1617
1591
  // A deploy is never cached; the build half beside it always is.
1618
1592
  //
@@ -1694,9 +1668,7 @@ async function createProjectTargets(
1694
1668
  executor: 'nx:noop',
1695
1669
  cache: true,
1696
1670
  outputs: [],
1697
- // `cargo-napi` is both an output family and a serialized cargo writer, so
1698
- // the two sources overlap; the Set keeps one edge per target while holding
1699
- // the order the families are listed in.
1671
+ // One edge per target, in the order the sources are listed.
1700
1672
  dependsOn: [
1701
1673
  ...new Set([
1702
1674
  '^build',
@@ -1704,7 +1676,7 @@ async function createProjectTargets(
1704
1676
  // reuses its incremental state; Nx never restores that shared state.
1705
1677
  ...(cargoTestCompileDependency ? [cargoTestCompileDependency] : []),
1706
1678
  ...buildOutputTargetNames([...Object.keys(declaredTargets), ...Object.keys(targets)]),
1707
- ...cargoWriterNames,
1679
+ ...hostPlatformBinaries,
1708
1680
  ]),
1709
1681
  ],
1710
1682
  };
@@ -1777,7 +1749,7 @@ async function createProjectTargets(
1777
1749
  ],
1778
1750
  ...(lintCommands.length > 0
1779
1751
  ? {
1780
- options: { commands: lintCommands, cwd: '.', parallel: false },
1752
+ options: { commands: lintCommands, cwd: '.' },
1781
1753
  }
1782
1754
  : {}),
1783
1755
  };
@@ -1817,7 +1789,7 @@ async function createProjectTargets(
1817
1789
  if (closureSources.length > 0) {
1818
1790
  namedInputs[CARGO_CLOSURE_INPUT] = await cargoClosureInputs(
1819
1791
  projectRoot,
1820
- await Promise.all(closureSources.map((workspace) => workspace.closureSource)),
1792
+ closureSources.map((workspace) => workspace.closureSource),
1821
1793
  );
1822
1794
  }
1823
1795
  return {
@@ -2298,12 +2270,8 @@ interface CargoWorkspace {
2298
2270
  projectRoot: string;
2299
2271
  /** One derivation memo for the whole graph computation (see cargoPackageTestInputs). */
2300
2272
  inputsCache: CargoInputsCache;
2301
- /**
2302
- * Cargo's own resolve, started once per graph computation and awaited only
2303
- * where a project's `cargoClosure` is built, so the `cargo metadata` process
2304
- * overlaps the rest of inference.
2305
- */
2306
- closureSource: Promise<CargoClosureSource>;
2273
+ /** The content-keyed Cargo resolve, checked before publishing any project's closure. */
2274
+ closureSource: CargoClosureSource;
2307
2275
  }
2308
2276
 
2309
2277
  type CargoTargetDependency = NonNullable<TargetConfiguration['dependsOn']>[number];
@@ -2363,12 +2331,13 @@ async function resolveCargoWorkspaces(
2363
2331
  });
2364
2332
  }
2365
2333
  if (pin !== null) {
2366
- const targetName = cargoTestPackageTargetName(pkg.name, CARGO_TEST_EXCEPTIONS_SUFFIX);
2367
- pieces.push({
2368
- extra: '',
2369
- selector: `package(${pkg.name}) and (${pin})`,
2370
- targetName,
2371
- });
2334
+ for (let index = 1; index <= pkg.testShards; index += 1) {
2335
+ pieces.push({
2336
+ extra: ` --partition hash:${index}/${pkg.testShards}`,
2337
+ selector: `package(${pkg.name}) and (${pin})`,
2338
+ targetName: cargoTestPackageTargetName(pkg.name, `${CARGO_TEST_EXCEPTIONS_SUFFIX}-shard${index}`),
2339
+ });
2340
+ }
2372
2341
  }
2373
2342
  packages.push({ package: pkg, pieces });
2374
2343
  }
@@ -2383,19 +2352,22 @@ async function resolveCargoWorkspaces(
2383
2352
  // Started only once every workspace listed cleanly, so a listing that throws
2384
2353
  // leaves no Cargo process behind.
2385
2354
  const indexedManifests = indexedCargoManifests(workspaceRoot);
2386
- return found.map((workspace) => ({
2387
- ...workspace,
2388
- closureSource: resolveCargoClosureSource(
2389
- posix.join(workspace.projectRoot, 'Cargo.toml'),
2390
- workspaceRoot,
2391
- indexedManifests,
2392
- ),
2393
- }));
2355
+ return Promise.all(
2356
+ found.map(async (workspace) => ({
2357
+ ...workspace,
2358
+ closureSource: await resolveCargoClosureSource(
2359
+ posix.join(workspace.projectRoot, 'Cargo.toml'),
2360
+ workspaceRoot,
2361
+ indexedManifests,
2362
+ ),
2363
+ })),
2364
+ );
2394
2365
  }
2395
2366
 
2396
2367
  /**
2397
2368
  * One bounded target per crate; a crate that declares `smoothbricks.test.shards`
2398
- * gets one per shard plus one for the tests nextest.toml singles out.
2369
+ * gets that many ordinary and exceptional shards, so a class of tests with its
2370
+ * own time bound never delays an ordinary shard.
2399
2371
  *
2400
2372
  * Every piece EXECUTES, it never builds: it runs the binaries
2401
2373
  * `cargo-test-archive` compiled once for the whole workspace. That is what
@@ -2437,12 +2409,11 @@ async function resolveCargoWorkspaces(
2437
2409
  * time: a test that spawns `env!("CARGO_BIN_EXE_x")` holds the producing tree's
2438
2410
  * path and must read the runtime variable instead to survive relocation.
2439
2411
  *
2440
- * nextest.toml singles some tests out with an override, and each such class
2441
- * breaks a shard in its own way — a `test-group` is scoped to one nextest RUN
2442
- * so the hash would dissolve it, and a raised `slow-timeout` marks a test whose
2443
- * cost is not the suite's. Those are lifted OUT of the hash into one target and
2444
- * the shards run the exact complement, so which tests a shard holds stops
2445
- * depending on how the hash happened to fall.
2412
+ * nextest.toml singles some tests out with an override: a raised `slow-timeout`
2413
+ * marks a test whose cost is not the suite's. Those are lifted OUT of the hash
2414
+ * into their own hash-partitioned targets and the ordinary shards run the exact
2415
+ * complement, so which tests a shard holds stops depending on how the hash
2416
+ * happened to fall.
2446
2417
  *
2447
2418
  * The two filtersets are exact complements, so their union is the crate whatever
2448
2419
  * either one matches. Every piece uses `--no-tests=pass`: a valid workspace
@@ -2456,9 +2427,9 @@ async function resolveCargoWorkspaces(
2456
2427
  * Detecting that regression requires a separate coverage policy rather than
2457
2428
  * making valid empty crates and shards fail execution.
2458
2429
  *
2459
- * Pieces fan out rather than chain. The chain existed because every runner was
2460
- * a cargo writer on one flocked `target/`; a reused-build run writes nothing
2461
- * there, so the only serialization left is the one extraction per archive.
2430
+ * Pieces fan out rather than chain: a reused-build run writes nothing to cargo's
2431
+ * `target/`, so no piece waits on another. The one extraction per archive is
2432
+ * coordinated by `smoo-nx-nextest-extract` itself.
2462
2433
  */
2463
2434
  async function addCargoTestTargets(
2464
2435
  targets: Record<string, TargetConfiguration>,
@@ -2,3 +2,15 @@ import { fileURLToPath } from 'node:url';
2
2
 
3
3
  /** Packaged assets, independent of the consumer's cwd and node_modules layout. */
4
4
  export const managedAssetsRoot = fileURLToPath(new URL('../managed/', import.meta.url));
5
+
6
+ /** Explicit package-manager relink intent for the packaged bootstrap preservation helper. */
7
+ export interface KeepDeveloperLinksOptions {
8
+ readonly relink: readonly string[];
9
+ }
10
+
11
+ /** The managed helper's callable contract; type-only consumers need no installed runtime import. */
12
+ export type KeepDeveloperLinks = <T>(
13
+ root: string,
14
+ install: () => Promise<T>,
15
+ options?: KeepDeveloperLinksOptions,
16
+ ) => Promise<T>;
@@ -135,6 +135,12 @@ const managedFiles: ManagedFileDescriptor[] = [
135
135
  source: 'tooling/direnv/shared-caches.sh',
136
136
  target: 'tooling/direnv/shared-caches.sh',
137
137
  },
138
+ {
139
+ kind: 'raw',
140
+ source: 'tooling/direnv/inherited-devenv.ts',
141
+ target: 'tooling/direnv/inherited-devenv.ts',
142
+ executable: true,
143
+ },
138
144
  {
139
145
  kind: 'raw',
140
146
  source: 'tooling/git-hooks/pre-commit.sh',
@@ -2,7 +2,8 @@ import { createHash, type Hash } from 'node:crypto';
2
2
  import { readFile } from 'node:fs/promises';
3
3
  import { join, posix } from 'node:path';
4
4
 
5
- import { globWithWorkspaceContext } from 'nx/src/utils/workspace-context.js';
5
+ import { globWithWorkspaceContext, globWithWorkspaceContextSync } from 'nx/src/utils/workspace-context.js';
6
+ import { workspaceRoot as daemonWorkspaceRoot } from 'nx/src/utils/workspace-root.js';
6
7
 
7
8
  /**
8
9
  * A release rewrites `version` in every manifest it publishes. Those manifests
@@ -180,7 +181,11 @@ export async function hashVersionlessCrateManifests(projectRoot: string, workspa
180
181
  */
181
182
  async function listCrateManifests(projectRoot: string, workspaceRoot: string): Promise<string[]> {
182
183
  const prefix = projectRoot === '.' ? '' : `${posix.normalize(projectRoot)}/`;
183
- const files = await globWithWorkspaceContext(workspaceRoot, [`${prefix}**/${CARGO_MANIFEST}`]);
184
+ const pattern = [`${prefix}**/${CARGO_MANIFEST}`];
185
+ const files =
186
+ workspaceRoot === daemonWorkspaceRoot
187
+ ? await globWithWorkspaceContext(workspaceRoot, pattern)
188
+ : globWithWorkspaceContextSync(workspaceRoot, pattern);
184
189
  return files.map((file) => file.slice(prefix.length)).sort();
185
190
  }
186
191
 
@@ -0,0 +1,150 @@
1
+ import { afterEach, expect, it } from 'bun:test';
2
+ import { type ChildProcess, spawn } from 'node:child_process';
3
+ import { mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
4
+ import { tmpdir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import type { TargetConfiguration } from 'nx/src/devkit-exports.js';
7
+ import { useFixtureCargoHome } from './__tests__/fixture-cargo-home.js';
8
+ import { createNodesV2 } from './index.js';
9
+
10
+ useFixtureCargoHome();
11
+
12
+ it('executes every archived test once across ordinary and exceptional partitions', async () => {
13
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'smoo-nextest-partitions-')));
14
+ fixtureRoot = root;
15
+ const ordinary = Array.from({ length: 13 }, (_, index) => `ordinary_${index}`);
16
+ // The classes the plugin's nextest.toml singles out with an override: a
17
+ // compile-fail test and the real-APFS lifecycle test each carry a raised
18
+ // slow-timeout, so each is lifted out of the ordinary hash.
19
+ const exceptional = [
20
+ ...Array.from({ length: 6 }, (_, index) => `compile_fail_partition_${index}`),
21
+ ...Array.from({ length: 6 }, (_, index) => `partition_${index}_fail_to_compile`),
22
+ 'real_apfs_partition_substrate_lifecycle',
23
+ ];
24
+ const expected = [...ordinary, ...exceptional];
25
+ await writeFile(join(root, 'package.json'), '{"name":"partition-fixture","private":true}\n');
26
+ await writeFile(join(root, '.gitignore'), 'node_modules/\ntarget/\n.nx/\n*.log\n');
27
+ await writeFile(
28
+ join(root, 'Cargo.toml'),
29
+ '[workspace]\nresolver = "2"\n\n[package]\nname = "partition-probe"\nversion = "0.1.0"\nedition = "2021"\n\n[package.metadata.smoothbricks.test]\nshards = 3\n',
30
+ );
31
+ await mkdir(join(root, 'src'));
32
+ await writeFile(
33
+ join(root, 'src/lib.rs'),
34
+ `#[cfg(test)] mod tests {
35
+ use std::io::Write;
36
+ fn record(name: &str) {
37
+ let mut file = std::fs::OpenOptions::new().create(true).append(true)
38
+ .open(std::env::var("PARTITION_EXECUTIONS").unwrap()).unwrap();
39
+ file.write_all(format!("{name}\\n").as_bytes()).unwrap();
40
+ }
41
+ ${expected.map((name) => ` #[test] fn ${name}() { record("${name}"); }`).join('\n')}
42
+ }
43
+ `,
44
+ );
45
+ await symlink(join(import.meta.dir, '../../../node_modules'), join(root, 'node_modules'), 'dir');
46
+ await run(root, ['git', 'init', '--quiet']);
47
+ await run(root, ['cargo', 'generate-lockfile', '--offline']);
48
+ const [, infer] = createNodesV2;
49
+ const result = await infer(['package.json'], undefined, { workspaceRoot: root, nxJsonConfiguration: {} });
50
+ const targets = result[0]?.[1].projects?.['.']?.targets;
51
+ if (targets === undefined) throw new Error('Cargo fixture inferred no targets');
52
+ // The archive is the one real prerequisite: every runner executes its binaries.
53
+ await run(root, ['sh', '-c', command(targets, 'cargo-test-archive')]);
54
+ // One log per partition kind, so the proof is exact selection and not only
55
+ // coverage: the ordinary shards ran every ordinary test once and nothing
56
+ // else, and the exceptional shards did the same for theirs. The six runners
57
+ // are independent, so they run side by side exactly as Nx fans them out.
58
+ const partitions = [
59
+ { kind: 'shard', log: join(root, 'ordinary.log'), tests: ordinary },
60
+ { kind: 'exceptions-shard', log: join(root, 'exceptional.log'), tests: exceptional },
61
+ ];
62
+ await Promise.all(
63
+ partitions.flatMap(({ kind, log }) =>
64
+ [1, 2, 3].map((index) =>
65
+ run(root, ['sh', '-c', command(targets, `cargo-test-partition-probe-${kind}${index}`)], {
66
+ PARTITION_EXECUTIONS: log,
67
+ }),
68
+ ),
69
+ ),
70
+ );
71
+ for (const { log, tests } of partitions) {
72
+ const observed = (await readFile(log, 'utf8')).trim().split('\n').sort();
73
+ expect(observed).toEqual([...tests].sort());
74
+ }
75
+ });
76
+
77
+ function command(targets: Readonly<Record<string, TargetConfiguration>>, name: string): string {
78
+ const single: unknown = targets[name]?.options?.command;
79
+ if (typeof single !== 'string') throw new Error(`${name} inferred no executable command`);
80
+ return single;
81
+ }
82
+
83
+ /** A child this test started and has not yet seen close, with the command line that names it. */
84
+ interface OpenChild {
85
+ readonly command: string;
86
+ readonly closed: Promise<unknown>;
87
+ }
88
+
89
+ const openChildren = new Map<ChildProcess, OpenChild>();
90
+ let fixtureRoot: string | undefined;
91
+
92
+ /**
93
+ * The only teardown, for a pass, a failure and the per-test deadline alike.
94
+ *
95
+ * The deadline abandons the test body mid-`await`, so the body cannot be
96
+ * trusted to clean up after itself. This hook signals each open child's whole
97
+ * process group (the same detached-group kill the bounded executor uses, since
98
+ * `sh -c` leaves cargo and nextest as grandchildren), waits for every one to
99
+ * close, and only then deletes the fixture they were running in. It then
100
+ * names what was still open instead of leaving a bare timeout.
101
+ */
102
+ afterEach(async () => {
103
+ const open = [...openChildren.values()];
104
+ for (const child of openChildren.keys()) killProcessGroup(child);
105
+ openChildren.clear();
106
+ await Promise.allSettled(open.map((child) => child.closed));
107
+ if (fixtureRoot !== undefined) {
108
+ const root = fixtureRoot;
109
+ fixtureRoot = undefined;
110
+ await rm(root, { recursive: true, force: true });
111
+ }
112
+ if (open.length > 0) {
113
+ throw new Error(`test ended with child processes still open:\n${open.map((child) => child.command).join('\n')}`);
114
+ }
115
+ });
116
+
117
+ function killProcessGroup(child: ChildProcess): void {
118
+ if (child.pid === undefined) return;
119
+ try {
120
+ process.kill(process.platform === 'win32' ? child.pid : -child.pid, 'SIGKILL');
121
+ } catch (error) {
122
+ if (!(error instanceof Error && 'code' in error && error.code === 'ESRCH')) throw error;
123
+ }
124
+ }
125
+
126
+ async function run(root: string, args: string[], overlay: Record<string, string> = {}): Promise<void> {
127
+ const [program, ...rest] = args;
128
+ if (program === undefined) throw new Error('run needs a program');
129
+ const child = spawn(program, rest, {
130
+ cwd: root,
131
+ env: { ...process.env, ...overlay },
132
+ detached: process.platform !== 'win32',
133
+ });
134
+ child.stdin?.end();
135
+ const output: Buffer[] = [];
136
+ child.stdout?.on('data', (chunk: Buffer) => output.push(chunk));
137
+ child.stderr?.on('data', (chunk: Buffer) => output.push(chunk));
138
+ const closed = new Promise<number | null>((resolve, reject) => {
139
+ child.once('error', reject);
140
+ child.once('close', resolve);
141
+ });
142
+ openChildren.set(child, { command: args.join(' '), closed });
143
+ let code: number | null;
144
+ try {
145
+ code = await closed;
146
+ } finally {
147
+ openChildren.delete(child);
148
+ }
149
+ expect(code, `${args.join(' ')}\n${Buffer.concat(output).toString()}`).toBe(0);
150
+ }