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
Expand Up @@ -44,8 +44,37 @@ SDK 的通知回调可能由并发工作线程执行,插件加锁的顺序不

Core 保留标准 LSP 进度的所有权;插件不申请转发宿主保留的方法。JDT 自己选择扩展通道,进度通知会跨现有插件通道转发,但普通进度报告只做常量级检查,不会逐条触发动作查询。延迟、恢复和预算耗尽分别进入 SDK 结构化诊断;这不修改宿主 API。Windows 长 classpath 另由插件复用 manifest JAR 规划,JAR 字节交给宿主临时文件接口,运行结束由宿主删除,不写安装目录。长 module-path 仍不属于已完成范围。

## 无源码依赖的类内容与导航

语言服务器能返回目标地址,不代表编辑器能打开它。Java 插件之前只声明支持类内容,
没有注册 `jdt:` 内容提供者;Viva 也没有给只读虚拟文档设置语言标识,并把其地址当作
磁盘路径重新编码。现在 Java 插件在返回有效服务器启动方案前注册已有内容扩展点,
调用 JDT 的 `java/classFileContents`;宿主只按通用语法注册表选择语言,保留原始 URI,
因此打开后的依赖类仍能继续发起导航。提供者按工作区代际持有订阅,不缓存源码或创建临时源码文件;
内容最多 1 MiB,请求有界,空响应按失败处理。卸载、工作区关闭和代际变化沿用既有清理。

原生界面还必须让读取属于发起时可见的文档和光标。用户换标签、移动光标、编辑、关闭文档或
发起另一次导航后,取消未结束的内容读取;提交只读标签前再次验证归属。否则慢响应会在用户
已经离开后抢走焦点。该临时光标快照只属于窗口,不作为插件协议或 Java 语义模型。
准备文本和加载语法在后台完成,窗口线程只提交呈现状态;不为每次导航保留第二份全文。

真实残缺类文件另外触发了 JDT 1.61 的上游缺陷:外部类列出但未提供的内部类使
`FernFlowerDecompiler.scanClassFiles` 抛出 Java Model 异常,整个外部类内容为空。
JDT 的 `java.decompile` 使用相同内容管理器,换命令不能绕过问题;当时核对的上游 main
也保留相同行为。插件对锁定的开源集成增加“类文件不存在则跳过”的小补丁,反编译、源码附加、
语义模型与行号映射仍由 JDT/FernFlower 完整子系统负责;不虚构缺失内部类的实现。
正确做法是显示已有外部类并保留真实行号,不要生成占位类或修改反编译文本来凑落点。

补丁只在独立 Java 插件构建阶段执行:校验原始源码和 JAR、修改源码、固定编译器与完整
class 集合,只修改新组装目录,单次操作的编译临时目录总会删除。修改过的 JDT core JAR
不能保留失效的 Eclipse 签名,因此移除签名及条目摘要、保留 OSGi 主清单,随包提供修改的
EPL-2.0 源码、补丁身份和生成物哈希。它不再声称由 Eclipse 签名;原生 JVM 签名不变。
下载缓存、已安装包和 app bundle 都只读,编译输出与组装包不跨 worktree 复用。
这些是打包补丁的维护成本;上游修复后须先用同一残缺内部类回归验证,再移除补丁。

## 考虑过的备选方案

- 自建反编译器或在宿主识别 `jdt:`:重复成熟上游语义且破坏插件边界;只补上游对残缺输入的遍历,不引入第二套 Java 模型。
- 放宽 Core 声明解码:掩盖宿主把路由信息当成声明的错误,也允许其他无效字段通过。
- 固定增大等待或对所有进度报告重查:前者已在真实冷导入中证明不能恢复丢失的动作,后者会在正常索引时反复触发昂贵查询。只在失败后的工作完成边界重查。
- 要求用户编辑文件或先打开 Debug 面板:把接线时序变成操作前提,不能修复首次使用。
Expand All @@ -67,10 +96,13 @@ Core 保留标准 LSP 进度的所有权;插件不申请转发宿主保留的
- `TestJavaPluginCleanupWhilePaused` 在真实 Java 断点暂停后直接返回,跳过成功路径关闭,由始终执行的清理收回运行并确认 Core 返回会话不存在。
- `scripts/desktop/desktop-go.sh test ./...` 与受影响包的 `-race`;本机证据来自 macOS arm64 无头工作台,原生窗口与其他平台未据此标记已验收。

- `TestJavaPluginBinaryNavigation` 使用自行编译的无源码 JAR,覆盖类型/方法落点、类间连续跳转、缺失内部类和迟到结果不能抢焦点;回归通过前不得据此发布。私有输入只在本地诊断,不进入公开测试或构建产物。

