@smoothbricks/nx-plugin 0.4.24 → 0.4.26

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 +147 -37
  2. package/dist/bin/smoo-nx-exec.js +3 -14
  3. package/dist/cargo-closure-input.d.ts +13 -15
  4. package/dist/cargo-closure-input.d.ts.map +1 -1
  5. package/dist/cargo-closure-input.js +29 -37
  6. package/dist/cargo-source-hash.d.ts +12 -7
  7. package/dist/cargo-source-hash.d.ts.map +1 -1
  8. package/dist/cargo-source-hash.js +416 -32
  9. package/dist/cargo-workspace.d.ts +7 -7
  10. package/dist/cargo-workspace.js +8 -8
  11. package/dist/ensure-built.d.ts +6 -0
  12. package/dist/ensure-built.d.ts.map +1 -1
  13. package/dist/ensure-built.js +79 -6
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +69 -96
  16. package/dist/managed-assets.d.ts +6 -0
  17. package/dist/managed-assets.d.ts.map +1 -1
  18. package/dist/managed-files/files.d.ts.map +1 -1
  19. package/dist/managed-files/files.js +6 -0
  20. package/dist/manifest-hash.d.ts.map +1 -1
  21. package/dist/manifest-hash.js +6 -2
  22. package/dist/workspace-manifest.d.ts +0 -1
  23. package/dist/workspace-manifest.d.ts.map +1 -1
  24. package/managed/raw/envrc +15 -0
  25. package/managed/raw/git-format-staged.yml +1 -0
  26. package/managed/raw/tooling/direnv/developer-links.ts +43 -11
  27. package/managed/raw/tooling/direnv/devenv.smoo.nix +5 -0
  28. package/managed/raw/tooling/direnv/inherited-devenv.ts +665 -0
  29. package/managed/raw/tooling/direnv/setup-environment.ts +123 -7
  30. package/nextest.toml +3 -37
  31. package/package.json +40 -3
  32. package/src/__tests__/counted-cargo-process.ts +286 -0
  33. package/src/__tests__/counted-cargo.ts +711 -0
  34. package/src/__tests__/fixture-cargo-home.ts +36 -0
  35. package/src/__tests__/fixture-nx-env.ts +93 -3
  36. package/src/bin/smoo-nx-exec.ts +3 -14
  37. package/src/bounded-test-policy.test.ts +0 -1
  38. package/src/cargo-closure-input.test.ts +313 -15
  39. package/src/cargo-closure-input.ts +42 -50
  40. package/src/cargo-resolve-cache.test.ts +975 -0
  41. package/src/cargo-source-hash.test.ts +17 -3
  42. package/src/cargo-source-hash.ts +447 -37
  43. package/src/cargo-workspace.ts +8 -8
  44. package/src/ensure-built.test.ts +122 -20
  45. package/src/ensure-built.ts +82 -5
  46. package/src/index.test.ts +203 -107
  47. package/src/index.ts +86 -115
  48. package/src/managed-assets.ts +12 -0
  49. package/src/managed-files/files.ts +6 -0
  50. package/src/manifest-hash.ts +7 -2
  51. package/src/nextest-partitions.test.ts +150 -0
  52. package/src/nx-cache-bound.test.ts +114 -0
  53. package/src/nx-resident-graph-workers.test.ts +121 -0
  54. package/src/nx-store-resolution.test.ts +98 -0
  55. package/src/task-history-namespace.test.ts +183 -0
  56. package/src/workspace-manifest.ts +0 -1
  57. package/test-timings.json +35 -0
  58. package/src/__tests__/in-process-nx-index.ts +0 -9
@@ -16,6 +16,7 @@ import {
16
16
  writeFileSync,
17
17
  } from 'node:fs';
18
18
  import { createRequire } from 'node:module';
19
+ import { homedir } from 'node:os';
19
20
  import path from 'node:path';
20
21
  import { parseArgs } from 'node:util';
21
22
  import { $ } from 'bun';
