@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
@@ -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
 
@@ -54,6 +54,24 @@ describe('cowshed CPU budget client', () => {
54
54
  want: 12,
55
55
  });
56
56
  expect(runnerDemand('cargo nextest run --test-threads=4', undefined, 18, {})).toEqual({ kind: 'nextest', want: 4 });
57
+ // `--no-capture` runs tests serially and ignores `--test-threads`: a host smoke of one test asks for one
58
+ // token, not one per core it would hold idle while its own scratch jobs queue for the host's tokens.
59
+ const smoke =
60
+ "cargo --frozen nextest run --archive-file a.tar.zst -E 'test(smoke)' --run-ignored only --no-capture";
61
+ expect(runnerDemand(smoke, undefined, 18, {})).toEqual({ kind: 'nextest', want: 1 });
62
+ expect(runnerDemand(`${smoke} --test-threads 4`, undefined, 18, {})).toEqual({ kind: 'nextest', want: 1 });
63
+ expect(runnerDemand('cargo nextest run -E all() -- --nocapture', undefined, 18, {})).toEqual({
64
+ kind: 'nextest',
65
+ want: 1,
66
+ });
67
+ // A flag that only shares the prefix is not it.
68
+ expect(runnerDemand('cargo nextest run --no-capture-x', undefined, 18, {})).toEqual({ kind: 'nextest', want: 18 });
69
+ // The same words in a cargo build are not nextest's.
70
+ expect(runnerDemand('cargo test --workspace -- --nocapture', undefined, 18, {})).toEqual({
71
+ kind: 'cargo',
72
+ want: 18,
73
+ testThreads: null,
74
+ });
57
75
  expect(runnerDemand('bun test --timeout=0 --parallel=3', undefined, 18, {})).toEqual({
58
76
  kind: 'bun-parallel',
59
77
  want: 3,
@@ -88,6 +106,193 @@ describe('cowshed CPU budget client', () => {
88
106
  expect(runnerDemand('cargo nextest run --test-threads=4', 6, 18, {})).toEqual({ kind: 'nextest', want: 6 });
89
107
  });
90
108
 
109
+ it("asks for Stryker's --concurrency, else its own default of one worker per core less one past four cores", () => {
110
+ expect(runnerDemand('bun stryker run --concurrency 4', undefined, 18, {})).toEqual({ kind: 'stryker', want: 4 });
111
+ expect(runnerDemand('bun stryker run -c 6', undefined, 18, {})).toEqual({ kind: 'stryker', want: 6 });
112
+ expect(runnerDemand('npx stryker run --concurrency=3', undefined, 18, {})).toEqual({ kind: 'stryker', want: 3 });
113
+ expect(runnerDemand('../../node_modules/.bin/stryker run -c 2', undefined, 18, {})).toEqual({
114
+ kind: 'stryker',
115
+ want: 2,
116
+ });
117
+ // Stryker takes the last count it is given.
118
+ expect(runnerDemand('bun stryker run -c 8 --concurrency 5', undefined, 18, {})).toEqual({
119
+ kind: 'stryker',
120
+ want: 5,
121
+ });
122
+ // A share is of the cores, and never less than one worker.
123
+ expect(runnerDemand('bun stryker run --concurrency=50%', undefined, 18, {})).toEqual({ kind: 'stryker', want: 9 });
124
+ expect(runnerDemand('bun stryker run --concurrency 0%', undefined, 18, {})).toEqual({ kind: 'stryker', want: 1 });
125
+ // Stryker's default, which sized nightly 477's sweeps to the whole host: n - 1 past four cores, else n.
126
+ expect(runnerDemand('bun stryker run', undefined, 32, {})).toEqual({ kind: 'stryker', want: 31 });
127
+ expect(runnerDemand('bun stryker run', undefined, 18, {})).toEqual({ kind: 'stryker', want: 17 });
128
+ expect(runnerDemand('bun stryker run', undefined, 4, {})).toEqual({ kind: 'stryker', want: 4 });
129
+ // Another command's `-c` in the same line is not Stryker's.
130
+ expect(runnerDemand('ulimit -c 0 && bun stryker run', undefined, 18, {})).toEqual({ kind: 'stryker', want: 17 });
131
+ expect(runnerDemand('bun stryker run && nice -c 3 true', undefined, 18, {})).toEqual({ kind: 'stryker', want: 17 });
132
+ // A declared parallelism outranks the flag.
133
+ expect(runnerDemand('bun stryker run --concurrency 8', 2, 18, {})).toEqual({ kind: 'stryker', want: 2 });
134
+ // A test file named for Stryker is one bun process.
135
+ expect(runnerDemand('bun test stryker.test.ts', undefined, 18, {})).toEqual({ kind: 'process', want: 1 });
136
+ });
137
+
138
+ it("asks for Vitest's --maxWorkers and go test's -p, else each runner's own per-core default", () => {
139
+ // Vitest outside watch mode runs one worker per core less one.
140
+ expect(runnerDemand('bun vitest run', undefined, 18, {})).toEqual({ kind: 'vitest', want: 17 });
141
+ expect(runnerDemand('bun vitest run --config vitest.config.workers.ts', undefined, 18, {})).toEqual({
142
+ kind: 'vitest',
143
+ want: 17,
144
+ });
145
+ expect(runnerDemand('bun vitest run --maxWorkers=4', undefined, 18, {})).toEqual({ kind: 'vitest', want: 4 });
146
+ expect(runnerDemand('vitest run --max-workers 50%', undefined, 18, {})).toEqual({ kind: 'vitest', want: 9 });
147
+ expect(runnerDemand('bun vitest run --no-file-parallelism', undefined, 18, {})).toEqual({
148
+ kind: 'vitest',
149
+ want: 1,
150
+ });
151
+ // `go test -p` defaults to GOMAXPROCS, normally one per core.
152
+ expect(runnerDemand('go test -count=1 ./hts/... ./host/...', undefined, 18, {})).toEqual({
153
+ kind: 'go-test',
154
+ want: 18,
155
+ });
156
+ expect(runnerDemand('cd go/ttscwire && go test ./...', undefined, 18, { GOMAXPROCS: '4' })).toEqual({
157
+ kind: 'go-test',
158
+ want: 4,
159
+ });
160
+ expect(runnerDemand('go test -p 3 -parallel 8 ./...', undefined, 18, {})).toEqual({ kind: 'go-test', want: 3 });
161
+ // A shell word that is no count leaves the runner's default; it is still sized by the grant.
162
+ expect(runnerDemand('bun ../scripts/gen-go-work.mjs && go test ./... -p="$(nproc)"', undefined, 18, {})).toEqual({
163
+ kind: 'go-test',
164
+ want: 18,
165
+ });
166
+ });
167
+
168
+ it("asks for cargo-mutants' own jobserver, one task per core unless --jobserver-tasks names fewer", () => {
169
+ // The plugin's inferred `mutation` command, with no flag: `--jobs` defaults to one cargo process, but its
170
+ // jobserver defaults to one task per core (`n_tasks=<cores>`), shared by every cargo that process starts.
171
+ expect(runnerDemand('cargo --frozen mutants --workspace', undefined, 18, {})).toEqual({
172
+ kind: 'cargo-mutants',
173
+ want: 18,
174
+ jobs: 1,
175
+ });
176
+ // `--jobs` counts cargo processes, which still share that per-core jobserver.
177
+ expect(runnerDemand('cargo mutants --jobs 4', undefined, 18, {})).toEqual({
178
+ kind: 'cargo-mutants',
179
+ want: 18,
180
+ jobs: 4,
181
+ });
182
+ expect(runnerDemand('cargo mutants --jobserver-tasks 4', undefined, 18, {})).toEqual({
183
+ kind: 'cargo-mutants',
184
+ want: 4,
185
+ jobs: 1,
186
+ });
187
+ expect(runnerDemand('cargo mutants -j2 --jobserver-tasks=4', undefined, 18, {})).toEqual({
188
+ kind: 'cargo-mutants',
189
+ want: 4,
190
+ jobs: 2,
191
+ });
192
+ // Each cargo process runs at least one thread, whatever the jobserver allows.
193
+ expect(runnerDemand('cargo mutants --jobs=6 --jobserver-tasks 4', undefined, 18, {})).toEqual({
194
+ kind: 'cargo-mutants',
195
+ want: 6,
196
+ jobs: 6,
197
+ });
198
+ // `CARGO_MUTANTS_JOBS` as the command will see it: the target's own environment over the inherited one.
199
+ expect(runnerDemand('cargo mutants --jobserver-tasks 4', undefined, 18, { CARGO_MUTANTS_JOBS: '3' })).toEqual({
200
+ kind: 'cargo-mutants',
201
+ want: 4,
202
+ jobs: 3,
203
+ });
204
+ expect(
205
+ runnerDemand(
206
+ 'cargo mutants --jobserver-tasks 4',
207
+ undefined,
208
+ 18,
209
+ { CARGO_MUTANTS_JOBS: '3' },
210
+ { CARGO_MUTANTS_JOBS: '2' },
211
+ ),
212
+ ).toEqual({ kind: 'cargo-mutants', want: 4, jobs: 2 });
213
+ expect(runnerDemand('cargo-mutants mutants --in-diff pr.diff', undefined, 12, {})).toEqual({
214
+ kind: 'cargo-mutants',
215
+ want: 12,
216
+ jobs: 1,
217
+ });
218
+ // Another command's `-j` in the same line is not cargo-mutants'.
219
+ expect(runnerDemand('cargo mutants --jobserver-tasks 4 && make -j 9', undefined, 18, {})).toEqual({
220
+ kind: 'cargo-mutants',
221
+ want: 4,
222
+ jobs: 1,
223
+ });
224
+ expect(runnerDemand('cargo mutants', 3, 18, {})).toEqual({ kind: 'cargo-mutants', want: 3, jobs: 1 });
225
+ // A package named `mutants` is an ordinary cargo build.
226
+ expect(runnerDemand('cargo test -p mutants', undefined, 18, {})).toEqual({
227
+ kind: 'cargo',
228
+ want: 18,
229
+ testThreads: null,
230
+ });
231
+ });
232
+
233
+ it('sizes the worker-flag runners and cargo-mutants to their grant', () => {
234
+ expect(sizeRunner({ kind: 'stryker', want: 17 }, 'bun stryker run', 4)).toEqual({
235
+ command: 'bun stryker run --concurrency=4',
236
+ env: { [CPU_TOKENS_ENV]: '4' },
237
+ });
238
+ expect(sizeRunner({ kind: 'stryker', want: 8 }, 'bun stryker run -c 8 --fileLogLevel off', 3).command).toBe(
239
+ 'bun stryker run --concurrency=3 --fileLogLevel off',
240
+ );
241
+ expect(
242
+ sizeRunner({ kind: 'stryker', want: 8 }, 'ulimit -c 0 && bun stryker run --concurrency 8 && echo done', 2)
243
+ .command,
244
+ ).toBe('ulimit -c 0 && bun stryker run --concurrency=2 && echo done');
245
+ // A grant larger than the ask sizes to the ask.
246
+ expect(sizeRunner({ kind: 'stryker', want: 4 }, 'bun stryker run --concurrency=4', 9).command).toBe(
247
+ 'bun stryker run --concurrency=4',
248
+ );
249
+ expect(sizeRunner({ kind: 'cargo-mutants', want: 18, jobs: 1 }, 'cargo --frozen mutants --workspace', 5)).toEqual({
250
+ command: 'cargo --frozen mutants --jobserver-tasks=5 --workspace',
251
+ env: { [CPU_TOKENS_ENV]: '5', CARGO_BUILD_JOBS: '5', RUST_TEST_THREADS: '5', NEXTEST_TEST_THREADS: '5' },
252
+ });
253
+ // Its cargo processes share the grant: each tests on its share, and there are never more of them than tokens.
254
+ expect(
255
+ sizeRunner({ kind: 'cargo-mutants', want: 18, jobs: 4 }, 'cargo mutants --jobs 4 --jobserver-tasks 18', 8),
256
+ ).toEqual({
257
+ command: 'cargo mutants --jobs=4 --jobserver-tasks=8',
258
+ env: {
259
+ [CPU_TOKENS_ENV]: '8',
260
+ CARGO_MUTANTS_JOBS: '4',
261
+ CARGO_BUILD_JOBS: '2',
262
+ RUST_TEST_THREADS: '2',
263
+ NEXTEST_TEST_THREADS: '2',
264
+ },
265
+ });
266
+ expect(sizeRunner({ kind: 'cargo-mutants', want: 18, jobs: 4 }, 'cargo mutants -j4', 2)).toEqual({
267
+ command: 'cargo mutants --jobserver-tasks=2 --jobs=2',
268
+ env: {
269
+ [CPU_TOKENS_ENV]: '2',
270
+ CARGO_MUTANTS_JOBS: '2',
271
+ CARGO_BUILD_JOBS: '1',
272
+ RUST_TEST_THREADS: '1',
273
+ NEXTEST_TEST_THREADS: '1',
274
+ },
275
+ });
276
+ // `--jobs` is never added: cargo-mutants refuses it beside `--in-place`.
277
+ expect(sizeRunner({ kind: 'cargo-mutants', want: 18, jobs: 1 }, 'cargo mutants --in-place', 3).command).toBe(
278
+ 'cargo mutants --jobserver-tasks=3 --in-place',
279
+ );
280
+ expect(sizeRunner({ kind: 'vitest', want: 17 }, 'bun vitest run --config vitest.config.workers.ts', 3)).toEqual({
281
+ command: 'bun vitest run --maxWorkers=3 --config vitest.config.workers.ts',
282
+ env: { [CPU_TOKENS_ENV]: '3' },
283
+ });
284
+ expect(sizeRunner({ kind: 'vitest', want: 4 }, 'bun vitest run --maxWorkers 4', 2).command).toBe(
285
+ 'bun vitest run --maxWorkers=2',
286
+ );
287
+ expect(
288
+ sizeRunner({ kind: 'go-test', want: 18 }, 'bun ../scripts/gen-go-work.mjs && go test ./... -p="$(nproc)"', 4)
289
+ .command,
290
+ ).toBe('bun ../scripts/gen-go-work.mjs && go test ./... -p=4');
291
+ expect(sizeRunner({ kind: 'go-test', want: 18 }, 'cd go/ttscwire && go test ./...', 2).command).toBe(
292
+ 'cd go/ttscwire && go test -p=2 ./...',
293
+ );
294
+ });
295
+
91
296
  it('sizes each runner to its grant', () => {
92
297
  expect(sizeRunner({ kind: 'nextest', want: 18 }, NEXTEST_PIECE, 5)).toEqual({
93
298
  command: NEXTEST_PIECE,