Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions desktop/internal/pluginbackend/pluginbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ func (e *SharedProviderError) Error() string {
return fmt.Sprintf("build provider %s shares the %s language-server session", e.BuildSystemID, e.LanguageID)
}

// SessionUnavailableError reports that a plugin server is not running and
// preserves any startup or exit diagnostic recorded for the user.
type SessionUnavailableError struct {
PluginID string
LanguageID string
Detail string
}

func (e *SessionUnavailableError) Error() string {
base := fmt.Sprintf("the %s language server of %s is not running", e.LanguageID, e.PluginID)
if e.Detail != "" {
return base + ": " + e.Detail
}
return base
}

func (e *SessionUnavailableError) Unwrap() error {
return ErrNoSession
}

// Kernel is the part of the Core client this package uses. *core.Client
// satisfies it.
type Kernel interface {
Expand Down
1 change: 1 addition & 0 deletions desktop/internal/settingswindow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type PluginState struct {
// PluginRuntimeState is a memory-only view of the current native session.
type PluginRuntimeState struct {
Running bool
Failed bool
RestartRequired bool
Restarting bool
Version string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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/marketplace"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginbackend"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginsettings"
"github.com/vivaui/viva/app"
Expand Down Expand Up @@ -106,3 +107,72 @@ func TestSettingsActionButtonsConfirmDismissAndPreserveUnsavedInput(t *testing.T
t.Fatalf("action saved preferences: %v", err)
}
}

type unavailableActionSource struct {
settingsFormSource
err error
}

func (s unavailableActionSource) Action(context.Context, pluginservice.SettingsEditor, string, bool) (pluginservice.SettingsActionResult, error) {
return pluginservice.SettingsActionResult{}, s.err
}

func TestSettingsActionUnavailableShowsProviderReasonAndRestartForFailedServer(t *testing.T) {
data, err := os.ReadFile(contractfixture.CoreFixture("plugins/settings-documents.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct{ Manifest map[string]any }
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
fixture.Manifest["settings"] = []any{map[string]any{"id": "count", "type": "integer", "title": "Count", "default": 3, "minimum": 1, "maximum": 10}}
fixture.Manifest["settingActions"] = []any{map[string]any{"id": "read", "title": "Read compiler settings", "function": "compiler.read"}}
manifest, err := json.Marshal(fixture.Manifest)
if err != nil {
t.Fatal(err)
}
workspace := t.TempDir()
source := unavailableActionSource{
settingsFormSource: settingsFormSource{store: pluginsettings.New(core.NewClient(), t.TempDir()), manifest: manifest, workspace: workspace},
err: &pluginbackend.SessionUnavailableError{PluginID: "dev.example.settings", LanguageID: "java", Detail: "missing jdtls"},
}
fetch := func(context.Context) (marketplace.Snapshot, error) {
return marketplace.Snapshot{Index: core.MarketplaceIndex{Plugins: []core.MarketplacePlugin{{ID: "dev.example.settings", Name: "Settings", LatestVersion: "1.0.0"}}}}, nil
}
restartCalls := 0
w := newWindow(NewController(Options{
PluginSettings: source,
Marketplace: fetch, MarketplaceInstalled: fetch,
PluginState: func(string) PluginState { return PluginState{Installed: true} },
InstalledPlugins: func() []PluginFacts {
return []PluginFacts{{ID: "dev.example.settings", Name: "Settings", Version: "1.0.0", Settings: 1}}
},
PluginRuntime: func(string) PluginRuntimeState {
return PluginRuntimeState{Failed: true, RestartRequired: true, Version: "1.0.0", Workspace: workspace}
},
RestartPlugin: func(string, string, string, string) { restartCalls++ },
}), Values{})
w.sess.page = pagePlugins
w.installed.tab = installedSettings
d := openWindow(t, w, func(config *app.Config) { config.Height = 1100 })
words := func(key string) string { return i18n.Shared(i18n.English, "desktop.pluginSettings."+key) }
d.until("the plugin form", func() bool { return w.pluginSettings.loaded })
d.click("Read compiler settings")
d.until("the action failure", func() bool { return w.pluginSettings.failed != nil && !w.pluginSettings.running })
d.pump()

if !d.textContaining("no running provider") {
t.Fatalf("missing provider-unavailable guidance, labels=%+v", d.driver.Snapshot().Labels)
}
if !d.textContaining("missing jdtls") {
t.Fatalf("missing recorded language-server startup detail, labels=%+v", d.driver.Snapshot().Labels)
}
if d.textContaining("Unable to read or save settings") {
t.Fatalf("still displayed misleading generic save failure %q", words("failed"))
}
d.click(words("restart"))
if restartCalls != 1 {
t.Fatalf("restart calls for failed server = %d, want 1", restartCalls)
}
}
6 changes: 5 additions & 1 deletion desktop/internal/settingswindow/plugin_settings_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (w *window) pluginSettingsPanel(c ui.C, id string) {
message = i18n.Shared(language, "desktop.pluginSettings.conflict")
}
ui.Wrap(c, message).FontSize(12).Color(w.pal.danger)
var unavailable *pluginbackend.SessionUnavailableError
if errors.As(p.failed, &unavailable) && unavailable.Detail != "" {
ui.Wrap(c, unavailable.Detail).FontSize(12).Color(w.pal.danger)
}
}
if p.running {
ui.Text(c, i18n.Shared(language, "native.loading")).FontSize(12).Color(w.pal.muted)
Expand All @@ -113,7 +117,7 @@ func (w *window) pluginSettingsPanel(c ui.C, id string) {
runtime := w.ctrl.opts.PluginRuntime(id)
if runtime.RestartRequired || runtime.Restarting {
ui.Wrap(c, i18n.Shared(language, "desktop.pluginSettings.restartRequired")).FontSize(12).Color(w.pal.muted)
if w.ctrl.opts.RestartPlugin != nil && ui.Button(c, i18n.Shared(language, "desktop.pluginSettings.restart")).Disabled(!runtime.Running || runtime.Restarting || p.running || !p.loaded || runtime.Version != p.editor.Snapshot.State.Metadata.Version).Clicked() {
if w.ctrl.opts.RestartPlugin != nil && ui.Button(c, i18n.Shared(language, "desktop.pluginSettings.restart")).Disabled((!runtime.Running && !runtime.Failed) || runtime.Restarting || p.running || !p.loaded || runtime.Version != p.editor.Snapshot.State.Metadata.Version).Clicked() {
scope := "global"
if p.scope == 1 {
scope = "project"
Expand Down
82 changes: 82 additions & 0 deletions desktop/internal/settingswindow/plugin_settings_visual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/i18n"
"github.com/1lck/Lithe-IDEA/desktop/internal/marketplace"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginbackend"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginsettings"
"github.com/vivaui/viva/headless"
)
Expand Down Expand Up @@ -158,3 +160,83 @@ func TestVisualPluginSettingsPage(t *testing.T) {
})
}
}

type visualUnavailableActionSource struct {
settingsFormSource
err error
}

func (s visualUnavailableActionSource) Action(context.Context, pluginservice.SettingsEditor, string, bool) (pluginservice.SettingsActionResult, error) {
return pluginservice.SettingsActionResult{}, s.err
}

func TestVisualPluginSettingsUnavailableAction(t *testing.T) {
out := os.Getenv("LITHE_VISUAL_OUTPUT_DIR")
if out == "" {
t.Skip("set LITHE_VISUAL_OUTPUT_DIR to capture the unavailable plugin settings action")
}
if err := os.MkdirAll(out, 0755); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(contractfixture.CoreFixture("plugins/settings-documents.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct {
Manifest map[string]any `json:"manifest"`
}
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
fixture.Manifest["settings"] = []any{map[string]any{"id": "languageServerFolder", "type": "directory", "title": "Language server folder", "default": "", "page": map[string]any{"languageID": "fixture"}, "effect": "restart"}}
fixture.Manifest["settingActions"] = []any{map[string]any{"id": "read", "title": "Read compiler settings", "function": "compiler.read", "page": map[string]any{"languageID": "fixture"}}}
manifest, err := json.Marshal(fixture.Manifest)
if err != nil {
t.Fatal(err)
}
workspace := t.TempDir()
for _, look := range []struct {
theme string
language i18n.Language
}{{"dark", i18n.Chinese}, {"light", i18n.English}} {
t.Run(look.theme, func(t *testing.T) {
source := visualUnavailableActionSource{
settingsFormSource: settingsFormSource{store: pluginsettings.New(core.NewClient(), t.TempDir()), manifest: manifest, workspace: workspace},
err: &pluginbackend.SessionUnavailableError{PluginID: "dev.example.settings", LanguageID: "fixture", Detail: "resolve the fixture launch: executable not found"},
}
fetch := func(context.Context) (marketplace.Snapshot, error) {
return marketplace.Snapshot{Index: core.MarketplaceIndex{Plugins: []core.MarketplacePlugin{{ID: "dev.example.settings", Name: "Fixture Support", LatestVersion: "1.0.0"}}}}, nil
}
w := newWindow(NewController(Options{
Text: func() *i18n.Messages { return i18n.For(look.language) }, Language: func() i18n.Language { return look.language },
SystemLanguage: look.language, SystemDark: func() bool { return look.theme == "dark" },
Marketplace: fetch, MarketplaceInstalled: fetch, PluginSettings: source,
PluginState: func(string) PluginState { return PluginState{Installed: true, Enabled: true, Version: "1.0.0"} },
InstalledPlugins: func() []PluginFacts {
return []PluginFacts{{ID: "dev.example.settings", Name: "Fixture Support", Version: "1.0.0", Settings: 1}}
},
PluginRuntime: func(string) PluginRuntimeState {
return PluginRuntimeState{Failed: true, RestartRequired: true, Version: "1.0.0", Workspace: workspace}
},
RestartPlugin: func(string, string, string, string) {},
}), Values{Theme: look.theme, Language: look.language.Code()})
w.sess.page = pagePlugins
w.installed.tab = installedSettings
config := w.config()
config.Height = 760
session, err := headless.Open(config, nil, headless.Options{})
if err != nil {
t.Fatal(err)
}
t.Cleanup(session.Close)
d := &windowDriver{t: t, w: w, driver: session.Driver()}
d.until("the plugin form", func() bool { return w.pluginSettings.loaded })
d.click("Read compiler settings")
d.until("the action failure", func() bool { return w.pluginSettings.failed != nil && !w.pluginSettings.running })
d.pump()
if err := session.Screenshot(filepath.Join(out, "plugin-settings-unavailable-"+look.theme+".png")); err != nil {
t.Fatal(err)
}
})
}
}
12 changes: 11 additions & 1 deletion desktop/internal/workbench/debug_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/1lck/Lithe-IDEA/desktop/internal/buildprocess"
"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/pluginhost"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginpkg"
"github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice"
Expand Down Expand Up @@ -895,14 +896,23 @@ func (a *App) waitReadyPluginServer(ctx context.Context, p *project, pluginID, l
a.pluginServerMu.Lock()
server := a.pluginServers[pluginID]
starting := a.pluginServerStarting[languageID]
failed := a.pluginServerFailed[languageID]
a.pluginServerMu.Unlock()
switch {
case server != nil && server.project == p && server.isReady():
return server, nil
case server == nil && !starting:
idle++
if idle >= 3 {
return nil, fmt.Errorf("the %s language server of %s is not running", languageID, pluginID)
detail := ""
if failed != nil && failed.project == p {
detail = failed.message
}
return nil, &pluginbackend.SessionUnavailableError{
PluginID: pluginID,
LanguageID: languageID,
Detail: detail,
}
}
default:
idle = 0
Expand Down
1 change: 0 additions & 1 deletion desktop/internal/workbench/pluginbackends.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,6 @@ func (a *App) retryFailedPluginServers() {
seen := make(map[string]bool)
var languages []string
a.pluginServerMu.Lock()
a.pluginServerNotifiedErrors = nil
for lang, failed := range a.pluginServerFailed {
if failed != nil && failed.project == a.project && !seen[lang] {
seen[lang] = true
Expand Down
110 changes: 110 additions & 0 deletions desktop/internal/workbench/pluginbackends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package workbench

import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"

"github.com/1lck/Lithe-IDEA/desktop/internal/buildprovider"
"github.com/1lck/Lithe-IDEA/desktop/internal/contractfixture"
"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"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/store"
"github.com/vivaui/viva/ui"
)

Expand Down Expand Up @@ -171,3 +179,105 @@ func TestRetryFailedLSPServerClearsFailureState(t *testing.T) {
t.Fatalf("retryFailedLSPServer did not clear failure state: failed=%v notified=%v", h.app.pluginServerFailed, h.app.pluginServerNotifiedErrors)
}
}

type failingLSPPlanKernel struct {
*testKernel
mu sync.Mutex
msg string
}

func (k *failingLSPPlanKernel) PlanPluginLanguageServer(context.Context, json.RawMessage, string, core.PluginBackendDirectories) (core.PluginBackendPlan, error) {
k.mu.Lock()
msg := k.msg
k.mu.Unlock()
return core.PluginBackendPlan{}, errors.New(msg)
}

func TestRetryFailedPluginServersPreservesNotifiedErrorDeduplication(t *testing.T) {
kernel := &failingLSPPlanKernel{testKernel: &testKernel{Client: core.NewClient()}, msg: "missing jdtls"}
h := newHarness(t, kernel)
preferences, plugins := t.TempDir(), t.TempDir()
raw, err := os.ReadFile(contractfixture.CoreFixture("plugins/settings-documents.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct {
Manifest json.RawMessage `json:"manifest"`
}
if err := json.Unmarshal(raw, &fixture); err != nil {
t.Fatal(err)
}
dir := filepath.Join(plugins, "dev.example.settings", "1.0.0")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, pluginpkg.ManifestFile), fixture.Manifest, 0o600); err != nil {
t.Fatal(err)
}
service, err := pluginservice.Open(kernel, store.At(preferences), plugins)
if err != nil {
t.Fatal(err)
}
if err := service.Manager.SetEnabled("dev.example.settings", true); err != nil {
t.Fatal(err)
}
h.app.plugins = service
h.open(t.TempDir())

h.app.startPluginServer("fixture")
awaitStateWithin(t, h, 5*time.Second, "initial lsp failure", func() bool {
h.app.pluginServerMu.Lock()
defer h.app.pluginServerMu.Unlock()
return !h.app.pluginServerStarting["fixture"] && h.app.pluginServerFailed["fixture"] != nil
})
if len(h.app.notifications.list) != 1 {
t.Fatalf("initial notifications = %d, want 1", len(h.app.notifications.list))
}

// Retrying after a toolchain save must not spam the exact same error toast again.
h.app.retryFailedPluginServers()
awaitStateWithin(t, h, 5*time.Second, "retried lsp failure", func() bool {
h.app.pluginServerMu.Lock()
defer h.app.pluginServerMu.Unlock()
return !h.app.pluginServerStarting["fixture"] && h.app.pluginServerFailed["fixture"] != nil
})
if len(h.app.notifications.list) != 1 || h.app.notifications.list[0].count != 1 {
t.Fatalf("notifications after identical retry failure = %+v, want 1 with count=1 (deduplicated)", h.app.notifications.list)
}

// A new failure message after changing toolchains must still notify the user.
kernel.mu.Lock()
kernel.msg = "bad jdk version"
kernel.mu.Unlock()
h.app.retryFailedPluginServers()
awaitStateWithin(t, h, 5*time.Second, "updated lsp failure", func() bool {
h.app.pluginServerMu.Lock()
defer h.app.pluginServerMu.Unlock()
failed := h.app.pluginServerFailed["fixture"]
return !h.app.pluginServerStarting["fixture"] && failed != nil && strings.Contains(failed.message, "bad jdk version")
})
if len(h.app.notifications.list) != 2 {
t.Fatalf("notifications after new failure = %d, want 2: %+v", len(h.app.notifications.list), h.app.notifications.list)
}
}

func TestWaitReadyPluginServerWrapsErrNoSession(t *testing.T) {
h := newHarness(t, &testKernel{Client: core.NewClient()})
h.open(t.TempDir())
p := h.app.project
h.app.pluginServerMu.Lock()
h.app.pluginServerFailed = map[string]*failedPluginServer{
"java": {pluginID: "dev.lithe.test.lsp", languageID: "java", project: p, message: "missing jdtls"},
}
h.app.pluginServerMu.Unlock()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := h.app.waitReadyPluginServer(ctx, p, "dev.lithe.test.lsp", "java")
if !errors.Is(err, pluginbackend.ErrNoSession) {
t.Fatalf("waitReadyPluginServer err = %v, want errors.Is(_, pluginbackend.ErrNoSession)", err)
}
if err == nil || !strings.Contains(err.Error(), "missing jdtls") {
t.Fatalf("waitReadyPluginServer err = %v, want recorded detail %q", err, "missing jdtls")
}
}
Loading
Loading