fix: ignore state-only extension directories during discovery
- commit
- b0db349cb0e01ea08ab4439cc417789fafdc62b4
- author
- patriceckhart · · 2026-09-15 16:16 UTC
- parent
- 035a5cd
- signature
- unsigned
4 files · +89 -0
M docs/extensions.md +10 -0
| @@ -822,6 +822,16 @@ name), the command reports the matching paths and makes no changes. Use a | |||
| 822 | 822 | unique directory name to disambiguate where possible. `logs` uses the | |
| 823 | 823 | manifest name. | |
| 824 | 824 | ||
| 825 | + | `remove` deletes the installation directory, including state stored inside | |
| 826 | + | it. It does not stop extensions in other running zot sessions. Close those | |
| 827 | + | sessions before uninstalling, otherwise an extension may recreate its state | |
| 828 | + | directory when saving or shutting down. Normal startup ignores directories | |
| 829 | + | without `extension.json`, including state-only leftovers. It does not delete | |
| 830 | + | that leftover data. Explicit `--ext` paths still report missing manifests, | |
| 831 | + | and malformed or unreadable manifests still produce errors. After closing | |
| 832 | + | active sessions, inspect and manually remove any leftover state directory | |
| 833 | + | if needed. `ext remove` requires an installation with a manifest. | |
| 834 | + | ||
| 825 | 835 | `zot ext doctor` runs the same discovery path as zot startup, but reports | |
| 826 | 836 | what happened instead of changing the fail-soft runtime behavior. It shows | |
| 827 | 837 | manifest errors, disabled or shadowed extensions, subprocess load errors, | |
| @@ -822,6 +822,16 @@ name), the command reports the matching paths and makes no changes. Use a | |||||
| 822 | unique directory name to disambiguate where possible. `logs` uses the | 822 | unique directory name to disambiguate where possible. `logs` uses the | ||
| 823 | manifest name. | 823 | manifest name. | ||
| 824 | 824 | ||||
| 825 | + | `remove` deletes the installation directory, including state stored inside | |||
| 826 | + | it. It does not stop extensions in other running zot sessions. Close those | |||
| 827 | + | sessions before uninstalling, otherwise an extension may recreate its state | |||
| 828 | + | directory when saving or shutting down. Normal startup ignores directories | |||
| 829 | + | without `extension.json`, including state-only leftovers. It does not delete | |||
| 830 | + | that leftover data. Explicit `--ext` paths still report missing manifests, | |||
| 831 | + | and malformed or unreadable manifests still produce errors. After closing | |||
| 832 | + | active sessions, inspect and manually remove any leftover state directory | |||
| 833 | + | if needed. `ext remove` requires an installation with a manifest. | |||
| 834 | + | ||||
| 825 | `zot ext doctor` runs the same discovery path as zot startup, but reports | 835 | `zot ext doctor` runs the same discovery path as zot startup, but reports | ||
| 826 | what happened instead of changing the fail-soft runtime behavior. It shows | 836 | what happened instead of changing the fail-soft runtime behavior. It shows | ||
| 827 | manifest errors, disabled or shadowed extensions, subprocess load errors, | 837 | manifest errors, disabled or shadowed extensions, subprocess load errors, | ||
A packages/agent/extensions/discovery_orphan_test.go +69 -0
| @@ -0,0 +1,69 @@ | |||
| 1 | + | package extensions | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "context" | |
| 5 | + | "os" | |
| 6 | + | "path/filepath" | |
| 7 | + | "strings" | |
| 8 | + | "testing" | |
| 9 | + | "time" | |
| 10 | + | ) | |
| 11 | + | ||
| 12 | + | func TestDiscoverIgnoresStateOnlyDirectories(t *testing.T) { | |
| 13 | + | for _, globalInstalled := range []bool{false, true} { | |
| 14 | + | name := "orphan-only" | |
| 15 | + | if globalInstalled { | |
| 16 | + | name = "orphan-does-not-shadow-global" | |
| 17 | + | } | |
| 18 | + | t.Run(name, func(t *testing.T) { | |
| 19 | + | home := t.TempDir() | |
| 20 | + | project := t.TempDir() | |
| 21 | + | orphan := filepath.Join(project, ".zot", "extensions", "zot-loop") | |
| 22 | + | writeDiscoveryFile(t, orphan, "state.json", `{}`) | |
| 23 | + | global := filepath.Join(home, "extensions", "zot-loop") | |
| 24 | + | if globalInstalled { | |
| 25 | + | writeDiscoveryFile(t, global, "extension.json", `{"name":"loop"}`) | |
| 26 | + | writeDiscoveryFile(t, global, "theme.json", `{"name":"Loop Theme","colors":{"dark":{"accent":204}}}`) | |
| 27 | + | } else { | |
| 28 | + | writeDiscoveryFile(t, filepath.Join(home, "extensions", "other-orphan"), "state.json", `{}`) | |
| 29 | + | } | |
| 30 | + | mgr := New(home, project, "test", "", "", nil) | |
| 31 | + | t.Cleanup(func() { mgr.Stop(time.Second) }) | |
| 32 | + | if errs := mgr.Discover(context.Background()); len(errs) != 0 { | |
| 33 | + | t.Fatalf("discover errors: %v", errs) | |
| 34 | + | } | |
| 35 | + | if globalInstalled { | |
| 36 | + | opts := mgr.ThemeOptions() | |
| 37 | + | if len(opts) != 1 || opts[0].Path != filepath.Join(global, "theme.json") { | |
| 38 | + | t.Fatalf("global extension was shadowed: %#v", opts) | |
| 39 | + | } | |
| 40 | + | } | |
| 41 | + | if _, err := os.Stat(filepath.Join(orphan, "state.json")); err != nil { | |
| 42 | + | t.Fatalf("discovery must not delete leftover state: %v", err) | |
| 43 | + | } | |
| 44 | + | if errs := mgr.LoadExplicit(context.Background(), []string{orphan}); len(errs) != 1 || !strings.Contains(errs[0].Error(), "extension.json") { | |
| 45 | + | t.Fatalf("explicit missing manifest errors: %v", errs) | |
| 46 | + | } | |
| 47 | + | }) | |
| 48 | + | } | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | func TestDiscoverStillReportsMalformedManifest(t *testing.T) { | |
| 52 | + | home := t.TempDir() | |
| 53 | + | writeDiscoveryFile(t, filepath.Join(home, "extensions", "broken"), "extension.json", `{`) | |
| 54 | + | mgr := New(home, "", "test", "", "", nil) | |
| 55 | + | t.Cleanup(func() { mgr.Stop(time.Second) }) | |
| 56 | + | if errs := mgr.Discover(context.Background()); len(errs) != 1 || !strings.Contains(errs[0].Error(), "parse manifest") { | |
| 57 | + | t.Fatalf("malformed manifest errors: %v", errs) | |
| 58 | + | } | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | func writeDiscoveryFile(t *testing.T, dir, name, content string) { | |
| 62 | + | t.Helper() | |
| 63 | + | if err := os.MkdirAll(dir, 0o755); err != nil { | |
| 64 | + | t.Fatal(err) | |
| 65 | + | } | |
| 66 | + | if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { | |
| 67 | + | t.Fatal(err) | |
| 68 | + | } | |
| 69 | + | } | |
| @@ -0,0 +1,69 @@ | |||||
| 1 | + | package extensions | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "context" | |||
| 5 | + | "os" | |||
| 6 | + | "path/filepath" | |||
| 7 | + | "strings" | |||
| 8 | + | "testing" | |||
| 9 | + | "time" | |||
| 10 | + | ) | |||
| 11 | + | ||||
| 12 | + | func TestDiscoverIgnoresStateOnlyDirectories(t *testing.T) { | |||
| 13 | + | for _, globalInstalled := range []bool{false, true} { | |||
| 14 | + | name := "orphan-only" | |||
| 15 | + | if globalInstalled { | |||
| 16 | + | name = "orphan-does-not-shadow-global" | |||
| 17 | + | } | |||
| 18 | + | t.Run(name, func(t *testing.T) { | |||
| 19 | + | home := t.TempDir() | |||
| 20 | + | project := t.TempDir() | |||
| 21 | + | orphan := filepath.Join(project, ".zot", "extensions", "zot-loop") | |||
| 22 | + | writeDiscoveryFile(t, orphan, "state.json", `{}`) | |||
| 23 | + | global := filepath.Join(home, "extensions", "zot-loop") | |||
| 24 | + | if globalInstalled { | |||
| 25 | + | writeDiscoveryFile(t, global, "extension.json", `{"name":"loop"}`) | |||
| 26 | + | writeDiscoveryFile(t, global, "theme.json", `{"name":"Loop Theme","colors":{"dark":{"accent":204}}}`) | |||
| 27 | + | } else { | |||
| 28 | + | writeDiscoveryFile(t, filepath.Join(home, "extensions", "other-orphan"), "state.json", `{}`) | |||
| 29 | + | } | |||
| 30 | + | mgr := New(home, project, "test", "", "", nil) | |||
| 31 | + | t.Cleanup(func() { mgr.Stop(time.Second) }) | |||
| 32 | + | if errs := mgr.Discover(context.Background()); len(errs) != 0 { | |||
| 33 | + | t.Fatalf("discover errors: %v", errs) | |||
| 34 | + | } | |||
| 35 | + | if globalInstalled { | |||
| 36 | + | opts := mgr.ThemeOptions() | |||
| 37 | + | if len(opts) != 1 || opts[0].Path != filepath.Join(global, "theme.json") { | |||
| 38 | + | t.Fatalf("global extension was shadowed: %#v", opts) | |||
| 39 | + | } | |||
| 40 | + | } | |||
| 41 | + | if _, err := os.Stat(filepath.Join(orphan, "state.json")); err != nil { | |||
| 42 | + | t.Fatalf("discovery must not delete leftover state: %v", err) | |||
| 43 | + | } | |||
| 44 | + | if errs := mgr.LoadExplicit(context.Background(), []string{orphan}); len(errs) != 1 || !strings.Contains(errs[0].Error(), "extension.json") { | |||
| 45 | + | t.Fatalf("explicit missing manifest errors: %v", errs) | |||
| 46 | + | } | |||
| 47 | + | }) | |||
| 48 | + | } | |||
| 49 | + | } | |||
| 50 | + | ||||
| 51 | + | func TestDiscoverStillReportsMalformedManifest(t *testing.T) { | |||
| 52 | + | home := t.TempDir() | |||
| 53 | + | writeDiscoveryFile(t, filepath.Join(home, "extensions", "broken"), "extension.json", `{`) | |||
| 54 | + | mgr := New(home, "", "test", "", "", nil) | |||
| 55 | + | t.Cleanup(func() { mgr.Stop(time.Second) }) | |||
| 56 | + | if errs := mgr.Discover(context.Background()); len(errs) != 1 || !strings.Contains(errs[0].Error(), "parse manifest") { | |||
| 57 | + | t.Fatalf("malformed manifest errors: %v", errs) | |||
| 58 | + | } | |||
| 59 | + | } | |||
| 60 | + | ||||
| 61 | + | func writeDiscoveryFile(t *testing.T, dir, name, content string) { | |||
| 62 | + | t.Helper() | |||
| 63 | + | if err := os.MkdirAll(dir, 0o755); err != nil { | |||
| 64 | + | t.Fatal(err) | |||
| 65 | + | } | |||
| 66 | + | if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { | |||
| 67 | + | t.Fatal(err) | |||
| 68 | + | } | |||
| 69 | + | } | |||
M packages/agent/extensions/manager.go +7 -0
| @@ -223,6 +223,13 @@ func (m *Manager) Discover(ctx context.Context) []error { | |||
| 223 | 223 | continue // missing directory is fine | |
| 224 | 224 | } | |
| 225 | 225 | for _, extDir := range extDirs { | |
| 226 | + | // A running extension can recreate its state directory after | |
| 227 | + | // uninstall. Ignore directories without a manifest before they | |
| 228 | + | // can shadow a valid installation in a lower-priority root. | |
| 229 | + | // Other errors still go through loadOne for diagnostics. | |
| 230 | + | if _, err := os.Stat(filepath.Join(extDir, "extension.json")); os.IsNotExist(err) { | |
| 231 | + | continue | |
| 232 | + | } | |
| 226 | 233 | name := filepath.Base(extDir) | |
| 227 | 234 | if seenDirs[name] { | |
| 228 | 235 | continue // higher-priority location already queued | |
| @@ -223,6 +223,13 @@ func (m *Manager) Discover(ctx context.Context) []error { | |||||
| 223 | continue // missing directory is fine | 223 | continue // missing directory is fine | ||
| 224 | } | 224 | } | ||
| 225 | for _, extDir := range extDirs { | 225 | for _, extDir := range extDirs { | ||
| 226 | + | // A running extension can recreate its state directory after | |||
| 227 | + | // uninstall. Ignore directories without a manifest before they | |||
| 228 | + | // can shadow a valid installation in a lower-priority root. | |||
| 229 | + | // Other errors still go through loadOne for diagnostics. | |||
| 230 | + | if _, err := os.Stat(filepath.Join(extDir, "extension.json")); os.IsNotExist(err) { | |||
| 231 | + | continue | |||
| 232 | + | } | |||
| 226 | name := filepath.Base(extDir) | 233 | name := filepath.Base(extDir) | ||
| 227 | if seenDirs[name] { | 234 | if seenDirs[name] { | ||
| 228 | continue // higher-priority location already queued | 235 | continue // higher-priority location already queued | ||
M packages/agent/extresolve_test.go +3 -0
| @@ -38,6 +38,9 @@ func TestFindExtensionDir(t *testing.T) { | |||
| 38 | 38 | if err := extToggle([]string{"todo"}, true); err != nil { | |
| 39 | 39 | t.Fatal(err) | |
| 40 | 40 | } | |
| 41 | + | if err := os.WriteFile(filepath.Join(dir, "state.json"), []byte(`{}`), 0o644); err != nil { | |
| 42 | + | t.Fatal(err) | |
| 43 | + | } | |
| 41 | 44 | if err := extRemove([]string{"todo", "--yes"}); err != nil { | |
| 42 | 45 | t.Fatal(err) | |
| 43 | 46 | } | |
| @@ -38,6 +38,9 @@ func TestFindExtensionDir(t *testing.T) { | |||||
| 38 | if err := extToggle([]string{"todo"}, true); err != nil { | 38 | if err := extToggle([]string{"todo"}, true); err != nil { | ||
| 39 | t.Fatal(err) | 39 | t.Fatal(err) | ||
| 40 | } | 40 | } | ||
| 41 | + | if err := os.WriteFile(filepath.Join(dir, "state.json"), []byte(`{}`), 0o644); err != nil { | |||
| 42 | + | t.Fatal(err) | |||
| 43 | + | } | |||
| 41 | if err := extRemove([]string{"todo", "--yes"}); err != nil { | 44 | if err := extRemove([]string{"todo", "--yes"}); err != nil { | ||
| 42 | t.Fatal(err) | 45 | t.Fatal(err) | ||
| 43 | } | 46 | } | ||