Skip to content

Commit 17536a9

Browse files
authored
fix(core): discover local plugin packages (anomalyco#41784)
1 parent 315bc6a commit 17536a9

2 files changed

Lines changed: 146 additions & 11 deletions

File tree

‎packages/core/src/config/plugin/source.ts‎

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Directory, Document, type Entry } from "@opencode-ai/schema/config"
44
import { ConfigPlugin } from "@opencode-ai/schema/config/plugin"
55
import { FSUtil } from "@opencode-ai/util/fs-util"
66
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
7-
import { Context, Effect, Layer, Option, PubSub, Scope, Stream } from "effect"
7+
import { Context, Effect, Layer, Option, Predicate, PubSub, Schema, Scope, Stream } from "effect"
88
import path from "path"
99
import { fileURLToPath } from "url"
1010
import { Config } from "../../config"
@@ -154,19 +154,67 @@ const scan = Effect.fn("ConfigPluginSource.scan")(function* (
154154
})
155155

156156
const sourceDirectories = ["plugin", "plugins"] as const
157+
const Package = Schema.Struct({
158+
exports: Schema.optional(Schema.Unknown),
159+
module: Schema.optional(Schema.Unknown),
160+
main: Schema.optional(Schema.Unknown),
161+
})
162+
const decodePackage = Schema.decodeUnknownOption(Package)
157163

158164
function discoverDirectory(fs: FSUtil.Interface, directory: string) {
159165
return Effect.gen(function* () {
160-
const files = yield* fs
161-
.scan(`{${sourceDirectories.join(",")}}/*.{ts,js}`, {
162-
cwd: directory,
163-
absolute: true,
164-
include: "file",
165-
dot: true,
166-
symlink: true,
167-
})
168-
.pipe(Effect.orElseSucceed(() => []))
169-
return files.sort().map((target): Operation => ({ type: "add", target, options: {} }))
166+
const children = (yield* Effect.forEach(sourceDirectories, (source) =>
167+
fs.readDirectoryEntries(path.join(directory, source)).pipe(
168+
Effect.orElseSucceed(() => []),
169+
Effect.map((entries) =>
170+
entries.map((entry) => ({ ...entry, target: path.join(directory, source, entry.name) })),
171+
),
172+
),
173+
))
174+
.flat()
175+
.sort((a, b) => (a.target < b.target ? -1 : a.target > b.target ? 1 : 0))
176+
const targets = yield* Effect.forEach(children, (entry) => discoverChild(fs, entry))
177+
return targets.flatMap(Option.toArray).map((target): Operation => ({ type: "add", target, options: {} }))
178+
})
179+
}
180+
181+
function discoverChild(fs: FSUtil.Interface, entry: FSUtil.DirEntry & { target: string }) {
182+
return Effect.gen(function* () {
183+
const source = entry.target.endsWith(".ts") || entry.target.endsWith(".js")
184+
if (entry.type === "file" && source) return Option.some(entry.target)
185+
if (entry.type === "directory") return yield* discoverPackage(fs, entry.target)
186+
if (entry.type !== "symlink") return Option.none<string>()
187+
if (source && (yield* fs.isFile(entry.target))) return Option.some(entry.target)
188+
if (yield* fs.isDir(entry.target)) return yield* discoverPackage(fs, entry.target)
189+
return Option.none<string>()
190+
})
191+
}
192+
193+
function discoverPackage(fs: FSUtil.Interface, directory: string) {
194+
return Effect.gen(function* () {
195+
const root = yield* fs.resolve(directory)
196+
const manifest = yield* fs
197+
.readJson(path.join(directory, "package.json"))
198+
.pipe(Effect.map(decodePackage), Effect.orElseSucceed(Option.none))
199+
const configured = Option.isSome(manifest)
200+
? [manifest.value.exports, manifest.value.module, manifest.value.main].filter(Predicate.isString)
201+
: []
202+
return yield* Effect.findFirst(
203+
[...configured, "index.ts", "index.js"]
204+
.filter((entry) => !path.isAbsolute(entry))
205+
.map((entry) => path.resolve(directory, entry))
206+
.filter((entry) => FSUtil.contains(directory, entry)),
207+
(entry) =>
208+
fs
209+
.isFile(entry)
210+
.pipe(
211+
Effect.flatMap((exists) =>
212+
exists
213+
? fs.resolve(entry).pipe(Effect.map((resolved) => FSUtil.contains(root, resolved)))
214+
: Effect.succeed(false),
215+
),
216+
),
217+
)
170218
})
171219
}
172220

