Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/core/src/fs-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ export namespace FSUtil {
})

const readFileStringSafe = Effect.fn("FileSystem.readFileStringSafe")(function* (path: string) {
return yield* fs
.readFileString(path)
.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(undefined)))
return yield* fs.readFileString(path).pipe(
Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(undefined)),
// A probed path that turns out to be a directory yields EISDIR, which the
// platform layer reports as BadResource. Treat it like a missing file so
// callers walking config locations don't crash on a directory match.
Effect.catchReason("PlatformError", "BadResource", () => Effect.succeed(undefined)),
)
})

const isDir = Effect.fn("FileSystem.isDir")(function* (path: string) {
Expand Down
14 changes: 14 additions & 0 deletions packages/core/test/filesystem/filesystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ describe("FSUtil", () => {
expect(result).toBeUndefined()
}),
)

it(
"returns undefined when the path is a directory (EISDIR)",
Effect.gen(function* () {
const fs = yield* FSUtil.Service
const filesys = yield* FileSystem.FileSystem
const tmp = yield* filesys.makeTempDirectoryScoped()
const dir = path.join(tmp, "subdir")
yield* filesys.makeDirectory(dir)

const result = yield* fs.readFileStringSafe(dir)
expect(result).toBeUndefined()
}),
)
})

describe("readJson / writeJson", () => {
Expand Down
Loading