From f09eee5002b8495bff5629bad7907a36767ab39a Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Fri, 16 Oct 2026 01:12:37 -0500 Subject: [PATCH] fix(core): treat unreadable config file references as empty (#55959) --- packages/core/src/config/variable.ts | 27 +++++++--------- packages/core/test/config/config.test.ts | 4 ++- packages/core/test/location-layer.test.ts | 38 +++-------------------- packages/core/test/session-move.test.ts | 6 +--- 4 files changed, 20 insertions(+), 55 deletions(-) diff --git a/packages/core/src/config/variable.ts b/packages/core/src/config/variable.ts index 7ed7f6fab88..4f96c085b86 100644 --- a/packages/core/src/config/variable.ts +++ b/packages/core/src/config/variable.ts @@ -5,7 +5,6 @@ import path from "path" import { Effect } from "effect" import { type ParseError, parse } from "jsonc-parser" import { FSUtil } from "@opencode/util/fs-util" -import { InvalidError } from "../v1/config/error.js" type ParseSource = | { @@ -61,24 +60,20 @@ const substituteFiles = Effect.fnUntraced(function* (input: SubstituteInput, tex const filePath = token.slice("{file:".length, -1) const expandedPath = filePath.startsWith("~/") ? path.join(os.homedir(), filePath.slice(2)) : filePath const resolvedPath = path.isAbsolute(expandedPath) ? expandedPath : path.resolve(configDir, expandedPath) - const fileContent = yield* fs - .readFileStringSafe(resolvedPath) - .pipe( - Effect.mapError( - (error) => - new InvalidError({ path: configSource, message: `bad file reference: "${token}"` }, { cause: error }), - ), - ) // Like a missing {env:VAR}, a missing or unreadable file substitutes empty so one stale secret // reference cannot take down every Location that loads this config. - if (fileContent === undefined) - yield* Effect.logWarning("config file reference is missing or unreadable", { - source: configSource, - reference: token, - path: resolvedPath, - }) + const fileContent = yield* fs.readFileString(resolvedPath).pipe( + Effect.catch((cause) => + Effect.logWarning("config file reference is missing or unreadable", { + source: configSource, + reference: token, + path: resolvedPath, + cause, + }).pipe(Effect.as("")), + ), + ) - out += JSON.stringify((fileContent ?? "").trim()).slice(1, -1) + out += JSON.stringify(fileContent.trim()).slice(1, -1) cursor = index + token.length } diff --git a/packages/core/test/config/config.test.ts b/packages/core/test/config/config.test.ts index e1c7a8cf460..91f289f3554 100644 --- a/packages/core/test/config/config.test.ts +++ b/packages/core/test/config/config.test.ts @@ -1366,7 +1366,8 @@ describe("Config", () => { "headers": { "Authorization": "Bearer {env:OPENCODE_TEST_MCP_TOKEN}", "X-Token": "{file:token.txt}", - "X-Missing": "Bearer {file:~/.opencode-test-missing/key}" + "X-Missing": "Bearer {file:~/.opencode-test-missing/key}", + "X-Folder": "{file:.}" } } } @@ -1388,6 +1389,7 @@ describe("Config", () => { Authorization: String.raw`Bearer C:\Users\new\"secret"`, "X-Token": 'file\n"token"', "X-Missing": "Bearer ", + "X-Folder": "", }) }).pipe(Effect.provide(testLayer(tmp.path))) }), diff --git a/packages/core/test/location-layer.test.ts b/packages/core/test/location-layer.test.ts index c9401a1f79e..1987338e0f2 100644 --- a/packages/core/test/location-layer.test.ts +++ b/packages/core/test/location-layer.test.ts @@ -5,7 +5,6 @@ import { describe, expect } from "bun:test" import { Config } from "@opencode/schema/config" import { Money } from "@opencode/schema/money" import { - Cause, DateTime, Deferred, Duration, @@ -79,11 +78,10 @@ const itWithActivity = testEffect( ) describe("LocationServiceMap", () => { - for (const failure of ["file", "permissions", "config reference"] as const) { + for (const failure of ["file", "permissions"] as const) { for (const invalidate of [false, true]) { // The file-path fixture boots on Windows rather than failing during - // discovery. The config-reference case covers repair on every OS. - // Windows does not enforce POSIX directory modes, and root bypasses them. + // discovery. Windows does not enforce POSIX directory modes, and root bypasses them. const test = (failure === "file" && process.platform === "win32") || (failure === "permissions" && (process.platform === "win32" || process.getuid?.() === 0)) @@ -102,20 +100,8 @@ describe("LocationServiceMap", () => { yield* Effect.promise(() => fs.mkdir(directory, { mode: 0o000 })) yield* Effect.addFinalizer(() => Effect.promise(() => fs.chmod(directory, 0o755))) } - if (failure === "config reference") { - yield* Effect.promise(() => fs.mkdir(path.join(directory, "username.txt"), { recursive: true })) - yield* Effect.promise(() => - fs.writeFile(path.join(directory, "opencode.json"), JSON.stringify({ username: "{file:username.txt}" })), - ) - } const first = yield* Effect.exit(load) expect(Exit.isFailure(first)).toBe(true) - if (failure === "config reference" && Exit.isFailure(first)) { - expect(Cause.squash(first.cause)).toMatchObject({ - name: "ConfigInvalidError", - data: { message: expect.stringContaining('bad file reference: "{file:username.txt}"') }, - }) - } if (!invalidate) expect(yield* locations.contextEffectOption(ref).pipe(Effect.scoped)).toEqual(Option.none()) if (failure === "file") { @@ -123,10 +109,6 @@ describe("LocationServiceMap", () => { yield* Effect.promise(() => fs.mkdir(directory)) } if (failure === "permissions") yield* Effect.promise(() => fs.chmod(directory, 0o755)) - if (failure === "config reference") { - yield* Effect.promise(() => fs.rm(path.join(directory, "username.txt"), { recursive: true })) - yield* Effect.promise(() => fs.writeFile(path.join(directory, "username.txt"), "test-user")) - } expect((yield* Effect.promise(() => fs.stat(directory))).isDirectory()).toBe(true) if (invalidate) yield* locations.invalidate(ref) const repaired = yield* Effect.exit(load) @@ -137,8 +119,8 @@ describe("LocationServiceMap", () => { } } - for (const failure of ["file", "missing", "config reference"] as const) { - // A file-path Location boots on Windows; use the missing config reference there. + for (const failure of ["file", "missing"] as const) { + // A file-path Location boots on Windows; the missing directory covers it there. const test = failure === "file" && process.platform === "win32" ? it.live.skip : it.live test(`keeps the repaired graph after concurrent ${failure} failures release`, () => Effect.gen(function* () { @@ -147,12 +129,6 @@ describe("LocationServiceMap", () => { const ref = Location.Ref.make({ directory: AbsolutePath.make(directory) }) const locations = yield* LocationServiceMap.Service if (failure === "file") yield* Effect.promise(() => fs.writeFile(directory, "file")) - if (failure === "config reference") { - yield* Effect.promise(() => fs.mkdir(path.join(directory, "username.txt"), { recursive: true })) - yield* Effect.promise(() => - fs.writeFile(path.join(directory, "opencode.json"), JSON.stringify({ username: "{file:username.txt}" })), - ) - } const scopes = yield* Effect.forEach(Array.from({ length: 8 }), () => Effect.acquireRelease(Scope.make(), (scope) => Scope.close(scope, Exit.void)), @@ -164,11 +140,7 @@ describe("LocationServiceMap", () => { ) expect(failures.every(Exit.isFailure)).toBe(true) if (failure === "file") yield* Effect.promise(() => fs.rm(directory)) - if (failure !== "config reference") yield* Effect.promise(() => fs.mkdir(directory)) - if (failure === "config reference") { - yield* Effect.promise(() => fs.rm(path.join(directory, "username.txt"), { recursive: true })) - yield* Effect.promise(() => fs.writeFile(path.join(directory, "username.txt"), "test-user")) - } + yield* Effect.promise(() => fs.mkdir(directory)) const repaired = yield* locations.contextEffect(ref) yield* Effect.forEach(scopes, (scope) => Scope.close(scope, Exit.void)) diff --git a/packages/core/test/session-move.test.ts b/packages/core/test/session-move.test.ts index cbcfa610468..99dc52b1657 100644 --- a/packages/core/test/session-move.test.ts +++ b/packages/core/test/session-move.test.ts @@ -293,11 +293,7 @@ describe("Session.move", () => { replacements: [ ...bindings, ...(broken - ? [ - Config.node.replace( - Config.configured({ project: false, global: false, content: config }), - ), - ] + ? [Config.node.replace(Layer.effect(Config.Service, Effect.die("broken config")))] : []), ], }),