Skip to content

fix(config): tolerate trailing commas in micode.json and warn on parse errors#30

Closed
nitoba wants to merge 2 commits intovtemian:mainfrom
nitoba:fix-json-trailing-commas
Closed

fix(config): tolerate trailing commas in micode.json and warn on parse errors#30
nitoba wants to merge 2 commits intovtemian:mainfrom
nitoba:fix-json-trailing-commas

Conversation

@nitoba
Copy link

@nitoba nitoba commented Mar 9, 2026

Summary

Add stripTrailingCommas helper with a string-safe regex that skips quoted values, preventing corruption of strings like "hello, }"
Apply trailing-comma tolerance to all three JSON.parse call sites (opencode.json, micode.json, model context limits)
Distinguish file-not-found (silent) from actual parse errors in loadMicodeConfig — logs a clear warning via log.warn so users know their config was ignored

Problem

Hand-edited JSON configs commonly include trailing commas (e.g. "compactionThreshold": 0.8,). JSON.parse rejects this and throws. The catch block was silently returning null, causing all user overrides to be lost with zero feedback — users had no way to know their micode.json was being ignored.
Approach

Instead of adding a JSONC parser dependency, a lightweight regex strips trailing commas before parsing. The regex uses alternation ("..." | ,\s*[]}]) to match quoted strings first and return them unchanged, so values containing , } or , ] are never corrupted.

For truly invalid JSON (beyond trailing commas), the error is now surfaced via log.warn with the parse error message, while file-not-found remains silent as expected.
Test plan

Trailing commas in micode.json are tolerated and parsed correctly
String values containing , } and , ] are NOT corrupted by the sanitizer
Invalid JSON (beyond trailing commas) returns null and logs a warning
File-not-found remains silent (no warning spam)
All 90 config + agent tests pass

Summary by cubic

Tolerates trailing commas in JSON configs and warns on read/parse errors so user overrides aren’t silently ignored. Applies to micode.json, opencode.json, and model context limits.

  • Bug Fixes
    • Strip trailing commas before parsing using a string-safe regex that leaves quoted text unchanged.
    • Apply parsing tolerance across all JSON reads: opencode.json, micode.json, and model context limits.
    • In loadMicodeConfig, keep file-not-found silent; warn on read failures (“Failed to read micode.json”) and on parse errors (“Failed to parse micode.json”).

Written for commit ef676ba. Summary will update on new commits.

…e errors

Hand-edited JSON configs commonly include trailing commas, which
causes JSON.parse to throw. The catch block silently returns null,
so all user overrides are lost with zero feedback.

- Add stripTrailingCommas helper using a string-safe regex that
  skips quoted values (prevents corrupting strings like "hello, }")
- Apply to all three JSON.parse sites (opencode.json, micode.json,
  model context limits)
- Distinguish file-not-found (silent) from parse errors in
  loadMicodeConfig — log a clear warning via log.warn so users
  know their config was ignored

https://claude.ai/code/session_019WCTQeKgwFg1RG2rrBDjas
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/config-loader.ts">

<violation number="1" location="src/config-loader.ts:184">
P2: The warning message in loadMicodeConfig conflates read errors with parse errors; any non-ENOENT filesystem error is logged as “Failed to parse micode.json,” which is misleading and can send users to fix JSON when the issue is permissions/path/I/O.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Split the single try/catch in loadMicodeConfig into two separate
try/catch blocks: one for the readFile call and one for JSON.parse
and validation. Filesystem errors (permissions, I/O, etc.) now log
"Failed to read micode.json" while JSON syntax errors continue to
log "Failed to parse micode.json", so users get accurate guidance
on what actually went wrong.
@nitoba
Copy link
Author

nitoba commented Mar 9, 2026

No fix needed. The error handling is already correctly separate @cubic-dev-ai

@vtemian
Copy link
Owner

vtemian commented Mar 10, 2026

Thanks for the contribution @nitoba! Good catch on the trailing commas issue.

This is superseded by #31 which added full JSONC support via jsonc-parser (the same library OpenCode uses). This covers trailing commas, line/block comments, and .jsonc file discovery — no regex needed.

@vtemian vtemian closed this Mar 10, 2026
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.

3 participants