@smoothbricks/nx-plugin 0.7.12 → 0.7.13

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 (89) hide show
  1. package/README.md +78 -24
  2. package/dist/.build-inputs.json +20 -12
  3. package/dist/built-output-packages.d.ts +27 -0
  4. package/dist/built-output-packages.d.ts.map +1 -0
  5. package/dist/built-output-packages.js +97 -0
  6. package/dist/cross-check-policy.d.ts +13 -1
  7. package/dist/cross-check-policy.d.ts.map +1 -1
  8. package/dist/cross-check-policy.js +13 -1
  9. package/dist/executors/bounded-exec/admission-queue.d.ts +68 -0
  10. package/dist/executors/bounded-exec/admission-queue.d.ts.map +1 -0
  11. package/dist/executors/bounded-exec/admission-queue.js +131 -0
  12. package/dist/executors/bounded-exec/cpu-grant.d.ts +14 -0
  13. package/dist/executors/bounded-exec/cpu-grant.d.ts.map +1 -0
  14. package/dist/executors/bounded-exec/cpu-grant.js +18 -0
  15. package/dist/executors/bounded-exec/cpu-pool.d.ts +1 -14
  16. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  17. package/dist/executors/bounded-exec/cpu-pool.js +2 -49
  18. package/dist/executors/bounded-exec/executor.d.ts +5 -12
  19. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  20. package/dist/executors/bounded-exec/executor.js +51 -154
  21. package/dist/executors/bounded-exec/memory-retry.d.ts +39 -0
  22. package/dist/executors/bounded-exec/memory-retry.d.ts.map +1 -0
  23. package/dist/executors/bounded-exec/memory-retry.js +33 -0
  24. package/dist/executors/bounded-exec/parent-grant.d.ts +43 -0
  25. package/dist/executors/bounded-exec/parent-grant.d.ts.map +1 -0
  26. package/dist/executors/bounded-exec/parent-grant.js +37 -0
  27. package/dist/executors/bounded-exec/peak-charge.d.ts +17 -0
  28. package/dist/executors/bounded-exec/peak-charge.d.ts.map +1 -0
  29. package/dist/executors/bounded-exec/peak-charge.js +20 -0
  30. package/dist/executors/bounded-exec/pool-environment.d.ts +25 -0
  31. package/dist/executors/bounded-exec/pool-environment.d.ts.map +1 -0
  32. package/dist/executors/bounded-exec/pool-environment.js +34 -0
  33. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  34. package/dist/executors/bounded-exec/run-cpu-pool.js +1 -1
  35. package/dist/executors/bounded-exec/verdict.d.ts +3 -18
  36. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  37. package/dist/executors/bounded-exec/verdict.js +4 -25
  38. package/dist/index.d.ts +4 -1
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +62 -21
  41. package/dist/managed-files/ci-workflow.d.ts +29 -5
  42. package/dist/managed-files/ci-workflow.d.ts.map +1 -1
  43. package/dist/managed-files/ci-workflow.js +111 -16
  44. package/dist/managed-files/context.d.ts.map +1 -1
  45. package/dist/managed-files/context.js +1 -0
  46. package/dist/managed-files/files.d.ts +3 -1
  47. package/dist/managed-files/files.d.ts.map +1 -1
  48. package/dist/managed-files/files.js +2 -0
  49. package/dist/managed-files/publish-workflow.d.ts +6 -1
  50. package/dist/managed-files/publish-workflow.d.ts.map +1 -1
  51. package/dist/managed-files/publish-workflow.js +16 -11
  52. package/dist/owned-installation.d.ts.map +1 -1
  53. package/dist/owned-installation.js +48 -14
  54. package/dist/typecheck-route.d.ts +28 -0
  55. package/dist/typecheck-route.d.ts.map +1 -0
  56. package/dist/typecheck-route.js +109 -0
  57. package/dist/workspace-manifest.d.ts +50 -5
  58. package/dist/workspace-manifest.d.ts.map +1 -1
  59. package/executors.json +10 -0
  60. package/managed/raw/patches/ttsc@0.34.1.patch +93 -8
  61. package/managed/raw/tooling/direnv/apple-developer.sh +8 -1
  62. package/managed/raw/tooling/direnv/compiler-variables.sh +1 -1
  63. package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +4 -2
  64. package/managed/raw/tooling/direnv/secret-references.ts +19 -5
  65. package/managed/raw/tooling/git-hooks/pre-push.sh +20 -10
  66. package/managed/templates/github/actions/forgejo-artifact.cjs +9 -9
  67. package/managed/templates/github/actions/setup-devenv/action.yml +53 -0
  68. package/package.json +2 -2
  69. package/src/built-output-packages.ts +125 -0
  70. package/src/cross-check-policy.ts +14 -1
  71. package/src/executors/bounded-exec/admission-queue.ts +163 -0
  72. package/src/executors/bounded-exec/cpu-grant.ts +32 -0
  73. package/src/executors/bounded-exec/cpu-pool.ts +2 -33
  74. package/src/executors/bounded-exec/executor.ts +60 -201
  75. package/src/executors/bounded-exec/memory-retry.ts +63 -0
  76. package/src/executors/bounded-exec/parent-grant.ts +75 -0
  77. package/src/executors/bounded-exec/peak-charge.ts +25 -0
  78. package/src/executors/bounded-exec/pool-environment.ts +45 -0
  79. package/src/executors/bounded-exec/run-cpu-pool.ts +2 -1
  80. package/src/executors/bounded-exec/verdict.ts +7 -35
  81. package/src/index.ts +70 -21
  82. package/src/managed-files/ci-workflow.ts +143 -16
  83. package/src/managed-files/context.ts +1 -0
  84. package/src/managed-files/files.ts +5 -0
  85. package/src/managed-files/publish-workflow.ts +26 -10
  86. package/src/owned-installation.ts +63 -14
  87. package/src/typecheck-route.ts +64 -0
  88. package/src/workspace-manifest.ts +56 -5
  89. package/test-timings.json +141 -139
