Skip to content

Commit 39065a5

Browse files
fix(file-safety): let safeWriteJson treat an orphaned pre-write backup as logged, not failed
safeWriteJson documents backup deletion as best-effort for its callers (the write commits, the pre-write copy may be left behind, the call still succeeds). The new OrphanedPublishedBackupError signal from safeWriteText propagated through that caller and turned ten safeWriteJson tests red on the platform lanes, so safeWriteJson now catches exactly that error, logs the orphan path as a warning, and keeps its contract. Callers that use safeWriteText directly (the guarded/edit path) still receive the strict signal. Local evidence: safeWriteJson + safeWriteText suites 94 passed / 1 skipped in this worktree. (Local eslint/tsc are not usable in this worktree because its borrowed node_modules layout cannot resolve the workspace packages; CI compile is the authority.)
1 parent 153a04c commit 39065a5

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎src/utils/safeWriteJson.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { JsonStreamStringify } from "json-stream-stringify"
55

66
import { acquireFileLock } from "./fileLock"
77
import {
8+
OrphanedPublishedBackupError,
89
resolveLockKey,
910
resolvePublishTarget,
1011
safeWriteText,
@@ -128,7 +129,20 @@ async function safeWriteJson(filePath: string, data: any, options?: SafeWriteJso
128129
backup: true,
129130
}
130131

131-
await safeWriteText(resolvedTargetPath, "", textOptions)
132+
try {
133+
await safeWriteText(resolvedTargetPath, "", textOptions)
134+
} catch (error) {
135+
// Backup cleanup is best-effort for this caller: the content is already published
136+
// at the target, and safeWriteJson's contract (and its existing callers) treats a
137+
// leftover pre-write copy as a logged condition rather than a failed write. The
138+
// strict signal stays available to callers that use safeWriteText directly.
139+
if (!(error instanceof OrphanedPublishedBackupError)) {
140+
throw error
141+
}
142+
console.warn(
143+
`safeWriteJson: ${resolvedTargetPath} was published but its pre-write backup could not be removed: ${error.orphanedBackupPath}`,
144+
)
145+
}
132146

133147
// If we reach here, the new file is successfully in place and any
134148
// backup has already been handled by safeWriteText.

0 commit comments

Comments
 (0)