@smoothbricks/nx-plugin 0.4.22 → 0.4.24

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 (72) hide show
  1. package/README.md +93 -33
  2. package/dist/bin/smoo-nx-exec.js +35 -11
  3. package/dist/bin/smoo-nx-nextest-extract.d.ts +3 -0
  4. package/dist/bin/smoo-nx-nextest-extract.d.ts.map +1 -0
  5. package/dist/bin/smoo-nx-nextest-extract.js +41 -0
  6. package/dist/cargo-closure-input.d.ts +63 -0
  7. package/dist/cargo-closure-input.d.ts.map +1 -0
  8. package/dist/cargo-closure-input.js +197 -0
  9. package/dist/cargo-source-hash.d.ts +37 -0
  10. package/dist/cargo-source-hash.d.ts.map +1 -1
  11. package/dist/cargo-source-hash.js +108 -190
  12. package/dist/cross-check-policy.d.ts +14 -0
  13. package/dist/cross-check-policy.d.ts.map +1 -1
  14. package/dist/cross-check-policy.js +15 -1
  15. package/dist/ensure-built.d.ts +3 -2
  16. package/dist/ensure-built.d.ts.map +1 -1
  17. package/dist/ensure-built.js +33 -56
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +152 -91
  20. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  21. package/dist/managed-files/ci-workflow.js +12 -15
  22. package/dist/managed-files/files.d.ts.map +1 -1
  23. package/dist/managed-files/files.js +10 -0
  24. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  25. package/dist/managed-files/publish-workflow.js +12 -6
  26. package/dist/manifest-hash.d.ts.map +1 -1
  27. package/dist/manifest-hash.js +24 -30
  28. package/dist/nextest-extraction.d.ts +101 -0
  29. package/dist/nextest-extraction.d.ts.map +1 -0
  30. package/dist/nextest-extraction.js +268 -0
  31. package/dist/package-target-policy.js +1 -1
  32. package/dist/platform-targets.d.ts +7 -0
  33. package/dist/platform-targets.d.ts.map +1 -1
  34. package/dist/platform-targets.js +7 -0
  35. package/dist/workspace-config-policy.d.ts +1 -1
  36. package/dist/workspace-config-policy.d.ts.map +1 -1
  37. package/dist/workspace-config-policy.js +1 -1
  38. package/managed/raw/envrc +38 -1
  39. package/managed/raw/tooling/direnv/developer-links.ts +201 -0
  40. package/managed/raw/tooling/direnv/devenv.smoo.nix +75 -51
  41. package/managed/raw/tooling/direnv/secret-references.ts +3 -1
  42. package/managed/raw/tooling/direnv/setup-environment.ts +562 -78
  43. package/managed/raw/tooling/direnv/shared-caches.sh +53 -0
  44. package/managed/raw/tooling/git-hooks/pre-push.sh +22 -1
  45. package/managed/templates/github/workflows/managed-files.yml +4 -3
  46. package/nextest.toml +5 -5
  47. package/package.json +4 -3
  48. package/src/__tests__/fixture-nx-env.ts +31 -0
  49. package/src/__tests__/in-process-nx-index.ts +9 -0
  50. package/src/bin/smoo-nx-exec.ts +40 -11
  51. package/src/bin/smoo-nx-nextest-extract.ts +43 -0
  52. package/src/bounded-test-policy.test.ts +4 -1
  53. package/src/cargo-closure-input.test.ts +184 -0
  54. package/src/cargo-closure-input.ts +247 -0
  55. package/src/cargo-source-hash.test.ts +8 -9
  56. package/src/cargo-source-hash.ts +146 -47
  57. package/src/cross-check-policy.ts +15 -3
  58. package/src/ensure-built.test.ts +65 -5
  59. package/src/ensure-built.ts +40 -85
  60. package/src/index.test.ts +222 -75
  61. package/src/index.ts +213 -101
  62. package/src/managed-files/ci-workflow.ts +12 -15
  63. package/src/managed-files/files.ts +10 -0
  64. package/src/managed-files/generator.test.ts +29 -0
  65. package/src/managed-files/publish-workflow.ts +17 -6
  66. package/src/manifest-hash.test.ts +21 -1
  67. package/src/manifest-hash.ts +26 -29
  68. package/src/nextest-extraction.test.ts +74 -0
  69. package/src/nextest-extraction.ts +329 -0
  70. package/src/package-target-policy.ts +1 -1
  71. package/src/platform-targets.ts +8 -0
  72. package/src/workspace-config-policy.ts +6 -1
package/src/index.ts CHANGED
@@ -15,6 +15,13 @@ import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
15
15
  import { parse as parseToml } from 'smol-toml';
16
16
 
17
17
  import { BOUNDED_TEST_KILL_AFTER_MS, BOUNDED_TEST_TIMEOUT_MS } from './bounded-test-policy.js';
18
+ import {
19
+ CARGO_CLOSURE_INPUT,
20
+ type CargoClosureSource,
21
+ cargoClosureInputs,
22
+ indexedCargoManifests,
23
+ resolveCargoClosureSource,
24
+ } from './cargo-closure-input.js';
18
25
  import { CARGO_TOOLCHAIN_NAMED_INPUT, CARGO_TOOLCHAIN_PIN_INPUTS } from './cargo-toolchain-policy.js';