@@ -1,6 +1,8 @@
1
1
  import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
2
2
  import { join } from 'node:path';
3
3
  import typia from 'typia';
4
+ import type { CpuGrantRecord } from './cpu-grant.js';
5
+ import { parseCpuGrant } from './cpu-grant.js';
4
6
  import type { CpuWaitRow } from './lease-wait.js';
5
7
  import { describeHolders, type LockWait, type Opener } from './lock-wait.js';
6
8
  import type { MbxQueuedRequest } from './mbx-queue.js';
@@ -88,14 +90,10 @@ export interface BoundedExecRecord {
88
90
  /**
89
91
  * The parent CPU pool's grant the run started under: tokens granted of those asked for, and
90
92
  * how long it waited for the pool. Absent when it ran ungranted. Its tokens are the parallelism the run's
91
- * `peakBytes` was measured under, which the task's next run reads (`readLastPeak`) to charge that peak in proportion
93
+ * `peakBytes` was measured under, which the task's next run reads (`readLastPeak`, peak-charge.ts) to charge that peak in proportion
92
94
  * to the parallelism it is lent.
93
95
  */
94
- readonly cpu?: {
95
- readonly tokens: number;
96
- readonly want: number;
97
- readonly waitedMs: number;
98
- };
96
+ readonly cpu?: CpuGrantRecord;
99
97
  /**
100
98
  * The parent CPU pool's wait that expired and refused the run: its code, bound and elapsed time, the last heartbeat
101
99
  * sequence it read, and the pool's socket and pid (specs/cowshed/05_gateway.md, "Deadlines"). Absent when no wait
@@ -496,24 +494,6 @@ export async function readRecord(directory: string): Promise<BoundedExecRecord |
496
494
  return parseRecord(JSON.parse(text));
497
495
  }
498
496
 
499
- /**
500
- * The peak the task's last record in `directory` names: its charge in its run's pool. Null when the task has no record
501
- * or its record no peak; a record that cannot be read is an error naming it, never a charge of nothing.
502
- */
503
- export async function readLastPeak(
504
- directory: string,
505
- ): Promise<{ ok: true; peakBytes: number | null; parallelism: number | null } | { ok: false; error: string }> {
506
- try {
507
- const record = await readRecord(directory);
508
- return { ok: true, peakBytes: record?.peakBytes ?? null, parallelism: record?.cpu?.tokens ?? null };
509
- } catch (error) {
510
- return {
511
- ok: false,
512
- error: `the task's last record ${join(directory, VERDICT_FILE)} cannot be read: ${error instanceof Error ? error.message : String(error)}`,
513
- };
514
- }
515
- }
516
-
517
497
  /** The JUnit report a command wrote into `directory`, or `null` when it wrote none. */
518
498
  export async function readJunitReport(
519
499
  directory: string,
@@ -542,23 +522,15 @@ function parseRecord(value: unknown): BoundedExecRecord {
542
522
  if (!(peakBytes === null || (typeof peakBytes === 'number' && Number.isSafeInteger(peakBytes) && peakBytes >= 0))) {
543
523
  throw new Error(`not a bounded-exec record: ${JSON.stringify(value)}`);
544
524
  }
545
- const cpu = value.cpu;
546
- if (cpu === undefined) {
547
- return { task: value.task, hash: value.hash, verdict: parseVerdict(value.verdict), peakBytes };
548
- }
549
- if (
550
- !isObject(cpu) ||
551
- !(typeof cpu.tokens === 'number' && Number.isSafeInteger(cpu.tokens) && cpu.tokens >= 1) ||
552
- !(typeof cpu.want === 'number' && Number.isSafeInteger(cpu.want) && cpu.want >= 1) ||
553
- !(typeof cpu.waitedMs === 'number' && Number.isFinite(cpu.waitedMs) && cpu.waitedMs >= 0)
554
- ) {
525
+ const cpu = parseCpuGrant(value.cpu);
526
+ if (cpu === 'malformed') {
555
527
  throw new Error(`not a bounded-exec record: ${JSON.stringify(value)}`);
556
528
  }
557
529
  return {
558
530
  task: value.task,
559
531
  hash: value.hash,
560
532
  verdict: parseVerdict(value.verdict),
561
- cpu: { tokens: cpu.tokens, want: cpu.want, waitedMs: cpu.waitedMs },
533
+ ...(cpu === undefined ? {} : { cpu }),
562
534
  peakBytes,
563
535
  };
564
536
  }
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ import { AggregateCreateNodesError } from 'nx/src/project-graph/error-types.js';
25
25
  import { parse as parseToml } from 'smol-toml';
26
26
  import { BOUNDED_TEST_IDLE_TIMEOUT_MS, BOUNDED_TEST_KILL_AFTER_MS } from './bounded-test-policy.js';
27
27
  import { refuseOverlappingOutputs } from './build-state-output-policy.js';
28
+ import { builtOutputLockfileDependencies, builtOutputLockfileNodes } from './built-output-packages.js';
28
29
  import { bunLockDependencies, bunLockInput, bunLockNode } from './bun-lock-scope.js';
29
30
  import {
30
31
  CARGO_CLOSURE_INPUT,
@@ -104,6 +105,7 @@ import {
104
105
  } from './test-timings.js';
105
106
  import { assertUpstreamBasePrecondition } from './test-walk-policy.js';
106
107
  import { type NativeTools, nativeToolCommand, nativeToolDependsOn, resolveNativeTools } from './tool-bins.js';
108
+ import { typecheckRoute } from './typecheck-route.js';
107
109
  import {
108
110
  isBuildOutputTargetName,
109
111
  isTestNamespaceTargetName,
@@ -130,15 +132,14 @@ const TYPESCRIPT_PATCH_INPUTS = ['{workspaceRoot}/patches/**/*', '!{workspaceRoo
130
132
  const WORKSPACE_TSCONFIG_BASE_INPUT = '{workspaceRoot}/tsconfig.base.json';
131
133
 
132
134
  /**
133
- * The native compiler a typecheck runs, apart from the one emit runs: devenv.smoo.nix builds it
134
- * from a pinned source revision and exports its path. The typecheck targets run it directly,
135
- * not through ttsc: ttsc auto-loads a dependency's transform plugin (typia) and then checks
136
- * inside that plugin's host, which links ttsc's own compiler and never runs the pinned one.
137
- * Transform diagnostics stay enforced where the transform runs: for the lib program in `tsc-js`'s
138
- * emit, for test files when the test runner transforms them. A test file no test executes has
139
- * its types checked here but its transform calls checked nowhere. Hashed by the store path,
140
- * which carries the revision, so a compiler bump misses the cache. The command refuses an unset
141
- * variable: falling back would check with another compiler under the same key.
135
+ * The native compiler a typecheck runs unless a dependency declares a dialect (typecheck-route.ts), apart from the one
136
+ * emit runs: devenv.smoo.nix builds it from a pinned source revision and exports its path. The typecheck targets run it
137
+ * directly, not through ttsc: ttsc auto-loads a dependency's transform plugin (typia) and then checks inside that
138
+ * plugin's host, which links ttsc's own compiler and never runs the pinned one. Transform diagnostics stay enforced
139
+ * where the transform runs: for the lib program in `tsc-js`'s emit, for test files when the test runner transforms
140
+ * them. A test file no test executes has its types checked here but its transform calls checked nowhere. Hashed by the
141
+ * store path, which carries the revision, so a compiler bump misses the cache. The command refuses an unset variable:
142
+ * falling back would check with another compiler under the same key.
142
143
  */
143
144
  const TYPECHECK_COMPILER_ENV = 'SMOO_TYPECHECK_TSC';
144
145
  const TYPECHECK_COMPILER_INPUT = { env: TYPECHECK_COMPILER_ENV };
@@ -146,6 +147,29 @@ function typecheckCommand(args: string): string {
146
147
  return `"\${${TYPECHECK_COMPILER_ENV}:?is unset, enter the devenv shell (devenv.smoo.nix builds the typecheck compiler)}" ${args}`;
147
148
  }
148
149
 
150
+ /** How one project's typecheck targets run: their command, the compiler input they hash, and what they wait for. */
151
+ interface TypecheckTargets {
152
+ readonly command: (args: string) => string;
153
+ readonly inputs: readonly NonNullable<TargetConfiguration['inputs']>[number][];
154
+ readonly dependsOn: readonly string[];
155
+ }
156
+
157
+ /**
158
+ * A project's typecheck targets. A project whose direct dependency declares a dialect typechecks through ttsc, whose
159
+ * linked plugin lowers the dialect and withdraws the checker's refusals of it; its compiler is the installed ttsc,
160
+ * which bun.lock and the patches in its check inputs already key, and it waits for the targets the declaring package
161
+ * names to prepare the plugin binaries ttsc loads. Every other project runs the pinned native compiler.
162
+ */
163
+ function typecheckTargets(packageJsonPath: string, packageJson: PackageJson): TypecheckTargets {
164
+ const route = typecheckRoute(packageJsonPath, [
165
+ ...Object.keys(packageJson.dependencies ?? {}),
166
+ ...Object.keys(packageJson.devDependencies ?? {}),
167
+ ]);
168
+ return route.declaredBy.length === 0
169
+ ? { command: typecheckCommand, inputs: [TYPECHECK_COMPILER_INPUT], dependsOn: [] }
170
+ : { command: (args) => `ttsc ${args}`, inputs: [], dependsOn: route.dependsOn };
171
+ }
172
+
149
173
  /**
150
174
  * The release-rewritten root manifest hashed by field instead of by byte, for
151
175
  * a check that only reads it.
@@ -1131,12 +1155,13 @@ function supportsJsonInputs(): boolean {
1131
1155
  type CreateNodesHandler = CreateNodesV2[1];
1132
1156
 
1133
1157
  function createNodesHandler(hostPlatform: NapiPlatform | null): CreateNodesHandler {
1134
- return async (projectConfigurationFiles, _options, context) => {
1158
+ return async (projectConfigurationFiles, options, context) => {
1135
1159
  const results: CreateNodesResultV2 = [];
1136
1160
  const errors: Array<[file: string | null, error: Error]> = [];
1137
1161
  let cargoWorkspaces: CargoWorkspace[];
1138
1162
  let eslint: EslintWorkspace | null;
1139
1163
  let nativeTools: NativeTools | null;
1164
+ let lockfileNodes: Record<string, ProjectGraphExternalNode>;
1140
1165
  try {
1141
1166
  cargoWorkspaces = await resolveCargoWorkspaces(projectConfigurationFiles, context.workspaceRoot, hostPlatform);
1142
1167
  eslint = await eslintWorkspace(context.workspaceRoot);
@@ -1149,6 +1174,7 @@ function createNodesHandler(hostPlatform: NapiPlatform | null): CreateNodesHandl
1149
1174
  EXECUTABLE_PLATFORM_DIRECTORY[hostPlatform.targetFamily][hostPlatform.architecture],
1150
1175
  scriptcTargetName(hostPlatform),
1151
1176
  );
1177
+ lockfileNodes = builtOutputLockfileNodes(options, context);
1152
1178
  } catch (error) {
1153
1179
  throw new AggregateCreateNodesError([[null, error instanceof Error ? error : new Error(String(error))]], results);
1154
1180
  }
@@ -1185,6 +1211,20 @@ function createNodesHandler(hostPlatform: NapiPlatform | null): CreateNodesHandl
1185
1211
  }),
1186
1212
  );
1187
1213
 
1214
+ // The root manifest's result carries the lockfile's package nodes, as it carries the root's lock-scope node.
1215
+ if (Object.keys(lockfileNodes).length > 0) {
1216
+ const rootIndex = results.findIndex(([file]) => file === 'package.json');
1217
+ const rootResult = results[rootIndex];
1218
+ if (rootResult === undefined) {
1219
+ errors.push([
1220
+ null,
1221
+ new Error('builtOutputPackages needs a root package.json to publish the lockfile nodes on'),
1222
+ ]);
1223
+ } else {
1224
+ const [file, result] = rootResult;
1225
+ results[rootIndex] = [file, { ...result, externalNodes: { ...lockfileNodes, ...result.externalNodes } }];
1226
+ }
1227
+ }
1188
1228
  if (errors.length > 0) {
1189
1229
  throw new AggregateCreateNodesError(errors, results);
1190
1230
  }
@@ -1241,8 +1281,14 @@ export const postTasksExecution: PostTasksExecution<ProducerReceiptsOptions> = a
1241
1281
  await postCargoRetention(options, context);
1242
1282
  };
1243
1283
 
1244
- /** The lock-scope nodes' edges: each bun node's to the packages and members its manifest resolves. */
1245
- export const createDependencies: CreateDependencies = (_options, context) => bunLockDependencies(context);
1284
+ /**
1285
+ * The lock-scope nodes' edges: each bun node's to the packages and members its manifest resolves; and, for a workspace
1286
+ * that names built-output packages, the lockfile's package edges (`built-output-packages.ts`).
1287
+ */
1288
+ export const createDependencies: CreateDependencies = (options, context) => [
1289
+ ...bunLockDependencies(context),
1290
+ ...builtOutputLockfileDependencies(options, context),
1291
+ ];
1246
1292
 
1247
1293
  /** Nx invokes this only after merging inferred/declarative targets and every plugin's project dependencies. */
1248
1294
  export const createMetadata: CreateMetadata = (graph, _options, context) => {
@@ -1255,6 +1301,7 @@ export default { createNodesV2, createDependencies, createMetadata, postTasksExe
1255
1301
 
1256
1302
  interface PackageJson {
1257
1303
  bin?: unknown;
1304
+ dependencies?: Record<string, unknown>;
1258
1305
  devDependencies?: Record<string, unknown>;
1259
1306
  name?: string;
1260
1307
  napi?: Record<string, unknown>;
@@ -1559,6 +1606,7 @@ async function createProjectTargets(
1559
1606
  const projectRoot = dirname(packageJsonPath);
1560
1607
  const absoluteProjectRoot = join(workspaceRoot, projectRoot);
1561
1608
  const packageJson = await readPackageJson(join(workspaceRoot, packageJsonPath));
1609
+ const typecheck = typecheckTargets(join(workspaceRoot, packageJsonPath), packageJson);
1562
1610
  const projectName = packageJson.nx?.name ?? packageJson.name;
1563
1611
  if (typeof projectName !== 'string' || projectName.length === 0) {
1564
1612
  throw new Error(`${packageJsonPath} must declare a non-empty package or nx project name`);
@@ -1669,13 +1717,13 @@ async function createProjectTargets(
1669
1717
  selfProduction,
1670
1718
  dependencyProduction,
1671
1719
  ...checkInputs,
1672
- TYPECHECK_COMPILER_INPUT,
1720
+ ...typecheck.inputs,
1673
1721
  '{projectRoot}/tsconfig.lib.json',
1674
1722
  ],
1675
1723
  outputs: [],
1676
- dependsOn: ['^*-js', ...(cargoWasmConfig ? ['cargo-wasm'] : [])],
1724
+ dependsOn: ['^*-js', ...(cargoWasmConfig ? ['cargo-wasm'] : []), ...typecheck.dependsOn],
1677
1725
  options: {
1678
- command: typecheckCommand('-p tsconfig.lib.json --noEmit'),
1726
+ command: typecheck.command('-p tsconfig.lib.json --noEmit'),
1679
1727
  cwd: projectRoot,
1680
1728
  },
1681
1729
  };
@@ -1693,12 +1741,12 @@ async function createProjectTargets(
1693
1741
  selfDefault,
1694
1742
  dependencyProduction,
1695
1743
  ...checkInputs,
1696
- TYPECHECK_COMPILER_INPUT,
1744
+ ...typecheck.inputs,
1697
1745
  '{projectRoot}/tsconfig.test.json',
1698
1746
  ],
1699
- dependsOn: ['typecheck'],
1747
+ dependsOn: ['typecheck', ...typecheck.dependsOn],
1700
1748
  options: {
1701
- command: typecheckCommand('-p tsconfig.test.json --noEmit'),
1749
+ command: typecheck.command('-p tsconfig.test.json --noEmit'),
1702
1750
  cwd: projectRoot,
1703
1751
  },
1704
1752
  };
@@ -1706,7 +1754,7 @@ async function createProjectTargets(
1706
1754
  executor: 'nx:run-commands',
1707
1755
  continuous: true,
1708
1756
  options: {
1709
- command: typecheckCommand('-p tsconfig.test.json --noEmit --watch'),
1757
+ command: typecheck.command('-p tsconfig.test.json --noEmit --watch'),
1710
1758
  cwd: projectRoot,
1711
1759
  },
1712
1760
  };
@@ -2296,9 +2344,9 @@ async function createProjectTargets(
2296
2344
  // Test configs may resolve the package's own `./index.js` through dist, so the
2297
2345
  // current-project declaration output is rebuilt first, not only the
2298
2346
  // dependency-project outputs reached through typecheck.
2299
- targets['typecheck-tests'].dependsOn = ['tsc-js', 'typecheck'];
2347
+ targets['typecheck-tests'].dependsOn = ['tsc-js', 'typecheck', ...typecheck.dependsOn];
2300
2348
  } else if (targets.build) {
2301
- targets['typecheck-tests'].dependsOn = ['build'];
2349
+ targets['typecheck-tests'].dependsOn = ['build', ...typecheck.dependsOn];
2302
2350
  }
2303
2351
  }
2304
2352
 
@@ -2913,6 +2961,7 @@ async function readPackageJson(packageJsonPath: string): Promise<PackageJson> {
2913
2961
  const rawNx = isRecord(parsed.nx) ? parsed.nx : undefined;
2914
2962
  return {
2915
2963
  ...('bin' in parsed ? { bin: parsed.bin } : {}),
2964
+ ...(isRecord(parsed.dependencies) ? { dependencies: parsed.dependencies } : {}),
2916
2965
  ...(isRecord(parsed.devDependencies) ? { devDependencies: parsed.devDependencies } : {}),
2917
2966
  ...(typeof parsed.name === 'string' ? { name: parsed.name } : {}),
2918
2967
  ...(isRecord(parsed.napi) ? { napi: parsed.napi } : {}),
@@ -9,14 +9,17 @@ import {
9
9
  } from '../cross-check-policy.js';
10
10
  import { PRODUCTION_PUSH_DEPLOY_TAG } from '../deploy-policy.js';
11
11
  import type {
12
+ LiteralCacheOrigin,
12
13
  NonEmptyArray,
13
14
  PackageCargoCredentialsConfig,
14
15
  PackageCargoGitOrigin,
16
+ PackageMbxRemoteConfig,
15
17
  PackagePrivateNpmConfig,
16
18
  PackageRemoteCacheConfig,
17
19
  PackageSmooGithub,
18
20
  PackageSmooGithubEnvironments,
19
21
  PackageSourceCheckoutConfig,
22
+ VariableCacheOrigin,
20
23
  } from '../workspace-manifest.js';
21
24
  import { renderRunsOnLine } from './github-runs-on.js';
22
25
 
@@ -146,6 +149,11 @@ export interface CiWorkflowDefinitionOptions extends DeployStepSecretConfig {
146
149
  * cache serves the whole fleet. Absent means each runner caches locally.
147
150
  */
148
151
  remoteCache?: PackageRemoteCacheConfig;
152
+ /**
153
+ * Declared mbx remote cache (package.json `smoo.mbxRemote`). Every job carries mbx's remote settings, which is
154
+ * what makes setup-devenv route Cargo through mbx. Absent means Cargo builds without mbx.
155
+ */
156
+ mbxRemote?: PackageMbxRemoteConfig;
149
157
  /** GitHub Environments: staging for the validate/e2e jobs, production for the production-on-push job. */
150
158
  environments?: PackageSmooGithubEnvironments;
151
159
  /** Secrets for the e2e-deployment step, env var name → repository secret name. */
@@ -316,7 +324,10 @@ jobs:
316
324
  ${renderRunsOnLine(options.runsOn)}
317
325
  timeout-minutes: 45
318
326
  ${
319
- options.privateNpm?.readTokenEnv || options.cargoCredentials !== undefined || options.remoteCache !== undefined
327
+ options.privateNpm?.readTokenEnv ||
328
+ options.cargoCredentials !== undefined ||
329
+ options.remoteCache !== undefined ||
330
+ options.mbxRemote !== undefined
320
331
  ? ` # Fork PRs receive no secrets, so neither a private dependency install nor an
321
332
  # authenticated remote cache read can run there.
322
333
  if: \${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}
@@ -330,7 +341,7 @@ ${
330
341
  : ''
331
342
  } env:
332
343
  GH_TOKEN: ${githubExpression('github.token')}
333
- ${remoteCacheJobEnvLines(options.remoteCache)}${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)} steps:
344
+ ${remoteCacheJobEnvLines(options.remoteCache, 'internal')}${mbxRemoteJobEnvLines(options.mbxRemote, 'internal')}${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)} steps:
334
345
  `;
335
346
  }
336
347
 
@@ -679,30 +690,58 @@ export function normalizeSourceCheckout(config: PackageSourceCheckoutConfig): Pa
679
690
  * fail every task on 401, which is why the job carries the same fork-PR gate
680
691
  * as a private dependency install (see `renderCiWorkflowHeader`).
681
692
  *
682
- * A declared `internalServer` is the address managed runners use, exactly as a
683
- * git origin's `internalMirror` is: both say this repository's runners sit
684
- * inside that network, and shells outside it keep the public origin.
693
+ * A declared `internalServer` is the address the repository's own runners use, exactly as a git origin's
694
+ * `internalMirror` is: both say those runners sit inside that network. A job on any other runner — a GitHub-hosted
695
+ * one, a macOS label — sits outside it and reaches the public `server`, as a developer shell does. Each job names
696
+ * its reach, so no job is pointed at an address its runner cannot route to.
685
697
  * Mechanism errors surface at managed-file render time, never inside CI.
686
698
  */
687
- export function remoteCacheJobEnvLines(config: PackageRemoteCacheConfig | undefined): string {
699
+ export function remoteCacheJobEnvLines(config: PackageRemoteCacheConfig | undefined, reach: CacheReach): string {
688
700
  if (config === undefined) {
689
701
  return '';
690
702
  }
691
703
  const normalized = normalizeRemoteCache(config);
704
+ const server =
705
+ reach === 'internal' ? (normalized.internalServer ?? publicCacheOrigin(normalized)) : publicCacheOrigin(normalized);
692
706
  return [
693
- ` NX_SELF_HOSTED_REMOTE_CACHE_SERVER: ${normalized.internalServer ?? normalized.server}\n`,
707
+ ` NX_SELF_HOSTED_REMOTE_CACHE_SERVER: ${server}\n`,
694
708
  ` NX_SELF_HOSTED_REMOTE_CACHE_ACCESS_TOKEN: ${githubExpression(`secrets.${normalized.tokenSecret}`)}\n`,
695
709
  ].join('');
696
710
  }
697
711
 
712
+ /**
713
+ * Which origin of a declared remote cache a job reaches: `internal` for a job on the repository's own runners
714
+ * (`smoo.github.runsOn`), inside the network `internalServer` names; `public` for any other runner, which reaches
715
+ * only `server`.
716
+ */
717
+ export type CacheReach = 'internal' | 'public';
718
+
719
+ /**
720
+ * The public origin as a job env value: the declared origin, or the expression that reads the repository variable
721
+ * naming it.
722
+ */
723
+ function publicCacheOrigin(config: LiteralCacheOrigin | VariableCacheOrigin): string {
724
+ return 'serverVariable' in config ? githubExpression(`vars.${config.serverVariable}`) : config.server;
725
+ }
726
+
698
727
  /** Mechanism errors surface at managed-file render time, never inside CI. */
699
728
  export function normalizeRemoteCache(config: PackageRemoteCacheConfig): PackageRemoteCacheConfig {
700
- assertCacheOrigin('server', config.server);
729
+ const literal = 'server' in config ? config.server : undefined;
730
+ if ('server' in config && 'serverVariable' in config) {
731
+ throw new Error('smoo.remoteCache names its public origin twice; declare server or serverVariable, not both');
732
+ }
733
+ if (literal !== undefined) {
734
+ assertCacheOrigin('smoo.remoteCache', 'server', literal);
735
+ } else if (!('serverVariable' in config) || !/^[A-Z_][A-Z0-9_]*$/.test(config.serverVariable)) {
736
+ throw new Error(
737
+ `smoo.remoteCache needs server, or an upper-case repository variable name for serverVariable, got ${JSON.stringify(config)}`,
738
+ );
739
+ }
701
740
  if (config.internalServer !== undefined) {
702
- assertCacheOrigin('internalServer', config.internalServer);
703
- if (config.internalServer === config.server) {
741
+ assertCacheOrigin('smoo.remoteCache', 'internalServer', config.internalServer);
742
+ if (config.internalServer === literal) {
704
743
  throw new Error(
705
- `smoo.remoteCache internalServer repeats server ${config.server}; drop internalServer or point it at the address internal runners reach`,
744
+ `smoo.remoteCache internalServer repeats server ${literal}; drop internalServer or point it at the address internal runners reach`,
706
745
  );
707
746
  }
708
747
  }
@@ -720,7 +759,7 @@ export function normalizeRemoteCache(config: PackageRemoteCacheConfig): PackageR
720
759
  * slash the server routes nowhere, and a path or credential would be dropped
721
760
  * or leaked rather than honored.
722
761
  */
723
- function assertCacheOrigin(field: 'server' | 'internalServer', value: string): void {
762
+ function assertCacheOrigin(declaration: string, field: 'server' | 'internalServer', value: string): void {
724
763
  let url: URL | null = null;
725
764
  try {
726
765
  url = new URL(value);
@@ -738,7 +777,95 @@ function assertCacheOrigin(field: 'server' | 'internalServer', value: string): v
738
777
  url.hash !== ''
739
778
  ) {
740
779
  throw new Error(
741
- `smoo.remoteCache ${field} needs a credential-free http(s) origin with no path and no trailing slash, got ${JSON.stringify(value)}`,
780
+ `${declaration} ${field} needs a credential-free http(s) origin with no path and no trailing slash, got ${JSON.stringify(value)}`,
781
+ );
782
+ }
783
+ }
784
+
785
+ /**
786
+ * The default branch's own runs: a push to it, or a workflow dispatched on it (a release). These are the only lanes
787
+ * given the mbx write token. A pull request's ref is never the default branch, and `pull_request_target` is left out
788
+ * because its ref is the base branch while the code it runs is not.
789
+ */
790
+ const DEFAULT_BRANCH_LANE =
791
+ "(github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)";
792
+
793
+ /**
794
+ * Job env lines carrying the declared mbx remote cache (`smoo.mbxRemote`). setup-devenv routes Cargo through mbx's
795
+ * Cargo shim when `MBX_REMOTE_URL` is set, so these lines are what turn mbx on for the job; `reach` picks the origin
796
+ * exactly as for the Nx cache. The token is chosen by lane: the write token and `MBX_REMOTE_TRUST_WRITES` on the
797
+ * default branch's own runs, the read-only token everywhere else. mbx's own write policy cannot make that call here:
798
+ * it requires `GITHUB_REF_PROTECTED`, which Forgejo Actions never sets, and it reads a release's `workflow_dispatch`
799
+ * as untrusted. The server's grant on the token is what stops any other job publishing.
800
+ * Mechanism errors surface at managed-file render time, never inside CI.
801
+ */
802
+ export function mbxRemoteJobEnvLines(config: PackageMbxRemoteConfig | undefined, reach: CacheReach): string {
803
+ if (config === undefined) {
804
+ return '';
805
+ }
806
+ const normalized = normalizeMbxRemote(config);
807
+ const server =
808
+ reach === 'internal' ? (normalized.internalServer ?? publicCacheOrigin(normalized)) : publicCacheOrigin(normalized);
809
+ const token = `${DEFAULT_BRANCH_LANE} && secrets.${normalized.writeTokenSecret} || secrets.${normalized.readTokenSecret}`;
810
+ return [
811
+ ` MBX_REMOTE_URL: ${server}\n`,
812
+ ` MBX_REMOTE_NAMESPACE: ${normalized.namespace}\n`,
813
+ // One expression per line: a folded scalar would split the lane condition, and Prettier refolds it otherwise.
814
+ ' # prettier-ignore\n',
815
+ ` MBX_REMOTE_TOKEN: ${githubExpression(token)}\n`,
816
+ ' # prettier-ignore\n',
817
+ ` MBX_REMOTE_TRUST_WRITES: ${githubExpression(DEFAULT_BRANCH_LANE)}\n`,
818
+ ].join('');
819
+ }
820
+
821
+ /** Mechanism errors surface at managed-file render time, never inside CI. */
822
+ export function normalizeMbxRemote(config: PackageMbxRemoteConfig): PackageMbxRemoteConfig {
823
+ if ('server' in config && 'serverVariable' in config) {
824
+ throw new Error('smoo.mbxRemote names its public origin twice; declare server or serverVariable, not both');
825
+ }
826
+ if ('server' in config) {
827
+ assertMbxOrigin('server', config.server);
828
+ } else if (!/^[A-Z_][A-Z0-9_]*$/.test(config.serverVariable)) {
829
+ throw new Error(
830
+ `smoo.mbxRemote needs server, or an upper-case repository variable name for serverVariable, got ${JSON.stringify(config)}`,
831
+ );
832
+ }
833
+ if (config.internalServer !== undefined) {
834
+ assertMbxOrigin('internalServer', config.internalServer);
835
+ }
836
+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]*(\/[A-Za-z0-9][A-Za-z0-9._-]*)*$/.test(config.namespace)) {
837
+ throw new Error(
838
+ `smoo.mbxRemote namespace must be slash-separated segments of letters, digits, '.', '_' and '-', got ${JSON.stringify(config.namespace)}`,
839
+ );
840
+ }
841
+ for (const [field, name] of [
842
+ ['writeTokenSecret', config.writeTokenSecret],
843
+ ['readTokenSecret', config.readTokenSecret],
844
+ ] as const) {
845
+ if (!/^[A-Z_][A-Z0-9_]*$/.test(name)) {
846
+ throw new Error(`smoo.mbxRemote needs an upper-case secret name for ${field}, got ${JSON.stringify(name)}`);
847
+ }
848
+ }
849
+ if (config.writeTokenSecret === config.readTokenSecret) {
850
+ throw new Error(
851
+ `smoo.mbxRemote writeTokenSecret and readTokenSecret both name ${config.writeTokenSecret}; untrusted jobs would hold the write token`,
852
+ );
853
+ }
854
+ return config;
855
+ }
856
+
857
+ /**
858
+ * An mbx origin is a cache origin mbx will send its token to: HTTPS, or plain HTTP to a loopback address only, which
859
+ * is mbx's own rule (it refuses the remote otherwise), checked here so a bad declaration fails at render time.
860
+ */
861
+ function assertMbxOrigin(field: 'server' | 'internalServer', value: string): void {
862
+ assertCacheOrigin('smoo.mbxRemote', field, value);
863
+ const url = new URL(value);
864
+ const loopback =
865
+ url.hostname === 'localhost' || url.hostname === '[::1]' || /^127\.\d+\.\d+\.\d+$/.test(url.hostname);
866
+ if (url.protocol === 'http:' && !loopback) {
867
+ throw new Error(
868
+ `smoo.mbxRemote ${field} ${JSON.stringify(value)} is plain HTTP to a non-loopback host; mbx sends its token only over HTTPS or to loopback`,
742
869
  );
743
870
  }
744
871
  }
@@ -1268,7 +1395,7 @@ ${options.macosRunsOn === undefined ? ' runs-on: macos-latest' : renderRunsOn
1268
1395
  if: \${{ needs.main.result == 'success' }}
1269
1396
  env:
1270
1397
  GH_TOKEN: \${{ github.token }}
1271
- ${remoteCacheJobEnvLines(options.remoteCache)}${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)} steps:
1398
+ ${remoteCacheJobEnvLines(options.remoteCache, 'public')}${mbxRemoteJobEnvLines(options.mbxRemote, 'public')}${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)} steps:
1272
1399
  ${renderCiWorkflowSteps(followUpSetupSteps(options, numbers), options)}
1273
1400
  ${renderCiWorkflowSteps(middleSteps, options)}
1274
1401
  ${renderCiWorkflowSteps(followUpCleanupStep(numbers), options)}`;
@@ -1366,7 +1493,7 @@ ${renderRunsOnLine(options.runsOn)}
1366
1493
  ${environmentLine(options.environments?.staging)} if: \${{ needs.main.result == 'success' && needs.main.outputs.deployment-stage != '' }}
1367
1494
  env:
1368
1495
  GH_TOKEN: \${{ github.token }}
1369
- ${remoteCacheJobEnvLines(options.remoteCache)}${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)} steps:
1496
+ ${remoteCacheJobEnvLines(options.remoteCache, 'internal')}${mbxRemoteJobEnvLines(options.mbxRemote, 'internal')}${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)} steps:
1370
1497
  ${renderCiWorkflowSteps(followUpSetupSteps(options, numbers), options)}
1371
1498
  # Step ${numbers.middle}
1372
1499
  - name: E2E Tests (Deployed Stage)
@@ -1398,7 +1525,7 @@ ${renderRunsOnLine(options.runsOn)}
1398
1525
  if: \${{ !cancelled() && github.event_name == 'push' && github.ref == ${stagingRefLiteral(options)} && needs.main.result == 'success'${e2eGate} }}
1399
1526
  ${environmentLine(options.environments?.production)} env:
1400
1527
  GH_TOKEN: \${{ github.token }}
1401
- ${remoteCacheJobEnvLines(options.remoteCache)}${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)} steps:
1528
+ ${remoteCacheJobEnvLines(options.remoteCache, 'internal')}${mbxRemoteJobEnvLines(options.mbxRemote, 'internal')}${cargoCredentialJobEnvLines(options.cargoCredentials)}${privateNpmReadTokenJobEnv(options)} steps:
1402
1529
  ${renderCiWorkflowSteps(followUpSetupSteps(options, numbers), options)}
1403
1530
  # Step ${numbers.middle}
1404
1531
  - name: 🚀 Deploy Production
@@ -77,6 +77,7 @@ export function deriveManagedFileContext(
77
77
  sourceCheckouts,
78
78
  cargoCredentials,
79
79
  remoteCache: manifest?.smoo?.remoteCache,
80
+ mbxRemote: manifest?.smoo?.mbxRemote,
80
81
  macosPlatformArchitectures: MACOS_PLATFORM_TARGET_GLOBS.some((glob) => releasePlatformTargetGlobs.includes(glob))
81
82
  ? macosPlatformArchitecturesForTest(targetNames)
82
83
  : [],
@@ -4,6 +4,7 @@ import { managedAssetsRoot } from '../managed-assets.js';
4
4
  import type {
5
5
  NonEmptyArray,
6
6
  PackageCargoCredentialsConfig,
7
+ PackageMbxRemoteConfig,
7
8
  PackagePrivateNpmConfig,
8
9
  PackageRemoteCacheConfig,
9
10
  PackageSmooGithub,
@@ -59,6 +60,8 @@ export interface ManagedFileContext {
59
60
  cargoCredentials?: PackageCargoCredentialsConfig;
60
61
  /** Declared self-hosted Nx remote cache from the root smoo config; absent means local caching only. */
61
62
  remoteCache?: PackageRemoteCacheConfig;
63
+ /** Declared mbx remote cache from the root smoo config; absent means Cargo builds without mbx. */
64
+ mbxRemote?: PackageMbxRemoteConfig;
62
65
  /**
63
66
  * Cross-built test archives the graph can produce, one per foreign target
64
67
  * triple the cargo workspace declares. Empty for a repository that declares
@@ -380,6 +383,7 @@ function getManagedContent(file: ManagedFileDescriptor, context: ManagedFileCont
380
383
  sourceCheckouts: context.sourceCheckouts,
381
384
  cargoCredentials: context.cargoCredentials,
382
385
  remoteCache: context.remoteCache,
386
+ mbxRemote: context.mbxRemote,
383
387
  environments: context.ciEnvironments,
384
388
  deploySecrets: context.ciDeploySecrets,
385
389
  e2eSecrets: context.ciE2eSecrets,
@@ -407,6 +411,7 @@ function getManagedContent(file: ManagedFileDescriptor, context: ManagedFileCont
407
411
  sourceCheckouts: context.sourceCheckouts,
408
412
  cargoCredentials: context.cargoCredentials,
409
413
  remoteCache: context.remoteCache,
414
+ mbxRemote: context.mbxRemote,
410
415
  deploySecrets: context.ciDeploySecrets,
411
416
  });
412
417
  }