Skip to content

Commit 099fc6b

Browse files
authored
fix: preserve Control UI assets during source rebuilds (#131477)
Keep Vite-owned dist/control-ui output through TypeScript wrapper cleanup while still removing stale runtime chunks and plugin staging. Source launcher and gateway-watch rebuilds retain the existing UI; UI edits still need ui:build or ui:dev. Replace the mock-only launcher assertion with real preparation-boundary coverage, consolidating declaration and startup-metadata cleanup cases. Historical context: Control UI preservation repair #10146.
1 parent 3a84b9f commit 099fc6b

4 files changed

Lines changed: 123 additions & 184 deletions

File tree

‎docs/start/setup.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ reloads on relevant source, config, and bundled-plugin metadata changes. If the
113113
watched Gateway exits during startup, `gateway:watch` runs
114114
`openclaw doctor --fix --non-interactive` once and retries; set
115115
`OPENCLAW_GATEWAY_WATCH_AUTO_DOCTOR=0` to disable that dev-only repair pass.
116-
`pnpm gateway:watch` does not rebuild `dist/control-ui`, so rerun `pnpm ui:build` after `ui/` changes or use `pnpm ui:dev` while developing the Control UI.
116+
TypeScript rebuilds triggered by `pnpm openclaw ...` or `pnpm gateway:watch` preserve existing `dist/control-ui` assets but do not rebuild them. Run `pnpm ui:build` once and again after `ui/` changes, or use `pnpm ui:dev` while developing the Control UI.
117117

118118
### 2) Point the macOS app at your running Gateway
119119

‎scripts/tsdown-build.mts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ export function cleanTsdownOutputRoots(params: OutputRootParams = {}) {
213213
? listExistingDeclarationOutputPaths(cwd, fsImpl, roots)
214214
: new Set<string>();
215215
const protectedPaths = new Set([
216+
// Vite owns and cleans this subtree; runtime-only builds cannot recreate it.
217+
path.resolve(cwd, "dist/control-ui"),
216218
...protectedDeclarationPaths,
217219
...listExistingPreservedOutputPaths(cwd, env, fsImpl),
218220
]);
@@ -261,7 +263,7 @@ function cleanOutputRootExcept(rootPath: string, protectedPaths: Set<string>, fs
261263
fsImpl.rmSync(entryPath, { force: true });
262264
}
263265
} catch {
264-
// Keep best-effort semantics; protected declaration children can keep a directory non-empty.
266+
// Keep best-effort semantics; protected children can keep a directory non-empty.
265267
}
266268
}
267269
}

‎src/infra/run-node.test.ts‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -412,57 +412,6 @@ async function expectManifestId(tmp: string, relativePath: string, id: string) {
412412
}
413413

