Skip to content

Commit 7520e61

Browse files
committed
fix(core): respect OPENCODE_CONFIG and OPENCODE_CONFIG_CONTENT in serve/web commands
opencode serve and opencode web only loaded the global config, completely ignoring the OPENCODE_CONFIG and OPENCODE_CONFIG_CONTENT environment variables. This adds getGlobalWithOverrides() to the Config service which merges the global config with those two env-var sources (in the same order as the full instance config chain), and uses it in resolveNetworkOptions() so that serve and web honor user-level config overrides. Closes anomalyco#19078
1 parent 8b1f0e2 commit 7520e61

3 files changed

Lines changed: 135 additions & 1 deletion

File tree

‎packages/opencode/src/cli/network.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function withNetworkOptions<T>(yargs: Argv<T>) {
3737
return yargs.options(options)
3838
}
3939
export async function resolveNetworkOptions(args: NetworkOptions) {
40-
const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.getGlobal()))
40+
const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.getGlobalWithOverrides()))
4141
return resolveNetworkOptionsNoConfig(args, config)
4242
}
4343

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ type State = {
283283
export interface Interface {
284284
readonly get: () => Effect.Effect<Info>
285285
readonly getGlobal: () => Effect.Effect<Info>
286+
readonly getGlobalWithOverrides: () => Effect.Effect<Info>
286287
readonly getConsoleState: () => Effect.Effect<ConsoleState>
287288
readonly update: (config: Info) => Effect.Effect<void>
288289
readonly updateGlobal: (config: Info) => Effect.Effect<Info>
@@ -485,6 +486,40 @@ export const layer = Layer.effect(
485486
return yield* cachedGlobal
486487
})
487488

489+
const [cachedGlobalWithOverrides, invalidateGlobalWithOverrides] = yield* Effect.cachedInvalidateWithTTL(
490+
Effect.gen(function* () {
491+
let result = yield* cachedGlobal
492+
493+
const configPath = process.env["OPENCODE_CONFIG"]
494+
if (configPath) {
495+
result = mergeConfigConcatArrays(result, yield* loadFile(configPath))
496+
log.debug("loaded custom config", { path: configPath })
497+
}
498+
499+
const configContent = process.env["OPENCODE_CONFIG_CONTENT"]
500+
if (configContent) {
501+
const next = yield* loadConfig(configContent, {
502+
dir: process.cwd(),
503+
source: "OPENCODE_CONFIG_CONTENT",
504+
})
505+
result = mergeConfigConcatArrays(result, next)
506+
log.debug("loaded custom config from OPENCODE_CONFIG_CONTENT")
507+
}
508+
509+
return result
510+
}).pipe(
511+
Effect.tapError((error) =>
512+
Effect.sync(() => log.error("failed to load global config with overrides, using defaults", { error: String(error) })),
513+
),
514+
Effect.orElseSucceed((): Info => ({})),
515+
),
516+
Duration.infinity,
517+
)
518+
519+
const getGlobalWithOverrides = Effect.fn("Config.getGlobalWithOverrides")(function* () {
520+
return yield* cachedGlobalWithOverrides
521+
})
522+
488523
const ensureGitignore = Effect.fn("Config.ensureGitignore")(function* (dir: string) {
489524
const gitignore = path.join(dir, ".gitignore")
490525
const hasIgnore = yield* fs.existsSafe(gitignore)
@@ -781,6 +816,7 @@ export const layer = Layer.effect(
781816

782817
const invalidate = Effect.fn("Config.invalidate")(function* (wait?: boolean) {
783818
yield* invalidateGlobal
819+
yield* invalidateGlobalWithOverrides
784820
const task = Instance.disposeAll()
785821
.catch(() => undefined)
786822
.finally(() =>
@@ -820,6 +856,7 @@ export const layer = Layer.effect(
820856
return Service.of({
821857
get,
822858
getGlobal,
859+
getGlobalWithOverrides,
823860
getConsoleState,
824861
update,
825862
updateGlobal,

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,3 +2288,100 @@ test("parseManagedPlist handles empty config", async () => {
22882288
)
22892289
expect(config.$schema).toBe("https://opencode.ai/config.json")
22902290
})
2291+
2292+
const loadGlobalWithOverrides = () =>
2293+
Effect.runPromise(
2294+
Config.Service.use((svc) => svc.getGlobalWithOverrides()).pipe(Effect.scoped, Effect.provide(layer)),
2295+
)
2296+
2297+
describe("getGlobalWithOverrides", () => {
2298+
const origConfig = process.env["OPENCODE_CONFIG"]
2299+
const origContent = process.env["OPENCODE_CONFIG_CONTENT"]
2300+
2301+
afterEach(async () => {
2302+
if (origConfig === undefined) delete process.env["OPENCODE_CONFIG"]
2303+
else process.env["OPENCODE_CONFIG"] = origConfig
2304+
if (origContent === undefined) delete process.env["OPENCODE_CONFIG_CONTENT"]
2305+
else process.env["OPENCODE_CONFIG_CONTENT"] = origContent
2306+
await clear(true)
2307+
})
2308+
2309+
test("returns global config when no env vars are set", async () => {
2310+
await using globalTmp = await tmpdir()
2311+
const prevConfigPath = Global.Path.config
2312+
;(Global.Path as { config: string }).config = globalTmp.path
2313+
delete process.env["OPENCODE_CONFIG"]
2314+
delete process.env["OPENCODE_CONFIG_CONTENT"]
2315+
try {
2316+
await writeConfig(globalTmp.path, {
2317+
$schema: "https://opencode.ai/config.json",
2318+
server: { port: 4096, hostname: "127.0.0.1" },
2319+
})
2320+
await clear(true)
2321+
const config = await loadGlobalWithOverrides()
2322+
expect(config.server?.port).toBe(4096)
2323+
expect(config.server?.hostname).toBe("127.0.0.1")
2324+
} finally {
2325+
;(Global.Path as { config: string }).config = prevConfigPath
2326+
await clear(true)
2327+
}
2328+
})
2329+
2330+
test("OPENCODE_CONFIG merges on top of global config", async () => {
2331+
await using globalTmp = await tmpdir()
2332+
await using overrideTmp = await tmpdir()
2333+
const prevConfigPath = Global.Path.config
2334+
;(Global.Path as { config: string }).config = globalTmp.path
2335+
try {
2336+
await writeConfig(globalTmp.path, {
2337+
$schema: "https://opencode.ai/config.json",
2338+
server: { port: 4096, hostname: "127.0.0.1" },
2339+
})
2340+
await writeConfig(overrideTmp.path, {
2341+
$schema: "https://opencode.ai/config.json",
2342+
server: { port: 9090 },
2343+
})
2344+
process.env["OPENCODE_CONFIG"] = path.join(overrideTmp.path, "opencode.json")
2345+
await clear(true)
2346+
const config = await loadGlobalWithOverrides()
2347+
// OPENCODE_CONFIG port wins
2348+
expect(config.server?.port).toBe(9090)
2349+
// global hostname is preserved since OPENCODE_CONFIG doesn't define it
2350+
expect(config.server?.hostname).toBe("127.0.0.1")
2351+
} finally {
2352+
;(Global.Path as { config: string }).config = prevConfigPath
2353+
await clear(true)
2354+
}
2355+
})
2356+
2357+
test("OPENCODE_CONFIG_CONTENT merges on top of global and OPENCODE_CONFIG", async () => {
2358+
await using globalTmp = await tmpdir()
2359+
await using overrideTmp = await tmpdir()
2360+
const prevConfigPath = Global.Path.config
2361+
;(Global.Path as { config: string }).config = globalTmp.path
2362+
try {
2363+
await writeConfig(globalTmp.path, {
2364+
$schema: "https://opencode.ai/config.json",
2365+
server: { port: 4096, hostname: "127.0.0.1" },
2366+
})
2367+
await writeConfig(overrideTmp.path, {
2368+
$schema: "https://opencode.ai/config.json",
2369+
server: { port: 9090, hostname: "0.0.0.0" },
2370+
})
2371+
process.env["OPENCODE_CONFIG"] = path.join(overrideTmp.path, "opencode.json")
2372+
process.env["OPENCODE_CONFIG_CONTENT"] = JSON.stringify({
2373+
$schema: "https://opencode.ai/config.json",
2374+
server: { port: 7777 },
2375+
})
2376+
await clear(true)
2377+
const config = await loadGlobalWithOverrides()
2378+
// OPENCODE_CONFIG_CONTENT port wins over both
2379+
expect(config.server?.port).toBe(7777)
2380+
// OPENCODE_CONFIG hostname is preserved since OPENCODE_CONFIG_CONTENT doesn't define it
2381+
expect(config.server?.hostname).toBe("0.0.0.0")
2382+
} finally {
2383+
;(Global.Path as { config: string }).config = prevConfigPath
2384+
await clear(true)
2385+
}
2386+
})
2387+
})

0 commit comments

Comments
 (0)