diff --git a/apps/desktop/src/utils.test.ts b/apps/desktop/src/utils.test.ts index aa099c6f65..a7b9c1904e 100644 --- a/apps/desktop/src/utils.test.ts +++ b/apps/desktop/src/utils.test.ts @@ -40,6 +40,7 @@ describe("loadJsonFile", () => { vol.fromJSON({ "./file.json": JSON.stringify({ file1: true }), "./nested/deep/file.json": JSON.stringify({ file2: true }), + "./bom.json": "\uFEFF" + JSON.stringify({ file3: true }), }); }); @@ -54,6 +55,12 @@ describe("loadJsonFile", () => { it("should return an empty object when file does not exist", () => { expect(loadJsonFile("unknown-file.json")).toStrictEqual({}); }); + + it("should parse a file saved with a UTF-8 byte order mark", () => { + // Guard: the fixture really does start with a BOM, otherwise this test passes vacuously. + expect(memfs.readFileSync("bom.json", { encoding: "utf-8" })).toMatch(/^\uFEFF/); + expect(loadJsonFile("bom.json")).toStrictEqual({ file3: true }); + }); }); describe("tryPaths", () => { diff --git a/apps/desktop/src/utils.ts b/apps/desktop/src/utils.ts index eedabf5237..f5355bad9b 100644 --- a/apps/desktop/src/utils.ts +++ b/apps/desktop/src/utils.ts @@ -40,7 +40,8 @@ export function loadJsonFile(...paths: string[]): T { return {} as T; } - const file = fs.readFileSync(joinedPaths, { encoding: "utf-8" }); + // Editors on Windows commonly save UTF-8 files with a byte order mark, which JSON.parse rejects. + const file = fs.readFileSync(joinedPaths, { encoding: "utf-8" }).replace(/^\uFEFF/, ""); return JSON.parse(file); }