codemod 2.0.0-alpha.2 → 2.0.0-alpha.4

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/runtime/host.mjs CHANGED
@@ -8,383 +8,485 @@
8
8
  //
9
9
  // Code here has whatever Node.js gives it: capabilities are not enforced.
10
10
  // The codemod CLI runs workflows in its own sandboxed runtime instead.
11
-
11
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
12
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
13
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
14
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
15
+ });
16
+ }
17
+ return path;
18
+ };
19
+ /**
20
+ * Run a workflow in this Node.js or Bun process.
21
+ *
22
+ * ```ts
23
+ * import { runWorkflow } from "codemod/host";
24
+ *
25
+ * const report = await runWorkflow("./my-codemod", { target: "./repo" });
26
+ * if (report.status !== "succeeded") throw new Error(report.error ?? report.status);
27
+ * ```
28
+ *
29
+ * The workflow's body, function steps and `forEachFile` callbacks run here,
30
+ * so they can be debugged, covered and mocked. Host tasks (`jssg`, `shell`,
31
+ * `codemod`, ...) and the run store go through the native addon.
32
+ *
33
+ * Code here has whatever Node.js gives it: the capabilities codemod.yaml
34
+ * declares are not enforced. The codemod CLI runs workflows in its own
35
+ * sandboxed runtime instead; run packages from the registry with the CLI.
36
+ *
37
+ * @module
38
+ */
12
39
  import "../register.mjs";
13
-
14
- import { readFileSync } from "node:fs";
15
40
  import { isAbsolute, resolve } from "node:path";
16
41
  import { fileURLToPath, pathToFileURL } from "node:url";
17
-
18
42
  import { addon } from "./addon.mjs";
19
- import { context, metricsOf, stateChanges, stepScope } from "./context.mjs";
20
- import {
21
- AwaitingApprovalError,
22
- CanceledError,
23
- checkJson,
24
- createRuntime,
25
- describeError,
26
- fileHandle,
27
- } from "./orchestrator.mjs";
43
+ import { context, stepScope } from "./context.mjs";
44
+ import { ORCHESTRATOR_VERSION } from "./orchestrator-version.mjs";
45
+ import { AwaitingApprovalError, CanceledError, checkJson, createRuntime, describeDuration, describeError, fileHandle, } from "./orchestrator.mjs";
28
46
  import { callbackId } from "./workflow.mjs";
29
-
30
47
  /**
31
- * Run a workflow and resolve with its report, for every outcome.
48
+ * Run a workflow (a workflow file, a package directory, or the workflow
49
+ * module itself). Resolves for every outcome; rejects when the workflow
50
+ * cannot be loaded. YAML workflows run only with the CLI.
32
51
  *
33
- * `workflow` is a workflow file, a package directory, or the workflow module
34
- * itself (`await import("./workflow.ts")`), for test runners that should load
35
- * it through their own module system. A module needs `options.path` (or
52
+ * A module (`await import("./workflow.ts")`), for test runners that should
53
+ * load it through their own module system, needs `options.path` (or
36
54
  * `options.workflowDir`) so relative task paths resolve.
37
55
  */
