@smoothbricks/nx-plugin 0.4.30 → 0.5.0

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 +36 -21
  2. package/dist/cargo-toolchain-policy.d.ts +6 -6
  3. package/dist/cargo-toolchain-policy.js +6 -6
  4. package/dist/cross-check-policy.d.ts +31 -14
  5. package/dist/cross-check-policy.d.ts.map +1 -1
  6. package/dist/cross-check-policy.js +43 -15
  7. package/dist/executors/bounded-exec/cpu-tokens.d.ts +22 -5
  8. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
  9. package/dist/executors/bounded-exec/cpu-tokens.js +189 -18
  10. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  11. package/dist/executors/bounded-exec/executor.js +3 -2
  12. package/dist/executors/bounded-exec/run-cpu-pool.d.ts +10 -0
  13. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -0
  14. package/dist/executors/bounded-exec/run-cpu-pool.js +63 -0
  15. package/dist/executors/cargo-zigbuild/executor.d.ts +60 -0
  16. package/dist/executors/cargo-zigbuild/executor.d.ts.map +1 -0
  17. package/dist/executors/cargo-zigbuild/executor.js +266 -0
  18. package/dist/executors/cargo-zigbuild/schema.d.ts +15 -0
  19. package/dist/executors/cargo-zigbuild/schema.d.ts.map +1 -0
  20. package/dist/index.d.ts +3 -16
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +69 -92
  23. package/dist/testing.d.ts.map +1 -1
  24. package/dist/testing.js +3 -2
  25. package/executors.json +4 -4
  26. package/managed/raw/patches/nx@23.2.1.patch +177 -9
  27. package/managed/raw/tooling/direnv/devenv.smoo.nix +46 -46
  28. package/managed/raw/tooling/direnv/setup-environment.ts +13 -0
  29. package/managed/raw/tooling/git-hooks/pre-push.sh +41 -23
  30. package/package.json +1 -1
  31. package/src/cargo-closure-key.test.ts +3 -2
  32. package/src/cargo-toolchain-policy.ts +6 -6
  33. package/src/cross-check-policy.test.ts +90 -11
  34. package/src/cross-check-policy.ts +47 -17
  35. package/src/executors/bounded-exec/cpu-tokens.test.ts +205 -0
  36. package/src/executors/bounded-exec/cpu-tokens.ts +229 -22
  37. package/src/executors/bounded-exec/executor.ts +3 -2
  38. package/src/executors/bounded-exec/run-cpu-pool.test.ts +76 -0
  39. package/src/executors/bounded-exec/run-cpu-pool.ts +75 -0
  40. package/src/executors/cargo-zigbuild/executor.test.ts +129 -0
  41. package/src/executors/cargo-zigbuild/executor.ts +176 -0
  42. package/src/executors/cargo-zigbuild/schema.json +35 -0
  43. package/src/executors/cargo-zigbuild/schema.ts +14 -0
  44. package/src/index.test.ts +41 -63
  45. package/src/index.ts +73 -101
  46. package/src/nx-daemon-validity.test.ts +65 -0
  47. package/src/testing.ts +3 -2
  48. package/test-timings.json +2 -1
  49. package/dist/executors/napi-cross-toolchain/executor.d.ts +0 -42
  50. package/dist/executors/napi-cross-toolchain/executor.d.ts.map +0 -1
  51. package/dist/executors/napi-cross-toolchain/executor.js +0 -126
  52. package/dist/executors/napi-cross-toolchain/schema.d.ts +0 -4
  53. package/dist/executors/napi-cross-toolchain/schema.d.ts.map +0 -1
  54. package/src/executors/napi-cross-toolchain/executor.test.ts +0 -112
  55. package/src/executors/napi-cross-toolchain/executor.ts +0 -165
  56. package/src/executors/napi-cross-toolchain/schema.json +0 -15
  57. package/src/executors/napi-cross-toolchain/schema.ts +0 -3
  58. /package/dist/executors/{napi-cross-toolchain → cargo-zigbuild}/schema.js +0 -0
package/src/index.ts CHANGED
@@ -58,14 +58,15 @@ import {
58
58
  CARGO_CROSS_LINT_TARGET,
59
59
  CARGO_FETCH_COMMAND,
60
60
  CARGO_FETCH_TARGET,
61
- CARGO_FROZEN_PREFIX,
62
61
  CARGO_LINT_CLIPPY_COMMAND,
63
62
  cargoCrossTestArchiveFile,
64
63
  cargoCrossTestArchiveTargetName,
65
64
  cargoCrossTestTargetName,
66
65
  cargoFrozen,
66
+ runsFrozenCargo,
67
67
  } from './cross-check-policy.js';
68
68
  import type { EslintFileSet } from './eslint-file-set.js';
69
+ import { closeRunCpuPool, serveRunCpuPool } from './executors/bounded-exec/run-cpu-pool.js';
69
70
  import { hashVersionlessCrateManifests } from './manifest-hash.js';
