import * as fs from 'node:fs/promises'; import * as pathlib from 'node:path'; import * as artifact from './artifact.ts'; import * as cas from './cas.ts'; import {BuildError} from './errors.ts'; import {runShell} from './exec.ts'; import {type Store} from './store.ts'; import {substitute} from './subst.ts'; export type DirtyReason = 'new' | 'cas-missing'; export type RuleOutcome = | {status: 'clean'} // key hit, CAS objects present | {status: 'ran', reason: DirtyReason} | {status: 'would-run', reason: DirtyReason} // dry run | {status: 'failed', message: string}; export type BuildResult = { outcomes: Map; // no entry = not attempted (aborted) keys: Map; // only decls whose key resolved ok: boolean; // every decl clean or ran }; export type ExecutorOpts = { root: string; // cwd for commands store: Store, casDir: string; // relative to root (outputs land here) tmpDir: string, jobs: number, dryRun: boolean, failFast: boolean, verbose?: boolean; // annotate run lines with the dirty reason sourceHashes: Map; // every source input/dep, pre-reconciled signal: AbortSignal, log?: (line: string) => void, logError?: (line: string) => void, write?: (text: string) => void, // raw stdout, for lines left open }; export function label(decl: artifact.RuleDecl): string { return decl.display ?? decl.cmds[0] ?? '(no commands)'; } // Parallel-array access: the arrays are constructed the same length, so a // miss is a bug worth crashing on. function paired(xs: readonly T[], n: number): T { let x = xs[n]; if (x === undefined) { throw new Error('parallel array length mismatch'); } return x; } function indent(text: string): string { return text.replace(/\n$/, '').split('\n').map(l => ' ' + l).join('\n'); } // Rule progress. A rule announces itself as it starts, `[n] label ...`, and // leaves the line open so its result lands on it: ` done`. With parallel jobs // something else can print first; the open line is then ended as it is and // the result gets a line of its own, `[n] label done`. Every other line goes // through `line`, which ends the open one first. export class JobLog { #write: (text: string) => void; #open: string | null = null; constructor(write: (text: string) => void) { this.#write = write; } start(text: string): void { this.break(); this.#write(`${text} ...`); this.#open = text; } finish(text: string): void { if (this.#open === text) { this.#write(' done\n'); this.#open = null; } else { this.line(`${text} done`, s => this.#write(s + '\n')); } } line(text: string, log: (line: string) => void): void { this.break(); log(text); } break(): void { if (this.#open !== null) { this.#write('\n'); this.#open = null; } } } // Sentinels. They carry no message; the outcome map is the report. class RuleFailed extends Error {} class Aborted extends Error {} class Semaphore { #available: number; #waiters: (() => void)[] = []; constructor(n: number) { this.#available = n; } async acquire(): Promise { if (this.#available > 0) { this.#available--; return; } await new Promise(resolve => this.#waiters.push(resolve)); } release(): void { let waiter = this.#waiters.shift(); if (waiter !== undefined) { waiter(); } else { this.#available++; } } } // Rules read source files only, so every identity key is computable upfront // and the rules are independent: this is a flat parallel sweep, no plan and // no dependency graph. The only coupling is that byte-identical declarations // share one execution. export class Executor { #opts: ExecutorOpts; #inflightByKey = new Map>(); #outcomes = new Map(); #keys = new Map(); #semaphore: Semaphore; #failAc = new AbortController(); #runSignal: AbortSignal; #counter = 0; #tmpSeq = 0; #jobLog: JobLog; #log: (line: string) => void; #logError: (line: string) => void; constructor(opts: ExecutorOpts) { this.#opts = opts; this.#semaphore = new Semaphore(opts.jobs); this.#runSignal = AbortSignal.any([opts.signal, this.#failAc.signal]); this.#jobLog = new JobLog(opts.write ?? (s => process.stdout.write(s))); let log = opts.log ?? console.log; let logError = opts.logError ?? console.error; this.#log = line => this.#jobLog.line(line, log); this.#logError = line => this.#jobLog.line(line, logError); } async build(decls: readonly artifact.RuleDecl[]): Promise { await Promise.all(decls.map(d => this.#guard(d))); let ok = decls.every(d => { let status = this.#outcomes.get(d)?.status; return status === 'clean' || status === 'ran'; }); return {outcomes: this.#outcomes, keys: this.#keys, ok}; } async #guard(decl: artifact.RuleDecl): Promise { try { await this.#run(decl); } catch (err) { if (err instanceof RuleFailed || err instanceof Aborted) { return; } // Any other escape (a store error, a resolve conflict, a bug) // must surface as a reported failure, not vanish here. this.#outcomes.set(decl, {status: 'failed', message: 'internal error'}); this.#logError(`FAILED (internal error): ${label(decl)}`); this.#logError(indent(err instanceof Error ? err.stack ?? err.message : String(err))); this.#failAc.abort(); } } #digestOf(path: string): string { let hash = this.#opts.sourceHashes.get(path); if (hash === undefined) { throw new BuildError(`No hash for source ${path}`); } return hash.toString('hex'); } async #run(decl: artifact.RuleDecl): Promise { let {store, casDir} = this.#opts; let key = artifact.computeKey(decl, p => this.#digestOf(p)); this.#keys.set(decl, key); let stored = store.lookupRule(key); if (stored !== null && stored.length === decl.outputs.length && stored.every((o, n) => o.ext === paired(decl.outputs, n).ext)) { let sizes = await Promise.all(stored.map(o => cas.casStat(casDir, o.digest, o.ext))); if (stored.every((o, n) => sizes[n] === o.size)) { this.#resolve(decl, stored.map(o => o.digest)); this.#outcomes.set(decl, {status: 'clean'}); return; } } let reason: DirtyReason = stored === null ? 'new' : 'cas-missing'; if (this.#opts.dryRun) { this.#outcomes.set(decl, {status: 'would-run', reason}); this.#log(`would run (${reason}): ${label(decl)}`); return; } // Byte-identical duplicate declarations share one execution; the // loser adopts the winner's digests and reads as clean. let existing = this.#inflightByKey.get(key); if (existing !== undefined) { try { this.#resolve(decl, await existing); } catch (err) { if (err instanceof RuleFailed) { this.#outcomes.set(decl, {status: 'failed', message: 'byte-identical rule failed'}); } throw err; } this.#outcomes.set(decl, {status: 'clean'}); return; } let work = this.#gated(decl, key, reason); this.#inflightByKey.set(key, work); this.#resolve(decl, await work); // #execute already recorded the outcome } #resolve(decl: artifact.RuleDecl, digests: readonly string[]): void { decl.outputs.forEach((o, n) => o.resolve(paired(digests, n))); } async #gated(decl: artifact.RuleDecl, key: string, reason: DirtyReason): Promise { await this.#semaphore.acquire(); try { if (this.#runSignal.aborted) { throw new Aborted(); } return await this.#execute(decl, key, reason); } finally { this.#semaphore.release(); } } async #execute(decl: artifact.RuleDecl, key: string, reason: DirtyReason): Promise { let {store, casDir, tmpDir, root} = this.#opts; let ruleTmp = pathlib.join(tmpDir, String(this.#tmpSeq++)); await fs.mkdir(ruleTmp, {recursive: true}); let tempOutputs = decl.outputs.map(o => pathlib.join(ruleTmp, o.filename)); let command = decl.cmds.map(c => substitute(c, decl.inputs, tempOutputs)).join(' && '); let announce = `[${++this.#counter}] ${label(decl)}` + (this.#opts.verbose ? ` (${reason})` : ''); this.#jobLog.start(announce); try { let result = await runShell(command, {cwd: root, signal: this.#runSignal}); if (this.#runSignal.aborted && result.code !== 0) { throw new Aborted(); // killed by the abort, not a real failure; stays dirty } if (result.code === 0) { let present = await Promise.all(tempOutputs.map( p => fs.access(p).then(() => true, () => false))); let missing = tempOutputs.filter((_, n) => !present[n]); if (missing.length === 0) { let outputs = []; for (let [n, o] of decl.outputs.entries()) { let object = await cas.casInsert(casDir, paired(tempOutputs, n), o.ext); outputs.push({digest: object.digest, ext: o.ext, size: object.size}); } store.recordRule(key, decl.cmds, outputs); this.#outcomes.set(decl, {status: 'ran', reason}); this.#jobLog.finish(announce); if (result.output !== '') { this.#log(indent(result.output)); } return outputs.map(o => o.digest); } return this.#fail(decl, command, result.output, `command succeeded but did not produce: ${missing.map(p => pathlib.basename(p)).join(' ')}`); } return this.#fail(decl, command, result.output, `exit status ${result.code ?? result.signal}`); } finally { await fs.rm(ruleTmp, {recursive: true, force: true}); } } // Nothing is recorded for a failure: failed and never-ran are the same // state, so the rule stays dirty. #fail(decl: artifact.RuleDecl, command: string, output: string, message: string): never { this.#outcomes.set(decl, {status: 'failed', message}); this.#logError(`FAILED: ${label(decl)} (${message})`); this.#logError(` command: ${command}`); if (output !== '') { this.#logError(indent(output)); } if (this.#opts.failFast) { this.#failAc.abort(); } throw new RuleFailed(); } }