Skip to content

Commit d15d2f2

Browse files
committed
fix(config-mutations): clean partial mutation temp writes
1 parent d723917 commit d15d2f2

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

packages/config-mutations/src/config-mutations.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,12 @@ describe("runMutations", () => {
845845
it("does not corrupt an existing document when replacement write fails", async () => {
846846
const targetPath = `${homeDir}/.config.json`;
847847
const base = createFsFromVolume(Volume.fromJSON({ [targetPath]: '{"existing":true}\n' })).promises as unknown as FileSystem;
848+
let tempPath: string | undefined;
848849
const fs: FileSystem = {
849850
...base,
850851
async writeFile(filePath, data, options) {
851852
if (filePath.includes(".mutation-tmp-")) {
853+
tempPath = filePath;
852854
await base.writeFile(filePath, "{", options);
853855
throw new Error("config disk full");
854856
}
@@ -860,6 +862,10 @@ describe("runMutations", () => {
860862
runMutations([configMutation.merge({ target: "~/.config.json", value: { added: true } })], { fs, homeDir })
861863
).rejects.toThrow("config disk full");
862864
await expect(base.readFile(targetPath, "utf8")).resolves.toBe('{"existing":true}\n');
865+
expect(tempPath).toBeDefined();
866+
await expect(base.readFile(tempPath ?? "", "utf8")).rejects.toMatchObject({
867+
code: "ENOENT"
868+
});
863869
});
864870

865871
it("does not remove a colliding mutation temp symlink", async () => {

packages/config-mutations/src/execution/apply-mutation.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,8 @@ async function writeAtomically(
112112
tempCreated = false;
113113
return;
114114
} catch (error) {
115-
if (isAlreadyExists(error)) {
116-
continue;
117-
}
118-
119-
if (tempCreated) {
115+
const alreadyExists = isAlreadyExists(error);
116+
if (tempCreated || !alreadyExists) {
120117
try {
121118
await context.fs.unlink(tempPath);
122119
} catch (cleanupError) {
@@ -125,6 +122,11 @@ async function writeAtomically(
125122
}
126123
}
127124
}
125+
126+
if (alreadyExists) {
127+
continue;
128+
}
129+
128130
throw error;
129131
}
130132
}

0 commit comments

Comments
 (0)