GeezerVison is showing github.com — fix(core): substitute empty for missing config file references (#55388) · matglas/opencode@c458ad7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/config/variable.ts +17-21Lines changed: 17 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,6 @@ type ParseSource = | 19 | 19 |
| 20 | 20 | type SubstituteInput = ParseSource & { | 21 | 21 | text: string | 22 | | - missing?: "error" | "empty" | 23 | 22 | env?: Record<string, string> | 24 | 23 | } | 25 | 24 |
| @@ -56,27 +55,24 @@ const substituteFiles = Effect.fnUntraced(function* (input: SubstituteInput, tex | 56 | 55 | const filePath = token.slice("{file:".length, -1) | 57 | 56 | const expandedPath = filePath.startsWith("~/") ? path.join(os.homedir(), filePath.slice(2)) : filePath | 58 | 57 | const resolvedPath = path.isAbsolute(expandedPath) ? expandedPath : path.resolve(configDir, expandedPath) | 59 | | - const fileContent = yield* fs.readFileString(resolvedPath).pipe( | 60 | | - Effect.catch((error) => { | 61 | | - if (input.missing === "empty") return Effect.succeed("") | | 58 | + const fileContent = yield* fs | | 59 | + .readFileStringSafe(resolvedPath) | | 60 | + .pipe( | | 61 | + Effect.mapError( | | 62 | + (error) => | | 63 | + new InvalidError({ path: configSource, message: `bad file reference: "${token}"` }, { cause: error }), | | 64 | + ), | | 65 | + ) | | 66 | + // Like a missing {env:VAR}, a missing or unreadable file substitutes empty so one stale secret | | 67 | + // reference cannot take down every Location that loads this config. | | 68 | + if (fileContent === undefined) | | 69 | + yield* Effect.logWarning("config file reference is missing or unreadable", { | | 70 | + source: configSource, | | 71 | + reference: token, | | 72 | + path: resolvedPath, | | 73 | + }) | 62 | 74 |
| 63 | | - const message = `bad file reference: "${token}"` | 64 | | - return Effect.fail( | 65 | | - new InvalidError( | 66 | | - { | 67 | | - path: configSource, | 68 | | - message: | 69 | | - error._tag === "PlatformError" && error.reason._tag === "NotFound" | 70 | | - ? `${message} ${resolvedPath} does not exist` | 71 | | - : message, | 72 | | - }, | 73 | | - { cause: error }, | 74 | | - ), | 75 | | - ) | 76 | | - }), | 77 | | - ) | 78 | | - | 79 | | - out += JSON.stringify(fileContent.trim()).slice(1, -1) | | 75 | + out += JSON.stringify((fileContent ?? "").trim()).slice(1, -1) | 80 | 76 | cursor = index + token.length | 81 | 77 | } | 82 | 78 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/config/config.test.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1306,7 +1306,7 @@ describe("Config", () => { | 1306 | 1306 | fs.writeFile( | 1307 | 1307 | path.join(tmp.path, "opencode.jsonc"), | 1308 | 1308 | `{ | 1309 | | - // Ignored reference: {file:missing.txt} | | 1309 | + // Ignored reference: {file:.} | 1310 | 1310 | "username": "user-{env:OPENCODE_TEST_MISSING}", | 1311 | 1311 | "mcp": { | 1312 | 1312 | "servers": { | @@ -1315,7 +1315,8 @@ describe("Config", () => { | 1315 | 1315 | "url": "https://example.com/mcp", | 1316 | 1316 | "headers": { | 1317 | 1317 | "Authorization": "Bearer {env:OPENCODE_TEST_MCP_TOKEN}", | 1318 | | - "X-Token": "{file:token.txt}" | | 1318 | + "X-Token": "{file:token.txt}", | | 1319 | + "X-Missing": "Bearer {file:~/.opencode-test-missing/key}" | 1319 | 1320 | } | 1320 | 1321 | } | 1321 | 1322 | } | @@ -1335,6 +1336,7 @@ describe("Config", () => { | 1335 | 1336 | expect(remote.headers).toEqual({ | 1336 | 1337 | Authorization: "Bearer secret", | 1337 | 1338 | "X-Token": 'file\n"token"', | | 1339 | + "X-Missing": "Bearer ", | 1338 | 1340 | }) | 1339 | 1341 | }).pipe(Effect.provide(testLayer(tmp.path))) | 1340 | 1342 | }), | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/location-layer.test.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,7 +103,7 @@ describe("LocationServiceMap", () => { | 103 | 103 | yield* Effect.addFinalizer(() => Effect.promise(() => fs.chmod(directory, 0o755))) | 104 | 104 | } | 105 | 105 | if (failure === "config reference") { | 106 | | - yield* Effect.promise(() => fs.mkdir(directory)) | | 106 | + yield* Effect.promise(() => fs.mkdir(path.join(directory, "username.txt"), { recursive: true })) | 107 | 107 | yield* Effect.promise(() => | 108 | 108 | fs.writeFile(path.join(directory, "opencode.json"), JSON.stringify({ username: "{file:username.txt}" })), | 109 | 109 | ) | @@ -124,6 +124,7 @@ describe("LocationServiceMap", () => { | 124 | 124 | } | 125 | 125 | if (failure === "permissions") yield* Effect.promise(() => fs.chmod(directory, 0o755)) | 126 | 126 | if (failure === "config reference") { | | 127 | + yield* Effect.promise(() => fs.rm(path.join(directory, "username.txt"), { recursive: true })) | 127 | 128 | yield* Effect.promise(() => fs.writeFile(path.join(directory, "username.txt"), "test-user")) | 128 | 129 | } | 129 | 130 | expect((yield* Effect.promise(() => fs.stat(directory))).isDirectory()).toBe(true) | @@ -147,7 +148,7 @@ describe("LocationServiceMap", () => { | 147 | 148 | const locations = yield* LocationServiceMap.Service | 148 | 149 | if (failure === "file") yield* Effect.promise(() => fs.writeFile(directory, "file")) | 149 | 150 | if (failure === "config reference") { | 150 | | - yield* Effect.promise(() => fs.mkdir(directory)) | | 151 | + yield* Effect.promise(() => fs.mkdir(path.join(directory, "username.txt"), { recursive: true })) | 151 | 152 | yield* Effect.promise(() => | 152 | 153 | fs.writeFile(path.join(directory, "opencode.json"), JSON.stringify({ username: "{file:username.txt}" })), | 153 | 154 | ) | @@ -165,6 +166,7 @@ describe("LocationServiceMap", () => { | 165 | 166 | if (failure === "file") yield* Effect.promise(() => fs.rm(directory)) | 166 | 167 | if (failure !== "config reference") yield* Effect.promise(() => fs.mkdir(directory)) | 167 | 168 | if (failure === "config reference") { | | 169 | + yield* Effect.promise(() => fs.rm(path.join(directory, "username.txt"), { recursive: true })) | 168 | 170 | yield* Effect.promise(() => fs.writeFile(path.join(directory, "username.txt"), "test-user")) | 169 | 171 | } | 170 | 172 | const repaired = yield* locations.contextEffect(ref) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/session-move.test.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -215,7 +215,7 @@ describe("Session.move", () => { | 215 | 215 | yield* Effect.promise(() => Promise.all([mkdir(source), mkdir(destination)])) | 216 | 216 | if (broken) | 217 | 217 | yield* Effect.promise(() => | 218 | | - Bun.write(path.join(source, "opencode.json"), JSON.stringify({ instructions: ["{file:./missing.txt}"] })), | | 218 | + Bun.write(path.join(source, "opencode.json"), JSON.stringify({ instructions: ["{file:.}"] })), | 219 | 219 | ) | 220 | 220 | const session = yield* Session.Service | 221 | 221 | const execution = yield* SessionExecution.Service | @@ -271,7 +271,7 @@ describe("Session.move", () => { | 271 | 271 | const source = Location.Ref.make({ directory: AbsolutePath.make(path.join(tmp.path, "source")) }) | 272 | 272 | const destination = AbsolutePath.make(path.join(tmp.path, "destination")) | 273 | 273 | yield* Effect.promise(() => Promise.all([mkdir(source.directory), mkdir(destination)])) | 274 | | - const config = JSON.stringify({ instructions: ["{file:./missing.txt}"] }) | | 274 | + const config = JSON.stringify({ instructions: ["{file:.}"] }) | 275 | 275 | if (!broken) yield* Effect.promise(() => Bun.write(path.join(source.directory, "opencode.json"), config)) | 276 | 276 | const selectedID = Session.ID.create() | 277 | 277 | const replacements: LayerNode.Replacements = [ | |
|