From 470c5b66fdbcd1dbb95e9d639503d1c0b1a9ba21 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Mon, 7 Jul 2025 15:51:02 -0400 Subject: [PATCH] fix --- packages/opencode/src/bun/index.ts | 21 +++++++++++------- packages/opencode/src/file/watch.ts | 27 +++++++++++------------- packages/opencode/src/storage/storage.ts | 13 ++++++------ 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/packages/opencode/src/bun/index.ts b/packages/opencode/src/bun/index.ts index 7a7d89cf5..23180e316 100644 --- a/packages/opencode/src/bun/index.ts +++ b/packages/opencode/src/bun/index.ts @@ -8,10 +8,7 @@ import { readableStreamToText } from "bun" export namespace BunProc { const log = Log.create({ service: "bun" }) - export async function run( - cmd: string[], - options?: Bun.SpawnOptions.OptionsObject, - ) { + export async function run(cmd: string[], options?: Bun.SpawnOptions.OptionsObject) { log.info("running", { cmd: [which(), ...cmd], ...options, @@ -26,9 +23,17 @@ export namespace BunProc { BUN_BE_BUN: "1", }, }) - const code = await result.exited; - const stdout = result.stdout ? typeof result.stdout === "number" ? result.stdout : await readableStreamToText(result.stdout) : undefined - const stderr = result.stderr ? typeof result.stderr === "number" ? result.stderr : await readableStreamToText(result.stderr) : undefined + const code = await result.exited + const stdout = result.stdout + ? typeof result.stdout === "number" + ? result.stdout + : await readableStreamToText(result.stdout) + : undefined + const stderr = result.stderr + ? typeof result.stderr === "number" + ? result.stderr + : await readableStreamToText(result.stderr) + : undefined log.info("done", { code, stdout, @@ -61,7 +66,7 @@ export namespace BunProc { if (parsed.dependencies[pkg] === version) return mod parsed.dependencies[pkg] = version await Bun.write(pkgjson, JSON.stringify(parsed, null, 2)) - await BunProc.run(["install", "--registry=https://registry.npmjs.org"], { + await BunProc.run(["install", "--cwd", Global.Path.cache, "--registry=https://registry.npmjs.org"], { cwd: Global.Path.cache, }).catch((e) => { throw new InstallFailedError( diff --git a/packages/opencode/src/file/watch.ts b/packages/opencode/src/file/watch.ts index 1d12168fa..6546f64d2 100644 --- a/packages/opencode/src/file/watch.ts +++ b/packages/opencode/src/file/watch.ts @@ -21,23 +21,20 @@ export namespace FileWatcher { "file.watcher", () => { const app = App.use() + if (!app.info.git) return {} try { - const watcher = fs.watch( - app.info.path.cwd, - { recursive: true }, - (event, file) => { - log.info("change", { file, event }) - if (!file) return - // for some reason async local storage is lost here - // https://github.com/oven-sh/bun/issues/20754 - App.provideExisting(app, async () => { - Bus.publish(Event.Updated, { - file, - event, - }) + const watcher = fs.watch(app.info.path.cwd, { recursive: true }, (event, file) => { + log.info("change", { file, event }) + if (!file) return + // for some reason async local storage is lost here + // https://github.com/oven-sh/bun/issues/20754 + App.provideExisting(app, async () => { + Bus.publish(Event.Updated, { + file, + event, }) - }, - ) + }) + }) return { watcher } } catch { return {} diff --git a/packages/opencode/src/storage/storage.ts b/packages/opencode/src/storage/storage.ts index c31be870a..ccafb34da 100644 --- a/packages/opencode/src/storage/storage.ts +++ b/packages/opencode/src/storage/storage.ts @@ -10,10 +10,7 @@ export namespace Storage { const log = Log.create({ service: "storage" }) export const Event = { - Write: Bus.event( - "storage.write", - z.object({ key: z.string(), content: z.any() }), - ), + Write: Bus.event("storage.write", z.object({ key: z.string(), content: z.any() })), } type Migration = (dir: string) => Promise @@ -28,8 +25,12 @@ export namespace Storage { const content = await Bun.file(file).json() if (!content.metadata) continue log.info("migrating to v2 message", { file }) - const result = MessageV2.fromV1(content) - await Bun.write(file, JSON.stringify(result, null, 2)) + try { + const result = MessageV2.fromV1(content) + await Bun.write(file, JSON.stringify(result, null, 2)) + } catch (e) { + await fs.rename(file, file.replace("storage", "broken")) + } } }, ]