fix: resolve symlinked argv1 for Control UI asset detection (#14919)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 07b85041dc
Co-authored-by: gumadeiras <116837+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
Gustavo Madeira Santana
2026-02-12 14:45:31 -05:00
committed by GitHub
parent bdd0c12329
commit 8d5094e1f4
3 changed files with 126 additions and 3 deletions

View File

@@ -2,6 +2,24 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
/** Try to create a symlink; returns false if the OS denies it (Windows CI without Developer Mode). */
async function trySymlink(target: string, linkPath: string): Promise<boolean> {
try {
await fs.symlink(target, linkPath);
return true;
} catch {
return false;
}
}
async function canonicalPath(p: string): Promise<string> {
try {
return await fs.realpath(p);
} catch {
return path.resolve(p);
}
}
import {
resolveControlUiDistIndexHealth,
resolveControlUiDistIndexPath,
@@ -10,6 +28,7 @@ import {
resolveControlUiRootOverrideSync,
resolveControlUiRootSync,
} from "./control-ui-assets.js";
import { resolveOpenClawPackageRoot } from "./openclaw-root.js";
describe("control UI assets helpers", () => {
it("resolves repo root from src argv1", async () => {
@@ -221,4 +240,89 @@ describe("control UI assets helpers", () => {
await fs.rm(tmp, { recursive: true, force: true });
}
});
it("resolves control-ui root when argv1 is a symlink (nvm scenario)", async () => {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
try {
const realPkg = path.join(tmp, "real-pkg");
const bin = path.join(tmp, "bin");
await fs.mkdir(realPkg, { recursive: true });
await fs.mkdir(bin, { recursive: true });
await fs.writeFile(path.join(realPkg, "package.json"), JSON.stringify({ name: "openclaw" }));
await fs.writeFile(path.join(realPkg, "openclaw.mjs"), "export {};\n");
await fs.mkdir(path.join(realPkg, "dist", "control-ui"), { recursive: true });
await fs.writeFile(path.join(realPkg, "dist", "control-ui", "index.html"), "<html></html>\n");
const ok = await trySymlink(
path.join("..", "real-pkg", "openclaw.mjs"),
path.join(bin, "openclaw"),
);
if (!ok) {
return; // symlinks not supported (Windows CI)
}
const resolvedRoot = resolveControlUiRootSync({ argv1: path.join(bin, "openclaw") });
expect(resolvedRoot).not.toBeNull();
expect(await canonicalPath(resolvedRoot ?? "")).toBe(
await canonicalPath(path.join(realPkg, "dist", "control-ui")),
);
} finally {
await fs.rm(tmp, { recursive: true, force: true });
}
});
it("resolves package root via symlinked argv1", async () => {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
try {
const realPkg = path.join(tmp, "real-pkg");
const bin = path.join(tmp, "bin");
await fs.mkdir(realPkg, { recursive: true });
await fs.mkdir(bin, { recursive: true });
await fs.writeFile(path.join(realPkg, "package.json"), JSON.stringify({ name: "openclaw" }));
await fs.writeFile(path.join(realPkg, "openclaw.mjs"), "export {};\n");
await fs.mkdir(path.join(realPkg, "dist", "control-ui"), { recursive: true });
await fs.writeFile(path.join(realPkg, "dist", "control-ui", "index.html"), "<html></html>\n");
const ok = await trySymlink(
path.join("..", "real-pkg", "openclaw.mjs"),
path.join(bin, "openclaw"),
);
if (!ok) {
return; // symlinks not supported (Windows CI)
}
const packageRoot = await resolveOpenClawPackageRoot({ argv1: path.join(bin, "openclaw") });
expect(packageRoot).not.toBeNull();
expect(await canonicalPath(packageRoot ?? "")).toBe(await canonicalPath(realPkg));
} finally {
await fs.rm(tmp, { recursive: true, force: true });
}
});
it("resolves dist index path via symlinked argv1 (async)", async () => {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-ui-"));
try {
const realPkg = path.join(tmp, "real-pkg");
const bin = path.join(tmp, "bin");
await fs.mkdir(realPkg, { recursive: true });
await fs.mkdir(bin, { recursive: true });
await fs.writeFile(path.join(realPkg, "package.json"), JSON.stringify({ name: "openclaw" }));
await fs.writeFile(path.join(realPkg, "openclaw.mjs"), "export {};\n");
await fs.mkdir(path.join(realPkg, "dist", "control-ui"), { recursive: true });
await fs.writeFile(path.join(realPkg, "dist", "control-ui", "index.html"), "<html></html>\n");
const ok = await trySymlink(
path.join("..", "real-pkg", "openclaw.mjs"),
path.join(bin, "openclaw"),
);
if (!ok) {
return; // symlinks not supported (Windows CI)
}
const indexPath = await resolveControlUiDistIndexPath(path.join(bin, "openclaw"));
expect(indexPath).not.toBeNull();
expect(await canonicalPath(indexPath ?? "")).toBe(
await canonicalPath(path.join(realPkg, "dist", "control-ui", "index.html")),
);
} finally {
await fs.rm(tmp, { recursive: true, force: true });
}
});
});

View File

@@ -20,11 +20,15 @@ export async function resolveControlUiDistIndexHealth(
opts: {
root?: string;
argv1?: string;
moduleUrl?: string;
} = {},
): Promise<ControlUiDistIndexHealth> {
const indexPath = opts.root
? resolveControlUiDistIndexPathForRoot(opts.root)
: await resolveControlUiDistIndexPath(opts.argv1 ?? process.argv[1]);
: await resolveControlUiDistIndexPath({
argv1: opts.argv1 ?? process.argv[1],
moduleUrl: opts.moduleUrl,
});
return {
indexPath,
exists: Boolean(indexPath && fs.existsSync(indexPath)),
@@ -66,8 +70,11 @@ export function resolveControlUiRepoRoot(
}
export async function resolveControlUiDistIndexPath(
argv1: string | undefined = process.argv[1],
argv1OrOpts?: string | { argv1?: string; moduleUrl?: string },
): Promise<string | null> {
const argv1 =
typeof argv1OrOpts === "string" ? argv1OrOpts : (argv1OrOpts?.argv1 ?? process.argv[1]);
const moduleUrl = typeof argv1OrOpts === "object" ? argv1OrOpts?.moduleUrl : undefined;
if (!argv1) {
return null;
}
@@ -79,7 +86,7 @@ export async function resolveControlUiDistIndexPath(
return path.join(distDir, "control-ui", "index.html");
}
const packageRoot = await resolveOpenClawPackageRoot({ argv1: normalized });
const packageRoot = await resolveOpenClawPackageRoot({ argv1: normalized, moduleUrl });
if (packageRoot) {
return path.join(packageRoot, "dist", "control-ui", "index.html");
}

View File

@@ -60,6 +60,18 @@ function findPackageRootSync(startDir: string, maxDepth = 12): string | null {
function candidateDirsFromArgv1(argv1: string): string[] {
const normalized = path.resolve(argv1);
const candidates = [path.dirname(normalized)];
// Resolve symlinks for version managers (nvm, fnm, n, Homebrew/Linuxbrew)
// that create symlinks in bin/ pointing to the real package location.
try {
const resolved = fsSync.realpathSync(normalized);
if (resolved !== normalized) {
candidates.push(path.dirname(resolved));
}
} catch {
// realpathSync throws if path doesn't exist; keep original candidates
}
const parts = normalized.split(path.sep);
const binIndex = parts.lastIndexOf(".bin");
if (binIndex > 0 && parts[binIndex - 1] === "node_modules") {