Skip to content

Commit 0f3c1ae

Browse files
fix(shared): saves in a non-listable directory skip the directory sync instead of failing
1 parent 2b6c568 commit 0f3c1ae

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

‎packages/shared/src/atomicWrite.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
320320

321321
// Injected because root ignores a chmod that drops read access.
322322
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
323-
"leaves the target intact when its directory cannot be opened for the sync",
323+
"saves when its directory cannot be opened for the sync",
324324
() =>
325325
Effect.gen(function* () {
326326
const fs = yield* FileSystem.FileSystem;
@@ -345,8 +345,8 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
345345
Effect.exit,
346346
);
347347

348-
assert.deepStrictEqual(exit, Exit.fail(openFailure));
349-
assert.strictEqual(yield* fs.readFileString(filePath), "before");
348+
assert.deepStrictEqual(exit, Exit.void);
349+
assert.strictEqual(yield* fs.readFileString(filePath), "after");
350350
}),
351351
);
352352

‎packages/shared/src/atomicWrite.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)