Skip to content

Commit 4676c93

Browse files
committed
refactor(plugin): resolve a plugin directory's real path in Host.resolve
Resolving the directory before asking the runtime for its entrypoints belongs in the shared resolver, not at the call sites that happened to hit the runtime's stale cache: the TUI plugin loader and `opencode plugin list` also pass a local plugin directory through Host.resolve, so a plugin behind a repointed symlink kept resolving to its previous target there. One real-path step in Host.resolve covers every caller and `ConfigPluginSource.scan` and `PluginModule.load` go back to resolving the directory the way they did before. A directory that does not exist keeps resolving to no entrypoints rather than throwing, the way the resolution of missing entrypoints already behaved.
1 parent 22e0aca commit 4676c93

3 files changed

Lines changed: 49 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,12 @@ const scan = Effect.fn("ConfigPluginSource.scan")(function* (
161161
if (operation.type === "remove" || !path.isAbsolute(operation.target)) return [operation]
162162
if (!(yield* fs.existsSafe(operation.target))) return [operation]
163163
const directory = yield* fs.isDir(operation.target)
164-
// Resolve the directory before asking the runtime for its entrypoints: the
165-
// runtime caches a symlinked directory's resolution for the life of the
166-
// process, so a plugin directory symlink retargeted since startup would
167-
// otherwise resolve to its old target while the containment check below
168-
// compares it against the new one.
169-
const root = directory ? yield* fs.resolve(operation.target) : operation.target
170164
const entrypoints: Host.Entrypoints = directory
171-
? yield* Effect.sync(() => Host.resolve({ directory: root }))
165+
? yield* Effect.sync(() => Host.resolve({ directory: operation.target }))
172166
: { server: pathToFileURL(operation.target).href }
173167
if (!entrypoints.server) return []
174168
if (directory) {
169+
const root = yield* fs.resolve(operation.target)
175170
const server = yield* fs.resolve(fileURLToPath(entrypoints.server))
176171
if (!FSUtil.contains(root, server)) {
177172
yield* Effect.logWarning("configured plugin resolves outside its directory", {

‎packages/plugin/src/host.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * as Host from "./host.js"
22

3+
import { realpathSync } from "node:fs"
34
import path from "node:path"
45
import { importModule, resolveModule } from "@opencode/util/runtime-import"
56

@@ -15,13 +16,19 @@ export interface Entrypoints {
1516
}
1617

1718
export function resolve(target: Target): Entrypoints {
19+
// The runtime caches a symlinked directory's resolution for the life of the
20+
// process, so a plugin directory symlink retargeted since it was first resolved
21+
// (nix, home-manager, stow, dotfiles) would keep reporting its previous
22+
// target's entrypoints for every caller. Follow the link before resolving, so
23+
// the entrypoints belong to the directory's current target.
24+
const directory = target.name ? target.directory : realDirectory(target.directory)
1825
const entry = (subpaths: readonly string[]) => {
1926
for (const subpath of subpaths) {
2027
const specifier = target.name
2128
? [target.name, subpath].filter(Boolean).join("/")
22-
: path.resolve(target.directory, subpath || "index")
29+
: path.resolve(directory, subpath || "index")
2330
try {
24-
return resolveModule(specifier, target.directory)
31+
return resolveModule(specifier, directory)
2532
} catch (error) {
2633
if (
2734
!(error instanceof Error) ||
@@ -46,3 +53,13 @@ export function resolve(target: Target): Entrypoints {
4653
export function load(entrypoint: string): Promise<unknown> {
4754
return importModule(entrypoint)
4855
}
56+
57+
// A directory that does not exist yet must keep resolving to nothing rather than
58+
// throw: callers ask about configured plugins that may have gone away.
59+
function realDirectory(directory: string) {
60+
try {
61+
return realpathSync(directory)
62+
} catch {
63+
return directory
64+
}
65+
}

‎packages/plugin/test/host.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "node:assert/strict"
2-
import { mkdir, mkdtemp, realpath, rm, writeFile } from "node:fs/promises"
2+
import { mkdir, mkdtemp, realpath, rm, symlink, writeFile } from "node:fs/promises"
33
import { tmpdir } from "node:os"
44
import path from "node:path"
55
import { describe, it } from "node:test"
@@ -153,4 +153,31 @@ describe("Host.resolve", () => {
153153
)
154154
assert.deepEqual(Host.resolve(plugin.target), { server: undefined, tui: undefined, rpc: undefined })
155155
})
156+
157+
it("resolves a directory symlink to its current target after it is retargeted", async () => {
158+
const root = await realpath(await mkdtemp(path.join(tmpdir(), "opencode-host-link-")))
159+
const first = path.join(root, "first")
160+
const second = path.join(root, "second")
161+
const link = path.join(root, "plugin")
162+
await mkdir(first, { recursive: true })
163+
await mkdir(second, { recursive: true })
164+
await writeFile(path.join(first, "index.ts"), source)
165+
await writeFile(path.join(second, "index.ts"), source)
166+
await symlink(first, link)
167+
assert.equal(Host.resolve({ directory: link }).server, pathToFileURL(path.join(first, "index.ts")).href)
168+
169+
// What a dotfiles tool does on update: repoint the symlink at the new directory.
170+
await rm(link)
171+
await symlink(second, link)
172+
assert.equal(Host.resolve({ directory: link }).server, pathToFileURL(path.join(second, "index.ts")).href)
173+
await rm(root, { recursive: true, force: true })
174+
})
175+
176+
it("resolves a directory that does not exist to no entrypoints", () => {
177+
assert.deepEqual(Host.resolve({ directory: path.join(tmpdir(), "opencode-host-absent") }), {
178+
server: undefined,
179+
tui: undefined,
180+
rpc: undefined,
181+
})
182+
})
156183
})

0 commit comments

Comments
 (0)