fix: stop git service when switching install roots

This commit is contained in:
Shakker
2026-06-10 20:40:31 +01:00
committed by Shakker
parent e44835ae29
commit 124625ec4d
2 changed files with 52 additions and 3 deletions
+45
View File
@@ -2877,6 +2877,51 @@ describe("update-cli", () => {
expect(updateCall?.beforeGitMutation).toEqual(expect.any(Function));
});
it("stops a managed gateway rooted at the git checkout when switching package installs to dev", async () => {
const packageRoot = createCaseDir("openclaw-update-package-root");
const gitRoot = await createTrackedTempDir("openclaw-update-git-service-root-");
const serviceEntrypoint = path.join(gitRoot, "dist", "index.js");
await fs.mkdir(path.join(gitRoot, ".git"), { recursive: true });
await fs.mkdir(path.dirname(serviceEntrypoint), { recursive: true });
await fs.writeFile(
path.join(gitRoot, "package.json"),
JSON.stringify({ name: "openclaw", version: "2026.4.21" }),
"utf-8",
);
await fs.writeFile(serviceEntrypoint, "export {};\n", "utf-8");
mockPackageInstallStatus(packageRoot);
pathExists.mockImplementation(async (candidate: string) => candidate === gitRoot);
serviceReadCommand.mockResolvedValue({
programArguments: ["node", serviceEntrypoint, "gateway", "run"],
environment: {
OPENCLAW_SERVICE_MARKER: "openclaw",
OPENCLAW_SERVICE_KIND: "gateway",
},
});
serviceLoaded.mockResolvedValue(true);
serviceReadRuntime.mockResolvedValue({
status: "running",
pid: 4242,
state: "running",
});
mockGitUpdateAfterMutation(
makeOkUpdateResult({
mode: "git",
root: gitRoot,
}),
);
await withEnvAsync({ OPENCLAW_GIT_DIR: gitRoot }, async () => {
await updateCommand({ channel: "dev", yes: true });
});
expect(serviceStop).toHaveBeenCalledTimes(1);
expect(runGatewayUpdate).toHaveBeenCalledTimes(1);
const updateCall = vi.mocked(runGatewayUpdate).mock.calls[0]?.[0];
expect(updateCall?.cwd).toBe(gitRoot);
expect(updateCall?.beforeGitMutation).toEqual(expect.any(Function));
});
it("does not stop or restart a managed gateway owned by another git checkout", async () => {
const otherRoot = await createTrackedTempDir("openclaw-update-other-service-root-");
const otherEntrypoint = path.join(otherRoot, "dist", "index.js");
+7 -3
View File
@@ -3469,14 +3469,16 @@ async function updateCommandInternal(opts: UpdateCommandOptions): Promise<void>
const preUpdatePluginInstallRecords = await loadInstalledPluginIndexInstallRecords();
let preManagedServiceStop: PreManagedServiceStop | undefined;
const stopManagedServiceBeforeMutableUpdate = async () => {
const gitMutationRoot =
updateInstallKind === "git" ? (switchToGit ? resolveGitInstallDir() : root) : null;
const stopManagedServiceBeforeMutableUpdate = async (mutationRoot: string = root) => {
if (updateInstallKind !== "package" && updateInstallKind !== "git") {
return;
}
try {
preManagedServiceStop = await maybeStopManagedServiceBeforeMutableUpdate({
updateInstallKind,
root,
root: mutationRoot,
shouldRestart,
jsonMode: Boolean(opts.json),
});
@@ -3552,7 +3554,9 @@ async function updateCommandInternal(opts: UpdateCommandOptions): Promise<void>
stop,
devTargetRef,
beforeGitMutation:
updateInstallKind === "git" ? stopManagedServiceBeforeMutableUpdate : undefined,
updateInstallKind === "git"
? () => stopManagedServiceBeforeMutableUpdate(gitMutationRoot ?? root)
: undefined,
});
} catch (err) {
stop();