Skip to content

Commit 0228d19

Browse files
committed
fix(plugin): resolve scoped packages from project directory in local plugins
When a local plugin (e.g. ~/.opencode/plugins/probe.js) imports a scoped package like @opencode/plugin, the Node resolver was resolving from the plugin file's directory (~/.opencode/plugins/) where the package is not installed, instead of from the project directory where it is. This adds a projectRoot() helper that finds the project directory by locating the .opencode segment in the entrypoint path, then uses it as the parent URL context when resolving @-scoped imports from within local plugin files. Resolved files inside node_modules are returned without tracking (external packages), while local files are tracked for hot-reload as before. Fixes anomalyco#50434
1 parent 681156c commit 0228d19

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

‎packages/plugin/src/source.node.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { registerHooks } from "node:module"
22
import path from "node:path"
3-
import { fileURLToPath } from "node:url"
3+
import { fileURLToPath, pathToFileURL } from "node:url"
44
import { localSource } from "./source.js"
55
import { missingPackageTarget } from "./source.package.js"
66
import { Host } from "./host.js"
77

88
let generation = Date.now()
99

10+
function projectRoot(entrypoint: string): string | undefined {
11+
const filePath = fileURLToPath(entrypoint)
12+
const parts = filePath.split(path.sep)
13+
const idx = parts.indexOf(".opencode")
14+
if (idx < 1) return undefined
15+
return parts.slice(0, idx).join(path.sep)
16+
}
17+
1018
export async function prepareSource(entrypoint: string, track: (file: string, directory?: boolean) => void) {
1119
const version = String(++generation)
20+
const projectDir = projectRoot(entrypoint)
1221
const fresh = (specifier: string) => {
1322
const url = new URL(specifier)
1423
url.searchParams.set("__opencode_reload", version)
@@ -18,11 +27,28 @@ export async function prepareSource(entrypoint: string, track: (file: string, di
1827
resolve(specifier, context, nextResolve) {
1928
if (!context.parentURL || new URL(context.parentURL).searchParams.get("__opencode_reload") !== version)
2029
return nextResolve(specifier, context)
30+
const parentDir = path.dirname(fileURLToPath(context.parentURL))
2131
const local =
2232
specifier.startsWith("./") || specifier.startsWith("../")
2333
? new URL(specifier, context.parentURL)
24-
: localSource(specifier, path.dirname(fileURLToPath(context.parentURL)))
34+
: localSource(specifier, parentDir)
2535
if (!local) {
36+
if (specifier.startsWith("@") && projectDir) {
37+
const pluginDir = path.dirname(fileURLToPath(context.parentURL))
38+
if (pluginDir.startsWith(projectDir + path.sep) || pluginDir === projectDir) {
39+
try {
40+
const resolved = nextResolve(specifier, { ...context, parentURL: pathToFileURL(projectDir).href })
41+
if (resolved.url.startsWith("file:")) {
42+
const file = fileURLToPath(resolved.url)
43+
if (!file.split(path.sep).includes("node_modules")) {
44+
track(file)
45+
return { ...resolved, url: fresh(resolved.url) }
46+
}
47+
}
48+
return resolved
49+
} catch {}
50+
}
51+
}
2652
try {
2753
return nextResolve(specifier, context)
2854
} catch (error) {

0 commit comments

Comments
 (0)