diff --git a/internal/plugin/ispath_test.go b/internal/plugin/ispath_test.go new file mode 100644 index 000000000..6c1ee9062 --- /dev/null +++ b/internal/plugin/ispath_test.go @@ -0,0 +1,28 @@ +package plugin + +import "testing" + +// #2229: plugins.json travels between machines in a backup or a sync, so +// a folder plugin's path written on macOS or Linux is a path on Windows +// too, and a Windows one is a path off Windows, or the restoring machine +// asks bun to install it as an npm package. +func TestIsPathWhicheverSystemWroteIt(t *testing.T) { + for spec, want := range map[string]bool{ + "/Users/me/plugin": true, + "/home/me/plugin/index.js": true, + `\Users\me\plugin`: true, + `\\server\share\plugin`: true, + `C:\plugins\x`: true, + "D:/plugins/x": true, + "file:///Users/me/plugin": true, + "./my-plugin.js": true, + "opencode-gemini-auth": false, + "@magpie-community/opencode-kiro-auth": false, + "owner/repo": false, + "github:owner/repo": false, + } { + if got := IsPath(spec); got != want { + t.Errorf("IsPath(%q) = %v, want %v", spec, got, want) + } + } +} diff --git a/internal/plugin/store.go b/internal/plugin/store.go index e81696fa0..5d6c70e59 100644 --- a/internal/plugin/store.go +++ b/internal/plugin/store.go @@ -281,11 +281,16 @@ func checkList() { } // IsPath is whether spec names a file or folder rather than a package, -// as OpenCode tells them apart. +// as OpenCode tells them apart. A path rooted the way any system roots +// one counts on every system, since plugins.json travels between +// machines in a backup or a sync: a leading / or \ (a UNC path among +// them) on Windows as well, and a drive letter off it (#2229). func IsPath(spec string) bool { - return strings.HasPrefix(spec, "file://") || strings.HasPrefix(spec, ".") || filepath.IsAbs(spec) || regexp.MustCompile(`^[A-Za-z]:[\\/]`).MatchString(spec) + return strings.HasPrefix(spec, "file://") || strings.HasPrefix(spec, ".") || strings.HasPrefix(spec, "/") || strings.HasPrefix(spec, `\`) || filepath.IsAbs(spec) || driveRooted.MatchString(spec) } +var driveRooted = regexp.MustCompile(`^[A-Za-z]:[\\/]`) + var pkgName = regexp.MustCompile(`^(@[a-z0-9][a-z0-9._~-]*/)?[a-z0-9][a-z0-9._~-]*$`) // gitSpec is a package bun fetches from a git repository, not npm: