@@ -46,9 +46,6 @@ export const writeFileStringAtomically = (input: {
4646 const targetDirectory = path . dirname ( targetPath ) ;
4747
4848 yield * fs . makeDirectory ( targetDirectory , { recursive : true } ) ;
49- // Opened up front so an unreadable directory fails the save before the target changes.
50- const directory =
51- platform === "win32" ? undefined : yield * fs . open ( targetDirectory , { flag : "r" } ) ;
5249 // Cleanup failure must not fail a write that already landed.
5350 const tempDirectory = yield * Effect . acquireRelease (
5451 fs . makeTempDirectory ( {
@@ -71,11 +68,17 @@ export const writeFileStringAtomically = (input: {
7168 Effect . flatMap ( ( file ) => file . sync ) ,
7269 Effect . scoped ,
7370 ) ;
74- if ( directory !== undefined ) {
71+ if ( platform !== "win32" ) {
7572 yield * fs . rename ( tempPath , targetPath ) ;
76- // Persists the rename; only filesystems that cannot sync directories are let through.
77- return yield * directory . sync . pipe (
78- Effect . catchIf ( isUnsupportedSync , ( error ) => Effect . logWarning ( error ) ) ,
73+ // Persists the rename. A directory without read access cannot be opened to sync, and
74+ // replacing in one needs only write and search access, so that save stands.
75+ return yield * fs . open ( targetDirectory , { flag : "r" } ) . pipe (
76+ Effect . flatMap ( ( directory ) => directory . sync ) ,
77+ Effect . scoped ,
78+ Effect . catchIf (
79+ ( error ) => error . reason . _tag === "PermissionDenied" || isUnsupportedSync ( error ) ,
80+ ( error ) => Effect . logWarning ( error ) ,
81+ ) ,
7982 ) ;
8083 }
8184
0 commit comments