fix(config): write valid JSON when auto-adding $schema to an empty config#36380
Open
YayoRazo wants to merge 1 commit into
Open
fix(config): write valid JSON when auto-adding $schema to an empty config#36380YayoRazo wants to merge 1 commit into
YayoRazo wants to merge 1 commit into
Conversation
…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.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #36374
Type of change
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'sopencode.jsonthen fails any strictJSON.parse(IDE JSON language server,jq).Fix: use the file's existing
patchJsonchelper (jsonc-parsermodify/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