Skip to content
Closed
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
14 changes: 12 additions & 2 deletions packages/core/src/fs-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export namespace FSUtil {
})

const readJson = Effect.fn("FileSystem.readJson")(function* (path: string) {
const text = yield* fs.readFileString(path)
const bytes = yield* fs.readFile(path)
return yield* Effect.try({
try: () => JSON.parse(text),
try: () => JSON.parse(decodeJsonBytes(bytes)),
catch: (cause) => new FileSystemError({ method: "readJson", cause }),
})
})
Expand Down Expand Up @@ -272,3 +272,13 @@ export namespace FSUtil {
return result === "" || (!isAbsolute(result) && result !== ".." && !result.startsWith(`..${sep}`))
}
}

// Windows shells commonly write redirected output as UTF-16 with a BOM
// (PowerShell's `>` uses UTF-16LE), so accept BOM-prefixed JSON files.
// TextDecoder's UTF-8 mode strips a UTF-8 BOM by default.
function decodeJsonBytes(bytes: Uint8Array): string {
if (bytes[0] === 0xff && bytes[1] === 0xfe) return Buffer.from(bytes.subarray(2)).toString("utf16le")
// UTF-16BE: swap to little-endian, then decode.
if (bytes[0] === 0xfe && bytes[1] === 0xff) return Buffer.from(bytes.subarray(2)).swap16().toString("utf16le")
return new TextDecoder("utf-8").decode(bytes)
}
48 changes: 48 additions & 0 deletions packages/core/test/filesystem/filesystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,54 @@ describe("FSUtil", () => {
expect(result).toHaveProperty("_tag", "FileSystemError")
}),
)

it(
"reads UTF-16LE JSON with BOM (Windows PowerShell redirection)",
Effect.gen(function* () {
const fs = yield* FSUtil.Service
const filesys = yield* FileSystem.FileSystem
const tmp = yield* filesys.makeTempDirectoryScoped()
const file = path.join(tmp, "utf16le.json")
const json = JSON.stringify({ name: "test", count: 42 })

yield* filesys.writeFile(file, Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(json, "utf16le")]))

expect(yield* fs.readJson(file)).toEqual({ name: "test", count: 42 })
}),
)

it(
"reads UTF-16BE JSON with BOM",
Effect.gen(function* () {
const fs = yield* FSUtil.Service
const filesys = yield* FileSystem.FileSystem
const tmp = yield* filesys.makeTempDirectoryScoped()
const file = path.join(tmp, "utf16be.json")
const json = JSON.stringify({ name: "test", count: 42 })

yield* filesys.writeFile(
file,
Buffer.concat([Buffer.from([0xfe, 0xff]), Buffer.from(json, "utf16le").swap16()]),
)

expect(yield* fs.readJson(file)).toEqual({ name: "test", count: 42 })
}),
)

it(
"reads UTF-8 JSON with BOM",
Effect.gen(function* () {
const fs = yield* FSUtil.Service
const filesys = yield* FileSystem.FileSystem
const tmp = yield* filesys.makeTempDirectoryScoped()
const file = path.join(tmp, "utf8bom.json")
const json = JSON.stringify({ name: "test", count: 42 })

yield* filesys.writeFile(file, Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(json, "utf8")]))

expect(yield* fs.readJson(file)).toEqual({ name: "test", count: 42 })
}),
)
})

describe("ensureDir", () => {
Expand Down
Loading