@smoothbricks/nx-plugin 0.3.11 → 0.3.13
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 +23 -2
- package/dist/bounded-test-policy.d.ts +26 -0
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +194 -0
- package/dist/cargo-workspace.d.ts +67 -0
- package/dist/cargo-workspace.d.ts.map +1 -0
- package/dist/cargo-workspace.js +215 -0
- package/dist/cross-check-policy.d.ts +60 -1
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +65 -1
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +86 -51
- package/dist/executors/bounded-exec/schema.d.ts +13 -0
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +455 -95
- package/dist/package-target-policy.d.ts +2 -1
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +54 -13
- package/dist/workspace-config-policy.js +32 -35
- package/nextest.toml +79 -0
- package/package.json +2 -1
- package/src/bounded-test-policy.test.ts +304 -0
- package/src/bounded-test-policy.ts +218 -0
- package/src/cargo-workspace.ts +243 -0
- package/src/cross-check-policy.test.ts +24 -0
- package/src/cross-check-policy.ts +80 -1
- package/src/executors/bounded-exec/executor.test.ts +147 -3
- package/src/executors/bounded-exec/executor.ts +103 -53
- package/src/executors/bounded-exec/schema.json +7 -2
- package/src/executors/bounded-exec/schema.ts +13 -0
- package/src/index.test.ts +531 -99
- package/src/index.ts +527 -104
- package/src/package-target-policy.test.ts +50 -0
- package/src/package-target-policy.ts +58 -9
- package/src/workspace-config-policy.test.ts +15 -30
- package/src/workspace-config-policy.ts +32 -39
|
@@ -4,6 +4,7 @@ import { isAbsolute, join } from 'node:path';
|
|
|
4
4
|
import type { BoundedExecOptions } from './schema.js';
|
|
5
5
|
|
|
6
6
|
const DEFAULT_KILL_AFTER_MS = 10_000;
|
|
7
|
+
const REAP_AFTER_FORCE_KILL_MS = 2_000;
|
|
7
8
|
const EXIT_CODE_BY_SIGNAL: Partial<Record<NodeJS.Signals, number>> = {
|
|
8
9
|
SIGHUP: 129,
|
|
9
10
|
SIGINT: 130,
|
|
@@ -19,9 +20,22 @@ export interface BoundedExecResult {
|
|
|
19
20
|
terminalOutput: string;
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Which bound ended the run.
|
|
25
|
+
*
|
|
26
|
+
* `total` is a wall-clock ceiling: it answers "is this run unbounded?".
|
|
27
|
+
* `idle` is a no-progress bound: it answers "is this run wedged?".
|
|
28
|
+
*
|
|
29
|
+
* These are different questions and one number cannot answer both. A ceiling
|
|
30
|
+
* tight enough to catch a wedge promptly is also tight enough to fail a correct
|
|
31
|
+
* run on a busy machine; a ceiling loose enough never to do that cannot catch a
|
|
32
|
+
* wedge promptly. Keeping the two bounds separate is what makes each honest.
|
|
33
|
+
*/
|
|
34
|
+
type ExpiredBound = { kind: 'total' | 'idle'; limitMs: number };
|
|
35
|
+
|
|
22
36
|
interface RunState {
|
|
23
37
|
settled: boolean;
|
|
24
|
-
|
|
38
|
+
expiry: ExpiredBound | null;
|
|
25
39
|
forceKillNeeded: boolean;
|
|
26
40
|
}
|
|
27
41
|
|
|
@@ -44,10 +58,11 @@ export async function runBoundedExec(
|
|
|
44
58
|
const cwd = resolveCwd(options.cwd, context.root);
|
|
45
59
|
const command = buildCommand(options);
|
|
46
60
|
const timeoutMs = options.timeoutMs;
|
|
61
|
+
const idleTimeoutMs = options.idleTimeoutMs;
|
|
47
62
|
const killAfterMs = options.killAfterMs ?? DEFAULT_KILL_AFTER_MS;
|
|
48
63
|
const startedAt = Date.now();
|
|
49
64
|
const outputChunks: string[] = [];
|
|
50
|
-
const state: RunState = { settled: false,
|
|
65
|
+
const state: RunState = { settled: false, expiry: null, forceKillNeeded: false };
|
|
51
66
|
|
|
52
67
|
const child = spawn(command, [], {
|
|
53
68
|
cwd,
|
|
@@ -68,8 +83,26 @@ export async function runBoundedExec(
|
|
|
68
83
|
process.stderr.write(text);
|
|
69
84
|
};
|
|
70
85
|
|
|
71
|
-
|
|
72
|
-
|
|
86
|
+
let idleTimer: NodeJS.Timeout | undefined;
|
|
87
|
+
const armIdleTimer = (): void => {
|
|
88
|
+
if (idleTimeoutMs === undefined || state.settled || state.expiry !== null) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
clearTimeout(idleTimer);
|
|
92
|
+
idleTimer = setTimeout(() => expire({ kind: 'idle', limitMs: idleTimeoutMs }), idleTimeoutMs);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Only the CHILD's own output counts as progress. The diagnostics below go
|
|
96
|
+
// through appendStderr as well, and re-arming on those would let a teardown
|
|
97
|
+
// message extend the very bound that just fired.
|
|
98
|
+
child.stdout?.on('data', (chunk: Buffer | string) => {
|
|
99
|
+
armIdleTimer();
|
|
100
|
+
appendStdout(chunk);
|
|
101
|
+
});
|
|
102
|
+
child.stderr?.on('data', (chunk: Buffer | string) => {
|
|
103
|
+
armIdleTimer();
|
|
104
|
+
appendStderr(chunk);
|
|
105
|
+
});
|
|
73
106
|
|
|
74
107
|
const killChildTree = async (force: boolean): Promise<void> => {
|
|
75
108
|
const pid = child.pid;
|
|
@@ -101,61 +134,75 @@ export async function runBoundedExec(
|
|
|
101
134
|
process.on('SIGTERM', onSigterm);
|
|
102
135
|
process.on('SIGHUP', onSighup);
|
|
103
136
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
state.timedOut = true;
|
|
108
|
-
const elapsedMs = Date.now() - startedAt;
|
|
109
|
-
appendStderr(`\nCommand timed out after ${elapsedMs}ms (timeoutMs=${timeoutMs}, cwd=${cwd}): ${command}\n`);
|
|
110
|
-
void (async () => {
|
|
111
|
-
await ignoreKillError(killChildTree(false));
|
|
112
|
-
if (!state.settled && killAfterMs > 0) {
|
|
113
|
-
await delay(killAfterMs);
|
|
114
|
-
}
|
|
115
|
-
if (!state.settled) {
|
|
116
|
-
state.forceKillNeeded = true;
|
|
117
|
-
appendStderr(`Force-killing timed out command after killAfterMs=${killAfterMs}: ${command}\n`);
|
|
118
|
-
await ignoreKillError(killChildTree(true));
|
|
119
|
-
}
|
|
120
|
-
resolve();
|
|
121
|
-
})();
|
|
122
|
-
}, timeoutMs);
|
|
123
|
-
});
|
|
124
|
-
|
|
137
|
+
// `Promise.withResolvers` would read better here but needs lib es2024; this
|
|
138
|
+
// package inherits lib es2022 from tsconfig.base.json.
|
|
139
|
+
let resolveExit!: (value: { code: number | null; signal: NodeJS.Signals | null }) => void;
|
|
125
140
|
const exitPromise = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve) => {
|
|
141
|
+
resolveExit = resolve;
|
|
126
142
|
child.once('error', (error) => {
|
|
127
143
|
appendStderr(`${error.message}\n`);
|
|
144
|
+
state.settled = true;
|
|
128
145
|
resolve({ code: 1, signal: null });
|
|
129
146
|
});
|
|
130
147
|
child.once('exit', (code, signal) => {
|
|
148
|
+
state.settled = true;
|
|
131
149
|
resolve({ code, signal });
|
|
132
150
|
});
|
|
133
151
|
});
|
|
134
152
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
153
|
+
// Both bounds escalate through one path, so a run expires at most once and
|
|
154
|
+
// the report always names which bound did it. A bare "timed out" would leave
|
|
155
|
+
// the reader unable to tell a wedged toolchain from a loaded machine — the
|
|
156
|
+
// two have opposite fixes.
|
|
157
|
+
const expire = (bound: ExpiredBound): void => {
|
|
158
|
+
if (state.settled || state.expiry !== null) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
state.expiry = bound;
|
|
162
|
+
clearTimeout(totalTimer);
|
|
163
|
+
clearTimeout(idleTimer);
|
|
164
|
+
const elapsedMs = Date.now() - startedAt;
|
|
165
|
+
appendStderr(
|
|
166
|
+
bound.kind === 'idle'
|
|
167
|
+
? `\nCommand made no progress: no output for ${bound.limitMs}ms (idleTimeoutMs=${bound.limitMs}) after ${elapsedMs}ms of runtime (cwd=${cwd}): ${command}\n`
|
|
168
|
+
: `\nCommand timed out after ${elapsedMs}ms (timeoutMs=${bound.limitMs}, cwd=${cwd}): ${command}\n`,
|
|
169
|
+
);
|
|
170
|
+
void (async () => {
|
|
171
|
+
await ignoreKillError(killChildTree(false));
|
|
172
|
+
if (!state.settled && killAfterMs > 0) {
|
|
173
|
+
await delay(killAfterMs);
|
|
174
|
+
}
|
|
175
|
+
if (!state.settled) {
|
|
176
|
+
state.forceKillNeeded = true;
|
|
177
|
+
appendStderr(`Force-killing timed out command after killAfterMs=${killAfterMs}: ${command}\n`);
|
|
178
|
+
await ignoreKillError(killChildTree(true));
|
|
179
|
+
await delay(REAP_AFTER_FORCE_KILL_MS);
|
|
180
|
+
}
|
|
181
|
+
if (!state.settled) {
|
|
182
|
+
resolveExit({ code: 1, signal: 'SIGKILL' });
|
|
183
|
+
}
|
|
184
|
+
})();
|
|
185
|
+
};
|
|
142
186
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
187
|
+
const totalTimer = setTimeout(() => expire({ kind: 'total', limitMs: timeoutMs }), timeoutMs);
|
|
188
|
+
armIdleTimer();
|
|
189
|
+
|
|
190
|
+
const { code: exitCode, signal: exitSignal } = await exitPromise;
|
|
191
|
+
clearTimeout(totalTimer);
|
|
192
|
+
clearTimeout(idleTimer);
|
|
193
|
+
removeSignalHandlers();
|
|
147
194
|
|
|
148
|
-
const code =
|
|
149
|
-
if (code !== 0 &&
|
|
195
|
+
const code = exitCode ?? signalToExitCode(exitSignal);
|
|
196
|
+
if (code !== 0 && state.expiry === null) {
|
|
150
197
|
appendStderr(`Command exited with status ${code}: ${command}\n`);
|
|
151
198
|
}
|
|
152
199
|
|
|
153
|
-
if (state.
|
|
200
|
+
if (state.expiry !== null && !state.forceKillNeeded) {
|
|
154
201
|
appendStderr(`Timed out command exited after graceful termination: ${command}\n`);
|
|
155
202
|
}
|
|
156
203
|
|
|
157
204
|
return {
|
|
158
|
-
success:
|
|
205
|
+
success: state.expiry === null && code === 0,
|
|
159
206
|
terminalOutput: outputChunks.join(''),
|
|
160
207
|
};
|
|
161
208
|
}
|
|
@@ -169,19 +216,22 @@ export function createProcessTreeKiller(): ProcessTreeKiller {
|
|
|
169
216
|
}
|
|
170
217
|
|
|
171
218
|
function killProcessGroup(pid: number, signal: NodeJS.Signals): Promise<void> {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
219
|
+
try {
|
|
220
|
+
// Send signal to the entire process group (negative PID).
|
|
221
|
+
// The executor spawns with detached: true on POSIX, which creates a
|
|
222
|
+
// dedicated process group. Signaling -pid is atomic and catches all
|
|
223
|
+
// descendants, including grandchildren that tree-walk libraries miss
|
|
224
|
+
// when parents die and children get reparented to init.
|
|
225
|
+
process.kill(-pid, signal);
|
|
226
|
+
} catch {
|
|
227
|
+
// ESRCH: process group already exited, or this pid is not a group leader.
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
process.kill(pid, signal);
|
|
231
|
+
} catch {
|
|
232
|
+
// Already reaped.
|
|
233
|
+
}
|
|
234
|
+
return Promise.resolve();
|
|
185
235
|
}
|
|
186
236
|
|
|
187
237
|
function resolveCwd(cwd: string | undefined, root: string): string {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 2,
|
|
3
3
|
"title": "Bounded Exec",
|
|
4
|
-
"description": "Run one shell command
|
|
4
|
+
"description": "Run one shell command under an absolute ceiling and a no-progress bound, with process-tree cleanup.",
|
|
5
5
|
"type": "object",
|
|
6
6
|
"cli": "nx",
|
|
7
7
|
"outputCapture": "pipe",
|
|
@@ -26,7 +26,12 @@
|
|
|
26
26
|
"timeoutMs": {
|
|
27
27
|
"type": "number",
|
|
28
28
|
"minimum": 1,
|
|
29
|
-
"description": "
|
|
29
|
+
"description": "Absolute wall-clock ceiling in milliseconds. A backstop against an unbounded run, not a performance budget."
|
|
30
|
+
},
|
|
31
|
+
"idleTimeoutMs": {
|
|
32
|
+
"type": "number",
|
|
33
|
+
"minimum": 1,
|
|
34
|
+
"description": "Kill the process tree after this many milliseconds with no stdout or stderr. Detects a wedged process independently of machine speed and load. Omit for no progress bound."
|
|
30
35
|
},
|
|
31
36
|
"killAfterMs": {
|
|
32
37
|
"type": "number",
|
|
@@ -3,6 +3,19 @@ export interface BoundedExecOptions {
|
|
|
3
3
|
cwd?: string;
|
|
4
4
|
env?: Record<string, string>;
|
|
5
5
|
timeoutMs: number;
|
|
6
|
+
/**
|
|
7
|
+
* Kill the process tree when it has produced no stdout or stderr for this
|
|
8
|
+
* long. Absent means no progress bound.
|
|
9
|
+
*
|
|
10
|
+
* This is the bound that catches a hang. A wedged process — a flock deadlock
|
|
11
|
+
* between two cargo writers, a transformer waiting on a lock nobody will
|
|
12
|
+
* release, a runner that ignores its own per-test timeout — emits nothing at
|
|
13
|
+
* all, so silence is the signal. Unlike `timeoutMs` it does not scale with
|
|
14
|
+
* machine speed or load: a machine ten times slower still prints progress,
|
|
15
|
+
* just further apart, and each gap is bounded by the slowest single unit of
|
|
16
|
+
* work rather than by the total.
|
|
17
|
+
*/
|
|
18
|
+
idleTimeoutMs?: number;
|
|
6
19
|
killAfterMs?: number;
|
|
7
20
|
forwardAllArgs?: boolean;
|
|
8
21
|
args?: string | string[];
|