Skip to content
15 changes: 14 additions & 1 deletion desktop/internal/pluginpkg/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ const (
StateDirectory = ".state"
)

// StateRoot returns the writable state directory of one installed plugin.
// It is outside every installed version, and RemoveState owns its cleanup.
// The caller must supply a validated plugin identity.
func StateRoot(root, pluginID string) string {
return filepath.Join(root, StateDirectory, pluginID)
}

// VersionStateRoot returns the writable state directory of one plugin version.
// The caller must supply validated plugin and version identities.
func VersionStateRoot(root, pluginID, version string) string {
return filepath.Join(StateRoot(root, pluginID), version)
}

// Installed is one plugin package version present in the root.
type Installed struct {
// ID is the plugin identifier the package was installed under.
Expand Down Expand Up @@ -172,7 +185,7 @@ func RemoveState(root, id string) error {
if err := CheckIdentity("plugin ID", id); err != nil {
return err
}
target := filepath.Join(root, StateDirectory, id)
target := StateRoot(root, id)
switch info, err := os.Lstat(target); {
case errors.Is(err, fs.ErrNotExist):
return nil
Expand Down
23 changes: 23 additions & 0 deletions desktop/internal/pluginpkg/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ func TestInstallRefusesRelativePaths(t *testing.T) {
}
}

func TestStateRootsFollowRemovalLayout(t *testing.T) {
root := t.TempDir()
const id = "dev.lithe.test.sample"
const version = "1.2.3"
wantRoot := filepath.Join(root, StateDirectory, id)
if got := StateRoot(root, id); got != wantRoot {
t.Fatalf("StateRoot = %q, want %q", got, wantRoot)
}
wantVersion := filepath.Join(wantRoot, version)
if got := VersionStateRoot(root, id, version); got != wantVersion {
t.Fatalf("VersionStateRoot = %q, want %q", got, wantVersion)
}
if err := os.MkdirAll(wantVersion, 0o700); err != nil {
t.Fatal(err)
}
if err := RemoveState(root, id); err != nil {
t.Fatalf("RemoveState = %v", err)
}
if _, err := os.Stat(wantVersion); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("version state survived removal: %v", err)
}
}

func TestUninstallRemovesVersionAndEmptyID(t *testing.T) {
root := t.TempDir()
if _, err := Install(root, "example", "1.0.0", newStaging(t, root)); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion desktop/internal/pluginservice/backendhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func (d pluginDirectories) create() error {
// The log directory is the log session's, which the log store has already created.
func (s *Service) backendDirectories(ctx context.Context, pluginID, version string) pluginDirectories {
return pluginDirectories{
state: filepath.Join(s.Root, pluginStateDirectory, pluginID, version),
state: pluginpkg.VersionStateRoot(s.Root, pluginID, version),
log: s.logDirectory(ctx, pluginID),
}
}
Expand Down
3 changes: 2 additions & 1 deletion desktop/internal/pluginservice/backendlaunch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginbackend"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginpkg"
)

// backendLaunch is a decoded backend-resolved launch and the directories it uses.
Expand All @@ -32,7 +33,7 @@ func (s *Service) resolveBackendLaunch(ctx context.Context, pluginID, version st
if workspace.ContextID == "" {
return backendLaunch{}, fmt.Errorf("%w: a backend-resolved server needs an open workspace", pluginbackend.ErrRequest)
}
base := filepath.Join(s.Root, pluginStateDirectory, pluginID)
base := pluginpkg.StateRoot(s.Root, pluginID)
temporary, err := os.MkdirTemp("", "lithe-server-")
if err != nil {
return backendLaunch{}, err
Expand Down
7 changes: 1 addition & 6 deletions desktop/internal/pluginservice/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *Service) StartLanguageServer(ctx context.Context, entries []pluginmanag
// State lives beside the installed versions, never inside one: an installed
// package is read-only for the host, and a version directory that changed
// would no longer match the digest the user approved.
stateDirectory := filepath.Join(s.Root, pluginStateDirectory, entry.Installed.ID, entry.Installed.Version)
stateDirectory := pluginpkg.VersionStateRoot(s.Root, entry.Installed.ID, entry.Installed.Version)
if stateErr := os.MkdirAll(stateDirectory, 0o700); stateErr != nil {
return fail(stateErr)
}
Expand Down Expand Up @@ -177,11 +177,6 @@ func (s *Service) StopLanguageServer(ctx context.Context, session *LanguageSessi
return s.Backend.Stop(ctx, session.Session)
}

// pluginStateDirectory is the reserved directory below the plugin root where
// running servers keep their state. It starts with a dot, so List never reads it
// as an installed plugin; uninstalling a plugin removes its part of it.
const pluginStateDirectory = pluginpkg.StateDirectory

type languageServerDeclaration struct {
LanguageServers []struct {
LanguageID string `json:"languageID"`
Expand Down
3 changes: 2 additions & 1 deletion desktop/internal/pluginservice/hostmethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginhost"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginpkg"
)

// Host service grants and methods this client answers. A launch plan lists
Expand Down Expand Up @@ -128,7 +129,7 @@ func (s *Service) contextJSON(pluginID string, workspace WorkspaceContext) conte
// plugin package and the application bundle. It is created on first use here and
// nowhere else, so listing contexts leaves no directories behind.
func (s *Service) contextState(pluginID string, workspace WorkspaceContext) (string, error) {
directory := filepath.Join(s.Root, pluginStateDirectory, pluginID, "backend", "workspaces", WorkspaceKey(workspace.Path))
directory := filepath.Join(pluginpkg.StateRoot(s.Root, pluginID), "backend", "workspaces", WorkspaceKey(workspace.Path))
if err := os.MkdirAll(directory, 0o700); err != nil {
return "", err
}
Expand Down
5 changes: 3 additions & 2 deletions desktop/internal/pluginservice/hostmethods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/1lck/Lithe-IDEA/desktop/internal/credentials"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginbackend"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginhost"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginpkg"
"github.com/1lck/Lithe-IDEA/desktop/internal/store"
"github.com/1lck/Lithe-IDEA/desktop/internal/winpath"
)
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestWorkspaceContextsAreScopedToTheCallingPlugin(t *testing.T) {
if listed.Contexts[0].StateDirectory != "" {
t.Fatal("listing contexts created or revealed a state directory")
}
if entries, _ := os.ReadDir(filepath.Join(service.Root, pluginStateDirectory, "a")); len(entries) != 0 {
if entries, _ := os.ReadDir(pluginpkg.StateRoot(service.Root, "a")); len(entries) != 0 {
t.Fatalf("listing left directories behind: %v", entries)
}
rootA := listed.Contexts[0].Folders[0].RootID
Expand All @@ -94,7 +95,7 @@ func TestWorkspaceContextsAreScopedToTheCallingPlugin(t *testing.T) {
StateDirectory string `json:"stateDirectory"`
}
_ = json.Unmarshal(result, &detailed)
pluginRoot := filepath.Join(service.Root, pluginStateDirectory) + string(filepath.Separator)
pluginRoot := filepath.Join(service.Root, pluginpkg.StateDirectory) + string(filepath.Separator)
if !strings.HasPrefix(detailed.StateDirectory, pluginRoot) {
t.Fatalf("state directory %q is outside the plugin storage %q", detailed.StateDirectory, pluginRoot)
}
Expand Down
4 changes: 2 additions & 2 deletions desktop/internal/pluginservice/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func runningBackend(service *Service, id string, stopped func()) *BackendSession
// the backend cannot write the state again; another plugin's backend runs on.
func TestUninstallStopsTheBackendBeforeRemovingThePlugin(t *testing.T) {
service, root := lifecycleService(t)
state := filepath.Join(root, pluginStateDirectory, "example", "1.0.0", "cache")
state := filepath.Join(pluginpkg.VersionStateRoot(root, "example", "1.0.0"), "cache")
if err := os.MkdirAll(state, 0o700); err != nil {
t.Fatal(err)
}
Expand All @@ -76,7 +76,7 @@ func TestUninstallStopsTheBackendBeforeRemovingThePlugin(t *testing.T) {
if service.Host.Running("other") == nil {
t.Fatal("the other plugin's backend stopped")
}
if _, err := os.Stat(filepath.Join(root, pluginStateDirectory, "example")); !errors.Is(err, os.ErrNotExist) {
if _, err := os.Stat(pluginpkg.StateRoot(root, "example")); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("the state survived: %v", err)
}
}
Expand Down
4 changes: 3 additions & 1 deletion desktop/internal/pluginservice/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"path/filepath"
"time"

"github.com/1lck/Lithe-IDEA/desktop/internal/pluginpkg"
)

// Plugin web pages (`ui` 2) reach a plugin's backend and keep a small state
Expand Down Expand Up @@ -40,7 +42,7 @@ func (s *Service) CallFromPage(ctx context.Context, pluginID, function string, p
// bundle. The window ID is a digest, so it is one path component.
func (s *Service) pageStatePath(pluginID, windowID, workspacePath string) string {
sum := sha256.Sum256([]byte(windowID))
return filepath.Join(s.Root, pluginStateDirectory, pluginID, "pages", "workspaces", WorkspaceKey(workspacePath), hex.EncodeToString(sum[:])[:24]+".json")
return filepath.Join(pluginpkg.StateRoot(s.Root, pluginID), "pages", "workspaces", WorkspaceKey(workspacePath), hex.EncodeToString(sum[:])[:24]+".json")
}

// PageState returns the stored state of a window, nil when there is none.
Expand Down
4 changes: 2 additions & 2 deletions desktop/internal/pluginservice/pluginservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func open(kernel Kernel, preferences *store.Store, root string, first bool) (*Se
Manager: pluginmanager.New(kernel, root, approvals, state),
Installer: plugininstall.New(kernel, NewReviewer(kernel), approvals, pluginhost.Declared()),
Backend: pluginbackend.New(kernel, hostServices),
Commands: &pluginhost.CommandStore{State: filepath.Join(root, pluginStateDirectory)},
Commands: &pluginhost.CommandStore{State: filepath.Join(root, pluginpkg.StateDirectory)},
Settings: pluginsettings.New(kernel, preferences.Dir()),
HostServices: hostServices,
Credentials: credentials.Default(),
Expand All @@ -187,7 +187,7 @@ func open(kernel Kernel, preferences *store.Store, root string, first bool) (*Se
service.Backend.SetCommandStore(service.Commands)
service.Host = newBackendHost(service)
service.Runs = newRuns(service)
service.Contexts = newWorkspaceContexts(filepath.Join(root, pluginStateDirectory))
service.Contexts = newWorkspaceContexts(filepath.Join(root, pluginpkg.StateDirectory))
service.Contexts.SetObserver(service.contextsChanged)
service.Contexts.SetClosedObserver(service.resources.releaseContext)
if err := service.registerHostServices(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion desktop/internal/workbench/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/i18n"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginbackend"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginpkg"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice"
"github.com/1lck/Lithe-IDEA/desktop/internal/processledger"
"github.com/1lck/Lithe-IDEA/desktop/internal/toolchain"
Expand Down Expand Up @@ -346,7 +347,7 @@ func workspaceDigest(root string) string {
}

func buildStateDirectory(pluginRoot, workspace string, instance buildInstance) string {
return filepath.Join(pluginRoot, ".state", instance.PluginID, instance.Version, "build", workspaceDigest(workspace))
return filepath.Join(pluginpkg.VersionStateRoot(pluginRoot, instance.PluginID, instance.Version), "build", workspaceDigest(workspace))
}

// buildProvider starts a provider process, or attaches to the language server
Expand Down
3 changes: 2 additions & 1 deletion desktop/internal/workbench/debug_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/i18n"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginhost"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginpkg"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice"
"github.com/vivaui/viva/ui"
)
Expand Down Expand Up @@ -460,7 +461,7 @@ func (a *App) beginDebug(p *project, start debugStart) {
if start.parent != nil {
parentSessionID = start.parent.id
}
stateRoot := filepath.Join(pluginRoot, ".state", adapter.PluginID, adapter.Version)
stateRoot := pluginpkg.VersionStateRoot(pluginRoot, adapter.PluginID, adapter.Version)
// Only a stdio adapter is written through its standard input; the other
// transports are written through their TCP stream.
var stdinReader *os.File
Expand Down
3 changes: 2 additions & 1 deletion desktop/internal/workbench/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/i18n"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginbackend"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginpkg"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice"
"github.com/vivaui/viva/ui"
)
Expand Down Expand Up @@ -388,7 +389,7 @@ func (r *vcsRuntime) session(ctx context.Context, runner *pluginbackend.Runner,
if client != nil && !client.Closed() {
return client, nil
}
state := filepath.Join(pluginRoot, ".state", window.PluginID, window.Version, "vcs", workspaceDigest(root))
state := filepath.Join(pluginpkg.VersionStateRoot(pluginRoot, window.PluginID, window.Version), "vcs", workspaceDigest(root))
workspaceState := filepath.Join(state, "workspace")
temporaryDirectory := filepath.Join(state, "tmp")
for _, directory := range []string{state, workspaceState, temporaryDirectory} {
Expand Down
Loading
Loading