fix(config): fsync backup and rename metadata

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-08-06 19:28:12 +00:00
1 parent 4203b34330
commit 2ba053e2c3
4 files changed
+62 -4

No files matched your search

@@ -1,3 +1,4 @@
import fsSync from "node:fs";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
@@ -1029,8 +1030,13 @@ describe("worktree config repair", () => {
delete process.env.PORT;
delete process.env.DATABASE_URL;
const open = vi.spyOn(fsSync, "openSync");
const sync = vi.spyOn(fsSync, "fsyncSync");
maybePersistWorktreeRuntimePorts({ serverPort: 3103, databasePort: 54335 });
expect(open).toHaveBeenCalledWith(paperclipDir, "r");
expect(sync).toHaveBeenCalled();
const writtenConfig = JSON.parse(await fs.readFile(configPath, "utf8"));
expect(writtenConfig).toMatchObject({
topLevelExtension: { enabled: true },
+15
View File
@@ -324,6 +324,21 @@ function atomicWriteFile(filePath: string, contents: string): void {
fs.closeSync(fileDescriptor);
fileDescriptor = null;
fs.renameSync(temporaryPath, filePath);
let directoryDescriptor: number | null = null;
try {
directoryDescriptor = fs.openSync(path.dirname(filePath), "r");
fs.fsyncSync(directoryDescriptor);
} catch (error) {
const code = error instanceof Error && "code" in error ? error.code : null;
if (
process.platform !== "win32" ||
!["EACCES", "EINVAL", "EISDIR", "ENOTSUP", "EPERM"].includes(String(code))
) {
throw error;
}
} finally {
if (directoryDescriptor !== null) fs.closeSync(directoryDescriptor);
}
return;
} catch (error) {
if (fileDescriptor !== null) fs.closeSync(fileDescriptor);