Skip to content

Commit 89aa254

Browse files
committed
fix(core): log skipped plugin discovery
A plugin directory with no server, GUI, or TUI entrypoint, or whose entrypoint resolves outside the directory (for example a NixOS/home-manager symlink into /nix/store), was dropped without any log line. Log a warning with the configured target and, for containment, the resolved root and entrypoint. The containment and loading behavior is unchanged. The no-entrypoint warning is limited to explicit plugin entries; helper directories discovered under .opencode/plugin(s)/ stay silent. Add focused tests for both skip paths, the GUI-only and TUI-only semantics, a valid plugin, and an auto-discovered helper directory. Based on the reference patch by @Dante-dan in the issue. Refs anomalyco#58647
1 parent 2fc3716 commit 89aa254

2 files changed

Lines changed: 167 additions & 3 deletions

File tree

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function parse(input: ConfigPlugin.Plugin): Operation {
119119
return { type: "remove", target: input.slice(1) }
120120
}
121121

122-
const scan = Effect.fn("ConfigPluginSource.scan")(function* (
122+
export const scan = Effect.fn("ConfigPluginSource.scan")(function* (
123123
fs: FSUtil.Interface,
124124
location: Location.Interface,
125125
entries: readonly Entry[],
@@ -155,6 +155,7 @@ const scan = Effect.fn("ConfigPluginSource.scan")(function* (
155155
return Option.some<Operation>(operation)
156156
}),
157157
).pipe(Effect.map((operations) => operations.flatMap(Option.toArray)))
158+
const configuredTargets = new Set(resolved.map((operation) => operation.target))
158159
// Explicit config is applied last so it can remove auto-discovered packages.
159160
return yield* Effect.forEach([...discovered, ...resolved], (operation) =>
160161
Effect.gen(function* () {
@@ -166,11 +167,26 @@ const scan = Effect.fn("ConfigPluginSource.scan")(function* (
166167
: { server: pathToFileURL(operation.target).href }
167168
// A directory without a server part still lists for its GUI part. A TUI-only directory stays the TUI's own.
168169
const entrypoint = entrypoints.server ?? entrypoints.gui
169-
if (!entrypoint) return []
170+
if (!entrypoint) {
171+
// Auto-discovered directories also include helper folders; only an explicit plugin entry is misconfigured.
172+
if (configuredTargets.has(operation.target) && !entrypoints.tui) {
173+
yield* Effect.logWarning("plugin directory has no server, GUI, or TUI entrypoint", {
174+
target: operation.target,
175+
})
176+
}
177+
return []
178+
}
170179
if (directory) {
171180
const root = yield* fs.resolve(operation.target)
172181
const resolved = yield* fs.resolve(fileURLToPath(entrypoint))
173-
if (!FSUtil.contains(root, resolved)) return []
182+
if (!FSUtil.contains(root, resolved)) {
183+
yield* Effect.logWarning("plugin entrypoint resolves outside plugin directory", {
184+
target: operation.target,
185+
root,
186+
entrypoint: resolved,
187+
})
188+
return []
189+
}
174190
}
175191
const times = yield* Effect.forEach(
176192
[
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { expect } from "bun:test"
2+
import fs from "node:fs/promises"
3+
import path from "node:path"
4+
import { ConfigPluginSource } from "@opencode/core/config/plugin/source"
5+
import { Location } from "@opencode/core/location"
6+
import { AbsolutePath } from "@opencode/core/schema"
7+
import { Directory, Document, Info, type Entry } from "@opencode/schema/config"
8+
import { FSUtil } from "@opencode/util/fs-util"
9+
import { Effect, Logger } from "effect"
10+
import { tmpdirScoped } from "../fixture/tmpdir"
11+
import { testEffect } from "../lib/effect"
12+
import { PluginTestLayer } from "../plugin/fixture"
13+
14+
const it = testEffect(PluginTestLayer)
15+
16+
const entries = (plugins: string[]) => [new Document({ type: "document", info: new Info({ plugins }) })]
17+
18+
const scanEntries = (fs: FSUtil.Interface, location: Location.Interface, entries: readonly Entry[]) => {
19+
const warnings: unknown[] = []
20+
const logger = Logger.make(({ logLevel, message }) => {
21+
if (logLevel === "Warn") warnings.push(message)
22+
})
23+
return ConfigPluginSource.scan(fs, location, entries).pipe(
24+
Effect.provide(Logger.layer([logger])),
25+
Effect.map((operations) => ({ operations, warnings })),
26+
)
27+
}
28+
29+
const scan = (fs: FSUtil.Interface, location: Location.Interface, plugins: string[]) =>
30+
scanEntries(fs, location, entries(plugins))
31+
32+
it.live("warns when a plugin directory has no server, GUI, or TUI entrypoint", () =>
33+
Effect.gen(function* () {
34+
const root = yield* tmpdirScoped()
35+
const directory = path.join(root.path, "empty-plugin")
36+
yield* Effect.promise(() => fs.mkdir(directory, { recursive: true }))
37+
const fsService = yield* FSUtil.Service
38+
const location = yield* Location.Service
39+
40+
const result = yield* scan(fsService, location, [directory])
41+
42+
expect(result.operations).toEqual([])
43+
expect(result.warnings).toContainEqual([
44+
"plugin directory has no server, GUI, or TUI entrypoint",
45+
{ target: directory },
46+
])
47+
}),
48+
)
49+
50+
// Windows requires Developer Mode or elevation for file symlinks; the containment path is covered on POSIX.
51+
const symlinkIt = process.platform === "win32" ? it.live.skip : it.live
52+
53+
symlinkIt("warns when a plugin entrypoint resolves outside its directory", () =>
54+
Effect.gen(function* () {
55+
const root = yield* tmpdirScoped()
56+
const directory = path.join(root.path, "plugin")
57+
const outside = path.join(root.path, "outside")
58+
yield* Effect.promise(async () => {
59+
await fs.mkdir(directory, { recursive: true })
60+
await fs.mkdir(outside, { recursive: true })
61+
await fs.writeFile(path.join(outside, "index.js"), "export default { id: 'probe', setup() {} }")
62+
await fs.symlink(path.join(outside, "index.js"), path.join(directory, "index.js"))
63+
})
64+
const fsService = yield* FSUtil.Service
65+
const location = yield* Location.Service
66+
const resolvedRoot = yield* fsService.resolve(directory)
67+
const resolvedEntrypoint = yield* fsService.resolve(path.join(outside, "index.js"))
68+
69+
const result = yield* scan(fsService, location, [directory])
70+
71+
expect(result.operations).toEqual([])
72+
expect(result.warnings).toContainEqual([
73+
"plugin entrypoint resolves outside plugin directory",
74+
{ target: directory, root: resolvedRoot, entrypoint: resolvedEntrypoint },
75+
])
76+
}),
77+
)
78+
79+
it.live("keeps a plugin directory whose server entrypoint is contained", () =>
80+
Effect.gen(function* () {
81+
const root = yield* tmpdirScoped()
82+
const directory = path.join(root.path, "plugin")
83+
yield* Effect.promise(async () => {
84+
await fs.mkdir(directory, { recursive: true })
85+
await fs.writeFile(path.join(directory, "index.js"), "export default { id: 'probe', setup() {} }")
86+
})
87+
const fsService = yield* FSUtil.Service
88+
const location = yield* Location.Service
89+
90+
const result = yield* scan(fsService, location, [directory])
91+
92+
expect(result.operations.map((operation) => operation.target)).toEqual([directory])
93+
expect(result.warnings).toEqual([])
94+
}),
95+
)
96+
97+
it.live("keeps a GUI-only plugin directory without warning", () =>
98+
Effect.gen(function* () {
99+
const root = yield* tmpdirScoped()
100+
const directory = path.join(root.path, "gui-plugin")
101+
yield* Effect.promise(async () => {
102+
await fs.mkdir(directory, { recursive: true })
103+
await fs.writeFile(path.join(directory, "gui-experimental.cjs"), "module.exports = {}")
104+
})
105+
const fsService = yield* FSUtil.Service
106+
const location = yield* Location.Service
107+
108+
const result = yield* scan(fsService, location, [directory])
109+
110+
expect(result.operations.map((operation) => operation.target)).toEqual([directory])
111+
expect(result.warnings).toEqual([])
112+
}),
113+
)
114+
115+
it.live("silently skips a TUI-only plugin directory", () =>
116+
Effect.gen(function* () {
117+
const root = yield* tmpdirScoped()
118+
const directory = path.join(root.path, "tui-plugin")
119+
yield* Effect.promise(async () => {
120+
await fs.mkdir(directory, { recursive: true })
121+
await fs.writeFile(path.join(directory, "tui.ts"), "export default {}")
122+
})
123+
const fsService = yield* FSUtil.Service
124+
const location = yield* Location.Service
125+
126+
const result = yield* scan(fsService, location, [directory])
127+
128+
expect(result.operations).toEqual([])
129+
expect(result.warnings).toEqual([])
130+
}),
131+
)
132+
133+
it.live("does not warn for auto-discovered helper directories", () =>
134+
Effect.gen(function* () {
135+
const root = yield* tmpdirScoped()
136+
const helper = path.join(root.path, "plugin", "lib")
137+
yield* Effect.promise(() => fs.mkdir(helper, { recursive: true }))
138+
const fsService = yield* FSUtil.Service
139+
const location = yield* Location.Service
140+
141+
const result = yield* scanEntries(fsService, location, [
142+
new Directory({ type: "directory", path: AbsolutePath.make(root.path) }),
143+
])
144+
145+
expect(result.operations).toEqual([])
146+
expect(result.warnings).toEqual([])
147+
}),
148+
)

0 commit comments

Comments
 (0)