Skip to content

fix(init): resolve config parsing crash and preserve jsonc comments#48

Merged
slkiser merged 1 commit intoslkiser:mainfrom
capyBearista:fix/init-config-parsing
Apr 18, 2026
Merged

fix(init): resolve config parsing crash and preserve jsonc comments#48
slkiser merged 1 commit intoslkiser:mainfrom
capyBearista:fix/init-config-parsing

Conversation

@capyBearista
Copy link
Copy Markdown
Contributor

@capyBearista capyBearista commented Apr 18, 2026

Summary

This PR addresses a fatal crash (Failed to parse opencode.json) that occurs during the init command 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 init appends new properties to the config.

  • Replaced the custom JSONC parser with an AST-based approach using comment-json to safely attach and preserve comment metadata during parsing and stringification.
  • Removed structuredClone from the config loader in init-installer.ts, which was stripping the hidden comment symbols before they could be written back to disk.
  • Updated 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.

Linked Issue

No existing issue. Rationale: The init command was completely blocked for users with manually annotated/commented opencode.json files, requiring an immediate parsing robustness fix to allow the installer to safely read and mutate the configuration.

OpenCode Validation

  • Current production released OpenCode version tested: 1.4.11
  • Why this version is relevant to the fix: This is the current stable release of OpenCode.

Quality Checklist

  • I ran npm run typecheck
  • I ran npm test
  • I ran npm run build
  • This is the smallest safe root-cause fix (no unnecessary hook/output mutation logic)
  • I preserved behavioral invariants and updated/added boundary tests as needed
  • I updated docs for user-facing workflow/command/config changes (README.md and CONTRIBUTING.md when applicable)

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.
@slkiser slkiser merged commit 9b6af80 into slkiser:main Apr 18, 2026
3 checks passed
@slkiser
Copy link
Copy Markdown
Owner

slkiser commented Apr 18, 2026

@capyBearista Thanks for contributing!

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.

2 participants