Skip to content

Commit e26b526

Browse files
authored
Merge pull request DeusData#1575 from DeusData/fix/opencode-jsonc
fix(cli): install into an existing opencode.jsonc instead of a second file (DeusData#1560)
2 parents 35659d5 + 1e6825f commit e26b526

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,6 +2432,20 @@ static void cbm_opencode_config_path(const char *home_dir, char *out, size_t out
24322432
snprintf(out, out_sz, "%s", custom);
24332433
return;
24342434
}
2435+
/* OpenCode reads either opencode.json or opencode.jsonc. We always wrote
2436+
* the .json name, so a user whose real config is .jsonc got a SECOND file
2437+
* that OpenCode ignores — their MCP server silently never appeared, and the
2438+
* install looked like it had succeeded (discussion #1560). Prefer whichever
2439+
* file already exists, .jsonc first since it is the one we used to miss;
2440+
* with neither present, create .json as before. Other clients in this file
2441+
* (pochi, kilo) already carry .jsonc paths — OpenCode was the gap. */
2442+
char candidate[CLI_BUF_1K];
2443+
int written = snprintf(candidate, sizeof(candidate), "%s/.config/opencode/opencode.jsonc",
2444+
home_dir ? home_dir : "");
2445+
if (written > 0 && (size_t)written < sizeof(candidate) && cbm_file_exists(candidate)) {
2446+
snprintf(out, out_sz, "%s", candidate);
2447+
return;
2448+
}
24352449
snprintf(out, out_sz, "%s/.config/opencode/opencode.json", home_dir);
24362450
}
24372451

‎tests/test_cli.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7349,6 +7349,40 @@ TEST(cli_opencode_honors_custom_config) {
73497349
PASS();
73507350
}
73517351

7352+
/* Discussion #1560: OpenCode reads either opencode.json or opencode.jsonc, but
7353+
* we always wrote the .json name. A user whose real config is .jsonc got a
7354+
* SECOND file that OpenCode ignores — the MCP server silently never appeared
7355+
* while the install reported success. Prefer an existing .jsonc; with neither
7356+
* present, .json is still created, so fresh installs are unchanged. */
7357+
TEST(cli_opencode_prefers_existing_jsonc_config_discussion1560) {
7358+
char tmpdir[256];
7359+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-opencode-jsonc-XXXXXX");
7360+
if (!cbm_mkdtemp(tmpdir))
7361+
FAIL("cbm_mkdtemp failed");
7362+
char config_dir[512];
7363+
snprintf(config_dir, sizeof(config_dir), "%s/.config/opencode", tmpdir);
7364+
test_mkdirp(config_dir);
7365+
char jsonc_path[640];
7366+
snprintf(jsonc_path, sizeof(jsonc_path), "%s/opencode.jsonc", config_dir);
7367+
write_test_file(jsonc_path, "{\n // user's hand-written config\n \"theme\": \"dark\"\n}\n");
7368+
7369+
char *saved_path = save_test_env("PATH");
7370+
char *saved_file = save_test_env("OPENCODE_CONFIG");
7371+
cbm_setenv("PATH", tmpdir, 1);
7372+
cbm_unsetenv("OPENCODE_CONFIG");
7373+
7374+
char *json = cbm_build_install_plan_json(tmpdir, "/usr/local/bin/codebase-memory-mcp");
7375+
bool targets_jsonc = json && strstr(json, "/.config/opencode/opencode.jsonc") != NULL;
7376+
7377+
free(json);
7378+
restore_test_env("PATH", saved_path);
7379+
restore_test_env("OPENCODE_CONFIG", saved_file);
7380+
test_rmdir_r(tmpdir);
7381+
if (!targets_jsonc)
7382+
FAIL("an existing opencode.jsonc must be the install target, not a new opencode.json");
7383+
PASS();
7384+
}
7385+
73527386
TEST(cli_opencode_config_dir_detects_without_retargeting_global_json) {
73537387
char tmpdir[256];
73547388
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-opencode-dir-XXXXXX");
@@ -12416,6 +12450,7 @@ SUITE(cli) {
1241612450
RUN_TEST(cli_antigravity_does_not_imply_gemini);
1241712451
RUN_TEST(cli_antigravity_plan_uses_documented_global_files);
1241812452
RUN_TEST(cli_opencode_honors_custom_config);
12453+
RUN_TEST(cli_opencode_prefers_existing_jsonc_config_discussion1560);
1241912454
RUN_TEST(cli_opencode_config_dir_detects_without_retargeting_global_json);
1242012455
RUN_TEST(cli_kiro_and_hermes_homes_are_honored);
1242112456
RUN_TEST(cli_detect_agents_finds_official_kiro_cli_executable);

0 commit comments

Comments
 (0)