Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/project/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
42 changes: 31 additions & 11 deletions packages/opencode/test/config/config.test.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
},
})
})
})

Expand Down