70
71
  import {
71
72
  EXTRACTED_BINARIES_METADATA,
@@ -465,14 +466,7 @@ function sameNapiPlatform(left: NapiPlatform, right: NapiPlatform | null): boole
465
466
  function hostPlatformTargetNames(targetNames: Iterable<string>, hostPlatform: NapiPlatform | null): string[] {
466
467
  if (hostPlatform === null) return [];
467
468
  const suffix = `-${hostPlatform.architecture}-${hostPlatform.targetFamily}`;
468
- return [...new Set(targetNames)]
469
- .filter(
470
- // Binary targets only. A toolchain prerequisite carries the same platform
471
- // suffix but produces no artifact: the cross builds that need it depend
472
- // on it directly, so the aggregate stays a list of outputs.
473
- (name) => name.endsWith(suffix) && !name.startsWith(NAPI_TOOLCHAIN_TARGET_PREFIX),
474
- )
475
- .sort();
469
+ return [...new Set(targetNames)].filter((name) => name.endsWith(suffix)).sort();
476
470
  }
477
471
 
478
472
  /**
@@ -522,6 +516,8 @@ const CARGO_INPUTS = [
522
516
  // build really reads is the package's `napi` block (binary name, targets) and
523
517
  // the `@napi-rs/cli` it runs; those are hashed as such by napiInputs().
524
518
  const CARGO_OUTPUT_INPUTS = CARGO_INPUTS;
519
+ /** A full cargo-mutants sweep's wall-clock backstop: a sweep takes hours, and a hung one must still end. */
520
+ const MUTATION_TIMEOUT_MS = 6 * 60 * 60 * 1000;
525
521
  // The toolchain half of a cargo task's identity lives in its own module
526
522
  // because `smoo monorepo validate` reads the same two constants to refuse a
527
523
  // declaration that drops it. See cargo-toolchain-policy.ts for why it is the
@@ -563,18 +559,23 @@ function targetCommandText(target: TargetConfiguration): string {
563
559
  }
564
560
  /**
565
561
  * The N-API build's inputs beyond the Cargo sources: the toolchain pin, the
566
- * package's `napi` block, which names the binary and the targets, and the napi
567
- * CLI's resolved version from the lockfile. Neither the package version nor
568
- * any other lockfile entry is part of the artifact.
562
+ * package's `napi` block, which names the binary and the targets, and the
563
+ * resolved version of the npm package that drives the build (the napi CLI on
564
+ * macOS; none on Linux, where devenv's zig and cargo-zigbuild are covered by
565
+ * the pin). Neither the package version nor any other lockfile entry is part
566
+ * of the artifact.
569
567
  *
570
- * The pin is stated here rather than inherited: `napi build` compiles with the
571
- * same rustc and links against the same SDK as any other cargo target, but its
572
- * command does not spell `cargo --frozen`, which is how every other cached
573
- * cargo target acquires its toolchain identity. Without it a `.node` linked
574
- * against one toolchain hashed equal to the same sources linked against the
575
- * next one.
568
+ * The pin is stated here rather than inherited: neither `napi build` nor the
569
+ * `cargo-zigbuild` executor spells `cargo --frozen`, which is how every other
570
+ * cached cargo target acquires its toolchain identity. Without it a `.node`
571
+ * linked against one toolchain hashed equal to the same sources linked against
572
+ * the next one.
576
573
  */
577
- function napiInputs(projectRoot: string, repoRooted: boolean): TargetConfiguration['inputs'] {
574
+ function napiInputs(
575
+ projectRoot: string,
576
+ repoRooted: boolean,
577
+ externalDependencies: readonly string[],
578
+ ): TargetConfiguration['inputs'] {
578
579
  // Runtime inputs execute from the workspace root, so the manifest is named
579
580
  // by its path from there whichever way the Cargo invocation is rooted.
580
581
  const packageJson = `./${posix.join(projectRoot, 'package.json')}`;
@@ -584,7 +585,7 @@ function napiInputs(projectRoot: string, repoRooted: boolean): TargetConfigurati
584
585
  ...(repoRooted ? ['{workspaceRoot}/scripts/*.sh'] : []),
585
586
  ...CARGO_TOOLCHAIN_PIN_INPUTS,
586
587
  runtimeInput(`bun -e 'console.log(JSON.stringify(require(${JSON.stringify(packageJson)}).napi ?? null))'`),
587
- { externalDependencies: ['@napi-rs/cli'] },
588
+ { externalDependencies: [...externalDependencies] },
588
589
  ];
589
590
  }
590
591
 
@@ -615,31 +616,14 @@ const NAPI_TARGET_CONVENTIONS: Readonly<Record<string, NapiTargetConvention>> =
615
616
  },
616
617
  };
617
618
 
618
- const NATIVE_LINUX_COMPILER_ENV = Object.freeze({ CC: 'cc', CXX: 'c++' });
619
- const CROSS_LINUX_COMPILER_ENV = Object.freeze({ TARGET_CC: 'clang', TARGET_CXX: 'clang++' });
620
-
621
- function usesNapiCross(target: NapiTargetConvention, hostPlatform: NapiPlatform | null): boolean {
622
- return target.targetFamily === 'linux' && !sameNapiPlatform(target, hostPlatform);
623
- }
624
-
625
- function napiCompilerEnv(
626
- target: NapiPlatform | null,
627
- hostPlatform: NapiPlatform | null,
628
- ): Readonly<Record<string, string>> | undefined {
629
- if (target?.targetFamily !== 'linux') return undefined;
630
- return sameNapiPlatform(target, hostPlatform) ? NATIVE_LINUX_COMPILER_ENV : CROSS_LINUX_COMPILER_ENV;
631
- }
632
-
633
- const NAPI_TOOLCHAIN_TARGET_PREFIX = 'napi-toolchain-';
634
-
635
619
  /**
636
- * Prerequisite target that extracts the cross toolchain one triple needs. The
637
- * platform suffix keeps it inside its own platform family, so it can be a
638
- * dependency of `napi-<arch>-linux` and package-local `cli-<arch>-linux`.
620
+ * The glibc symbol-version floor every Linux N-API addon links against.
621
+ * cargo-zigbuild reads it from the target triple's suffix
622
+ * (`x86_64-unknown-linux-gnu.2.17`) and links zig's stubs for exactly that
623
+ * glibc, so the floor is this literal on every host rather than whatever glibc
624
+ * the build machine carries. 2.17 is below Node ≥ 18's own 2.28 floor.
639
625
  */