@@ -35,6 +36,12 @@ import {
35
36
  // above the repository and failing with "could not find a package.json".
36
37
  const devenvRoot = process.env.DEVENV_ROOT;
37
38
 
39
+ /** What a setup command printed, as captured. */
40
+ interface CapturedOutput {
41
+ readonly stdout: Uint8Array;
42
+ readonly stderr: Uint8Array;
43
+ }
44
+
38
45
  class CapturedCommandError extends Error {
39
46
  constructor(
40
47
  public readonly command: string,
@@ -121,6 +128,10 @@ const POST_COMMIT_BLOCK = [
121
128
  // Declared above the bootstrap block for the same hoisting reason as above.
122
129
  const { values: flags } = parseArgs({ options: { python: { type: 'string' } } });
123
130
 
131
+ // The Nx plugin's test for a Cargo workspace root manifest (CARGO_WORKSPACE_PATTERN), which
132
+ // cargoFetchInputs applies during the bootstrap block; declared above it for the same reason.
133
+ const CARGO_WORKSPACE = /^\s*\[workspace\]/m;
134
+
124
135
  // Go to project root
125
136
  process.chdir(projectRoot);
126
137
 
@@ -130,7 +141,9 @@ try {
130
141
  // and package resolution/Typia transforms are not available yet.
131
142
  const bunInputs = bunInstallInputs();
132
143
  const uvInputs = uvSyncInputs();
133
- recordInstallInputs([...bunInputs, ...uvInputs]);
144
+ const cargo = cargoFetchInputs(bunInputs);
145
+ recordInstallInputs([...bunInputs, ...uvInputs, ...cargo.inputs]);
146
+ const cargoFetches = cargo.workspaces.map(cargoFetcher);
134
147
  // A CI runner is GitHub Actions or Forgejo Actions, which mirrors every FORGEJO_*
135
148
  // variable as GITHUB_*. Not `CI`: agent harnesses set CI=true on every command
136
149
  // they run, and this branch installs unconditionally.
@@ -163,12 +176,14 @@ try {
163
176
  if (frozenError !== undefined) {
164
177
  console.error('git diff after install:');
165
178
  try {
166
- await runSetupCommand('git diff', $`git diff`, { quiet: false });
179
+ // Captured and written to stderr, the one stream devenv shows for a failed entry.
180
+ await writeCapturedOutput(await runSetupCommand('git diff', $`git diff`));
167
181
  } catch (diffError) {
168
182
  // This diff is diagnostics for the install failure we are already
169
183
  // reporting, so it must never become the failure: say it broke and keep
170
184
  // the original exit path. Plain `git diff` exits 0 even when the
171
- // lockfile drifted, so a non-zero here means git itself failed.
185
+ // lockfile drifted, so a failure here is git itself failing or stderr
186
+ // closing under the write.
172
187
  console.error(`! git diff failed: ${diffError instanceof Error ? diffError.message : String(diffError)}`);
173
188
  }
174
189
  process.exit(1);
@@ -176,6 +191,9 @@ try {
176
191
  if (uv !== null) {
177
192
  await uv.install({ quiet: false });
178
193
  }
194
+ for (const fetch of cargoFetches) {
195
+ await fetch.install({ quiet: false });
196
+ }
179
197
  });
180
198
  } else {
181
199
  // A local secret-resolution or install failure (a provider that is not
@@ -188,7 +206,9 @@ try {
188
206
  const uv = uvInstaller(uvInputs, { locked: false });
189
207
  const bun = bunInstaller(bunInputs);
190
208
  const installError = await withInstallLock(async () => {
191
- const pending = (uv === null ? [bun] : [bun, uv]).filter((installer) => !installer.isCurrent());
209
+ const pending = [bun, ...(uv === null ? [] : [uv]), ...cargoFetches].filter(
210
+ (installer) => !installer.isCurrent(),
211
+ );
192
212
  const error = await installLocalDependencies(pending);
193
213
  if (!pending.includes(bun)) {
194
214
  reportDeveloperLinks(projectRoot);
@@ -198,8 +218,13 @@ try {
198
218
  ensureTypeScriptApiPackage(projectRoot);
199
219
  }
200
220
  // Under the same lock: the repository config and hooks are one more thing two shell
201
- // entries would otherwise write at once.
202
- await applyWorkspaceGitConfig(projectRoot);
221
+ // entries would otherwise write at once. Hooks this shell may not write — a cowshed
222
+ // job's sandbox refuses `.git/hooks` — take the shell down no more than an install
223
+ // does: say so and load it.
224
+ await applyWorkspaceGitConfig(projectRoot).catch((configError: unknown) => {
225
+ describeFailure('WARNING', configError);
226
+ console.error('Git hooks and repository config were not applied; the shell is loaded without them.\n---');
227
+ });
203
228
  return error;
204
229
  });
205
230
  if (installError !== undefined) {
@@ -420,6 +445,41 @@ function uvInstaller(inputs: readonly string[], options: { locked: boolean }): I
420
445
  };
421
446
  }
422
447
 
448
+ /**
449
+ * `cargo fetch --locked` for one Cargo workspace. The Nx plugin infers every
450
+ * project's Cargo closure from locked, offline `cargo metadata`, which Cargo
451
+ * can answer only when the packages Cargo.lock pins are already in
452
+ * CARGO_HOME; without them the project graph refuses, and the graph's own
453
+ * `cargo-fetch` target can never run. This establishes that precondition the
454
+ * way `bun install` establishes node_modules.
455
+ *
456
+ * What it installs lives in CARGO_HOME, so the stamp lives there too, named
457
+ * by the digest of the workspace's Cargo.lock, manifest and Cargo
458
+ * configuration: replacing CARGO_HOME takes the stamps with it, and a
459
+ * copy-on-write clone of this checkout sharing that CARGO_HOME enters
460
+ * fetched. `--locked` makes a stale Cargo.lock fail here, in Cargo's words,
461
+ * rather than fetch a graph the lockfile does not name.
462
+ */
463
+ function cargoFetcher(workspace: string): Installer {
464
+ const manifest = path.posix.join(workspace, 'Cargo.toml');
465
+ const cargo = Bun.which('cargo');
466
+ const identity = ['cargo fetch --locked', cargo === null ? 'cargo not on PATH' : realpathSync(cargo)];
467
+ const digest = inputsDigest(identity, cargoWorkspaceInputs(workspace));
468
+ const cargoHome = path.resolve(process.env.CARGO_HOME ?? path.join(homedir(), '.cargo'));
469
+ const stampPath = path.join(cargoHome, 'smoo-fetched', digest);
470
+ return {
471
+ isCurrent: () => readInstallStamp(stampPath)?.inputs === digest,
472
+ install: async ({ quiet }) => {
473
+ await runSetupCommand(
474
+ `cargo fetch --locked --manifest-path ${manifest}`,
475
+ $`cargo fetch --locked --manifest-path ${manifest}`,
476
+ { quiet },
477
+ );
478
+ writeInstallStamp(stampPath, { inputs: digest });
479
+ },
480
+ };
481
+ }
482
+
423
483
  /**
424
484
  * Rewrite every `.pth` line in the environment's site-packages that names a
425
485
  * directory inside this checkout as a path relative to that site-packages,
@@ -489,6 +549,36 @@ function uvSyncInputs(): string[] {
489
549
  return [...new Set(['pyproject.toml', 'uv.lock', ...workspaceFiles(patterns, 'pyproject.toml')])].sort();
490
550
  }
491
551
 
552
+ /**
553
+ * The Cargo workspaces the Nx plugin resolves — a project directory (the root
554
+ * or a workspace member, found by its package.json) whose Cargo.toml declares
555
+ * `[workspace]` — and what their fetch reads. Every project directory's
556
+ * Cargo.toml is an input whether or not it exists, so adding a Cargo
557
+ * workspace is itself a change.
558
+ */
559
+ function cargoFetchInputs(bunInputs: readonly string[]): { workspaces: string[]; inputs: string[] } {
560
+ const workspaces: string[] = [];
561
+ const inputs: string[] = [];
562
+ for (const projectManifest of bunInputs.filter((input) => path.posix.basename(input) === 'package.json')) {
563
+ const directory = path.posix.dirname(projectManifest);
564
+ const manifest = path.posix.join(directory, 'Cargo.toml');
565
+ inputs.push(manifest);
566
+ const content = readFileIfPresent(path.join(projectRoot, manifest));
567
+ if (content !== null && CARGO_WORKSPACE.test(content.toString('utf8'))) {
568
+ workspaces.push(directory);
569
+ inputs.push(...cargoWorkspaceInputs(directory));
570
+ }
571
+ }
572
+ return { workspaces: workspaces.sort(), inputs: [...new Set(inputs)].sort() };
573
+ }
574
+
575
+ /** What `cargo fetch --locked` reads for the workspace at `directory`, relative to the project root. */
576
+ function cargoWorkspaceInputs(directory: string): string[] {
577
+ return ['Cargo.toml', 'Cargo.lock', '.cargo/config', '.cargo/config.toml'].map((file) =>
578
+ path.posix.join(directory, file),
579
+ );
580
+ }
581
+
492
582
  /**
493
583
  * A manifest that does not parse contributes no members, never a failure
494
584
  * here: its bytes are still an input, so the installer runs and reports the
@@ -717,6 +807,9 @@ function assertTypescriptApiAt(typescriptRoot: string, expectedTarget: string):
717
807
  * package graph exists — not the installer. Called under the install lock.
718
808
  */
719
809
  async function applyWorkspaceGitConfig(root: string): Promise<void> {
810
+ // A workspace job inherits the installed hooks, but cannot mutate .git/config
811
+ // or .git/hooks. Git wiring belongs to setup outside the job's sandbox.
812
+ if (process.env.COWSHED_WORKSPACE_TOKEN) return;
720
813
  await keepRepositoryConfig(root);
721
814
 
722
815
  const gitDirResult = await $`git rev-parse --git-dir`.cwd(root).quiet().nothrow();
@@ -810,7 +903,7 @@ async function runSetupCommand(
810
903
  command: string,
811
904
  shell: ReturnType<typeof $>,
812
905
  options: { quiet?: boolean; cwd?: string } = {},
813
- ): Promise<void> {
906
+ ): Promise<CapturedOutput> {
814
907
  const result = await shell
815
908
  .quiet(options.quiet ?? true)
816
909
  .nothrow()
@@ -818,6 +911,7 @@ async function runSetupCommand(
818
911
  if (result.exitCode !== 0) {
819
912
  throw new CapturedCommandError(command, result.exitCode, result.stdout, result.stderr);
820
913
  }
914
+ return result;
821
915
  }
822
916
 
823
917
  /**
@@ -999,3 +1093,25 @@ function replayCapturedOutput(error: unknown): void {
999
1093
  process.stderr.write(stderr);
1000
1094
  }
1001
1095
  }
1096
+
1097
+ /**
1098
+ * Writes what a setup command printed to stderr and returns once the bytes have
1099
+ * left this process. Stderr, not stdout, because devenv reports a failed shell
1100
+ * entry as "Shell environment capture failed:" plus the shell's stderr and
1101
+ * discards the stdout it captured. Awaited, because a pipe write still queued
1102
+ * when `process.exit` runs is cut short (Bun 1.4.2: 5 MB written to a slow
1103
+ * reader arrived as 1.3 MB), and a diagnostic that ends mid-hunk is not one.
1104
+ * Redacts what a replay redacts: the values this script resolved itself. A CI
1105
+ * runner resolves none, so the secrets its store injects are the platform's to
1106
+ * mask in the job log, as for every other line of it.
1107
+ */
1108
+ async function writeCapturedOutput(output: CapturedOutput): Promise<void> {
1109
+ for (const stream of [output.stdout, output.stderr]) {
1110
+ const masked = maskSecretValues(stream, resolvedSecretValues);
1111
+ if (masked.length > 0) {
1112
+ await new Promise<void>((resolve, reject) =>
1113
+ process.stderr.write(masked, (error) => (error ? reject(error) : resolve())),
1114
+ );
1115
+ }
1116
+ }
1117
+ }
package/nextest.toml CHANGED
@@ -9,40 +9,12 @@ slow-timeout = { period = "30s", terminate-after = 1 }
9
9
  # at test time. Filterset matches the class by test name rather than listing
10
10
  # today's two tests (`compile_fail_cases`, `lesser_capabilities_fail_to_compile_*`).
11
11
  #
12
- # 180s is provisional: measure on an idle machine with `--test-threads=1`.
12
+ # 180s is provisional: measure the test alone on an idle machine.
13
13
  # An unbounded run phase is how a hung test becomes a hung lane.
14
14
  [[profile.default.overrides]]
15
15
  filter = 'test(/compile_fail|fail_to_compile/)'
16
16
  slow-timeout = { period = "180s", terminate-after = 1 }
17
17
 
18
- # Tests that drive the host's REAL APFS stack — creating, attaching, mounting and
19
- # detaching actual disk images — contend on a single machine-wide serialization
20
- # point: Disk Arbitration and the APFS driver. Measured on this host, a read-only
21
- # `diskutil apfs list -plist` costs 0.7s idle at 33 attached images but up to
22
- # 14.4s while another process is attaching/detaching. Run in parallel these tests
23
- # manufacture exactly that contention for each other, so each one's wall time
24
- # becomes a function of how many siblings happen to be mid-attach — a test whose
25
- # verdict depends on that is measuring the scheduler, not the code.
26
- #
27
- # `max-threads = 1` makes the group a mutex across the WHOLE run (groups are
28
- # run-wide, not per-binary, so the lib unit tests and the apfs_integration binary
29
- # serialize against each other as intended). The other ~830 tests stay parallel.
30
- #
31
- # This bounds contention; it does NOT grant more time. The per-test deadline above
32
- # is unchanged and each of these tests is expected to fit it on its own merits.
33
- # Membership is by name: `real_apfs_*` is the class marker, so a new real-APFS
34
- # test joins the group by being named for what it touches. A bare `real_*` filter
35
- # would be wrong here — unrelated `real_*` tests exist in columine-arrow,
36
- # cowshed-cli and cowshed-gateway that touch no disk image at all.
37
- #
38
- # The `@tool:smoo:` prefix is not decoration: this file is passed as
39
- # `--tool-config-file smoo:...` so a repository's own `.config/nextest.toml`
40
- # can override it, and nextest REFUSES a tool config whose groups are not
41
- # namespaced ("test groups must start with '@tool:<tool-name>:'") precisely so
42
- # a tool cannot collide with a repository's group of the same name.
43
- [test-groups]
44
- "@tool:smoo:real-apfs" = { max-threads = 1 }
45
-
46
18
  # The full-lifecycle real-APFS tests cannot meet the default deadline, and
47
19
  # `specs/cowshed/08_testing.md` already contains the arithmetic that proves it.
48
20
  #
@@ -71,15 +43,9 @@ slow-timeout = { period = "180s", terminate-after = 1 }
71
43
  # bounded because an unbounded run phase is how a hung test becomes a hung lane.
72
44
  # Re-derive it if the idle measurements in `apfs_integration.rs` change.
73
45
  #
74
- # Membership is by name and narrower than the group: the fast `real_apfs_*` unit
46
+ # Membership is by name and narrower than the class: the fast `real_apfs_*` unit
75
47
  # tests in the lib keep the 30s default — they passed at 4.4s and 7.3s and have no
76
- # claim on more. This override precedes the group-only one so it wins the
77
- # first-match for both keys it sets.
48
+ # claim on more.
78
49
  [[profile.default.overrides]]
79
50
  filter = 'test(/real_apfs_.*_substrate_lifecycle/)'
80
51
  slow-timeout = { period = "60s", terminate-after = 1 }
81
- test-group = '@tool:smoo:real-apfs'
82
-
83
- [[profile.default.overrides]]
84
- filter = 'test(/real_apfs_/)'
85
- test-group = '@tool:smoo:real-apfs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoothbricks/nx-plugin",
3
- "version": "0.4.24",
3
+ "version": "0.4.26",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "main": "./loader.js",
@@ -153,9 +153,45 @@
153
153
  "targets": {
154
154
  "lint": {},
155
155
  "test": {
156
+ "executor": "nx:noop",
157
+ "dependsOn": [
158
+ "test-shard1",
159
+ "test-shard2",
160
+ "test-shard3",
161
+ "test-shard4"
162
+ ]
163
+ },
164
+ "test-shard1": {
156
165
  "executor": "@smoothbricks/nx-plugin:bounded-exec",
157
166
  "options": {
158
- "command": "bun test --timeout=30000",
167
+ "command": "bun test --timeout=30000 --timings=test-timings.json --shard=1/4",
168
+ "cwd": "{projectRoot}",
169
+ "timeoutMs": 120000,
170
+ "killAfterMs": 10000
171
+ }
172
+ },
173
+ "test-shard2": {
174
+ "executor": "@smoothbricks/nx-plugin:bounded-exec",
175
+ "options": {
176
+ "command": "bun test --timeout=30000 --timings=test-timings.json --shard=2/4",
177
+ "cwd": "{projectRoot}",
178
+ "timeoutMs": 120000,
179
+ "killAfterMs": 10000
180
+ }
181
+ },
182
+ "test-shard3": {
183
+ "executor": "@smoothbricks/nx-plugin:bounded-exec",
184
+ "options": {
185
+ "command": "bun test --timeout=30000 --timings=test-timings.json --shard=3/4",
186
+ "cwd": "{projectRoot}",
187
+ "timeoutMs": 120000,
188
+ "killAfterMs": 10000
189
+ }
190
+ },
191
+ "test-shard4": {
192
+ "executor": "@smoothbricks/nx-plugin:bounded-exec",
193
+ "options": {
194
+ "command": "bun test --timeout=30000 --timings=test-timings.json --shard=4/4",
159
195
  "cwd": "{projectRoot}",
160
196
  "timeoutMs": 120000,
161
197
  "killAfterMs": 10000
@@ -171,6 +207,7 @@
171
207
  "dist",
172
208
  "src",
173
209
  "nextest.toml",
210
+ "test-timings.json",
174
211
  "README.md",
175
212
  "loader.js",
176
213
  "generators.json",
@@ -197,7 +234,7 @@
197
234
  "yaml": "^2.9.0"
198
235
  },
199
236
  "devDependencies": {
200
- "@smoothbricks/lmao": "0.5.2",
237
+ "@smoothbricks/lmao": "0.5.4",
201
238
  "@smoothbricks/validation": "0.1.10",
202
239
  "fast-check": "^4.4.0"
203
240
  }
@@ -0,0 +1,286 @@
1
+ /**
2
+ * The processes behind the counted Cargo (`counted-cargo.ts`): every `cargo metadata` child, and the
3
+ * process a child leaves running behind it when asked to, as a real Cargo leaves rustc. Each holds one
4
+ * control connection to the fixture's broker in the test process for its whole life and reports its
5
+ * life over it; the broker answers each step when the test lets it. Nothing here waits on a clock:
6
+ * a child is held by a read on its connection, and a process's death is its connection's EOF.
7
+ *
8
+ * The protocol is a line per message, the broker's payload text JSON-quoted:
9
+ *
10
+ * child → broker start <pid> | read <grandchild pid | -> | end <exit status>
11
+ * grandchild → broker grandchild <pid> <parent pid>
12
+ * broker → child read <0|1 leave a grandchild> <canned answer | null> | answer <failure stderr | null>
13
+ *
14
+ * The broker never speaks to a grandchild; it ends a grandchild's connection on `release`, and a
15
+ * grandchild lives exactly until its connection ends.
16
+ */
17
+ import { spawn } from 'node:child_process';
18
+ import { closeSync, openSync, writeSync } from 'node:fs';
19
+ import { connect, type Socket } from 'node:net';
20
+ import { constants } from 'node:os';
21
+ import { join } from 'node:path';
22
+ import { fileURLToPath } from 'node:url';
23
+
24
+ /** What a `cargo metadata` child or a grandchild tells the broker. */
25
+ export type ProcessReport =
26
+ | { readonly kind: 'start'; readonly pid: number }
27
+ | { readonly kind: 'read'; readonly grandchild: number | null }
28
+ | { readonly kind: 'end'; readonly status: number }
29
+ | { readonly kind: 'grandchild'; readonly pid: number; readonly parent: number };
30
+
31
+ /** What the broker tells a `cargo metadata` child, each when its gate lets it. */
32
+ export type BrokerOrder =
33
+ | { readonly kind: 'read'; readonly grandchild: boolean; readonly canned: string | null }
34
+ | { readonly kind: 'answer'; readonly failure: string | null };
35
+
36
+ export const BROKER_HOST = '127.0.0.1';
37
+
38
+ export function encodeReport(report: ProcessReport): string {
39
+ switch (report.kind) {
40
+ case 'start':
41
+ return `start ${report.pid}\n`;
42
+ case 'read':
43
+ return `read ${report.grandchild ?? '-'}\n`;
44
+ case 'end':
45
+ return `end ${report.status}\n`;
46
+ case 'grandchild':
47
+ return `grandchild ${report.pid} ${report.parent}\n`;
48
+ }
49
+ }
50
+
51
+ export function encodeOrder(order: BrokerOrder): string {
52
+ switch (order.kind) {
53
+ case 'read':
54
+ return `read ${order.grandchild ? 1 : 0} ${JSON.stringify(order.canned)}\n`;
55
+ case 'answer':
56
+ return `answer ${JSON.stringify(order.failure)}\n`;
57
+ }
58
+ }
59
+
60
+ function integer(text: string | undefined, what: string): number | Error {
61
+ const value = Number(text);
62
+ return text !== undefined && text !== '' && Number.isSafeInteger(value) && value >= 0
63
+ ? value
64
+ : new Error(`${what} is not a non-negative integer: ${JSON.stringify(text)}`);
65
+ }
66
+
67
+ function text(json: string): string | null | Error {
68
+ let value: unknown;
69
+ try {
70
+ value = JSON.parse(json);
71
+ } catch (error) {
72
+ return new Error(`payload is not JSON: ${json}`, { cause: error });
73
+ }
74
+ return typeof value === 'string' || value === null ? value : new Error(`expected a JSON string or null, got ${json}`);
75
+ }
76
+
77
+ export function parseReport(line: string): ProcessReport | Error {
78
+ const [kind, first, second, ...extra] = line.split(' ');
79
+ if (extra.length > 0 || (kind === 'grandchild') === (second === undefined)) {
80
+ return new Error(`malformed report ${JSON.stringify(line)}`);
81
+ }
82
+ switch (kind) {
83
+ case 'start': {
84
+ const pid = integer(first, 'start pid');
85
+ return pid instanceof Error ? pid : { kind: 'start', pid };
86
+ }
87
+ case 'read': {
88
+ if (first === '-') return { kind: 'read', grandchild: null };
89
+ const grandchild = integer(first, 'grandchild pid');
90
+ return grandchild instanceof Error ? grandchild : { kind: 'read', grandchild };
91
+ }
92
+ case 'end': {
93
+ const status = integer(first, 'exit status');
94
+ return status instanceof Error ? status : { kind: 'end', status };
95
+ }
96
+ case 'grandchild': {
97
+ const pid = integer(first, 'grandchild pid');
98
+ if (pid instanceof Error) return pid;
99
+ const parent = integer(second, 'parent pid');
100
+ return parent instanceof Error ? parent : { kind: 'grandchild', pid, parent };
101
+ }
102
+ default:
103
+ return new Error(`unknown report ${JSON.stringify(line)}`);
104
+ }
105
+ }
106
+
107
+ export function parseOrder(line: string): BrokerOrder | Error {
108
+ if (line.startsWith('read ')) {
109
+ const flag = line.slice(5, 6);
110
+ if ((flag !== '0' && flag !== '1') || line[6] !== ' ') return new Error(`malformed order ${JSON.stringify(line)}`);
111
+ const canned = text(line.slice(7));
112
+ return canned instanceof Error ? canned : { kind: 'read', grandchild: flag === '1', canned };
113
+ }
114
+ if (line.startsWith('answer ')) {
115
+ const failure = text(line.slice(7));
116
+ return failure instanceof Error ? failure : { kind: 'answer', failure };
117
+ }
118
+ return new Error(`unknown order ${JSON.stringify(line)}`);
119
+ }
120
+
121
+ /**
122
+ * Calls `onLine` with each newline-terminated line `socket` delivers, in order. Returns what has
123
+ * arrived since the last newline: a peer that died mid-line leaves it there for diagnostics.
124
+ */
125
+ export function readLines(socket: Socket, onLine: (line: string) => void): () => string {
126
+ let pending: Buffer = Buffer.alloc(0);
127
+ socket.on('data', (chunk: Buffer) => {
128
+ let data = pending.length === 0 ? chunk : Buffer.concat([pending, chunk]);
129
+ for (let newline = data.indexOf(10); newline !== -1; newline = data.indexOf(10)) {
130
+ onLine(data.toString('utf8', 0, newline));
131
+ data = data.subarray(newline + 1);
132
+ }
133
+ pending = data;
134
+ });
135
+ return () => pending.toString('utf8');
136
+ }
137
+
138
+ /** One process's end, said on its stderr: the parent that reads it, or the log a grandchild writes. */
139
+ function fail(message: string): never {
140
+ writeSync(2, `counted cargo process ${process.pid}: ${message}\n`);
141
+ process.exit(1);
142
+ }
143
+
144
+ function connectToBroker(port: number): Promise<Socket> {
145
+ return new Promise((resolve, reject) => {
146
+ const socket = connect({ host: BROKER_HOST, port });
147
+ socket.once('error', reject);
148
+ socket.once('connect', () => {
149
+ socket.off('error', reject);
150
+ resolve(socket);
151
+ });
152
+ });
153
+ }
154
+
155
+ /** The broker's orders, one at a time, in the order they arrive; `null` once the broker has hung up. */
156
+ function orders(socket: Socket): () => Promise<BrokerOrder | null> {
157
+ const arrived: (BrokerOrder | Error)[] = [];
158
+ const waiting: ((order: BrokerOrder | Error | null) => void)[] = [];
159
+ let closed = false;
160
+ readLines(socket, (line) => {
161
+ const order = parseOrder(line);
162
+ const waiter = waiting.shift();
163
+ if (waiter === undefined) arrived.push(order);
164
+ else waiter(order);
165
+ });
166
+ socket.on('close', () => {
167
+ closed = true;
168
+ for (const waiter of waiting.splice(0)) waiter(null);
169
+ });
170
+ socket.on('error', (error) => fail(`connection to the broker failed: ${error.message}`));
171
+ return async () => {
172
+ const next =
173
+ arrived.shift() ??
174
+ (closed
175
+ ? null
176
+ : await new Promise<BrokerOrder | Error | null>((resolve) => {
177
+ waiting.push(resolve);
178
+ }));
179
+ if (next instanceof Error) fail(next.message);
180
+ return next;
181
+ };
182
+ }
183
+
184
+ function write(stream: NodeJS.WritableStream, data: string | Buffer): Promise<void> {
185
+ return new Promise((resolve, reject) => {
186
+ stream.write(data, (error) => (error ? reject(error) : resolve()));
187
+ });
188
+ }
189
+
190
+ /** What the real `cargo metadata` prints, and the status it exits with; its stderr is this child's. */
191
+ function runReal(real: string, args: readonly string[]): Promise<{ stdout: Buffer; status: number }> {
192
+ return new Promise((resolve, reject) => {
193
+ const cargo = spawn(real, args, { stdio: ['ignore', 'pipe', 'inherit'] });
194
+ const chunks: Buffer[] = [];
195
+ cargo.stdout.on('data', (chunk: Buffer) => chunks.push(chunk));
196
+ cargo.once('error', reject);
197
+ cargo.once('close', (code, signal) =>
198
+ resolve({
199
+ stdout: Buffer.concat(chunks),
200
+ status: code ?? 128 + (signal === null ? 0 : constants.signals[signal]),
201
+ }),
202
+ );
203
+ });
204
+ }
205
+
206
+ /**
207
+ * A process left running in this child's process group, as real Cargo leaves rustc. Its output goes
208
+ * to a log in the fixture, never this child's pipes, so it holds nothing of Cargo's open.
209
+ */
210
+ function leaveGrandchild(port: number, directory: string): number {
211
+ const log = openSync(join(directory, `grandchild-of-${process.pid}.log`), 'a');
212
+ try {
213
+ const program = fileURLToPath(import.meta.url);
214
+ const grandchild = spawn(process.execPath, [program, 'grandchild', String(port), String(process.pid)], {
215
+ stdio: ['ignore', log, log],
216
+ });
217
+ const pid = grandchild.pid;
218
+ if (pid === undefined) fail('the grandchild did not start');
219
+ grandchild.once('error', (error) => fail(`the grandchild failed: ${error.message}`));
220
+ grandchild.unref();
221
+ return pid;
222
+ } finally {
223
+ closeSync(log);
224
+ }
225
+ }
226
+
227
+ /** One `cargo metadata` child: reports its life, and takes each step only when the broker orders it. */
228
+ async function metadataChild(port: number, real: string, directory: string, args: readonly string[]): Promise<void> {
229
+ const socket = await connectToBroker(port);
230
+ const next = orders(socket);
231
+ socket.write(encodeReport({ kind: 'start', pid: process.pid }));
232
+
233
+ const read = await next();
234
+ if (read === null) fail('the broker hung up before letting this child read the workspace');
235
+ if (read.kind !== 'read') fail(`expected the order to read, got ${read.kind}`);
236
+ const produced = read.canned === null ? await runReal(real, args) : { stdout: Buffer.from(read.canned), status: 0 };
237
+ const grandchild = read.grandchild ? leaveGrandchild(port, directory) : null;
238
+ socket.write(encodeReport({ kind: 'read', grandchild }));
239
+
240
+ const answer = await next();
241
+ if (answer === null) fail('the broker hung up before letting this child answer');
242
+ if (answer.kind !== 'answer') fail(`expected the order to answer, got ${answer.kind}`);
243
+ let status = produced.status;
244
+ if (answer.failure === null) {
245
+ await write(process.stdout, produced.stdout);
246
+ } else {
247
+ await write(process.stderr, answer.failure);
248
+ status = 101;
249
+ }
250
+ socket.end(encodeReport({ kind: 'end', status }));
251
+ // The process ends when the connection is done and the event loop drains, with every byte written.
252
+ process.exitCode = status;
253
+ }
254
+
255
+ /** A grandchild: registers, and lives exactly as long as its connection to the broker. */
256
+ async function grandchild(port: number, parent: number): Promise<void> {
257
+ const socket = await connectToBroker(port);
258
+ socket.on('error', (error) => fail(`connection to the broker failed: ${error.message}`));
259
+ socket.on('data', (chunk: Buffer) => fail(`the broker spoke to a grandchild: ${JSON.stringify(String(chunk))}`));
260
+ socket.write(encodeReport({ kind: 'grandchild', pid: process.pid, parent }));
261
+ }
262
+
263
+ async function main([role, portText, ...rest]: readonly string[]): Promise<void> {
264
+ const port = integer(portText, 'broker port');
265
+ if (port instanceof Error) fail(port.message);
266
+ switch (role) {
267
+ case 'metadata': {
268
+ const [real, directory, ...args] = rest;
269
+ if (real === undefined || directory === undefined) fail('usage: metadata <port> <real> <directory> <args...>');
270
+ return metadataChild(port, real, directory, args);
271
+ }
272
+ case 'grandchild': {
273
+ const parent = integer(rest[0], 'parent pid');
274
+ if (parent instanceof Error) fail(parent.message);
275
+ return grandchild(port, parent);
276
+ }
277
+ default:
278
+ fail(`unknown role ${JSON.stringify(role)}`);
279
+ }
280
+ }
281
+
282
+ if (import.meta.main) {
283
+ await main(process.argv.slice(2)).catch((error: unknown) =>
284
+ fail(error instanceof Error ? (error.stack ?? error.message) : String(error)),
285
+ );
286
+ }