@smoothbricks/nx-plugin 0.4.31 → 0.5.1

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 (74) hide show
  1. package/README.md +67 -46
  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/ensure-built.d.ts.map +1 -1
  8. package/dist/ensure-built.js +14 -9
  9. package/dist/executors/bounded-exec/cpu-pool.d.ts +14 -7
  10. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  11. package/dist/executors/bounded-exec/cpu-pool.js +297 -38
  12. package/dist/executors/bounded-exec/cpu-tokens.d.ts +19 -7
  13. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
  14. package/dist/executors/bounded-exec/cpu-tokens.js +21 -7
  15. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  16. package/dist/executors/bounded-exec/executor.js +12 -6
  17. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  18. package/dist/executors/bounded-exec/run-cpu-pool.js +5 -4
  19. package/dist/executors/cargo-zigbuild/executor.d.ts +60 -0
  20. package/dist/executors/cargo-zigbuild/executor.d.ts.map +1 -0
  21. package/dist/executors/cargo-zigbuild/executor.js +266 -0
  22. package/dist/executors/cargo-zigbuild/schema.d.ts +15 -0
  23. package/dist/executors/cargo-zigbuild/schema.d.ts.map +1 -0
  24. package/dist/index.d.ts +0 -15
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +56 -89
  27. package/dist/producer-receipts.d.ts +10 -0
  28. package/dist/producer-receipts.d.ts.map +1 -1
  29. package/dist/producer-receipts.js +30 -5
  30. package/dist/testing.d.ts.map +1 -1
  31. package/dist/testing.js +3 -2
  32. package/executors.json +4 -4
  33. package/managed/raw/patches/nx@23.2.1.patch +177 -9
  34. package/managed/raw/tooling/direnv/devenv.smoo.nix +47 -49
  35. package/managed/raw/tooling/direnv/linked-snapshots.ts +18 -8
  36. package/managed/raw/tooling/direnv/repo-path +1 -1
  37. package/managed/raw/tooling/git-hooks/pre-push.sh +41 -23
  38. package/package.json +1 -1
  39. package/src/__tests__/counted-cargo.ts +1 -1
  40. package/src/cargo-closure-input.test.ts +1 -1
  41. package/src/cargo-toolchain-policy.ts +6 -6
  42. package/src/cross-check-policy.test.ts +90 -11
  43. package/src/cross-check-policy.ts +47 -17
  44. package/src/ensure-built.test.ts +39 -0
  45. package/src/ensure-built.ts +23 -9
  46. package/src/executors/bounded-exec/cpu-pool.test.ts +132 -4
  47. package/src/executors/bounded-exec/cpu-pool.ts +300 -46
  48. package/src/executors/bounded-exec/cpu-tokens.test.ts +60 -15
  49. package/src/executors/bounded-exec/cpu-tokens.ts +34 -11
  50. package/src/executors/bounded-exec/executor.test.ts +72 -4
  51. package/src/executors/bounded-exec/executor.ts +12 -6
  52. package/src/executors/bounded-exec/run-cpu-pool.test.ts +3 -0
  53. package/src/executors/bounded-exec/run-cpu-pool.ts +5 -4
  54. package/src/executors/cargo-zigbuild/executor.test.ts +129 -0
  55. package/src/executors/cargo-zigbuild/executor.ts +176 -0
  56. package/src/executors/cargo-zigbuild/schema.json +35 -0
  57. package/src/executors/cargo-zigbuild/schema.ts +14 -0
  58. package/src/index.test.ts +29 -61
  59. package/src/index.ts +60 -98
  60. package/src/nx-daemon-validity.test.ts +65 -0
  61. package/src/producer-receipts.test.ts +37 -15
  62. package/src/producer-receipts.ts +29 -5
  63. package/src/testing.ts +3 -2
  64. package/test-timings.json +1 -1
  65. package/dist/executors/napi-cross-toolchain/executor.d.ts +0 -42
  66. package/dist/executors/napi-cross-toolchain/executor.d.ts.map +0 -1
  67. package/dist/executors/napi-cross-toolchain/executor.js +0 -126
  68. package/dist/executors/napi-cross-toolchain/schema.d.ts +0 -4
  69. package/dist/executors/napi-cross-toolchain/schema.d.ts.map +0 -1
  70. package/src/executors/napi-cross-toolchain/executor.test.ts +0 -112
  71. package/src/executors/napi-cross-toolchain/executor.ts +0 -165
  72. package/src/executors/napi-cross-toolchain/schema.json +0 -15
  73. package/src/executors/napi-cross-toolchain/schema.ts +0 -3
  74. /package/dist/executors/{napi-cross-toolchain → cargo-zigbuild}/schema.js +0 -0
@@ -1,10 +1,12 @@
1
1
  import { describe, expect, it } from 'bun:test';