640
- export function napiToolchainTargetName(convention: NapiTargetConvention): string {
641
- return `${NAPI_TOOLCHAIN_TARGET_PREFIX}${convention.architecture}-${convention.targetFamily}`;
642
- }
626
+ const LINUX_GLIBC_FLOOR = '2.17';
643
627
 
644
628
  /**
645
629
  * Cargo's two profiles. A build target's default options compile `dev`, the
@@ -1087,15 +1071,19 @@ function isBuildOutputPackageJson(packageJsonPath: string): boolean {
1087
1071
  /**
1088
1072
  * Nx accepts one pre/post task-hook pair per plugin. Two independent observers share it: the controller's task-results
1089
1073
  * hint and the opt-in producer completion receipts. Each sees the same options and run context; neither reads the
1090
- * other's output. The hint is recorded first so a producer-receipt bug cannot cost the controller its evidence.
1074
+ * other's output. The hint is recorded first so a producer-receipt bug cannot cost the controller its evidence. The
1075
+ * pair also serves a run on a host without a gateway its own CPU pool, which every task inherits
1076
+ * (`run-cpu-pool.ts`), and closes it when the run ends.
1091
1077
  */
1092
1078
  export const preTasksExecution: PreTasksExecution<ProducerReceiptsOptions> = async (options, context) => {
1093
1079
  preTaskResults(options, context);
1094
1080
  await preProducerReceipts(options, context);
1081
+ await serveRunCpuPool(context);
1095
1082
  };
1096
1083
  export const postTasksExecution: PostTasksExecution<ProducerReceiptsOptions> = async (options, context) => {
1097
1084
  await postTaskResults(options, context);
1098
1085
  await postProducerReceipts(options, context);
1086
+ await closeRunCpuPool(context);
1099
1087
  };
1100
1088
 
1101
1089
  export default { createNodesV2, postTasksExecution, preTasksExecution };
