Skip to content

Commit 74830e4

Browse files
ariane-emorythdxractions-useropencode-agent[bot]rekram1-node
authored
fix: merge plugin selections (resolves #4565) (#4724)
Co-authored-by: Dax Raad <[email protected]> Co-authored-by: GitHub Action <[email protected]> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: rekram1-node <[email protected]>
1 parent 603dbbb commit 74830e4

2 files changed

Lines changed: 114 additions & 5 deletions

File tree

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,44 @@ import { ConfigMarkdown } from "./markdown"
2121
export namespace Config {
2222
const log = Log.create({ service: "config" })
2323

24+
// Custom merge function that concatenates plugin arrays instead of replacing them
25+
function mergeConfigWithPlugins(target: Info, source: Info): Info {
26+
const merged = mergeDeep(target, source)
27+
// If both configs have plugin arrays, concatenate them instead of replacing
28+
if (target.plugin && source.plugin) {
29+
const pluginSet = new Set([...target.plugin, ...source.plugin])
30+
merged.plugin = Array.from(pluginSet)
31+
}
32+
return merged
33+
}
34+
2435
export const state = Instance.state(async () => {
2536
const auth = await Auth.all()
2637
let result = await global()
2738

2839
// Override with custom config if provided
2940
if (Flag.OPENCODE_CONFIG) {
30-
result = mergeDeep(result, await loadFile(Flag.OPENCODE_CONFIG))
41+
result = mergeConfigWithPlugins(result, await loadFile(Flag.OPENCODE_CONFIG))
3142
log.debug("loaded custom config", { path: Flag.OPENCODE_CONFIG })
3243
}
3344

3445
for (const file of ["opencode.jsonc", "opencode.json"]) {
3546
const found = await Filesystem.findUp(file, Instance.directory, Instance.worktree)
3647
for (const resolved of found.toReversed()) {
37-
result = mergeDeep(result, await loadFile(resolved))
48+
result = mergeConfigWithPlugins(result, await loadFile(resolved))
3849
}
3950
}
4051

4152
if (Flag.OPENCODE_CONFIG_CONTENT) {
42-
result = mergeDeep(result, JSON.parse(Flag.OPENCODE_CONFIG_CONTENT))
53+
result = mergeConfigWithPlugins(result, JSON.parse(Flag.OPENCODE_CONFIG_CONTENT))
4354
log.debug("loaded custom config from OPENCODE_CONFIG_CONTENT")
4455
}
4556

4657
for (const [key, value] of Object.entries(auth)) {
4758
if (value.type === "wellknown") {
4859
process.env[value.key] = value.token
4960
const wellknown = (await fetch(`${key}/.well-known/opencode`).then((x) => x.json())) as any
50-
result = mergeDeep(result, await load(JSON.stringify(wellknown.config ?? {}), process.cwd()))
61+
result = mergeConfigWithPlugins(result, await load(JSON.stringify(wellknown.config ?? {}), process.cwd()))
5162
}
5263
}
5364

@@ -78,7 +89,7 @@ export namespace Config {
7889
if (dir.endsWith(".opencode") || dir === Flag.OPENCODE_CONFIG_DIR) {
7990
for (const file of ["opencode.jsonc", "opencode.json"]) {
8091
log.debug(`loading config from ${path.join(dir, file)}`)
81-
result = mergeDeep(result, await loadFile(path.join(dir, file)))
92+
result = mergeConfigWithPlugins(result, await loadFile(path.join(dir, file)))
8293
// to satisy the type checker
8394
result.agent ??= {}
8495
result.mode ??= {}

‎packages/opencode/test/config/config.test.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,101 @@ test("resolves scoped npm plugins in config", async () => {
403403
},
404404
})
405405
})
406+
407+
test("merges plugin arrays from global and local configs", async () => {
408+
await using tmp = await tmpdir({
409+
init: async (dir) => {
410+
// Create a nested project structure with local .opencode config
411+
const projectDir = path.join(dir, "project")
412+
const opencodeDir = path.join(projectDir, ".opencode")
413+
await fs.mkdir(opencodeDir, { recursive: true })
414+
415+
// Global config with plugins
416+
await Bun.write(
417+
path.join(dir, "opencode.json"),
418+
JSON.stringify({
419+
$schema: "https://opencode.ai/config.json",
420+
plugin: ["global-plugin-1", "global-plugin-2"],
421+
}),
422+
)
423+
424+
// Local .opencode config with different plugins
425+
await Bun.write(
426+
path.join(opencodeDir, "opencode.json"),
427+
JSON.stringify({
428+
$schema: "https://opencode.ai/config.json",
429+
plugin: ["local-plugin-1"],
430+
}),
431+
)
432+
},
433+
})
434+
435+
await Instance.provide({
436+
directory: path.join(tmp.path, "project"),
437+
fn: async () => {
438+
const config = await Config.get()
439+
const plugins = config.plugin ?? []
440+
441+
// Should contain both global and local plugins
442+
expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
443+
expect(plugins.some((p) => p.includes("global-plugin-2"))).toBe(true)
444+
expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
445+
446+
// Should have all 3 plugins (not replaced, but merged)
447+
const pluginNames = plugins.filter((p) => p.includes("global-plugin") || p.includes("local-plugin"))
448+
expect(pluginNames.length).toBeGreaterThanOrEqual(3)
449+
},
450+
})
451+
})
452+
453+
test("deduplicates duplicate plugins from global and local configs", async () => {
454+
await using tmp = await tmpdir({
455+
init: async (dir) => {
456+
// Create a nested project structure with local .opencode config
457+
const projectDir = path.join(dir, "project")
458+
const opencodeDir = path.join(projectDir, ".opencode")
459+
await fs.mkdir(opencodeDir, { recursive: true })
460+
461+
// Global config with plugins
462+
await Bun.write(
463+
path.join(dir, "opencode.json"),
464+
JSON.stringify({
465+
$schema: "https://opencode.ai/config.json",
466+
plugin: ["duplicate-plugin", "global-plugin-1"],
467+
}),
468+
)
469+
470+
// Local .opencode config with some overlapping plugins
471+
await Bun.write(
472+
path.join(opencodeDir, "opencode.json"),
473+
JSON.stringify({
474+
$schema: "https://opencode.ai/config.json",
475+
plugin: ["duplicate-plugin", "local-plugin-1"],
476+
}),
477+
)
478+
},
479+
})
480+
481+
await Instance.provide({
482+
directory: path.join(tmp.path, "project"),
483+
fn: async () => {
484+
const config = await Config.get()
485+
const plugins = config.plugin ?? []
486+
487+
// Should contain all unique plugins
488+
expect(plugins.some((p) => p.includes("global-plugin-1"))).toBe(true)
489+
expect(plugins.some((p) => p.includes("local-plugin-1"))).toBe(true)
490+
expect(plugins.some((p) => p.includes("duplicate-plugin"))).toBe(true)
491+
492+
// Should deduplicate the duplicate plugin
493+
const duplicatePlugins = plugins.filter((p) => p.includes("duplicate-plugin"))
494+
expect(duplicatePlugins.length).toBe(1)
495+
496+
// Should have exactly 3 unique plugins
497+
const pluginNames = plugins.filter(
498+
(p) => p.includes("global-plugin") || p.includes("local-plugin") || p.includes("duplicate-plugin"),
499+
)
500+
expect(pluginNames.length).toBe(3)
501+
},
502+
})
503+
})

0 commit comments

Comments
 (0)