@zgeoff/rig 0.0.1 → 0.0.3
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 +87 -3
- package/dist/browser/browser-preload.d.ts +1 -0
- package/dist/browser/build-browser-run.d.ts +9 -0
- package/dist/browser/build-lane-environments.d.ts +8 -0
- package/dist/browser/build-recorded-command.d.ts +1 -0
- package/dist/browser/find-browser-run.d.ts +4 -0
- package/dist/browser/find-private-dir-problem.d.ts +2 -0
- package/dist/browser/has-browser-build-script.d.ts +1 -0
- package/dist/browser/has-error-code.d.ts +1 -0
- package/dist/browser/is-zombie-state.d.ts +1 -0
- package/dist/browser/lane-files.d.ts +13 -0
- package/dist/browser/make-browser-lane-hooks.d.ts +22 -0
- package/dist/browser/pick-chrome-dirs.d.ts +7 -0
- package/dist/browser/pick-exit-code-after-tests.d.ts +1 -0
- package/dist/browser/pick-step-after-build.d.ts +7 -0
- package/dist/browser/pick-stop-step.d.ts +17 -0
- package/dist/browser/probe-host-process.d.ts +1 -0
- package/dist/browser/probe-process.d.ts +6 -0
- package/dist/browser/probe-run.d.ts +9 -0
- package/dist/browser/process-group-still-running-error.d.ts +4 -0
- package/dist/browser/process-group-stop-error.d.ts +4 -0
- package/dist/browser/process-groups.d.ts +5 -0
- package/dist/browser/process-runtime.d.ts +15 -0
- package/dist/browser/read-dir-names.d.ts +1 -0
- package/dist/browser/read-process-table.d.ts +6 -0
- package/dist/browser/remove-exited-run-dirs.d.ts +2 -0
- package/dist/browser/remove-path.d.ts +1 -0
- package/dist/browser/run-in-page.d.ts +23 -0
- package/dist/browser/should-keep-run-folders.d.ts +1 -0
- package/dist/browser/start-browser.d.ts +9 -0
- package/dist/browser/start-process-group.d.ts +17 -0
- package/dist/browser/wire-browser-lane-hooks.d.ts +12 -0
- package/dist/build-diet-command.d.ts +1 -0
- package/dist/build-init-scripts.d.ts +3 -0
- package/dist/build-test-selection.d.ts +23 -0
- package/dist/build-unit-command.d.ts +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/define-rig.d.ts +2 -0
- package/dist/find-timeout-arg.d.ts +1 -0
- package/dist/format-no-match-message.d.ts +1 -0
- package/dist/format-no-tests-message.d.ts +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +255 -0
- package/dist/init-files.d.ts +6 -0
- package/dist/is-command-name.d.ts +3 -0
- package/dist/lane-hooks.d.ts +13 -0
- package/dist/make-repo-lane-hooks.d.ts +12 -0
- package/dist/parse-duration.d.ts +1 -0
- package/dist/parse-lane-hook.d.ts +3 -0
- package/dist/parse-lane-start.d.ts +2 -0
- package/dist/parse-rig-config.d.ts +9 -0
- package/dist/parse-test-args.d.ts +5 -0
- package/dist/pick-e2e-dirs.d.ts +1 -0
- package/dist/pick-lane-selection.d.ts +2 -0
- package/dist/read-cli-args.d.ts +5 -0
- package/dist/read-memory-budget.d.ts +8 -0
- package/dist/read-rig-config.d.ts +6 -0
- package/dist/read-tracked-files.d.ts +1 -0
- package/dist/read-worker-count.d.ts +13 -0
- package/dist/resolve-diet-script.d.ts +1 -0
- package/dist/resolve-related-tests.d.ts +6 -0
- package/dist/run-cli.d.ts +9 -0
- package/dist/run-command.d.ts +6 -0
- package/dist/run-init.d.ts +8 -0
- package/dist/run-lane-hooks.d.ts +2 -0
- package/dist/run-lane.d.ts +18 -0
- package/dist/run-related-tests.d.ts +10 -0
- package/dist/run-unit-tests.d.ts +12 -0
- package/dist/run-unit.d.ts +8 -0
- package/dist/standard-lanes.d.ts +8 -0
- package/dist/system-clock.d.ts +5 -0
- package/dist/types.d.ts +42 -0
- package/package.json +41 -3
- package/src/browser/browser-preload.ts +36 -0
- package/src/browser/build-browser-run.ts +32 -0
- package/src/browser/build-lane-environments.ts +33 -0
- package/src/browser/build-recorded-command.ts +12 -0
- package/src/browser/find-browser-run.ts +15 -0
- package/src/browser/find-private-dir-problem.ts +21 -0
- package/src/browser/has-browser-build-script.ts +7 -0
- package/src/browser/has-error-code.ts +3 -0
- package/src/browser/is-zombie-state.ts +4 -0
- package/src/browser/lane-files.ts +56 -0
- package/src/browser/make-browser-lane-hooks.ts +228 -0
- package/src/browser/pick-chrome-dirs.ts +34 -0
- package/src/browser/pick-exit-code-after-tests.ts +5 -0
- package/src/browser/pick-step-after-build.ts +20 -0
- package/src/browser/pick-stop-step.ts +28 -0
- package/src/browser/probe-host-process.ts +11 -0
- package/src/browser/probe-process.ts +35 -0
- package/src/browser/probe-run.ts +38 -0
- package/src/browser/process-group-still-running-error.ts +9 -0
- package/src/browser/process-group-stop-error.ts +10 -0
- package/src/browser/process-groups.ts +45 -0
- package/src/browser/process-runtime.ts +40 -0
- package/src/browser/read-dir-names.ts +15 -0
- package/src/browser/read-process-table.ts +35 -0
- package/src/browser/remove-exited-run-dirs.ts +21 -0
- package/src/browser/remove-path.ts +5 -0
- package/src/browser/run-in-page.ts +103 -0
- package/src/browser/should-keep-run-folders.ts +7 -0
- package/src/browser/start-browser.ts +40 -0
- package/src/browser/start-process-group.ts +168 -0
- package/src/browser/wire-browser-lane-hooks.ts +88 -0
- package/src/build-diet-command.ts +11 -0
- package/src/build-init-scripts.ts +40 -0
- package/src/build-test-selection.ts +124 -0
- package/src/build-unit-command.ts +22 -0
- package/src/cli.ts +73 -0
- package/src/define-rig.ts +195 -0
- package/src/find-timeout-arg.ts +3 -0
- package/src/format-no-match-message.ts +3 -0
- package/src/format-no-tests-message.ts +3 -0
- package/src/index.ts +14 -0
- package/src/init-files.ts +15 -0
- package/src/is-command-name.ts +9 -0
- package/src/lane-hooks.ts +15 -0
- package/src/make-repo-lane-hooks.ts +70 -0
- package/src/parse-duration.ts +14 -0
- package/src/parse-lane-hook.ts +20 -0
- package/src/parse-lane-start.ts +30 -0
- package/src/parse-rig-config.ts +96 -0
- package/src/parse-test-args.ts +14 -0
- package/src/pick-e2e-dirs.ts +25 -0
- package/src/pick-lane-selection.ts +14 -0
- package/src/read-cli-args.ts +57 -0
- package/src/read-memory-budget.ts +65 -0
- package/src/read-rig-config.ts +32 -0
- package/src/read-tracked-files.ts +20 -0
- package/src/read-worker-count.ts +40 -0
- package/src/resolve-diet-script.ts +7 -0
- package/src/resolve-related-tests.ts +52 -0
- package/src/run-cli.ts +68 -0
- package/src/run-command.ts +36 -0
- package/src/run-init.ts +71 -0
- package/src/run-lane-hooks.ts +49 -0
- package/src/run-lane.ts +138 -0
- package/src/run-related-tests.ts +113 -0
- package/src/run-unit-tests.ts +42 -0
- package/src/run-unit.ts +68 -0
- package/src/standard-lanes.ts +23 -0
- package/src/system-clock.ts +10 -0
- package/src/types.ts +75 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Clock } from '../system-clock.ts';
|
|
2
|
+
import { systemClock } from '../system-clock.ts';
|
|
3
|
+
|
|
4
|
+
export interface SpawnedProcess {
|
|
5
|
+
readonly pid: number;
|
|
6
|
+
|
|
7
|
+
readonly stdout: ReadableStream<Uint8Array> | null;
|
|
8
|
+
|
|
9
|
+
readonly exited: Promise<number>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SpawnOptions {
|
|
13
|
+
readonly cwd: string;
|
|
14
|
+
|
|
15
|
+
readonly environment: Readonly<Record<string, string | undefined>>;
|
|
16
|
+
|
|
17
|
+
readonly stdout: 'inherit' | 'pipe';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ProcessRuntime extends Clock {
|
|
21
|
+
// Starts the command as the leader of a process group of its own.
|
|
22
|
+
readonly spawn: (command: readonly string[], options: SpawnOptions) => SpawnedProcess;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const processRuntime: ProcessRuntime = {
|
|
26
|
+
spawn: (command, options) => {
|
|
27
|
+
const child = Bun.spawn([...command], {
|
|
28
|
+
cwd: options.cwd,
|
|
29
|
+
env: options.environment,
|
|
30
|
+
detached: true,
|
|
31
|
+
stderr: 'inherit',
|
|
32
|
+
stdin: 'inherit',
|
|
33
|
+
stdout: options.stdout,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return { pid: child.pid, stdout: child.stdout ?? null, exited: child.exited };
|
|
37
|
+
},
|
|
38
|
+
now: systemClock.now,
|
|
39
|
+
sleep: systemClock.sleep,
|
|
40
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
|
2
|
+
import { hasErrorCode } from './has-error-code.ts';
|
|
3
|
+
|
|
4
|
+
// Lists no names for a folder that does not exist.
|
|
5
|
+
export async function readDirNames(path: string): Promise<readonly string[]> {
|
|
6
|
+
try {
|
|
7
|
+
return await readdir(path);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (hasErrorCode(error, 'ENOENT')) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface ProcessEntry {
|
|
2
|
+
readonly pid: number;
|
|
3
|
+
|
|
4
|
+
readonly groupID: number;
|
|
5
|
+
|
|
6
|
+
readonly state: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// Null when `ps` fails to start or exits nonzero. `ps -A` lists every process, so a nonzero exit
|
|
10
|
+
// is a failed listing, never a sign that a process has exited.
|
|
11
|
+
export function readProcessTable(): readonly ProcessEntry[] | null {
|
|
12
|
+
const listing = runListing();
|
|
13
|
+
|
|
14
|
+
if (listing === null || listing.exitCode !== 0) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return listing.stdout
|
|
19
|
+
.toString()
|
|
20
|
+
.split('\n')
|
|
21
|
+
.map((line) => line.trim().split(/\s+/u))
|
|
22
|
+
.flatMap(([pid, groupID, state]) =>
|
|
23
|
+
pid === undefined || groupID === undefined || state === undefined
|
|
24
|
+
? []
|
|
25
|
+
: [{ pid: Number(pid), groupID: Number(groupID), state }],
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function runListing() {
|
|
30
|
+
try {
|
|
31
|
+
return Bun.spawnSync(['ps', '-A', '-o', 'pid=,pgid=,stat=']);
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import type { LaneFiles } from './lane-files.ts';
|
|
3
|
+
|
|
4
|
+
// A run's folder takes the name of the lane's process ID. A folder whose run has ended, with its
|
|
5
|
+
// lane and every process group it recorded exited, belongs to an older run that a signal or a
|
|
6
|
+
// crash stopped before its cleanup.
|
|
7
|
+
export async function removeExitedRunDirs(
|
|
8
|
+
parentDir: string,
|
|
9
|
+
files: Pick<LaneFiles, 'readDirNames' | 'removePath'>,
|
|
10
|
+
probeRun: (runID: string) => Promise<boolean>,
|
|
11
|
+
): Promise<void> {
|
|
12
|
+
const entries = await files.readDirNames(parentDir);
|
|
13
|
+
|
|
14
|
+
const runIDs = entries.filter((entry) => /^\d+$/u.test(entry));
|
|
15
|
+
|
|
16
|
+
const isRunning = await Promise.all(runIDs.map((runID) => probeRun(runID)));
|
|
17
|
+
|
|
18
|
+
const exited = runIDs.filter((_, index) => isRunning[index] !== true);
|
|
19
|
+
|
|
20
|
+
await Promise.all(exited.map((runID) => files.removePath(join(parentDir, runID))));
|
|
21
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
interface PageResultIssue {
|
|
2
|
+
readonly message: string;
|
|
3
|
+
|
|
4
|
+
readonly path?: readonly (PropertyKey | { readonly key: PropertyKey })[] | undefined;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
type PageResult<Output> =
|
|
8
|
+
| { readonly value: Output; readonly issues?: undefined }
|
|
9
|
+
| { readonly issues: readonly PageResultIssue[] };
|
|
10
|
+
|
|
11
|
+
// The part of a Standard Schema (standardschema.dev) that the helper reads, so a zod, valibot or
|
|
12
|
+
// arktype schema parses the result with no dependency here.
|
|
13
|
+
export interface PageResultSchema<Input, Output> {
|
|
14
|
+
readonly '~standard': {
|
|
15
|
+
readonly validate: (value: unknown) => PageResult<Output> | Promise<PageResult<Output>>;
|
|
16
|
+
|
|
17
|
+
readonly types?: { readonly input: Input; readonly output: Output } | undefined;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// The function runs in the page, so it sees only its argument and the page's globals, and its
|
|
22
|
+
// result crosses back as JSON. The argument crosses as a JSON string that the page parses,
|
|
23
|
+
// because an object literal in the script reads some keys differently from JSON.
|
|
24
|
+
export async function runInPage<Argument, Input, Output>(
|
|
25
|
+
view: Bun.WebView,
|
|
26
|
+
pageFunction: (argument: Argument) => NoInfer<Input> | Promise<NoInfer<Input>>,
|
|
27
|
+
argument: Argument,
|
|
28
|
+
schema: PageResultSchema<Input, Output>,
|
|
29
|
+
): Promise<Output> {
|
|
30
|
+
const source = pageFunction.toString();
|
|
31
|
+
|
|
32
|
+
// The page reads the source as an expression, which a method or a bound function is not.
|
|
33
|
+
if (!isFunctionExpression(source)) {
|
|
34
|
+
throw new TypeError(
|
|
35
|
+
'runInPage takes an arrow function or a function expression, never a method or a bound function',
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// JSON has no undefined, so an undefined argument reaches the page as no argument.
|
|
40
|
+
const call =
|
|
41
|
+
argument === undefined
|
|
42
|
+
? `(${source})()`
|
|
43
|
+
: `(${source})(JSON.parse(${JSON.stringify(formatArgumentJSON(argument))}))`;
|
|
44
|
+
|
|
45
|
+
const value: unknown = await view.evaluate(call);
|
|
46
|
+
const result = await schema['~standard'].validate(value);
|
|
47
|
+
|
|
48
|
+
if (result.issues !== undefined) {
|
|
49
|
+
throw new Error(`the page's result fails its schema: ${formatIssues(result.issues)}`, {
|
|
50
|
+
cause: result.issues,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return result.value;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The Transpiler starts on the first call, because this module loads under Node, which has no
|
|
58
|
+
// Bun global, when oxlint reads a repo's rig.config.ts.
|
|
59
|
+
function isFunctionExpression(source: string): boolean {
|
|
60
|
+
const pageCodeTranspiler = new Bun.Transpiler({ loader: 'js' });
|
|
61
|
+
|
|
62
|
+
try {
|
|
63
|
+
pageCodeTranspiler.transformSync(`(${source});`);
|
|
64
|
+
|
|
65
|
+
return true;
|
|
66
|
+
} catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// JSON turns a Date into a string and a Map into an empty object, so an argument that JSON changes
|
|
72
|
+
// would reach the page as a value that its type does not describe.
|
|
73
|
+
function formatArgumentJSON(argument: unknown): string {
|
|
74
|
+
const json: unknown = JSON.stringify(argument);
|
|
75
|
+
|
|
76
|
+
if (typeof json !== 'string' || !isJSONUnchanged(json, argument)) {
|
|
77
|
+
throw new TypeError('runInPage takes an argument that JSON carries to the page unchanged');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return json;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isJSONUnchanged(json: string, argument: unknown): boolean {
|
|
84
|
+
const parsed: unknown = JSON.parse(json);
|
|
85
|
+
|
|
86
|
+
return Bun.deepEquals(parsed, argument);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function formatIssues(issues: readonly PageResultIssue[]): string {
|
|
90
|
+
return issues
|
|
91
|
+
.map((issue) => {
|
|
92
|
+
const path = (issue.path ?? []).map((segment) => formatPathSegment(segment)).join('.');
|
|
93
|
+
|
|
94
|
+
return path === '' ? issue.message : `${path}: ${issue.message}`;
|
|
95
|
+
})
|
|
96
|
+
.join('; ');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function formatPathSegment(segment: PropertyKey | { readonly key: PropertyKey }): string {
|
|
100
|
+
const key = typeof segment === 'object' ? segment.key : segment;
|
|
101
|
+
|
|
102
|
+
return String(key);
|
|
103
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ProcessGroupStopError } from './process-group-stop-error.ts';
|
|
2
|
+
|
|
3
|
+
// A process group that the lane could not stop can still write to the run's folders, and the
|
|
4
|
+
// sweep of a later run needs its record, so the lane keeps them until the group has exited.
|
|
5
|
+
export function shouldKeepRunFolders(error: unknown): boolean {
|
|
6
|
+
return error instanceof ProcessGroupStopError;
|
|
7
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { buildDirVariable, findBrowserRun, runIDVariable } from './find-browser-run.ts';
|
|
3
|
+
|
|
4
|
+
export interface BrowserStart {
|
|
5
|
+
readonly repoRoot: string;
|
|
6
|
+
|
|
7
|
+
readonly hostName: string;
|
|
8
|
+
|
|
9
|
+
readonly environment: Readonly<Record<string, string | undefined>>;
|
|
10
|
+
|
|
11
|
+
readonly profileName: string;
|
|
12
|
+
|
|
13
|
+
readonly readDirNames: (path: string) => Promise<readonly string[]>;
|
|
14
|
+
|
|
15
|
+
readonly openView: (profileDir: string) => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function startBrowser(start: BrowserStart): Promise<void> {
|
|
19
|
+
const run = findBrowserRun(start.repoRoot, start.hostName, start.environment);
|
|
20
|
+
|
|
21
|
+
if (run === null) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
`the browser preload needs the run that the browser lane sets up, and ${runIDVariable} names none; run the suites with \`rig browser\``,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const buildDir = start.environment[buildDirVariable];
|
|
28
|
+
|
|
29
|
+
if (buildDir !== undefined) {
|
|
30
|
+
const entries = await start.readDirNames(buildDir);
|
|
31
|
+
|
|
32
|
+
if (entries.length === 0) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`the browser run ${run.runID} has no build in ${buildDir}; the build:browser script writes it to ${buildDirVariable}`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
await start.openView(join(run.profilesDir, start.profileName));
|
|
40
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { match } from 'ts-pattern';
|
|
2
|
+
import type { StopPhase } from './pick-stop-step.ts';
|
|
3
|
+
import { pickStopStep } from './pick-stop-step.ts';
|
|
4
|
+
import { ProcessGroupStillRunningError } from './process-group-still-running-error.ts';
|
|
5
|
+
import { ProcessGroupStopError } from './process-group-stop-error.ts';
|
|
6
|
+
import type { ProcessGroups } from './process-groups.ts';
|
|
7
|
+
import type { ProcessRuntime } from './process-runtime.ts';
|
|
8
|
+
|
|
9
|
+
export interface ProcessGroupOptions {
|
|
10
|
+
readonly cwd: string;
|
|
11
|
+
|
|
12
|
+
readonly environment: Readonly<Record<string, string | undefined>>;
|
|
13
|
+
|
|
14
|
+
readonly signal: AbortSignal;
|
|
15
|
+
|
|
16
|
+
readonly stdout: 'inherit' | 'pipe';
|
|
17
|
+
|
|
18
|
+
readonly stopDeadlineMs: number;
|
|
19
|
+
|
|
20
|
+
readonly groups: ProcessGroups;
|
|
21
|
+
|
|
22
|
+
readonly runtime: ProcessRuntime;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface StartedProcessGroup {
|
|
26
|
+
readonly pid: number;
|
|
27
|
+
|
|
28
|
+
readonly stdout: ReadableStream<Uint8Array> | null;
|
|
29
|
+
|
|
30
|
+
// Settles once the command and every process left in its group have exited, and rejects with
|
|
31
|
+
// ProcessGroupStillRunningError when the group outlives SIGKILL.
|
|
32
|
+
readonly exited: Promise<number>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The command runs in a process group of its own, because `bun test` and a build start processes
|
|
36
|
+
// of their own, such as Chrome, that a signal to the command alone leaves running.
|
|
37
|
+
export function startProcessGroup(
|
|
38
|
+
command: readonly string[],
|
|
39
|
+
options: ProcessGroupOptions,
|
|
40
|
+
): StartedProcessGroup {
|
|
41
|
+
const child = options.runtime.spawn(command, {
|
|
42
|
+
cwd: options.cwd,
|
|
43
|
+
environment: options.environment,
|
|
44
|
+
stdout: options.stdout,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const abortStops: Promise<Error | null>[] = [];
|
|
48
|
+
const abortStopFailed = Promise.withResolvers<Error>();
|
|
49
|
+
|
|
50
|
+
const handleAbort = () => {
|
|
51
|
+
abortStops.push(captureFailure(stopGroup(child.pid, options), abortStopFailed.resolve));
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
options.signal.addEventListener('abort', handleAbort, { once: true });
|
|
55
|
+
|
|
56
|
+
if (options.signal.aborted) {
|
|
57
|
+
handleAbort();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const exited = (async () => {
|
|
61
|
+
try {
|
|
62
|
+
// A leader that outlives SIGKILL never exits, so a failed stop settles the wait instead.
|
|
63
|
+
const leaderOutcome = await Promise.race([child.exited, abortStopFailed.promise]);
|
|
64
|
+
|
|
65
|
+
if (leaderOutcome instanceof Error) {
|
|
66
|
+
throw leaderOutcome;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const abortStopFailures = await Promise.all(abortStops);
|
|
70
|
+
|
|
71
|
+
const abortStopFailure = abortStopFailures.find((failure) => failure !== null);
|
|
72
|
+
|
|
73
|
+
if (abortStopFailure !== undefined) {
|
|
74
|
+
throw abortStopFailure;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
await stopGroup(child.pid, options);
|
|
78
|
+
|
|
79
|
+
return leaderOutcome;
|
|
80
|
+
} finally {
|
|
81
|
+
options.signal.removeEventListener('abort', handleAbort);
|
|
82
|
+
}
|
|
83
|
+
})();
|
|
84
|
+
|
|
85
|
+
return { pid: child.pid, stdout: child.stdout, exited };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// An abort stops the group while the command still runs, and its failure belongs to the command's
|
|
89
|
+
// exit, so it waits there instead of rejecting with no handler.
|
|
90
|
+
async function captureFailure(
|
|
91
|
+
stop: Promise<void>,
|
|
92
|
+
onFailure: (failure: Error) => void,
|
|
93
|
+
): Promise<Error | null> {
|
|
94
|
+
try {
|
|
95
|
+
await stop;
|
|
96
|
+
|
|
97
|
+
return null;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
const failure =
|
|
100
|
+
error instanceof Error
|
|
101
|
+
? error
|
|
102
|
+
: new Error('rig could not stop a process group', { cause: error });
|
|
103
|
+
|
|
104
|
+
onFailure(failure);
|
|
105
|
+
|
|
106
|
+
return failure;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// A failed probe or signal leaves the group's state unknown, so it fails the stop as well.
|
|
111
|
+
async function stopGroup(groupID: number, options: ProcessGroupOptions): Promise<void> {
|
|
112
|
+
try {
|
|
113
|
+
await runStopSteps(groupID, options);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (error instanceof ProcessGroupStopError) {
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
throw new ProcessGroupStopError(groupID, `rig could not stop the process group ${groupID}`, {
|
|
120
|
+
cause: error,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const groupProbeIntervalMs = 50;
|
|
126
|
+
|
|
127
|
+
async function runStopSteps(groupID: number, options: ProcessGroupOptions): Promise<void> {
|
|
128
|
+
const stop: { phase: StopPhase; deadlineMs: number } = {
|
|
129
|
+
phase: 'before-stop',
|
|
130
|
+
deadlineMs: Number.POSITIVE_INFINITY,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
for (;;) {
|
|
134
|
+
const step = pickStopStep({
|
|
135
|
+
phase: stop.phase,
|
|
136
|
+
isGroupRunning: options.groups.probeGroup(groupID),
|
|
137
|
+
isPastDeadline: options.runtime.now() >= stop.deadlineMs,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
const isStopped = await match(step)
|
|
141
|
+
.with({ kind: 'done' }, () => true)
|
|
142
|
+
.with({ kind: 'fail' }, () => {
|
|
143
|
+
throw new ProcessGroupStillRunningError(groupID);
|
|
144
|
+
})
|
|
145
|
+
.with({ kind: 'signal' }, (signalStep) => {
|
|
146
|
+
options.groups.signalGroup(groupID, signalStep.signal);
|
|
147
|
+
|
|
148
|
+
stop.phase = match(signalStep.signal)
|
|
149
|
+
.with('SIGTERM', (): StopPhase => 'after-sigterm')
|
|
150
|
+
.with('SIGKILL', (): StopPhase => 'after-sigkill')
|
|
151
|
+
.exhaustive();
|
|
152
|
+
|
|
153
|
+
stop.deadlineMs = options.runtime.now() + options.stopDeadlineMs;
|
|
154
|
+
|
|
155
|
+
return false;
|
|
156
|
+
})
|
|
157
|
+
.with({ kind: 'wait' }, async () => {
|
|
158
|
+
await options.runtime.sleep(groupProbeIntervalMs);
|
|
159
|
+
|
|
160
|
+
return false;
|
|
161
|
+
})
|
|
162
|
+
.exhaustive();
|
|
163
|
+
|
|
164
|
+
if (isStopped) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { hostname, tmpdir, userInfo } from 'node:os';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { buildDietCommand } from '../build-diet-command.ts';
|
|
4
|
+
import type { LaneHooks } from '../lane-hooks.ts';
|
|
5
|
+
import { buildBrowserRun } from './build-browser-run.ts';
|
|
6
|
+
import { buildRecordedCommand } from './build-recorded-command.ts';
|
|
7
|
+
import { hasBrowserBuildScript } from './has-browser-build-script.ts';
|
|
8
|
+
import { laneFiles } from './lane-files.ts';
|
|
9
|
+
import { makeBrowserLaneHooks } from './make-browser-lane-hooks.ts';
|
|
10
|
+
import { pickChromeDirs } from './pick-chrome-dirs.ts';
|
|
11
|
+
import { probeHostProcess } from './probe-host-process.ts';
|
|
12
|
+
import { processGroups } from './process-groups.ts';
|
|
13
|
+
import { processRuntime } from './process-runtime.ts';
|
|
14
|
+
import { startProcessGroup } from './start-process-group.ts';
|
|
15
|
+
|
|
16
|
+
type Environment = Readonly<Record<string, string | undefined>>;
|
|
17
|
+
|
|
18
|
+
export interface BrowserLaneWiring {
|
|
19
|
+
readonly repoRoot: string;
|
|
20
|
+
|
|
21
|
+
readonly runID: string;
|
|
22
|
+
|
|
23
|
+
readonly memory: string | null;
|
|
24
|
+
|
|
25
|
+
readonly dietScript: string;
|
|
26
|
+
|
|
27
|
+
readonly environment: Environment;
|
|
28
|
+
|
|
29
|
+
readonly signal: AbortSignal;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// A build or a test process gets this long to exit after SIGTERM before the lane sends SIGKILL.
|
|
33
|
+
const stopDeadlineMs = 10_000;
|
|
34
|
+
|
|
35
|
+
export async function wireBrowserLaneHooks(wiring: BrowserLaneWiring): Promise<LaneHooks> {
|
|
36
|
+
const run = buildBrowserRun(wiring.repoRoot, hostname(), wiring.runID);
|
|
37
|
+
const userID = userInfo().uid;
|
|
38
|
+
|
|
39
|
+
const manifest: unknown = await Bun.file(join(wiring.repoRoot, 'package.json')).json();
|
|
40
|
+
|
|
41
|
+
// Each command runs under diet, inside the process group that the lane records.
|
|
42
|
+
const startCommand = (
|
|
43
|
+
command: readonly string[],
|
|
44
|
+
environment: Environment,
|
|
45
|
+
signal: AbortSignal,
|
|
46
|
+
pendingRecord: string,
|
|
47
|
+
) => {
|
|
48
|
+
const capped = buildDietCommand(wiring.dietScript, wiring.memory, command);
|
|
49
|
+
|
|
50
|
+
const group = startProcessGroup(buildRecordedCommand(run.groupsDir, pendingRecord, capped), {
|
|
51
|
+
cwd: wiring.repoRoot,
|
|
52
|
+
environment,
|
|
53
|
+
signal,
|
|
54
|
+
stdout: 'inherit',
|
|
55
|
+
stopDeadlineMs,
|
|
56
|
+
groups: processGroups,
|
|
57
|
+
runtime: processRuntime,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return group.exited;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return makeBrowserLaneHooks({
|
|
64
|
+
run,
|
|
65
|
+
chromeDirs: pickChromeDirs(tmpdir(), userID, run),
|
|
66
|
+
userID,
|
|
67
|
+
preloadPath: join(import.meta.dir, 'browser-preload.ts'),
|
|
68
|
+
environment: wiring.environment,
|
|
69
|
+
signal: wiring.signal,
|
|
70
|
+
files: laneFiles,
|
|
71
|
+
probeProcess: probeHostProcess,
|
|
72
|
+
groups: processGroups,
|
|
73
|
+
reportCleanupError: (error) => {
|
|
74
|
+
console.error('the browser lane could not remove a folder:', error);
|
|
75
|
+
},
|
|
76
|
+
runBuild: hasBrowserBuildScript(manifest)
|
|
77
|
+
? (environment, signal, pendingRecord) =>
|
|
78
|
+
startCommand(
|
|
79
|
+
[process.execPath, 'run', 'build:browser'],
|
|
80
|
+
environment,
|
|
81
|
+
signal,
|
|
82
|
+
pendingRecord,
|
|
83
|
+
)
|
|
84
|
+
: null,
|
|
85
|
+
runTests: (testArgs, environment, signal, pendingRecord) =>
|
|
86
|
+
startCommand([process.execPath, 'test', ...testArgs], environment, signal, pendingRecord),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// A command under diet's cap. diet's own script runs by path through bash, so the cap never
|
|
2
|
+
// depends on a PATH lookup.
|
|
3
|
+
export function buildDietCommand(
|
|
4
|
+
dietScript: string,
|
|
5
|
+
memory: string | null,
|
|
6
|
+
command: readonly string[],
|
|
7
|
+
): readonly string[] {
|
|
8
|
+
const budget = memory === null ? [] : ['--budget', memory];
|
|
9
|
+
|
|
10
|
+
return ['bash', dietScript, ...budget, '--', ...command];
|
|
11
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { pickE2EDirs } from './pick-e2e-dirs.ts';
|
|
2
|
+
import { standardLanes } from './standard-lanes.ts';
|
|
3
|
+
import type { ResolvedLane, RigConfig } from './types.ts';
|
|
4
|
+
|
|
5
|
+
export type PackageScripts = Readonly<Record<string, string>>;
|
|
6
|
+
|
|
7
|
+
// A lane the config adds always gets its script. A built-in lane gets one only when git tracks a
|
|
8
|
+
// test file for it, so a repo without browser tests has no `test:browser` job to run.
|
|
9
|
+
export function buildInitScripts(
|
|
10
|
+
oldScripts: PackageScripts,
|
|
11
|
+
config: RigConfig,
|
|
12
|
+
trackedFiles: readonly string[],
|
|
13
|
+
): PackageScripts {
|
|
14
|
+
const laneScripts = config.lanes
|
|
15
|
+
.filter((lane) => lane.name !== 'unit' && isLaneWanted(lane, trackedFiles))
|
|
16
|
+
.map((lane): [string, string] => [`test:${lane.name}`, `rig ${lane.name}`]);
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
...oldScripts,
|
|
20
|
+
test: 'rig unit',
|
|
21
|
+
'test:related': 'rig related',
|
|
22
|
+
...Object.fromEntries(laneScripts),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isLaneWanted(lane: ResolvedLane, trackedFiles: readonly string[]): boolean {
|
|
27
|
+
if (!standardLanes.some((standard) => standard.name === lane.name)) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (lane.suffix === null) {
|
|
32
|
+
return pickE2EDirs(trackedFiles).length > 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const suffix = lane.suffix;
|
|
36
|
+
|
|
37
|
+
return trackedFiles.some(
|
|
38
|
+
(file) => file.endsWith(`${suffix}.test.ts`) || file.endsWith(`${suffix}.test.tsx`),
|
|
39
|
+
);
|
|
40
|
+
}
|