diff --git a/packages/core/src/fs-util.ts b/packages/core/src/fs-util.ts index 24263cbadf93..293160b9a42d 100644 --- a/packages/core/src/fs-util.ts +++ b/packages/core/src/fs-util.ts @@ -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) { diff --git a/packages/core/test/filesystem/filesystem.test.ts b/packages/core/test/filesystem/filesystem.test.ts index 10f61d8a97f9..7deb25d7e56c 100644 --- a/packages/core/test/filesystem/filesystem.test.ts +++ b/packages/core/test/filesystem/filesystem.test.ts @@ -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", () => {