Skip to content

Commit a23d192

Browse files
authored
fix(doctor): surface legacy-config copy failures (#124948)
* fix(doctor): surface legacy-config copy failures maybeMigrateLegacyConfig swallowed every copyFile error with a bare catch whose comment claimed EEXIST-only. EACCES, ENOSPC, and cross-device failures were silently ignored: the operator's ~/.clawdbot/clawdbot.json exists, migration was attempted and failed, and doctor proceeded as a clean fresh install with no config, no change note, and no warning. Root cause: failure collapsed into the skip-silently success shape. The catch now rethrows anything other than EEXIST with the source/target paths in the message; EEXIST (config already at the target) keeps its skip semantics. Regression: read-only target dir makes the preflight reject with 'Failed to migrate legacy config' — fails pre-fix (resolved as a clean run); companion test proves the successful copy path still migrates. * chore(doctor): annotate the errno assertion for the safety ratchet * refactor(doctor): narrow the copy error without a type assertion The assertion-safety ratchet flagged the errno cast; property narrowing removes the assertion instead of annotating it.
1 parent 89f6ab1 commit a23d192

2 files changed

Lines changed: 94 additions & 2 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// A failing legacy-config copy must surface, not silently leave doctor
2+
// looking like a clean fresh install while the operator's config exists.
3+
import fs from "node:fs/promises";
4+
import path from "node:path";
5+
import { afterEach, describe, expect, it } from "vitest";
6+
import { withTempDir } from "../test-utils/temp-dir.js";
7+
import { runDoctorConfigPreflight } from "./doctor-config-preflight.js";
8+
9+
const envKeys = ["HOME", "OPENCLAW_CONFIG_PATH", "OPENCLAW_STATE_DIR"] as const;
10+
const savedEnv = new Map<string, string | undefined>();
11+
12+
function setEnv(values: Partial<Record<(typeof envKeys)[number], string>>) {
13+
for (const key of envKeys) {
14+
if (!savedEnv.has(key)) {
15+
savedEnv.set(key, process.env[key]);
16+
}
17+
const value = values[key];
18+
if (value === undefined) {
19+
delete process.env[key];
20+
} else {
21+
process.env[key] = value;
22+
}
23+
}
24+
}
25+
26+
afterEach(() => {
27+
for (const [key, value] of savedEnv) {
28+
if (value === undefined) {
29+
delete process.env[key];
30+
} else {
31+
process.env[key] = value;
32+
}
33+
}
34+
savedEnv.clear();
35+
});
36+
37+
describe("doctor legacy config migration failures", () => {
38+
it.runIf(process.platform !== "win32" && process.getuid?.() !== 0)(
39+
"surfaces a copy failure instead of proceeding as a fresh install",
40+
async () => {
41+
await withTempDir("openclaw-doctor-legacy-copy-", async (home) => {
42+
const legacyDir = path.join(home, ".clawdbot");
43+
await fs.mkdir(legacyDir, { recursive: true });
44+
await fs.writeFile(path.join(legacyDir, "clawdbot.json"), "{}\n", "utf-8");
45+
const targetDir = path.join(home, "readonly-state");
46+
await fs.mkdir(targetDir, { recursive: true });
47+
await fs.chmod(targetDir, 0o555);
48+
setEnv({
49+
HOME: home,
50+
OPENCLAW_CONFIG_PATH: path.join(targetDir, "openclaw.json"),
51+
OPENCLAW_STATE_DIR: path.join(home, "state"),
52+
});
53+
54+
try {
55+
await expect(
56+
runDoctorConfigPreflight({ migrateState: false, invalidConfigNote: false }),
57+
).rejects.toThrow(/Failed to migrate legacy config/);
58+
} finally {
59+
await fs.chmod(targetDir, 0o755);
60+
}
61+
});
62+
},
63+
);
64+
65+
it("migrates the legacy config and reports the change when the copy works", async () => {
66+
await withTempDir("openclaw-doctor-legacy-copy-", async (home) => {
67+
const legacyDir = path.join(home, ".clawdbot");
68+
await fs.mkdir(legacyDir, { recursive: true });
69+
await fs.writeFile(path.join(legacyDir, "clawdbot.json"), "{}\n", "utf-8");
70+
const targetPath = path.join(home, "state-root", "openclaw.json");
71+
setEnv({
72+
HOME: home,
73+
OPENCLAW_CONFIG_PATH: targetPath,
74+
OPENCLAW_STATE_DIR: path.join(home, "state"),
75+
});
76+
77+
await runDoctorConfigPreflight({ migrateState: false, invalidConfigNote: false });
78+
79+
await expect(fs.readFile(targetPath, "utf-8")).resolves.toBe("{}\n");
80+
});
81+
});
82+
});

‎src/commands/doctor-config-preflight.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { resolveCanonicalConfigPath } from "../config/paths.js";
1616
import type { ConfigFileSnapshot } from "../config/types.js";
1717
import type { OpenClawConfig } from "../config/types.openclaw.js";
1818
import { isTruthyEnvValue } from "../infra/env.js";
19+
import { formatErrorMessage } from "../infra/errors.js";
1920
import type {
2021
MigrationCheckpointIdentity,
2122
StartupMigrationLease,
@@ -99,8 +100,17 @@ async function maybeMigrateLegacyConfig(): Promise<string[]> {
99100
try {
100101
await fs.copyFile(legacyPath, targetPath, fs.constants.COPYFILE_EXCL);
101102
changes.push(`Migrated legacy config: ${legacyPath} -> ${targetPath}`);
102-
} catch {
103-
// If it already exists, skip silently.
103+
} catch (err) {
104+
// EEXIST means a config already lives at the target — nothing to migrate.
105+
// Any other failure (EACCES, ENOSPC) must surface: doctor would otherwise
106+
// proceed as a fresh install while the operator's legacy config exists.
107+
const code = err && typeof err === "object" && "code" in err ? err.code : undefined;
108+
if (code !== "EEXIST") {
109+
throw new Error(
110+
`Failed to migrate legacy config ${legacyPath} -> ${targetPath}: ${formatErrorMessage(err)}`,
111+
{ cause: err },
112+
);
113+
}
104114
}
105115

106116
return changes;

0 commit comments

Comments
 (0)