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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Agent 笔记:同一语言只由一个已启用插件提供语言服务器

状态:已实现

## 先说结论

Java 现在有两个插件:Java Support(JDT LS,补全、诊断、运行、调试都有,安装包约 250 MiB)和 Java Support Lite(原名 Java Navigation,插件 ID 仍为 `io.github.lithe-idea.java-navigation`;不依赖 JDT,只做跳转、引用和文档结构,安装包约 1 MiB)。两个都为 `java` 声明了语言服务器,同时启用时宿主会静默选中其中一个,另一个形同失效。现在规定:同一语言只由一个已启用插件提供语言服务器。启用或安装其中一个,另一个会被自动停用,宿主告诉用户停用了谁、为什么。两个插件作为同一语言的“完整版”和“轻量版”二选一,不分工合作。

## 问题

- Core 的清单校验只禁止**同一个插件**为一门语言声明两个语言服务器,对不同插件之间没有任何规则。
- Viva 宿主的 `pluginservice.Service.LanguageServerProvider` 按已安装插件的顺序取第一个声明了该语言服务器的已启用插件,不报错也不提示。它的 ID(`…java-navigation`)按字母排在 Java Support 前面,装上它会让 Java Support 的 JDT 根本不启动,补全、诊断消失,调试一直等 JDT 就绪。
- 轻量版的 README 只能写“暂时不要同时启用”,用户在界面上看不到这句话。

## 决策

