fix(config): fail closed when config content yields no JSON value

Treating every undefined parse as empty config let a file that is not JSON
at all (YAML, plain text) read as {}, so a later write would back it up and
replace it - the same data loss this fix is meant to prevent. Only a
comment-only parse, where ValueExpected is the sole error, counts as empty.
This commit is contained in:
Serhii Dziupin
2026-08-17 16:44:17 +03:00
parent 07f8264e72
commit 6d6ece6856
6 changed files with 49 additions and 16 deletions
@@ -343,6 +343,16 @@ describe('readConfigFile / writeConfig JSONC safety (issue #2923)', () => {
expect(readConfigFile(file)).toEqual({});
});
it('throws INVALID_JSONC for content that yields no JSON value at all', () => {
const yamlish = writeFixture('yamlish.jsonc', 'mcp:\n openproject:\n type: remote\n');
expect(() => readConfigFile(yamlish)).toThrow(/cannot be loaded safely/);
expect(() => writeConfig({ $schema: 'https://opencode.ai/config.json' }, yamlish)).toThrow(
/cannot be loaded safely/,
);
expect(fs.readFileSync(yamlish, 'utf8')).toBe('mcp:\n openproject:\n type: remote\n');
expect(fs.existsSync(`${yamlish}.openchamber.backup`)).toBe(false);
});
it('keeps a valid custom layer readable when a project layer is unparseable', () => {
const custom = writeFixture('custom.jsonc', VALID_CONFIG);
const projectDir = path.join(FIXTURE_DIR, 'project');