@smoothbricks/nx-plugin 0.5.0 → 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 (34) hide show
  1. package/README.md +52 -35
  2. package/dist/ensure-built.d.ts.map +1 -1
  3. package/dist/ensure-built.js +14 -9
  4. package/dist/executors/bounded-exec/cpu-pool.d.ts +14 -7
  5. package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -1
  6. package/dist/executors/bounded-exec/cpu-pool.js +297 -38
  7. package/dist/executors/bounded-exec/cpu-tokens.d.ts +19 -7
  8. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
  9. package/dist/executors/bounded-exec/cpu-tokens.js +21 -7
  10. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  11. package/dist/executors/bounded-exec/executor.js +12 -6
  12. package/dist/executors/bounded-exec/run-cpu-pool.d.ts.map +1 -1
  13. package/dist/executors/bounded-exec/run-cpu-pool.js +5 -4
  14. package/dist/producer-receipts.d.ts +10 -0
  15. package/dist/producer-receipts.d.ts.map +1 -1
  16. package/dist/producer-receipts.js +30 -5
  17. package/managed/raw/tooling/direnv/devenv.smoo.nix +1 -3
  18. package/managed/raw/tooling/direnv/linked-snapshots.ts +18 -8
  19. package/managed/raw/tooling/direnv/repo-path +1 -1
  20. package/package.json +1 -1
  21. package/src/__tests__/counted-cargo.ts +1 -1
  22. package/src/cargo-closure-input.test.ts +1 -1
  23. package/src/ensure-built.test.ts +39 -0
  24. package/src/ensure-built.ts +23 -9
  25. package/src/executors/bounded-exec/cpu-pool.test.ts +132 -4
  26. package/src/executors/bounded-exec/cpu-pool.ts +300 -46
  27. package/src/executors/bounded-exec/cpu-tokens.test.ts +60 -15
  28. package/src/executors/bounded-exec/cpu-tokens.ts +34 -11
  29. package/src/executors/bounded-exec/executor.test.ts +72 -4
  30. package/src/executors/bounded-exec/executor.ts +12 -6
  31. package/src/executors/bounded-exec/run-cpu-pool.test.ts +3 -0
  32. package/src/executors/bounded-exec/run-cpu-pool.ts +5 -4
  33. package/src/producer-receipts.test.ts +37 -15
  34. package/src/producer-receipts.ts +29 -5
@@ -29,13 +29,22 @@ export type RunnerKind =
29
29
  | 'process';
30
30
 
31
31
  /**
32
- * What a runner asks the budget for. Cargo also carries its independent test-thread ceiling; cargo-mutants carries
33
- * how many cargo processes it runs at once, which share its grant.
32
+ * The inherited ceilings, lowered by configuration, of test threads, which take no make jobserver token: libtest's
33
+ * `RUST_TEST_THREADS` and nextest's `NEXTEST_TEST_THREADS`. Null where neither environment names one.
34
+ */
35
+ export interface TestThreadCeilings {
36
+ readonly rust: number | null;
37
+ readonly nextest: number | null;
38
+ }
39
+
40
+ /**
41
+ * What a runner asks the budget for. A cargo build, and a process whose script may start Cargo, carry the test-thread
42
+ * ceilings their sizing keeps; cargo-mutants carries how many cargo processes it runs at once, which share its grant.
34
43
  */
35
44
  export type Demand =
36
- | { kind: 'cargo'; want: number; testThreads: number | null }
45
+ | { kind: 'cargo' | 'process'; want: number; testThreads: TestThreadCeilings }
37
46
  | { kind: 'cargo-mutants'; want: number; jobs: number }
38
- | { kind: Exclude<RunnerKind, 'cargo' | 'cargo-mutants'>; want: number };
47
+ | { kind: Exclude<RunnerKind, 'cargo' | 'cargo-mutants' | 'process'>; want: number };
39
48
 
40
49
  /** Every budgeted command learns its grant here, so a script that starts a runner can size it. */
41
50
  export const CPU_TOKENS_ENV = 'BOUNDED_EXEC_CPU_TOKENS';
