diff --git a/test/test-fs-wrapper.js b/test/test-fs-wrapper.js index ed02d5756..0102829d2 100644 --- a/test/test-fs-wrapper.js +++ b/test/test-fs-wrapper.js @@ -405,12 +405,15 @@ async function runTests() { const p = path.join(TMP, 'bad.json'); nativeFs.writeFileSync(p, '{ invalid json }'); let threw = false; + let errorMessage = ''; try { fs.readJsonSync(p); - } catch { + } catch (error) { threw = true; + errorMessage = error.message; } 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', () => { diff --git a/tools/cli/lib/fs.js b/tools/cli/lib/fs.js index 4e6dcfa70..6cd2c631d 100644 --- a/tools/cli/lib/fs.js +++ b/tools/cli/lib/fs.js @@ -166,5 +166,10 @@ module.exports.move = async function move(src, dest) { */ module.exports.readJsonSync = function readJsonSync(filePath) { const raw = fs.readFileSync(filePath, 'utf8').replace(/^\uFEFF/, ''); - return JSON.parse(raw); + try { + return JSON.parse(raw); + } catch (error) { + error.message = `Failed to parse JSON in ${filePath}: ${error.message}`; + throw error; + } };