From 88988b8c12a169c9ecadbb44205207b95b756713 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Thu, 27 Aug 2026 22:13:34 -0400 Subject: [PATCH] fix(core): preserve typed config test-layer errors (#45778) --- packages/core/src/config.ts | 5 ++- packages/core/test/config/config.test.ts | 46 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 2740ff68848..23bde08186b 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -84,7 +84,10 @@ export const testLayer = (initial: Entry[] = []) => const entry = current[index] if (!entry || entry.type !== "document") return yield* Effect.fail(new UpdateError({ message: "No editable config document found" })) - const info = produce(entry.info, update) + const info = yield* Effect.try({ + try: () => produce(entry.info, update), + catch: (cause) => new UpdateError({ message: "Config update failed", cause }), + }) yield* Ref.set(entries, current.with(index, new Document({ type: "document", path: entry.path, info }))) return info }), diff --git a/packages/core/test/config/config.test.ts b/packages/core/test/config/config.test.ts index 926a3859109..d0ffdb1f797 100644 --- a/packages/core/test/config/config.test.ts +++ b/packages/core/test/config/config.test.ts @@ -97,6 +97,20 @@ describe("Config", () => { Effect.andThen( Effect.gen(function* () { const config = yield* Config.Service + const content = yield* Effect.promise(() => fs.readFile(globalFile, "utf8")) + const cause = new Error("Rejected config update") + const error = yield* config + .update((draft) => { + draft.shell = "discarded" + throw cause + }) + .pipe(Effect.flip) + + expect(error).toBeInstanceOf(Config.UpdateError) + expect(error.message).toBe("Config update failed") + expect(error.cause).toBe(cause) + expect(yield* Effect.promise(() => fs.readFile(globalFile, "utf8"))).toBe(content) + const updated = yield* config.update((draft) => { draft.shell = "updated" }) @@ -338,6 +352,38 @@ describe("Config", () => { }).pipe(Effect.provide(Config.testLayer())), ) + it.effect("keeps test config unchanged after an update callback fails", () => + Effect.gen(function* () { + const config = yield* Config.Service + const test = yield* Config.Test + const entry = new Document({ + type: "document", + path: AbsolutePath.make(path.join(import.meta.dir, "opencode.json")), + info: new Info({ shell: "initial" }), + }) + yield* test.setEntries([entry]) + const cause = new Error("Rejected config update") + const error = yield* config + .update((draft) => { + draft.shell = "discarded" + throw cause + }) + .pipe(Effect.flip) + + expect(error).toBeInstanceOf(Config.UpdateError) + expect(error.message).toBe("Config update failed") + expect(error.cause).toBe(cause) + expect(yield* config.entries()).toEqual([entry]) + expect(entry.info.shell).toBe("initial") + + const updated = yield* config.update((draft) => { + draft.shell = "recovered" + }) + expect(updated.shell).toBe("recovered") + expect(Config.latest(yield* test.entries(), "shell")).toBe("recovered") + }).pipe(Effect.provide(Config.testLayer())), + ) + it.effect("returns the latest defined scalar from priority-ordered documents", () => Effect.sync(() => { const entries = [