2
- import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
2
+ import { spawnSync } from 'node:child_process';
3
+ import { chmod, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
3
4
  import { tmpdir } from 'node:os';
4
5
  import { join } from 'node:path';
5
6
 
6
7
  import {
7
8
  CARGO_CROSS_LINT_COMMAND,
9
+ CARGO_CROSS_LINT_REFUSAL,
8
10
  CARGO_CROSS_LINT_TARGET,
9
11
  CARGO_DENY_WARNINGS,
10
12
  CARGO_LINT_CLIPPY_COMMAND,
@@ -13,6 +15,7 @@ import {
13
15
  CROSS_CHECK_SCRIPT_NAME,
14
16
  cargoFrozen,
15
17
  DEVENV_CROSS_PROFILE,
18
+ runsFrozenCargo,
16
19
  } from './cross-check-policy.js';
17
20
  import { BUILD_OUTPUT_DEPENDENCIES, PLATFORM_TARGET_GLOBS } from './workspace-config-policy.js';
18
21
 
@@ -119,12 +122,24 @@ describe('Linux cross-check policy', () => {
119
122
  }
120
123
  });
121
124
 
122
- it('fails closed on Darwin without the linux-cross compiler instead of compiling', () => {
123
- // Without this guard a Darwin cache miss sits in ring's cc-rs looking for
124
- // x86_64-linux-gnu-gcc. The linux-cross profile exports
125
- // CC_x86_64_unknown_linux_gnu; Linux CI is already the target.
126
- expect(CARGO_CROSS_LINT_COMMAND).toContain('CC_x86_64_unknown_linux_gnu');
127
- expect(CARGO_CROSS_LINT_COMMAND).toContain('uname -s');
125
+ it('refuses to start without cargo-zigbuild instead of compiling', async () => {
126
+ // Without this guard a cache miss outside the linux-cross toolchain would
127
+ // start a compile that cannot finish. Run for real against a PATH with no
128
+ // cargo-zigbuild: it must refuse, name the fix, and never reach cargo.
129
+ const child = Bun.spawn(['/bin/sh', '-c', CARGO_CROSS_LINT_COMMAND], {
130
+ cwd: tmpdir(),
131
+ env: { PATH: '/usr/bin:/bin' },
132
+ stdout: 'pipe',
133
+ stderr: 'pipe',
134
+ });
135
+ const [exitCode, stderr] = await Promise.all([child.exited, new Response(child.stderr).text()]);
136
+ expect(exitCode).toBe(2);
137
+ expect(stderr).toContain('needs cargo-zigbuild');
138
+ expect(stderr).toContain('bun run check:linux');
139
+ });
140
+
141
+ it('prints the refusal the managed pre-push hook reads as a cache miss', () => {
142
+ expect(CARGO_CROSS_LINT_COMMAND).toContain(`echo '${CARGO_CROSS_LINT_REFUSAL}' >&2; exit 2`);
128
143
  });
129
144
 
130
145
  it('carries the triple explicitly so a host lint cannot pass for a cross one', () => {
@@ -171,8 +186,16 @@ describe('Linux cross-check policy', () => {
171
186
 
172
187
  it('pins clippy to the committed lockfile and local cache', () => {
173
188
  expect(cargoFrozen('clippy --workspace')).toBe('cargo --frozen clippy --workspace');
174
- expect(CARGO_CROSS_LINT_COMMAND).toContain('cargo --frozen clippy');
189
+ // cargo drops a global --frozen in front of an external subcommand, so the
190
+ // flag sits on cargo-zigbuild's own subcommand.
191
+ expect(CARGO_CROSS_LINT_COMMAND).toContain('cargo-zigbuild clippy --frozen ');
192
+ expect(CARGO_CROSS_LINT_COMMAND).not.toContain('cargo --frozen zigbuild');
175
193
  expect(CARGO_LINT_CLIPPY_COMMAND).toContain('cargo --frozen clippy');
194
+ // Inference recognizes both spellings, so both get the fetch prerequisite
195
+ // and the toolchain identity.
196
+ expect(runsFrozenCargo(CARGO_CROSS_LINT_COMMAND)).toBe(true);
197
+ expect(runsFrozenCargo(CARGO_LINT_CLIPPY_COMMAND)).toBe(true);
198
+ expect(runsFrozenCargo('cargo clippy --workspace')).toBe(false);
176
199
  });
177
200
 
178
201
  it('drives the Nx target through the cross profile from the root script', () => {
@@ -184,9 +207,9 @@ describe('Linux cross-check policy', () => {
184
207
  // ...and runs Nx rather than cargo directly, so caching and project
185
208
  // discovery stay Nx's job.
186
209
  expect(CROSS_CHECK_SCRIPT_COMMAND).toContain(`nx run-many -t ${CARGO_CROSS_LINT_TARGET}`);
187
- // The probe-only pre-push gate never runs this, so a human does — and
188
- // devenv's own progress has to reach them, or a 0.4 GiB closure and an
189
- // unbounded clippy are indistinguishable from a hang.
210
+ // The pre-push gate never enters the cross profile, so a human does — and
211
+ // devenv's own progress has to reach them, or a 0.9 GiB toolchain closure
212
+ // and an unbounded clippy are indistinguishable from a hang.
190
213
  expect(CROSS_CHECK_SCRIPT_COMMAND).not.toContain('--quiet');
191
214
  });
192
215
 
@@ -194,3 +217,59 @@ describe('Linux cross-check policy', () => {
194
217
  expect(CROSS_CHECK_SCRIPT_NAME).toMatch(/^[a-z]+:[a-z-]+$/);
195
218
  });
196
219
  });
220
+
221
+ /**
222
+ * The managed pre-push hook, run for real in a fixture repository whose `tooling/direnv/repo-path` puts stand-ins
223
+ * first on PATH: `uname` says Darwin, `smoo` passes the denylist, and `nx` prints what a cross run printed and exits
224
+ * with its status. The hook must call a miss a miss and every other Nx failure what it is.
225
+ */
226
+ describe('managed pre-push cross probe', () => {
227
+ const hook = join(import.meta.dir, '../managed/raw/tooling/git-hooks/pre-push.sh');
228
+
229
+ async function push(nxOutput: string, nxStatus: number): Promise<{ status: number | null; stderr: string }> {
230
+ const root = await mkdtemp(join(tmpdir(), 'pre-push-cross-'));
231
+ try {
232
+ const bin = join(root, 'bin');
233
+ await mkdir(bin);
234
+ await mkdir(join(root, 'repo', 'tooling', 'direnv'), { recursive: true });
235
+ const stub = async (name: string, body: string) => {
236
+ await writeFile(join(bin, name), `#!/bin/sh\n${body}\n`);
237
+ await chmod(join(bin, name), 0o755);
238
+ };
239
+ await stub('uname', 'echo Darwin');
240
+ await stub('smoo', 'exit 0');
241
+ await stub('nx', `printf '%s\\n' "$NX_STUB_OUTPUT"; exit ${nxStatus}`);
242
+ await writeFile(join(root, 'repo', 'tooling', 'direnv', 'repo-path'), `#!/bin/sh\necho "${bin}:$PATH"\n`);
243
+ await chmod(join(root, 'repo', 'tooling', 'direnv', 'repo-path'), 0o755);
244
+ await writeFile(join(root, 'repo', 'tooling', 'direnv', 'ttsc-env.sh'), '');
245
+ expect(spawnSync('git', ['init', '-q', join(root, 'repo')]).status).toBe(0);
246
+ const run = spawnSync('bash', [hook, 'origin'], {
247
+ cwd: join(root, 'repo'),
248
+ env: { ...process.env, NX_STUB_OUTPUT: nxOutput },
249
+ input: '',
250
+ encoding: 'utf8',
251
+ });
252
+ return { status: run.status, stderr: run.stderr };
253
+ } finally {
254
+ await rm(root, { recursive: true, force: true });
255
+ }
256
+ }
257
+
258
+ it('passes a hit', async () => {
259
+ expect((await push('Successfully ran target cargo-lint-cross', 0)).status).toBe(0);
260
+ });
261
+
262
+ it('names a miss when the cross target refused to compile', async () => {
263
+ const { status, stderr } = await push(CARGO_CROSS_LINT_REFUSAL, 1);
264
+ expect(status).toBe(1);
265
+ expect(stderr).toContain('NOT cached for this tree');
266
+ expect(stderr).toContain('bun run check:linux');
267
+ });
268
+
269
+ it('names any other Nx failure as that, never as a cache miss', async () => {
270
+ const { status, stderr } = await push('NX NX_WORKSPACE_ROOT_PATH names another workspace', 1);
271
+ expect(status).toBe(1);
272
+ expect(stderr).toContain('Nx failed (exit 1) before the Linux cross-compile check reached');
273
+ expect(stderr).not.toContain('NOT cached');
274
+ });
275
+ });
@@ -5,11 +5,12 @@
5
5
  * compiles is invisible to every check a macOS host can run: a full local lint
6
6
  * passes and CI still goes red on unused imports, unresolved names and dead code
7
7
  * inside the `not(target_os = "macos")` branch. Closing that needs three pieces
8
- * to agree — a devenv profile carrying the cross C toolchain, an Nx target per
9
- * Cargo workspace that asks for the triple, and a root script that runs the
10
- * second inside the first. They live here together because the plugin infers the
11
- * target while the CLI installs the script and reports a project missing it; kept
12
- * apart, the three spellings drift and the gate silently stops gating.
8
+ * to agree — a devenv profile carrying the Linux toolchain (zig, cargo-zigbuild
9
+ * and the Linux rust-std set), an Nx target per Cargo workspace that asks for
10
+ * the triple, and a root script that runs the second inside the first. They
11
+ * live here together because the plugin infers the target while the CLI
12
+ * installs the script and reports a project missing it; kept apart, the three
13
+ * spellings drift and the gate silently stops gating.
13
14
  */
14
15
 
15
16
  /**
@@ -19,9 +20,10 @@
19
20
  export const CARGO_LINUX_TRIPLE = 'x86_64-unknown-linux-gnu';
20
21
 
21
22
  /**
22
- * The devenv profile carrying the cross C compiler, defined in the managed
23
- * `tooling/direnv/devenv.smoo.nix`. Opt-in because its closure is 0.4 GiB and
24
- * nothing on Darwin links a Linux object.
23
+ * The devenv profile carrying the Linux toolchain — zig, cargo-zigbuild and the
24
+ * Linux rust-std set — defined in the managed `tooling/direnv/devenv.smoo.nix`.
25
+ * Opt-in on macOS because zig alone is a 380 MiB closure that nothing but a
26
+ * Linux artifact or this check uses.
25
27
  */
26
28
  export const DEVENV_CROSS_PROFILE = 'linux-cross';
27
29
 
@@ -38,7 +40,16 @@ export const DEVENV_CROSS_PROFILE = 'linux-cross';
38
40
  */
39
41
  export const CARGO_CROSS_LINT_TARGET = 'cargo-lint-cross';
40
42
 
41
- const CARGO_CROSS_LINT_GUARD = `[ -n "\${CC_x86_64_unknown_linux_gnu:-}" ] || [ "$(uname -s)" = Linux ] || { echo 'cargo-lint-cross needs the linux-cross C toolchain; run: bun run check:linux' >&2; exit 2; }`;
43
+ /**
44
+ * What the cross lint prints when it refuses to compile without cargo-zigbuild
45
+ * on PATH: on a Darwin cache miss this line, and only this line, means the
46
+ * key missed. The managed pre-push hook reads its output for exactly this text
47
+ * to tell a miss from any other Nx failure.
48
+ */
49
+ export const CARGO_CROSS_LINT_REFUSAL =
50
+ 'cargo-lint-cross needs cargo-zigbuild from the linux-cross toolchain; run: bun run check:linux';
51
+
52
+ const CARGO_CROSS_LINT_GUARD = `command -v cargo-zigbuild >/dev/null || { echo '${CARGO_CROSS_LINT_REFUSAL}' >&2; exit 2; }`;
42
53
 
43
54
  /**
44
55
  * The prefix `cargoFrozen` writes. Exported so target inference can recognize
@@ -47,6 +58,21 @@ const CARGO_CROSS_LINT_GUARD = `[ -n "\${CC_x86_64_unknown_linux_gnu:-}" ] || [
47
58
  */
48
59
  export const CARGO_FROZEN_PREFIX = 'cargo --frozen ';
49
60
 
61
+ /**
62
+ * cargo-zigbuild's spelling of frozen clippy. The flag sits on the subcommand
63
+ * because cargo does not forward a global `--frozen` to an external one:
64
+ * `cargo --frozen zigbuild` resolves and writes Cargo.lock as if unflagged
65
+ * (measured). And `cargo-zigbuild` is run directly rather than as
66
+ * `cargo zigbuild`, so its inner `cargo` is the one on PATH rather than the
67
+ * real binary cargo would hand it through `CARGO`.
68
+ */
69
+ const CARGO_ZIGBUILD_CLIPPY_FROZEN_PREFIX = 'cargo-zigbuild clippy --frozen ';
70
+
71
+ /** Does this command text run frozen cargo, directly or through cargo-zigbuild? */
72
+ export function runsFrozenCargo(text: string): boolean {
73
+ return text.includes(CARGO_FROZEN_PREFIX) || text.includes(CARGO_ZIGBUILD_CLIPPY_FROZEN_PREFIX);
74
+ }
75
+
50
76
  /**
51
77
  * `--frozen` is cargo's `--locked` + `--offline`. Cargo.lock and the registry
52
78
  * cache are inputs: inferred cargo must not rewrite the lockfile or fetch.
@@ -122,8 +148,13 @@ export const CARGO_DENY_WARNINGS = `--config 'build.warnings="deny"'`;
122
148
  * must then refuse to launch.
123
149
  *
124
150
  * `--target` is explicit rather than inherited from a CARGO_BUILD_TARGET exported
125
- * by the profile, so running this outside the cross shell fails loudly on a
126
- * missing tool instead of quietly linting the host and reporting green.
151
+ * by the profile, so running this outside the cross shell fails loudly on the
152
+ * missing cargo-zigbuild instead of quietly linting the host and reporting green.
153
+ *
154
+ * cargo-zigbuild rather than plain cargo because ring compiles C for the target
155
+ * from its build script, and build scripts run even under clippy: zig is the
156
+ * C compiler for every Linux triple on every host, the same one the Linux
157
+ * artifacts build with, so the check needs no per-triple cross gcc.
127
158
  *
128
159
  * `cargo fmt` is absent on purpose: formatting is target-independent and already
129
160
  * covered by `cargo-lint`, so repeating it here would only cost time.
@@ -139,9 +170,7 @@ export const CARGO_DENY_WARNINGS = `--config 'build.warnings="deny"'`;
139
170
  * are distinct anyway, and its own lock keeps the cross check from queueing
140
171
  * behind host builds.
141
172
  */
142
- export const CARGO_CROSS_LINT_COMMAND = `${CARGO_CROSS_LINT_GUARD}; ${cargoFrozen(
143
- `clippy --workspace --all-targets --target ${CARGO_LINUX_TRIPLE} --target-dir target/cargo-lint-cross ${CARGO_DENY_WARNINGS}`,
144
- )}`;
173
+ export const CARGO_CROSS_LINT_COMMAND = `${CARGO_CROSS_LINT_GUARD}; ${CARGO_ZIGBUILD_CLIPPY_FROZEN_PREFIX}--workspace --all-targets --target ${CARGO_LINUX_TRIPLE} --target-dir target/cargo-lint-cross ${CARGO_DENY_WARNINGS}`;
145
174
 
146
175
  /**
147
176
  * No `--target-dir`: clippy writes the workspace's own `target/`, beside every
@@ -168,9 +197,10 @@ export const CROSS_CHECK_SCRIPT_NAME = 'check:linux';
168
197
  * cache and refuses a push that has no hit, never entering a profile or starting
169
198
  * a compile of its own. That makes this script the only place the gate ever
170
199
  * runs, and a human watching it needs devenv's progress too — the cross profile
171
- * is a 0.4 GiB closure and the clippy behind it is unbounded, so a silent
172
- * terminal is indistinguishable from a hang. Suppressing that output only paid
173
- * while a hook ran this for you and nobody read it.
200
+ * adds about 0.9 GiB (zig plus three rust-std targets) and the clippy behind it
201
+ * is unbounded, so a silent terminal is indistinguishable from a hang.
202
+ * Suppressing that output only paid while a hook ran this for you and nobody
203
+ * read it.
174
204
  */
175
205
  export const CROSS_CHECK_SCRIPT_COMMAND = `tooling/devenv -P ${DEVENV_CROSS_PROFILE} shell -- nx run-many -t ${CARGO_CROSS_LINT_TARGET}`;
176
206
 
@@ -1191,6 +1191,45 @@ await new Promise<void>((resolve, reject) => process.stdout.write(JSON.stringify
1191
1191
  }
1192
1192
  });
1193
1193
 
1194
+ // A sandboxed consumer is granted its provider read-only, and starting a daemon writes the provider's workspace
1195
+ // data (`.nx/workspace-data/d`): the read-only probe connects to a serving daemon or reports that none serves.
1196
+ it('reports no-daemon and writes nothing when no daemon serves a provider without a receipt', async () => {
1197
+ await withNxFixture(
1198
+ 'ensure-built-probe-no-daemon-',
1199
+ async ({ root, workspace, socketDirectory }) => {
1200
+ await writeTwoProjectWorkspace(workspace, join(root, 'lib-builds.log'), true);
1201
+ const listing = async () =>
1202
+ (await readdir(workspace, { recursive: true })).filter((entry) => !entry.startsWith('node_modules')).sort();
1203
+ const before = await listing();
1204
+ const probeEntry = join(root, 'probe-no-daemon.ts');
1205
+ await writeFile(
1206
+ probeEntry,
1207
+ `
1208
+ import { probeBuilt } from ${JSON.stringify(join(packageRoot, 'dist', 'ensure-built.js'))};
1209
+ const result = await probeBuilt({ target: 'app:build', cwd: ${JSON.stringify(workspace)} });
1210
+ await new Promise<void>((resolve, reject) => process.stdout.write(JSON.stringify(result) + "\\n", error => error ? reject(error) : resolve()));
1211
+ `,
1212
+ );
1213
+ // Run from the fixture root, outside the provider workspace, as a consumer calls it.
1214
+ const probed = await run('bun', probeEntry, root, [], { NX_DAEMON: 'true', NX_SOCKET_DIR: socketDirectory });
1215
+ expect(probed.code, probed.stdout + probed.stderr).toBe(0);
1216
+ const result = typia.json.assertParse<EnsureBuiltProbeResult>(probed.stdout);
1217
+ expect(result).toMatchObject({
1218
+ disposition: 'failed',
1219
+ reason: { kind: 'provider-proof', taskId: 'app:build' },
1220
+ });
1221
+ if (result.disposition !== 'failed' || result.reason.kind !== 'provider-proof') {
1222
+ throw new Error(`the probe must refuse with its provider proof, not ${probed.stdout}`);
1223
+ }
1224
+ expect(result.reason.detail).toEndWith(
1225
+ `authoritative graph unavailable: ${describeMiss({ kind: 'no-daemon', taskId: 'app:build' })}`,
1226
+ );
1227
+ expect(await listing()).toEqual(before);
1228
+ },
1229
+ 'workspace',
1230
+ );
1231
+ });
1232
+
1194
1233
  // The graph/exec case runs real Nx because its subject is the daemon:
1195
1234
  // the daemon a probe starts holds no output records, so every output must be
1196
1235
  // vouched for by comparing the working tree with Nx's cache artifact. The
@@ -302,7 +302,7 @@ export function describeMiss(reason: MissReason): string {
302
302
  case 'cache-disabled':
303
303
  return `the Nx cache is disabled, so ${reason.taskId} must run`;
304
304
  case 'no-daemon':
305
- return `the Nx daemon is disabled, so ${reason.taskId} cannot be checked without running it`;
305
+ return `no Nx daemon serves this workspace, so ${reason.taskId} cannot be checked without running it`;
306
306
  }
307
307
  }
308
308
 
@@ -735,23 +735,36 @@ type PreparedWorkspaceProbe =
735
735
  | ReadyWorkspaceProbe
736
736
  | { readonly kind: 'no-daemon'; readonly reason: Extract<MissReason, { kind: 'no-daemon' }> };
737
737
 
738
+ /**
739
+ * The two operations a probe is prepared for. `build` may start the workspace's Nx daemon, as `nx` itself does.
740
+ * `read-only` only connects to a daemon already serving the workspace, and asks it for the authoritative graph:
741
+ * starting one writes the workspace's data directory (`.nx/workspace-data/d`), and a read-only caller may not
742
+ * write it at all — a sandboxed consumer is granted its provider read-only. A daemon that exits between that
743
+ * connection check and the first request is the one window in which Nx's client still starts a successor.
744
+ */
745
+ type ProbeOperation = 'build' | 'read-only';
746
+
738
747
  async function prepareWorkspaceProbe(
739
748
  workspaceRoot: string,
740
749
  selector: TargetSelector,
741
- forceGraphRpc = false,
750
+ operation: ProbeOperation,
742
751
  ): Promise<PreparedWorkspaceProbe> {
743
752
  const taskId = selectorTaskId(selector);
753
+ const noDaemon: PreparedWorkspaceProbe = { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
744
754
  // This explicit fallback path loads no Nx. The build operation may run the
745
755
  // checkout-local CLI; the read-only operation can only report the miss.
746
756
  if (process.env.NX_DAEMON === 'false') {
747
- return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
757
+ return noDaemon;
748
758
  }
749
759
  // Runtime modules always belong to the provider's installed Nx version.
750
760
  const requireNx = createWorkspaceNxRequire(workspaceRoot);
751
761
  bindWorkspaceRoot(workspaceRoot, requireNx);
752
762
  const { daemonClient } = requireNx('nx/src/daemon/client/client');
753
763
  if (!daemonClient.enabled()) {
754
- return { kind: 'no-daemon', reason: { kind: 'no-daemon', taskId } };
764
+ return noDaemon;
765
+ }
766
+ if (operation === 'read-only' && !(await daemonClient.isServerAvailable())) {
767
+ return noDaemon;
755
768
  }
756
769
  assignEnvironment(process.env, withoutOuterTaskCacheBypass(process.env));
757
770
 
@@ -771,7 +784,7 @@ async function prepareWorkspaceProbe(
771
784
  setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles);
772
785
 
773
786
  performance.mark('ensureBuilt:graph:start');
774
- const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx, forceGraphRpc);
787
+ const projectGraph = await currentProjectGraph(workspaceRoot, nxJson, requireNx, operation === 'read-only');
775
788
  nxJson = readNxJson();
776
789
  requireProject(projectGraph, selector.project);
777
790
  const taskGraph = createTaskGraph(
@@ -819,7 +832,7 @@ async function withTaskHooks<Result>(
819
832
  }
820
833
 
821
834
  async function ensureBuiltInWorkspace(workspaceRoot: string, selector: TargetSelector): Promise<EnsureBuiltResult> {
822
- const prepared = await prepareWorkspaceProbe(workspaceRoot, selector);
835
+ const prepared = await prepareWorkspaceProbe(workspaceRoot, selector, 'build');
823
836
  if (prepared.kind === 'no-daemon') return runViaCli(workspaceRoot, selector, prepared.reason);
824
837
  return withTaskHooks(workspaceRoot, selector, prepared, buildPreparedTarget);
825
838
  }
@@ -842,9 +855,10 @@ async function probeBuiltInWorkspace(workspaceRoot: string, selector: TargetSele
842
855
  const taskId = selectorTaskId(selector);
843
856
  const receipt = readProducerReceipt(workspaceRoot, taskId);
844
857
  if (receipt.ok) return { disposition: 'hit', receipt: receipt.value };
845
- // A failed publication proof asks Nx for the authoritative generation, but
846
- // never hashes against the consumer environment or starts a provider runner.
847
- const prepared = await prepareWorkspaceProbe(workspaceRoot, selector, true);
858
+ // A failed publication proof asks a serving Nx daemon for the authoritative
859
+ // generation, but never hashes against the consumer environment, starts a
860
+ // provider runner or starts the provider's daemon.
861
+ const prepared = await prepareWorkspaceProbe(workspaceRoot, selector, 'read-only');
848
862
  const detail =
849
863
  prepared.kind === 'no-daemon'
850
864
  ? `${receipt.detail}; authoritative graph unavailable: ${describeMiss(prepared.reason)}`
@@ -1,5 +1,17 @@
1
1
  import { afterEach, describe, expect, it } from 'bun:test';
2
- import { chmodSync, existsSync, mkdirSync, readdirSync, rmSync, statSync } from 'node:fs';
2
+ import {
3
+ chmodSync,
4
+ closeSync,
5
+ constants,
6
+ existsSync,
7
+ mkdirSync,
8
+ openSync,
9
+ readdirSync,
10
+ readSync,
11
+ rmSync,
12
+ statSync,
13
+ writeSync,
14
+ } from 'node:fs';
3
15
  import { writeFile } from 'node:fs/promises';
4
16
  import { connect, type Socket } from 'node:net';
5
17
  import { dirname, join } from 'node:path';
@@ -82,11 +94,16 @@ const QUEUED: PoolReply = { ok: true, lease: 'queued' };
82
94
  const pools: CpuPool[] = [];
83
95
  const sockets: Socket[] = [];
84
96
  const directories: string[] = [];
97
+ // Descriptors a test opened on a pool's jobserver fifo, as a make-protocol client does.
98
+ const descriptors: number[] = [];
85
99
 
86
100
  afterEach(async () => {
87
101
  for (const socket of sockets.splice(0)) {
88
102
  socket.destroy();
89
103
  }
104
+ for (const fd of descriptors.splice(0)) {
105
+ closeSync(fd);
106
+ }
90
107
  for (const pool of pools.splice(0)) {
91
108
  // Every pool a test leaves open is closed here, and its outcome is asserted, not dropped.
92
109
  expect(await pool.close()).toEqual({ ok: true });
@@ -220,8 +237,59 @@ async function untilLine(stream: ReadableStream<Uint8Array>, marker: string): Pr
220
237
  reader.releaseLock();
221
238
  }
222
239
 
240
+ /** The fifo a pool's environment names for Cargo and make, which name the same one. */
241
+ function jobserverFifo(pool: CpuPool): string {
242
+ const flags = pool.environment.CARGO_MAKEFLAGS ?? '';
243
+ expect(pool.environment.MAKEFLAGS).toBe(flags);
244
+ const named = /^-j --jobserver-auth=fifo:(\/\S+)$/.exec(flags)?.[1];
245
+ if (named === undefined) {
246
+ throw new Error(`the pool names no jobserver fifo: ${JSON.stringify(pool.environment)}`);
247
+ }
248
+ return named;
249
+ }
250
+
251
+ /**
252
+ * A make-protocol client of `fifo`, opened read-write as Cargo opens it, and non-blocking here so that `take` reads
253
+ * what the fifo holds now and an empty fifo reads 0.
254
+ */
255
+ function jobserverClient(fifo: string): { take(most: number): number; give(count: number): void } {
256
+ const fd = openSync(fifo, constants.O_RDWR | constants.O_NONBLOCK);
257
+ descriptors.push(fd);
258
+ return {
259
+ take(most) {
260
+ try {
261
+ return readSync(fd, Buffer.alloc(most), 0, most, null);
262
+ } catch (error) {
263
+ if (error instanceof Error && 'code' in error && error.code === 'EAGAIN') return 0;
264
+ throw error;
265
+ }
266
+ },
267
+ give(count) {
268
+ expect(writeSync(fd, Buffer.alloc(count, '+'))).toBe(count);
269
+ },
270
+ };
271
+ }
272
+
273
+ /** Wait for `count` token bytes in `fifo` and take them, as a blocked Cargo does; resolves to the count it took. */
274
+ async function awaitTokens(fifo: string, count: number): Promise<number> {
275
+ const fd = openSync(fifo, constants.O_RDONLY | constants.O_NONBLOCK);
276
+ const reader = Bun.file(fd).stream().getReader();
277
+ let taken = 0;
278
+ try {
279
+ while (taken < count) {
280
+ const chunk = await reader.read();
281
+ if (chunk.done) break;
282
+ taken += chunk.value.byteLength;
283
+ }
284
+ } finally {
285
+ await reader.cancel();
286
+ closeSync(fd);
287
+ }
288
+ return taken;
289
+ }
290
+
223
291
  describe('the parent-held CPU pool', () => {
224
- it('serves a Cargo allocation as its own live child pool without competing with the occupied ancestor', async () => {
292
+ it('serves an allocation as its own live child pool without competing with the occupied ancestor', async () => {
225
293
  const { parent, pool } = await openPool(4);
226
294
  const cargo = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 4, children: true }));
227
295
  expect(await cargo.next()).toEqual(QUEUED);
@@ -382,7 +450,7 @@ describe('the parent-held CPU pool', () => {
382
450
  }
383
451
  });
384
452
 
385
- it('names the endpoint and count it holds, in private owned paths, and removes them on close', async () => {
453
+ it('names its endpoint, count and jobserver fifo in private owned paths, and removes them on close', async () => {
386
454
  const made = await createCpuPool(3, NEVER, countedWaits());
387
455
  if (!made.ok) {
388
456
  throw new Error(made.error);
@@ -396,8 +464,17 @@ describe('the parent-held CPU pool', () => {
396
464
  expect(made.pool.environment[CPU_TOKENS_ENV]).toBe('3');
397
465
  expect(statSync(dirname(inherited.socket)).mode & 0o777).toBe(0o700);
398
466
  expect(statSync(inherited.socket).mode & 0o777).toBe(0o600);
467
+ // The jobserver fifo lies beside the socket, as private, and holds the pool's tokens but the implicit first.
468
+ const fifo = jobserverFifo(made.pool);
469
+ expect(dirname(fifo)).toBe(dirname(inherited.socket));
470
+ expect(statSync(fifo).isFIFO()).toBe(true);
471
+ expect(statSync(fifo).mode & 0o777).toBe(0o600);
472
+ const cargo = jobserverClient(fifo);
473
+ expect(cargo.take(3)).toBe(2);
474
+ cargo.give(2);
399
475
  expect(await made.pool.close()).toEqual({ ok: true });
400
476
  expect(await made.pool.close()).toEqual({ ok: true });
477
+ expect(existsSync(fifo)).toBe(false);
401
478
  expect(existsSync(dirname(inherited.socket))).toBe(false);
402
479
  });
403
480
 
@@ -408,6 +485,8 @@ describe('the parent-held CPU pool', () => {
408
485
  '{"op":"other","want":1}',
409
486
  '{"op":"cpu-pool","want":0}',
410
487
  '{"op":"cpu-pool","want":1.5}',
488
+ // A grant never grows, so `grow` is a field like any other this pool does not read.
489
+ '{"op":"cpu-pool","want":1,"grow":true}',
411
490
  'x'.repeat(5000),
412
491
  'x'.repeat(1024 * 1024),
413
492
  ];
@@ -428,7 +507,7 @@ describe('the parent-held CPU pool', () => {
428
507
  // A wait holds no token: the whole pool is still there to grant.
429
508
  expect(await grantedFor(parent, 4)).toBe(4);
430
509
 
431
- // Cargo's launcher is served a child pool; the test it runs reports its waits there, to the same run.
510
+ // A child is served a pool of its own; the test it runs reports its waits there, to the same run.
432
511
  const cargo = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 2, children: true }));
433
512
  expect(await cargo.next()).toEqual(QUEUED);
434
513
  const granted = await cargo.next();
@@ -553,4 +632,53 @@ describe('the parent-held CPU pool', () => {
553
632
  expect(cpuPoolDirectories()).toEqual(before);
554
633
  await expect(createCpuPool(0, NEVER, countedWaits())).rejects.toBeInstanceOf(RangeError);
555
634
  });
635
+
636
+ it("lends a 1-token pool's implicit slot to a socket child, since its fifo holds no byte", async () => {
637
+ const { pool, parent } = await openPool(1);
638
+ expect(jobserverClient(jobserverFifo(pool)).take(1)).toBe(0);
639
+ // The holder is not using its own token while it waits on this child: lending it is what avoids the deadlock.
640
+ const first = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 2 }));
641
+ expect(await first.next()).toEqual(QUEUED);
642
+ expect(await first.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
643
+ const second = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 1 }));
644
+ expect(await second.next()).toEqual(QUEUED);
645
+ first.socket.destroy();
646
+ expect(await second.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
647
+ });
648
+
649
+ it('grants no byte a fifo reader holds, and wakes its queued head when the reader writes it back', async () => {
650
+ const { pool, parent } = await openPool(2);
651
+ const cargo = jobserverClient(jobserverFifo(pool));
652
+ // Cargo takes the fifo's one byte: only the implicit slot is left to lend.
653
+ expect(cargo.take(1)).toBe(1);
654
+ const first = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 2 }));
655
+ expect(await first.next()).toEqual(QUEUED);
656
+ expect(await first.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
657
+ const second = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 1 }));
658
+ expect(await second.next()).toEqual(QUEUED);
659
+ const third = childOf(parent.socket, JSON.stringify({ op: 'cpu-pool', want: 1 }));
660
+ expect(await third.next()).toEqual(QUEUED);
661
+ // No socket child releases anything: only the fifo turning readable can wake the head, and it takes the byte.
662
+ cargo.give(1);
663
+ expect(await second.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
664
+ // The implicit slot goes back with the first child, to the next in line.
665
+ first.socket.destroy();
666
+ expect(await third.next()).toEqual({ ok: true, lease: 'granted', tokens: 1 });
667
+ });
668
+
669
+ it("writes a released child's bytes back to the fifo, where Cargo and the next child both find them", async () => {
670
+ const { pool, parent } = await openPool(3);
671
+ const fifo = jobserverFifo(pool);
672
+ const cargo = jobserverClient(fifo);
673
+ const holder = await takeOrThrow(parent, 3);
674
+ expect(holder.tokens).toBe(3);
675
+ // The grant took both bytes and the implicit slot: the fifo is empty while it holds them.
676
+ expect(cargo.take(2)).toBe(0);
677
+ holder.release();
678
+ await holder.closed;
679
+ // The pool hears the close after the child does; a waiting reader takes the two bytes it writes back.
680
+ expect(await awaitTokens(fifo, 2)).toBe(2);
681
+ cargo.give(2);
682
+ expect(await grantedFor(parent, 3)).toBe(3);
683
+ });
556
684
  });