diff --git a/packages/opencode/src/file/watcher.ts b/packages/opencode/src/file/watcher.ts index 8244f1c77c..56a3dcb70f 100644 --- a/packages/opencode/src/file/watcher.ts +++ b/packages/opencode/src/file/watcher.ts @@ -1,6 +1,7 @@ import { BusEvent } from "@/bus/bus-event" import { Bus } from "@/bus" import { InstanceContext } from "@/effect/instances" +import { Instance } from "@/project/instance" import z from "zod" import { Log } from "../util/log" import { FileIgnore } from "./ignore" @@ -88,13 +89,21 @@ export class FileWatcherService extends ServiceMap.Service Effect.promise(() => Promise.allSettled(subs.map((sub) => sub.unsubscribe())))) + const directory = instance.directory const cb: ParcelWatcher.SubscribeCallback = (err, evts) => { if (err) return - for (const evt of evts) { - if (evt.type === "create") Bus.publish(event.Updated, { file: evt.path, event: "add" }) - if (evt.type === "update") Bus.publish(event.Updated, { file: evt.path, event: "change" }) - if (evt.type === "delete") Bus.publish(event.Updated, { file: evt.path, event: "unlink" }) - } + // Instance.provide restores ALS context since native watcher callbacks + // fire outside the Instance async context + Instance.provide({ + directory, + fn: () => { + for (const evt of evts) { + if (evt.type === "create") Bus.publish(event.Updated, { file: evt.path, event: "add" }) + if (evt.type === "update") Bus.publish(event.Updated, { file: evt.path, event: "change" }) + if (evt.type === "delete") Bus.publish(event.Updated, { file: evt.path, event: "unlink" }) + } + }, + }) } const subscribe = (dir: string, ignore: string[]) =>