From 664c378d34254133c1418c6ba5c7477f8fbe53da Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Sun, 7 Dec 2025 19:57:55 +0000 Subject: [PATCH 1/2] fix: add a parse option to allow empty json files - Added a `allowEmptyContent: true` option in jsonc-parser's `parse` method to allow empty json files without it erroring. --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 553b7af..597b650 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -15,7 +15,7 @@ export function readJsonFile(filepath: string): any { const jsonErrors: jsonc.ParseError[] = []; const fileContent = fs.readFileSync(filepath).toString(); - const jsonContents = jsonc.parse(fileContent, jsonErrors) ?? {}; + const jsonContents = jsonc.parse(fileContent, jsonErrors, {allowEmptyContent: true}) ?? {}; if (jsonErrors.length > 0) { const errorMessages = constructJsonParseErrorMsg(filepath, fileContent, jsonErrors); From 86830056bb8a5b0e021b696278ae35ac1c44919d Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Sun, 7 Dec 2025 21:29:16 +0000 Subject: [PATCH 2/2] fix: `readJsonFile` to remove BOM if present in utf8 files. - Added `encoding: "utf"` option to Node's `readFileSync` in the `readJsonFile` util function. This is to ensure files are read as utf8 encoding. - Fixed `Invalid Symbol` parsing error. This could happen if the JSON file is saved as encoding UTF-8 with BOM, it's the BOM marker at the start of the file that fails parsing. So we remove the BOM character codes from the contents before parsing it. ref: https://stackoverflow.com/a/24376813/2358222 --- src/utils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 597b650..ea4faba 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -14,7 +14,10 @@ import {window} from "vscode"; export function readJsonFile(filepath: string): any { const jsonErrors: jsonc.ParseError[] = []; - const fileContent = fs.readFileSync(filepath).toString(); + const fileContent = fs + .readFileSync(filepath, {encoding: "utf8"}) + .toString() + .replace(/^\uFEFF/, ""); // Remove BOM if present. const jsonContents = jsonc.parse(fileContent, jsonErrors, {allowEmptyContent: true}) ?? {}; if (jsonErrors.length > 0) {