mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 21:15:57 +03:00
perf(config): list each ancestor folder once when discovering config files (#60803)
This commit is contained in:
committed by
GitHub
Unverified
parent
df832cb2ca
commit
6b08407a5a
@@ -72,22 +72,41 @@ export const discover = Effect.fn("ConfigDiscovery.discover")(function* (options
|
||||
const globalRoots = yield* Effect.forEach(
|
||||
[...configDirectories, globalClaudeDirectory, globalAgentsDirectory],
|
||||
(item) => fs.resolve(item),
|
||||
{ concurrency: "unbounded" },
|
||||
)
|
||||
const current = yield* fs.resolve(location.directory)
|
||||
const directories =
|
||||
globalRoots.slice(0, configDirectories.length).includes(current) || options?.project === false
|
||||
? []
|
||||
: yield* fs.up({ targets: ["."], start: location.directory }).pipe(Effect.orDie)
|
||||
const discovered = yield* Effect.forEach(directories, (directory) =>
|
||||
Effect.gen(function* () {
|
||||
// Resolve the parent too: missing children must honor symlinked global roots.
|
||||
const parent = yield* fs.resolve(directory)
|
||||
return yield* Effect.forEach([".claude", ".agents", ".opencode", ...names.toReversed()], (name) =>
|
||||
fs
|
||||
.resolve(path.join(parent, name))
|
||||
.pipe(Effect.map((resolved) => ({ item: AbsolutePath.make(path.join(directory, name)), resolved }))),
|
||||
)
|
||||
}),
|
||||
const candidates = [".claude", ".agents", ".opencode", ...names.toReversed()]
|
||||
const discovered = yield* Effect.forEach(
|
||||
directories,
|
||||
(directory) =>
|
||||
Effect.gen(function* () {
|
||||
// Resolve the parent too: missing children must honor symlinked global roots.
|
||||
const parent = directory === location.directory ? current : yield* fs.resolve(directory)
|
||||
// One directory listing skips realpath and stat on absent candidates; sandboxes that deny
|
||||
// listing an ancestor fall back to probing each candidate directly.
|
||||
const entries = yield* fs.readDirectory(parent).pipe(
|
||||
Effect.map((list) => new Set(list.map((entry) => entry.toLowerCase()))),
|
||||
Effect.orElseSucceed(() => undefined),
|
||||
)
|
||||
return yield* Effect.forEach(
|
||||
candidates,
|
||||
(name) => {
|
||||
const item = AbsolutePath.make(path.join(directory, name))
|
||||
if (entries && !entries.has(name.toLowerCase())) {
|
||||
return Effect.succeed({ item, resolved: path.resolve(parent, name), missing: true })
|
||||
}
|
||||
return fs
|
||||
.resolve(path.join(parent, name))
|
||||
.pipe(Effect.map((resolved) => ({ item, resolved, missing: false })))
|
||||
},
|
||||
{ concurrency: "unbounded" },
|
||||
)
|
||||
}),
|
||||
{ concurrency: "unbounded" },
|
||||
).pipe(
|
||||
Effect.map((items) => items.flat()),
|
||||
Effect.orDie,
|
||||
@@ -97,15 +116,15 @@ export const discover = Effect.fn("ConfigDiscovery.discover")(function* (options
|
||||
const globalFiles = yield* Effect.forEach(
|
||||
configDirectories.flatMap((directory) => names.map((name) => path.join(directory, name))),
|
||||
(file) => fs.resolve(file),
|
||||
{ concurrency: "unbounded" },
|
||||
)
|
||||
// Global sources must not re-enter through the project walk.
|
||||
const visible = discovered
|
||||
.filter(({ resolved }) =>
|
||||
globalEnabled
|
||||
? !globalRoots.includes(resolved) && !globalFiles.includes(resolved)
|
||||
: !globalRoots.some((root) => resolved === root || resolved.startsWith(root + path.sep)),
|
||||
)
|
||||
.map(({ item }) => item)
|
||||
const visibleEntries = discovered.filter((entry) =>
|
||||
globalEnabled
|
||||
? !globalRoots.includes(entry.resolved) && !globalFiles.includes(entry.resolved)
|
||||
: !globalRoots.some((root) => entry.resolved === root || entry.resolved.startsWith(root + path.sep)),
|
||||
)
|
||||
const visible = visibleEntries.map((entry) => entry.item)
|
||||
|
||||
// A custom system directory replaces platform discovery, so it also skips host MDM preferences.
|
||||
const preferences =
|
||||
@@ -123,8 +142,12 @@ export const discover = Effect.fn("ConfigDiscovery.discover")(function* (options
|
||||
explicit: options?.file ? AbsolutePath.make(path.resolve(options.file)) : undefined,
|
||||
direct: visible.filter((item) => ![".agents", ".claude", ".opencode"].includes(path.basename(item))).toReversed(),
|
||||
project: yield* Effect.forEach(
|
||||
visible.filter((item) => path.basename(item) === ".opencode").toReversed(),
|
||||
(directory) => fs.isDir(directory).pipe(Effect.map((present) => ({ path: directory, present }))),
|
||||
visibleEntries.filter((entry) => path.basename(entry.item) === ".opencode").toReversed(),
|
||||
(entry) =>
|
||||
entry.missing
|
||||
? Effect.succeed({ path: entry.item, present: false })
|
||||
: fs.isDir(entry.item).pipe(Effect.map((present) => ({ path: entry.item, present }))),
|
||||
{ concurrency: "unbounded" },
|
||||
),
|
||||
claude:
|
||||
options?.claude === false
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
import path from "path"
|
||||
import fs from "fs/promises"
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect, Layer } from "effect"
|
||||
import { ConfigDiscovery } from "@opencode/core/config/discovery"
|
||||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { Location } from "@opencode/core/location"
|
||||
import { AbsolutePath } from "@opencode/core/schema"
|
||||
import { FSUtil } from "@opencode/util/fs-util"
|
||||
import { Global } from "@opencode/util/global"
|
||||
import { location } from "../fixture/location"
|
||||
import { tmpdir } from "../fixture/tmpdir"
|
||||
import { testEffect } from "../lib/effect"
|
||||
|
||||
const it = testEffect(Layer.empty)
|
||||
|
||||
function inFixture(root: string, target: string) {
|
||||
const relative = path.relative(root, target)
|
||||
return relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative))
|
||||
}
|
||||
|
||||
function discoverWith(
|
||||
root: string,
|
||||
directory: string,
|
||||
projectDirectory = directory,
|
||||
wrapFs?: (base: FSUtil.Interface) => Partial<FSUtil.Interface>,
|
||||
) {
|
||||
const globalDir = path.join(root, "global")
|
||||
const fsLayer = wrapFs
|
||||
? Layer.effect(
|
||||
FSUtil.Service,
|
||||
Effect.map(FSUtil.Service, (base) => FSUtil.Service.of({ ...base, ...wrapFs(base) })),
|
||||
).pipe(Layer.provide(AppNodeBuilder.build(FSUtil.node)))
|
||||
: AppNodeBuilder.build(FSUtil.node)
|
||||
|
||||
return ConfigDiscovery.discover({ system: false }).pipe(
|
||||
Effect.provide(
|
||||
Layer.mergeAll(
|
||||
fsLayer,
|
||||
Global.layerWith({ config: globalDir, home: path.join(globalDir, "home") }),
|
||||
Layer.succeed(
|
||||
Location.Service,
|
||||
Location.Service.of(
|
||||
location(
|
||||
{ directory: AbsolutePath.make(directory) },
|
||||
{ projectDirectory: AbsolutePath.make(projectDirectory) },
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
describe("ConfigDiscovery.discover", () => {
|
||||
it.live("skips resolve and stat calls for absent ancestor candidates while keeping watch paths", () =>
|
||||
Effect.acquireDisposable(Effect.promise(() => tmpdir())).pipe(
|
||||
Effect.flatMap((tmp) =>
|
||||
Effect.gen(function* () {
|
||||
const root = yield* Effect.promise(() => fs.realpath(tmp.path))
|
||||
const ancestor = path.join(root, "ws", "org", "repo")
|
||||
const nested = path.join(ancestor, "packages", "feature")
|
||||
yield* Effect.promise(async () => {
|
||||
await fs.mkdir(path.join(ancestor, ".opencode"), { recursive: true })
|
||||
await fs.mkdir(nested, { recursive: true })
|
||||
await fs.writeFile(path.join(nested, "opencode.json"), "{}")
|
||||
})
|
||||
|
||||
const resolved: string[] = []
|
||||
const dirChecks: string[] = []
|
||||
const sources = yield* discoverWith(root, nested, ancestor, (base) => ({
|
||||
resolve: (input) => {
|
||||
resolved.push(input)
|
||||
return base.resolve(input)
|
||||
},
|
||||
isDir: (input) => {
|
||||
dirChecks.push(input)
|
||||
return base.isDir(input)
|
||||
},
|
||||
}))
|
||||
|
||||
const ws = path.join(root, "ws")
|
||||
const fixtureResolved = resolved.filter((item) => inFixture(ws, item))
|
||||
expect(fixtureResolved).not.toContain(path.join(nested, "opencode.jsonc"))
|
||||
expect(fixtureResolved).not.toContain(path.join(nested, ".opencode"))
|
||||
expect(fixtureResolved).not.toContain(path.join(ancestor, "opencode.json"))
|
||||
expect(fixtureResolved).toContain(path.join(nested, "opencode.json"))
|
||||
expect(fixtureResolved).toContain(path.join(ancestor, ".opencode"))
|
||||
|
||||
expect(dirChecks.filter((item) => inFixture(ws, item))).toEqual([path.join(ancestor, ".opencode")])
|
||||
|
||||
const wsDirect = sources.direct.filter((item) => inFixture(ws, item))
|
||||
expect(wsDirect).toContain(AbsolutePath.make(path.join(nested, "opencode.jsonc")))
|
||||
expect(wsDirect).toContain(AbsolutePath.make(path.join(nested, "opencode.json")))
|
||||
const wsProject = sources.project.filter((entry) => inFixture(ws, entry.path))
|
||||
expect(wsProject).toContainEqual({ path: AbsolutePath.make(path.join(ancestor, ".opencode")), present: true })
|
||||
expect(wsProject).toContainEqual({ path: AbsolutePath.make(path.join(nested, ".opencode")), present: false })
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("falls back to direct candidate probes when listing an ancestor directory is denied", () =>
|
||||
Effect.acquireDisposable(Effect.promise(() => tmpdir())).pipe(
|
||||
Effect.flatMap((tmp) =>
|
||||
Effect.gen(function* () {
|
||||
const root = yield* Effect.promise(() => fs.realpath(tmp.path))
|
||||
const guarded = path.join(root, "guarded")
|
||||
const project = path.join(guarded, "project")
|
||||
yield* Effect.promise(async () => {
|
||||
await fs.mkdir(path.join(guarded, ".opencode"), { recursive: true })
|
||||
await fs.mkdir(project, { recursive: true })
|
||||
})
|
||||
|
||||
const sources = yield* discoverWith(root, project, project, (base) => ({
|
||||
readDirectory: (target, options) =>
|
||||
target === guarded
|
||||
? base.readDirectory(path.join(target, "unreadable-missing"), options)
|
||||
: base.readDirectory(target, options),
|
||||
}))
|
||||
expect(sources.project).toContainEqual({
|
||||
path: AbsolutePath.make(path.join(guarded, ".opencode")),
|
||||
present: true,
|
||||
})
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
it.live("probes candidates when readDirectory reports a differently cased entry", () =>
|
||||
Effect.acquireDisposable(Effect.promise(() => tmpdir())).pipe(
|
||||
Effect.flatMap((tmp) =>
|
||||
Effect.gen(function* () {
|
||||
const root = yield* Effect.promise(() => fs.realpath(tmp.path))
|
||||
const project = path.join(root, "cased")
|
||||
yield* Effect.promise(() => fs.mkdir(path.join(project, ".opencode"), { recursive: true }))
|
||||
|
||||
const sources = yield* discoverWith(root, project, project, (base) => ({
|
||||
readDirectory: (target, options) =>
|
||||
target === project ? Effect.succeed([".OPENCODE"]) : base.readDirectory(target, options),
|
||||
}))
|
||||
expect(sources.project).toContainEqual({
|
||||
path: AbsolutePath.make(path.join(project, ".opencode")),
|
||||
present: true,
|
||||
})
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
})
|
||||
@@ -82,10 +82,11 @@ const instances = Layer.effect(
|
||||
).pipe(Layer.provide(Watcher.testLayer))
|
||||
|
||||
const it = testEffect(
|
||||
AppNodeBuilder.build(
|
||||
LayerNode.group([Database.node, Bus.node, SdkPlugins.node, LocationServiceMap.node]),
|
||||
[Global.node.replace(tempGlobalLayer), offlineModels, LocationServiceMap.node.replace(instances)],
|
||||
).pipe(Layer.provideMerge(Watcher.testLayer)),
|
||||
AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, SdkPlugins.node, LocationServiceMap.node]), [
|
||||
Global.node.replace(tempGlobalLayer),
|
||||
offlineModels,
|
||||
LocationServiceMap.node.replace(instances),
|
||||
]).pipe(Layer.provideMerge(Watcher.testLayer)),
|
||||
)
|
||||
|
||||
const greeter = (command: string) => `export default {
|
||||
@@ -359,17 +360,17 @@ it.live("applies organization integration changes to running external plugins",
|
||||
}),
|
||||
)
|
||||
})
|
||||
const watcher = yield* Watcher.Test
|
||||
const locations = yield* LocationServiceMap.Service
|
||||
yield* Effect.gen(function* () {
|
||||
const plugins = yield* Plugin.Service
|
||||
const commands = yield* Command.Service
|
||||
const config = yield* Config.Service
|
||||
const bus = yield* Bus.Service
|
||||
yield* plugins.awaitActivation
|
||||
expect(yield* commands.get("policy-greet")).toBeUndefined()
|
||||
expect(yield* Effect.promise(() => Bun.file(entered).exists())).toBe(false)
|
||||
yield* Effect.promise(() => Bun.write(configuration, JSON.stringify({ plugins: [root] })))
|
||||
yield* bus.publish(Event.Updated, {})
|
||||
yield* watcher.emit({ path: configuration, type: "update" })
|
||||
yield* commands.get("policy-greet").pipe(
|
||||
Effect.flatMap((command) => (command ? Effect.void : Effect.fail("pending"))),
|
||||
Effect.retry({ times: 200, schedule: Schedule.spaced("10 millis") }),
|
||||
|
||||
Reference in New Issue
Block a user