plugin: a plugin added by its path is loaded again when its files change
A plugin added by its path (the user's own folder or file, as its author has it while writing it) was loaded by the host as it was when the host started, and an edit reached it only once magpie was quit and opened again (yansheng1003 on X: "每次改了需要重新magpie"). checkList, which runs on every call to a plugin, now compares the files of every enabled path plugin too (names, times, sizes; node_modules and dot folders left out, 2000 files at most, walked at most once a second), and a change starts the host again as a plugins.json change already did. The middleware already reloaded its own files this way. Run (mote): go test ./internal/plugin -run 'TestPathPluginEdit|TestPathsLookedAt|TestTerminalUpdateRestarts' ok. Broken by hand: checkList back on listStamp alone, and TestPathPluginEditRestartsTheHost is red at "the plugin's own code was edited". The full suite runs with the restart change that follows.
This commit is contained in:
@@ -846,7 +846,7 @@ func Restart() {
|
||||
// the next host loads the plugins as they are now: what this magpie
|
||||
// installed is not another magpie's change (checkList)
|
||||
listSeen.Lock()
|
||||
listSeen.stamp, listSeen.set = listStamp(), true
|
||||
listSeen.stamp, listSeen.set = seenStamp(), true
|
||||
listSeen.Unlock()
|
||||
hostMu.Lock()
|
||||
h := current
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A plugin added by its path (a file or folder of the user's own, as a
|
||||
// plugin's author has it while writing it) is loaded by the host as it
|
||||
// was when the host started. An edit to it went unseen until magpie was
|
||||
// quit and opened again (yansheng1003 on X: "每次改了需要重新magpie"), so
|
||||
// what the host loaded is looked at as plugins.json is (checkList): once
|
||||
// its files change, the host is started again with them, as the
|
||||
// middleware already is (middleware.stamp).
|
||||
|
||||
// pathsEvery is how often the path plugins' files are looked at: checkList
|
||||
// runs on every call to a plugin, and a folder is walked.
|
||||
var pathsEvery = time.Second
|
||||
|
||||
// pathsMax bounds the files looked at in one plugin's folder: a path
|
||||
// plugin pointed at a large tree is seen changing in its first files only.
|
||||
const pathsMax = 2000
|
||||
|
||||
var pathsSeen struct {
|
||||
sync.Mutex
|
||||
at time.Time
|
||||
list string // plugins.json's stamp the paths were found under
|
||||
stamp string
|
||||
}
|
||||
|
||||
// seenStamp is what the host loaded the plugins from: plugins.json and
|
||||
// what is installed (listStamp), and the files of the plugins added by
|
||||
// their path.
|
||||
func seenStamp() string { return listStamp() + "|" + pathsStamp() }
|
||||
|
||||
// pathsStamp is the files of every plugin on in plugins.json that was
|
||||
// added by its path: their names, times and sizes. Walked at most once a
|
||||
// pathsEvery, unless plugins.json changed since.
|
||||
func pathsStamp() string {
|
||||
ls := listStamp()
|
||||
pathsSeen.Lock()
|
||||
defer pathsSeen.Unlock()
|
||||
if pathsSeen.list == ls && time.Since(pathsSeen.at) < pathsEvery {
|
||||
return pathsSeen.stamp
|
||||
}
|
||||
var b strings.Builder
|
||||
for _, e := range list().Plugins {
|
||||
if e.Off || !IsPath(e.Spec) {
|
||||
continue
|
||||
}
|
||||
stampPath(&b, Target(e.Spec))
|
||||
}
|
||||
pathsSeen.at, pathsSeen.list, pathsSeen.stamp = time.Now(), ls, b.String()
|
||||
return pathsSeen.stamp
|
||||
}
|
||||
|
||||
// stampPath writes root's files to b: root itself when it is a file, else
|
||||
// what is under it, its dependencies (node_modules) and hidden folders
|
||||
// (.git) left out.
|
||||
func stampPath(b *strings.Builder, root string) {
|
||||
n := 0
|
||||
_ = filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return nil // unreadable: as it is, nothing to compare
|
||||
}
|
||||
if d.IsDir() {
|
||||
if p != root && (d.Name() == "node_modules" || strings.HasPrefix(d.Name(), ".")) {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if n++; n > pathsMax {
|
||||
return filepath.SkipAll
|
||||
}
|
||||
fi, err := d.Info()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if d.Type()&os.ModeSymlink != 0 {
|
||||
if t, err := os.Stat(p); err == nil {
|
||||
fi = t
|
||||
}
|
||||
}
|
||||
fmt.Fprint(b, p, ":", fi.ModTime().UnixNano(), ":", fi.Size(), ";")
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestPathPluginEditRestartsTheHost (yansheng1003 on X: "每次改了需要重新
|
||||
// magpie"): a plugin added by its path is the user's own code, edited in
|
||||
// place. The host running had loaded it as it was, and an edit reached it
|
||||
// only once magpie was quit and opened again. A change to any of its files
|
||||
// (not its node_modules or .git) now starts the host again; nothing
|
||||
// changed, or a plugin turned off, does not.
|
||||
func TestPathPluginEditRestartsTheHost(t *testing.T) {
|
||||
storeSandbox(t)
|
||||
every := pathsEvery
|
||||
pathsEvery = 0 // every look walks: the test is about what is seen, not how often
|
||||
t.Cleanup(func() { pathsEvery = every })
|
||||
|
||||
dir := filepath.Join(t.TempDir(), "my-plugin")
|
||||
for _, d := range []string{"src", "node_modules/dep", ".git"} {
|
||||
if err := os.MkdirAll(filepath.Join(dir, d), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
then := time.Now().Add(-time.Hour)
|
||||
write := func(rel, body string, at time.Time) {
|
||||
t.Helper()
|
||||
p := filepath.Join(dir, rel)
|
||||
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chtimes(p, at, at); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
write("package.json", `{"main":"src/index.js"}`, then)
|
||||
write("src/index.js", `export default async () => ({})`, then)
|
||||
write("node_modules/dep/index.js", `1`, then)
|
||||
write(".git/HEAD", `ref: refs/heads/main`, then)
|
||||
file := filepath.Join(t.TempDir(), "solo.js")
|
||||
if err := os.WriteFile(file, []byte(`export default async () => ({})`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chtimes(file, then, then); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := json.Marshal(List{Plugins: []Entry{{Spec: dir}, {Spec: "file://" + file}}})
|
||||
writeList(t, string(b))
|
||||
|
||||
prevStale := hostStale.Swap(false)
|
||||
t.Cleanup(func() { hostStale.Store(prevStale) })
|
||||
checkList() // the app has seen the plugins as they are
|
||||
hostStale.Store(false)
|
||||
stale := func() bool { checkList(); return hostStale.Swap(false) }
|
||||
|
||||
if stale() {
|
||||
t.Fatal("nothing changed, and the host is stale")
|
||||
}
|
||||
write("node_modules/dep/index.js", `2`, then.Add(time.Minute))
|
||||
write(".git/HEAD", `ref: refs/heads/other`, then.Add(time.Minute))
|
||||
if stale() {
|
||||
t.Fatal("only its dependencies and .git changed, and the host was started again")
|
||||
}
|
||||
write("src/index.js", `export default async () => ({ v: 2 })`, then.Add(2*time.Minute))
|
||||
if !stale() {
|
||||
t.Fatal("the plugin's own code was edited, and the running host goes on with the old code")
|
||||
}
|
||||
if stale() {
|
||||
t.Fatal("one edit restarted the host twice")
|
||||
}
|
||||
if err := os.WriteFile(file, []byte(`export default async () => ({ v: 2 })`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !stale() {
|
||||
t.Fatal("a plugin that is one file was edited, and the running host goes on with the old code")
|
||||
}
|
||||
|
||||
// this magpie's own restart takes up the edit: no second restart
|
||||
write("src/index.js", `export default async () => ({ v: 3 })`, then.Add(3*time.Minute))
|
||||
Restart()
|
||||
if stale() {
|
||||
t.Fatal("this magpie's own restart was taken for an edit")
|
||||
}
|
||||
|
||||
// an edit to a plugin turned off restarts nothing
|
||||
b, _ = json.Marshal(List{Plugins: []Entry{{Spec: dir, Off: true}}})
|
||||
writeList(t, string(b))
|
||||
stale()
|
||||
write("src/index.js", `export default async () => ({ v: 4 })`, then.Add(4*time.Minute))
|
||||
if stale() {
|
||||
t.Fatal("a plugin turned off was edited, and the host was started again")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPathsLookedAtOnceASecond: checkList runs on every call to a plugin,
|
||||
// and a path plugin's folder is walked; the walk is made again only once
|
||||
// pathsEvery has gone by, or plugins.json changed.
|
||||
func TestPathsLookedAtOnceASecond(t *testing.T) {
|
||||
storeSandbox(t)
|
||||
every := pathsEvery
|
||||
pathsEvery = time.Hour
|
||||
t.Cleanup(func() { pathsEvery = every })
|
||||
dir := t.TempDir()
|
||||
p := filepath.Join(dir, "index.js")
|
||||
if err := os.WriteFile(p, []byte("1"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, _ := json.Marshal(List{Plugins: []Entry{{Spec: dir}}})
|
||||
writeList(t, string(b))
|
||||
first := pathsStamp()
|
||||
at := time.Now().Add(time.Minute)
|
||||
if err := os.WriteFile(p, []byte("22"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = os.Chtimes(p, at, at)
|
||||
if pathsStamp() != first {
|
||||
t.Fatal("the folder was walked again inside pathsEvery")
|
||||
}
|
||||
pathsEvery = 0
|
||||
if pathsStamp() == first {
|
||||
t.Fatal("the edit wasn't seen once pathsEvery went by")
|
||||
}
|
||||
}
|
||||
@@ -601,7 +601,7 @@ func UseCached(ps []Provider) {
|
||||
provMu.Unlock()
|
||||
// as asked with the plugins as they are now
|
||||
listSeen.Lock()
|
||||
listSeen.stamp, listSeen.set = listStamp(), ps != nil
|
||||
listSeen.stamp, listSeen.set = seenStamp(), ps != nil
|
||||
listSeen.Unlock()
|
||||
}
|
||||
|
||||
|
||||
@@ -284,7 +284,7 @@ func save(l List) error {
|
||||
return err
|
||||
}
|
||||
listSeen.Lock()
|
||||
listSeen.stamp, listSeen.set = listStamp(), true
|
||||
listSeen.stamp, listSeen.set = seenStamp(), true
|
||||
listSeen.Unlock()
|
||||
return nil
|
||||
}
|
||||
@@ -316,11 +316,12 @@ func listStamp() string {
|
||||
// running has the old ones loaded.
|
||||
var hostStale atomic.Bool
|
||||
|
||||
// checkList notices plugins.json changed by another magpie: the host is
|
||||
// started again with the plugins as they are, and the providers the other
|
||||
// magpie last saw asked for are the ones known meanwhile.
|
||||
// checkList notices plugins.json changed by another magpie, or a plugin
|
||||
// added by its path edited (pathstamp.go): the host is started again with
|
||||
// the plugins as they are, and the providers the other magpie last saw
|
||||
// asked for are the ones known meanwhile.
|
||||
func checkList() {
|
||||
st := listStamp()
|
||||
st := seenStamp()
|
||||
listSeen.Lock()
|
||||
moved := listSeen.set && listSeen.stamp != st
|
||||
listSeen.stamp, listSeen.set = st, true
|
||||
|
||||
Reference in New Issue
Block a user