Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/opencode/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Log } from "../util/log"
import path from "path"
import { pathToFileURL, fileURLToPath } from "url"
import { pathToFileURL } from "url"
import { createRequire } from "module"
import os from "os"
import z from "zod"
Expand Down Expand Up @@ -473,7 +473,18 @@ export namespace Config {
*/
export function getPluginName(plugin: string): string {
if (plugin.startsWith("file://")) {
return path.parse(new URL(plugin).pathname).name
const pathname = decodeURIComponent(new URL(plugin).pathname)
const parts = pathname.split("/")
const nodeModules = parts.lastIndexOf("node_modules")
if (nodeModules > -1) {
const name = parts[nodeModules + 1]
if (name?.startsWith("@")) {
const scoped = parts[nodeModules + 2]
if (scoped) return `${name}/${scoped}`
}
if (name) return name
}
return path.posix.parse(pathname).name
}
const lastAt = plugin.lastIndexOf("@")
if (lastAt > 0) {
Expand Down
19 changes: 19 additions & 0 deletions packages/opencode/test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,14 @@ describe("getPluginName", () => {
expect(Config.getPluginName("file:///some/path/my-plugin.js")).toBe("my-plugin")
})

test("extracts package name from node_modules file:// URL", () => {
const basic = pathToFileURL(path.join("/tmp", "project", "node_modules", "foo", "index.js")).href
const scoped = pathToFileURL(path.join("/tmp", "project", "node_modules", "@scope", "pkg", "dist", "index.js")).href

expect(Config.getPluginName(basic)).toBe("foo")
expect(Config.getPluginName(scoped)).toBe("@scope/pkg")
})

test("extracts name from npm package with version", () => {
expect(Config.getPluginName("oh-my-opencode@2.4.3")).toBe("oh-my-opencode")
expect(Config.getPluginName("some-plugin@1.0.0")).toBe("some-plugin")
Expand Down Expand Up @@ -1589,6 +1597,17 @@ describe("deduplicatePlugins", () => {
expect(result).toEqual(["a-plugin@1.0.0", "b-plugin@1.0.0", "c-plugin@1.0.0"])
})

test("keeps multiple npm plugins resolved as file:// URLs", () => {
const plugins = [
pathToFileURL(path.join("/tmp", "project", "node_modules", "foo", "index.js")).href,
pathToFileURL(path.join("/tmp", "project", "node_modules", "bar", "index.js")).href,
]

const result = Config.deduplicatePlugins(plugins)

expect(result).toEqual(plugins)
})

test("local plugin directory overrides global opencode.json plugin", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
Expand Down
Loading