Skip to content

Context7 Platform -- Up-to-date docs for AI agents

fix(cli): accept trailing commas in OpenCode JSONC (#3305)

lorenzozane9b6f702f4089
diff --git a/.changeset/opencode-jsonc-trailing-commas.md b/.changeset/opencode-jsonc-trailing-commas.md
new file mode 100644
index 00000000..8d524fff
--- /dev/null
+++ b/.changeset/opencode-jsonc-trailing-commas.md
@@ -0,0 +1,5 @@
+---
+"ctx7": patch
+---
+
+Accept trailing commas in OpenCode JSONC config files during setup and removal.
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 57bd2e6d..47de9c2b 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -27,6 +27,7 @@
"boxen": "^8.0.1",
"commander": "^15.0.0",
"figlet": "^1.9.4",
+ "jsonc-parser": "^3.3.1",
"open": "^10.1.0",
"ora": "^9.4.0",
"picocolors": "^1.1.1"
diff --git a/packages/cli/src/__tests__/setup.test.ts b/packages/cli/src/__tests__/setup.test.ts
index a5641981..88f10cc1 100644
--- a/packages/cli/src/__tests__/setup.test.ts
+++ b/packages/cli/src/__tests__/setup.test.ts
@@ -284,6 +284,27 @@ describe("JSONC support", () => {
expect(result.mcp).toEqual({});
});
+ test("readJsonConfig accepts trailing commas in OpenCode config", async () => {
+ const path = join(tempDir, "opencode.jsonc");
+ await writeFile(
+ path,
+ '{\n "$schema": "https://opencode.ai/config.json",\n "lsp": true,\n}',
+ "utf-8"
+ );
+ const result = await readJsonConfig(path);
+ expect(result.lsp).toBe(true);
+ });
+
+ test("readJsonConfig rejects malformed JSONC", async () => {
+ const path = join(tempDir, "invalid.jsonc");
+ await writeFile(path, '{ "mcp": {', "utf-8");
+ await expect(readJsonConfig(path)).rejects.toThrow(SyntaxError);
+ });
+
+ test("readJsonConfig propagates non-missing file errors", async () => {
+ await expect(readJsonConfig(tempDir)).rejects.toThrow();
+ });
+
test("readJsonConfig handles block comments", async () => {
const path = join(tempDir, "config.jsonc");
await writeFile(path, '{ /* block */ "key": "value" }', "utf-8");
diff --git a/packages/cli/src/setup/mcp-writer.ts b/packages/cli/src/setup/mcp-writer.ts
index 129b9c85..f17a2ff1 100644
--- a/packages/cli/src/setup/mcp-writer.ts
+++ b/packages/cli/src/setup/mcp-writer.ts
@@ -1,46 +1,31 @@
import { access, readFile, writeFile, mkdir } from "fs/promises";
import { dirname } from "path";
+import { parse, printParseErrorCode, type ParseError } from "jsonc-parser";
import { STDIO_PACKAGE } from "./agents.js";
export { patchTomlStdioApiKey } from "./toml-editor.js";
-function stripJsonComments(text: string): string {
- let result = "";
- let i = 0;
- while (i < text.length) {
- if (text[i] === '"') {
- const start = i++;
- while (i < text.length && text[i] !== '"') {
- if (text[i] === "\\") i++;
- i++;
- }
- result += text.slice(start, ++i);
- } else if (text[i] === "/" && text[i + 1] === "/") {
- i += 2;
- while (i < text.length && text[i] !== "\n") i++;
- } else if (text[i] === "/" && text[i + 1] === "*") {
- i += 2;
- while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++;
- i += 2;
- } else {
- result += text[i++];
- }
- }
- return result;
-}
-
export async function readJsonConfig(filePath: string): Promise<Record<string, unknown>> {
let raw: string;
try {
raw = await readFile(filePath, "utf-8");
- } catch {
- return {};
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
+ throw error;
}
raw = raw.trim();
if (!raw) return {};
- return JSON.parse(stripJsonComments(raw)) as Record<string, unknown>;
+ const errors: ParseError[] = [];
+ const config = parse(raw, errors, { allowTrailingComma: true });
+ if (errors.length > 0) {
+ const firstError = errors[0];
+ throw new SyntaxError(
+ `Invalid JSON config at offset ${firstError.offset}: ${printParseErrorCode(firstError.error)}`
+ );
+ }
+ return config as Record<string, unknown>;
}
export function mergeServerEntry(
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 0f47f504..41bc4a52 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -63,6 +63,9 @@ importers:
figlet:
specifier: ^1.9.4
version: 1.9.4
+ jsonc-parser:
+ specifier: ^3.3.1
+ version: 3.3.1
open:
specifier: ^10.1.0
version: 10.2.0
@@ -3003,6 +3006,9 @@ packages:
resolution: {integrity: sha512-5Z5RFW63yxReJ7vANgW6eZFGWaQvnPE3WNmZoOJrSkGju2etKA2L5rrOa1sm877TVTFt57A80BH1bArcmlLfPw==}
+ resolution: {integrity: sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==}
+
resolution: {integrity: sha512-m6F1R3z8jjlf2imQHS2Qez5sjKWQzbuuhuJ/FKYFRZvPE3PuHcSMVZzfsLhGVOkfd20obL5SWEBew5ShlquNxg==}
@@ -7084,6 +7090,8 @@ snapshots:
+
optionalDependencies:
graceful-fs: 4.2.11