@smoothbricks/nx-plugin 0.5.3 → 0.5.5

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 (29) hide show
  1. package/README.md +52 -12
  2. package/dist/executors/bounded-exec/cpu-tokens.d.ts +13 -0
  3. package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -1
  4. package/dist/executors/bounded-exec/cpu-tokens.js +9 -9
  5. package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
  6. package/dist/executors/bounded-exec/executor.js +20 -6
  7. package/dist/executors/bounded-exec/progress.d.ts +8 -3
  8. package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
  9. package/dist/executors/bounded-exec/progress.js +100 -10
  10. package/dist/executors/bounded-exec/verdict.d.ts +5 -4
  11. package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
  12. package/dist/executors/bounded-exec/verdict.js +1 -0
  13. package/dist/runtime-input-policy.d.ts +60 -0
  14. package/dist/runtime-input-policy.d.ts.map +1 -0
  15. package/dist/runtime-input-policy.js +379 -0
  16. package/dist/testing.js +1 -1
  17. package/managed/raw/patches/nx@23.2.1.patch +26 -0
  18. package/package.json +7 -2
  19. package/src/executors/bounded-exec/cpu-tokens.ts +15 -9
  20. package/src/executors/bounded-exec/executor.test.ts +26 -23
  21. package/src/executors/bounded-exec/executor.ts +19 -4
  22. package/src/executors/bounded-exec/go-progress-host.test.ts +171 -0
  23. package/src/executors/bounded-exec/progress.test.ts +73 -0
  24. package/src/executors/bounded-exec/progress.ts +95 -12
  25. package/src/executors/bounded-exec/verdict.ts +5 -4
  26. package/src/nx-daemon-start-never.test.ts +30 -1
  27. package/src/runtime-input-policy.ts +395 -0
  28. package/src/runtime-input.test.ts +160 -3
  29. package/src/testing.ts +1 -1
