mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-02 22:01:36 +00:00
* fix(scripts): keep format-docs output tails UTF-8 safe When formatter (oxfmt) error output exceeds the 16 KiB tail cap and the retained byte window starts inside a multibyte UTF-8 character, the diagnostic tail would begin with a replacement character (�). Add a decodeUtf8Tail helper that skips leading UTF-8 continuation bytes before decoding the byte suffix, matching the existing pattern in scripts/run-additional-boundary-checks.mjs (#109167). Keep the existing 16 KiB cap and ASCII behavior unchanged. * refactor(scripts): share UTF-8-safe output tails Co-authored-by: 陈志强0668000989 <chen.zhiqiang1@xydigit.com> * test(scripts): prove real formatter UTF-8 failures --------- Co-authored-by: Peter Steinberger <steipete@gmail.com>
72 lines
2.7 KiB
JavaScript
72 lines
2.7 KiB
JavaScript
// Formats generated TypeScript/JavaScript modules through the repo formatter.
|
|
import { spawnSync } from "node:child_process";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { outputTail } from "./output-tail.mjs";
|
|
|
|
export const GENERATED_MODULE_FORMAT_TIMEOUT_MS = 30_000;
|
|
export const GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES = 1024 * 1024;
|
|
const FORMATTER_OUTPUT_TAIL_BYTES = 16 * 1024;
|
|
|
|
function formatterFailureDetails(formatter) {
|
|
const details = [];
|
|
const errorCode = formatter.error?.code;
|
|
if (errorCode === "ETIMEDOUT") {
|
|
details.push(`formatter timed out after ${GENERATED_MODULE_FORMAT_TIMEOUT_MS}ms`);
|
|
} else if (errorCode === "ENOBUFS") {
|
|
details.push(`formatter output exceeded ${GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES} bytes`);
|
|
} else if (formatter.error?.message) {
|
|
details.push(formatter.error.message);
|
|
}
|
|
if (formatter.status !== null && formatter.status !== undefined && formatter.status !== 0) {
|
|
details.push(`formatter exited with status ${formatter.status}`);
|
|
}
|
|
if (formatter.signal) {
|
|
details.push(`formatter exited with signal ${formatter.signal}`);
|
|
}
|
|
const stderrTail = outputTail(formatter.stderr, FORMATTER_OUTPUT_TAIL_BYTES);
|
|
if (stderrTail) {
|
|
details.push(`stderr tail:\n${stderrTail}`);
|
|
}
|
|
const stdoutTail = outputTail(formatter.stdout, FORMATTER_OUTPUT_TAIL_BYTES);
|
|
if (stdoutTail) {
|
|
details.push(`stdout tail:\n${stdoutTail}`);
|
|
}
|
|
return details.join("\n") || "unknown formatter failure";
|
|
}
|
|
|
|
/** Format generated source in a temporary file and return the formatter output. */
|
|
export function formatGeneratedModule(source, { repoRoot, outputPath, errorLabel }, deps = {}) {
|
|
const spawnSyncImpl = deps.spawnSync ?? spawnSync;
|
|
const resolvedRepoRoot = path.resolve(repoRoot);
|
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-generated-format-"));
|
|
const tempOutputPath = path.join(tempDir, path.basename(outputPath));
|
|
|
|
try {
|
|
fs.writeFileSync(tempOutputPath, source, "utf8");
|
|
const formatter = spawnSyncImpl(
|
|
process.execPath,
|
|
[
|
|
path.join(resolvedRepoRoot, "node_modules", "oxfmt", "bin", "oxfmt"),
|
|
"--write",
|
|
tempOutputPath,
|
|
],
|
|
{
|
|
cwd: resolvedRepoRoot,
|
|
encoding: "utf8",
|
|
maxBuffer: GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES,
|
|
shell: false,
|
|
timeout: GENERATED_MODULE_FORMAT_TIMEOUT_MS,
|
|
},
|
|
);
|
|
if (formatter.error || formatter.status !== 0) {
|
|
const details = formatterFailureDetails(formatter);
|
|
throw new Error(`failed to format generated ${errorLabel}: ${details}`);
|
|
}
|
|
return fs.readFileSync(tempOutputPath, "utf8");
|
|
} finally {
|
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
}
|
|
}
|