Skip to content

Commit 160e0ee

Browse files
committed
fix(build): preflight generated output roots
1 parent 0a2acec commit 160e0ee

17 files changed

Lines changed: 259 additions & 20 deletions

‎docs/install/updating.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ for an efficient source-server update: it restores tracked build outputs that
108108
`main` (or rebases a local server branch onto `origin/main`), installs
109109
dependencies, builds clean, and restarts the gateway.
110110

111+
Generated output roots such as `dist`, `dist-runtime`, and package-local
112+
`dist` directories must be real directories. Builds refuse symbolic-link roots
113+
before reading or mutating their contents so cleanup cannot affect the link
114+
target. Replace an output-root symlink with a real directory before updating or
115+
building a source checkout.
116+
111117
```bash
112118
ssh you@server 'cd /path/to/openclaw && scripts/update-gateway.sh'
113119
```

‎scripts/bundled-plugin-assets.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import fs from "node:fs/promises";
66
import path from "node:path";
77
import { fileURLToPath, pathToFileURL } from "node:url";
88
import { runManagedCommand } from "./lib/managed-child-process.mjs";
9+
import { assertRealOutputRoot } from "./lib/output-root-guard.mjs";
910
import { listGeneratedExtensionAssetSources } from "./lib/static-extension-assets.mjs";
1011

1112
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
@@ -128,6 +129,9 @@ export async function runBundledPluginAssetHooks(options = {}) {
128129
console.log(`No bundled plugin asset ${phase} hooks${scope}; skipping.`);
129130
return;
130131
}
132+
if (phase === "copy") {
133+
assertRealOutputRoot(path.join(options.rootDir ?? rootDir, "dist"));
134+
}
131135

