Skip to content

Commit 3538f82

Browse files
committed
fix(config): clean partial invalid backup writes
1 parent ffb0627 commit 3538f82

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

packages/poe-code-config/src/poe-code-config.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,28 @@ describe("store", () => {
12361236
);
12371237
});
12381238

1239+
it("cleans a partial invalid config backup when recovery fails", async () => {
1240+
const original = "not json\n";
1241+
const base = createMockFs({ "~/.poe-code/config.json": original }, homeDir);
1242+
let backupPath: string | undefined;
1243+
const fs: FileSystem = {
1244+
...base,
1245+
async writeFile(targetPath, content, options) {
1246+
if (targetPath.includes(".invalid-")) {
1247+
backupPath = targetPath;
1248+
await base.writeFile(targetPath, "partial backup\n", options);
1249+
throw new Error("config backup disk full");
1250+
}
1251+
await base.writeFile(targetPath, content, options);
1252+
}
1253+
};
1254+
1255+
await expect(readDocument(fs, configPath)).rejects.toThrow("config backup disk full");
1256+
expect(backupPath).toBeDefined();
1257+
expect(base.getContent(backupPath ?? "")).toBeUndefined();
1258+
await expect(base.readFile(configPath, "utf8")).resolves.toBe(original);
1259+
});
1260+
12391261
it("writes a scope while preserving unrelated scopes", async () => {
12401262
const fs = createMockFs(
12411263
{

packages/poe-code-config/src/store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ async function writeInvalidBackup(fs: FileSystem, filePath: string, content: str
240240
return;
241241
} catch (error) {
242242
if (!isAlreadyExists(error)) {
243+
await fs.unlink(candidate).catch(() => undefined);
243244
throw error;
244245
}
245246
}

0 commit comments

Comments
 (0)