Skip to content

Commit 7e9050e

Browse files
authored
feat: jsonc configuration file support (anomalyco#1434)
1 parent 3c49a9b commit 7e9050e

5 files changed

Lines changed: 31 additions & 8 deletions

File tree

‎bun.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/opencode/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"hono": "catalog:",
4646
"hono-openapi": "0.4.8",
4747
"isomorphic-git": "1.32.1",
48+
"jsonc-parser": "3.3.1",
4849
"open": "10.1.2",
4950
"remeda": "catalog:",
5051
"turndown": "7.2.0",

‎packages/opencode/src/cli/error.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { UI } from "./ui"
55
export function FormatError(input: unknown) {
66
if (MCP.Failed.isInstance(input))
77
return `MCP server "${input.data.name}" failed. Note, opencode does not support MCP authentication yet.`
8-
if (Config.JsonError.isInstance(input)) return `Config file at ${input.data.path} is not valid JSON`
8+
if (Config.JsonError.isInstance(input)) {
9+
return (
10+
`Config file at ${input.data.path} is not valid JSON(C)` + (input.data.message ? `: ${input.data.message}` : "")
11+
)
12+
}
913
if (Config.InvalidError.isInstance(input))
1014
return [
1115
`Config file at ${input.data.path} is invalid`,

‎packages/opencode/src/config/config.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { NamedError } from "../util/error"
1212
import matter from "gray-matter"
1313
import { Flag } from "../flag/flag"
1414
import { Auth } from "../auth"
15+
import { type ParseError as JsoncParseError, parse as parseJsonc, printParseErrorCode } from "jsonc-parser"
1516

1617
export namespace Config {
1718
const log = Log.create({ service: "config" })
@@ -371,11 +372,20 @@ export namespace Config {
371372
}
372373
}
373374

374-
let data: any
375-
try {
376-
data = JSON.parse(text)
377-
} catch (err) {
378-
throw new JsonError({ path: configPath }, { cause: err as Error })
375+
const errors: JsoncParseError[] = []
376+
const data = parseJsonc(text, errors, { allowTrailingComma: true })
377+
if (errors.length) {
378+
throw new JsonError({
379+
path: configPath,
380+
message: errors
381+
.map((e) => {
382+
const lines = text.substring(0, e.offset).split("\n")
383+
const line = lines.length
384+
const column = lines[lines.length - 1].length + 1
385+
return `${printParseErrorCode(e.error)} at line ${line}, column ${column}`
386+
})
387+
.join("; "),
388+
})
379389
}
380390

381391
const parsed = Info.safeParse(data)
@@ -392,6 +402,7 @@ export namespace Config {
392402
"ConfigJsonError",
393403
z.object({
394404
path: z.string(),
405+
message: z.string().optional(),
395406
}),
396407
)
397408

‎packages/web/src/content/docs/docs/config.mdx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ description: Using the opencode JSON config.
55

66
You can configure opencode using a JSON config file.
77

8-
```json title="opencode.json"
8+
## Format
9+
10+
opencode supports both JSON and JSONC (JSON with Comments) formats. You can use comments in your configuration files:
11+
12+
```jsonc title="opencode.jsonc"
913
{
1014
"$schema": "https://opencode.ai/config.json",
15+
// Theme configuration
1116
"theme": "opencode",
1217
"model": "anthropic/claude-sonnet-4-20250514",
1318
"autoupdate": true
@@ -199,7 +204,7 @@ about rules here](/docs/rules).
199204

200205
You can configure specialized agents for specific tasks through the `agent` option.
201206

202-
```json title="opencode.json"
207+
```jsonc title="opencode.jsonc"
203208
{
204209
"$schema": "https://opencode.ai/config.json",
205210
"agent": {
@@ -208,6 +213,7 @@ You can configure specialized agents for specific tasks through the `agent` opti
208213
"model": "anthropic/claude-sonnet-4-20250514",
209214
"prompt": "You are a code reviewer. Focus on security, performance, and maintainability.",
210215
"tools": {
216+
// Disable file modification tools for review-only agent
211217
"write": false,
212218
"edit": false
213219
}

0 commit comments

Comments
 (0)