diff --git a/packages/opencode/src/config/config.ts b/packages/opencode/src/config/config.ts index eaac1dd4f30..ac50a5e721f 100644 --- a/packages/opencode/src/config/config.ts +++ b/packages/opencode/src/config/config.ts @@ -775,7 +775,19 @@ export namespace Config { } export async function update(config: Info) { - const filepath = path.join(Instance.directory, "config.json") + let filepath = path.join(Instance.directory, "opencode.json") + for (const file of [ + "opencode.json", + "opencode.jsonc", + path.join(".opencode", "opencode.json"), + path.join(".opencode", "opencode.jsonc"), + ]) { + const fullPath = path.join(Instance.directory, file) + if (await Bun.file(fullPath).exists()) { + filepath = fullPath + break + } + } const existing = await loadFile(filepath) await Bun.write(filepath, JSON.stringify(mergeDeep(existing, config), null, 2)) await Instance.dispose() diff --git a/packages/opencode/src/project/state.ts b/packages/opencode/src/project/state.ts index 5846bf85686..a9dc8adcd2b 100644 --- a/packages/opencode/src/project/state.ts +++ b/packages/opencode/src/project/state.ts @@ -57,7 +57,7 @@ export namespace State { tasks.push(task) } - entries.delete(key) + recordsByKey.delete(key) await Promise.all(tasks) disposalFinished = true log.info("state disposal completed", { key }) diff --git a/packages/opencode/test/config/config.test.ts b/packages/opencode/test/config/config.test.ts index 967972842f5..5f406b1ff57 100644 --- a/packages/opencode/test/config/config.test.ts +++ b/packages/opencode/test/config/config.test.ts @@ -1,4 +1,4 @@ -import { test, expect } from "bun:test" +import { test, expect, describe } from "bun:test" import { Config } from "../../src/config/config" import { Instance } from "../../src/project/instance" import { tmpdir } from "../fixture/fixture" @@ -327,17 +327,37 @@ Test agent prompt`, }) }) -test("updates config and writes to file", async () => { - await using tmp = await tmpdir() - await Instance.provide({ - directory: tmp.path, - fn: async () => { - const newConfig = { model: "updated/model" } - await Config.update(newConfig as any) +describe("updates config and writes to file", async () => { + test("should correctly pick existing file to overwrite", async () => { + await using tmp = await tmpdir() + await Instance.provide({ + directory: tmp.path, + fn: async () => { + const files = [path.join(tmp.path, "opencode.jsonc"), path.join(tmp.path, ".opencode", "opencode.json")] + const oldConfig = { $schema: "https://opencode.ai/config.json", model: "old/model" } + await Bun.write(files[0], JSON.stringify(oldConfig, null, 2)) + await Bun.write(files[1], JSON.stringify(oldConfig, null, 2)) - const writtenConfig = JSON.parse(await Bun.file(path.join(tmp.path, "config.json")).text()) - expect(writtenConfig.model).toBe("updated/model") - }, + const newConfig = { $schema: "https://opencode.ai/config.json", model: "updated/model" } + await Config.update(newConfig as any) + + const writtenConfig = JSON.parse(await Bun.file(files[0]).text()) + expect(writtenConfig.model).toBe("updated/model") + }, + }) + }) + test("should create new config file if missing", async () => { + await using tmp = await tmpdir() + await Instance.provide({ + directory: tmp.path, + fn: async () => { + const newConfig = { model: "updated/model" } + await Config.update(newConfig as any) + + const writtenConfig = JSON.parse(await Bun.file(path.join(tmp.path, "opencode.json")).text()) + expect(writtenConfig.model).toBe("updated/model") + }, + }) }) })