@@ -1529,8 +1517,8 @@ async function createProjectTargets(
1529
1517
  // than a silent absence.
1530
1518
  //
1531
1519
  // Deliberately NOT pushed to validationTargets: joining the lint aggregate
1532
- // would make `bun run lint` — and CI's own Linux lint, already native — demand
1533
- // the 0.4 GiB cross toolchain that the devenv profile exists to keep opt-in.
1520
+ // would make `bun run lint` — and CI's own Linux lint, already native — on a
1521
+ // macOS host demand the zig toolchain that the devenv profile keeps opt-in.
1534
1522
  targets[CARGO_CROSS_LINT_TARGET] = {
1535
1523
  executor: 'nx:run-commands',
1536
1524
  cache: true,
@@ -1543,10 +1531,13 @@ async function createProjectTargets(
1543
1531
  };
1544
1532
  // Mutation runs are minutes-to-hours: never cached, never part of build/lint.
1545
1533
  // CI runs these per-PR via `cargo mutants --in-diff` (see mutants.toml docs).
1534
+ // bounded-exec sizes cargo-mutants' own jobserver to its CPU grant (its `cargo-mutants` runner kind), which
1535
+ // otherwise starts one task per core whatever else runs on the host. It reports no test bounded-exec can read as
1536
+ // progress, so its bound is a wall-clock backstop, not a performance budget.
1546
1537
  targets.mutation = {
1547
- executor: 'nx:run-commands',
1538
+ executor: '@smoothbricks/nx-plugin:bounded-exec',
1548
1539
  cache: false,
1549
- options: { command: cargoFrozen('mutants --workspace'), cwd: cargoWorkspaceRoot },
1540
+ options: { command: cargoFrozen('mutants --workspace'), cwd: cargoWorkspaceRoot, timeoutMs: MUTATION_TIMEOUT_MS },
1550
1541
  };
1551
1542
  targets.bench = {
1552
1543
  executor: 'nx:run-commands',
@@ -1656,9 +1647,8 @@ async function createProjectTargets(
1656
1647
  targets[CARGO_FETCH_TARGET] = {
1657
1648
  executor: 'nx:run-commands',
1658
1649
  // The download lands in CARGO_HOME, outside the workspace: a cache hit
1659
- // would skip work a cold registry still needs — same reason
1660
- // napi-toolchain-* is uncached. Nothing here is a workspace output, so
1661
- // there is nothing for Nx to restore in its place.
1650
+ // would skip work a cold registry still needs. Nothing here is a
1651
+ // workspace output, so there is nothing for Nx to restore in its place.
1662
1652
  cache: false,
1663
1653
  options: {
1664
1654
  // One fetch per manifest, independent of the others: Cargo's own
@@ -1678,7 +1668,7 @@ async function createProjectTargets(
1678
1668
  continue;
1679
1669
  }
1680
1670
  const text = targetCommandText(target);
1681
- if (text.includes(CARGO_FROZEN_PREFIX)) {
1671
+ if (runsFrozenCargo(text)) {
1682
1672
  target.dependsOn = [CARGO_FETCH_TARGET, ...(target.dependsOn ?? [])];
1683
1673
  }
1684
1674
  }
@@ -1689,7 +1679,7 @@ async function createProjectTargets(
1689
1679
  });
1690
1680
  for (const target of Object.values(targets)) {
1691
1681
  const text = targetCommandText(target);
1692
- if (text.includes(CARGO_FROZEN_PREFIX)) {
1682
+ if (runsFrozenCargo(text)) {
1693
1683
  target.dependsOn = [rootFetch, ...(target.dependsOn ?? [])];
1694
1684
  }
1695
1685
  }
@@ -1701,7 +1691,7 @@ async function createProjectTargets(
1701
1691
  // the target already does.
1702
1692
  for (const [name, target] of Object.entries(targets)) {
1703
1693
  const text = targetCommandText(target);
1704
- if (!text.includes(CARGO_FROZEN_PREFIX)) continue;
1694
+ if (!runsFrozenCargo(text)) continue;
1705
1695
  target.outputs ??= [];
1706
1696
  if (!target.cache) continue;
1707
1697
  const cwd: unknown = target.options?.cwd;
@@ -2232,27 +2222,17 @@ function createNapiTargets(
2232
2222
  // that stale local artifacts can mask for months.
2233
2223
  const packageJsonPath = repoRooted ? posix.join(projectRoot, 'package.json') : 'package.json';
2234
2224
  const commonCommand = `--manifest-path ${config.manifestPath} --package ${config.cargoPackage} --package-json-path ${packageJsonPath}`;
2235
- const cargoInputs = napiInputs(projectRoot, repoRooted);
2225
+ const cargoInputs = napiInputs(projectRoot, repoRooted, ['@napi-rs/cli']);
2236
2226
  const cargoCwd = repoRooted ? '.' : projectRoot;
2237
2227
  // A repository-root Cargo invocation runs outside the owning npm package, so
2238
2228
  // Nx's root-only PATH cannot resolve that package's napi CLI.
2239
2229
  const napiCommand = repoRooted ? posix.join(projectRoot, 'node_modules/.bin/napi') : 'napi';
2240
2230
  const outputPath = (projectOutput: string) => (repoRooted ? posix.join(projectRoot, projectOutput) : projectOutput);
2241
- const hostPlatformTargetName =
2242
- hostPlatform === null ? null : `napi-${hostPlatform.architecture}-${hostPlatform.targetFamily}`;
2243
- let nativeHostTargetName: string | null = null;
2244
- if (hostPlatformTargetName !== null) {
2245
- for (const triple of config.targets) {
2246
- const convention = NAPI_TARGET_CONVENTIONS[triple];
2247
- const targetName = convention ? `napi-${convention.architecture}-${convention.targetFamily}` : null;
2248
- if (targetName === hostPlatformTargetName && convention && !usesNapiCross(convention, hostPlatform)) {
2249
- nativeHostTargetName = targetName;
2250
- break;
2251
- }
2252
- }
2253
- }
2231
+ const hasHostPlatformTarget = config.targets.some((triple) => {
2232
+ const convention = NAPI_TARGET_CONVENTIONS[triple];
2233
+ return convention !== undefined && sameNapiPlatform(convention, hostPlatform);
2234
+ });
2254
2235
  const targets: Record<string, TargetConfiguration> = {};
2255
- const hostCompilerEnv = napiCompilerEnv(hostPlatform, hostPlatform);
2256
2236
 
2257
2237
  // The addon the test suite loads. A plain dev build shares target/debug with
2258
2238
  // cargo-test's dependency graph, so after the tests compile this is nearly
@@ -2271,11 +2251,10 @@ function createNapiTargets(
2271
2251
  options: {
2272
2252
  cwd: cargoCwd,
2273
2253
  command: `${napiCommand} build --platform --no-js --dts ${config.binaryName}.napi.d.ts ${commonCommand} --output-dir ${outputPath('.cache/native-debug')}`,
2274
- ...(hostCompilerEnv ? { env: hostCompilerEnv } : {}),
2275
2254
  },
2276
2255
  };
2277
2256
 
2278
- if (nativeHostTargetName === null) {
2257
+ if (!hasHostPlatformTarget) {
2279
2258
  // The platform target for this host triple is the same compilation, so a
2280
2259
  // second cargo-napi invocation only buys a duplicate dependency graph
2281
2260
  // (a plain build and a `--target` build write different target/
@@ -2293,32 +2272,11 @@ function createNapiTargets(
2293
2272
  options: {
2294
2273
  cwd: cargoCwd,
2295
2274
  command: hostBuild('dev'),
2296
- ...(hostCompilerEnv ? { env: hostCompilerEnv } : {}),
2297
2275
  },
2298
2276
  configurations: { [RELEASE_CONFIGURATION]: { command: hostBuild('release') } },
2299
2277
  };
2300
2278
  }
2301
2279
 
2302
- for (const triple of config.targets) {
2303
- const convention = NAPI_TARGET_CONVENTIONS[triple];
2304
- if (!convention || !usesNapiCross(convention, hostPlatform)) {
2305
- continue;
2306
- }
2307
- // One toolchain target per triple, shared by every cross build that needs
2308
- // it: the inferred napi-<arch>-linux plus any package-local cli-<arch>-linux.
2309
- // Nx runs a shared dependency once, which is what stops those builds from
2310
- // racing inside the Bun store. See the executor for the failure it removes.
2311
- // The name carries the platform suffix so a Linux platform target's
2312
- // dependency closure stays Linux-only (package-target-policy).
2313
- targets[napiToolchainTargetName(convention)] = {
2314
- executor: '@smoothbricks/nx-plugin:napi-cross-toolchain',
2315
- // Extraction lands in ~/.napi-rs, outside the workspace: a cache hit
2316
- // would skip work a cold home directory still needs.
2317
- cache: false,
2318
- options: { triple },
2319
- };
2320
- }
2321
-
2322
2280
  for (const triple of config.targets) {
2323
2281
  const convention = NAPI_TARGET_CONVENTIONS[triple];
2324
2282
  if (!convention) {
@@ -2326,24 +2284,38 @@ function createNapiTargets(
2326
2284
  }
2327
2285
  const targetName = `napi-${convention.architecture}-${convention.targetFamily}`;
2328
2286
  const outputDirectory = `dist/native/${convention.outputName}`;
2329
- const useNapiCross = usesNapiCross(convention, hostPlatform);
2330
- const crossFlag = useNapiCross ? ' --use-napi-cross' : '';
2331
- const compilerEnv = napiCompilerEnv(convention, hostPlatform);
2287
+ if (convention.targetFamily === 'linux') {
2288
+ // Every host builds a Linux addon the same way: devenv's zig through
2289
+ // cargo-zigbuild, glibc pinned by the triple suffix. The napi CLI cannot
2290
+ // carry that suffix (it looks for the artifact under
2291
+ // target/<triple>.2.17/), so the executor names the `.node` itself.
2292
+ targets[targetName] = {
2293
+ executor: '@smoothbricks/nx-plugin:cargo-zigbuild',
2294
+ cache: true,
2295
+ inputs: napiInputs(projectRoot, repoRooted, []),
2296
+ outputs: [`{projectRoot}/${outputDirectory}`],
2297
+ options: {
2298
+ target: `${triple}.${LINUX_GLIBC_FLOOR}`,
2299
+ manifestPath: posix.join(cargoCwd, config.manifestPath),
2300
+ package: config.cargoPackage,
2301
+ output: `{projectRoot}/${outputDirectory}/${config.binaryName}.${convention.outputName}.node`,
2302
+ },
2303
+ configurations: { [RELEASE_CONFIGURATION]: { release: true } },
2304
+ };
2305
+ continue;
2306
+ }
2307
+ // macOS addons need Apple's SDK, which zig does not carry: the napi CLI
2308
+ // drives the host's own toolchain and writes the platform `.d.ts`.
2332
2309
  const platformBuild = (profile: CargoProfile): string =>
2333
- `${napiCommand} build${CARGO_PROFILE_FLAG[profile]} --platform --no-js --dts ${config.binaryName}.${convention.outputName}.d.ts --target ${triple}${crossFlag} ${commonCommand} --output-dir ${outputPath(outputDirectory)}`;
2310
+ `${napiCommand} build${CARGO_PROFILE_FLAG[profile]} --platform --no-js --dts ${config.binaryName}.${convention.outputName}.d.ts --target ${triple} ${commonCommand} --output-dir ${outputPath(outputDirectory)}`;
2334
2311
  targets[targetName] = {
2335
2312
  executor: 'nx:run-commands',
2336
2313
  cache: true,
2337
- ...(useNapiCross ? { dependsOn: [napiToolchainTargetName(convention)] } : {}),
2338
2314
  inputs: cargoInputs,
2339
2315
  outputs: [`{projectRoot}/${outputDirectory}`],
2340
2316
  options: {
2341
2317
  cwd: cargoCwd,
2342
2318
  command: platformBuild('dev'),
2343
- // Genuine Linux cross-compiles use Clang plus napi-rs's downloaded
2344
- // GNU sysroot. A native Linux target uses Nix's cc wrapper so C build
2345
- // scripts resolve the host libc headers instead.
2346
- ...(compilerEnv ? { env: compilerEnv } : {}),
2347
2319
  },
2348
2320
  configurations: { [RELEASE_CONFIGURATION]: { command: platformBuild('release') } },
2349
2321
  };
@@ -800,6 +800,71 @@ it('stops a daemon once the workspace nx link points at a different version', as
800
800
  );
801
801
  });
802
802
 
803
+ it('restarts a daemon once a plugin linked from outside the workspace changes its bytes, in place or by a moved link, and only then', async () => {
804
+ await withNxFixture(
805
+ 'nx-daemon-validity-plugin-',
806
+ async ({ root, socketDirectory, workspace }) => {
807
+ const inference = (version: string) =>
808
+ `// inference ${version}\nmodule.exports = { name: 'linked-plugin', createNodesV2: ['**/never.json', () => []] };\n`;
809
+ // A developer link: the plugin is a package of another checkout, which has its own lockfile.
810
+ const checkout = async (name: string) => {
811
+ const plugin = join(root, name, 'plugin');
812
+ await mkdir(join(plugin, 'dist'), { recursive: true });
813
+ await writeFile(join(root, name, 'bun.lock'), 'checkout lock one\n');
814
+ await writeFile(
815
+ join(plugin, 'package.json'),
816
+ JSON.stringify({ name: 'linked-plugin', version: '1.0.0', main: 'dist/index.js', files: ['dist'] }),
817
+ );
818
+ await writeFile(join(plugin, 'dist', 'index.js'), inference('one'));
819
+ return { plugin, entry: join(plugin, 'dist', 'index.js'), lock: join(root, name, 'bun.lock') };
820
+ };
821
+ const first = await checkout('checkout-a');
822
+ const copy = await checkout('checkout-b');
823
+ const link = join(workspace, 'node_modules', 'linked-plugin');
824
+ const relink = async (target: string) => {
825
+ await unlink(link);
826
+ await symlink(target, link, 'dir');
827
+ };
828
+ await fixtureWorkspace(workspace, join(repositoryRoot, 'node_modules', 'nx'));
829
+ await symlink(first.plugin, link, 'dir');
830
+ await writeFile(join(workspace, 'nx.json'), JSON.stringify({ plugins: ['linked-plugin'] }));
831
+ const env = fixtureNxEnv(workspace, socketDirectory);
832
+ // Nothing in the workspace changes, so no watcher event reaches the daemon: each request judges the link.
833
+ const serves = async (daemon: Daemon, what: string) => {
834
+ expect(
835
+ await requestOnce(daemon.record.socketPath, ping(workspace), `a PING after ${what}`),
836
+ `${what} was refused`,
837
+ ).toEqual({ answered: true, message: true });
838
+ expect(await recordedPid(workspace), `${what} replaced the daemon`).toBe(daemon.record.processId);
839
+ };
840
+ const replaced = async (daemon: Daemon, what: string) => {
841
+ assertNotServed(
842
+ await requestOnce(daemon.record.socketPath, ping(workspace), `a PING sent after ${what}`),
843
+ `a PING sent after ${what}`,
844
+ );
845
+ await awaitExit([daemon.entry], `daemon ${daemon.record.processId} after ${what}`);
846
+ return readyDaemon(
847
+ workspace,
848
+ await replacementAfter({ workspace, previous: daemon.record.processId, what: `a successor after ${what}` }),
849
+ );
850
+ };
851
+ let daemon = await startDaemon(workspace, env);
852
+ await writeFile(first.entry, inference('one'));
853
+ await serves(daemon, 'a same-bytes rewrite of the linked plugin');
854
+ await relink(copy.plugin);
855
+ await serves(daemon, 'the link moving to a copy with the same bytes');
856
+ await writeFile(copy.entry, inference('two'));
857
+ daemon = await replaced(daemon, 'the linked plugin changing in place');
858
+ await relink(first.plugin);
859
+ daemon = await replaced(daemon, 'the link moving to a plugin with other bytes');
860
+ // The checkout's lockfile selects the plugin's dependencies, so its change restarts the daemon as well.
861
+ await writeFile(first.lock, 'checkout lock two\n');
862
+ await replaced(daemon, "the linked plugin checkout's lockfile changing");
863
+ },
864
+ FIXTURE_WORKSPACE,
865
+ );
866
+ });
867
+
803
868
  it('leaves a successor record and socket in place when another process takes the record', async () => {
804
869
  await withNxFixture(
805
870
  'nx-daemon-validity-successor-',
package/src/testing.ts CHANGED
@@ -138,9 +138,10 @@ export async function awaitExit(
138
138
  owner: string,
139
139
  timeoutMs = EXIT_TIMEOUT_MS,
140
140
  ): Promise<void> {
141
- const deadline = Date.now() + timeoutMs;
141
+ // The monotonic clock: a wall-clock step (NTP, sleep and wake) must not move the deadline.
142
+ const deadline = performance.now() + timeoutMs;
142
143
  for (let waiting = processes.filter((entry) => isRunning(entry.pid)); waiting.length > 0; ) {
143
- if (Date.now() > deadline) {
144
+ if (performance.now() > deadline) {
144
145
  // A signal probe also answers for a zombie, which has already exited.
145
146
  const live = new Set(
146
147
  (await processTable()).filter((entry) => !entry.stat.startsWith('Z')).map((entry) => entry.pid),
package/test-timings.json CHANGED
@@ -7,13 +7,13 @@
7
7
  "src/cargo-closure-key.test.ts": 22410,
8
8
  "src/ensure-built.test.ts": 18391,
9
9
  "src/graph-input-contract.test.ts": 16550,
10
+ "src/executors/bounded-exec/run-cpu-pool.test.ts": 15709,
10
11
  "src/executors/bounded-exec/executor.test.ts": 12975,
11
12
  "src/cargo-resolve-cache.test.ts": 11421,
12
13
  "src/cargo-closure-input.property.test.ts": 10902,
13
14
  "src/executors/bounded-exec/ram-temp.test.ts": 10766,
14
15
  "src/producer-receipts.test.ts": 10581,
15
16
  "src/typescript-resolution.test.ts": 10482,
16
- "src/executors/napi-cross-toolchain/executor.test.ts": 10474,
17
17
  "src/nx-daemon-claim.test.ts": 9477,
18
18
  "src/cargo-source-hash.test.ts": 9179,
19
19
  "src/nx-use-local.test.ts": 8333,
@@ -80,6 +80,7 @@
80
80
  "src/managed-files/managed-content.test.ts": 31,
81
81
  "src/executors/nextest-group/executor.test.ts": 28,
82
82
  "src/executors/bounded-exec/ram-temp-held.test.ts": 22,
83
+ "src/executors/cargo-zigbuild/executor.test.ts": 20,
83
84
  "src/build-state-output-policy.test.ts": 20,
84
85
  "src/managed-files/tree-permissions.test.ts": 20,
85
86
  "src/executors/bounded-exec/progress.test.ts": 18,
@@ -1,42 +0,0 @@
1
- import type { NapiCrossToolchainOptions } from './schema.js';
2
- interface NapiCrossToolchainContext {
3
- root: string;
4
- projectName?: string;
5
- projectsConfigurations?: {
6
- projects: Record<string, {
7
- root?: string;
8
- }>;
9
- };
10
- }
11
- export type PrewarmOutcome = 'ready' | 'extracted' | 'unsupported-host';
12
- export default function napiCrossToolchainExecutor(options: NapiCrossToolchainOptions, context: NapiCrossToolchainContext): Promise<{
13
- success: boolean;
14
- }>;
15
- /**
16
- * Extract the pinned cross toolchain into the directory
17
- * `napi build --use-napi-cross` probes, so its own download never runs.
18
- *
19
- * WHY this exists: `@napi-rs/cross-toolchain@1.0.3`'s `download()` uses its own
20
- * package directory as scratch space — `npm pack` there, unpack, then a
21
- * non-`force` `rmSync` of the tarball. Under `linker = "isolated"` with
22
- * `globalStore = true` that directory is the shared Bun store (in CI a
23
- * host-wide mutable bind mount), so two concurrent `--use-napi-cross` builds of
24
- * one triple pack the same filename into the same directory and the loser dies
25
- * with `ENOENT ... lstat '<tarball>'`. Nx runs `cli-x64-linux` and
26
- * `napi-x64-linux` in one wave, which is exactly that race.
27
- *
28
- * Extraction stages into a private sibling directory and lands with `rename`,
29
- * so racing callers — and repeated runs after a crash — cannot corrupt the
30
- * result: `rename` replaces an empty directory (napi itself creates one before
31
- * probing) and fails with ENOTEMPTY against a populated one, which means
32
- * another caller already won.
33
- */
34
- export declare function prewarmCrossToolchain(options: {
35
- projectRoot: string;
36
- triple: string;
37
- platform?: string;
38
- hostArch?: string;
39
- home?: string;
40
- }): PrewarmOutcome;
41
- export {};
42
- //# sourceMappingURL=executor.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"executor.d.ts","sourceRoot":"","sources":["../../../src/executors/napi-cross-toolchain/executor.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAC;AAE7D,UAAU,yBAAyB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sBAAsB,CAAC,EAAE;QACvB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE;YAAE,IAAI,CAAC,EAAE,MAAM,CAAA;SAAE,CAAC,CAAC;KAC7C,CAAC;CACH;AAaD,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,WAAW,GAAG,kBAAkB,CAAC;AAExE,wBAA8B,0BAA0B,CACtD,OAAO,EAAE,yBAAyB,EAClC,OAAO,EAAE,yBAAyB,GACjC,OAAO,CAAC;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC,CAe/B;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE;IAC7C,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,GAAG,cAAc,CAyEjB"}
@@ -1,126 +0,0 @@
1
- import { execFileSync } from 'node:child_process';
2
- import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync } from 'node:fs';
3
- import { createRequire } from 'node:module';
4
- import { homedir } from 'node:os';
5
- import path from 'node:path';
6
- /** Package-name arch segment of `@napi-rs/cross-toolchain-<host>-target-<arch>`. */
7
- const TARGET_PACKAGE_ARCH = {
8
- 'aarch64-unknown-linux-gnu': 'aarch64',
9
- 'x86_64-unknown-linux-gnu': 'x86_64',
10
- };
11
- const HOST_PACKAGE_ARCH = {
12
- arm64: 'arm64',
13
- x64: 'x64',
14
- };
15
- export default async function napiCrossToolchainExecutor(options, context) {
16
- const projectName = context.projectName;
17
- if (!projectName) {
18
- throw new Error('@smoothbricks/nx-plugin:napi-cross-toolchain requires a project context.');
19
- }
20
- const project = context.projectsConfigurations?.projects[projectName];
21
- if (!project) {
22
- throw new Error(`Project ${projectName} was not found in the Nx project graph.`);
23
- }
24
- const projectRoot = path.resolve(context.root, project.root ?? '.');
25
- const triple = options.triple;
26
- console.log(`${triple}: ${prewarmCrossToolchain({ projectRoot, triple })}`);
27
- return { success: true };
28
- }
29
- /**
30
- * Extract the pinned cross toolchain into the directory
31
- * `napi build --use-napi-cross` probes, so its own download never runs.
32
- *
33
- * WHY this exists: `@napi-rs/cross-toolchain@1.0.3`'s `download()` uses its own
34
- * package directory as scratch space — `npm pack` there, unpack, then a
35
- * non-`force` `rmSync` of the tarball. Under `linker = "isolated"` with
36
- * `globalStore = true` that directory is the shared Bun store (in CI a
37
- * host-wide mutable bind mount), so two concurrent `--use-napi-cross` builds of
38
- * one triple pack the same filename into the same directory and the loser dies
39
- * with `ENOENT ... lstat '<tarball>'`. Nx runs `cli-x64-linux` and
40
- * `napi-x64-linux` in one wave, which is exactly that race.
41
- *
42
- * Extraction stages into a private sibling directory and lands with `rename`,
43
- * so racing callers — and repeated runs after a crash — cannot corrupt the
44
- * result: `rename` replaces an empty directory (napi itself creates one before
45
- * probing) and fails with ENOTEMPTY against a populated one, which means
46
- * another caller already won.
47
- */
48
- export function prewarmCrossToolchain(options) {
49
- const { projectRoot, triple } = options;
50
- const targetArch = TARGET_PACKAGE_ARCH[triple];
51
- if (!targetArch) {
52
- throw new Error(`Unsupported --use-napi-cross triple ${triple}. Supported: ${Object.keys(TARGET_PACKAGE_ARCH).join(', ')}.`);
53
- }
54
- const hostArch = HOST_PACKAGE_ARCH[options.hostArch ?? process.arch];
55
- // napi-rs ships these toolchains for Linux x64 and Linux arm64 hosts only,
56
- // and refuses `--use-napi-cross` anywhere else. Extracting on a host that can
57
- // never use the result would only demand pins nothing needs, so leave the
58
- // unsupported-host diagnostic to the build itself.
59
- if (!hostArch || (options.platform ?? process.platform) !== 'linux') {
60
- return 'unsupported-host';
61
- }
62
- const requireFromProject = createRequire(path.join(projectRoot, 'package.json'));
63
- const targetPackage = `@napi-rs/cross-toolchain-${hostArch}-target-${targetArch}`;
64
- let targetModule;
65
- try {
66
- targetModule = requireFromProject(targetPackage);
67
- }
68
- catch (error) {
69
- throw new Error(`${targetPackage} is not installed for ${projectRoot}. Add it to optionalDependencies so ${triple} ` +
70
- 'builds extract the pinned toolchain instead of downloading into the shared Bun store.', { cause: error });
71
- }
72
- if (typeof targetModule !== 'object' ||
73
- targetModule === null ||
74
- !('toolchainPath' in targetModule) ||
75
- typeof targetModule.toolchainPath !== 'string') {
76
- throw new Error(`${targetPackage} no longer exports a toolchainPath string.`);
77
- }
78
- const tarball = targetModule.toolchainPath;
79
- const home = options.home ?? homedir();
80
- const destination = path.join(home, '.napi-rs', 'cross-toolchain', crossToolchainVersion(requireFromProject), triple);
81
- const marker = path.join(destination, 'package.json');
82
- if (existsSync(marker)) {
83
- return 'ready';
84
- }
85
- mkdirSync(path.dirname(destination), { recursive: true });
86
- const staging = mkdtempSync(`${destination}.staging-`);
87
- const command = `tar -xJf ${tarball} -C ${staging}`;
88
- try {
89
- execFileSync('tar', ['-xJf', tarball, '-C', staging], { stdio: 'inherit' });
90
- if (!existsSync(path.join(staging, 'package.json'))) {
91
- throw new Error(`${tarball} did not contain the package.json marker napi probes; the toolchain archive layout changed.`);
92
- }
93
- renameSync(staging, destination);
94
- }
95
- catch (error) {
96
- rmSync(staging, { force: true, recursive: true });
97
- const code = error instanceof Error && 'code' in error ? error.code : undefined;
98
- if ((code === 'ENOTEMPTY' || code === 'EEXIST') && existsSync(marker)) {
99
- return 'ready';
100
- }
101
- const status = error instanceof Error && 'status' in error ? error.status : undefined;
102
- if (status !== undefined) {
103
- if (error instanceof Error) {
104
- console.error(error.message);
105
- }
106
- throw new Error(`${command} failed with exit code ${typeof status === 'number' ? status : 1}`, { cause: error });
107
- }
108
- throw error;
109
- }
110
- return 'extracted';
111
- }
112
- /**
113
- * `napi` derives the extraction directory from `@napi-rs/cross-toolchain`'s own
114
- * version, so read it from the package the CLI resolves — not from the
115
- * per-target package, whose version only happens to match today.
116
- */
117
- function crossToolchainVersion(requireFromProject) {
118
- const cliRequire = createRequire(requireFromProject.resolve('@napi-rs/cli/package.json'));
119
- const indexPath = cliRequire.resolve('@napi-rs/cross-toolchain');
120
- const manifest = JSON.parse(readFileSync(path.join(path.dirname(indexPath), 'package.json'), 'utf-8'));
121
- const version = typeof manifest === 'object' && manifest !== null && 'version' in manifest ? manifest.version : undefined;
122
- if (typeof version !== 'string' || version.length === 0) {
123
- throw new Error(`@napi-rs/cross-toolchain resolved at ${indexPath} has no version.`);
124
- }
125
- return version;
126
- }
@@ -1,4 +0,0 @@
1
- export interface NapiCrossToolchainOptions {
2
- triple: string;
3
- }
4
- //# sourceMappingURL=schema.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/executors/napi-cross-toolchain/schema.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,yBAAyB;IACxC,MAAM,EAAE,MAAM,CAAC;CAChB"}