## 适用范围

- `desktop/internal/pluginservice/hostactions.go`、`hostproviders.go`、`runs.go`
- `desktop/internal/workbench/debug_start.go`、`debug.go`、`pluginworkbench.go`、`pluginbackends.go`
- `desktop/internal/workbench/java_plugin_e2e_test.go`、`java_legacy_e2e_test.go`、`java_unmanaged_e2e_test.go`
- `desktop/internal/pluginservice/backendlaunch.go`、`backendlaunch_test.go`;Core 的语言服务器引擎握手期限
- `desktop/internal/workbench/plugincontent.go`、`pluginsync.go`、`pluginsemantics.go`、`java_binary_e2e_test.go`;独立 Java 插件的内容提供者与构建期 JDT 补丁
- 通用职责边界见 [插件后端会话与宿主服务](../architecture/2026-11-10-viva-plugin-host-services-and-backend-sessions.md)。
3 changes: 2 additions & 1 deletion desktop/internal/workbench/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ type App struct {
// withdraws only its own diagnostics.
pluginDiagnostics map[string]*plugindiag.Set
// semantics is the editor's pending and presented language answers.
semantics pluginSemantics
semantics pluginSemantics
contentOpen *contentOpenRequest
// editor holds the state of the editor features that are not part of the language
// semantics: back/forward history, the outline, references and so on (editor_features.go).
editor editorFeatures
Expand Down
297 changes: 297 additions & 0 deletions desktop/internal/workbench/java_binary_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
package workbench

import (
"archive/zip"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/plugininstall"
"github.com/1lck/Lithe-IDEA/desktop/internal/plugintrust"
"github.com/1lck/Lithe-IDEA/desktop/internal/store"
"github.com/vivaui/viva/gfx"
)

// Real JDT and a jar generated entirely from this test's own sources: no
// proprietary classes, source attachments, network dependencies or execution.
func TestJavaPluginBinaryNavigation(t *testing.T) {
archive := os.Getenv("LITHE_TEST_JAVA_PLUGIN_ARCHIVE")
if archive == "" {
t.Skip("Java integration archive required")
}
home := os.Getenv("JAVA_HOME")
if home == "" {
t.Fatal("JAVA_HOME with javac is required")
}
javac := filepath.Join(home, "bin", "javac")
if runtime.GOOS == "windows" {
javac += ".exe"
}
library := t.TempDir()
writeFixture(t, library, map[string]string{
"library/Slow.java": "package library; public class Slow {}",
"library/Entity.java": "package library;\npublic class Entity {\n public String label() { return \"entity\"; }\n public EntitySet owner() { return null; }\n}\n",
"library/Incomplete.java": "package library;\npublic class Incomplete {\n public static class Missing {}\n public String legacyName() { return \"outer\"; }\n}\n",
"library/EntitySet.java": "package library;\npublic class EntitySet {\n public String label() { return \"set\"; }\n public Entity first() { return null; }\n}\n",
})
classes := filepath.Join(library, "classes")
if err := os.MkdirAll(classes, 0700); err != nil {
t.Fatal(err)
}
compileCtx, compileCancel := context.WithTimeout(context.Background(), 30*time.Second)
command := exec.CommandContext(compileCtx, javac, "--release", "8", "-g", "-d", classes, filepath.Join(library, "library/Entity.java"), filepath.Join(library, "library/EntitySet.java"), filepath.Join(library, "library/Incomplete.java"), filepath.Join(library, "library/Slow.java"))
command.WaitDelay = 2 * time.Second
output, err := command.CombinedOutput()
compileCancel()
if err != nil {
t.Fatalf("compile own binary fixture: %v: %s", err, output)
}
preferenceDir := t.TempDir()
if err := os.WriteFile(filepath.Join(preferenceDir, settingsFile), []byte(`{"theme":"dark"}`), 0600); err != nil {
t.Fatal(err)
}
kernel := &binaryNavigationKernel{testKernel: &testKernel{Client: core.NewClient()}}
h := newHarnessWithOptions(t, Options{Kernel: kernel, PluginKernel: kernel, PluginRoot: t.TempDir(), Store: store.At(preferenceDir), Languages: []string{"en"}})
service := h.app.plugins
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
plan, err := service.Installer.Prepare(ctx, plugininstall.Request{ArchivePath: archive, Root: service.Root, Source: plugintrust.Disk})
if err != nil {
t.Fatal(err)
}
defer func() {
if e := service.Installer.Discard(plan); e != nil {
t.Error(e)
}
}()
installed, err := service.Installer.Commit(plan)
if err != nil {
t.Fatal(err)
}
if err = service.Manager.SetEnabled(plan.ID, true); err != nil {
t.Fatal(err)
}
before := packageDigest(t, installed.Path)
h.app.reloadPluginLanguages()
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
source := "package app;\nimport library.Entity;\nimport library.EntitySet;\nimport library.Incomplete;\nimport library.Slow;\npublic class Main {\n public static void main(String[] args) {}\n String labels(Entity item, EntitySet set) {return item.label() + set.label();}\n void slow(Slow value) {}\n String legacy(Incomplete old) { return old.legacyName(); }\n}\n"
writeFixture(t, root, map[string]string{
".project": `<projectDescription><name>binary-navigation</name><buildSpec><buildCommand><name>org.eclipse.jdt.core.javabuilder</name></buildCommand></buildSpec><natures><nature>org.eclipse.jdt.core.javanature</nature></natures></projectDescription>`,
".classpath": `<classpath><classpathentry kind="src" path="src"/><classpathentry kind="lib" path="lib/library.jar"/><classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/><classpathentry kind="output" path="classes"/></classpath>`,
".settings/org.eclipse.jdt.core.prefs": "eclipse.preferences.version=1\norg.eclipse.jdt.core.compiler.source=17\norg.eclipse.jdt.core.compiler.compliance=17\norg.eclipse.jdt.core.compiler.codegen.targetPlatform=17\n",
"src/app/Main.java": source,
})
if err = os.MkdirAll(filepath.Join(root, "lib"), 0700); err != nil {
t.Fatal(err)
}
file, err := os.Create(filepath.Join(root, "lib/library.jar"))
if err != nil {
t.Fatal(err)
}
writer := zip.NewWriter(file)
for _, name := range []string{"Entity", "EntitySet", "Incomplete", "Slow"} {
bytes, e := os.ReadFile(filepath.Join(classes, "library", name+".class"))
if e != nil {
writer.Close()
file.Close()
t.Fatal(e)
}
entry, e := writer.Create("library/" + name + ".class")
if e == nil {
_, e = entry.Write(bytes)
}
if e != nil {
writer.Close()
file.Close()
t.Fatal(e)
}
}
zipErr, fileErr := writer.Close(), file.Close()
if zipErr != nil || fileErr != nil {
t.Fatalf("close binary jar: %v / %v", zipErr, fileErr)
}
t.Cleanup(func() {
if p := h.app.project; p != nil {
h.app.finishCloseProject(p, nil)
awaitStateWithin(t, h, 30*time.Second, "binary project closed", func() bool { return h.app.project == nil })
}
assertPackageUnchanged(t, installed.Path, before)
})
h.open(root)
p := h.app.project
t.Cleanup(func() {
if t.Failed() {
t.Logf("notice=%s", h.app.notice)
kernel.mu.Lock()
defer kernel.mu.Unlock()
for _, e := range kernel.events {
t.Logf("definition %s %s => %s error=%s", e.URI, e.OperationID, e.Result, e.Error)
}
}
})
mainPath := filepath.Join(root, "src/app/Main.java")
h.app.openFile(p, mainPath)
awaitStateWithin(t, h, 120*time.Second, "Java binary project ready", func() bool {
d := documentNamed(p, "Main.java")
return d != nil && len(d.actions) > 0 && service.HasContentProvider(p.root, "jdt://contents/library")
})
jump := func(d *document, needle, target, symbol string) {
t.Helper()
h.app.selectDocument(p, d.id)
h.app.invalidator.Invalidate()
if err := h.driver.Pump(); err != nil {
t.Fatal(err)
}
text, _ := d.view.Text()
at := strings.Index(string(text), needle)
if at < 0 {
t.Fatalf("missing fixture symbol %q", needle)
}
// Search strings begin at the symbol, including member calls' method name.
prefix := string(text[:at+1])
line := strings.Count(prefix, "\n")
column := len(prefix) - strings.LastIndex(prefix, "\n") - 1
d.view.GoToNavigation(line, column, false)
h.app.invalidator.Invalidate()
if err := h.driver.Pump(); err != nil {
t.Fatal(err)
}

if err := h.driver.Key(gfx.KeyF12, 0); err != nil {
t.Fatal(err)
}
awaitStateWithin(t, h, 30*time.Second, "binary target "+target, func() bool {
v := p.selectedDocument(p.docs.group)
return v != nil && v.virtual && strings.Contains(v.path, "/"+target+".java")
})
v := p.selectedDocument(p.docs.group)
content, _ := v.view.Text()
line, _ = v.view.CaretUTF16()
lines := strings.Split(string(content), "\n")
if !v.view.ReadOnly() || v.languageID != "java" || line < 0 || line >= len(lines) || !strings.Contains(lines[line], symbol) {
t.Fatalf("binary target %s: language=%s readOnly=%v line=%d expected %s", target, v.languageID, v.view.ReadOnly(), line+1, symbol)
}
}
main := p.docs.byPath(mainPath)
jump(main, "label();", "EntitySet", "label")
jump(main, "Entity item", "Entity", "Entity")
jump(main, "EntitySet set", "EntitySet", "EntitySet")
jump(main, "label() +", "Entity", "label")
jump(main, "label();", "EntitySet", "label")
var entity *document
for _, d := range p.docs.list {
if d.virtual && strings.Contains(d.path, "/Entity.java") {
entity = d
}
}
if entity == nil {
t.Fatal("missing entity view")
}
jump(entity, "EntitySet owner", "EntitySet", "EntitySet")
set := p.selectedDocument(p.docs.group)
jump(set, "Entity first", "Entity", "Entity")
jump(main, "Incomplete old", "Incomplete", "Incomplete")
jump(main, "legacyName();", "Incomplete", "legacyName")
// Hold the real class-content response, move the visible caret, then release it.
// An obsolete navigation must finish without opening a tab or stealing focus.
gate := &binaryContentGate{entered: make(chan struct{}), release: make(chan struct{})}
defer gate.releaseOnce.Do(func() { close(gate.release) })
kernel.mu.Lock()
kernel.gate = gate
kernel.mu.Unlock()
h.app.selectDocument(p, main.id)
text, _ := main.view.Text()
at := strings.Index(string(text), "Slow value") + 1
prefix := string(text[:at])
line := strings.Count(prefix, "\n")
column := len(prefix) - strings.LastIndex(prefix, "\n") - 1
main.view.GoToNavigation(line, column, false)
h.app.invalidator.Invalidate()
if err := h.driver.Pump(); err != nil {
t.Fatal(err)
}
if err := h.driver.Key(gfx.KeyF12, 0); err != nil {
t.Fatal(err)
}
awaitValueWithin(t, h, 20*time.Second, gate.entered, "class content response held")
if err := h.driver.Key(gfx.KeyRight, 0); err != nil {
t.Fatal(err)
}
gate.releaseOnce.Do(func() { close(gate.release) })
awaitStateWithin(t, h, 20*time.Second, "obsolete class navigation finished", func() bool { return h.app.contentOpen == nil })
h.app.invalidator.Invalidate()
if err := h.driver.Pump(); err != nil {
t.Fatal(err)
}
if p.selectedDocument(p.docs.group) != main {
t.Fatal("obsolete class navigation stole focus")
}
for _, d := range p.docs.list {
if d.virtual && strings.Contains(d.path, "/Slow.java") {
t.Fatal("obsolete class navigation opened a tab")
}
}
if len(problemMessages(h.app)) != 0 {
t.Fatalf("fixture has diagnostics: %v", problemMessages(h.app))
}
}

// Keep only this fixture's bounded definition results for failure diagnostics.
type binaryNavigationKernel struct {
*testKernel
mu sync.Mutex
events []core.BackendEvent
gate *binaryContentGate
}

func (k *binaryNavigationKernel) WaitBackendEvents(ctx context.Context, session string, timeout int) ([]core.BackendEvent, error) {
events, err := k.Client.WaitBackendEvents(ctx, session, timeout)
for _, e := range events {
if e.Type == "requestCompleted" && e.Method == "java/classFileContents" {
k.mu.Lock()
gate := k.gate
k.gate = nil
k.mu.Unlock()
if gate != nil {
close(gate.entered)
timer := time.NewTimer(15 * time.Second)
select {
case <-gate.release:
case <-ctx.Done():
timer.Stop()
return nil, ctx.Err()
case <-timer.C:
return nil, fmt.Errorf("class response gate timed out")
}
timer.Stop()
}
}
if e.Type == "requestCompleted" && e.Method == "textDocument/definition" {
k.mu.Lock()
if len(k.events) < 32 {
k.events = append(k.events, e)
}
k.mu.Unlock()
}
}
return events, err
}

// This gate is consumed once, released on all assertion paths, and times out on
// the backend worker rather than blocking the UI thread.
type binaryContentGate struct {
entered chan struct{}
release chan struct{}
releaseOnce sync.Once
}
Loading
Loading