fix(init): resolve config parsing crash and preserve jsonc comments#48
Merged
slkiser merged 1 commit intoslkiser:mainfrom Apr 18, 2026
Merged
Conversation
Replaces the fragile custom JSONC parser with an AST-based approach (`comment-json`) to fix a fatal crash ("Failed to parse opencode.json") when the configuration file contains inline comments or trailing commas.
Additionally, this ensures existing user comments and formatting are not destroyed when the `init` command appends new properties:
- Adds the `comment-json` dependency and updates `jsonc.ts` to attach/preserve comment metadata during parsing and stringification.
- Removes `structuredClone` from config loading in `init-installer.ts`, which was destructively stripping the hidden comment symbols before they could be saved.
- Updates the `readJson` test helper in `lib.init-installer.test.ts` to use the new parser, resolving a test failure caused by the successfully preserved comments.
Owner
|
@capyBearista Thanks for contributing! |
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.
Summary
This PR addresses a fatal crash (
Failed to parse opencode.json) that occurs during theinitcommand if the user's config file contains inline comments or complex trailing commas. The previous custom JSONC parsing logic relied on regex and backslash-counting that failed on certain edge cases (like Windows paths or specifically formatted strings).Additionally, this PR ensures that existing user comments and formatting are not permanently destroyed when
initappends new properties to the config.comment-jsonto safely attach and preserve comment metadata during parsing and stringification.structuredClonefrom the config loader ininit-installer.ts, which was stripping the hidden comment symbols before they could be written back to disk.readJsontest helper inlib.init-installer.test.tsto use the new parser, resolving a test failure caused by the successfully preserved comments.Linked Issue
No existing issue. Rationale: The
initcommand was completely blocked for users with manually annotated/commentedopencode.jsonfiles, requiring an immediate parsing robustness fix to allow the installer to safely read and mutate the configuration.OpenCode Validation
Quality Checklist
npm run typechecknpm testnpm run buildREADME.mdandCONTRIBUTING.mdwhen applicable)