mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 04:31:10 +00:00
fix: harden update dev switch and refresh changelog
This commit is contained in:
@@ -355,4 +355,34 @@ describe("runDaemonInstall", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("reuses env-backed service secrets during forced reinstall when the current shell is missing them", async () => {
|
||||
service.isLoaded.mockResolvedValue(true);
|
||||
service.readCommand.mockResolvedValue({
|
||||
programArguments: ["openclaw", "gateway", "run"],
|
||||
environment: {
|
||||
OPENAI_API_KEY: "service-openai-key",
|
||||
},
|
||||
} as never);
|
||||
const previous = process.env.OPENAI_API_KEY;
|
||||
delete process.env.OPENAI_API_KEY;
|
||||
try {
|
||||
await runDaemonInstall({ json: true, force: true });
|
||||
|
||||
expect(buildGatewayInstallPlanMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
env: expect.objectContaining({
|
||||
OPENAI_API_KEY: "service-openai-key",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(installDaemonServiceAndEmitMock).toHaveBeenCalledTimes(1);
|
||||
} finally {
|
||||
if (previous === undefined) {
|
||||
delete process.env.OPENAI_API_KEY;
|
||||
} else {
|
||||
process.env.OPENAI_API_KEY = previous;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -18,6 +18,19 @@ import {
|
||||
} from "./shared.js";
|
||||
import type { DaemonInstallOptions } from "./types.js";
|
||||
|
||||
function mergeInstallInvocationEnv(params: {
|
||||
env: NodeJS.ProcessEnv;
|
||||
existingServiceEnv?: Record<string, string>;
|
||||
}): NodeJS.ProcessEnv {
|
||||
if (!params.existingServiceEnv || Object.keys(params.existingServiceEnv).length === 0) {
|
||||
return params.env;
|
||||
}
|
||||
return {
|
||||
...params.existingServiceEnv,
|
||||
...params.env,
|
||||
};
|
||||
}
|
||||
|
||||
export async function runDaemonInstall(opts: DaemonInstallOptions) {
|
||||
const { json, stdout, warnings, emit, fail } = createDaemonInstallActionContext(opts.json);
|
||||
if (failIfNixDaemonInstallMode(fail)) {
|
||||
@@ -43,6 +56,7 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
|
||||
|
||||
const service = resolveGatewayService();
|
||||
let loaded = false;
|
||||
let existingServiceEnv: Record<string, string> | undefined;
|
||||
try {
|
||||
loaded = await service.isLoaded({ env: process.env });
|
||||
} catch (err) {
|
||||
@@ -53,6 +67,13 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (loaded) {
|
||||
existingServiceEnv = (await service.readCommand(process.env).catch(() => null))?.environment;
|
||||
}
|
||||
const installEnv = mergeInstallInvocationEnv({
|
||||
env: process.env,
|
||||
existingServiceEnv,
|
||||
});
|
||||
if (loaded) {
|
||||
if (!opts.force) {
|
||||
if (await gatewayServiceNeedsAutoNodeExtraCaCertsRefresh({ service, env: process.env })) {
|
||||
@@ -82,7 +103,7 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
|
||||
|
||||
const tokenResolution = await resolveGatewayInstallToken({
|
||||
config: cfg,
|
||||
env: process.env,
|
||||
env: installEnv,
|
||||
explicitToken: opts.token,
|
||||
autoGenerateWhenMissing: true,
|
||||
persistGeneratedToken: true,
|
||||
@@ -100,7 +121,7 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
|
||||
}
|
||||
|
||||
const { programArguments, workingDirectory, environment } = await buildGatewayInstallPlan({
|
||||
env: process.env,
|
||||
env: installEnv,
|
||||
port,
|
||||
runtime: runtimeRaw,
|
||||
warn: (message) => {
|
||||
@@ -121,7 +142,7 @@ export async function runDaemonInstall(opts: DaemonInstallOptions) {
|
||||
fail,
|
||||
install: async () => {
|
||||
await service.install({
|
||||
env: process.env,
|
||||
env: installEnv,
|
||||
stdout,
|
||||
programArguments,
|
||||
workingDirectory,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { Command } from "commander";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
@@ -145,6 +146,7 @@ const { runDaemonRestart, runDaemonInstall } = await import("./daemon-cli.js");
|
||||
const { doctorCommand } = await import("../commands/doctor.js");
|
||||
const { defaultRuntime } = await import("../runtime.js");
|
||||
const { updateCommand, updateStatusCommand, updateWizardCommand } = await import("./update-cli.js");
|
||||
const { resolveGitInstallDir } = await import("./update-cli/shared.js");
|
||||
|
||||
describe("update-cli", () => {
|
||||
const fixtureRoot = "/tmp/openclaw-update-tests";
|
||||
@@ -714,6 +716,92 @@ describe("update-cli", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("persists the requested channel only after a successful package update", async () => {
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
mockPackageInstallStatus(tempDir);
|
||||
|
||||
await updateCommand({ channel: "beta", yes: true });
|
||||
|
||||
const installCallIndex = vi
|
||||
.mocked(runCommandWithTimeout)
|
||||
.mock.calls.findIndex(
|
||||
(call) =>
|
||||
Array.isArray(call[0]) &&
|
||||
call[0][0] === "npm" &&
|
||||
call[0][1] === "i" &&
|
||||
call[0][2] === "-g",
|
||||
);
|
||||
expect(installCallIndex).toBeGreaterThanOrEqual(0);
|
||||
expect(writeConfigFile).toHaveBeenCalledTimes(1);
|
||||
expect(writeConfigFile).toHaveBeenCalledWith({
|
||||
update: {
|
||||
channel: "beta",
|
||||
},
|
||||
});
|
||||
expect(
|
||||
vi.mocked(runCommandWithTimeout).mock.invocationCallOrder[installCallIndex] ?? 0,
|
||||
).toBeLessThan(
|
||||
vi.mocked(writeConfigFile).mock.invocationCallOrder[0] ?? Number.MAX_SAFE_INTEGER,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not persist the requested channel when the package update fails", async () => {
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
mockPackageInstallStatus(tempDir);
|
||||
vi.mocked(runCommandWithTimeout).mockImplementation(async (argv) => {
|
||||
if (Array.isArray(argv) && argv[0] === "npm" && argv[1] === "i" && argv[2] === "-g") {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "install failed",
|
||||
code: 1,
|
||||
signal: null,
|
||||
killed: false,
|
||||
termination: "exit",
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: "",
|
||||
code: 0,
|
||||
signal: null,
|
||||
killed: false,
|
||||
termination: "exit",
|
||||
};
|
||||
});
|
||||
|
||||
await updateCommand({ channel: "beta", yes: true });
|
||||
|
||||
expect(writeConfigFile).not.toHaveBeenCalled();
|
||||
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
|
||||
});
|
||||
|
||||
it("keeps the requested channel when plugin sync writes config after update", async () => {
|
||||
const tempDir = createCaseDir("openclaw-update");
|
||||
mockPackageInstallStatus(tempDir);
|
||||
syncPluginsForUpdateChannel.mockImplementation(async ({ config }) => ({
|
||||
changed: true,
|
||||
config,
|
||||
summary: {
|
||||
switchedToBundled: [],
|
||||
switchedToNpm: [],
|
||||
warnings: [],
|
||||
errors: [],
|
||||
},
|
||||
}));
|
||||
updateNpmInstalledPlugins.mockImplementation(async ({ config }) => ({
|
||||
changed: false,
|
||||
config,
|
||||
outcomes: [],
|
||||
}));
|
||||
|
||||
await updateCommand({ channel: "beta", yes: true });
|
||||
|
||||
const lastWrite = vi.mocked(writeConfigFile).mock.calls.at(-1)?.[0] as
|
||||
| { update?: { channel?: string } }
|
||||
| undefined;
|
||||
expect(lastWrite?.update?.channel).toBe("beta");
|
||||
});
|
||||
|
||||
it("updateCommand handles service env refresh and restart behavior", async () => {
|
||||
const cases = [
|
||||
{
|
||||
@@ -1040,4 +1128,12 @@ describe("update-cli", () => {
|
||||
expect(call?.channel).toBe("dev");
|
||||
});
|
||||
});
|
||||
|
||||
it("uses ~/openclaw as the default dev checkout directory", async () => {
|
||||
const homedirSpy = vi.spyOn(os, "homedir").mockReturnValue("/tmp/oc-home");
|
||||
await withEnvAsync({ OPENCLAW_GIT_DIR: undefined }, async () => {
|
||||
expect(resolveGitInstallDir()).toBe("/tmp/oc-home/openclaw");
|
||||
});
|
||||
homedirSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,6 @@ import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { resolveStateDir } from "../../config/paths.js";
|
||||
import { resolveOpenClawPackageRoot } from "../../infra/openclaw-root.js";
|
||||
import { readPackageName, readPackageVersion } from "../../infra/package-json.js";
|
||||
import { normalizePackageTagInput } from "../../infra/package-tag.js";
|
||||
@@ -11,6 +10,7 @@ import { parseSemver } from "../../infra/runtime-guard.js";
|
||||
import { fetchNpmTagVersion } from "../../infra/update-check.js";
|
||||
import {
|
||||
canResolveRegistryVersionForPackageTarget,
|
||||
createGlobalInstallEnv,
|
||||
detectGlobalInstallManagerByPresence,
|
||||
detectGlobalInstallManagerForRoot,
|
||||
type CommandRunner,
|
||||
@@ -121,7 +121,7 @@ export function resolveGitInstallDir(): string {
|
||||
}
|
||||
|
||||
function resolveDefaultGitDir(): string {
|
||||
return resolveStateDir(process.env, os.homedir);
|
||||
return path.join(os.homedir(), "openclaw");
|
||||
}
|
||||
|
||||
export function resolveNodeRunner(): string {
|
||||
@@ -192,12 +192,15 @@ export async function ensureGitCheckout(params: {
|
||||
dir: string;
|
||||
timeoutMs: number;
|
||||
progress?: UpdateStepProgress;
|
||||
env?: NodeJS.ProcessEnv;
|
||||
}): Promise<UpdateStepResult | null> {
|
||||
const gitEnv = params.env ?? (await createGlobalInstallEnv());
|
||||
const dirExists = await pathExists(params.dir);
|
||||
if (!dirExists) {
|
||||
return await runUpdateStep({
|
||||
name: "git clone",
|
||||
argv: ["git", "clone", OPENCLAW_REPO_URL, params.dir],
|
||||
env: gitEnv,
|
||||
timeoutMs: params.timeoutMs,
|
||||
progress: params.progress,
|
||||
});
|
||||
@@ -215,6 +218,7 @@ export async function ensureGitCheckout(params: {
|
||||
name: "git clone",
|
||||
argv: ["git", "clone", OPENCLAW_REPO_URL, params.dir],
|
||||
cwd: params.dir,
|
||||
env: gitEnv,
|
||||
timeoutMs: params.timeoutMs,
|
||||
progress: params.progress,
|
||||
});
|
||||
|
||||
@@ -385,10 +385,12 @@ async function runGitUpdate(params: {
|
||||
}): Promise<UpdateRunResult> {
|
||||
const updateRoot = params.switchToGit ? resolveGitInstallDir() : params.root;
|
||||
const effectiveTimeout = params.timeoutMs ?? 20 * 60_000;
|
||||
const installEnv = await createGlobalInstallEnv();
|
||||
|
||||
const cloneStep = params.switchToGit
|
||||
? await ensureGitCheckout({
|
||||
dir: updateRoot,
|
||||
env: installEnv,
|
||||
timeoutMs: effectiveTimeout,
|
||||
progress: params.progress,
|
||||
})
|
||||
@@ -429,7 +431,7 @@ async function runGitUpdate(params: {
|
||||
name: "global install",
|
||||
argv: globalInstallArgs(manager, updateRoot),
|
||||
cwd: updateRoot,
|
||||
env: await createGlobalInstallEnv(),
|
||||
env: installEnv,
|
||||
timeoutMs: effectiveTimeout,
|
||||
progress: params.progress,
|
||||
});
|
||||
@@ -859,20 +861,6 @@ export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
|
||||
);
|
||||
}
|
||||
|
||||
if (requestedChannel && configSnapshot.valid) {
|
||||
const next = {
|
||||
...configSnapshot.config,
|
||||
update: {
|
||||
...configSnapshot.config.update,
|
||||
channel: requestedChannel,
|
||||
},
|
||||
};
|
||||
await writeConfigFile(next);
|
||||
if (!opts.json) {
|
||||
defaultRuntime.log(theme.muted(`Update channel set to ${requestedChannel}.`));
|
||||
}
|
||||
}
|
||||
|
||||
const showProgress = !opts.json && process.stdout.isTTY;
|
||||
if (!opts.json) {
|
||||
defaultRuntime.log(theme.heading("Updating OpenClaw..."));
|
||||
@@ -956,10 +944,31 @@ export async function updateCommand(opts: UpdateCommandOptions): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
let postUpdateConfigSnapshot = configSnapshot;
|
||||
if (requestedChannel && configSnapshot.valid && requestedChannel !== storedChannel) {
|
||||
const next = {
|
||||
...configSnapshot.config,
|
||||
update: {
|
||||
...configSnapshot.config.update,
|
||||
channel: requestedChannel,
|
||||
},
|
||||
};
|
||||
await writeConfigFile(next);
|
||||
postUpdateConfigSnapshot = {
|
||||
...configSnapshot,
|
||||
parsed: next,
|
||||
resolved: next,
|
||||
config: next,
|
||||
};
|
||||
if (!opts.json) {
|
||||
defaultRuntime.log(theme.muted(`Update channel set to ${requestedChannel}.`));
|
||||
}
|
||||
}
|
||||
|
||||
await updatePluginsAfterCoreUpdate({
|
||||
root,
|
||||
channel,
|
||||
configSnapshot,
|
||||
configSnapshot: postUpdateConfigSnapshot,
|
||||
opts,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user