‎packages/core/test/config/plugin.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,75 @@ describe("PluginSupervisor config", () => {
168168
),
169169
)
170170

171+
it.live("loads auto-discovered plugin package entrypoints in order", () =>
172+
withLocation(
173+
undefined,
174+
Effect.gen(function* () {
175+
yield* ready()
176+
const plugins = yield* Plugin.Service
177+
const ids = (yield* plugins.list()).map((plugin) => String(plugin.id))
178+
expect(ids).toContain("package-exports")
179+
expect(ids).toContain("package-module")
180+
expect(ids).toContain("package-main")
181+
expect(ids).toContain("package-index")
182+
}),
183+
false,
184+
async (directory) => {
185+
await Promise.all([
186+
writeDiscoveredPackage(directory, "exports", { exports: "./entry.ts" }, { "entry.ts": "package-exports" }),
187+
writeDiscoveredPackage(
188+
directory,
189+
"module",
190+
{ exports: "./missing.js", module: "./entry.js" },
191+
{ "entry.js": "package-module" },
192+
),
193+
writeDiscoveredPackage(
194+
directory,
195+
"main",
196+
{ exports: { import: "./missing.js" }, module: "./missing.js", main: "./entry.js" },
197+
{ "entry.js": "package-main" },
198+
),
199+
writeDiscoveredPackage(directory, "index", undefined, { "index.js": "package-index" }),
200+
])
201+
},
202+
),
203+
)
204+
205+
it.live("keeps auto-discovered package entrypoints inside the package directory", () =>
206+
withLocation(
207+
undefined,
208+
Effect.gen(function* () {
209+
yield* ready()
210+
const plugins = yield* Plugin.Service
211+
const ids = (yield* plugins.list()).map((plugin) => String(plugin.id))
212+
expect(ids).toContain("contained-fallback")
213+
expect(ids).toContain("symlink-fallback")
214+
expect(ids).not.toContain("escaped-entrypoint")
215+
}),
216+
false,
217+
async (directory) => {
218+
await fs.mkdir(path.join(directory, ".opencode"), { recursive: true })
219+
await fs.writeFile(path.join(directory, ".opencode", "escape.js"), discoveredPlugin("escaped-entrypoint"))
220+
await writeDiscoveredPackage(
221+
directory,
222+
"contained",
223+
{ exports: "../../escape.js" },
224+
{ "index.js": "contained-fallback" },
225+
)
226+
await writeDiscoveredPackage(
227+
directory,
228+
"symlink",
229+
{ exports: "./entry.js" },
230+
{ "index.js": "symlink-fallback" },
231+
)
232+
await fs.symlink(
233+
path.join(directory, ".opencode", "escape.js"),
234+
path.join(directory, ".opencode", "plugins", "symlink", "entry.js"),
235+
)
236+
},
237+
),
238+
)
239+
171240
staticIt.live("uses only internal and SDK plugins when the static source is wired", () =>
172241
Effect.gen(function* () {
173242
const sdk = yield* SdkPlugins.Service
@@ -389,3 +458,21 @@ export default Plugin.define({
389458
})
390459
`
391460
}
461+
462+
function discoveredPlugin(id: string) {
463+
return `export default { id: ${JSON.stringify(id)}, setup() {} }`
464+
}
465+
466+
async function writeDiscoveredPackage(
467+
directory: string,
468+
name: string,
469+
manifest: Record<string, unknown> | undefined,
470+
files: Record<string, string>,
471+
) {
472+
const plugin = path.join(directory, ".opencode", "plugins", name)
473+
await fs.mkdir(plugin, { recursive: true })
474+
await Promise.all([
475+
...(manifest ? [fs.writeFile(path.join(plugin, "package.json"), JSON.stringify(manifest))] : []),
476+
...Object.entries(files).map(([file, id]) => fs.writeFile(path.join(plugin, file), discoveredPlugin(id))),
477+
])
478+
}

0 commit comments

Comments
 (0)