38
56
  export async function runWorkflow(workflow, options = {}) {
39
- const native = addon();
40
- checkAddon(native);
41
- const { path, load } = locate(workflow, options);
42
- // A workflow that cannot be loaded is not a run: reject.
43
- const module = await load();
44
- const main = module?.default;
45
- if (typeof main !== "function") {
46
- throw new TypeError(`${path} must export a default function`);
47
- }
48
- if (options.captureConsole) captureConsole();
49
- const run = native.Run.open({
50
- workflow: path,
51
- target: resolve(options.target ?? "."),
52
- params: options.params ?? {},
53
- name: options.name,
54
- dryRun: options.dryRun ?? false,
55
- until: options.until,
56
- given: options.given,
57
- approve: options.approve ?? true,
58
- capabilities: options.capabilities ?? [],
59
- store: options.store,
60
- resume: options.resume,
61
- });
62
-
63
- const flight = inFlight();
64
- const runtime = createRuntime(host(run, flight, () => runtime));
65
- flight.closed = () => runtime.isClosed();
66
- const scope = { run, runtime, captureConsole: options.captureConsole ?? false };
67
- const abort = () => run.cancel();
68
- if (options.signal?.aborted) abort();
69
- options.signal?.addEventListener?.("abort", abort, { once: true });
70
-
71
- // How the body ended; `null` when it cannot end any more.
72
- let body;
73
- try {
74
- const input = { params: run.params, target: run.target, dryRun: run.dryRun };
75
- const ended = context
76
- .run(scope, () => main(input))
77
- .then((output) => ({ status: "succeeded", detail: toJson(output) }));
78
- body = await Promise.race([ended, flight.stalled]);
79
- } catch (error) {
80
- body = endingOf(error);
81
- }
82
- // Steps still running finish and are recorded before the run reports;
83
- // none starts from here on.
84
- runtime.close();
85
- await flight.idle();
86
- options.signal?.removeEventListener?.("abort", abort);
87
- const report = run.finish(JSON.stringify(body));
88
- return { ...report, events: run.takeEvents() };
57
+ const native = addon();
58
+ checkAddon(native);
59
+ const { path, load } = locate(workflow, options);
60
+ // A workflow that cannot be loaded is not a run: reject.
61
+ const module = await load();
62
+ const main = module?.default;
63
+ if (typeof main !== "function") {
64
+ throw new TypeError(`${path} must export a default function`);
65
+ }
66
+ if (options.captureConsole)
67
+ captureConsole();
68
+ const run = native.Run.open({
69
+ workflow: path,
70
+ target: resolve(options.target ?? "."),
71
+ params: options.params ?? {},
72
+ name: options.name,
73
+ dryRun: options.dryRun ?? false,
74
+ until: options.until,
75
+ given: options.given,
76
+ approve: options.approve ?? true,
77
+ capabilities: options.capabilities ?? [],
78
+ store: options.store,
79
+ resume: options.resume,
80
+ });
81
+ const flight = inFlight();
82
+ const runtime = createRuntime(host(run, flight, () => runtime));
83
+ flight.closed = () => runtime.isClosed();
84
+ const scope = { run, runtime, captureConsole: options.captureConsole ?? false };
85
+ const abort = () => run.cancel();
86
+ if (options.signal?.aborted)
87
+ abort();
88
+ options.signal?.addEventListener?.("abort", abort, { once: true });
89
+ let body;
90
+ try {
91
+ const input = { params: run.params, target: run.target, dryRun: run.dryRun };
92
+ const ended = context
93
+ .run(scope, async () => main(input))
94
+ .then((output) => ({ status: "succeeded", detail: toJson(output) }));
95
+ body = await Promise.race([ended, flight.stalled]);
96
+ }
97
+ catch (error) {
98
+ body = endingOf(error);
99
+ }
100
+ // Steps still running finish and are recorded before the run reports;
101
+ // none starts from here on.
102
+ runtime.close();
103
+ await flight.idle();
104
+ options.signal?.removeEventListener?.("abort", abort);
105
+ const report = run.finish(JSON.stringify(body));
106
+ return { ...report, events: run.takeEvents() };
89
107
  }
90
-
91
108
  let addonChecked = false;
92
-
93
109
  /**
94
110
  * The addon and this package share the orchestrator: one from another
95
111
  * version would record and replay steps differently.
96
112
  */
97
113
  function checkAddon(native) {
98
- if (addonChecked) return;
99
- const shipped = readFileSync(new URL("./orchestrator.mjs", import.meta.url), "utf8");
100
- if (!shipped.endsWith(native.orchestratorSource())) {
101
- throw new Error(
102
- "the codemod native addon is from another version than the codemod package; reinstall codemod so both match (in a checkout, rebuild with `cargo build -p codemod-node`)",
103
- );
104
- }
105
- addonChecked = true;
114
+ if (addonChecked)
115
+ return;
116
+ // An addon from before the check by version has no `orchestratorVersion`.
117
+ const built = typeof native.orchestratorVersion === "function" ? native.orchestratorVersion() : undefined;
118
+ if (built !== ORCHESTRATOR_VERSION) {
119
+ throw new Error("the codemod native addon is from another version than the codemod package; reinstall codemod so both match (in a checkout, rebuild both: `cargo build -p codemod-node` and `pnpm --filter codemod build`)");
120
+ }
121
+ addonChecked = true;
106
122
  }
