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
7 changes: 6 additions & 1 deletion packages/opencode/src/config/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function dir(input: ParseSource) {
export async function substitute(input: SubstituteInput) {
const missing = input.missing ?? "error"
let text = input.text.replace(/\{env:([^}]+)\}/g, (_, varName) => {
return (input.env?.[varName] ?? process.env[varName]) || ""
return normalizePathEnvValue((input.env?.[varName] ?? process.env[varName]) || "")
})

const fileMatches = Array.from(text.matchAll(/\{file:[^}]+\}/g))
Expand Down Expand Up @@ -89,3 +89,8 @@ export async function substitute(input: SubstituteInput) {
out += text.slice(cursor)
return out
}

function normalizePathEnvValue(value: string) {
if (/^[A-Za-z]:\\/.test(value) || value.startsWith("\\\\")) return value.replaceAll("\\", "/")
return value
}
26 changes: 26 additions & 0 deletions packages/opencode/test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,32 @@ it.instance("handles environment variable substitution", () =>
),
)

it.instance("normalizes Windows path env variables before parsing permission keys", () =>
withProcessEnv(
"TEST_PERMISSION_DIR",
String.raw`C:\Users\me\AppData\Roaming\MyApp\config`,
Effect.gen(function* () {
const test = yield* TestInstance
yield* FSUtil.use.writeWithDirs(
path.join(test.directory, "opencode.json"),
`{
"$schema": "https://opencode.ai/config.json",
"permission": {
"external_directory": {
"{env:TEST_PERMISSION_DIR}/**": "allow"
}
}
}`,
)

const config = yield* Config.use.get()
expect(config.permission?.external_directory).toEqual({
"C:/Users/me/AppData/Roaming/MyApp/config/**": "allow",
})
}),
),
)

it.instance("preserves env variables when adding $schema to config", () =>
withProcessEnv(
"PRESERVE_VAR",
Expand Down
Loading