132136
for (const hook of hooks) {
133137
console.log(`[${hook.pluginId}] ${phase}: ${hook.command}`);

‎scripts/copy-export-html-templates.ts‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55

66
import fs from "node:fs";
77
import path from "node:path";
8+
import { pathToFileURL } from "node:url";
89
import { ensureDirectory, logVerboseCopy, resolveBuildCopyContext } from "./lib/copy-assets.ts";
10+
import { assertRealOutputRoot } from "./lib/output-root-guard.mjs";
911

1012
const context = resolveBuildCopyContext(import.meta.url);
1113

12-
const exportHtmlSrcDir = path.join(
13-
context.projectRoot,
14-
"src",
15-
"auto-reply",
16-
"reply",
17-
"export-html",
18-
);
19-
const exportHtmlDistDir = path.join(context.projectRoot, "dist", "export-html");
20-
21-
function copyExportHtmlTemplates() {
14+
export function copyExportHtmlTemplates(params: { projectRoot?: string } = {}) {
15+
const projectRoot = params.projectRoot ?? context.projectRoot;
16+
const exportHtmlSrcDir = path.join(projectRoot, "src", "auto-reply", "reply", "export-html");
17+
const exportHtmlDistDir = path.join(projectRoot, "dist", "export-html");
18+
assertRealOutputRoot(path.join(projectRoot, "dist"));
2219
if (!fs.existsSync(exportHtmlSrcDir)) {
2320
console.warn(`${context.prefix} Source directory not found:`, exportHtmlSrcDir);
2421
return;
@@ -52,4 +49,6 @@ function copyExportHtmlTemplates() {
5249
console.log(`${context.prefix} Copied ${copiedCount} export-html assets.`);
5350
}
5451

55-
copyExportHtmlTemplates();
52+
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
53+
copyExportHtmlTemplates();
54+
}

‎scripts/lib/output-root-guard.mjs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ export function assertRealOutputRoot(rootPath, params = {}) {
1111
let stat;
1212
try {
1313
stat = fsImpl.lstatSync(rootPath);
14-
} catch {
14+
} catch (error) {
1515
// Missing roots are normal on a first build; the build recreates them.
16-
return;
16+
if (error?.code === "ENOENT") {
17+
return;
18+
}
19+
throw error;
1720
}
1821
if (!stat.isSymbolicLink()) {
1922
return;

‎scripts/lib/plugin-npm-runtime-build.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
collectPluginSourceEntries,
88
collectTopLevelPublicSurfaceEntries,
99
} from "./bundled-plugin-build-entries.mjs";
10+
import { assertRealOutputRoot } from "./output-root-guard.mjs";
1011
import {
1112
listMissingPackageStaticAssetSources,
1213
runPackageAssetBuild,
@@ -352,6 +353,7 @@ export async function buildPluginNpmRuntime(params) {
352353
return null;
353354
}
354355

356+
assertRealOutputRoot(plan.outDir);
355357
fs.rmSync(plan.outDir, { recursive: true, force: true });
356358
await build({
357359
clean: false,

‎scripts/runtime-postbuild.mjs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,12 @@ function shouldCopyStaticExtensionAssets(params) {
548548
* Runs every runtime postbuild phase after the main dist build.
549549
*/
550550
export function runRuntimePostBuild(params = {}) {
551+
const rootDir = params.rootDir ?? params.cwd ?? params.repoRoot ?? ROOT;
552+
const fsImpl = params.fs ?? fs;
553+
// Postbuild phases share both roots. Validate the whole mutation set before
554+
// any phase runs so a later unsafe root cannot leave earlier output changed.
555+
assertRealOutputRoot(path.join(rootDir, "dist"), { fs: fsImpl });
556+
assertRealOutputRoot(path.join(rootDir, "dist-runtime"), { fs: fsImpl });
551557
const timingsSetting = params.timings ?? process.env.OPENCLAW_RUNTIME_POSTBUILD_TIMINGS;
552558
const timingsEnabled = timingsSetting !== "0" && timingsSetting !== false;
553559
// Per-phase lines are debug detail; default output is one summary line so a

‎scripts/test-projects.test-support.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,8 @@ const TOOLING_SOURCE_TEST_TARGETS = new Map([
14421442
"scripts/lib/plugin-npm-runtime-build.mjs",
14431443
["test/scripts/plugin-npm-runtime-build-args.test.ts", "test/plugin-npm-runtime-build.test.ts"],
14441444
],
1445+
["scripts/lib/output-root-guard.mjs", ["test/scripts/output-root-guard.test.ts"]],
1446+
["scripts/lib/output-root-guard.d.mts", ["test/scripts/output-root-guard.test.ts"]],
14451447
[
14461448
"scripts/lib/npm-publish-plan.mjs",
14471449
[
@@ -2252,6 +2254,8 @@ const TOOLING_SOURCE_TEST_TARGETS = new Map([
22522254
["scripts/write-package-dist-inventory.ts", ["test/scripts/test-install-sh-docker.test.ts"]],
22532255
["scripts/e2e/cron-mcp-cleanup-seed.ts", ["test/scripts/docker-e2e-seeds.test.ts"]],
22542256
["scripts/bundled-plugin-assets.mjs", ["test/scripts/bundled-plugin-assets.test.ts"]],
2257+
["scripts/copy-export-html-templates.ts", ["test/scripts/copy-export-html-templates.test.ts"]],
2258+
["scripts/ui.js", ["test/scripts/ui.test.ts"]],
22552259
["scripts/bundle-a2ui.mjs", ["test/scripts/bundled-plugin-assets.test.ts"]],
22562260
["scripts/build-discord-activity-sdk.mjs", ["test/scripts/bundled-plugin-assets.test.ts"]],
22572261
["scripts/build-diffs-viewer-runtime.mjs", ["test/scripts/build-diffs-viewer-runtime.test.ts"]],

‎scripts/tsdown-build.mjs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export function cleanTsdownOutputRoots(params = {}) {
9898
const fsImpl = params.fs ?? fs;
9999
const env = params.env ?? process.env;
100100
const roots = params.roots ?? listTsdownOutputRoots();
101+
const rootPaths = roots.map((root) => path.join(cwd, root));
102+
// Validate the complete mutation set before traversing protected children or
103+
// cleaning any earlier root; otherwise a later symlink can leave a partial build.
104+
for (const rootPath of rootPaths) {
105+
assertRealOutputRoot(rootPath, { fs: fsImpl });
106+
}
101107
const protectedDeclarationPaths =
102108
env[RUN_NODE_SKIP_DTS_BUILD_ENV] === "1"
103109
? listExistingDeclarationOutputPaths({
@@ -110,11 +116,7 @@ export function cleanTsdownOutputRoots(params = {}) {
110116
...protectedDeclarationPaths,
111117
...listExistingPreservedOutputPaths({ cwd, env, fs: fsImpl }),
112118
]);
113-
for (const root of roots) {
114-
const rootPath = path.join(cwd, root);
115-
// The guard throws outside the best-effort catch below: a symlinked root
116-
// must abort the build, not be swallowed as a skippable cleanup error.
117-
assertRealOutputRoot(rootPath, { fs: fsImpl });
119+
for (const rootPath of rootPaths) {
118120
try {
119121
if (hasProtectedChild({ rootPath, protectedPaths })) {
120122
cleanOutputRootExcept(rootPath, protectedPaths, fsImpl);
@@ -214,6 +216,8 @@ export function pruneStaleRootChunkFiles(params = {}) {
214216
const roots = listTsdownOutputRoots({ cwd, fs: fsImpl }).map((root) => path.join(cwd, root));
215217
for (const root of roots) {
216218
assertRealOutputRoot(root, { fs: fsImpl });
219+
}
220+
for (const root of roots) {
217221
let entries;
218222
try {
219223
entries = fsImpl.readdirSync(root, { withFileTypes: true });

‎scripts/ui.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import fs from "node:fs";
55
import { createRequire } from "node:module";
66
import path from "node:path";
77
import { fileURLToPath } from "node:url";
8+
import { assertRealOutputRoot } from "./lib/output-root-guard.mjs";
89
import { resolvePnpmRunner } from "./pnpm-runner.mjs";
910
import { buildCmdExeCommandLine, resolveWindowsCmdExePath } from "./windows-cmd-helpers.mjs";
1011

@@ -301,6 +302,10 @@ function resolveScriptAction(action) {
301302
return null;
302303
}
303304

305+
export function assertUiBuildOutputRoot(params = {}) {
306+
assertRealOutputRoot(path.join(params.rootDir ?? repoRoot, "dist"), { fs: params.fs ?? fs });
307+
}
308+
304309
export function main(argv = process.argv.slice(2)) {
305310
const [action, ...rest] = argv;
306311
if (!action) {
@@ -313,6 +318,9 @@ export function main(argv = process.argv.slice(2)) {
313318
usage();
314319
process.exit(2);
315320
}
321+
if (action === "build") {
322+
assertUiBuildOutputRoot();
323+
}
316324

317325
if (process.env.OPENCLAW_BUILD_ALL_NO_PNPM === "1" && action === "build") {
318326
run(process.execPath, [path.join(repoRoot, "node_modules/vite/bin/vite.js"), "build", ...rest]);

‎test/external-script-modules.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ declare module "*scripts/ui.js" {
1616
windowsVerbatimArguments?: boolean;
1717
};
1818
};
19+
export function assertUiBuildOutputRoot(params?: {
20+
rootDir?: string;
21+
fs?: {
22+
lstatSync(path: string): { isSymbolicLink(): boolean };
23+
};
24+
}): void;
1925
export function shouldUseCmdExeForCommand(cmd: string, platform?: NodeJS.Platform): boolean;
2026
export function resolveSpawnCall(
2127
cmd: string,

0 commit comments

Comments
 (0)