@@ -138,7 +147,15 @@ export function runnerDemand(
138
147
  const want = Math.max(1, Math.min(requested, ceiling ?? requested));
139
148
  switch (kind) {
140
149
  case 'cargo':
141
- return { kind, want, testThreads: inheritedCeiling(inherited.RUST_TEST_THREADS, configured?.RUST_TEST_THREADS) };
150
+ case 'process':
151
+ return {
152
+ kind,
153
+ want,
154
+ testThreads: {
155
+ rust: inheritedCeiling(inherited.RUST_TEST_THREADS, configured?.RUST_TEST_THREADS),
156
+ nextest: inheritedCeiling(inherited.NEXTEST_TEST_THREADS, configured?.NEXTEST_TEST_THREADS),
157
+ },
158
+ };
142
159
  case 'cargo-mutants':
143
160
  return { kind, want, jobs };
144
161
  default:
@@ -259,8 +276,12 @@ function withSizedFlag(command: string, invocation: RegExp, flag: RegExp, sized:
259
276
  }
260
277
 
261
278
  /**
262
- * Size the runner to the lesser of its bounded demand and grant, retaining Cargo's test-thread ceiling.
263
- * Rewrite named flags so they cannot outrank that ceiling. `CPU_TOKENS_ENV` still reports the actual held grant.
279
+ * Size the runner to the lesser of its bounded demand and grant, keeping the test-thread ceilings. Rewrite named flags
280
+ * so they cannot outrank that size. `CPU_TOKENS_ENV` still reports the actual held grant.
281
+ *
282
+ * Cargo's rustc count is the pool's: the executor's sub-pool exports its jobserver fifo, so no `CARGO_BUILD_JOBS`
283
+ * comes from the grant (specs/cowshed/05_gateway.md, "The jobserver fifo"). Test threads take no jobserver token, so a
284
+ * cargo build's test threads are its size, and a process's, whose script may start Cargo, are its whole held grant.
264
285
  */
265
286
  export function sizeRunner(
266
287
  demand: Demand,
@@ -279,14 +300,18 @@ export function sizeRunner(
279
300
  case 'bun-parallel':
280
301
  return { command: command.replace(BUN_PARALLEL, `--parallel=${count}`), env: { [CPU_TOKENS_ENV]: held } };
281
302
  case 'cargo':
303
+ case 'process': {
304
+ const threads = demand.kind === 'cargo' ? runnerCount : tokens;
305
+ const { rust, nextest } = demand.testThreads;
282
306
  return {
283
307
  command,
284
308
  env: {
285
309
  [CPU_TOKENS_ENV]: held,
286
- CARGO_BUILD_JOBS: count,
287
- RUST_TEST_THREADS: String(Math.min(runnerCount, demand.testThreads ?? runnerCount)),
310
+ RUST_TEST_THREADS: String(Math.min(threads, rust ?? threads)),
311
+ NEXTEST_TEST_THREADS: String(Math.min(threads, nextest ?? threads)),
288
312
  },
289
313
  };
314
+ }
290
315
  case 'stryker':
291
316
  case 'vitest':
292
317
  case 'go-test': {
@@ -313,8 +338,6 @@ export function sizeRunner(
313
338
  },
314
339
  };
315
340
  }
316
- case 'process':
317
- return { command, env: { [CPU_TOKENS_ENV]: held } };
318
341
  }
319
342
  }
320
343
 
@@ -1,4 +1,4 @@
1
- import { afterEach, describe, expect, it, jest } from 'bun:test';
1
+ import { afterAll, afterEach, beforeAll, describe, expect, it, jest } from 'bun:test';
2
2
  import { spawnSync } from 'node:child_process';
3
3
  import { access, mkdir, mkdtemp, open, readFile, rm, writeFile } from 'node:fs/promises';
4
4
  import { createServer } from 'node:net';
@@ -8,7 +8,7 @@ import { onProgressClock } from '../../__tests__/progress-clock.js';
8
8
  import { BOUNDED_TEST_IDLE_TIMEOUT_MS } from '../../bounded-test-policy.js';
9
9
  import { ownedSocketDirectory } from '../../testing.js';
10
10
  import { CPU_POOL_ENV, type CpuPool, createCpuPool, type InheritedCpuPool, readInheritedCpuPool } from './cpu-pool.js';
11
- import { CPU_TOKENS_ENV, type CpuBudget, parseInheritedTokens, type TokenGrant } from './cpu-tokens.js';
11
+ import { CPU_TOKENS_ENV, type CpuBudget, type TokenGrant } from './cpu-tokens.js';
12
12
  import {
13
13
  type BoundedExecContext,
14
14
  type BoundedExecResult,
@@ -28,7 +28,26 @@ const ownedProcessGroups = new Set<number>();
28
28
  const originalStdoutWrite = process.stdout.write;
29
29
  const originalStderrWrite = process.stderr.write;
30
30
 
31
- const nextestWant = Math.min(8, parseInheritedTokens(process.env.NEXTEST_TEST_THREADS) ?? 8);
31
+ /** What the nextest cases declare as `parallelism`, and so what they ask: the suite runs under no inherited ceiling. */
32
+ const nextestWant = 8;
33
+
34
+ /**
35
+ * Every sizing variable a holder exports to its command (specs/cowshed/05_gateway.md, "Sized to the grant", "Cargo reads
36
+ * its holder's jobserver"). This suite's own process runs under them when a gate's `bounded-exec` started it, and the
37
+ * executor reads them as inherited ceilings and pool beside the ones its cases construct, so the suite clears them
38
+ * (specs/cowshed/08_testing.md: fixtures clear every inherited sizing variable before they set their own).
39
+ */
40
+ const INHERITED_SIZING_ENV = [
41
+ CPU_POOL_ENV,
42
+ CPU_TOKENS_ENV,
43
+ 'NEXTEST_TEST_THREADS',
44
+ 'RUST_TEST_THREADS',
45
+ 'CARGO_BUILD_JOBS',
46
+ 'CARGO_MAKEFLAGS',
47
+ 'MAKEFLAGS',
48
+ 'MFLAGS',
49
+ ] as const;
50
+ const inheritedSizing = INHERITED_SIZING_ENV.map((name) => [name, process.env[name]] as const);
32
51
 
33
52
  /**
34
53
  * Prints the budget variables it sees, then what a grandchild sees: a command's environment reaches its descendants,
@@ -41,6 +60,19 @@ console.log(execFileSync('node', ['-p', read]).toString().trim());
41
60
  `;
42
61
 
43
62
  describe('@smoothbricks/nx-plugin:bounded-exec', () => {
63
+ beforeAll(() => {
64
+ for (const name of INHERITED_SIZING_ENV) {
65
+ delete process.env[name];
66
+ }
67
+ });
68
+
69
+ afterAll(() => {
70
+ for (const [name, value] of inheritedSizing) {
71
+ if (value === undefined) delete process.env[name];
72
+ else process.env[name] = value;
73
+ }
74
+ });
75
+
44
76
  afterEach(async () => {
45
77
  process.stdout.write = originalStdoutWrite;
46
78
  process.stderr.write = originalStderrWrite;
@@ -931,9 +963,11 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
931
963
  return heldGrant(tokens, released);
932
964
  },
933
965
  };
966
+ // Cargo's ceiling bounds its ask; its rustc count is then the sub-pool's jobserver fifo, and its test threads
967
+ // are sized under their own ceiling, never to the target's 64.
934
968
  for (const scenario of [
935
969
  { runner: 'nextest run', variable: 'NEXTEST_TEST_THREADS', cap: 6 },
936
- { runner: 'cargo test', variable: 'CARGO_BUILD_JOBS', cap: 4 },
970
+ { runner: 'cargo test', variable: 'RUST_TEST_THREADS', cap: 4 },
937
971
  ]) {
938
972
  const command = `node -e "console.log('cap=' + process.env.${scenario.variable} + ' held=' + process.env.BOUNDED_EXEC_CPU_TOKENS)" # ${scenario.runner}`;
939
973
  const result = await runBoundedExec(
@@ -964,6 +998,40 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
964
998
  }
965
999
  });
966
1000
 
1001
+ it("names the run's own jobserver fifo to its command, never one the target's env names", async () => {
1002
+ const workspace = await createWorkspace();
1003
+ const released: number[] = [];
1004
+ const injected = '-j --jobserver-auth=fifo:/injected';
1005
+ const command = `node -e "console.log(JSON.stringify([process.env.CARGO_MAKEFLAGS, process.env.MAKEFLAGS, process.env.${CPU_POOL_ENV}]))"`;
1006
+ const result = await runBoundedExec(
1007
+ {
1008
+ command,
1009
+ timeoutMs: BOUNDED_TEST_IDLE_TIMEOUT_MS,
1010
+ env: { CARGO_MAKEFLAGS: injected, MAKEFLAGS: injected, MFLAGS: injected },
1011
+ },
1012
+ workspace.context,
1013
+ createProcessTreeKiller(),
1014
+ null,
1015
+ {
1016
+ async take() {
1017
+ return heldGrant(2, released);
1018
+ },
1019
+ },
1020
+ );
1021
+ expect(result.success).toBe(true);
1022
+ for (const name of ['CARGO_MAKEFLAGS', 'MAKEFLAGS', 'MFLAGS']) {
1023
+ expect(result.terminalOutput).toContain(
1024
+ `cowshed: ${name} in the target's env is ignored: the CPU budget sets it`,
1025
+ );
1026
+ }
1027
+ // Cargo and make both read the fifo beside the sub-pool's socket that this run serves its descendants.
1028
+ expect(result.terminalOutput).toMatch(
1029
+ /^\["-j --jobserver-auth=fifo:(\/\S+)\/j","-j --jobserver-auth=fifo:\1\/j","\1\/p\.sock"\]$/m,
1030
+ );
1031
+ expect(result.terminalOutput).not.toContain('/injected');
1032
+ expect(released).toEqual([2]);
1033
+ });
1034
+
967
1035
  it('starts the runner once granted, sized to its grant, and returns the tokens when it exits', async () => {
968
1036
  const workspace = await createWorkspace();
969
1037
  const asked: { want: number; checkout: string; command: string }[] = [];
@@ -64,8 +64,14 @@ const EXIT_CODE_BY_SIGNAL: Partial<Record<NodeJS.Signals, number>> = {
64
64
  SIGTERM: 143,
65
65
  };
66
66
 
67
- /** The variables the CPU budget sets for the command it grants: no target's environment may set them. */
68
- const BUDGET_ENV = [CPU_TOKENS_ENV, CPU_POOL_ENV];
67
+ /** The pool descriptor: only a grant may give it, so neither the target's nor the inherited environment passes it on. */
68
+ const POOL_ENV = [CPU_TOKENS_ENV, CPU_POOL_ENV];
69
+ /**
70
+ * The variables the CPU budget sets for the command it grants: no target's environment may set them. A jobserver the
71
+ * executor itself inherits, a parent make's, still reaches an ungranted command by Cargo's own rule; a granted one's
72
+ * sub-pool names its own fifo over it.
73
+ */
74
+ const BUDGET_ENV = [...POOL_ENV, 'CARGO_MAKEFLAGS', 'MAKEFLAGS', 'MFLAGS'];
69
75
 
70
76
  export interface BoundedExecContext {
71
77
  root: string;
@@ -859,14 +865,14 @@ function buildCommand(options: BoundedExecOptions): string {
859
865
 
860
866
  /** The lease's directory is the command's TMPDIR unless the target names one itself. */
861
867
  function mergeEnv(lease: RamTempLease | null, env: Record<string, string>): NodeJS.ProcessEnv {
862
- const inherited = withoutBudgetEnv(process.env);
868
+ const inherited = withoutPoolEnv(process.env);
863
869
  return lease === null ? { ...inherited, ...env } : { ...inherited, TMPDIR: lease.directory, ...env };
864
870
  }
865
871
 
866
- /** The process's environment without the CPU budget's variables: a pool and its count only a grant may give. */
867
- function withoutBudgetEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
872
+ /** The process's environment without the CPU pool's descriptor: a pool and its count only a grant may give. */
873
+ function withoutPoolEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
868
874
  const kept = { ...env };
869
- for (const name of BUDGET_ENV) {
875
+ for (const name of POOL_ENV) {
870
876
  delete kept[name];
871
877
  }
872
878
  return kept;
@@ -31,6 +31,9 @@ describe('a run without a gateway', () => {
31
31
  throw new Error(`the run published no pool: ${JSON.stringify(env)}`);
32
32
  }
33
33
  expect(pool.tokens).toBe(4);
34
+ // A Cargo that Nx's own `nx:run-commands` starts reads the same count through the run's jobserver fifo.
35
+ expect(env.CARGO_MAKEFLAGS).toMatch(/^-j --jobserver-auth=fifo:\/\S+$/);
36
+ expect(env.MAKEFLAGS).toBe(env.CARGO_MAKEFLAGS);
34
37
  // Nightly 477's shape: two mutation sweeps, each asking for the whole host. The first holds the run's cores;
35
38
  // the second queues for them instead of starting a second host's worth of workers beside it.
36
39
  const first = await takeParentCpuTokens(pool, 31);
@@ -12,12 +12,13 @@ import { GATEWAY_SOCKET } from './gateway-lease.js';
12
12
  * Every runner sizes itself to the whole host unless something budgets it, and without a gateway nothing did: under
13
13
  * `NX_PARALLEL=100%` Nx starts every target side by side and each one's runner takes the whole machine. The run
14
14
  * therefore serves its own pool of one token per core, as a gateway-granted executor serves its children, and every
15
- * task inherits it: `bounded-exec` and the `cargo` launcher take from it as from any parent pool, FIFO, the head
16
- * taking `min(want, free)` once one token is free, so the runners of one run together stay within the cores.
15
+ * task inherits it: `bounded-exec` takes from it as from any parent pool, FIFO, the head taking `min(want, free)` once
16
+ * one token is free, and a Cargo that Nx's own `nx:run-commands` starts reads its jobserver fifo through the
17
+ * `CARGO_MAKEFLAGS` the pool exports, so the runners of one run together stay within the cores.
17
18
  *
18
19
  * The run serves it only when nothing else budgets it: it inherits no pool, and the gateway's control socket does
19
- * not exist. Only `ENOENT` proves the gateway absent, as for the launcher; a socket that exists but does not answer
20
- * may be a live, saturated gateway, and a run budgeting itself beside it would count the host twice.
20
+ * not exist. Only `ENOENT` proves the gateway absent; a socket that exists but does not answer may be a live,
21
+ * saturated gateway, and a run budgeting itself beside it would count the host twice.
21
22
  */
22
23
 
23
24
  /** The pools this process serves, by Nx run id, until that run's `postTasksExecution`. */
@@ -9,6 +9,7 @@ import type { TaskResults } from 'nx/src/tasks-runner/life-cycle';
9
9
  import type { EnsureBuiltProducerReceipt } from './ensure-built.js';
10
10
  import {
11
11
  completedProducerTasks,
12
+ PRODUCER_RECORDS,
12
13
  postTasksExecution,
13
14
  preTasksExecution,
14
15
  producerEnvironmentIdentity,
@@ -116,16 +117,14 @@ describe('actual producer completion evidence', () => {
116
117
  });
117
118
  it('publishes a refusal beside a published receipt, never over it, and names it in the miss', async () => {
118
119
  const root = await realpath(await mkdtemp(join(tmpdir(), 'producer-refusal-')));
119
- const inherited = process.env.NX_WORKSPACE_DATA_DIRECTORY;
120
- delete process.env.NX_WORKSPACE_DATA_DIRECTORY;
121
120
  try {
122
121
  await writeFile(join(root, 'package.json'), JSON.stringify({ name: 'refusal-fixture' }));
123
122
  await writeFile(join(root, 'nx.json'), '{}');
124
123
  await symlink(join(repoRoot, 'node_modules'), join(root, 'node_modules'), 'dir');
125
- const data = join(root, '.nx', 'workspace-data');
126
- await mkdir(data, { recursive: true });
124
+ const records = join(root, PRODUCER_RECORDS);
125
+ await mkdir(records, { recursive: true });
127
126
  const name = createHash('sha256').update(JSON.stringify(task.id)).digest('hex');
128
- const receiptPath = join(data, `producer-receipt-${name}.json`);
127
+ const receiptPath = join(records, `producer-receipt-${name}.json`);
129
128
  // What an earlier run published; this run's refusal is no evidence about it.
130
129
  const published = JSON.stringify({ version: 1, disposition: 'published', receipt: 'an earlier run' });
131
130
  await writeFile(receiptPath, published);
@@ -143,21 +142,44 @@ describe('actual producer completion evidence', () => {
143
142
  },
144
143
  );
145
144
  expect(await readFile(receiptPath, 'utf8')).toBe(published);
146
- expect((await readdir(data)).sort()).toEqual([`producer-receipt-${name}.json`, `producer-refusal-${name}.json`]);
145
+ expect((await readdir(records)).sort()).toEqual([
146
+ '.gitignore',
147
+ `producer-receipt-${name}.json`,
148
+ `producer-refusal-${name}.json`,
149
+ ]);
150
+ // The records directory ignores itself, so no checkout tracks or hashes a record.
151
+ expect(await readFile(join(records, '.gitignore'), 'utf8')).toBe('*\n');
147
152
  expect(readProducerReceipt(root, task.id)).toEqual({
148
153
  ok: false,
149
- detail:
150
- "Missing/corrupt producer completion contract; the producer's latest run refused: No bound producer pre-execution frame",
154
+ detail: `Corrupt producer completion contract at ${receiptPath}; the producer's latest run refused: No bound producer pre-execution frame`,
151
155
  });
152
- // With no receipt at all, the miss still says why the latest run published none.
156
+ // With no receipt at all, the miss names where the receipt would be and why the latest run published none.
153
157
  await rm(receiptPath);
154
- const missing = readProducerReceipt(root, task.id);
155
- expect(missing.ok ? 'hit' : missing.detail).toEndWith(
156
- "; the producer's latest run refused: No bound producer pre-execution frame",
157
- );
158
+ expect(readProducerReceipt(root, task.id)).toEqual({
159
+ ok: false,
160
+ detail: `No producer completion receipt at ${receiptPath}; the producer's latest run refused: No bound producer pre-execution frame`,
161
+ });
162
+ } finally {
163
+ await rm(root, { recursive: true, force: true });
164
+ }
165
+ });
166
+ it('tells a receipt it cannot read from a missing one, naming the path either way', async () => {
167
+ const root = await realpath(await mkdtemp(join(tmpdir(), 'producer-unreadable-')));
168
+ try {
169
+ const records = join(root, PRODUCER_RECORDS);
170
+ await mkdir(records, { recursive: true });
171
+ const name = createHash('sha256').update(JSON.stringify(task.id)).digest('hex');
172
+ const receiptPath = join(records, `producer-receipt-${name}.json`);
173
+ expect(readProducerReceipt(root, task.id)).toEqual({
174
+ ok: false,
175
+ detail: `No producer completion receipt at ${receiptPath}`,
176
+ });
177
+ // A sandbox that may not read the receipt answers EPERM; an unreadable file answers EACCES the same way.
178
+ await writeFile(receiptPath, '{}', { mode: 0 });
179
+ const denied = readProducerReceipt(root, task.id);
180
+ expect(denied.ok ? 'hit' : denied.detail).toStartWith('Cannot read producer completion receipt: EACCES');
181
+ expect(denied.ok ? 'hit' : denied.detail).toContain(receiptPath);
158
182
  } finally {
159
- if (inherited === undefined) delete process.env.NX_WORKSPACE_DATA_DIRECTORY;
160
- else process.env.NX_WORKSPACE_DATA_DIRECTORY = inherited;
161
183
  await rm(root, { recursive: true, force: true });
162
184
  }
163
185
  });
@@ -46,6 +46,16 @@ import { graphInputContractIdentity } from './graph-input-contract.js';
46
46
  export interface ProducerReceiptsOptions {
47
47
  readonly producerReceipts?: { readonly targets: readonly string[] };
48
48
  }
49
+ /**
50
+ * Where a checkout keeps its producer records, relative to its root: in its own source tree, never in Nx's
51
+ * workspace-data. Under cowshed that directory is build state on the checkout's build volume
52
+ * (specs/cowshed/16_build_volumes.md), and a consumer's read grant of the provider checkout does not reach a volume
53
+ * the checkout only links to (specs/cowshed/04_sandbox.md, "Read grants have one narrow meaning"), so a consumer
54
+ * could never read the receipt it is asked to validate. The directory ignores itself (a `.gitignore` of `*` written
55
+ * with its first record), so in any checkout git never tracks a record and Nx's file walk never hashes one: a record
56
+ * is no source or input of the run that publishes it.
57
+ */
58
+ export const PRODUCER_RECORDS = join('.cache', 'producer-receipts');
49
59
  interface ToolResolution {
50
60
  readonly name: string;
51
61
  readonly from: string;
@@ -95,16 +105,18 @@ function configured(options: ProducerReceiptsOptions | undefined): readonly stri
95
105
  * its products never replaces the receipt an earlier run published: readers validate that receipt on its own evidence.
96
106
  */
97
107
  function recordPath(root: string, target: string, disposition: ProducerRecord['disposition']): string {
98
- const load = createWorkspaceNxRequire(root);
99
108
  return join(
100
- load('nx/src/utils/cache-directory').workspaceDataDirectoryForWorkspace(root),
109
+ root,
110
+ PRODUCER_RECORDS,
101
111
  `producer-${disposition === 'published' ? 'receipt' : 'refusal'}-${sha(target)}.json`,
102
112
  );
103
113
  }
104
114
  function publish(root: string, target: string, record: ProducerRecord): ReceiptEvidence<true> {
105
115
  try {
106
116
  const path = recordPath(root, target, record.disposition);
107
- mkdirSync(dirname(path), { recursive: true });
117
+ const records = dirname(path);
118
+ mkdirSync(records, { recursive: true });
119
+ if (!existsSync(join(records, '.gitignore'))) writeFileSync(join(records, '.gitignore'), '*\n');
108
120
  const temporary = `${path}.${randomUUID()}`;
109
121
  try {
110
122
  writeFileSync(temporary, JSON.stringify(record), { flag: 'wx', mode: 0o600 });
@@ -550,10 +562,22 @@ function latestRefusal(root: string, target: string): string | null {
550
562
  }
551
563
  }
552
564
  function validatedReceipt(root: string, target: string): ReceiptEvidence<EnsureBuiltReceipt> {
565
+ const path = recordPath(root, target, 'published');
566
+ let text: string;
567
+ try {
568
+ text = readFileSync(path, 'utf8');
569
+ } catch (error) {
570
+ // No receipt is a producer that never published one; any other failure is a receipt this reader cannot reach.
571
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT')
572
+ return { ok: false, detail: `No producer completion receipt at ${path}` };
573
+ if (error instanceof Error)
574
+ return { ok: false, detail: `Cannot read producer completion receipt: ${error.message}` };
575
+ throw error;
576
+ }
553
577
  try {
554
- const record = typia.json.isParse<PublishedReceipt>(readFileSync(recordPath(root, target, 'published'), 'utf8'));
578
+ const record = typia.json.isParse<PublishedReceipt>(text);
555
579
  if (record === null || record.version !== 1)
556
- return { ok: false, detail: 'Missing/corrupt producer completion contract' };
580
+ return { ok: false, detail: `Corrupt producer completion contract at ${path}` };
557
581
  const receipt = record.receipt;
558
582
  if (
559
583
  receipt.workspaceRoot !== realpathSync(root) ||