From 5e70133a718a3173c16b273d13919c05cc935c36 Mon Sep 17 00:00:00 2001 From: phuc Date: Sat, 20 Jun 2026 11:47:02 +0700 Subject: [PATCH] feat(opencode): autoload .env from global config directory for {env:} substitution --- packages/opencode/src/config/config.ts | 2 ++ packages/opencode/test/config/config.test.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/packages/opencode/src/config/config.ts b/packages/opencode/src/config/config.ts index 7f568f492073..becf46396e03 100644 --- a/packages/opencode/src/config/config.ts +++ b/packages/opencode/src/config/config.ts @@ -244,6 +244,8 @@ export const layer = Layer.effect( }) const loadGlobal = Effect.fnUntraced(function* (env?: Record) { + const envPath = path.join(Global.Path.config, ".env") + if (existsSync(envPath)) process.loadEnvFile(envPath) let result: Info = {} // Seed the default global config with the schema for editor completion, but avoid writing when the user // explicitly routes config through env-provided paths or content. diff --git a/packages/opencode/test/config/config.test.ts b/packages/opencode/test/config/config.test.ts index 02ace5366880..f56f068597a2 100644 --- a/packages/opencode/test/config/config.test.ts +++ b/packages/opencode/test/config/config.test.ts @@ -515,6 +515,21 @@ it.instance("handles environment variable substitution", () => ), ) +it.instance("loads .env from global config directory for {env:} substitution", () => + Effect.gen(function* () { + const dir = yield* tmpdirScoped() + yield* FSUtil.use.writeWithDirs(path.join(dir, ".env"), "MY_DOTENV_VAR=from-global-dotenv\n") + yield* writeConfigEffect(dir, { + $schema: "https://opencode.ai/config.json", + username: "{env:MY_DOTENV_VAR}", + }) + yield* withGlobalConfigDir(dir, Effect.gen(function* () { + const config = yield* Config.use.get() + expect(config.username).toBe("from-global-dotenv") + })) + }).pipe(Effect.ensuring(Effect.sync(() => delete process.env.MY_DOTENV_VAR))), +) + it.instance("preserves env variables when adding $schema to config", () => withProcessEnv( "PRESERVE_VAR",