规则放在 Core 的 `plugins.installed.lifecycle`(`rust/crates/plugin-lifecycle`,Lithe-core-rust#39),因为“哪个插件启用”本来就由它回答;宿主只提供事实、执行步骤。

- **宿主提供事实**:`pluginmanager.Manager` 规划时,为每个已安装版本读出清单 `languageServers[].languageID`,作为 `installed[].languageServers` 交给 Core。读不到或解析失败的清单按“不提供语言服务器”处理,因为那个版本的服务器本来也启动不了,启动和注册语言的路径会报告这次失败。
- **Core 决定谁让位**:只看每个插件**最新安装版本**的语言服务器。用户启用插件 Y,或确认安装后 Y 处于启用状态时,其他已启用、与 Y 共享语言的插件 X 各得到一步 `displacePlugin { pluginID, languageIDs }`。停用不顶替任何插件,所以停用 Y 后,X 原来的启用选择会自然接手。
- **规则之前的旧状态**:用户以前把两个都启用了,Core 报告启用状态时按 ID 只保留第一个,选择本身不改;用户再启用其中一个时,用 `displacePlugin` 定下来。
- **宿主执行步骤**(`workbench/plugin_displace.go`):`Manager.Apply` 先把 X 记为停用;工作台回到窗口线程用 `detachPluginServer` 把 X 的服务器移出登记表,再在后台用 `pluginservice.Service.StopDisplaced` 停掉 X 的语言服务器和后端;停完后,如果项目里打开着共享语言的文档,就为它启动 Y 的服务器。同时用 `desktop.marketplace.languageServerDisplaced` 提示“已停用「X」:「Y」也提供 Java 语言服务器……”。

正确做法:新语言插件想做“轻量版 + 完整版”,两个插件都正常声明同一语言的 `languageServers`,由用户二选一。

不要这样做:在宿主里按插件 ID 或语言名写“Java Support Lite 让位给 Java Support”之类的特判;也不要让完整版去掉自己的跳转、期望轻量版补上,现有契约下一门语言的所有 LSP 请求只发给一个插件。

## 考虑过的备选方案

1. **同一语言同时跑多个服务器,按请求类型分配**(跳转给轻量版、补全给完整版):需要新契约,还要定义结果冲突、就绪前后切换的规则;而且 JDT 建好语义模型后跳转几乎不额外花资源,分出去省不了内存。现在不做。
2. **启用第二个时直接拒绝,让用户先手动停用另一个**:多一步操作,用户要自己弄明白两个插件为什么冲突。改为自动停用并提示。
3. **在清单里写 `conflicts: [插件 ID]`**:要求插件作者互相知道对方,第三方插件做同一语言时又会漏掉。按语言服务器声明自动判断更通用。
4. **把 Java Support Lite 并进 Java Support**:轻量用户就得下载 250 MiB 的 JDT 和 JDK。两个插件分开上架、二选一更合适;以后可以把轻量引擎打进 Java Support,作为 JDT 就绪前的快速层。

## 后果

- 用户不会再遇到两个 Java 插件互相抢、其中一个静默失效的情况;切换时有明确提示。
- 启用一个插件可能改变另一个插件的启用状态,这是用户可见的副作用,所以必须提示。
- 每次规划启停都要读取已安装插件的清单,有界(单个清单最多 8 MiB,通常只有几 KB)。
- 旧状态按 ID 选出的那个不一定是用户想要的,用户再启用另一个即可,不会丢选择。
- 尚未在原生窗口和 Windows 上验证。

## 验证

- Core 仓库 `cargo test -p lithe-core --lib shared_plugin_lifecycle_fixture`:`rust/fixtures/plugins/installed-lifecycle.json` 覆盖启用、确认安装、更新后新增语言、禁用插件的更新、只看最新版本、旧状态、反向启用和停用接手。
- `scripts/desktop/desktop-go.sh test ./internal/pluginmanager -run 'TestEnable|TestEarlierChoices|TestUnreadableManifest' -count=1`:顶替记录与报告、确认安装顶替、旧状态只启用一个且停用后接手、清单损坏不影响其他插件。
- `scripts/desktop/desktop-go.sh test ./internal/workbench -run TestInstallingAPluginTurnsOffTheOneServingTheSameLanguage -count=1`:从磁盘先后安装两个都服务同一语言的插件,第一个被停用并出现提示;再启用第一个,第二个被停用。去掉工作台的 `stopDisplaced` 调用后此测试失败。

## 适用范围

Viva 客户端(`desktop/`)的插件启用与安装:Core 的 `plugins.installed.lifecycle`、`desktop/internal/core/plugin_lifecycle.go`、`desktop/internal/pluginmanager/`、`desktop/internal/pluginservice/lifecycle.go`、`desktop/internal/workbench/plugins.go`、`desktop/internal/workbench/plugin_displace.go`。启用、禁用、卸载的其余规则见[Viva 确认安装即启用插件,卸载先停进程再清理状态目录](../bug-fix/2026-11-13-viva-plugin-install-enables-and-uninstall-cleans-state.md)。
30 changes: 22 additions & 8 deletions desktop/internal/core/plugin_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// PluginNotInstalled is the rejection of an action on a plugin that has no installed version.
const PluginNotInstalled = "plugin.lifecycle.notInstalled"

// The kinds of PluginLifecycleStep, all on the action's plugin.
// The kinds of PluginLifecycleStep, all on the action's plugin but PluginDisplacePlugin.
const (
// PluginRecordChoice records the user's choice, Enabled.
PluginRecordChoice = "recordChoice"
Expand All @@ -34,12 +34,19 @@ const (
PluginRemoveVersion = "removeVersion"
// PluginRemoveState removes the state directory the plugin's programs wrote.
PluginRemoveState = "removeState"
// PluginDisplacePlugin turns off another plugin, PluginID, because the action's plugin now
// serves LanguageIDs too and one enabled plugin serves a language: the host records the
// choice disabled for PluginID, stops its programs and tells the user.
PluginDisplacePlugin = "displacePlugin"
)

// PluginInstalledVersion is one installed plugin version, by identity only.
// PluginInstalledVersion is one installed plugin version: its identity and the languageID of
// every language server its manifest declares, which Core uses to keep one enabled plugin per
// language.
type PluginInstalledVersion struct {
PluginID string `json:"pluginID"`
Version string `json:"version"`
PluginID string `json:"pluginID"`
Version string `json:"version"`
LanguageServers []string `json:"languageServers,omitempty"`
}

// PluginEnablement is one plugin and whether it is enabled: a choice the user made, or a
Expand All @@ -59,11 +66,14 @@ type PluginLifecycleRequest struct {
PluginID string
}

// PluginLifecycleStep is one thing the host does on the action's plugin.
// PluginLifecycleStep is one thing the host does, on the action's plugin unless Kind is
// PluginDisplacePlugin, which names the plugin it turns off and the languages they share.
type PluginLifecycleStep struct {
Kind string `json:"kind"`
Enabled bool `json:"enabled,omitempty"`
Version string `json:"version,omitempty"`
Kind string `json:"kind"`
Enabled bool `json:"enabled,omitempty"`
Version string `json:"version,omitempty"`
PluginID string `json:"pluginID,omitempty"`
LanguageIDs []string `json:"languageIDs,omitempty"`
}

// PluginLifecycle is Core's answer: every installed plugin with its enablement once the steps
Expand Down Expand Up @@ -110,6 +120,10 @@ func (c *Client) PlanPluginLifecycle(ctx context.Context, request PluginLifecycl
if step.Version == "" {
return PluginLifecycle{}, newParseError(command, "a removeVersion step has no version", "")
}
case PluginDisplacePlugin:
if step.PluginID == "" || len(step.LanguageIDs) == 0 {
return PluginLifecycle{}, newParseError(command, "a displacePlugin step has no plugin or no language", "")
}
default:
return PluginLifecycle{}, newParseError(command, "unknown lifecycle step", step.Kind)
}
Expand Down
2 changes: 2 additions & 0 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 91 additions & 14 deletions desktop/internal/pluginmanager/pluginmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
// approving to plugintrust, storing enablement choices to pluginstate, and the
// lifecycle that spans them to Core (plugins.installed.lifecycle) — which plugin
// is enabled, what confirming an install, enabling, disabling and uninstalling
// do, and in which order. The manager hands Core the installed versions and the
// choices, and carries out the steps it gets back.
// do, and in which order. The manager hands Core the installed versions, the
// languages each one's manifest declares a language server for, and the choices,
// and carries out the steps it gets back. Core keeps one enabled plugin per
// language, so enabling a plugin can turn another off; the manager records that
// and reports it, and the caller stops that plugin's programs and tells the user.
package pluginmanager

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
Expand All @@ -28,6 +35,10 @@ var ErrUnknownPlugin = errors.New("plugin is not installed")
// the identities it is given, without reading a file.
const planTimeout = 5 * time.Second

// maxManifestBytes bounds the manifest read for its language servers; it is the limit the
// installer accepts, so a package on disk never exceeds it.
const maxManifestBytes = 8 << 20

// Kernel is the Core the manager asks for the lifecycle rules.
type Kernel interface {
PlanPluginLifecycle(context.Context, core.PluginLifecycleRequest) (core.PluginLifecycle, error)
Expand All @@ -41,6 +52,22 @@ type Entry struct {
Enabled bool
}

// Displaced is a plugin a lifecycle action turned off because the action's plugin
// serves LanguageIDs too. Its choice is already recorded disabled; its programs
// still run until the caller stops them.
type Displaced struct {
PluginID string
LanguageIDs []string
}

// Outcome is what a lifecycle action did beyond its own plugin.
type Outcome struct {
// Changed says the action changed the choice for its plugin.
Changed bool
// Displaced are the plugins the action turned off, in plugin ID order.
Displaced []Displaced
}

// Manager ties the installed packages to their approval records and enablement
// choices.
type Manager struct {
Expand Down Expand Up @@ -83,17 +110,18 @@ func (m *Manager) Installed() ([]Entry, error) {
// installed; every failure is returned for the caller to log. An approval is only
// forgotten once its version is gone, so the next installation of a version that
// could not be removed asks the user again.
func (m *Manager) Apply(ctx context.Context, action core.PluginLifecycleAction, id string, stop func() error) (bool, error) {
func (m *Manager) Apply(ctx context.Context, action core.PluginLifecycleAction, id string, stop func() error) (Outcome, error) {
_, plan, err := m.plan(ctx, action, id)
if err != nil {
return false, err
return Outcome{}, err
}
if plan.Rejection == core.PluginNotInstalled {
return false, fmt.Errorf("%w: %s", ErrUnknownPlugin, id)
return Outcome{}, fmt.Errorf("%w: %s", ErrUnknownPlugin, id)
}
if plan.Rejection != "" {
return false, fmt.Errorf("the plugin lifecycle of %s was refused: %s", id, plan.Rejection)
return Outcome{}, fmt.Errorf("the plugin lifecycle of %s was refused: %s", id, plan.Rejection)
}
outcome := Outcome{Changed: plan.Changed}
var failures []error
for _, step := range plan.Steps {
switch step.Kind {
Expand Down Expand Up @@ -123,28 +151,46 @@ func (m *Manager) Apply(ctx context.Context, action core.PluginLifecycleAction,
if err := pluginpkg.RemoveState(m.root, id); err != nil {
failures = append(failures, fmt.Errorf("remove the state of %s: %w", id, err))
}
case core.PluginDisplacePlugin:
// Recorded before the caller stops the programs, as Disable does, so
// nothing starts the displaced plugin again while it stops.
if err := m.state.SetEnabled(step.PluginID, false); err != nil {
failures = append(failures, fmt.Errorf("record that %s was turned off for %s: %w", step.PluginID, id, err))
}
outcome.Displaced = append(outcome.Displaced, Displaced{PluginID: step.PluginID, LanguageIDs: step.LanguageIDs})
}
}
return plan.Changed, errors.Join(failures...)
return outcome, errors.Join(failures...)
}

// SetEnabled records the user turning an installed plugin on or off. It stops
// nothing; pluginservice.Service.Disable also stops what a disabled plugin runs.
// nothing; pluginservice.Service.Disable also stops what a disabled plugin runs,
// and Enable reports the plugins enabling this one turned off.
func (m *Manager) SetEnabled(id string, enabled bool) error {
action := core.PluginDisable
if enabled {
action = core.PluginEnable
_, err := m.Enable(id)
return err
}
ctx, cancel := context.WithTimeout(context.Background(), planTimeout)
defer cancel()
_, err := m.Apply(ctx, action, id, nil)
_, err := m.Apply(ctx, core.PluginDisable, id, nil)
return err
}

// Enable records the user turning an installed plugin on and reports the plugins
// it turned off because it serves one of their languages; their programs are the
// caller's to stop.
func (m *Manager) Enable(id string) ([]Displaced, error) {
ctx, cancel := context.WithTimeout(context.Background(), planTimeout)
defer cancel()
outcome, err := m.Apply(ctx, core.PluginEnable, id, nil)
return outcome.Displaced, err
}

// EnableInstalled records that the user confirmed installing plugin id, which
// enables it unless the user already chose to enable or disable it, and reports
// whether it changed the choice.
func (m *Manager) EnableInstalled(id string) (bool, error) {
// whether it changed the choice and which plugins it turned off.
func (m *Manager) EnableInstalled(id string) (Outcome, error) {
ctx, cancel := context.WithTimeout(context.Background(), planTimeout)
defer cancel()
return m.Apply(ctx, core.PluginConfirmInstall, id, nil)
Expand All @@ -163,7 +209,9 @@ func (m *Manager) plan(ctx context.Context, action core.PluginLifecycleAction, i
}
request := core.PluginLifecycleRequest{Action: action, PluginID: id}
for _, entry := range installed {
request.Installed = append(request.Installed, core.PluginInstalledVersion{PluginID: entry.ID, Version: entry.Version})
request.Installed = append(request.Installed, core.PluginInstalledVersion{
PluginID: entry.ID, Version: entry.Version, LanguageServers: languageServers(entry.Path),
})
}
for _, choice := range choices {
request.Choices = append(request.Choices, core.PluginEnablement{PluginID: choice.PluginID, Enabled: choice.Enabled})
Expand All @@ -181,3 +229,32 @@ func (m *Manager) plan(ctx context.Context, action core.PluginLifecycleAction, i
func (m *Manager) Approval(id, version string) (plugintrust.Record, bool, error) {
return m.approvals.Record(id, version)
}

// languageServers is the languageID of every language server the manifest in
// directory declares, in declaration order.
//
// A manifest that cannot be read or decoded serves nothing: the host cannot start
// that version's server either, and the paths that load the manifest to start it
// or register its languages report the failure.
func languageServers(directory string) []string {
file, err := os.Open(filepath.Join(directory, pluginpkg.ManifestFile))
if err != nil {
// log-exempt: 同一次失败已经记录过:启动语言服务器与注册语言时读取同一份清单并报告失败
return nil
}
defer file.Close()
var manifest struct {
LanguageServers []struct {
LanguageID string `json:"languageID"`
} `json:"languageServers"`
}
if err := json.NewDecoder(io.LimitReader(file, maxManifestBytes)).Decode(&manifest); err != nil {
// log-exempt: 同一次失败已经记录过:启动语言服务器与注册语言时读取同一份清单并报告失败
return nil
}
languages := make([]string, 0, len(manifest.LanguageServers))
for _, server := range manifest.LanguageServers {
languages = append(languages, server.LanguageID)
}
return languages
}
Loading
Loading