@@ -0,0 +1,395 @@
1
+ import { accessSync, constants, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
2
+ import { delimiter, isAbsolute, join, relative, resolve, sep } from 'node:path';
3
+ import { RUNTIME_INPUT_SCRIPT } from './runtime-input.js';
4
+
5
+ /**
6
+ * What a runtime input may run, and how its failures are read back after Nx refuses it.
7
+ *
8
+ * A runtime input runs wherever Nx hashes: a developer's Mac, a NixOS CI runner, a cowshed sandbox. Only the programs a
9
+ * workspace declares are the same in all of them: devenv's Nix profile and the checkout's own `node_modules`. A
10
+ * program the host's operating system ships is not. macOS ships `shasum` in /usr/bin; it is a Perl script, not
11
+ * coreutils, and no devenv profile provides it (devenv's carries coreutils, `sha256sum` included). An input that named
12
+ * it hashed on every Mac and exited 127 on the NixOS runner, which Nx reported only as
13
+ * `invalid utf-8 sequence of 1 bytes from index 0` (runtime-input.sh says why).
14
+ */
15
+
16
+ /** Words a POSIX `sh` runs itself, without a PATH lookup. */
17
+ const SHELL_BUILTINS: ReadonlySet<string> = new Set([
18
+ '.',
19
+ ':',
20
+ '[',
21
+ 'alias',
22
+ 'break',
23
+ 'cd',
24
+ 'continue',
25
+ 'echo',
26
+ 'eval',
27
+ 'exit',
28
+ 'export',
29
+ 'false',
30
+ 'getopts',
31
+ 'hash',
32
+ 'local',
33
+ 'printf',
34
+ 'pwd',
35
+ 'read',
36
+ 'readonly',
37
+ 'return',
38
+ 'set',
39
+ 'shift',
40
+ 'test',
41
+ 'times',
42
+ 'trap',
43
+ 'true',
44
+ 'type',
45
+ 'ulimit',
46
+ 'umask',
47
+ 'unalias',
48
+ 'unset',
49
+ 'wait',
50
+ ]);
51
+
52
+ /** Reserved words that may stand before a command's program. */
53
+ const SHELL_KEYWORDS: ReadonlySet<string> = new Set([
54
+ '!',
55
+ '{',
56
+ '}',
57
+ 'if',
58
+ 'then',
59
+ 'else',
60
+ 'elif',
61
+ 'fi',
62
+ 'do',
63
+ 'done',
64
+ 'while',
65
+ 'until',
66
+ ]);
67
+
68
+ /** Reserved words whose clause names no program (`for name in words`, `case word in`). */
69
+ const CLAUSE_KEYWORDS: ReadonlySet<string> = new Set(['for', 'case', 'esac', 'in']);
70
+
71
+ /** Shells whose `-c` operand is a script of further commands. */
72
+ const SHELLS: ReadonlySet<string> = new Set(['sh', 'bash']);
73
+
74
+ const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
75
+
76
+ type Token =
77
+ | { readonly kind: 'word'; readonly text: string }
78
+ | { readonly kind: 'separator' }
79
+ | { readonly kind: 'redirect' };
80
+
81
+ /** The index of the character that closes the quote opened before `from`, or the end of `text` when none does. */
82
+ function closingQuote(text: string, from: number, quote: string): number {
83
+ for (let at = from; at < text.length; at += 1) {
84
+ if (text[at] === '\\' && quote !== "'") at += 1;
85
+ else if (text[at] === quote) return at;
86
+ }
87
+ return text.length;
88
+ }
89
+
90
+ /** The index of the `close` that balances the `open` before `from`, skipping quoted text, or the end of `text`. */
91
+ function closingBracket(text: string, from: number, open: string, close: string): number {
92
+ let depth = 1;
93
+ for (let at = from; at < text.length; at += 1) {
94
+ const character = text[at];
95
+ if (character === '\\') at += 1;
96
+ else if (character === "'" || character === '"' || character === '`') at = closingQuote(text, at + 1, character);
97
+ else if (character === open) depth += 1;
98
+ else if (character === close) {
99
+ depth -= 1;
100
+ if (depth === 0) return at;
101
+ }
102
+ }
103
+ return text.length;
104
+ }
105
+
106
+ /**
107
+ * The shell tokens of `script`. A command substitution is part of its word, and the programs it runs are appended to
108
+ * `nested`; quoting is removed from the words, which are only inspected, never run.
109
+ */
110
+ function shellTokens(script: string, nested: string[]): Token[] {
111
+ const tokens: Token[] = [];
112
+ let word: string | undefined;
113
+ const flush = (): void => {
114
+ if (word !== undefined) tokens.push({ kind: 'word', text: word });
115
+ word = undefined;
116
+ };
117
+ const append = (text: string): void => {
118
+ word = (word ?? '') + text;
119
+ };
120
+ /** Consumes the expansion starting with `$` or a backquote at `at`; returns the index after it. */
121
+ const expansion = (at: number): number => {
122
+ if (script[at] === '`') {
123
+ const end = closingQuote(script, at + 1, '`');
124
+ nested.push(...commandPrograms(script.slice(at + 1, end)));
125
+ append('`…`');
126
+ return end + 1;
127
+ }
128
+ if (script.startsWith('$((', at)) {
129
+ const end = closingBracket(script, at + 2, '(', ')');
130
+ append(script.slice(at, end + 1));
131
+ return end + 1;
132
+ }
133
+ if (script.startsWith('$(', at)) {
134
+ const end = closingBracket(script, at + 2, '(', ')');
135
+ nested.push(...commandPrograms(script.slice(at + 2, end)));
136
+ append('$(…)');
137
+ return end + 1;
138
+ }
139
+ if (script.startsWith('${', at)) {
140
+ const end = closingBracket(script, at + 2, '{', '}');
141
+ append(script.slice(at, end + 1));
142
+ return end + 1;
143
+ }
144
+ append('$');
145
+ return at + 1;
146
+ };
147
+ let at = 0;
148
+ while (at < script.length) {
149
+ const character = script[at];
150
+ if (character === "'") {
151
+ const end = closingQuote(script, at + 1, "'");
152
+ append(script.slice(at + 1, end));
153
+ at = end + 1;
154
+ } else if (character === '"') {
155
+ append('');
156
+ at += 1;
157
+ while (at < script.length && script[at] !== '"') {
158
+ if (script[at] === '\\' && at + 1 < script.length) {
159
+ append('$`"\\\n'.includes(script[at + 1]) ? script[at + 1] : script.slice(at, at + 2));
160
+ at += 2;
161
+ } else if (script[at] === '$' || script[at] === '`') {
162
+ at = expansion(at);
163
+ } else {
164
+ append(script[at]);
165
+ at += 1;
166
+ }
167
+ }
168
+ at += 1;
169
+ } else if (character === '\\') {
170
+ append(script.slice(at + 1, at + 2));
171
+ at += 2;
172
+ } else if (character === '$' || character === '`') {
173
+ at = expansion(at);
174
+ } else if (character === '#' && word === undefined) {
175
+ at = script.indexOf('\n', at);
176
+ if (at < 0) at = script.length;
177
+ } else if (character === ' ' || character === '\t') {
178
+ flush();
179
+ at += 1;
180
+ } else if (';&|()\n'.includes(character)) {
181
+ flush();
182
+ tokens.push({ kind: 'separator' });
183
+ at += 1;
184
+ } else if (character === '<' || character === '>') {
185
+ // A file-descriptor number written against the operator (`2>&1`) is part of it, not a word.
186
+ if (word !== undefined && /^\d+$/.test(word)) word = undefined;
187
+ flush();
188
+ at += 1;
189
+ while (at < script.length && '<>&|'.includes(script[at])) at += 1;
190
+ tokens.push({ kind: 'redirect' });
191
+ } else {
192
+ append(character);
193
+ at += 1;
194
+ }
195
+ }
196
+ flush();
197
+ return tokens;
198
+ }
199
+
200
+ /** The programs one simple command runs, given its words in order. */
201
+ function simpleCommandPrograms(words: readonly string[]): string[] {
202
+ let at = 0;
203
+ while (at < words.length && (SHELL_KEYWORDS.has(words[at]) || ASSIGNMENT.test(words[at]))) at += 1;
204
+ const program = words[at];
205
+ if (program === undefined || CLAUSE_KEYWORDS.has(program)) return [];
206
+ const operands = words.slice(at + 1);
207
+ if (program === 'exec') return simpleCommandPrograms(operands);
208
+ // `command -v name` looks a name up; `command name` runs it.
209
+ if (program === 'command') return operands[0]?.startsWith('-') ? [] : simpleCommandPrograms(operands);
210
+ if (program === 'env') {
211
+ let operand = 0;
212
+ while (operand < operands.length && operands[operand].startsWith('-') && operands[operand] !== '--') {
213
+ operand += operands[operand] === '-u' || operands[operand] === '-C' ? 2 : 1;
214
+ }
215
+ if (operands[operand] === '--') operand += 1;
216
+ return [program, ...simpleCommandPrograms(operands.slice(operand))];
217
+ }
218
+ if (SHELLS.has(program)) {
219
+ // The wrapper runs its operands as one command.
220
+ if (operands[0] === RUNTIME_INPUT_SCRIPT) return [program, ...simpleCommandPrograms(operands.slice(1))];
221
+ const options = operands.findIndex((operand) => !operand.startsWith('-'));
222
+ const flags = operands.slice(0, options < 0 ? operands.length : options);
223
+ const script = options < 0 ? undefined : operands[options];
224
+ if (script !== undefined && flags.some((flag) => flag !== '--' && !flag.startsWith('--') && flag.includes('c'))) {
225
+ return [program, ...commandPrograms(script)];
226
+ }
227
+ }
228
+ return [program];
229
+ }
230
+
231
+ /**
232
+ * The programs `script` runs by name or path: the program of each simple command, through `env`, `exec`,
233
+ * `command`, `sh -c` and command substitutions, and the command a `runtime-input.sh` input runs. Shell builtins are
234
+ * not programs. A script file a command runs (`sh tooling/x.sh`) is its own source's concern, not read here.
235
+ */
236
+ export function commandPrograms(script: string): string[] {
237
+ const programs: string[] = [];
238
+ const nested: string[] = [];
239
+ let words: string[] = [];
240
+ let redirectTarget = false;
241
+ const end = (): void => {
242
+ programs.push(...simpleCommandPrograms(words));
243
+ words = [];
244
+ };
245
+ for (const token of shellTokens(script, nested)) {
246
+ if (token.kind === 'separator') end();
247
+ else if (token.kind === 'redirect') redirectTarget = true;
248
+ else if (redirectTarget) redirectTarget = false;
249
+ else words.push(token.text);
250
+ }
251
+ end();
252
+ return [...programs, ...nested].filter((program) => !SHELL_BUILTINS.has(program));
253
+ }
254
+
255
+ /** Where a runtime input's programs are looked up, and the locations that count as declared rather than the host's. */
256
+ export interface ProgramSearch {
257
+ /** The PATH the input runs with: Nx passes its own environment through. */
258
+ readonly path: string;
259
+ /** The workspace root: its `node_modules/.bin` and tooling are declared by the checkout. */
260
+ readonly root: string;
261
+ /**
262
+ * The user's home. A developer's devenv shell keeps its PATH entries in the checkout direnv loaded (a main
263
+ * checkout's `node_modules/.bin` while a shed is judged) and the user's own tools there; neither is the OS image.
264
+ */
265
+ readonly home: string;
266
+ }
267
+
268
+ /** A program a runtime input runs that the workspace does not declare, and why it is refused. */
269
+ export type HostProgram =
270
+ /** PATH finds it outside /nix, the checkout, the home and /bin/sh or /usr/bin/env. */
271
+ | { readonly program: string; readonly refusal: 'host'; readonly found: string }
272
+ /** Nothing on PATH provides it. */
273
+ | { readonly program: string; readonly refusal: 'absent' }
274
+ /**
275
+ * PATH finds it in the checkout's `node_modules/.bin` as a link to a build output of a package in this checkout. Bun
276
+ * links a workspace package's bin only when its target exists at install time, and a fresh checkout installs before
277
+ * it builds, so the name is absent there while every checkout that built first has it.
278
+ */
279
+ | { readonly program: string; readonly refusal: 'workspace-bin'; readonly found: string; readonly target: string };
280
+
281
+ function within(directory: string, file: string): boolean {
282
+ const path = relative(directory, file);
283
+ return path !== '' && !path.startsWith('..') && !isAbsolute(path);
284
+ }
285
+
286
+ function isExecutableFile(file: string): boolean {
287
+ try {
288
+ accessSync(file, constants.X_OK);
289
+ return statSync(file).isFile();
290
+ } catch (error) {
291
+ if (error instanceof Error && 'code' in error && ['ENOENT', 'ENOTDIR', 'EACCES'].includes(String(error.code))) {
292
+ return false;
293
+ }
294
+ throw error;
295
+ }
296
+ }
297
+
298
+ function onPath(program: string, path: string): string | undefined {
299
+ for (const directory of path.split(delimiter)) {
300
+ // An empty entry means the working directory; Nx runs inputs from the root, which a name never resolves into.
301
+ if (directory === '') continue;
302
+ const candidate = join(directory, program);
303
+ if (isExecutableFile(candidate)) return candidate;
304
+ }
305
+ return undefined;
306
+ }
307
+
308
+ /**
309
+ * The only host paths every platform guarantees: NixOS keeps exactly these two outside the store, for `#!` lines and
310
+ * for Nx, which runs every runtime input as `sh -c`.
311
+ */
312
+ const GUARANTEED_HOST_PROGRAMS: ReadonlySet<string> = new Set(['/bin/sh', '/usr/bin/env']);
313
+ /**
314
+ * The build output of a package in this checkout that `found`, a link in the checkout's `node_modules/.bin`, names;
315
+ * undefined for any other file, an installed package's bin included.
316
+ */
317
+ function workspaceBuildOutput(found: string, root: string): string | undefined {
318
+ if (!within(join(root, 'node_modules', '.bin'), found)) return undefined;
319
+ const physicalRoot = realpathSync(root);
320
+ const target = realpathSync(found);
321
+ return within(physicalRoot, target) && !relative(physicalRoot, target).split(sep).includes('node_modules')
322
+ ? target
323
+ : undefined;
324
+ }
325
+
326
+ /**
327
+ * Every program `command` runs that the workspace does not declare: one PATH finds outside the Nix store, the
328
+ * checkout, the home and the guaranteed host paths, one nothing on PATH provides, or one only a workspace package's
329
+ * build output provides. A relative path names a file in the checkout.
330
+ */
331
+ export function hostPrograms(command: string, search: ProgramSearch): HostProgram[] {
332
+ const declared = (file: string): boolean =>
333
+ file.startsWith('/nix/') ||
334
+ GUARANTEED_HOST_PROGRAMS.has(file) ||
335
+ within(search.root, file) ||
336
+ within(search.home, file);
337
+ return [...new Set(commandPrograms(command))].flatMap((program): HostProgram[] => {
338
+ if (program.includes('/')) {
339
+ const found = resolve(search.root, program);
340
+ return declared(found) ? [] : [{ program, refusal: 'host', found }];
341
+ }
342
+ const found = onPath(program, search.path);
343
+ if (found === undefined) return [{ program, refusal: 'absent' }];
344
+ if (!declared(found)) return [{ program, refusal: 'host', found }];
345
+ const target = workspaceBuildOutput(found, search.root);
346
+ return target === undefined ? [] : [{ program, refusal: 'workspace-bin', found, target }];
347
+ });
348
+ }
349
+
350
+ /** Why `host` is refused, naming where PATH found it and what to run instead. */
351
+ export function hostProgramReason(host: HostProgram): string {
352
+ switch (host.refusal) {
353
+ case 'host':
354
+ return `PATH finds ${host.program} at ${host.found}, a host program no devenv profile declares`;
355
+ case 'absent':
356
+ return `nothing on PATH provides ${host.program}`;
357
+ case 'workspace-bin':
358
+ return (
359
+ `PATH finds ${host.program} at ${host.found}, a link to this checkout's build output ${host.target}, ` +
360
+ 'which an install that runs before the build does not create; run that file with node or bun instead'
361
+ );
362
+ }
363
+ }
364
+
365
+ /** The directory under the workspace root where runtime-input.sh retains each failed input. */
366
+ export const RUNTIME_INPUT_FAILURES = '.runtime-input-failures';
367
+
368
+ function absentAsUndefined<T>(read: () => T): T | undefined {
369
+ try {
370
+ return read();
371
+ } catch (error) {
372
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT') return undefined;
373
+ throw error;
374
+ }
375
+ }
376
+
377
+ /**
378
+ * The report of every failure runtime-input.sh retained under `root` at or after `sinceMs`, oldest first, each headed
379
+ * by its path. Nx 23.2.1 names a failed runtime input only as `invalid utf-8 sequence of 1 bytes from index N` and
380
+ * drops the input's stderr, so a headless run prints these after Nx fails: they name the command, its exit status and
381
+ * its stderr. A record without a report (its evidence could not be written) is named as such.
382
+ */
383
+ export function runtimeInputFailureReports(root: string, sinceMs: number): string[] {
384
+ const directory = join(root, RUNTIME_INPUT_FAILURES);
385
+ return (absentAsUndefined(() => readdirSync(directory)) ?? [])
386
+ .map((name) => join(directory, name))
387
+ .map((record) => ({ record, modifiedMs: statSync(record).mtimeMs }))
388
+ .filter(({ modifiedMs }) => modifiedMs >= sinceMs)
389
+ .sort((left, right) => left.modifiedMs - right.modifiedMs)
390
+ .map(({ record }) => {
391
+ const report = join(record, 'report');
392
+ const text = absentAsUndefined(() => readFileSync(report, 'utf8'));
393
+ return text === undefined ? `${record}: no report; its evidence is incomplete\n` : `${report}:\n${text}`;
394
+ });
395
+ }
@@ -2,12 +2,31 @@ import { describe, expect, it } from 'bun:test';
2
2
  import { spawnSync } from 'node:child_process';
3
3
  import { createHash } from 'node:crypto';
4
4
  import { existsSync } from 'node:fs';
5
- import { mkdir, mkdtemp, readdir, readFile, rm, stat, symlink, writeFile } from 'node:fs/promises';
6
- import { tmpdir } from 'node:os';
7
- import { dirname, join } from 'node:path';
5
+ import {
6
+ chmod,
7
+ mkdir,
8
+ mkdtemp,
9
+ readdir,
10
+ readFile,
11
+ realpath,
12
+ rm,
13
+ stat,
14
+ symlink,
15
+ utimes,
16
+ writeFile,
17
+ } from 'node:fs/promises';
18
+ import { homedir, tmpdir } from 'node:os';
19
+ import { delimiter, dirname, join } from 'node:path';
8
20
  import { fileURLToPath } from 'node:url';
9
21
  import { fixtureNxEnv, withNxFixture } from './__tests__/fixture-nx-env.js';
10
22
  import { RUNTIME_INPUT_SCRIPT, runtimeInput, shellWord } from './runtime-input.js';
23
+ import {
24
+ commandPrograms,
25
+ hostProgramReason,
26
+ hostPrograms,
27
+ RUNTIME_INPUT_FAILURES,
28
+ runtimeInputFailureReports,
29
+ } from './runtime-input-policy.js';
11
30
 
12
31
  const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
13
32
  const repoRoot = dirname(dirname(packageRoot));
@@ -130,11 +149,18 @@ describe('a planted runtime input failure through real Nx', () => {
130
149
  expect(good.status, good.output).toBe(0);
131
150
  expect(existsSync(join(workspace, 'good.ran'))).toBe(true);
132
151
 
152
+ const started = Date.now();
133
153
  const planted = nx(workspace, env, 'probe:planted');
134
154
  expect(planted.status, planted.output).toBe(1);
135
155
  expect(planted.output).toContain('invalid utf-8 sequence');
156
+ // Nx's message names neither the command nor its cause; the retained report names both.
157
+ expect(planted.output).not.toContain('planted-cause');
136
158
  expect(existsSync(join(workspace, 'planted.ran'))).toBe(false);
137
159
  const record = await failureRecord(workspace);
160
+ const reports = runtimeInputFailureReports(workspace, started);
161
+ expect(reports).toHaveLength(1);
162
+ expect(reports[0]).toContain(`${join(record, 'report')}:\nruntime input exited 3: sh -c`);
163
+ expect(reports[0]).toContain('planted-cause');
138
164
  expect(await readFile(join(record, 'argv'))).toEqual(Buffer.from('sh\0-c\0echo planted-cause >&2; exit 3\0'));
139
165
  expect(await readFile(join(record, 'stderr'), 'utf8')).toBe('planted-cause\n');
140
166
  expect(await readFile(join(record, 'exit-status'), 'utf8')).toBe('3\n');
@@ -200,6 +226,137 @@ describe('runtime inputs of this repository', () => {
200
226
  }
201
227
  expect(offenders).toEqual([]);
202
228
  });
229
+
230
+ it('name only programs the workspace declares, on the PATH it declares', async () => {
231
+ // Nx hands a runtime input its own process environment unchanged (nx task-env.js getTaskSpecificEnv), and this
232
+ // repository composes that PATH with tooling/direnv/repo-path, in its devenv shell and on CI (`--github-path`): the
233
+ // devenv profile and the checkout's own directories, no /usr/bin or /bin. Judged on that PATH alone, an input that
234
+ // needs a program the host's directories supply fails here on every OS, as it fails on the NixOS runner.
235
+ const composed = spawnSync('bash', [join(repoRoot, 'tooling', 'direnv', 'repo-path'), '--print'], {
236
+ cwd: repoRoot,
237
+ encoding: 'utf8',
238
+ });
239
+ expect(composed.status, composed.stderr).toBe(0);
240
+ const path = composed.stdout.trim().split('\n').join(delimiter);
241
+ const venv = process.env.VIRTUAL_ENV === undefined ? undefined : join(process.env.VIRTUAL_ENV, 'bin');
242
+ expect(
243
+ path
244
+ .split(delimiter)
245
+ .filter((entry) => !entry.startsWith('/nix/') && !entry.startsWith(`${repoRoot}/`) && entry !== venv),
246
+ ).toEqual([]);
247
+ const search = { path, root: repoRoot, home: homedir() };
248
+ const offenders: string[] = [];
249
+ const declared = (file: string, value: unknown): void => {
250
+ if (Array.isArray(value)) {
251
+ for (const item of value) declared(file, item);
252
+ } else if (value !== null && typeof value === 'object') {
253
+ for (const [key, item] of Object.entries(value)) {
254
+ if (key !== 'runtime' || typeof item !== 'string') declared(file, item);
255
+ else for (const host of hostPrograms(item, search)) offenders.push(`${file}: ${hostProgramReason(host)}`);
256
+ }
257
+ }
258
+ };
259
+ const tracked = spawnSync(
260
+ 'git',
261
+ ['ls-files', '-z', 'nx.json', 'package.json', '**/package.json', '**/project.json'],
262
+ {
263
+ cwd: repoRoot,
264
+ encoding: 'utf8',
265
+ },
266
+ );
267
+ expect(tracked.status, tracked.stderr).toBe(0);
268
+ for (const file of tracked.stdout.split('\0').filter(Boolean)) {
269
+ declared(file, JSON.parse(await readFile(join(repoRoot, file), 'utf8')));
270
+ }
271
+ expect(offenders).toEqual([]);
272
+ });
273
+ });
274
+
275
+ describe('runtime input programs', () => {
276
+ it("are each simple command's program, through the wrapper, env, sh -c and substitutions, without builtins", () => {
277
+ expect(commandPrograms(`sh ${RUNTIME_INPUT_SCRIPT} shasum -a 256 packages/x/y.wasm`)).toEqual(['sh', 'shasum']);
278
+ expect(
279
+ commandPrograms(
280
+ `sh ${RUNTIME_INPUT_SCRIPT} sh -c 'if [ -n "\${SRC:-}" ]; then cd "$SRC" && export A=/dev/null && git rev-parse HEAD && changes=$(git status --porcelain) && printf "%s\\n" "$changes" | shasum -a 256; else echo installed; fi'`,
281
+ ),
282
+ ).toEqual(['sh', 'sh', 'git', 'shasum', 'git']);
283
+ expect(
284
+ commandPrograms(
285
+ 'env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 git -c core.excludesFile=/dev/null ls-files -z',
286
+ ),
287
+ ).toEqual(['env', 'git']);
288
+ expect(commandPrograms("bun -e 'console.log(1)' >/dev/null 2>&1 </dev/null")).toEqual(['bun']);
289
+ expect(commandPrograms('x=`cut -d: -f1 list` && command -v tr; exec date')).toEqual(['date', 'cut']);
290
+ expect(commandPrograms('for f in a b; do stat "$f"; done # head')).toEqual(['stat']);
291
+ expect(commandPrograms("sh tooling/identity.sh rustc 'sh -c awk'")).toEqual(['sh']);
292
+ });
293
+
294
+ it('that the host supplies, nothing supplies, or only a workspace build supplies, are refused by name', async () => {
295
+ const scratch = await mkdtemp(join(tmpdir(), 'runtime-input-programs-'));
296
+ try {
297
+ const host = join(scratch, 'usr-bin');
298
+ const root = join(scratch, 'checkout');
299
+ const bin = join(root, 'node_modules', '.bin');
300
+ await mkdir(host, { recursive: true });
301
+ await mkdir(bin, { recursive: true });
302
+ await mkdir(join(root, 'packages', 'tool', 'dist'), { recursive: true });
303
+ await mkdir(join(root, 'node_modules', '.bun', 'ext', 'bin'), { recursive: true });
304
+ for (const file of [
305
+ join(host, 'shasum'),
306
+ join(bin, 'tool'),
307
+ join(root, 'packages', 'tool', 'dist', 'built.js'),
308
+ join(root, 'node_modules', '.bun', 'ext', 'bin', 'ext.js'),
309
+ ]) {
310
+ await writeFile(file, '#!/bin/sh\n');
311
+ await chmod(file, 0o755);
312
+ }
313
+ // A workspace package's bin links into its build output; an installed package's into node_modules.
314
+ await symlink('../../packages/tool/dist/built.js', join(bin, 'built'));
315
+ await symlink('../.bun/ext/bin/ext.js', join(bin, 'ext'));
316
+ // A file that is not executable provides nothing.
317
+ await writeFile(join(host, 'absent'), '');
318
+ const search = { path: `${host}:${bin}`, root, home: join(scratch, 'home') };
319
+ const command = 'shasum -a 256 f && tool && ext && tooling/x.sh && /nix/store/0-a/bin/a && absent; built';
320
+ const refused = hostPrograms(`${command}; /usr/bin/env true; /usr/bin/shasum`, search);
321
+ const built = await realpath(join(root, 'packages', 'tool', 'dist', 'built.js'));
322
+ expect(refused).toEqual([
323
+ { program: 'shasum', refusal: 'host', found: join(host, 'shasum') },
324
+ { program: 'absent', refusal: 'absent' },
325
+ { program: 'built', refusal: 'workspace-bin', found: join(bin, 'built'), target: built },
326
+ { program: '/usr/bin/shasum', refusal: 'host', found: '/usr/bin/shasum' },
327
+ ]);
328
+ expect(refused.map(hostProgramReason)).toEqual([
329
+ `PATH finds shasum at ${join(host, 'shasum')}, a host program no devenv profile declares`,
330
+ 'nothing on PATH provides absent',
331
+ `PATH finds built at ${join(bin, 'built')}, a link to this checkout's build output ${built}, which an ` +
332
+ 'install that runs before the build does not create; run that file with node or bun instead',
333
+ 'PATH finds /usr/bin/shasum at /usr/bin/shasum, a host program no devenv profile declares',
334
+ ]);
335
+ } finally {
336
+ await rm(scratch, { recursive: true, force: true });
337
+ }
338
+ });
339
+ });
340
+
341
+ describe('retained runtime input failure reports', () => {
342
+ it('are read back from a time on, oldest first, naming a record whose report is missing', async () => {
343
+ await withWrapperWorkspace(async (workspace) => {
344
+ const failures = join(workspace, RUNTIME_INPUT_FAILURES);
345
+ const record = async (name: string, at: number, report?: string): Promise<void> => {
346
+ await mkdir(join(failures, name), { recursive: true });
347
+ if (report !== undefined) await writeFile(join(failures, name, 'report'), report);
348
+ await utimes(join(failures, name), at / 1000, at / 1000);
349
+ };
350
+ expect(runtimeInputFailureReports(workspace, 0)).toEqual([]);
351
+ await record('failure.old', 1_000_000, 'old\n');
352
+ await record('failure.second', 3_000_000, 'second\n');
353
+ await record('failure.first', 2_000_000);
354
+ expect(runtimeInputFailureReports(workspace, 2_000_000)).toEqual([
355
+ `${join(failures, 'failure.first')}: no report; its evidence is incomplete\n`,
356
+ `${join(failures, 'failure.second', 'report')}:\nsecond\n`,
357
+ ]);
358
+ });
359
+ });
203
360
  });
204
361
 
205
362
  async function writeProbeWorkspace(workspace: string): Promise<void> {
package/src/testing.ts CHANGED
@@ -168,7 +168,7 @@ export async function pidsWorkingIn(root: string): Promise<number[]> {
168
168
  for (const pid of (await readdir('/proc')).filter((name) => /^\d+$/.test(name))) {
169
169
  const cwd = await readlink(`/proc/${pid}/cwd`).catch((error: unknown) => {
170
170
  // Another user's process, or one that exited since the listing.
171
- if (errorCode(error) === 'EACCES' || errorCode(error) === 'ENOENT') {
171
+ if (errorCode(error) === 'EACCES' || errorCode(error) === 'ENOENT' || errorCode(error) === 'ESRCH') {
172
172
  return null;
173
173
  }
174
174
  throw error;