107
-
123
+ const CONSOLE_METHODS = ["log", "info", "debug", "warn", "error"];
108
124
  /**
109
125
  * Route `console` output of code that runs in a run started with
110
126
  * `captureConsole` to the run's logs, by step. Other code is untouched.
111
127
  */
112
128
  function captureConsole() {
113
- const key = Symbol.for("codemod.host.console");
114
- if (globalThis[key]) return;
115
- globalThis[key] = true;
116
- for (const method of ["log", "info", "debug", "warn", "error"]) {
117
- const original = console[method].bind(console);
118
- console[method] = (...items) => {
119
- const store = context.getStore();
120
- if (!store?.captureConsole) return original(...items);
121
- store.run.log(store.step?.position ?? null, items.map(formatLogItem).join(" "));
122
- };
123
- }
129
+ const key = Symbol.for("codemod.host.console");
130
+ const shared = globalThis;
131
+ if (shared[key])
132
+ return;
133
+ shared[key] = true;
134
+ for (const method of CONSOLE_METHODS) {
135
+ const original = console[method].bind(console);
136
+ console[method] = (...items) => {
137
+ const store = context.getStore();
138
+ if (!store?.captureConsole)
139
+ return original(...items);
140
+ store.run.log(store.step?.position ?? null, items.map(formatLogItem).join(" "));
141
+ };
142
+ }
124
143
  }
125
-
126
144
  function formatLogItem(value) {
127
- if (typeof value === "string") return value;
128
- if (value instanceof Error) return value.stack || `${value.name}: ${value.message}`;
129
- try {
130
- return JSON.stringify(value) ?? String(value);
131
- } catch {
132
- return String(value);
133
- }
145
+ if (typeof value === "string")
146
+ return value;
147
+ if (value instanceof Error)
148
+ return value.stack || `${value.name}: ${value.message}`;
149
+ try {
150
+ return JSON.stringify(value) ?? String(value);
151
+ }
152
+ catch {
153
+ return String(value);
154
+ }
134
155
  }
135
-
136
156
  /** Where the workflow is, and how to load it. */
