@smoothbricks/nx-plugin 0.6.2 → 0.7.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.
- package/README.md +417 -171
- package/dist/.build-inputs.json +39 -28
- package/dist/bin/smoo-nx-go-test-build.d.ts +3 -0
- package/dist/bin/smoo-nx-go-test-build.d.ts.map +1 -0
- package/dist/bin/smoo-nx-go-test-build.js +79 -0
- package/dist/bin/smoo-nx-go-test-run.d.ts +3 -0
- package/dist/bin/smoo-nx-go-test-run.d.ts.map +1 -0
- package/dist/bin/smoo-nx-go-test-run.js +129 -0
- package/dist/bin/smoo-nx-publish.d.ts +3 -0
- package/dist/bin/smoo-nx-publish.d.ts.map +1 -0
- package/dist/bin/smoo-nx-publish.js +3 -0
- package/dist/bounded-test-policy.d.ts +1 -1
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +9 -8
- package/dist/cargo-retention-hook.d.ts +6 -2
- package/dist/cargo-retention-hook.d.ts.map +1 -1
- package/dist/cargo-retention-hook.js +13 -7
- package/dist/cargo-workspace.d.ts +10 -20
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +12 -22
- package/dist/cross-check-policy.d.ts +2 -2
- package/dist/cross-check-policy.js +2 -2
- package/dist/deploy-policy.d.ts +6 -0
- package/dist/deploy-policy.d.ts.map +1 -1
- package/dist/deploy-policy.js +11 -0
- package/dist/eslint-file-set.d.ts +4 -1
- package/dist/eslint-file-set.d.ts.map +1 -1
- package/dist/eslint-sources.d.ts +25 -0
- package/dist/eslint-sources.d.ts.map +1 -0
- package/dist/eslint-sources.js +99 -0
- package/dist/executors/all-verdicts/executor.d.ts +2 -1
- package/dist/executors/all-verdicts/executor.d.ts.map +1 -1
- package/dist/executors/all-verdicts/executor.js +48 -2
- package/dist/executors/all-verdicts/schema.d.ts +13 -0
- package/dist/executors/all-verdicts/schema.d.ts.map +1 -1
- package/dist/executors/all-verdicts/schema.js +7 -0
- package/dist/executors/bounded-exec/bounds.d.ts +57 -8
- package/dist/executors/bounded-exec/bounds.d.ts.map +1 -1
- package/dist/executors/bounded-exec/bounds.js +100 -11
- package/dist/executors/bounded-exec/executor.d.ts +2 -1
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +289 -34
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts +2 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -1
- package/dist/executors/bounded-exec/hang-diagnostics.js +2 -2
- package/dist/executors/bounded-exec/mbx-pool.d.ts +56 -10
- package/dist/executors/bounded-exec/mbx-pool.d.ts.map +1 -1
- package/dist/executors/bounded-exec/mbx-pool.js +145 -11
- package/dist/executors/bounded-exec/mbx-queue.d.ts +30 -0
- package/dist/executors/bounded-exec/mbx-queue.d.ts.map +1 -0
- package/dist/executors/bounded-exec/mbx-queue.js +229 -0
- package/dist/executors/bounded-exec/progress.d.ts +9 -2
- package/dist/executors/bounded-exec/progress.d.ts.map +1 -1
- package/dist/executors/bounded-exec/progress.js +151 -25
- package/dist/executors/bounded-exec/runner-sizing.d.ts +40 -2
- package/dist/executors/bounded-exec/runner-sizing.d.ts.map +1 -1
- package/dist/executors/bounded-exec/runner-sizing.js +55 -4
- package/dist/executors/bounded-exec/runtime-pin.js +1 -1
- package/dist/executors/bounded-exec/test-result-writer.d.ts +30 -0
- package/dist/executors/bounded-exec/test-result-writer.d.ts.map +1 -0
- package/dist/executors/bounded-exec/test-result-writer.js +126 -0
- package/dist/executors/bounded-exec/test-results.d.ts +33 -0
- package/dist/executors/bounded-exec/test-results.d.ts.map +1 -0
- package/dist/executors/bounded-exec/test-results.js +33 -0
- package/dist/executors/bounded-exec/verdict.d.ts +26 -2
- package/dist/executors/bounded-exec/verdict.d.ts.map +1 -1
- package/dist/executors/bounded-exec/verdict.js +108 -4
- package/dist/executors/wrangler-deploy/deploy-hash.d.ts +10 -0
- package/dist/executors/wrangler-deploy/deploy-hash.d.ts.map +1 -0
- package/dist/executors/wrangler-deploy/deploy-hash.js +14 -0
- package/dist/executors/wrangler-deploy/executor.d.ts +24 -0
- package/dist/executors/wrangler-deploy/executor.d.ts.map +1 -0
- package/dist/executors/wrangler-deploy/executor.js +43 -0
- package/dist/executors/wrangler-deploy/hasher.d.ts +27 -0
- package/dist/executors/wrangler-deploy/hasher.d.ts.map +1 -0
- package/dist/executors/wrangler-deploy/hasher.js +90 -0
- package/dist/executors/wrangler-deploy/schema.d.ts +5 -0
- package/dist/executors/wrangler-deploy/schema.d.ts.map +1 -0
- package/dist/go-tests.d.ts +30 -0
- package/dist/go-tests.d.ts.map +1 -0
- package/dist/go-tests.js +43 -0
- package/dist/index.d.ts +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +338 -295
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +5 -0
- package/dist/nx-publish.d.ts +24 -0
- package/dist/nx-publish.d.ts.map +1 -0
- package/dist/nx-publish.js +150 -0
- package/dist/process-group.js +2 -2
- package/dist/runtime-input-policy.d.ts +17 -3
- package/dist/runtime-input-policy.d.ts.map +1 -1
- package/dist/runtime-input-policy.js +4 -4
- package/dist/test-timings.d.ts +23 -4
- package/dist/test-timings.d.ts.map +1 -1
- package/dist/test-timings.js +72 -12
- package/dist/tool-bins.d.ts +6 -8
- package/dist/tool-bins.d.ts.map +1 -1
- package/dist/tool-bins.js +7 -5
- package/dist/version-actions.cjs +10 -11
- package/dist/version-actions.d.cts.map +1 -1
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +17 -22
- package/dist/workspace-index.d.ts +7 -0
- package/dist/workspace-index.d.ts.map +1 -1
- package/dist/workspace-index.js +11 -0
- package/dist/workspace-manifest.js +3 -3
- package/executors.json +7 -6
- package/managed/raw/patches/nx@23.2.1.patch +118 -24
- package/managed/raw/patches/ttsc@0.34.1.patch +139 -0
- package/managed/raw/tooling/devenv +1 -2
- package/managed/raw/tooling/direnv/developer-links.ts +124 -51
- package/managed/raw/tooling/direnv/devenv.smoo.nix +34 -15
- package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -10
- package/managed/raw/tooling/direnv/installed-bun-links.ts +7 -6
- package/managed/raw/tooling/direnv/setup-environment.ts +49 -24
- package/managed/raw/tooling/direnv/ttsc-env.sh +8 -6
- package/managed/raw/tooling/git-hooks/pinned-revisions-served.ts +252 -0
- package/managed/raw/tooling/git-hooks/pre-push.sh +34 -32
- package/managed/templates/github/actions/cache-node-modules/action.yml +1 -1
- package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +12 -11
- package/package.json +277 -11
- package/runtime/nx-publish/.build-inputs.json +11 -0
- package/runtime/nx-publish/cache_adapter.js +45 -0
- package/runtime/nx-publish/nx_publish.js +60 -0
- package/runtime-input.sh +62 -26
- package/src/bin/smoo-nx-go-test-build.ts +83 -0
- package/src/bin/smoo-nx-go-test-run.ts +140 -0
- package/src/bin/smoo-nx-publish.ts +4 -0
- package/src/bounded-test-policy.ts +9 -8
- package/src/cargo-retention-hook.ts +16 -10
- package/src/cargo-workspace.ts +12 -24
- package/src/cross-check-policy.ts +2 -2
- package/src/deploy-policy.ts +11 -0
- package/src/eslint-file-set.ts +4 -1
- package/src/eslint-sources.ts +134 -0
- package/src/executors/all-verdicts/executor.ts +58 -3
- package/src/executors/all-verdicts/schema.json +4 -0
- package/src/executors/all-verdicts/schema.ts +14 -0
- package/src/executors/bounded-exec/bounds.ts +132 -15
- package/src/executors/bounded-exec/executor.ts +329 -34
- package/src/executors/bounded-exec/hang-diagnostics.ts +3 -2
- package/src/executors/bounded-exec/mbx-pool.ts +158 -17
- package/src/executors/bounded-exec/mbx-queue.ts +170 -0
- package/src/executors/bounded-exec/progress.ts +171 -18
- package/src/executors/bounded-exec/runner-sizing.ts +73 -7
- package/src/executors/bounded-exec/runtime-pin.ts +1 -1
- package/src/executors/bounded-exec/test-result-writer.ts +152 -0
- package/src/executors/bounded-exec/test-results.ts +39 -0
- package/src/executors/bounded-exec/verdict.ts +115 -5
- package/src/executors/wrangler-deploy/deploy-hash.ts +15 -0
- package/src/executors/wrangler-deploy/executor.ts +63 -0
- package/src/executors/wrangler-deploy/hasher.ts +111 -0
- package/src/executors/wrangler-deploy/schema.json +16 -0
- package/src/executors/wrangler-deploy/schema.ts +4 -0
- package/src/go-tests.ts +67 -0
- package/src/index.ts +368 -323
- package/src/managed-files/files.ts +5 -0
- package/src/nx-publish.ts +157 -0
- package/src/process-group.ts +2 -2
- package/src/runtime-input-policy.ts +4 -4
- package/src/test-timings.ts +72 -14
- package/src/tool-bins.ts +13 -9
- package/src/version-actions.cts +10 -11
- package/src/workspace-config-policy.ts +20 -29
- package/src/workspace-index.ts +12 -0
- package/src/workspace-manifest.ts +3 -3
- package/test-timings.json +131 -125
- package/dist/executors/clean-outputs/executor.d.ts +0 -26
- package/dist/executors/clean-outputs/executor.d.ts.map +0 -1
- package/dist/executors/clean-outputs/executor.js +0 -83
- package/dist/executors/clean-outputs/schema.d.ts +0 -4
- package/dist/executors/clean-outputs/schema.d.ts.map +0 -1
- package/src/executors/clean-outputs/executor.ts +0 -131
- package/src/executors/clean-outputs/schema.json +0 -15
- package/src/executors/clean-outputs/schema.ts +0 -3
- /package/dist/executors/{clean-outputs → wrangler-deploy}/schema.js +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BUN_TEST,
|
|
1
|
+
import { BUN_TEST, GO_TEST_RUNNER, NEXTEST_RUN } from './verdict.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* How a runner's parallelism is read from its command and sized to the tokens its CPU pool lends
|
|
@@ -40,13 +40,27 @@ export interface TestThreadCeilings {
|
|
|
40
40
|
* ceilings their sizing keeps; cargo-mutants carries how many cargo processes it runs at once, which share its grant.
|
|
41
41
|
*/
|
|
42
42
|
export type Demand =
|
|
43
|
-
| { kind: 'cargo'
|
|
43
|
+
| { kind: 'cargo'; want: number; testThreads: TestThreadCeilings }
|
|
44
|
+
| { kind: 'process'; want: number; testThreads: TestThreadCeilings }
|
|
44
45
|
| { kind: 'cargo-mutants'; want: number; jobs: number }
|
|
45
|
-
| { kind:
|
|
46
|
+
| { kind: 'nextest'; want: number }
|
|
47
|
+
| { kind: 'bun-parallel'; want: number }
|
|
48
|
+
| { kind: 'stryker'; want: number }
|
|
49
|
+
| { kind: 'vitest'; want: number }
|
|
50
|
+
| { kind: 'go-test'; want: number };
|
|
51
|
+
|
|
52
|
+
/** A runner a leaf reserves for under mbx (mbx-pool.ts, `mbxLeaf`): one that Cargo does not admit through mbx itself. */
|
|
53
|
+
export type ReservedDemand = Extract<Demand, { kind: 'nextest' | 'bun-parallel' | 'stryker' | 'process' }>;
|
|
46
54
|
|
|
47
55
|
/** Every budgeted command learns its grant here, so a script that starts a runner can size it. */
|
|
48
56
|
export const CPU_TOKENS_ENV = 'BOUNDED_EXEC_CPU_TOKENS';
|
|
49
57
|
|
|
58
|
+
/**
|
|
59
|
+
* The cores mbx granted the reservation a leaf runs under: `mbx reserve --cpus N` names at most N to its command, as many
|
|
60
|
+
* as were free once one was.
|
|
61
|
+
*/
|
|
62
|
+
export const GRANTED_CPUS_ENV = 'MBX_RESERVE_CPUS';
|
|
63
|
+
|
|
50
64
|
const CARGO_BUILD =
|
|
51
65
|
/(?<=^|[\s;&|()])(?:cargo(?:\s+-\S+)*\s+(?:build|test|check|clippy|rustc|doc|run|nextest\s+archive)|napi\s+build)(?=\s|$)/;
|
|
52
66
|
const TEST_THREADS = /--test-threads(?:=|\s+)(\d+)/g;
|
|
@@ -75,7 +89,7 @@ interface WorkerFlag {
|
|
|
75
89
|
/** The flag with its value, one shell word, captured. */
|
|
76
90
|
readonly flag: RegExp;
|
|
77
91
|
/** The flag naming `count`. */
|
|
78
|
-
readonly sized: (count:
|
|
92
|
+
readonly sized: (count: string) => string;
|
|
79
93
|
/** The runner's own count when the flag names none, from the cores and the environment the command will see. */
|
|
80
94
|
readonly ownDefault: (cores: number, env: Readonly<NodeJS.ProcessEnv>) => number;
|
|
81
95
|
/** A flag with which the runner runs one worker whatever count it names. */
|
|
@@ -101,9 +115,9 @@ const WORKER_FLAGS: Readonly<Record<WorkerFlagKind, WorkerFlag>> = {
|
|
|
101
115
|
ownDefault: (cores) => Math.max(cores - 1, 1),
|
|
102
116
|
serial: /(?<![\w-])(?:--no-file-parallelism|--file(?:P|-p)arallelism=false)(?![\w-])/,
|
|
103
117
|
},
|
|
104
|
-
/** `go test -p`: packages built and tested at once, `GOMAXPROCS` by default, normally one per core. */
|
|
118
|
+
/** `go test -p`: packages built and tested at once, `GOMAXPROCS` by default, normally one per core; prebuilt runs alike. */
|
|
105
119
|
'go-test': {
|
|
106
|
-
invocation:
|
|
120
|
+
invocation: GO_TEST_RUNNER,
|
|
107
121
|
flag: new RegExp(String.raw`(?<![\w-])-p(?:=|\s+)${WORD}`, 'g'),
|
|
108
122
|
sized: (count) => `-p=${count}`,
|
|
109
123
|
ownDefault: (cores, env) => parseInheritedTokens(env.GOMAXPROCS) ?? cores,
|
|
@@ -322,7 +336,7 @@ export function sizeRunner(
|
|
|
322
336
|
case 'go-test': {
|
|
323
337
|
const runner = WORKER_FLAGS[demand.kind];
|
|
324
338
|
return {
|
|
325
|
-
command: withInvocationFlag(command, runner.invocation, runner.flag, runner.sized(
|
|
339
|
+
command: withInvocationFlag(command, runner.invocation, runner.flag, runner.sized(count), true),
|
|
326
340
|
env: { [CPU_TOKENS_ENV]: held },
|
|
327
341
|
};
|
|
328
342
|
}
|
|
@@ -346,6 +360,58 @@ export function sizeRunner(
|
|
|
346
360
|
}
|
|
347
361
|
}
|
|
348
362
|
|
|
363
|
+
/**
|
|
364
|
+
* The shell statements that run first in a reserved leaf's reservation and size its runner to the grant, which only
|
|
365
|
+
* the command learns as it starts: `CPU_TOKENS_ENV` becomes the cores mbx granted (`GRANTED_CPUS_ENV`), never more
|
|
366
|
+
* than the leaf asked for, its `--cpus` (the runner's own count), which also stands in under an mbx that grants a
|
|
367
|
+
* reservation whole and names none. The test-thread variables a sizing sets follow from it, under their inherited
|
|
368
|
+
* ceilings, outranking the target's own as a numeric grant's do (`sizeRunner`).
|
|
369
|
+
*/
|
|
370
|
+
export function grantedSizing(demand: ReservedDemand): string {
|
|
371
|
+
const asked = demand.want;
|
|
372
|
+
const named = `\${${GRANTED_CPUS_ENV}:-${asked}}`;
|
|
373
|
+
const tokens = `$${CPU_TOKENS_ENV}`;
|
|
374
|
+
const atMost = (ceiling: number | null): string =>
|
|
375
|
+
ceiling === null ? `"${tokens}"` : `"$(( ${tokens} < ${ceiling} ? ${tokens} : ${ceiling} ))"`;
|
|
376
|
+
const statements = [`export ${CPU_TOKENS_ENV}="$(( ${named} < ${asked} ? ${named} : ${asked} ))"`];
|
|
377
|
+
switch (demand.kind) {
|
|
378
|
+
case 'nextest':
|
|
379
|
+
statements.push(`export NEXTEST_TEST_THREADS="${tokens}"`);
|
|
380
|
+
break;
|
|
381
|
+
case 'process':
|
|
382
|
+
// A process's script may start Cargo, whose test threads take no jobserver token: its whole grant.
|
|
383
|
+
statements.push(
|
|
384
|
+
`export RUST_TEST_THREADS=${atMost(demand.testThreads.rust)}`,
|
|
385
|
+
`export NEXTEST_TEST_THREADS=${atMost(demand.testThreads.nextest)}`,
|
|
386
|
+
);
|
|
387
|
+
break;
|
|
388
|
+
case 'bun-parallel':
|
|
389
|
+
case 'stryker':
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
return statements.map((statement) => `${statement}; `).join('');
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* `command` with a reserved runner's named count read from its grant, `CPU_TOKENS_ENV` as `grantedSizing` sets it,
|
|
397
|
+
* where the runner's shell runs it; the runner then runs on all it holds.
|
|
398
|
+
*/
|
|
399
|
+
export function sizeRunnerToGrant(demand: ReservedDemand, command: string): string {
|
|
400
|
+
const granted = `\${${CPU_TOKENS_ENV}}`;
|
|
401
|
+
switch (demand.kind) {
|
|
402
|
+
case 'nextest':
|
|
403
|
+
return command.replace(TEST_THREADS, `--test-threads=${granted}`);
|
|
404
|
+
case 'bun-parallel':
|
|
405
|
+
return command.replace(BUN_PARALLEL, `--parallel=${granted}`);
|
|
406
|
+
case 'stryker': {
|
|
407
|
+
const runner = WORKER_FLAGS.stryker;
|
|
408
|
+
return withInvocationFlag(command, runner.invocation, runner.flag, runner.sized(granted), true);
|
|
409
|
+
}
|
|
410
|
+
case 'process':
|
|
411
|
+
return command;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
349
415
|
/** A configured value may lower an inherited ceiling, never raise it. */
|
|
350
416
|
function inheritedCeiling(inherited: string | undefined, configured: string | undefined): number | null {
|
|
351
417
|
const left = parseInheritedTokens(inherited);
|
|
@@ -7,7 +7,7 @@ import type { Result } from './ram-temp.js';
|
|
|
7
7
|
/**
|
|
8
8
|
* A leg that runs `bun` runs the Bun its workspace declares.
|
|
9
9
|
*
|
|
10
|
-
* The root `package.json` names that Bun in `packageManager` (`bun@
|
|
10
|
+
* The root `package.json` names that Bun in `packageManager` (`bun@x.y.z`); the Bun the command gets is the first `bun`
|
|
11
11
|
* on the PATH it runs with. When the two disagree the leg is refused before it asks for anything, naming both versions
|
|
12
12
|
* and the binary. A Bun other than the declared one does not fail at startup: on Bun 1.4.2 `console.log` drops what a
|
|
13
13
|
* full pipe refuses, so a child's JSON document reaches the parent that reads it cut in the middle of a string, the
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { type ClosedTestResult, parseTestSourceRef, TEST_FAILURES_FILE, TEST_RESULTS_FILE } from './test-results.js';
|
|
2
|
+
|
|
3
|
+
export { taskDirectory } from './verdict.js';
|
|
4
|
+
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
6
|
+
import { appendFileSync } from 'node:fs';
|
|
7
|
+
import { type FileHandle, open, rename, rm } from 'node:fs/promises';
|
|
8
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
9
|
+
/** A runner's closed observation, before the artifact writer assigns its references. */
|
|
10
|
+
export interface NativeTestResult {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly status: ClosedTestResult['status'];
|
|
13
|
+
readonly source: ClosedTestResult['source'];
|
|
14
|
+
readonly contradictsCursor?: number;
|
|
15
|
+
readonly durationMs: number | null;
|
|
16
|
+
readonly sourceRef: string | null;
|
|
17
|
+
readonly failureOutput: string | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TestResultWriter {
|
|
21
|
+
/** Completes the evidence write before publishing its referring JSONL record. */
|
|
22
|
+
append(result: NativeTestResult): string | null;
|
|
23
|
+
reconcile(results: readonly NativeTestResult[]): string | null;
|
|
24
|
+
close(): Promise<string | null>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The cursor includes its own decimal digits: converge on their width before publishing. */
|
|
28
|
+
function resultLine(result: Omit<ClosedTestResult, 'cursor'>, offset: number): { line: string; cursor: number } {
|
|
29
|
+
let cursor = offset;
|
|
30
|
+
for (;;) {
|
|
31
|
+
const line = `${JSON.stringify({ ...result, cursor })}\n`;
|
|
32
|
+
const next = offset + Buffer.byteLength(line);
|
|
33
|
+
if (next === cursor) return { line, cursor };
|
|
34
|
+
cursor = next;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeSourceRef(sourceRef: string | null, sourceRoot: { cwd: string; root: string }): string | null {
|
|
39
|
+
const location = parseTestSourceRef(sourceRef);
|
|
40
|
+
if (location === null) return null;
|
|
41
|
+
return `${relative(sourceRoot.root, resolve(sourceRoot.cwd, location.path)).split(sep).join('/')}:${location.line}`;
|
|
42
|
+
}
|
|
43
|
+
async function closeResultFiles(
|
|
44
|
+
files: readonly FileHandle[],
|
|
45
|
+
stagedPaths: readonly string[] = [],
|
|
46
|
+
): Promise<string | null> {
|
|
47
|
+
const results = await Promise.allSettled([
|
|
48
|
+
...files.map((file) => file.close()),
|
|
49
|
+
...stagedPaths.map((path) => rm(path, { force: true })),
|
|
50
|
+
]);
|
|
51
|
+
const failures = results.flatMap((result) => (result.status === 'rejected' ? [String(result.reason)] : []));
|
|
52
|
+
return failures.length === 0 ? null : `closing test result artifacts: ${failures.join('; ')}`;
|
|
53
|
+
}
|
|
54
|
+
/** A new inode and UUID identify a replacement run, even when it has the same byte length. */
|
|
55
|
+
export async function createTestResultWriter(
|
|
56
|
+
directory: string,
|
|
57
|
+
sourceRoot: { readonly cwd: string; readonly root: string },
|
|
58
|
+
): Promise<{ readonly ok: true; readonly writer: TestResultWriter } | { readonly ok: false; readonly error: string }> {
|
|
59
|
+
const runId = randomUUID();
|
|
60
|
+
const target = join(directory, TEST_RESULTS_FILE);
|
|
61
|
+
const staged = `${target}.${runId}`;
|
|
62
|
+
const causePath = join(directory, TEST_FAILURES_FILE);
|
|
63
|
+
const stagedCauses = `${causePath}.${runId}`;
|
|
64
|
+
const [records, causes] = await Promise.allSettled([open(staged, 'ax'), open(stagedCauses, 'ax')]);
|
|
65
|
+
if (records.status !== 'fulfilled' || causes.status !== 'fulfilled') {
|
|
66
|
+
const cleanup = await closeResultFiles(
|
|
67
|
+
[
|
|
68
|
+
...(records.status === 'fulfilled' ? [records.value] : []),
|
|
69
|
+
...(causes.status === 'fulfilled' ? [causes.value] : []),
|
|
70
|
+
],
|
|
71
|
+
[staged, stagedCauses],
|
|
72
|
+
);
|
|
73
|
+
const failures = [records, causes].flatMap((result) =>
|
|
74
|
+
result.status === 'rejected' ? [String(result.reason)] : [],
|
|
75
|
+
);
|
|
76
|
+
return {
|
|
77
|
+
ok: false,
|
|
78
|
+
error: `initializing test result stream ${target}: ${failures.join('; ')}${cleanup === null ? '' : `; ${cleanup}`}`,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
await rename(stagedCauses, causePath);
|
|
83
|
+
await rename(staged, target);
|
|
84
|
+
} catch (cause) {
|
|
85
|
+
const cleanup = await closeResultFiles([records.value, causes.value], [staged, stagedCauses]);
|
|
86
|
+
return {
|
|
87
|
+
ok: false,
|
|
88
|
+
error: `initializing test result stream ${target}: ${String(cause)}${cleanup === null ? '' : `; ${cleanup}`}`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
let offset = 0;
|
|
92
|
+
let causeOffset = 0;
|
|
93
|
+
let failure: string | null = null;
|
|
94
|
+
let closing: Promise<string | null> | null = null;
|
|
95
|
+
const provisional = new Map<string, { status: ClosedTestResult['status']; cursor: number }>();
|
|
96
|
+
return {
|
|
97
|
+
ok: true,
|
|
98
|
+
writer: {
|
|
99
|
+
append(result) {
|
|
100
|
+
if (failure !== null) return failure;
|
|
101
|
+
if (closing !== null) return `appending test result ${result.id} to ${target}: stream is closed`;
|
|
102
|
+
try {
|
|
103
|
+
let causeRef: ClosedTestResult['causeRef'] = null;
|
|
104
|
+
if (result.status === 'fail' && result.failureOutput !== null) {
|
|
105
|
+
const endByte = causeOffset + Buffer.byteLength(result.failureOutput);
|
|
106
|
+
appendFileSync(causes.value.fd, result.failureOutput);
|
|
107
|
+
causeRef = { path: TEST_FAILURES_FILE, startByte: causeOffset, endByte };
|
|
108
|
+
causeOffset = endByte;
|
|
109
|
+
}
|
|
110
|
+
const { line, cursor } = resultLine(
|
|
111
|
+
{
|
|
112
|
+
runId,
|
|
113
|
+
id: result.id,
|
|
114
|
+
status: result.status,
|
|
115
|
+
source: result.source,
|
|
116
|
+
...(result.contradictsCursor === undefined ? {} : { contradictsCursor: result.contradictsCursor }),
|
|
117
|
+
durationMs: result.durationMs,
|
|
118
|
+
sourceRef: normalizeSourceRef(result.sourceRef, sourceRoot),
|
|
119
|
+
causeRef,
|
|
120
|
+
},
|
|
121
|
+
offset,
|
|
122
|
+
);
|
|
123
|
+
// appendFileSync has no user-space buffering: each LF is visible before append returns.
|
|
124
|
+
appendFileSync(records.value.fd, line);
|
|
125
|
+
offset = cursor;
|
|
126
|
+
if (result.source === 'progress') provisional.set(result.id, { status: result.status, cursor });
|
|
127
|
+
return null;
|
|
128
|
+
} catch (cause) {
|
|
129
|
+
failure = `appending test result ${result.id} to ${target}: ${String(cause)}`;
|
|
130
|
+
return failure;
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
reconcile(results) {
|
|
134
|
+
for (const result of results) {
|
|
135
|
+
const prior = provisional.get(result.id);
|
|
136
|
+
if (prior !== undefined && prior.status === result.status) continue;
|
|
137
|
+
const error = this.append({
|
|
138
|
+
...result,
|
|
139
|
+
source: 'junit',
|
|
140
|
+
...(prior === undefined ? {} : { contradictsCursor: prior.cursor }),
|
|
141
|
+
});
|
|
142
|
+
if (error !== null) return error;
|
|
143
|
+
}
|
|
144
|
+
return null;
|
|
145
|
+
},
|
|
146
|
+
close() {
|
|
147
|
+
closing ??= closeResultFiles([records.value, causes.value]);
|
|
148
|
+
return closing;
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import typia, { type tags } from 'typia';
|
|
2
|
+
|
|
3
|
+
export const TEST_RESULTS_FILE = 'test-results.jsonl';
|
|
4
|
+
export const TEST_FAILURES_FILE = 'test-failures.log';
|
|
5
|
+
|
|
6
|
+
type ByteOffset = number & tags.Type<'uint64'> & tags.Maximum<9007199254740991>;
|
|
7
|
+
|
|
8
|
+
/** Live observations, not gate evidence: report.xml and verdict.json remain authoritative. */
|
|
9
|
+
export interface ClosedTestResult {
|
|
10
|
+
readonly runId: string & tags.Format<'uuid'>;
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly status: 'pass' | 'fail' | 'skip';
|
|
13
|
+
readonly source: 'native' | 'progress' | 'junit';
|
|
14
|
+
/** Set only on a terminal JUnit correction of a provisional progress observation. */
|
|
15
|
+
readonly contradictsCursor?: ByteOffset;
|
|
16
|
+
/** Null when the native runner reports no duration (for example a skipped test). */
|
|
17
|
+
readonly durationMs: (number & tags.Minimum<0>) | null;
|
|
18
|
+
/** Native file:line, never a location inferred from human output. */
|
|
19
|
+
readonly sourceRef: string | null;
|
|
20
|
+
/** UTF-8 byte range [startByte, endByte), relative to the task artifact directory. */
|
|
21
|
+
readonly causeRef: {
|
|
22
|
+
readonly path: typeof TEST_FAILURES_FILE;
|
|
23
|
+
readonly startByte: ByteOffset;
|
|
24
|
+
readonly endByte: ByteOffset;
|
|
25
|
+
} | null;
|
|
26
|
+
/** Next UTF-8 byte offset in test-results.jsonl, including this record's LF. */
|
|
27
|
+
readonly cursor: ByteOffset;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const isClosedTestResult = typia.createEquals<ClosedTestResult>();
|
|
31
|
+
|
|
32
|
+
/** A producer-normalized workspace-relative declaration location, never inferred from a test name. */
|
|
33
|
+
export function parseTestSourceRef(sourceRef: string | null): { path: string; line: number } | null {
|
|
34
|
+
if (sourceRef === null) return null;
|
|
35
|
+
const match = /^(.+):([1-9]\d*)$/.exec(sourceRef);
|
|
36
|
+
const path = match?.[1];
|
|
37
|
+
const line = Number(match?.[2]);
|
|
38
|
+
return path !== undefined && Number.isSafeInteger(line) ? { path, line } : null;
|
|
39
|
+
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
|
+
import typia from 'typia';
|
|
3
4
|
import type { CpuWaitRow } from './lease-wait.js';
|
|
4
5
|
import { describeHolders, type LockWait, type Opener } from './lock-wait.js';
|
|
6
|
+
import type { MbxQueuedRequest } from './mbx-queue.js';
|
|
7
|
+
import type { NativeTestResult } from './test-result-writer.js';
|
|
8
|
+
import { TEST_FAILURES_FILE, TEST_RESULTS_FILE } from './test-results.js';
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* Why one bounded run ended, recorded per Nx task so a caller can tell a load-sensitive bound
|
|
@@ -21,11 +25,14 @@ import { describeHolders, type LockWait, type Opener } from './lock-wait.js';
|
|
|
21
25
|
* - `no-space`: the run failed and its output said a write found no space (ENOSPC). Whatever its tests then reported,
|
|
22
26
|
* they failed on the disk, not on their own: the verdict names the line that said so and, when the task held one,
|
|
23
27
|
* its RAM temp volume as it stood when that line arrived.
|
|
28
|
+
* - `queued`: the command failed before any admission after a live task PID was explicitly seen waiting in mbx.
|
|
29
|
+
* Its observed wait and public queue positions describe admission pressure, never a wedged test.
|
|
24
30
|
* - `failed`: anything else that did not pass. That includes an assertion named in the report,
|
|
25
31
|
* or a non-zero exit with no report to say why.
|
|
26
32
|
*/
|
|
27
33
|
export type BoundedExecVerdict =
|
|
28
34
|
| { readonly outcome: 'passed' }
|
|
35
|
+
| ({ readonly outcome: 'queued'; readonly exitCode: number } & AdmissionQueueWait)
|
|
29
36
|
| {
|
|
30
37
|
readonly outcome: 'bound';
|
|
31
38
|
readonly bound: 'total' | 'fixture';
|
|
@@ -44,6 +51,13 @@ export type BoundedExecVerdict =
|
|
|
44
51
|
| ({ readonly outcome: 'no-space'; readonly exitCode: number; readonly tests: readonly string[] } & NoSpaceSighting)
|
|
45
52
|
| { readonly outcome: 'failed'; readonly exitCode: number; readonly tests: readonly string[] };
|
|
46
53
|
|
|
54
|
+
/** Observed admission wait, supplied only while the command has never received a grant. */
|
|
55
|
+
export interface AdmissionQueueWait {
|
|
56
|
+
readonly waitedMs: number;
|
|
57
|
+
readonly requests: readonly MbxQueuedRequest[];
|
|
58
|
+
}
|
|
59
|
+
const isQueuedVerdict = typia.createIs<Extract<BoundedExecVerdict, { outcome: 'queued' }>>();
|
|
60
|
+
|
|
47
61
|
/**
|
|
48
62
|
* The task's RAM temp volume, read when a line saying ENOSPC arrived: the writer's own lease and the space left on the
|
|
49
63
|
* volume it was taken on (`measured`), or why there was none to measure: the lease was no longer on that volume
|
|
@@ -178,6 +192,64 @@ export function junitFailures(xml: string): JunitFailures {
|
|
|
178
192
|
return { timedOut, cancelled, failed };
|
|
179
193
|
}
|
|
180
194
|
|
|
195
|
+
/** Bun's nested suites carry the describe chain; testcase.classname is in the reverse order. */
|
|
196
|
+
export function bunJunitResults(xml: string): NativeTestResult[] {
|
|
197
|
+
const results: NativeTestResult[] = [];
|
|
198
|
+
const suites: string[] = [];
|
|
199
|
+
let current: { attributes: string; body: string } | null = null;
|
|
200
|
+
const text = xml;
|
|
201
|
+
const tags =
|
|
202
|
+
/<!\[CDATA\[[\s\S]*?\]\]>|<!--[\s\S]*?-->|<(\/?)(testsuite|testcase)\b((?:[^>"']|"[^"]*"|'[^']*')*?)(\/?)>/g;
|
|
203
|
+
let end = 0;
|
|
204
|
+
const close = (): void => {
|
|
205
|
+
if (current === null) return;
|
|
206
|
+
const name = attribute(current.attributes, 'name');
|
|
207
|
+
const file = attribute(current.attributes, 'file');
|
|
208
|
+
if (name !== null && file !== null) {
|
|
209
|
+
const failed = /<(?:failure|error)\b/.test(current.body);
|
|
210
|
+
const skipped = /<skipped\b/.test(current.body);
|
|
211
|
+
const line = attribute(current.attributes, 'line');
|
|
212
|
+
const time = attribute(current.attributes, 'time');
|
|
213
|
+
const seconds = time === null ? Number.NaN : Number(time);
|
|
214
|
+
const output =
|
|
215
|
+
/<(?:failure|error)\b((?:[^>"']|"[^"]*"|'[^']*')*?)(?:\/>|>((?:<!\[CDATA\[[\s\S]*?\]\]>|(?!<!\[CDATA\[|<\/(?:failure|error)>)[\s\S])*)<\/(?:failure|error)>)/.exec(
|
|
216
|
+
current.body,
|
|
217
|
+
);
|
|
218
|
+
const failureOutput =
|
|
219
|
+
output === null
|
|
220
|
+
? null
|
|
221
|
+
: output[2] === undefined || output[2] === ''
|
|
222
|
+
? (attribute(output[1] ?? '', 'message') ?? '')
|
|
223
|
+
: decodeJunitText(output[2]);
|
|
224
|
+
results.push({
|
|
225
|
+
id: `${file}::${[...suites.slice(1), name].join(' > ')}`,
|
|
226
|
+
status: failed ? 'fail' : skipped ? 'skip' : 'pass',
|
|
227
|
+
source: 'junit',
|
|
228
|
+
durationMs: Number.isFinite(seconds) && seconds >= 0 ? seconds * 1000 : null,
|
|
229
|
+
sourceRef: line === null ? null : `${file}:${line}`,
|
|
230
|
+
failureOutput,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
current = null;
|
|
234
|
+
};
|
|
235
|
+
for (const match of text.matchAll(tags)) {
|
|
236
|
+
if (current !== null) current.body += text.slice(end, match.index);
|
|
237
|
+
end = match.index + match[0].length;
|
|
238
|
+
const [, slash, tag, attributes = '', selfClosing] = match;
|
|
239
|
+
if (tag === undefined) {
|
|
240
|
+
if (current !== null) current.body += match[0];
|
|
241
|
+
} else if (tag === 'testsuite') {
|
|
242
|
+
if (slash === '/') suites.pop();
|
|
243
|
+
else if (selfClosing !== '/') suites.push(attribute(attributes, 'name') ?? '');
|
|
244
|
+
} else if (slash === '/') close();
|
|
245
|
+
else {
|
|
246
|
+
close();
|
|
247
|
+
current = { attributes, body: '' };
|
|
248
|
+
if (selfClosing === '/') close();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return results;
|
|
252
|
+
}
|
|
181
253
|
/** One test case of a JUnit report: nextest's binary id (its `classname`), the test's name and its seconds. */
|
|
182
254
|
export interface JunitCaseTime {
|
|
183
255
|
readonly binary: string;
|
|
@@ -209,6 +281,16 @@ function attribute(attributes: string, name: string): string | null {
|
|
|
209
281
|
return decodeEntities(match[2] ?? match[3] ?? '');
|
|
210
282
|
}
|
|
211
283
|
|
|
284
|
+
function decodeJunitText(value: string): string {
|
|
285
|
+
let text = '';
|
|
286
|
+
let end = 0;
|
|
287
|
+
for (const match of value.matchAll(/<!\[CDATA\[([\s\S]*?)\]\]>|<!--[\s\S]*?-->/g)) {
|
|
288
|
+
text += decodeEntities(value.slice(end, match.index));
|
|
289
|
+
text += match[1] ?? '';
|
|
290
|
+
end = match.index + match[0].length;
|
|
291
|
+
}
|
|
292
|
+
return text + decodeEntities(value.slice(end));
|
|
293
|
+
}
|
|
212
294
|
function decodeEntities(value: string): string {
|
|
213
295
|
return value.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|amp|lt|gt|quot|apos);/g, (_, entity: string) => {
|
|
214
296
|
switch (entity) {
|
|
@@ -255,6 +337,8 @@ export interface RunEnd {
|
|
|
255
337
|
readonly noSpace: NoSpaceSighting | null;
|
|
256
338
|
/** The lock waits the expiry's snapshot named; empty when none, or when that capture did not complete. */
|
|
257
339
|
readonly lockWaits: readonly LockWait[];
|
|
340
|
+
/** Explicit observed queue evidence before first admission; absent after any grant. */
|
|
341
|
+
readonly admissionQueue?: AdmissionQueueWait | null;
|
|
258
342
|
}
|
|
259
343
|
|
|
260
344
|
/**
|
|
@@ -270,6 +354,9 @@ export function judgeRun(run: RunEnd): BoundedExecVerdict {
|
|
|
270
354
|
const tests = run.report === null ? [] : [...run.report.failed, ...run.report.timedOut, ...run.report.cancelled];
|
|
271
355
|
return { outcome: 'no-space', exitCode: run.exitCode === 0 ? 1 : run.exitCode, tests, ...run.noSpace };
|
|
272
356
|
}
|
|
357
|
+
if (run.admissionQueue != null && run.exitCode !== 0) {
|
|
358
|
+
return { outcome: 'queued', exitCode: run.exitCode, ...run.admissionQueue };
|
|
359
|
+
}
|
|
273
360
|
if (run.expiry?.kind === 'idle') {
|
|
274
361
|
const tests = [...new Set([...run.expiry.tests, ...(run.report?.cancelled ?? [])])];
|
|
275
362
|
return { outcome: 'wedged', idleMs: run.expiry.limitMs, elapsedMs: run.elapsedMs, tests, lockWaits: run.lockWaits };
|
|
@@ -328,6 +415,13 @@ export const BUN_TEST = /(?<=^|[\s;&|()])((?:[^\s;&|()<>'"`\\=]*\/)?bun)\s+test(
|
|
|
328
415
|
export const NEXTEST_RUN = /(?<=^|[\s;&|()-])nextest\s+run(?=\s|$)/g;
|
|
329
416
|
export const GO_TEST = /(?<=^|[\s;&|()])go\s+test(?=\s|$)/;
|
|
330
417
|
|
|
418
|
+
/**
|
|
419
|
+
* `go test`, or `smoo-nx-go-test-run` running the test binaries `smoo-nx-go-test-build` made with `go test -c`, under
|
|
420
|
+
* any path: both write `go test -json` events on stdout and run `-p` packages at once. Only `go test` builds, so only
|
|
421
|
+
* it is asked for `-json` (withProgressReport) or given a build phase (withRunnerLifecycle).
|
|
422
|
+
*/
|
|
423
|
+
export const GO_TEST_RUNNER = /(?<=^|[\s;&|()])(?:go\s+test|(?:[^\s;&|()<>'"`\\=]*\/)?smoo-nx-go-test-run)(?=\s|$)/;
|
|
424
|
+
|
|
331
425
|
/**
|
|
332
426
|
* `command` with its test runner asked to write a JUnit report into `directory`, the report
|
|
333
427
|
* `judgeRun` reads. Both runners are recognised by their invocation, so every target that runs
|
|
@@ -367,6 +461,8 @@ export async function clearTaskDirectory(directory: string): Promise<void> {
|
|
|
367
461
|
await Promise.all([
|
|
368
462
|
rm(join(directory, VERDICT_FILE), { force: true }),
|
|
369
463
|
rm(join(directory, JUNIT_FILE), { force: true }),
|
|
464
|
+
rm(join(directory, TEST_RESULTS_FILE), { force: true }),
|
|
465
|
+
rm(join(directory, TEST_FAILURES_FILE), { force: true }),
|
|
370
466
|
]);
|
|
371
467
|
}
|
|
372
468
|
|
|
@@ -392,13 +488,16 @@ export async function readRecord(directory: string): Promise<BoundedExecRecord |
|
|
|
392
488
|
}
|
|
393
489
|
|
|
394
490
|
/** The JUnit report a command wrote into `directory`, or `null` when it wrote none. */
|
|
395
|
-
export async function readJunitReport(
|
|
491
|
+
export async function readJunitReport(
|
|
492
|
+
directory: string,
|
|
493
|
+
bunClosed?: (results: readonly NativeTestResult[]) => void,
|
|
494
|
+
): Promise<JunitFailures | null> {
|
|
396
495
|
try {
|
|
397
|
-
|
|
496
|
+
const xml = await readFile(join(directory, JUNIT_FILE), 'utf8');
|
|
497
|
+
bunClosed?.(bunJunitResults(xml));
|
|
498
|
+
return junitFailures(xml);
|
|
398
499
|
} catch (error) {
|
|
399
|
-
if (isNotFound(error))
|
|
400
|
-
return null;
|
|
401
|
-
}
|
|
500
|
+
if (isNotFound(error)) return null;
|
|
402
501
|
throw error;
|
|
403
502
|
}
|
|
404
503
|
}
|
|
@@ -419,6 +518,9 @@ function parseVerdict(value: unknown): BoundedExecVerdict {
|
|
|
419
518
|
switch (value.outcome) {
|
|
420
519
|
case 'passed':
|
|
421
520
|
return { outcome: 'passed' };
|
|
521
|
+
case 'queued':
|
|
522
|
+
if (isQueuedVerdict(value)) return value;
|
|
523
|
+
break;
|
|
422
524
|
case 'bound':
|
|
423
525
|
if (
|
|
424
526
|
(value.bound === 'total' || value.bound === 'fixture') &&
|
|
@@ -568,10 +670,18 @@ export async function failedTasksOfRun(runJson: unknown, workspaceData: string):
|
|
|
568
670
|
return failed;
|
|
569
671
|
}
|
|
570
672
|
|
|
673
|
+
/** The same honest admission diagnostic in live output and a terminal verdict. */
|
|
674
|
+
export function describeAdmissionQueue(wait: AdmissionQueueWait): string {
|
|
675
|
+
return `queued for admission ${(wait.waitedMs / 1_000).toFixed(1)} s; mbx ${wait.requests
|
|
676
|
+
.map((request) => `pid=${request.pid} ticket=${request.ticket} queue position=${request.queuePosition}`)
|
|
677
|
+
.join(', ')}`;
|
|
678
|
+
}
|
|
571
679
|
export function describeVerdict(verdict: BoundedExecVerdict): string {
|
|
572
680
|
switch (verdict.outcome) {
|
|
573
681
|
case 'passed':
|
|
574
682
|
return 'passed';
|
|
683
|
+
case 'queued':
|
|
684
|
+
return `${describeAdmissionQueue(verdict)}; exited ${verdict.exitCode} before first admission`;
|
|
575
685
|
case 'bound':
|
|
576
686
|
return verdict.bound === 'test'
|
|
577
687
|
? `per-test timeout only: ${verdict.tests.join(', ')}`
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hash a wrangler deploy runs under when its hasher could not confirm that the stage serves its artifact's
|
|
3
|
+
* version: the artifact hash, then a nonce no cache holds, so the run misses whatever an earlier run stored, rolled
|
|
4
|
+
* back to or not. A miss is the right answer there. The deploy still tags the artifact's version
|
|
5
|
+
* (`deployArtifactHash`), so once that version is live the next hash is the artifact's own.
|
|
6
|
+
*/
|
|
7
|
+
export function unconfirmedDeployHash(artifactHash: string, nonce: string): string {
|
|
8
|
+
return `${artifactHash}-${nonce}`;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The artifact hash a wrangler deploy task's hash carries: all of a confirmed one, the part before an unconfirmed one's nonce. */
|
|
12
|
+
export function deployArtifactHash(taskHash: string): string {
|
|
13
|
+
const nonce = taskHash.indexOf('-');
|
|
14
|
+
return nonce === -1 ? taskHash : taskHash.slice(0, nonce);
|
|
15
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { deployArtifactHash } from './deploy-hash.js';
|
|
4
|
+
import { workspaceBinPath } from './hasher.js';
|
|
5
|
+
import type { WranglerDeployOptions } from './schema.js';
|
|
6
|
+
|
|
7
|
+
/** What the executor reads of Nx's context: the workspace and the project's root. */
|
|
8
|
+
interface WranglerDeployContext {
|
|
9
|
+
readonly root: string;
|
|
10
|
+
readonly projectName?: string | undefined;
|
|
11
|
+
readonly projectsConfigurations: { readonly projects: Readonly<Record<string, { readonly root: string }>> };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* `smoo wrangler deploy-stage --stage <stage>` in the project, as the workspace's own `smoo`, under the artifact hash
|
|
16
|
+
* of the hash the task ran under: `deploy-stage` tags the version it deploys with that hash, and the hasher compares
|
|
17
|
+
* the stage's live tag with it.
|
|
18
|
+
*/
|
|
19
|
+
export function runWranglerDeploy(
|
|
20
|
+
options: WranglerDeployOptions,
|
|
21
|
+
context: WranglerDeployContext,
|
|
22
|
+
env: NodeJS.ProcessEnv,
|
|
23
|
+
): Promise<{ success: boolean }> {
|
|
24
|
+
const project =
|
|
25
|
+
context.projectName === undefined ? undefined : context.projectsConfigurations.projects[context.projectName];
|
|
26
|
+
if (project === undefined) {
|
|
27
|
+
throw new Error('@smoothbricks/nx-plugin:wrangler-deploy requires a project context.');
|
|
28
|
+
}
|
|
29
|
+
const hash = env.NX_TASK_HASH;
|
|
30
|
+
const deployEnv: NodeJS.ProcessEnv = {
|
|
31
|
+
...env,
|
|
32
|
+
PATH: workspaceBinPath(context.root, env),
|
|
33
|
+
...(hash === undefined ? {} : { NX_TASK_HASH: deployArtifactHash(hash) }),
|
|
34
|
+
};
|
|
35
|
+
const command = `smoo wrangler deploy-stage --stage ${options.stage}`;
|
|
36
|
+
// eslint-disable-next-line deadline/no-unbounded-wait -- WHY unbounded: a deploy lasts as long as wrangler's upload and the live-version wait deploy-stage bounds itself, as it did under nx:run-commands
|
|
37
|
+
return new Promise((resolve) => {
|
|
38
|
+
const child = spawn('smoo', ['wrangler', 'deploy-stage', '--stage', options.stage], {
|
|
39
|
+
cwd: join(context.root, project.root),
|
|
40
|
+
env: deployEnv,
|
|
41
|
+
stdio: 'inherit',
|
|
42
|
+
});
|
|
43
|
+
child.on('error', (error) => {
|
|
44
|
+
console.error(`wrangler-deploy: \`${command}\` could not start: ${error.message}`);
|
|
45
|
+
resolve({ success: false });
|
|
46
|
+
});
|
|
47
|
+
child.on('close', (code, signal) => {
|
|
48
|
+
if (code !== 0) {
|
|
49
|
+
console.error(
|
|
50
|
+
`wrangler-deploy: \`${command}\` ${signal === null ? `exited ${code}` : `was ended by ${signal}`}`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
resolve({ success: code === 0 });
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default function wranglerDeployExecutor(
|
|
59
|
+
options: WranglerDeployOptions,
|
|
60
|
+
context: WranglerDeployContext,
|
|
61
|
+
): Promise<{ success: boolean }> {
|
|
62
|
+
return runWranglerDeploy(options, context, process.env);
|
|
63
|
+
}
|