fix: clean up temp file if renameSync fails in writeFileAtomic

Co-Authored-By: mantra <mantra@stack-auth.com>
This commit is contained in:
Devin AI 2026-06-04 23:48:19 +00:00
parent dc37e13515
commit 992ea13316

View File

@ -98,7 +98,12 @@ function writeFileAtomic(configFilePath: string, content: string): void {
mkdirSync(dir, { recursive: true });
const tempPath = path.join(dir, `.stack.config.${Math.random().toString(36).slice(2)}.tmp`);
writeFileSync(tempPath, content, "utf-8");
renameSync(tempPath, configFilePath);
try {
renameSync(tempPath, configFilePath);
} catch (error) {
try { rmSync(tempPath); } catch { /* best-effort cleanup */ }
throw error;
}
}
function renderConfigObjectToFile(configFilePath: string, config: Config): void {