Fix updater refresh cwd for service reinstall (#45452)

* Fix updater refresh cwd for service reinstall

* Update: preserve relative env overrides during service refresh

* Test: cover updater service refresh env rebasing
This commit is contained in:
Vincent Koc 2026-03-13 17:27:12 -04:00 committed by GitHub
parent 090c0c4b5d
commit e56e0cc913
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 1 deletions

View File

@ -624,12 +624,50 @@ describe("update-cli", () => {
expect(runCommandWithTimeout).toHaveBeenCalledWith(
[expect.stringMatching(/node/), entryPath, "gateway", "install", "--force"],
expect.objectContaining({ timeoutMs: 60_000 }),
expect.objectContaining({ cwd: root, timeoutMs: 60_000 }),
);
expect(runDaemonInstall).not.toHaveBeenCalled();
expect(runRestartScript).toHaveBeenCalled();
});
it("updateCommand preserves invocation-relative service env overrides during refresh", async () => {
const root = createCaseDir("openclaw-updated-root");
const entryPath = path.join(root, "dist", "entry.js");
pathExists.mockImplementation(async (candidate: string) => candidate === entryPath);
vi.mocked(runGatewayUpdate).mockResolvedValue({
status: "ok",
mode: "npm",
root,
steps: [],
durationMs: 100,
});
serviceLoaded.mockResolvedValue(true);
await withEnvAsync(
{
OPENCLAW_STATE_DIR: "./state",
OPENCLAW_CONFIG_PATH: "./config/openclaw.json",
},
async () => {
await updateCommand({});
},
);
expect(runCommandWithTimeout).toHaveBeenCalledWith(
[expect.stringMatching(/node/), entryPath, "gateway", "install", "--force"],
expect.objectContaining({
cwd: root,
env: expect.objectContaining({
OPENCLAW_STATE_DIR: path.resolve("./state"),
OPENCLAW_CONFIG_PATH: path.resolve("./config/openclaw.json"),
}),
timeoutMs: 60_000,
}),
);
expect(runDaemonInstall).not.toHaveBeenCalled();
});
it("updateCommand falls back to restart when env refresh install fails", async () => {
await runRestartFallbackScenario({ daemonInstall: "fail" });
});

View File

@ -69,6 +69,13 @@ import { suppressDeprecations } from "./suppress-deprecations.js";
const CLI_NAME = resolveCliName();
const SERVICE_REFRESH_TIMEOUT_MS = 60_000;
const SERVICE_REFRESH_PATH_ENV_KEYS = [
"OPENCLAW_HOME",
"OPENCLAW_STATE_DIR",
"CLAWDBOT_STATE_DIR",
"OPENCLAW_CONFIG_PATH",
"CLAWDBOT_CONFIG_PATH",
] as const;
const UPDATE_QUIPS = [
"Leveled up! New skills unlocked. You're welcome.",
@ -117,6 +124,25 @@ function formatCommandFailure(stdout: string, stderr: string): string {
return detail.split("\n").slice(-3).join("\n");
}
function resolveServiceRefreshEnv(
env: NodeJS.ProcessEnv,
invocationCwd: string = process.cwd(),
): NodeJS.ProcessEnv {
const resolvedEnv: NodeJS.ProcessEnv = { ...env };
for (const key of SERVICE_REFRESH_PATH_ENV_KEYS) {
const rawValue = resolvedEnv[key]?.trim();
if (!rawValue) {
continue;
}
if (rawValue.startsWith("~") || path.isAbsolute(rawValue) || path.win32.isAbsolute(rawValue)) {
resolvedEnv[key] = rawValue;
continue;
}
resolvedEnv[key] = path.resolve(invocationCwd, rawValue);
}
return resolvedEnv;
}
type UpdateDryRunPreview = {
dryRun: true;
root: string;
@ -190,6 +216,8 @@ async function refreshGatewayServiceEnv(params: {
continue;
}
const res = await runCommandWithTimeout([resolveNodeRunner(), candidate, ...args], {
cwd: params.result.root,
env: resolveServiceRefreshEnv(process.env),
timeoutMs: SERVICE_REFRESH_TIMEOUT_MS,
});
if (res.code === 0) {