library: a plugin from a git download keeps one name across updates (#7269)
A plugin at the top of a git or GitHub download was named after the temp folder it was cloned into (magpie-skill-<random>), so each Update read a new name and added a second bundle, while the project's .pi/settings.json kept pointing at the first folder and Pi went on loading v1 (jinpwng, v0.1.4572). - rootName names the download's top after the repository (file:///x/acme.git is "acme", owner/repo is "repo", git@host:a/b.git is "b"); a folder inside the download, or a local folder, keeps its own name. - UpdatePlugin of a one-plugin source installs under the bundle's own name, so a bundle a magpie before this named magpie-skill-... is updated in place too: same folder, same project assignment, same Pi entry. Run (mote): go test ./internal/library -run 'TestPluginRootName|TestPiPackageFromGitUpdatesInPlace|TestPluginUpdateKeepsItsName|Plugin' -count=1 -timeout 45m: ok. Full suite with the batch's last commit. Real Pi: a throwaway test drove Pi 1.5.2 (npm, --mode rpc --approve) in a sandbox HOME against a project the Library gave the git package to. With this change: one bundle "skills" (the repo's name), get_commands acme-v1 before and acme-v2 after Update, settings.json unchanged. Broken by hand: the same run on origin/main's plugin.go gave bundles [magpie-skill-332298903 magpie-skill-745278034] and Pi still listed acme-v1 after Update; the two new Go tests were red there too (temp-folder name; two bundles). Not tried: the reporter's own repository.
This commit is contained in:
File diff suppressed because one or more lines are too long
+48
-10
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -789,16 +790,44 @@ func fetchSourceRoot(input string) (root, tmp string, src Source, err error) {
|
||||
return sub, tmp, src, nil
|
||||
}
|
||||
|
||||
// rootName is the name a plugin at root goes by when nothing in it names it:
|
||||
// its folder's, or, for the top of a repository downloaded into tmp, the
|
||||
// repository's (#7269). The download's folder is a new temp name on every
|
||||
// fetch, and a bundle named after it was a second bundle on each update.
|
||||
func rootName(input, root, tmp string) string {
|
||||
if tmp == "" || filepath.Clean(root) != filepath.Clean(tmp) {
|
||||
return filepath.Base(root)
|
||||
}
|
||||
repo := ""
|
||||
if src, ok := githubSource(input); ok {
|
||||
repo = src.Repo
|
||||
} else if src, ok, err := gitSource(input); ok && err == nil {
|
||||
repo = src.Repo
|
||||
if u, err := url.Parse(repo); err == nil && u.Scheme != "" {
|
||||
repo = u.Path
|
||||
} else if _, after, ok := strings.Cut(repo, ":"); ok {
|
||||
repo = after // user@host:group/repo.git
|
||||
}
|
||||
}
|
||||
parts := strings.FieldsFunc(repo, func(r rune) bool { return r == '/' || r == '\\' })
|
||||
for i := len(parts) - 1; i >= 0; i-- {
|
||||
if n := strings.TrimSuffix(parts[i], ".git"); n != "" {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// findPlugins reads the plugins at root: a marketplace's entries, or the
|
||||
// one plugin there.
|
||||
func findPlugins(root string) (*PluginProbe, error) {
|
||||
// one plugin there, which goes by hint when nothing in it names it.
|
||||
func findPlugins(root, hint string) (*PluginProbe, error) {
|
||||
p := &PluginProbe{root: root, Candidates: []PluginCandidate{}}
|
||||
mk, err := readJSONFile(filepath.Join(root, ".claude-plugin", "marketplace.json"))
|
||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
if mk == nil {
|
||||
pl, err := readPlugin(root, filepath.Base(root), "", nil)
|
||||
pl, err := readPlugin(root, hint, "", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -901,7 +930,7 @@ func ProbePlugins(input string) (*PluginProbe, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := findPlugins(root)
|
||||
p, err := findPlugins(root, rootName(input, root, tmp))
|
||||
if err != nil {
|
||||
if tmp != "" {
|
||||
os.RemoveAll(tmp)
|
||||
@@ -935,7 +964,7 @@ func InstallPlugins(input string, names, agents []string) (*Result, error) {
|
||||
return nil, err
|
||||
}
|
||||
defer cleanup()
|
||||
return installPlugins(p, input, names, agents)
|
||||
return installPlugins(p, input, names, agents, "")
|
||||
}
|
||||
|
||||
// pluginProbeFor is the probe of input, from the one the page just made when it
|
||||
@@ -947,7 +976,7 @@ func pluginProbeFor(input string) (*PluginProbe, func(), error) {
|
||||
probes.Unlock()
|
||||
if e != nil {
|
||||
if _, err := os.Stat(e.tmp); err == nil {
|
||||
if p, err := findPlugins(e.p.root); err == nil {
|
||||
if p, err := findPlugins(e.p.root, rootName(input, e.p.root, e.tmp)); err == nil {
|
||||
p.Source = input
|
||||
return p, func() {}, nil
|
||||
}
|
||||
@@ -958,7 +987,7 @@ func pluginProbeFor(input string) (*PluginProbe, func(), error) {
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
p, err := findPlugins(root)
|
||||
p, err := findPlugins(root, rootName(input, root, tmp))
|
||||
if err != nil {
|
||||
if tmp != "" {
|
||||
os.RemoveAll(tmp)
|
||||
@@ -973,7 +1002,8 @@ func pluginProbeFor(input string) (*PluginProbe, func(), error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func installPlugins(p *PluginProbe, input string, names, agents []string) (*Result, error) {
|
||||
// as is the name an update keeps for the one plugin, "" otherwise.
|
||||
func installPlugins(p *PluginProbe, input string, names, agents []string, as string) (*Result, error) {
|
||||
var picked []PluginCandidate
|
||||
switch {
|
||||
case len(names) == 0 && len(p.Candidates) == 1:
|
||||
@@ -1019,7 +1049,7 @@ func installPlugins(p *PluginProbe, input string, names, agents []string) (*Resu
|
||||
// a marketplace's name for it is its folder's: read with it, so
|
||||
// its parts name that folder, not the one its manifest's name
|
||||
// would have
|
||||
force := ""
|
||||
force := as
|
||||
if len(p.Candidates) > 1 || c.Remote {
|
||||
force = c.Name
|
||||
}
|
||||
@@ -1320,7 +1350,15 @@ func UpdatePlugin(name string) (*Result, error) {
|
||||
return nil, err
|
||||
}
|
||||
defer cleanup()
|
||||
return installPlugins(p, b.Source, names, nil)
|
||||
// the bundle keeps its name: it is what the projects and agents have it
|
||||
// by, and its folder what a project's Pi package entry names (#7269). A
|
||||
// name derived again can differ: a bundle named before by its
|
||||
// download's temp folder, or a manifest that renamed the plugin.
|
||||
if b.Pick == "" && len(p.Candidates) == 1 {
|
||||
p.Candidates[0].Name = name
|
||||
return installPlugins(p, b.Source, nil, nil, name)
|
||||
}
|
||||
return installPlugins(p, b.Source, names, nil, "")
|
||||
}
|
||||
|
||||
// ---- the page --------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// #7269 (jinpwng): a Pi package imported from a git repository's file://
|
||||
// address, given to a project for Pi, is updated in place: the same bundle,
|
||||
// the same folder, so the project's one entry in .pi/settings.json reaches
|
||||
// the new version and nothing else in it changes.
|
||||
func TestPiPackageFromGitUpdatesInPlace(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
pluginAgents(t, h)
|
||||
addr, commit := gitRepo(t, map[string]string{
|
||||
"package.json": `{"name":"acme-pi","version":"1.0.0","pi":{"extensions":["./extension.ts"],"skills":["./skills"]}}`,
|
||||
"extension.ts": "export default function (pi) { pi.registerCommand('acme-v1', {}) }\n",
|
||||
"skills/tidy/SKILL.md": "---\nname: tidy\ndescription: Tidies\n---\nv1\n",
|
||||
})
|
||||
if _, err := ProbePlugins(addr); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res := ok(t)(InstallPlugins(addr, nil, []string{}))
|
||||
if len(res.Installed) != 1 {
|
||||
t.Fatalf("installed: %+v", res.Installed)
|
||||
}
|
||||
name := res.Installed[0]
|
||||
if strings.HasPrefix(name, skillTemp) {
|
||||
t.Fatalf("named after its download's temp folder: %s", name)
|
||||
}
|
||||
proj := filepath.Join(t.TempDir(), "proj")
|
||||
gitInit(t, proj)
|
||||
ok(t)(AddProject(proj))
|
||||
file := filepath.Join(proj, ".pi/settings.json")
|
||||
write(t, file, "{\n \"packages\": [\"npm:pi-own\"]\n}\n")
|
||||
ok(t)(ProjectPlugin(proj, name, []string{"pi"}))
|
||||
before := read(t, file)
|
||||
if !strings.Contains(before, pluginDir(name)) || !strings.Contains(before, "npm:pi-own") {
|
||||
t.Fatalf("assigned:\n%s", before)
|
||||
}
|
||||
|
||||
commit(map[string]string{
|
||||
"package.json": `{"name":"acme-pi","version":"2.0.0","pi":{"extensions":["./extension.ts"],"skills":["./skills"]}}`,
|
||||
"extension.ts": "export default function (pi) { pi.registerCommand('acme-v2', {}) }\n",
|
||||
})
|
||||
forgetProbes()
|
||||
ok(t)(UpdatePlugin(name))
|
||||
|
||||
v := View0(t)
|
||||
if len(v.Plugins) != 1 || v.Plugins[0].Name != name {
|
||||
var names []string
|
||||
for _, b := range v.Plugins {
|
||||
names = append(names, b.Name)
|
||||
}
|
||||
t.Fatalf("after the update the library has %v, want only %s", names, name)
|
||||
}
|
||||
if s := read(t, filepath.Join(pluginDir(name), "extension.ts")); !strings.Contains(s, "acme-v2") {
|
||||
t.Fatalf("the bundle's folder has %q", s)
|
||||
}
|
||||
if after := read(t, file); after != before {
|
||||
t.Fatalf("the project's settings changed:\n%s\nwas\n%s", after, before)
|
||||
}
|
||||
if !slices.Contains(v.Projects[0].Plugins[name], "pi") {
|
||||
t.Fatalf("project's plugins: %+v", v.Projects[0].Plugins)
|
||||
}
|
||||
}
|
||||
|
||||
// A bundle named before by its download's temp folder (magpie-skill-…)
|
||||
// keeps that name on its update: it is the one the projects have.
|
||||
func TestPluginUpdateKeepsItsName(t *testing.T) {
|
||||
h := sandbox(t)
|
||||
pluginAgents(t, h)
|
||||
addr, commit := gitRepo(t, map[string]string{
|
||||
"package.json": `{"name":"acme-pi","version":"1.0.0","pi":{"skills":["./skills"]}}`,
|
||||
"skills/tidy/SKILL.md": "---\nname: tidy\ndescription: Tidies\n---\nv1\n",
|
||||
})
|
||||
res := ok(t)(InstallPlugins(addr, nil, []string{}))
|
||||
was := res.Installed[0]
|
||||
old := skillTemp + "4102397733"
|
||||
ok(t)(change(func(l *Library) error {
|
||||
b := l.plugin(was)
|
||||
removePluginParts(l, b)
|
||||
l.Plugins = slices.DeleteFunc(l.Plugins, func(x *Plugin) bool { return x == b })
|
||||
return nil
|
||||
}))
|
||||
if err := os.Rename(pluginDir(was), pluginDir(old)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ok(t)(change(func(l *Library) error {
|
||||
l.Plugins = append(l.Plugins, &Plugin{Name: old, Source: addr, Agents: []string{}})
|
||||
return nil
|
||||
}))
|
||||
proj := filepath.Join(t.TempDir(), "proj")
|
||||
gitInit(t, proj)
|
||||
ok(t)(AddProject(proj))
|
||||
ok(t)(ProjectPlugin(proj, old, []string{"pi"}))
|
||||
file := filepath.Join(proj, ".pi/settings.json")
|
||||
before := read(t, file)
|
||||
|
||||
commit(map[string]string{"skills/tidy/SKILL.md": "---\nname: tidy\ndescription: Tidies\n---\nv2\n"})
|
||||
forgetProbes()
|
||||
ok(t)(UpdatePlugin(old))
|
||||
v := View0(t)
|
||||
if len(v.Plugins) != 1 || v.Plugins[0].Name != old {
|
||||
t.Fatalf("plugins: %+v", v.Plugins)
|
||||
}
|
||||
if s := read(t, filepath.Join(pluginDir(old), "skills/tidy/SKILL.md")); !strings.Contains(s, "v2") {
|
||||
t.Fatalf("not updated: %q", s)
|
||||
}
|
||||
if read(t, file) != before {
|
||||
t.Fatalf("project settings changed:\n%s", read(t, file))
|
||||
}
|
||||
}
|
||||
|
||||
// The top of a downloaded repository goes by the repository's name, never
|
||||
// by its download's temp folder; a folder by its own name.
|
||||
func TestPluginRootName(t *testing.T) {
|
||||
tmp := filepath.Join(os.TempDir(), skillTemp+"123")
|
||||
for in, want := range map[string]string{
|
||||
"file:///srv/git/acme-pi.git": "acme-pi",
|
||||
"file:///Users/me/src/acme-pi": "acme-pi",
|
||||
"file:///Users/me/src/acme-pi/.git": "acme-pi",
|
||||
"https://github.com/acme/pi-kit": "pi-kit",
|
||||
"acme/pi-kit": "pi-kit",
|
||||
"git@gitlab.example.com:team/pi-kit.git": "pi-kit",
|
||||
"https://gitlab.com/g/sub/pi-kit.git": "pi-kit",
|
||||
} {
|
||||
if got := rootName(in, tmp, tmp); got != want {
|
||||
t.Errorf("%s: %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
if got := rootName("acme/pi-kit", filepath.Join(tmp, "plugins", "kit"), tmp); got != "kit" {
|
||||
t.Errorf("a folder in the download: %q", got)
|
||||
}
|
||||
if got := rootName("/Users/me/acme", "/Users/me/acme", ""); got != "acme" {
|
||||
t.Errorf("a folder: %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user