19
26
  import {
20
27
  type AttributedCargoWorkspacePackage,
@@ -48,8 +55,19 @@ import {
48
55
  cargoFrozen,
49
56
  } from './cross-check-policy.js';
50
57
  import { hashVersionlessCrateManifests } from './manifest-hash.js';
58
+ import {
59
+ EXTRACTED_BINARIES_METADATA,
60
+ EXTRACTED_CARGO_METADATA,
61
+ EXTRACTED_TARGET_DIR,
62
+ NEXTEST_EXTRACT_BIN,
63
+ } from './nextest-extraction.js';
51
64
  import { isNonSourceDirectory } from './source-directories.js';
52
- import { isBuildOutputTargetName, isTestRunnerTargetName, PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
65
+ import {
66
+ isBuildOutputTargetName,
67
+ isTestRunnerTargetName,
68
+ PLATFORM_TARGET_GLOBS,
69
+ RELEASE_CONFIGURATION,
70
+ } from './workspace-config-policy.js';
53
71
 
54
72
  export { CARGO_TEST_COMPILE_TARGET };
55
73
 
@@ -507,17 +525,56 @@ export function napiToolchainTargetName(convention: NapiTargetConvention): strin
507
525
  return `${NAPI_TOOLCHAIN_TARGET_PREFIX}${convention.architecture}-${convention.targetFamily}`;
508
526
  }
509
527
 
528
+ /**
529
+ * Cargo's two profiles. A build target's default options compile `dev`, the
530
+ * profile every local build, test and gate shares, so one `target/` holds one
531
+ * set of units; RELEASE_CONFIGURATION compiles `release` for what ships.
532
+ */
533
+ type CargoProfile = 'dev' | 'release';
534
+
535
+ const CARGO_PROFILE_FLAG: Readonly<Record<CargoProfile, string>> = { dev: '', release: ' --release' };
536
+ /** The directory under `target/<triple>/` each profile writes. */
537
+ const CARGO_PROFILE_OUTPUT_DIR: Readonly<Record<CargoProfile, string>> = { dev: 'debug', release: 'release' };
538
+
539
+ /**
540
+ * Cargo's default target directory for a crate, relative to the command's cwd:
541
+ * `target/` beside the workspace root that owns the crate, or beside the
542
+ * crate's own manifest when no workspace claims it. Naming it rather than
543
+ * passing `--target-dir` keeps the wasm build in the directory every other
544
+ * build of the same workspace writes.
545
+ */
546
+ function cargoWasmTargetDirectory(
547
+ projectRoot: string,
548
+ config: ResolvedCargoWasmConfig,
549
+ repoRooted: boolean,
550
+ cargoWorkspace: CargoWorkspace | undefined,
551
+ ): string {
552
+ const cwd = repoRooted ? '.' : projectRoot;
553
+ const crateDir = posix.join(cwd, posix.dirname(config.manifestPath));
554
+ const owner =
555
+ cargoWorkspace !== undefined &&
556
+ cargoWorkspace.packages.some((plan) => posix.join(cargoWorkspace.projectRoot, plan.package.dir) === crateDir)
557
+ ? cargoWorkspace.projectRoot
558
+ : crateDir;
559
+ return posix.relative(cwd, posix.join(owner, 'target'));
560
+ }
561
+
510
562
  function createCargoWasmTarget(
511
563
  projectRoot: string,
512
564
  config: ResolvedCargoWasmConfig,
513
565
  repoRooted: boolean,
566
+ cargoTargetDirectory: string,
514
567
  ): TargetConfiguration {
515
- const cargoTargetDirectory = repoRooted
516
- ? 'target/cargo-wasm'
517
- : posix.join(posix.dirname(config.manifestPath), 'target/cargo-wasm');
518
- const wasmInput = `${cargoTargetDirectory}/wasm32-unknown-unknown/release/${config.libraryName}.wasm`;
519
568
  const outputDirectory = repoRooted ? posix.join(projectRoot, config.outputDirectory) : config.outputDirectory;
520
569
  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
+ ];
521
578
  return {
522
579
  executor: 'nx:run-commands',
523
580
  cache: true,
@@ -525,18 +582,11 @@ function createCargoWasmTarget(
525
582
  inputs: repoRooted ? REPO_ROOT_CARGO_OUTPUT_INPUTS : CARGO_OUTPUT_INPUTS,
526
583
  outputs: [`{projectRoot}/${config.outputDirectory}`],
527
584
  options: {
528
- commands: [
529
- cargoFrozen(
530
- `build --release --target wasm32-unknown-unknown --target-dir ${cargoTargetDirectory} ${cargoSelection}`,
531
- ),
532
- ...config.targets.map(
533
- ({ bindgenTarget, outputName }) =>
534
- `wasm-bindgen --target ${bindgenTarget} --out-dir ${outputDirectory}/${outputName} ${wasmInput}`,
535
- ),
536
- ],
585
+ commands: commands('dev'),
537
586
  cwd: repoRooted ? '.' : projectRoot,
538
587
  parallel: false,
539
588
  },
589
+ configurations: { [RELEASE_CONFIGURATION]: { commands: commands('release') } },
540
590
  };
541
591
  }
542
592
 
@@ -553,13 +603,19 @@ function createCargoWasmTarget(
553
603
  * timeout. Inference serializes writers that share the default target dir
554
604
  * (`napi-debug` after compile).
555
605
  *
556
- * Clippy writes ONE shared `--target-dir target/cargo-lint` rather than a
557
- * directory per crate. Per-crate dirs were never about overlap: clippy of a
558
- * crate is a check build of its whole closure, so 49 crates meant 49 closure
559
- * rebuilds and 11 GiB of target directories on one repository, and no
560
- * per-crate cache hit ever repaid that. Sharing the directory costs nothing:
561
- * cargo's own lock serializes the invocations that use it and each dependency
562
- * artifact is then compiled once for all of them.
606
+ * Clippy runs ONCE over the whole workspace, in that same `target/`, rather
607
+ * than once per crate. Clippy of a crate is a check build of its whole
608
+ * closure, so 49 crates meant 49 closure rebuilds and 11 GiB of target
609
+ * directories on one repository, and no per-crate cache hit ever repaid that.
610
+ * Sharing the build's directory costs nothing: cargo's own lock serializes
611
+ * the invocations that use it, and the host units a dev build compiled are
612
+ * reused instead of compiled again.
613
+ *
614
+ * Tests compile the dev profile only, so no test target carries a
615
+ * RELEASE_CONFIGURATION: Nx forwards a run's configuration down every
616
+ * `dependsOn` edge to each target that has it, and a release `build` must
617
+ * warm the dev test binaries the test run will use, not release ones nobody
618
+ * runs.
563
619
  */
564
620
  function createCargoTestCompileTarget(projectRoot: string): TargetConfiguration {
565
621
  return {
@@ -571,9 +627,6 @@ function createCargoTestCompileTarget(projectRoot: string): TargetConfiguration
571
627
  command: cargoFrozen('test --workspace --no-run'),
572
628
  cwd: projectRoot,
573
629
  },
574
- configurations: {
575
- production: { command: cargoFrozen('test --workspace --release --no-run') },
576
- },
577
630
  };
578
631
  }
579
632
 
@@ -586,33 +639,29 @@ function createCargoTestCompileTarget(projectRoot: string): TargetConfiguration
586
639
  * build with a filter: 32 such tasks on one repository each re-entered cargo,
587
640
  * re-resolved features and chained behind one another so the flocked `target/`
588
641
  * had a single writer. Reading from an archive removes the build from the run
589
- * entirely — the runner extracts binaries into its own temporary directory and
590
- * touches no shared state — which is what lets the runners fan out.
642
+ * entirely — the runners execute one extraction of it and never write cargo's
643
+ * `target/` build tree — which is what lets the runners fan out.
591
644
  *
592
645
  * Cached, unlike `cargo-test-compile`, because it produces a VALUE: one file,
593
646
  * not a mutable build tree Nx would have to restore. `--workspace` matches the
594
647
  * clippy selection exactly, so both gates unify features the same way.
595
648
  */
596
649
  function createCargoTestArchiveTarget(projectRoot: string, toolConfig: string): TargetConfiguration {
597
- const archiveCommand = (profile: string): string =>
598
- cargoFrozen(
599
- `nextest archive --workspace${profile} --archive-file ${CARGO_TEST_ARCHIVE_FILE} --user-config-file none ${toolConfig}`,
600
- );
601
650
  return {
602
651
  executor: 'nx:run-commands',
603
652
  cache: true,
604
653
  inputs: CARGO_INPUTS,
605
654
  outputs: [`{projectRoot}/${CARGO_TEST_ARCHIVE_FILE}`],
606
655
  options: {
607
- commands: [`mkdir -p ${posix.dirname(CARGO_TEST_ARCHIVE_FILE)}`, archiveCommand('')],
656
+ commands: [
657
+ `mkdir -p ${posix.dirname(CARGO_TEST_ARCHIVE_FILE)}`,
658
+ cargoFrozen(
659
+ `nextest archive --workspace --archive-file ${CARGO_TEST_ARCHIVE_FILE} --user-config-file none ${toolConfig}`,
660
+ ),
661
+ ],
608
662
  cwd: projectRoot,
609
663
  parallel: false,
610
664
  },
611
- configurations: {
612
- production: {
613
- commands: [`mkdir -p ${posix.dirname(CARGO_TEST_ARCHIVE_FILE)}`, archiveCommand(' --release')],
614
- },
615
- },
616
665
  };
617
666
  }
618
667
 
@@ -654,9 +703,6 @@ function createCargoCrossTestTargets(
654
703
  ): Record<string, TargetConfiguration> {
655
704
  const { target: triple, cargo } = crossTarget;
656
705
  const archiveFile = cargoCrossTestArchiveFile(triple);
657
- const archiveCommand = (profile: string): string =>
658
- `cargo-nextest nextest archive --workspace${profile} --target ${triple} --frozen ` +
659
- `--archive-file ${archiveFile} --user-config-file none ${toolConfig}`;
660
706
  return {
661
707
  [cargoCrossTestArchiveTargetName(triple)]: {
662
708
  executor: 'nx:run-commands',
@@ -681,7 +727,11 @@ function createCargoCrossTestTargets(
681
727
  outputs: [`{projectRoot}/${archiveFile}`],
682
728
  dependsOn: [CARGO_FETCH_TARGET],
683
729
  options: {
684
- commands: [`mkdir -p ${posix.dirname(archiveFile)}`, archiveCommand('')],
730
+ commands: [
731
+ `mkdir -p ${posix.dirname(archiveFile)}`,
732
+ `cargo-nextest nextest archive --workspace --target ${triple} --frozen ` +
733
+ `--archive-file ${archiveFile} --user-config-file none ${toolConfig}`,
734
+ ],
685
735
  cwd: projectRoot,
686
736
  parallel: false,
687
737
  // `CARGO` is cargo's own driver variable and the only seam nextest
@@ -692,11 +742,6 @@ function createCargoCrossTestTargets(
692
742
  // it applies to this archive and to nothing else cargo runs.
693
743
  ...(cargo === undefined ? {} : { env: { CARGO: cargo } }),
694
744
  },
695
- configurations: {
696
- production: {
697
- commands: [`mkdir -p ${posix.dirname(archiveFile)}`, archiveCommand(' --release')],
698
- },
699
- },
700
745
  },
701
746
  [cargoCrossTestTargetName(triple)]: {
702
747
  executor: '@smoothbricks/nx-plugin:bounded-exec',
@@ -712,9 +757,6 @@ function createCargoCrossTestTargets(
712
757
  timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
713
758
  killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
714
759
  },
715
- // The archive already holds the profile it was built with, and nextest
716
- // rejects `--release` beside `--archive-file`.
717
- configurations: { production: {} },
718
760
  },
719
761
  };
720
762
  }
@@ -731,9 +773,6 @@ function createCargoTestTarget(projectRoot: string): TargetConfiguration {
731
773
  timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
732
774
  killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
733
775
  },
734
- configurations: {
735
- production: { command: cargoFrozen('test --workspace --release') },
736
- },
737
776
  };
738
777
  }
739
778
 
@@ -855,6 +894,9 @@ function createNodesHandler(hostPlatform: NapiPlatform | null): CreateNodesHandl
855
894
  }
856
895
  }),
857
896
  );
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));
858
900
 
859
901
  if (errors.length > 0) {
860
902
  throw new AggregateCreateNodesError(errors, results);
@@ -1114,7 +1156,12 @@ async function createProjectTargets(
1114
1156
  }
1115
1157
 
1116
1158
  if (inferCargoWasm && cargoWasmConfig) {
1117
- targets['cargo-wasm'] = createCargoWasmTarget(projectRoot, cargoWasmConfig, isRepoRootedCargoProject);
1159
+ targets['cargo-wasm'] = createCargoWasmTarget(
1160
+ projectRoot,
1161
+ cargoWasmConfig,
1162
+ isRepoRootedCargoProject,
1163
+ cargoWasmTargetDirectory(projectRoot, cargoWasmConfig, isRepoRootedCargoProject, cargoWorkspace),
1164
+ );
1118
1165
  }
1119
1166
 
1120
1167
  if (napiConfig) {
@@ -1245,12 +1292,13 @@ async function createProjectTargets(
1245
1292
  aggregateDependencies.length > 0
1246
1293
  ? { executor: 'nx:noop', cache: true, outputs: [], dependsOn: aggregateDependencies }
1247
1294
  : createCargoTestTarget(cargoWorkspaceRoot);
1248
- // One clippy over the whole workspace, in one target dir. Clippy of a crate
1249
- // is a check build of its closure; a target dir per crate compiled every
1250
- // shared dependency once per crate (49 closures, 11 GiB on one repository)
1251
- // and no per-crate cache hit ever repaid that. The workspace inputs are
1252
- // exactly the union of every crate's, so the verdict caches on the same
1253
- // key set the compile does. Non-root packages depend on this target.
1295
+ // One clippy over the whole workspace, in the build's target dir. Clippy
1296
+ // of a crate is a check build of its closure; a target dir per crate
1297
+ // compiled every shared dependency once per crate (49 closures, 11 GiB on
1298
+ // one repository) and no per-crate cache hit ever repaid that. The
1299
+ // workspace inputs are exactly the union of every crate's, so the verdict
1300
+ // caches on the same key set the compile does. Non-root packages depend on
1301
+ // this target.
1254
1302
  targets['cargo-lint'] = {
1255
1303
  executor: 'nx:run-commands',
1256
1304
  cache: true,
@@ -1374,7 +1422,6 @@ async function createProjectTargets(
1374
1422
  const target = targets[name];
1375
1423
  if (target?.executor === 'nx:noop') {
1376
1424
  target.outputs = [];
1377
- target.configurations = { production: {} };
1378
1425
  }
1379
1426
  }
1380
1427
  }
@@ -1551,7 +1598,7 @@ async function createProjectTargets(
1551
1598
  // The platform binaries therefore stay Nx siblings of cargo-test-compile and
1552
1599
  // serialize on cargo's flock instead of on a graph edge. That is the same
1553
1600
  // arrangement `cli-<arch>-<os>` and `napi-<arch>-<os>` already have with each
1554
- // other — both `napi build --release` on one target dir — so this adds a third
1601
+ // other — both `napi build` on one target dir — so this adds a third
1555
1602
  // participant to an existing wait, not a new hazard. The flock makes them wait;
1556
1603
  // it does not corrupt anything, and these are unbounded `nx:run-commands`
1557
1604
  // targets, so the wait is not charged against a test budget.
@@ -1664,11 +1711,11 @@ async function createProjectTargets(
1664
1711
  }
1665
1712
  // Every cargo build directory this project's own commands write. `clean`
1666
1713
  // cannot learn them from `outputs`: a clippy or nextest target dir is a
1667
- // multi-GB build tree that must never enter the Nx cache (`target/cargo-lint`
1668
- // alone was 11 GiB when each of 49 crates had its own), so the targets
1714
+ // multi-GB build tree that must never enter the Nx cache, so the targets
1669
1715
  // deliberately declare `outputs: []`. Reading `--target-dir` back off the
1670
1716
  // commands means the list cannot drift from what actually gets written, and a
1671
1717
  // directory is named relative to the command's own cwd, not the project root.
1718
+ // The workspace's default `target/` is not listed: every dev build shares it.
1672
1719
  const cargoBuildDirs = [
1673
1720
  ...new Set(
1674
1721
  Object.entries(targets).flatMap(([name, target]) => {
@@ -1756,6 +1803,23 @@ async function createProjectTargets(
1756
1803
  ? { [CARGO_TOOLCHAIN_NAMED_INPUT]: [...CARGO_TOOLCHAIN_PIN_INPUTS] }
1757
1804
  : {}),
1758
1805
  };
1806
+ // A custom Cargo target names this instead of a hand-kept crate list or a
1807
+ // per-project runtime command: Cargo decides the closure once per graph
1808
+ // computation and Nx hashes its files from its own index. Defined for a
1809
+ // project with a workspace crate under its root — the same projects the
1810
+ // `--closure <projectRoot>` command could describe.
1811
+ const closureSources = cargoWorkspaces.filter((workspace) =>
1812
+ workspace.packages.some(({ package: pkg }) => {
1813
+ const crate = posix.join(workspace.projectRoot, pkg.dir);
1814
+ return projectRoot === '.' || crate === projectRoot || crate.startsWith(`${projectRoot}/`);
1815
+ }),
1816
+ );
1817
+ if (closureSources.length > 0) {
1818
+ namedInputs[CARGO_CLOSURE_INPUT] = await cargoClosureInputs(
1819
+ projectRoot,
1820
+ await Promise.all(closureSources.map((workspace) => workspace.closureSource)),
1821
+ );
1822
+ }
1759
1823
  return {
1760
1824
  projects: {
1761
1825
  [projectRoot]: {
@@ -2002,13 +2066,14 @@ function createNapiTargets(
2002
2066
  const targets: Record<string, TargetConfiguration> = {};
2003
2067
  const hostCompilerEnv = napiCompilerEnv(hostPlatform, hostPlatform);
2004
2068
 
2005
- // The addon the test suite loads. A dev-profile build shares target/debug
2006
- // with cargo-test's dependency graph, so after the tests compile this is
2007
- // nearly free — where the release platform build it replaces cost minutes
2008
- // and sat alone on the test critical path. The output lives OUTSIDE dist/
2009
- // because packages publish `files: ["dist"]` wholesale: a debug addon under
2010
- // dist/ would ship. native.ts resolves this directory only when the
2011
- // napi-test target sets NAPI_DEBUG_ADDON=1, so production loads never see it.
2069
+ // The addon the test suite loads. A plain dev build shares target/debug with
2070
+ // cargo-test's dependency graph, so after the tests compile this is nearly
2071
+ // free, where a `--target <triple>` platform build compiles the graph again
2072
+ // under target/<triple>/. The output lives OUTSIDE dist/ because packages
2073
+ // publish `files: ["dist"]` wholesale: an addon the test run wrote under
2074
+ // dist/ would ship in place of the release build. native.ts resolves this
2075
+ // directory only when the napi-test target sets NAPI_DEBUG_ADDON=1, so
2076
+ // production loads never see it.
2012
2077
  targets['napi-debug'] = {
2013
2078
  executor: 'nx:run-commands',
2014
2079
  cache: true,
@@ -2024,9 +2089,13 @@ function createNapiTargets(
2024
2089
 
2025
2090
  if (nativeHostTargetName === null) {
2026
2091
  // The platform target for this host triple is the same compilation, so a
2027
- // second cargo-napi invocation only buys a duplicate dependency graph in
2028
- // target/release; native.ts already resolves the identical platform-suffixed
2029
- // filename from dist/native/<platform-dir> after dist/native/host.
2092
+ // second cargo-napi invocation only buys a duplicate dependency graph
2093
+ // (a plain build and a `--target` build write different target/
2094
+ // subdirectories); native.ts already resolves the identical
2095
+ // platform-suffixed filename from dist/native/<platform-dir> after
2096
+ // dist/native/host.
2097
+ const hostBuild = (profile: CargoProfile): string =>
2098
+ `${napiCommand} build${CARGO_PROFILE_FLAG[profile]} --platform --no-js --dts ${config.binaryName}.napi.d.ts ${commonCommand} --output-dir ${outputPath('dist/native/host')}`;
2030
2099
  targets['cargo-napi'] = {
2031
2100
  executor: 'nx:run-commands',
2032
2101
  cache: true,
@@ -2035,9 +2104,10 @@ function createNapiTargets(
2035
2104
  outputs: ['{projectRoot}/dist/native/host'],
2036
2105
  options: {
2037
2106
  cwd: cargoCwd,
2038
- command: `${napiCommand} build --release --platform --no-js --dts ${config.binaryName}.napi.d.ts ${commonCommand} --output-dir ${outputPath('dist/native/host')}`,
2107
+ command: hostBuild('dev'),
2039
2108
  ...(hostCompilerEnv ? { env: hostCompilerEnv } : {}),
2040
2109
  },
2110
+ configurations: { [RELEASE_CONFIGURATION]: { command: hostBuild('release') } },
2041
2111
  };
2042
2112
  }
2043
2113
 
@@ -2071,6 +2141,8 @@ function createNapiTargets(
2071
2141
  const useNapiCross = usesNapiCross(convention, hostPlatform);
2072
2142
  const crossFlag = useNapiCross ? ' --use-napi-cross' : '';
2073
2143
  const compilerEnv = napiCompilerEnv(convention, hostPlatform);
2144
+ const platformBuild = (profile: CargoProfile): string =>
2145
+ `${napiCommand} build${CARGO_PROFILE_FLAG[profile]} --platform --no-js --dts ${config.binaryName}.${convention.outputName}.d.ts --target ${triple}${crossFlag} ${commonCommand} --output-dir ${outputPath(outputDirectory)}`;
2074
2146
  targets[targetName] = {
2075
2147
  executor: 'nx:run-commands',
2076
2148
  cache: true,
@@ -2079,12 +2151,13 @@ function createNapiTargets(
2079
2151
  outputs: [`{projectRoot}/${outputDirectory}`],
2080
2152
  options: {
2081
2153
  cwd: cargoCwd,
2082
- command: `${napiCommand} build --release --platform --no-js --dts ${config.binaryName}.${convention.outputName}.d.ts --target ${triple}${crossFlag} ${commonCommand} --output-dir ${outputPath(outputDirectory)}`,
2154
+ command: platformBuild('dev'),
2083
2155
  // Genuine Linux cross-compiles use Clang plus napi-rs's downloaded
2084
2156
  // GNU sysroot. A native Linux target uses Nix's cc wrapper so C build
2085
2157
  // scripts resolve the host libc headers instead.
2086
2158
  ...(compilerEnv ? { env: compilerEnv } : {}),
2087
2159
  },
2160
+ configurations: { [RELEASE_CONFIGURATION]: { command: platformBuild('release') } },
2088
2161
  };
2089
2162
  }
2090
2163
  return targets;
@@ -2095,10 +2168,11 @@ function createNapiTestTarget(projectRoot: string, hasDedicatedBunfig: boolean):
2095
2168
  return {
2096
2169
  executor: '@smoothbricks/nx-plugin:bounded-exec',
2097
2170
  cache: true,
2098
- // Deliberately NOT `build`: the aggregate drags the host-platform release
2099
- // binaries onto the test critical path, and the tests assert behavior, not
2100
- // optimization level. tsc-js supplies dist/ts for the suite's own imports;
2101
- // napi-debug supplies the addon.
2171
+ // Deliberately NOT `build`: the aggregate drags the host-platform binaries
2172
+ // onto the test critical path, and a `--target <triple>` platform build
2173
+ // compiles the whole graph again. tsc-js supplies dist/ts for the suite's
2174
+ // own imports; napi-debug supplies the addon from the graph cargo-test
2175
+ // already built.
2102
2176
  dependsOn: ['cargo-test', 'napi-debug', 'tsc-js', '^build'],
2103
2177
  options: {
2104
2178
  // cwd is src/, not the package root: `bun test <arg>` treats the arg as a
@@ -2224,6 +2298,12 @@ interface CargoWorkspace {
2224
2298
  projectRoot: string;
2225
2299
  /** One derivation memo for the whole graph computation (see cargoPackageTestInputs). */
2226
2300
  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>;
2227
2307
  }
2228
2308
 
2229
2309
  type CargoTargetDependency = NonNullable<TargetConfiguration['dependsOn']>[number];
@@ -2253,7 +2333,7 @@ async function resolveCargoWorkspaces(
2253
2333
  // createProjectTargets records the path-specific parse failure below.
2254
2334
  }
2255
2335
  }
2256
- const workspaces: CargoWorkspace[] = [];
2336
+ const found: Omit<CargoWorkspace, 'closureSource'>[] = [];
2257
2337
  for (const rootProject of projects) {
2258
2338
  const absoluteRoot = join(workspaceRoot, rootProject.root);
2259
2339
  const manifest = join(absoluteRoot, 'Cargo.toml');
@@ -2292,30 +2372,59 @@ async function resolveCargoWorkspaces(
2292
2372
  }
2293
2373
  packages.push({ package: pkg, pieces });
2294
2374
  }
2295
- workspaces.push({
2375
+ found.push({
2296
2376
  packages,
2297
2377
  rootProjectName: rootProject.name,
2298
2378
  projectRoot: rootProject.root,
2299
2379
  inputsCache: createCargoInputsCache(),
2300
2380
  });
2301
2381
  }
2302
- return workspaces;
2382
+ if (found.length === 0) return [];
2383
+ // Started only once every workspace listed cleanly, so a listing that throws
2384
+ // leaves no Cargo process behind.
2385
+ 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
+ }));
2303
2394
  }
2304
2395
 
2305
2396
  /**
2306
2397
  * One bounded target per crate; a crate that declares `smoothbricks.test.shards`
2307
2398
  * gets one per shard plus one for the tests nextest.toml singles out.
2308
2399
  *
2309
- * Every piece EXECUTES, it never builds: `--archive-file` runs the binaries
2310
- * `cargo-test-archive` compiled once for the whole workspace, extracted into
2311
- * the runner's own temporary directory. That is what removed the two costs the
2312
- * old shape could not avoid. `nextest run --workspace -E 'package(X)'` re-entered
2313
- * cargo once per crate, and the `-p X` form re-resolved that crate's features so
2314
- * cargo rebuilt the divergent half inside the bounded window — measured at 57.9s
2315
- * of a 120s budget on a hosted 3-core macOS runner, killing the run with 264
2316
- * tests still to go. Neither form is reachable from an archive: nextest rejects
2317
- * `--workspace` and `-p` beside `--archive-file`, so the filterset is the only
2318
- * selector left, and it selects the same tests it always did.
2400
+ * Every piece EXECUTES, it never builds: it runs the binaries
2401
+ * `cargo-test-archive` compiled once for the whole workspace. That is what
2402
+ * removed the two costs the old shape could not avoid. `nextest run --workspace
2403
+ * -E 'package(X)'` re-entered cargo once per crate, and the `-p X` form
2404
+ * re-resolved that crate's features so cargo rebuilt the divergent half inside
2405
+ * the bounded window — measured at 57.9s of a 120s budget on a hosted 3-core
2406
+ * macOS runner, killing the run with 264 tests still to go. Neither form is
2407
+ * reachable from a reused build: nextest rejects `--workspace` and `-p` beside
2408
+ * it, so the filterset is the only selector left, and it selects the same tests
2409
+ * it always did.
2410
+ *
2411
+ * The archive is unpacked ONCE per content, not once per piece. `nextest run
2412
+ * --archive-file` extracts the whole archive into a temporary directory of its
2413
+ * own on every run, so N pieces paid N extractions of every binary in the
2414
+ * workspace to run 1/N of the tests each: on a workspace with ~200 test
2415
+ * binaries, ~25 runners extracting side by side on a loaded host all hit the
2416
+ * 120s bound still "Extracting". `smoo-nx-nextest-extract` hashes the archive,
2417
+ * unpacks it under that hash unless a previous runner already did — one
2418
+ * extracts while the rest wait — and prints the directory; the run then reuses
2419
+ * it through nextest's own reuse-build flags, which is what `--archive-file`
2420
+ * does after unpacking. Keyed by the archive's bytes, so a rebuilt or restored
2421
+ * archive can never run an older extraction's binaries. The one difference
2422
+ * from `--archive-file` is the Rust standard library directory: nextest has no
2423
+ * flag to remap it, so a run uses the producing toolchain's own sysroot rather
2424
+ * than the archived copy of it — the same toolchain, because the archive's hash
2425
+ * covers the toolchain pin. The extraction lives under `target/nextest`, never
2426
+ * in an Nx output: it is a multi-GB tree for a cached verdict that has no
2427
+ * outputs.
2319
2428
  *
2320
2429
  * `--workspace-remap .` is not cosmetic. An archive records the ABSOLUTE paths
2321
2430
  * of the tree that produced it; without the remap, a restored archive hands
@@ -2348,8 +2457,8 @@ async function resolveCargoWorkspaces(
2348
2457
  * making valid empty crates and shards fail execution.
2349
2458
  *
2350
2459
  * Pieces fan out rather than chain. The chain existed because every runner was
2351
- * a cargo writer on one flocked `target/`; an archive run writes nothing there,
2352
- * so the only serialization left is the one the machine's own scheduler does.
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.
2353
2462
  */
2354
2463
  async function addCargoTestTargets(
2355
2464
  targets: Record<string, TargetConfiguration>,
@@ -2360,6 +2469,12 @@ async function addCargoTestTargets(
2360
2469
  ): Promise<string[]> {
2361
2470
  const targetNames: string[] = [];
2362
2471
  const toolConfig = nextestToolConfigArg(workspaceRoot, workspace.projectRoot, PLUGIN_NEXTEST_CONFIG);
2472
+ // Relative to the runner's cwd, so the hashed command is the same in every checkout.
2473
+ const extractBin = posix.join(posix.relative(workspace.projectRoot, '.'), NEXTEST_EXTRACT_BIN);
2474
+ const reuseExtraction =
2475
+ `--binaries-metadata "$extracted/${EXTRACTED_BINARIES_METADATA}" ` +
2476
+ `--cargo-metadata "$extracted/${EXTRACTED_CARGO_METADATA}" ` +
2477
+ `--target-dir-remap "$extracted/${EXTRACTED_TARGET_DIR}"`;
2363
2478
  for (const plan of workspace.packages) {
2364
2479
  if (plan.package.projectRoot !== projectRoot) {
2365
2480
  continue;
@@ -2402,18 +2517,15 @@ async function addCargoTestTargets(
2402
2517
  inputs,
2403
2518
  dependsOn: [archive],
2404
2519
  options: {
2405
- command: cargoFrozen(
2406
- `nextest run --archive-file ${CARGO_TEST_ARCHIVE_FILE} --workspace-remap . -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none ${toolConfig}`,
2407
- ),
2520
+ command:
2521
+ `extracted="$(node ${extractBin} ${CARGO_TEST_ARCHIVE_FILE})" && ` +
2522
+ cargoFrozen(
2523
+ `nextest run ${reuseExtraction} --workspace-remap . -E '${piece.selector}'${piece.extra} --no-tests=pass --user-config-file none ${toolConfig}`,
2524
+ ),
2408
2525
  cwd: workspace.projectRoot,
2409
2526
  timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
2410
2527
  killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
2411
2528
  },
2412
- // The archive already holds the profile it was built with, and nextest
2413
- // rejects `--release` beside `--archive-file`. Nx propagates the
2414
- // configuration to this target's dependencies, so `:production` builds
2415
- // the release archive and this command runs exactly what it finds.
2416
- configurations: { production: {} },
2417
2529
  };
2418
2530
  }
2419
2531
  }
@@ -202,7 +202,7 @@ export function defineCiWorkflow(options: CiWorkflowDefinitionOptions): CiWorkfl
202
202
  steps.push({ kind: CiWorkflowStepKind.BrowserTests, name: '🌐 Browser Tests' });
203
203
  }
204
204
  steps.push(
205
- { kind: CiWorkflowStepKind.ManagedFilesCheck, name: '🩺 Check managed-file drift' },
205
+ { kind: CiWorkflowStepKind.ManagedFilesCheck, name: '🩺 Check monorepo policies and managed-file drift' },
206
206
  { kind: CiWorkflowStepKind.ManagedFilesDispatch, name: '🔁 Dispatch managed-file drift healing' },
207
207
  { kind: CiWorkflowStepKind.SaveNxCache, name: '💾 Save Nx cache' },
208
208
  { kind: CiWorkflowStepKind.UploadTraceDbs, name: '📎 Upload trace DBs' },
@@ -477,24 +477,21 @@ function yamlLinesForStep(step: CiWorkflowStep, options: CiWorkflowDefinitionOpt
477
477
  ` run: smoo github-ci nx-run-many --targets "${darwinCrossTestTargetNames(options).join(',')}"`,
478
478
  ];
479
479
  case CiWorkflowStepKind.ManagedFilesCheck:
480
- // The authoritative drift check is nearly free here (devenv is already
481
- // up). --warn never fails the job and publishes the step output
482
- // `drifted=<count>` for the dispatch step below.
483
- return [
484
- ` - name: ${step.name}`,
485
- ' id: managed-drift',
486
- ' if:',
487
- " ${{ github.event_name == 'push' && github.ref == format('refs/heads/{0}',",
488
- ' github.event.repository.default_branch) }}',
489
- ' run: smoo monorepo check --warn',
490
- ];
480
+ // Runs on every event, pull requests included: the package policies fail the job, since
481
+ // nothing heals them, while --warn keeps managed-file drift non-blocking and publishes the
482
+ // step output `drifted=<count>` for the dispatch step below. The check is nearly free here
483
+ // (devenv is already up).
484
+ return [` - name: ${step.name}`, ' id: managed-drift', ' run: smoo monorepo check --warn'];
491
485
  case CiWorkflowStepKind.ManagedFilesDispatch:
492
486
  // Dispatches the managed-files workflow, which maintains the persistent
493
- // review PR. workflow_dispatch is exempt from GitHub's GITHUB_TOKEN
494
- // recursion guard, so the default token suffices.
487
+ // review PR, from the default branch only. workflow_dispatch is exempt from
488
+ // GitHub's GITHUB_TOKEN recursion guard, so the default token suffices.
495
489
  return [
496
490
  ` - name: ${step.name}`,
497
- " if: steps.managed-drift.outputs.drifted != '' && steps.managed-drift.outputs.drifted != '0'",
491
+ ' if: >-',
492
+ " ${{ github.event_name == 'push' && github.ref == format('refs/heads/{0}',",
493
+ " github.event.repository.default_branch) && steps.managed-drift.outputs.drifted != '' &&",
494
+ " steps.managed-drift.outputs.drifted != '0' }}",
498
495
  ' run: smoo github-ci dispatch-workflow --workflow managed-files.yml --ref "$GITHUB_REF_NAME"',
499
496
  ];
500
497
  case CiWorkflowStepKind.Deploy:
@@ -109,6 +109,11 @@ const managedFiles: ManagedFileDescriptor[] = [
109
109
  target: 'tooling/direnv/setup-environment.ts',
110
110
  executable: true,
111
111
  },
112
+ {
113
+ kind: 'raw',
114
+ source: 'tooling/direnv/developer-links.ts',
115
+ target: 'tooling/direnv/developer-links.ts',
116
+ },
112
117
  {
113
118
  kind: 'raw',
114
119
  source: 'tooling/direnv/secret-references.ts',
@@ -125,6 +130,11 @@ const managedFiles: ManagedFileDescriptor[] = [
125
130
  source: 'tooling/direnv/devenv.smoo.nix',
126
131
  target: 'tooling/direnv/devenv.smoo.nix',
127
132
  },
133
+ {
134
+ kind: 'raw',
135
+ source: 'tooling/direnv/shared-caches.sh',
136
+ target: 'tooling/direnv/shared-caches.sh',
137
+ },
128
138
  {
129
139
  kind: 'raw',
130
140
  source: 'tooling/git-hooks/pre-commit.sh',