137
157
  function locate(workflow, options) {
138
- if (workflow && typeof workflow === "object" && !isUrl(workflow)) {
139
- if (!options.path && !options.workflowDir) {
140
- throw new TypeError(
141
- "runWorkflow(module, { path }): give the workflow file's path, so relative task paths resolve",
142
- );
158
+ if (typeof workflow !== "string" && !isUrl(workflow)) {
159
+ if (!options.path && !options.workflowDir) {
160
+ throw new TypeError("runWorkflow(module, { path }): give the workflow file's path, so relative task paths resolve");
161
+ }
162
+ // The file need not exist: only its directory matters here.
163
+ const file = options.path
164
+ ? resolve(String(options.path))
165
+ : resolve(String(options.workflowDir), "workflow.ts");
166
+ return { path: file, load: async () => workflow };
167
+ }
168
+ const source = isUrl(workflow) ? fileURLToPath(workflow.href) : String(workflow);
169
+ const path = addon().resolveWorkflow(isAbsolute(source) ? source : resolve(source), options.workflowName ?? null);
170
+ if (!/\.m?ts$|\.m?js$/.test(path)) {
171
+ throw new Error(`${path} is not a TypeScript workflow; run YAML workflows with the codemod CLI, or upgrade them with \`codemod workflow upgrade\``);
143
172
  }
144
- // The file need not exist: only its directory matters here.
145
- const file = options.path
146
- ? resolve(String(options.path))
147
- : resolve(String(options.workflowDir), "workflow.ts");
148
- return { path: file, load: async () => workflow };
149
- }
150
- const source = isUrl(workflow) ? fileURLToPath(workflow.href) : String(workflow);
151
- const path = addon().resolveWorkflow(
152
- isAbsolute(source) ? source : resolve(source),
153
- options.workflowName ?? null,
154
- );
155
- if (!/\.m?ts$|\.m?js$/.test(path)) {
156
- throw new Error(
157
- `${path} is not a TypeScript workflow; run YAML workflows with the codemod CLI, or upgrade them with \`codemod workflow upgrade\``,
158
- );
159
- }
160
- return { path, load: () => import(pathToFileURL(path).href) };
173
+ return { path, load: () => import(__rewriteRelativeImportExtension(pathToFileURL(path).href)) };
161
174
  }
162
-
175
+ function isRecord(value) {
176
+ return value !== null && typeof value === "object";
177
+ }
178
+ // Not `instanceof URL`: test environments may have their own URL class.
163
179
  function isUrl(value) {
164
- return value !== null && typeof value === "object" && typeof value.href === "string";
180
+ return isRecord(value) && typeof value.href === "string";
165
181
  }
166
-
167
- /**
168
- * Tracks steps in flight. `stalled` resolves (with `null`) when the run is
169
- * closed and nothing is running: the body may be waiting on steps that will
170
- * never start, as after a test's `until` step. `idle()` resolves once nothing
171
- * is running.
172
- */
173
182
  function inFlight() {
174
- let count = 0;
175
- let stall;
176
- let waiters = [];
177
- const flight = {
178
- closed: () => false,
179
- stalled: new Promise((resolvePromise) => (stall = resolvePromise)),
180
- begin() {
181
- count++;
182
- },
183
- end() {
184
- count--;
185
- flight.check();
186
- },
187
- /** Look again once the body has reacted to whatever just happened. */
188
- check() {
189
- setImmediate(() => {
190
- if (count !== 0) return;
191
- if (flight.closed()) stall(null);
192
- for (const waiter of waiters.splice(0)) waiter();
193
- });
194
- },
195
- idle() {
196
- return new Promise((resolvePromise) => {
197
- waiters.push(resolvePromise);
198
- flight.check();
199
- });
200
- },
201
- };
202
- return flight;
183
+ let count = 0;
184
+ let stall = () => { };
185
+ const waiters = [];
186
+ const flight = {
187
+ closed: () => false,
188
+ stalled: new Promise((resolvePromise) => (stall = resolvePromise)),
189
+ begin() {
190
+ count++;
191
+ },
192
+ end() {
193
+ count--;
194
+ flight.check();
195
+ },
196
+ check() {
197
+ setImmediate(() => {
198
+ if (count !== 0)
199
+ return;
200
+ if (flight.closed())
201
+ stall(null);
202
+ for (const waiter of waiters.splice(0))
203
+ waiter();
204
+ });
205
+ },
206
+ idle() {
207
+ return new Promise((resolvePromise) => {
208
+ waiters.push(resolvePromise);
209
+ flight.check();
210
+ });
211
+ },
212
+ };
213
+ return flight;
203
214
  }
204
-
205
215
  function host(run, flight, runtime) {
206
- // Replayed steps settle one per turn of the event loop, so the body reacts
207
- // to each before the next.
208
- let replaying = false;
209
- const replay = () => {
210
- replaying = runtime().replayNext();
211
- if (replaying) setImmediate(replay);
212
- };
213
- return {
214
- history: () => run.history(),
215
- record: (entry, ms) => run.record(JSON.stringify(entry), ms),
216
- event: (event) => run.event(JSON.stringify(event)),
217
- canceled: () => run.refusesSteps(),
218
- now: () => Date.now(),
219
- advanceClock() {},
220
- testPlan: () => run.testPlan(),
221
- stop() {
222
- run.stop();
223
- flight.check();
224
- },
225
- wake() {
226
- if (replaying) return;
227
- replaying = true;
228
- setImmediate(replay);
229
- },
230
- evaluate: (kind, expression, scope) => run.evaluate(kind, expression, JSON.stringify(scope)),
231
- callbackId,
232
- async start(request) {
233
- const { fn, ...task } = request;
234
- flight.begin();
235
- try {
236
- if (fn) return await runInPlace(run, task, fn);
237
- try {
238
- return await run.runTask(JSON.stringify(task));
239
- } catch (error) {
240
- throw payloadOf(error);
216
+ // Replayed steps settle one per turn of the event loop, so the body reacts
217
+ // to each before the next.
218
+ let replaying = false;
219
+ const replay = () => {
220
+ replaying = runtime().replayNext();
221
+ if (replaying)
222
+ setImmediate(replay);
223
+ };
224
+ return {
225
+ history: () => run.history(),
226
+ record: (entry, ms) => run.record(JSON.stringify(entry), ms),
227
+ event: (event) => run.event(JSON.stringify(event)),
228
+ canceled: () => run.refusesSteps(),
229
+ now: () => Date.now(),
230
+ advanceClock() { },
231
+ testPlan: () => run.testPlan(),
232
+ stop() {
233
+ run.stop();
234
+ flight.check();
235
+ },
236
+ wake() {
237
+ if (replaying)
238
+ return;
239
+ replaying = true;
240
+ setImmediate(replay);
241
+ },
242
+ evaluate: (kind, expression, scope) => run.evaluate(kind, expression, JSON.stringify(scope)),
243
+ callbackId,
244
+ async start(request) {
245
+ const { fn, ...task } = request;
246
+ flight.begin();
247
+ try {
248
+ if (fn && task.task.kind === "jssg") {
249
+ return await reduceInPlace(run, task, fn);
250
+ }
251
+ if (fn)
252
+ return await within(task.timeoutMs, (limit) => runInPlace(run, task, fn, limit));
253
+ return await runNative(run, task);
254
+ }
255
+ finally {
256
+ flight.end();
257
+ }
258
+ },
259
+ };
260
+ }
261
+ async function runNative(run, task) {
262
+ try {
263
+ return await run.runTask(JSON.stringify(task));
264
+ }
265
+ catch (error) {
266
+ throw payloadOf(error);
267
+ }
268
+ }
269
+ /**
270
+ * `jssg(path, { reduce, initial })`: the transform runs natively and returns
271
+ * every file's data, which is then folded here before applying any edits.
272
+ */
273
+ async function reduceInPlace(run, task, fn) {
274
+ const { reduce, ...content } = task.task;
275
+ const outer = context.getStore();
276
+ if (!outer)
277
+ throw new Error("a step started outside its run");
278
+ const { prepared, folded } = await within(task.timeoutMs, async (limit) => {
279
+ const prepared = await run
280
+ .prepareJssg(JSON.stringify({ ...task, task: content }))
281
+ .catch((error) => {
282
+ throw payloadOf(error);
283
+ });
284
+ const step = stepScope(task.position, task.label);
285
+ // A copy, so a reducer that changes its accumulator can't change a retry's.
286
+ let folded = structuredClone(reduce.initial);
287
+ for (const entry of prepared.data) {
288
+ limit.check();
289
+ if (run.refusesSteps())
290
+ throw new CanceledError();
291
+ folded = (await context.run({ ...outer, step }, () => fn(folded, entry)));
292
+ // A failure reported with failStep() stands even if the reducer caught it.
293
+ if (step.failure !== undefined)
294
+ throw new Error(step.failure);
241
295
  }
242
- } finally {
243
- flight.end();
244
- }
245
- },
246
- };
296
+ if (folded === undefined)
297
+ folded = null;
298
+ checkJson(folded, "the reducer's result");
299
+ const data = JSON.stringify(folded);
300
+ limit.check();
301
+ return { prepared, folded: data };
302
+ });
303
+ // Once the deadline has been met, wait for the commit instead of racing it
304
+ // with the timer: a step must not report a timeout after applying its edits.
305
+ try {
306
+ return await prepared.commit(folded);
307
+ }
308
+ catch (error) {
309
+ throw payloadOf(error);
310
+ }
247
311
  }
248
-
249
312
  /** A host task rejected with `{ name, message }` as JSON in the message. */
250
313
  function payloadOf(error) {
251
- try {
252
- const payload = JSON.parse(error.message);
253
- if (payload && typeof payload.message === "string") return payload;
254
- } catch {
255
- // a plain message
256
- }
257
- return { name: "Error", message: error?.message ?? String(error) };
258
- }
259
-
260
- // ----------------------------------------------------------- in place ---
261
-
262
- async function runInPlace(run, task, fn) {
263
- const outer = context.getStore();
264
- if (task.task.kind === "function") {
265
- const step = stepScope(task.position, task.label, task.state);
266
- let value;
314
+ const message = isRecord(error) && typeof error.message === "string" ? error.message : undefined;
267
315
  try {
268
- value = await context.run({ ...outer, step }, () => fn());
269
- } finally {
270
- // What the step measured counts even when it then fails.
271
- flushMetrics(run, step);
316
+ const payload = JSON.parse(message ?? "");
317
+ if (isRecord(payload) && typeof payload.message === "string") {
318
+ return { name: String(payload.name ?? "Error"), message: payload.message };
319
+ }
272
320
  }
273
- // A failure reported with failStep() stands even if the code caught it.
274
- if (step.failure !== undefined) throw new Error(step.failure);
275
- const result = value === undefined ? null : value;
276
- checkJson(result, "the step's result");
277
- return outcome(result, step);
278
- }
279
- if (task.task.kind === "forEachFile") return forEachFile(run, outer, task, fn);
280
- throw new Error(`cannot run a ${task.task.kind} task in place`);
321
+ catch {
322
+ // a plain message
323
+ }
324
+ return { name: "Error", message: message ?? String(error) };
281
325
  }
282
-
283
- function outcome(result, step) {
284
- const state = stateChanges(step);
285
- return Object.keys(state).length ? { result, state } : { result };
326
+ /**
327
+ * Run `work` for at most `ms`. JavaScript that is running cannot be stopped
328
+ * from outside: when the time is up the step fails, `limit.expired` tells
329
+ * the work to stop where it can, and whatever it still does is discarded.
330
+ */
331
+ async function within(ms, work) {
332
+ const started = performance.now();
333
+ const timeout = (ms) => ({
334
+ name: "Error",
335
+ message: `timed out after ${describeDuration(ms)}`,
336
+ });
337
+ const limit = {
338
+ expired: false,
339
+ check() {
340
+ if (ms !== undefined && (limit.expired || performance.now() - started >= ms))
341
+ throw timeout(ms);
342
+ },
343
+ };
344
+ if (ms === undefined)
345
+ return work(limit);
346
+ let timer;
347
+ const late = new Promise((_, reject) => {
348
+ timer = setTimeout(() => {
349
+ limit.expired = true;
350
+ reject(timeout(ms));
351
+ }, ms);
352
+ });
353
+ try {
354
+ return await Promise.race([work(limit), late]);
355
+ }
356
+ finally {
357
+ clearTimeout(timer);
358
+ }
286
359
  }
287
-
288
- function flushMetrics(run, step) {
289
- const metrics = metricsOf(step);
290
- if (Object.keys(metrics).length) run.addMetrics(JSON.stringify(metrics));
291
- return metrics;
360
+ async function runInPlace(run, task, fn, limit) {
361
+ const outer = context.getStore();
362
+ if (!outer)
363
+ throw new Error("a step started outside its run");
364
+ if (task.task.kind === "function") {
365
+ const step = stepScope(task.position, task.label);
366
+ const value = await context.run({ ...outer, step }, () => fn());
367
+ // A failure reported with failStep() stands even if the code caught it.
368
+ if (step.failure !== undefined)
369
+ throw new Error(step.failure);
370
+ const result = value === undefined ? null : value;
371
+ checkJson(result, "the step's result");
372
+ return { result };
373
+ }
374
+ if (task.task.kind === "forEachFile")
375
+ return forEachFile(run, outer, task, fn, limit);
376
+ throw new Error(`cannot run a ${task.task.kind} task in place`);
292
377
  }
293
-
294
- async function forEachFile(run, outer, task, fn) {
295
- const { selector } = task.task;
296
- const { language, files } = await run.selectFiles(JSON.stringify(selector));
297
- const step = stepScope(task.position, task.label, task.state, language ?? undefined);
298
- const total = files.length;
299
- const every = Math.max(1, Math.floor(total / 100));
300
- let processed = 0;
301
- const outputs = Array.from({ length: total });
302
- const failures = [];
303
- const effects = [];
304
- const concurrency = selector.concurrency ?? 1;
305
-
306
- const one = async (index) => {
307
- const [path, text] = files[index];
308
- const { file, effects: done, close } = fileHandle(path, text, step.language);
309
- try {
310
- step.failure = undefined;
311
- const output = await context.run({ ...outer, step }, () => fn(file));
312
- // A failure reported with failFile() stands even if the callback
313
- // caught it. With files in flight together it cannot be told whose it
314
- // was, as in the CLI.
315
- if (step.failure !== undefined && concurrency === 1) {
316
- failures.push({ path, error: step.failure });
317
- return;
318
- }
319
- if (output !== undefined) {
320
- checkJson(output, "return value");
321
- outputs[index] = { output };
322
- }
323
- if (done.length) effects.push({ path, original: text, effects: done });
324
- } catch (error) {
325
- failures.push({ path, error: describeError(error) });
326
- } finally {
327
- close();
328
- processed++;
329
- if (processed === total || processed % every === 0) {
330
- run.progress(task.position, processed, total);
331
- }
378
+ async function forEachFile(run, outer, task, fn, limit) {
379
+ const selector = task.task.selector;
380
+ const { language, files } = await run.selectFiles(JSON.stringify(selector));
381
+ const step = stepScope(task.position, task.label, language ?? undefined);
382
+ const total = files.length;
383
+ const every = Math.max(1, Math.floor(total / 100));
384
+ let processed = 0;
385
+ const outputs = Array.from({ length: total });
386
+ const failures = [];
387
+ const effects = [];
388
+ const concurrency = selector.concurrency ?? 1;
389
+ const one = async (index, [path, text]) => {
390
+ const { file, effects: done, close } = fileHandle(path, text, step.language);
391
+ try {
392
+ step.failure = undefined;
393
+ const output = await context.run({ ...outer, step }, () => fn(file));
394
+ // A failure reported with failFile() stands even if the callback
395
+ // caught it. With files in flight together it cannot be told whose it
396
+ // was, as in the CLI.
397
+ if (step.failure !== undefined && concurrency === 1) {
398
+ failures.push({ path, error: step.failure });
399
+ return;
400
+ }
401
+ if (output !== undefined) {
402
+ checkJson(output, "return value");
403
+ outputs[index] = { output };
404
+ }
405
+ if (done.length)
406
+ effects.push({ path, original: text, effects: done });
407
+ }
408
+ catch (error) {
409
+ failures.push({ path, error: describeError(error) });
410
+ }
411
+ finally {
412
+ close();
413
+ processed++;
414
+ if (processed === total || processed % every === 0) {
415
+ run.progress(task.position, processed, total);
416
+ }
417
+ }
418
+ };
419
+ let next = 0;
420
+ let sinceTurn = 0;
421
+ const worker = async () => {
422
+ for (;;) {
423
+ const index = next++;
424
+ const file = files[index];
425
+ if (!file)
426
+ return;
427
+ // Callbacks that never wait would keep the event loop to themselves:
428
+ // give it a turn now and then, so a cancel can arrive.
429
+ if (++sinceTurn >= 64) {
430
+ sinceTurn = 0;
431
+ await new Promise((turn) => setImmediate(turn));
432
+ }
433
+ if (run.refusesSteps())
434
+ throw new CanceledError();
435
+ if (limit.expired)
436
+ return;
437
+ await one(index, file);
438
+ }
439
+ };
440
+ await Promise.all(Array.from({ length: Math.min(concurrency, total) }, worker));
441
+ // Out of time: the step has already failed, and nothing it did is written.
442
+ if (limit.expired)
443
+ throw new CanceledError("the step ran out of time");
444
+ effects.sort((a, b) => (a.path < b.path ? -1 : 1));
445
+ const committed = await run.commitFiles(task.position, task.label, JSON.stringify(effects));
446
+ const allFailures = [...failures, ...committed.failures].sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
447
+ // As the CLI does: each file that failed, then its error.
448
+ for (const { path, error } of allFailures) {
449
+ run.log(task.position, path);
450
+ for (const line of error.split("\n"))
451
+ run.log(task.position, ` ${line}`);
332
452
  }
333
- };
334
-
335
- let next = 0;
336
- let sinceTurn = 0;
337
- const worker = async () => {
338
- while (next < total) {
339
- // Callbacks that never wait would keep the event loop to themselves:
340
- // give it a turn now and then, so a cancel can arrive.
341
- if (++sinceTurn >= 64) {
342
- sinceTurn = 0;
343
- await new Promise((turn) => setImmediate(turn));
344
- }
345
- if (run.refusesSteps()) throw new CanceledError();
346
- await one(next++);
453
+ // The step fails when the callback failed on every file, and is partial
454
+ // when it failed on some.
455
+ const [first] = allFailures;
456
+ if (first && allFailures.length === total) {
457
+ throw new Error(`the callback failed on every file; first error: ${first.path}: ${first.error}`);
347
458
  }
348
- };
349
- await Promise.all(Array.from({ length: Math.min(concurrency, total) }, worker));
350
-
351
- // Before the commit: a commit that fails must not lose what was measured.
352
- const metrics = flushMetrics(run, step);
353
- effects.sort((a, b) => (a.path < b.path ? -1 : 1));
354
- const committed = await run.commitFiles(task.position, task.label, JSON.stringify(effects));
355
- const allFailures = [...failures, ...committed.failures].sort((a, b) =>
356
- a.path < b.path ? -1 : a.path > b.path ? 1 : 0,
357
- );
358
- return outcome(
359
- {
360
- outputs: outputs.filter(Boolean).map((entry) => entry.output),
361
- changes: committed.changes,
362
- failures: allFailures,
363
- metrics,
364
- },
365
- step,
366
- );
459
+ const result = {
460
+ result: {
461
+ outputs: outputs.flatMap((entry) => (entry ? [entry.output] : [])),
462
+ changes: committed.changes,
463
+ failures: allFailures,
464
+ },
465
+ };
466
+ if (!allFailures.length)
467
+ return result;
468
+ return {
469
+ ...result,
470
+ partial: `${allFailures.length} of ${total} ${total === 1 ? "file" : "files"} failed`,
471
+ };
367
472
  }
368
-
369
473
  // -------------------------------------------------------------- ending ---
370
-
371
474
  /** How a body that threw ended. */
372
475
  function endingOf(error) {
373
- if (error instanceof CanceledError || error?.name === "CanceledError") {
374
- return { status: "canceled" };
375
- }
376
- if (error instanceof AwaitingApprovalError || error?.name === "AwaitingApprovalError") {
377
- return { status: "awaiting-approval", detail: error.message };
378
- }
379
- const message =
380
- error && typeof error === "object" && "message" in error
381
- ? `${error.name ?? "Error"}: ${error.message}`
382
- : String(error);
383
- return { status: "failed", detail: message };
476
+ const name = isRecord(error) ? error.name : undefined;
477
+ if (error instanceof CanceledError || name === "CanceledError") {
478
+ return { status: "canceled" };
479
+ }
480
+ if (error instanceof AwaitingApprovalError || name === "AwaitingApprovalError") {
481
+ return { status: "awaiting-approval", detail: String(error.message) };
482
+ }
483
+ const message = isRecord(error) && "message" in error
484
+ ? `${String(name ?? "Error")}: ${String(error.message)}`
485
+ : String(error);
486
+ return { status: "failed", detail: message };
384
487
  }
385
-
386
488
  /** A value as JSON keeps it; what JSON has no form for (a function) is null. */
387
489
  function toJson(value) {
388
- const text = JSON.stringify(value);
389
- return text === undefined ? null : JSON.parse(text);
490
+ const text = JSON.stringify(value);
491
+ return text === undefined ? null : JSON.parse(text);
390
492
  }