fix(installer): include file path in JSON parse errors

This commit is contained in:
Adam Biggs 2026-03-06 12:29:48 -08:00
parent 32c2724561
commit bce48d8139
2 changed files with 10 additions and 2 deletions

View File

@ -405,12 +405,15 @@ async function runTests() {
const p = path.join(TMP, 'bad.json'); const p = path.join(TMP, 'bad.json');
nativeFs.writeFileSync(p, '{ invalid json }'); nativeFs.writeFileSync(p, '{ invalid json }');
let threw = false; let threw = false;
let errorMessage = '';
try { try {
fs.readJsonSync(p); fs.readJsonSync(p);
} catch { } catch (error) {
threw = true; threw = true;
errorMessage = error.message;
} }
assert(threw, 'readJsonSync did not throw on invalid JSON'); assert(threw, 'readJsonSync did not throw on invalid JSON');
assert(errorMessage.includes(p), 'readJsonSync error did not include file path');
}); });
test('readJsonSync strips UTF-8 BOM', () => { test('readJsonSync strips UTF-8 BOM', () => {

View File

@ -166,5 +166,10 @@ module.exports.move = async function move(src, dest) {
*/ */
module.exports.readJsonSync = function readJsonSync(filePath) { module.exports.readJsonSync = function readJsonSync(filePath) {
const raw = fs.readFileSync(filePath, 'utf8').replace(/^\uFEFF/, ''); const raw = fs.readFileSync(filePath, 'utf8').replace(/^\uFEFF/, '');
try {
return JSON.parse(raw); return JSON.parse(raw);
} catch (error) {
error.message = `Failed to parse JSON in ${filePath}: ${error.message}`;
throw error;
}
}; };