Skip to content

fix(config): write valid JSON when auto-adding $schema to an empty config#36380

Open
YayoRazo wants to merge 1 commit into
anomalyco:devfrom
YayoRazo:fix-config-schema-trailing-comma
Open

fix(config): write valid JSON when auto-adding $schema to an empty config#36380
YayoRazo wants to merge 1 commit into
anomalyco:devfrom
YayoRazo:fix-config-schema-trailing-comma

Conversation

@YayoRazo

Copy link
Copy Markdown

Issue for this PR

Closes #36374

Type of change

  • Bug fix

What does this PR do?

When a loaded config lacks $schema, opencode auto-adds it and writes the file back. The injection used a regex that prepends "$schema": "...", after the opening brace, so for a config whose whole content is {} the result had a trailing comma ({ "$schema": "...", }) — invalid strict JSON. opencode re-reads it tolerantly via jsonc, but the file written to the user's opencode.json then fails any strict JSON.parse (IDE JSON language server, jq).

Fix: use the file's existing patchJsonc helper (jsonc-parser modify/applyEdits) instead of the regex, so the insertion is JSON-aware and always valid. Non-empty configs are unaffected.

How did you verify your code works?

Added a regression test (writes {}, loads config, asserts the written-back file is valid strict JSON and has $schema). It fails on the current code (JSON Parse error: Property name must be a string literal) and passes with the fix. The full config test file shows no new failures; lint and typecheck are clean.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…nfig

Injecting $schema via a regex that prepends `"$schema": "...",` after the
opening brace left a trailing comma when the config had no other keys (e.g.
`{}`), producing invalid strict JSON. opencode re-reads it tolerantly, but the
file written back to the user's opencode.json broke strict JSON.parse consumers
(IDE JSON language server, jq).

Use the existing patchJsonc helper (jsonc-parser modify/applyEdits) instead of
the regex, so the insertion is JSON-aware and always valid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Config auto-adding $schema writes invalid JSON (trailing comma) for an empty {} config

1 participant