|
@@ -1,7 +1,8 @@
|
|
|
import { BusEvent } from "@/bus/bus-event"
|
|
import { BusEvent } from "@/bus/bus-event"
|
|
|
import { Bus } from "@/bus"
|
|
import { Bus } from "@/bus"
|
|
|
|
|
+import { InstanceContext } from "@/effect/instances"
|
|
|
|
|
+import { Instance } from "@/project/instance"
|
|
|
import z from "zod"
|
|
import z from "zod"
|
|
|
-import { Instance } from "../project/instance"
|
|
|
|
|
import { Log } from "../util/log"
|
|
import { Log } from "../util/log"
|
|
|
import { FileIgnore } from "./ignore"
|
|
import { FileIgnore } from "./ignore"
|
|
|
import { Config } from "../config/config"
|
|
import { Config } from "../config/config"
|
|
@@ -9,118 +10,139 @@ import path from "path"
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
import { createWrapper } from "@parcel/watcher/wrapper"
|
|
import { createWrapper } from "@parcel/watcher/wrapper"
|
|
|
import { lazy } from "@/util/lazy"
|
|
import { lazy } from "@/util/lazy"
|
|
|
-import { withTimeout } from "@/util/timeout"
|
|
|
|
|
import type ParcelWatcher from "@parcel/watcher"
|
|
import type ParcelWatcher from "@parcel/watcher"
|
|
|
-import { Flag } from "@/flag/flag"
|
|
|
|
|
import { readdir } from "fs/promises"
|
|
import { readdir } from "fs/promises"
|
|
|
import { git } from "@/util/git"
|
|
import { git } from "@/util/git"
|
|
|
import { Protected } from "./protected"
|
|
import { Protected } from "./protected"
|
|
|
|
|
+import { Flag } from "@/flag/flag"
|
|
|
|
|
+import { Cause, Effect, Layer, ServiceMap } from "effect"
|
|
|
|
|
|
|
|
const SUBSCRIBE_TIMEOUT_MS = 10_000
|
|
const SUBSCRIBE_TIMEOUT_MS = 10_000
|
|
|
|
|
|
|
|
declare const OPENCODE_LIBC: string | undefined
|
|
declare const OPENCODE_LIBC: string | undefined
|
|
|
|
|
|
|
|
|
|
+const log = Log.create({ service: "file.watcher" })
|
|
|
|
|
+
|
|
|
|
|
+const event = {
|
|
|
|
|
+ Updated: BusEvent.define(
|
|
|
|
|
+ "file.watcher.updated",
|
|
|
|
|
+ z.object({
|
|
|
|
|
+ file: z.string(),
|
|
|
|
|
+ event: z.union([z.literal("add"), z.literal("change"), z.literal("unlink")]),
|
|
|
|
|
+ }),
|
|
|
|
|
+ ),
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const watcher = lazy((): typeof import("@parcel/watcher") | undefined => {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const binding = require(
|
|
|
|
|
+ `@parcel/watcher-${process.platform}-${process.arch}${process.platform === "linux" ? `-${OPENCODE_LIBC || "glibc"}` : ""}`,
|
|
|
|
|
+ )
|
|
|
|
|
+ return createWrapper(binding) as typeof import("@parcel/watcher")
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ log.error("failed to load watcher binding", { error })
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+})
|
|
|
|
|
+
|
|
|
|
|
+function getBackend() {
|
|
|
|
|
+ if (process.platform === "win32") return "windows"
|
|
|
|
|
+ if (process.platform === "darwin") return "fs-events"
|
|
|
|
|
+ if (process.platform === "linux") return "inotify"
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export namespace FileWatcher {
|
|
export namespace FileWatcher {
|
|
|
- const log = Log.create({ service: "file.watcher" })
|
|
|
|
|
-
|
|
|
|
|
- export const Event = {
|
|
|
|
|
- Updated: BusEvent.define(
|
|
|
|
|
- "file.watcher.updated",
|
|
|
|
|
- z.object({
|
|
|
|
|
- file: z.string(),
|
|
|
|
|
- event: z.union([z.literal("add"), z.literal("change"), z.literal("unlink")]),
|
|
|
|
|
- }),
|
|
|
|
|
- ),
|
|
|
|
|
|
|
+ export const Event = event
|
|
|
|
|
+ /** Whether the native @parcel/watcher binding is available on this platform. */
|
|
|
|
|
+ export const hasNativeBinding = () => !!watcher()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const init = Effect.fn("FileWatcherService.init")(function* () {})
|
|
|
|
|
+
|
|
|
|
|
+export namespace FileWatcherService {
|
|
|
|
|
+ export interface Service {
|
|
|
|
|
+ readonly init: () => Effect.Effect<void>
|
|
|
}
|
|
}
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export class FileWatcherService extends ServiceMap.Service<FileWatcherService, FileWatcherService.Service>()(
|
|
|
|
|
+ "@opencode/FileWatcher",
|
|
|
|
|
+) {
|
|
|
|
|
+ static readonly layer = Layer.effect(
|
|
|
|
|
+ FileWatcherService,
|
|
|
|
|
+ Effect.gen(function* () {
|
|
|
|
|
+ const instance = yield* InstanceContext
|
|
|
|
|
+ if (yield* Flag.OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER) return FileWatcherService.of({ init })
|
|
|
|
|
|
|
|
- const watcher = lazy((): typeof import("@parcel/watcher") | undefined => {
|
|
|
|
|
- try {
|
|
|
|
|
- const binding = require(
|
|
|
|
|
- `@parcel/watcher-${process.platform}-${process.arch}${process.platform === "linux" ? `-${OPENCODE_LIBC || "glibc"}` : ""}`,
|
|
|
|
|
- )
|
|
|
|
|
- return createWrapper(binding) as typeof import("@parcel/watcher")
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- log.error("failed to load watcher binding", { error })
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- const state = Instance.state(
|
|
|
|
|
- async () => {
|
|
|
|
|
- log.info("init")
|
|
|
|
|
- const cfg = await Config.get()
|
|
|
|
|
- const backend = (() => {
|
|
|
|
|
- if (process.platform === "win32") return "windows"
|
|
|
|
|
- if (process.platform === "darwin") return "fs-events"
|
|
|
|
|
- if (process.platform === "linux") return "inotify"
|
|
|
|
|
- })()
|
|
|
|
|
|
|
+ log.info("init", { directory: instance.directory })
|
|
|
|
|
+
|
|
|
|
|
+ const backend = getBackend()
|
|
|
if (!backend) {
|
|
if (!backend) {
|
|
|
- log.error("watcher backend not supported", { platform: process.platform })
|
|
|
|
|
- return {}
|
|
|
|
|
|
|
+ log.error("watcher backend not supported", { directory: instance.directory, platform: process.platform })
|
|
|
|
|
+ return FileWatcherService.of({ init })
|
|
|
}
|
|
}
|
|
|
- log.info("watcher backend", { platform: process.platform, backend })
|
|
|
|
|
|
|
|
|
|
const w = watcher()
|
|
const w = watcher()
|
|
|
- if (!w) return {}
|
|
|
|
|
|
|
+ if (!w) return FileWatcherService.of({ init })
|
|
|
|
|
+
|
|
|
|
|
+ log.info("watcher backend", { directory: instance.directory, platform: process.platform, backend })
|
|
|
|
|
|
|
|
- const subscribe: ParcelWatcher.SubscribeCallback = (err, evts) => {
|
|
|
|
|
|
|
+ const subs: ParcelWatcher.AsyncSubscription[] = []
|
|
|
|
|
+ yield* Effect.addFinalizer(() => Effect.promise(() => Promise.allSettled(subs.map((sub) => sub.unsubscribe()))))
|
|
|
|
|
+
|
|
|
|
|
+ const cb: ParcelWatcher.SubscribeCallback = Instance.bind((err, evts) => {
|
|
|
if (err) return
|
|
if (err) return
|
|
|
for (const evt of evts) {
|
|
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" })
|
|
|
|
|
|
|
+ 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[]) => {
|
|
|
|
|
+ const pending = w.subscribe(dir, cb, { ignore, backend })
|
|
|
|
|
+ return Effect.gen(function* () {
|
|
|
|
|
+ const sub = yield* Effect.promise(() => pending)
|
|
|
|
|
+ subs.push(sub)
|
|
|
|
|
+ }).pipe(
|
|
|
|
|
+ Effect.timeout(SUBSCRIBE_TIMEOUT_MS),
|
|
|
|
|
+ Effect.catchCause((cause) => {
|
|
|
|
|
+ log.error("failed to subscribe", { dir, cause: Cause.pretty(cause) })
|
|
|
|
|
+ // Clean up a subscription that resolves after timeout
|
|
|
|
|
+ pending.then((s) => s.unsubscribe()).catch(() => {})
|
|
|
|
|
+ return Effect.void
|
|
|
|
|
+ }),
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const subs: ParcelWatcher.AsyncSubscription[] = []
|
|
|
|
|
|
|
+ const cfg = yield* Effect.promise(() => Config.get())
|
|
|
const cfgIgnores = cfg.watcher?.ignore ?? []
|
|
const cfgIgnores = cfg.watcher?.ignore ?? []
|
|
|
|
|
|
|
|
- if (Flag.OPENCODE_EXPERIMENTAL_FILEWATCHER) {
|
|
|
|
|
- const pending = w.subscribe(Instance.directory, subscribe, {
|
|
|
|
|
- ignore: [...FileIgnore.PATTERNS, ...cfgIgnores, ...Protected.paths()],
|
|
|
|
|
- backend,
|
|
|
|
|
- })
|
|
|
|
|
- const sub = await withTimeout(pending, SUBSCRIBE_TIMEOUT_MS).catch((err) => {
|
|
|
|
|
- log.error("failed to subscribe to Instance.directory", { error: err })
|
|
|
|
|
- pending.then((s) => s.unsubscribe()).catch(() => {})
|
|
|
|
|
- return undefined
|
|
|
|
|
- })
|
|
|
|
|
- if (sub) subs.push(sub)
|
|
|
|
|
|
|
+ if (yield* Flag.OPENCODE_EXPERIMENTAL_FILEWATCHER) {
|
|
|
|
|
+ yield* subscribe(instance.directory, [...FileIgnore.PATTERNS, ...cfgIgnores, ...Protected.paths()])
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (Instance.project.vcs === "git") {
|
|
|
|
|
- const result = await git(["rev-parse", "--git-dir"], {
|
|
|
|
|
- cwd: Instance.worktree,
|
|
|
|
|
- })
|
|
|
|
|
- const vcsDir = result.exitCode === 0 ? path.resolve(Instance.worktree, result.text().trim()) : undefined
|
|
|
|
|
|
|
+ if (instance.project.vcs === "git") {
|
|
|
|
|
+ const result = yield* Effect.promise(() =>
|
|
|
|
|
+ git(["rev-parse", "--git-dir"], {
|
|
|
|
|
+ cwd: instance.project.worktree,
|
|
|
|
|
+ }),
|
|
|
|
|
+ )
|
|
|
|
|
+ const vcsDir = result.exitCode === 0 ? path.resolve(instance.project.worktree, result.text().trim()) : undefined
|
|
|
if (vcsDir && !cfgIgnores.includes(".git") && !cfgIgnores.includes(vcsDir)) {
|
|
if (vcsDir && !cfgIgnores.includes(".git") && !cfgIgnores.includes(vcsDir)) {
|
|
|
- const gitDirContents = await readdir(vcsDir).catch(() => [])
|
|
|
|
|
- const ignoreList = gitDirContents.filter((entry) => entry !== "HEAD")
|
|
|
|
|
- const pending = w.subscribe(vcsDir, subscribe, {
|
|
|
|
|
- ignore: ignoreList,
|
|
|
|
|
- backend,
|
|
|
|
|
- })
|
|
|
|
|
- const sub = await withTimeout(pending, SUBSCRIBE_TIMEOUT_MS).catch((err) => {
|
|
|
|
|
- log.error("failed to subscribe to vcsDir", { error: err })
|
|
|
|
|
- pending.then((s) => s.unsubscribe()).catch(() => {})
|
|
|
|
|
- return undefined
|
|
|
|
|
- })
|
|
|
|
|
- if (sub) subs.push(sub)
|
|
|
|
|
|
|
+ const ignore = (yield* Effect.promise(() => readdir(vcsDir).catch(() => []))).filter(
|
|
|
|
|
+ (entry) => entry !== "HEAD",
|
|
|
|
|
+ )
|
|
|
|
|
+ yield* subscribe(vcsDir, ignore)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return { subs }
|
|
|
|
|
- },
|
|
|
|
|
- async (state) => {
|
|
|
|
|
- if (!state.subs) return
|
|
|
|
|
- await Promise.all(state.subs.map((sub) => sub?.unsubscribe()))
|
|
|
|
|
- },
|
|
|
|
|
|
|
+ return FileWatcherService.of({ init })
|
|
|
|
|
+ }).pipe(
|
|
|
|
|
+ Effect.catchCause((cause) => {
|
|
|
|
|
+ log.error("failed to init watcher service", { cause: Cause.pretty(cause) })
|
|
|
|
|
+ return Effect.succeed(FileWatcherService.of({ init }))
|
|
|
|
|
+ }),
|
|
|
|
|
+ ),
|
|
|
)
|
|
)
|
|
|
-
|
|
|
|
|
- export function init() {
|
|
|
|
|
- if (Flag.OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER) {
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
- state()
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|