414414
describe("run-node script", () => {
415-
it.runIf(process.platform !== "win32")(
416-
"preserves control-ui assets by building with tsdown --no-clean",
417-
async ({ tmp }) => {
418-
const argsPath = resolvePath(tmp, ".build-args.txt");
419-
const indexPath = resolvePath(tmp, "dist/control-ui/index.html");
420-
421-
await writeRuntimePostBuildScaffold(tmp);
422-
await fs.mkdir(path.dirname(indexPath), { recursive: true });
423-
await fs.writeFile(indexPath, "<html>sentinel</html>\n", "utf-8");
424-
425-
const nodeCalls: string[][] = [];
426-
const spawn = (cmd: string, args: string[]) => {
427-
if (cmd === process.execPath && isTsxScriptArgs(args, "scripts/tsdown-build.mts")) {
428-
fsSync.writeFileSync(argsPath, args.join(" "), "utf-8");
429-
if (!args.includes("--no-clean")) {
430-
fsSync.rmSync(resolvePath(tmp, "dist/control-ui"), { recursive: true, force: true });
431-
}
432-
}
433-
if (cmd === process.execPath) {
434-
nodeCalls.push([cmd, ...args]);
435-
}
436-
return createExitedProcess(0);
437-
};
438-
439-
const exitCode = await runNodeCommand(tmp, {
440-
args: ["--version"],
441-
env: { OPENCLAW_FORCE_BUILD: "1" },
442-
spawn,
443-
runRuntimePostBuild: skipRuntimePostBuild,
444-
});
445-
446-
expect(exitCode).toBe(0);
447-
await expect(fs.readFile(argsPath, "utf-8")).resolves.toContain(
448-
"scripts/tsdown-build.mts --no-clean",
449-
);
450-
await expect(fs.readFile(indexPath, "utf-8")).resolves.toContain("sentinel");
451-
expect(nodeCalls).toEqual([
452-
[
453-
process.execPath,
454-
"--import",
455-
"tsx",
456-
"scripts/bundled-plugin-assets.mts",
457-
"--phase",
458-
"build",
459-
],
460-
[process.execPath, "--import", "tsx", "scripts/tsdown-build.mts", "--no-clean"],
461-
[process.execPath, "openclaw.mjs", "--version"],
462-
]);
463-
},
464-
);
465-
466415
it("copies bundled plugin metadata after rebuilding from a clean dist", async ({ tmp }) => {
467416
await writeRuntimePostBuildScaffold(tmp);
468417
await writeProjectFiles(tmp, {

‎test/scripts/tsdown-build.test.ts‎

Lines changed: 119 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,50 +1854,126 @@ describe("resolveTsdownBuildInvocation", () => {
18541854
await expectPathMissing(path.join(distRuntimeDir, "heartbeat-runner.runtime-fspOEj_1.js"));
18551855
});
18561856

1857-
it("cleans tsdown output roots before using tsdown --no-clean", async () => {
1858-
const rootDir = createTempDir("openclaw-tsdown-clean-");
1859-
const distFile = path.join(rootDir, "dist", "stale.js");
1860-
const pluginGeneratedFile = path.join(rootDir, "dist", "extensions", "telegram", "index.js");
1861-
const distRuntimeFile = path.join(rootDir, "dist-runtime", "stale.js");
1862-
const agentCorePackageFile = path.join(rootDir, "packages", "agent-core", "dist", "stale.js");
1863-
const netPolicyPackageFile = path.join(rootDir, "packages", "net-policy", "dist", "stale.js");
1864-
const pluginSdkPackageFile = path.join(rootDir, "packages", "plugin-sdk", "dist", "keep.js");
1865-
const packageSourceFile = path.join(rootDir, "packages", "agent-core", "src", "keep.ts");
1866-
const unrelatedFile = path.join(rootDir, "tmp", "keep.js");
1867-
await fsPromises.mkdir(path.dirname(distFile), { recursive: true });
1868-
await fsPromises.mkdir(path.dirname(pluginGeneratedFile), { recursive: true });
1869-
await fsPromises.mkdir(path.dirname(distRuntimeFile), { recursive: true });
1870-
await fsPromises.mkdir(path.dirname(agentCorePackageFile), { recursive: true });
1871-
await fsPromises.mkdir(path.dirname(netPolicyPackageFile), { recursive: true });
1872-
await fsPromises.mkdir(path.dirname(pluginSdkPackageFile), { recursive: true });
1873-
await fsPromises.mkdir(path.dirname(packageSourceFile), { recursive: true });
1874-
await fsPromises.mkdir(path.dirname(unrelatedFile), { recursive: true });
1875-
await fsPromises.writeFile(distFile, "stale\n");
1876-
await fsPromises.writeFile(pluginGeneratedFile, "generated\n");
1877-
await fsPromises.writeFile(distRuntimeFile, "stale\n");
1878-
await fsPromises.writeFile(agentCorePackageFile, "stale\n");
1879-
await fsPromises.writeFile(netPolicyPackageFile, "stale\n");
1880-
await fsPromises.writeFile(pluginSdkPackageFile, "keep\n");
1881-
await fsPromises.writeFile(packageSourceFile, "keep\n");
1882-
await fsPromises.writeFile(unrelatedFile, "keep\n");
1883-
1884-
const outputRoots = listTsdownOutputRoots();
1885-
expect(outputRoots).toEqual(
1886-
expect.arrayContaining(["packages/agent-core/dist", "packages/net-policy/dist"]),
1887-
);
1888-
expect(outputRoots).not.toContain(path.join("packages", "plugin-sdk", "dist"));
1889-
1890-
cleanTsdownOutputRoots({ cwd: rootDir });
1857+
it.each([
1858+
{ label: "default build", args: [], skipDts: "0", preserveMetadata: "0" },
1859+
{
1860+
label: "source launcher --no-clean",
1861+
args: ["--no-clean"],
1862+
skipDts: "1",
1863+
preserveMetadata: "0",
1864+
},
1865+
{ label: "build-all startup metadata", args: [], skipDts: "0", preserveMetadata: "1" },
1866+
{ label: "cached build-all", args: [], skipDts: "1", preserveMetadata: "1" },
1867+
])(
1868+
"preserves separately owned outputs during $label cleanup",
1869+
async ({ args, skipDts, preserveMetadata }) => {
1870+
const rootDir = createTempDir("openclaw-tsdown-clean-");
1871+
const retainedFiles = [
1872+
"dist/control-ui/index.html",
1873+
"dist/control-ui/sw.js",
1874+
"dist/control-ui/asset-manifest.json",
1875+
"dist/control-ui/assets/index-AbCd1234.js",
1876+
"dist/control-ui/assets/index-AbCd1234.js.map",
1877+
"dist/control-ui/assets/index-AbCd1234.js.br",
1878+
"dist/control-ui/assets/nested/styles-AbCd1234.css",
1879+
"packages/plugin-sdk/dist/keep.js",
1880+
"packages/agent-core/src/keep.ts",
1881+
"tmp/keep.js",
1882+
];
1883+
const declarationFiles = [
1884+
"dist/plugin-sdk/core.d.ts",
1885+
"dist/plugin-sdk/nested/types.d.cts",
1886+
"dist-runtime/extensions/demo/index.d.ts",
1887+
"packages/media-understanding-common/dist/index.d.mts",
1888+
"packages/media-understanding-common/dist/nested/types.d.ts",
1889+
];
1890+
const metadataFile = "dist/cli-startup-metadata.json";
1891+
const staleFiles = [
1892+
"dist/entry.js",
1893+
"dist/stale-AbCd1234.js",
1894+
"dist/stale-AbCd1234.js.map",
1895+
"dist/plugin-sdk/core.js",
1896+
"dist/nested/stale.js",
1897+
"dist/nested/stale.js.map",
1898+
"dist/control-ui-old/index.html",
1899+
"dist/extensions/demo/src/index.js",
1900+
"dist/extensions/demo/node_modules/staged/index.js",
1901+
"dist/extensions/node_modules/openclaw/plugin-sdk/core.js",
1902+
"dist-runtime/stale.js",
1903+
"dist-runtime/stale.js.map",
1904+
"dist-runtime/control-ui/index.html",
1905+
"dist-runtime/extensions/demo/index.js",
1906+
"dist-runtime/extensions/demo/node_modules/staged/index.js",
1907+
"packages/agent-core/dist/stale.js",
1908+
"packages/net-policy/dist/stale.js",
1909+
"packages/media-understanding-common/dist/index.mjs",
1910+
"packages/media-understanding-common/dist/chunks/old.js",
1911+
];
1912+
for (const relativePath of [
1913+
...retainedFiles,
1914+
...declarationFiles,
1915+
metadataFile,
1916+
...staleFiles,
1917+
]) {
1918+
const filePath = path.join(rootDir, relativePath);
1919+
await fsPromises.mkdir(path.dirname(filePath), { recursive: true });
1920+
await fsPromises.writeFile(filePath, `sentinel:${relativePath}\n`);
1921+
}
18911922

1892-
await expectPathMissing(distFile);
1893-
await expectPathMissing(pluginGeneratedFile);
1894-
await expectPathMissing(path.join(rootDir, "dist-runtime"));
1895-
await expectPathMissing(path.join(rootDir, "packages", "agent-core", "dist"));
1896-
await expectPathMissing(path.join(rootDir, "packages", "net-policy", "dist"));
1897-
await expect(fsPromises.readFile(pluginSdkPackageFile, "utf8")).resolves.toBe("keep\n");
1898-
await expect(fsPromises.readFile(packageSourceFile, "utf8")).resolves.toBe("keep\n");
1899-
await expect(fsPromises.readFile(unrelatedFile, "utf8")).resolves.toBe("keep\n");
1900-
});
1923+
const scriptUrl = pathToFileURL(path.resolve("scripts/tsdown-build.mts")).href;
1924+
const result = spawnSync(
1925+
process.execPath,
1926+
[
1927+
"--import",
1928+
import.meta.resolve("tsx"),
1929+
"--input-type=module",
1930+
"-e",
1931+
`import { prepareTsdownBuildExecution } from ${JSON.stringify(scriptUrl)};
1932+
const plan = prepareTsdownBuildExecution(${JSON.stringify({ args, ...NO_MEMORY_LIMIT })});
1933+
if (!plan) throw new Error("fixture build was not admitted");`,
1934+
],
1935+
{
1936+
cwd: rootDir,
1937+
encoding: "utf8",
1938+
env: {
1939+
...process.env,
1940+
OPENCLAW_RUN_NODE_SKIP_DTS_BUILD: skipDts,
1941+
OPENCLAW_PRESERVE_CLI_STARTUP_METADATA: preserveMetadata,
1942+
},
1943+
},
1944+
);
1945+
expect(result.status, result.stderr).toBe(0);
1946+
for (const relativePath of staleFiles) {
1947+
await expectPathMissing(path.join(rootDir, relativePath));
1948+
}
1949+
for (const relativePath of [
1950+
"dist/extensions/demo/node_modules",
1951+
"dist/extensions/node_modules",
1952+
"dist-runtime/extensions/demo/node_modules",
1953+
"packages/agent-core/dist",
1954+
"packages/net-policy/dist",
1955+
]) {
1956+
await expectPathMissing(path.join(rootDir, relativePath));
1957+
}
1958+
for (const [files, preserve] of [
1959+
[declarationFiles, skipDts === "1"],
1960+
[[metadataFile], preserveMetadata === "1"],
1961+
] as const) {
1962+
for (const relativePath of files) {
1963+
if (preserve) {
1964+
retainedFiles.push(relativePath);
1965+
} else {
1966+
await expectPathMissing(path.join(rootDir, relativePath));
1967+
}
1968+
}
1969+
}
1970+
for (const relativePath of retainedFiles) {
1971+
await expect(fsPromises.readFile(path.join(rootDir, relativePath), "utf8")).resolves.toBe(
1972+
`sentinel:${relativePath}\n`,
1973+
);
1974+
}
1975+
},
1976+
);
19011977

19021978
it("cleans only selected tsdown output roots", async () => {
19031979
const rootDir = createTempDir("openclaw-tsdown-selected-clean-");
@@ -1971,39 +2047,6 @@ describe("resolveTsdownBuildInvocation", () => {
19712047
}
19722048
});
19732049

1974-
it("removes CLI startup metadata during default tsdown clean", async () => {
1975-
const rootDir = createTempDir("openclaw-tsdown-clean-metadata-default-");
1976-
const metadataFile = path.join(rootDir, "dist", "cli-startup-metadata.json");
1977-
await fsPromises.mkdir(path.dirname(metadataFile), { recursive: true });
1978-
await fsPromises.writeFile(metadataFile, '{"generatedBy":"test"}\n');
1979-
1980-
cleanTsdownOutputRoots({ cwd: rootDir });
1981-
1982-
await expectPathMissing(metadataFile);
1983-
});
1984-
1985-
it("preserves CLI startup metadata across opted-in build-all tsdown clean", async () => {
1986-
const rootDir = createTempDir("openclaw-tsdown-clean-metadata-");
1987-
const metadataFile = path.join(rootDir, "dist", "cli-startup-metadata.json");
1988-
const staleFile = path.join(rootDir, "dist", "stale.js");
1989-
const nestedStaleFile = path.join(rootDir, "dist", "nested", "stale.js");
1990-
await fsPromises.mkdir(path.dirname(nestedStaleFile), { recursive: true });
1991-
await fsPromises.writeFile(metadataFile, '{"generatedBy":"test"}\n');
1992-
await fsPromises.writeFile(staleFile, "stale\n");
1993-
await fsPromises.writeFile(nestedStaleFile, "stale\n");
1994-
1995-
cleanTsdownOutputRoots({
1996-
cwd: rootDir,
1997-
env: { OPENCLAW_PRESERVE_CLI_STARTUP_METADATA: "1" },
1998-
});
1999-
2000-
await expect(fsPromises.readFile(metadataFile, "utf8")).resolves.toBe(
2001-
'{"generatedBy":"test"}\n',
2002-
);
2003-
await expectPathMissing(staleFile);
2004-
await expectPathMissing(nestedStaleFile);
2005-
});
2006-
20072050
it("refuses a symlinked output root with preserved children and leaves the target unchanged", async () => {
20082051
const rootDir = createTempDir("openclaw-tsdown-clean-symlink-");
20092052
const targetDir = path.join(rootDir, "gateway-dist");
@@ -2147,61 +2190,6 @@ describe("resolveTsdownBuildInvocation", () => {
21472190
await expect(fsPromises.readFile(markerFile, "utf8")).resolves.toBe("keep\n");
21482191
});
21492192

2150-
it("preserves existing package declarations when tsdown DTS output is skipped", async () => {
2151-
const rootDir = createTempDir("openclaw-tsdown-clean-skip-dts-");
2152-
const declarationFile = path.join(
2153-
rootDir,
2154-
"packages",
2155-
"media-understanding-common",
2156-
"dist",
2157-
"index.d.mts",
2158-
);
2159-
const nestedDeclarationFile = path.join(
2160-
rootDir,
2161-
"packages",
2162-
"media-understanding-common",
2163-
"dist",
2164-
"nested",
2165-
"types.d.ts",
2166-
);
2167-
const staleJsFile = path.join(
2168-
rootDir,
2169-
"packages",
2170-
"media-understanding-common",
2171-
"dist",
2172-
"index.mjs",
2173-
);
2174-
const nestedStaleFile = path.join(
2175-
rootDir,
2176-
"packages",
2177-
"media-understanding-common",
2178-
"dist",
2179-
"chunks",
2180-
"old.js",
2181-
);
2182-
const agentCorePackageFile = path.join(rootDir, "packages", "agent-core", "dist", "stale.js");
2183-
await fsPromises.mkdir(path.dirname(declarationFile), { recursive: true });
2184-
await fsPromises.mkdir(path.dirname(nestedDeclarationFile), { recursive: true });
2185-
await fsPromises.mkdir(path.dirname(nestedStaleFile), { recursive: true });
2186-
await fsPromises.mkdir(path.dirname(agentCorePackageFile), { recursive: true });
2187-
await fsPromises.writeFile(declarationFile, "export {};\n");
2188-
await fsPromises.writeFile(nestedDeclarationFile, "export {};\n");
2189-
await fsPromises.writeFile(staleJsFile, "stale\n");
2190-
await fsPromises.writeFile(nestedStaleFile, "old\n");
2191-
await fsPromises.writeFile(agentCorePackageFile, "stale\n");
2192-
2193-
cleanTsdownOutputRoots({
2194-
cwd: rootDir,
2195-
env: { OPENCLAW_RUN_NODE_SKIP_DTS_BUILD: "1" },
2196-
});
2197-
2198-
await expect(fsPromises.readFile(declarationFile, "utf8")).resolves.toBe("export {};\n");
2199-
await expect(fsPromises.readFile(nestedDeclarationFile, "utf8")).resolves.toBe("export {};\n");
2200-
await expectPathMissing(staleJsFile);
2201-
await expectPathMissing(nestedStaleFile);
2202-
await expectPathMissing(path.join(rootDir, "packages", "agent-core", "dist"));
2203-
});
2204-
22052193
it("prunes untracked generated declaration files that shadow source entries", async () => {
22062194
const rootDir = createTempDir("openclaw-tsdown-source-dts-");
22072195
const signalDir = path.join(rootDir, "extensions", "signal");

0 commit comments

Comments
 (0)