diff --git a/.agents/notes/implemented/feature/2026-11-18-run-current-file-entrypoint.md b/.agents/notes/implemented/feature/2026-11-18-run-current-file-entrypoint.md index e552ace44..8f61a8ab7 100644 --- a/.agents/notes/implemented/feature/2026-11-18-run-current-file-entrypoint.md +++ b/.agents/notes/implemented/feature/2026-11-18-run-current-file-entrypoint.md @@ -1,17 +1,17 @@ -# Agent 笔记:当前文件运行(按需运行入口) +# Agent 笔记:当前文件的运行与调试 状态:已实现 ## 先说结论 -标题栏选“当前文件”后点运行,可以直接运行当前文件里的入口,例如没有在 POM 里写明主类、但带 `main` 方法的 Java 类(#2104): +标题栏选“当前文件”后点运行或调试,可以使用构建提供者按需发现的入口,无须预先创建运行配置(#2104、#2074): - 插件在构建系统里声明入口运行 `{"id": "main", "source": "provider", "entrypoints": true}`,需要 `requires.buildProviders: 1`。入口运行不对应构建目标,不出现在构建树和运行配置列表里,只在用户运行某个文件时才产生配置。 - 宿主把当前文件(工作区相对路径)交给根目录包含它的构建实例(内层优先)。Core 用 `build.runEntrypointsPlan` 规划 `lithe/build/runEntrypoints` 请求,插件按文件回答入口,Core 用 `build.decodeRunEntrypoints` 校验:运行必须是入口运行、名称和入口长度受限、项目在实例根目录内,答案不合法时整份拒绝。 - 宿主取第一个入口,按项目路径找到模型里的项目,用 `runConfig.createUserConfiguration` 保存本机配置:名称取入口名(类名),引用的 `targetID` 为空,入口存为 `extensions.build-run.entrypoint`,入口是配置身份的一部分。同一入口再次运行复用已保存的配置。 - 保存后重新读取配置、在标题栏选中并运行。准备运行时请求不带 `target`,入口通过 `launchOptions.entrypoint` 交给插件。之后用户可以像编辑其他构建提供者运行一样修改参数、环境变量和工作目录。 - 没有打开文件、没有可回答的构建实例或文件里没有入口时,提示“当前文件没有可运行的入口”。 -- Maven 插件按源码文本回答:文件所在的最近一个含 `pom.xml` 的模块,包名加文件名作为类名,必须有 `static void main(String[])` 或 `String...`;测试源码不算,因为运行使用的运行时类路径不含测试类。不编译,回答很快。 +- 提供者声明 `buildSystems: 2` 后,可以给应用入口加 `debug.configurationType`。调试只在声明该类型的入口中查找,保存或复用同一份配置,然后使用已注册的适配器连接暂停的进程。没有匹配入口或调试查找失败时,继续尝试编辑器提供的操作。 ## 问题 @@ -25,23 +25,28 @@ - 入口作为配置身份的一部分,同一模块里不同类各存一条配置,同一个类不会重复保存。 - 一个文件有多个入口时先运行第一个;选择菜单以后需要时再加。 +- 构建提供者按磁盘内容查找入口,因此先通过编辑器原有的流程保存修改并处理冲突。查找期间切换文件或修改内容时,丢弃这次查找结果。 +- 调试查找失败时记录失败原因,当前文档仍可使用自己的 Debug 动作。没有可用的编辑器动作时显示查找错误。 + ## 考虑过的备选方案 - 模型读取时扫描源码把所有 main 类列成目标:读取变慢、工具类和测试辅助类会淹没下拉(#2074 已否决)。 - 行号旁运行图标:需要编辑器按语言识别 main 方法或额外的插件协议;标题栏“当前文件”已有入口,先复用它。 -- 入口运行支持调试:Maven 的调试需要声明调试方式(服务运行用 `debug.configurationType`),入口运行的调试和准备方式另行设计,本轮只做运行。 +- 为源码入口增加独立调试协议:现有注册适配器和回环连接已能表达启动方式,应用入口复用同一机制。 ## 后果 - 插件可以按文件提供可运行的入口;新增语言或构建工具时只需在插件里回答 `lithe/build/runEntrypoints`。 - 使用入口运行的插件版本需要支持 `buildProviders` 1 的宿主,插件应和支持它的桌面版本一起发布。 -- 以后加入口运行的调试,需要在契约里给入口运行声明调试方式,并在 `decideTitleAction` 的可调试判断里覆盖入口运行。 +- 仅声明运行的入口仍可正常运行。调试依赖相应适配器可用,旧插件仍可通过编辑器动作启动调试。 ## 验证 - Core:`cargo test -p lithe-core provider_run_entrypoints_are_found_by_file_and_start_without_a_target entrypoint_runs_are_saved_per_entrypoint_without_a_target`。 - 宿主:`./scripts/desktop/desktop-go.sh test ./internal/workbench/ -run TestRunOnCurrentFile -count=1`(合成构建提供者回答入口、准备运行);截图 `TestVisualRunOnCurrentFile`。 -- Maven 插件:`node scripts/test.mjs` 中的入口查找和入口运行检查(真实 Maven)。 +- 宿主:`TestCurrentFileDebugSavesAndAttachesToTheDiscoveredEntrypoint` 验证首次调试保存入口,并让已注册的适配器连接该入口的回环端口。 + +- 宿主:`TestCurrentFileDebugSavesEditsBeforeDiscoveringAnEntrypoint` 验证从缓冲区删除入口时,提供者读取的是保存后的文件。`TestCurrentFileDebugFallsBackWhenProviderDiscoveryFails` 验证提供者失败后仍能启动当前文档的 Debug 动作。 ## 适用范围 @@ -52,4 +57,4 @@ - `desktop/internal/pluginservice/builds.go`(`EntrypointRuns`) - `desktop/internal/pluginhost/pluginhost.go`(`buildProviders` 声明 `[0, 1]`) - Core:`rust/crates/build/src/run_entrypoints.rs`、`rust/crates/execution/src/configuration/editing.rs` -- Maven 插件:`provider/src/main/java/io/github/litheidea/maven/provider/RunEntrypoints.java` +- `desktop/internal/workbench/build_connection_debug.go`(使用注册适配器连接暂停的进程) diff --git a/desktop/internal/pluginhost/pluginhost.go b/desktop/internal/pluginhost/pluginhost.go index fabf9f773..f57ba8a03 100644 --- a/desktop/internal/pluginhost/pluginhost.go +++ b/desktop/internal/pluginhost/pluginhost.go @@ -196,7 +196,8 @@ func Declared() Capabilities { // settings window's source forms render. declared["sourceSettings"] = []int{0, 1} // Version 1 adds command-sourced discovery in the Build test tree. - declared["buildSystems"] = []int{0, 1} + // Version 2 adds registry connection debugging for provider applications. + declared["buildSystems"] = []int{0, 1, 2} // Version 1 adds entrypoint runs, which Run on Current File starts. declared["buildProviders"] = []int{0, 1} // Version 1 adds a definition's category and audience, by which the diff --git a/desktop/internal/pluginhost/pluginhost_test.go b/desktop/internal/pluginhost/pluginhost_test.go index 8f6233d70..308d7ce95 100644 --- a/desktop/internal/pluginhost/pluginhost_test.go +++ b/desktop/internal/pluginhost/pluginhost_test.go @@ -74,9 +74,9 @@ func TestDeclaredServesTheBaselineOfEveryWiredCapability(t *testing.T) { if name == "sourceSettings" { wanted = []int{0, 1} } - // The Build test tree supports the discovery command version 1 adds. + // Version 1 adds test discovery, and version 2 adds registry attach for provider applications. if name == "buildSystems" { - wanted = []int{0, 1} + wanted = []int{0, 1, 2} } // Run on Current File starts the entrypoint runs version 1 adds. if name == "buildProviders" { diff --git a/desktop/internal/pluginservice/builds.go b/desktop/internal/pluginservice/builds.go index 440ae9ec7..52c85e13a 100644 --- a/desktop/internal/pluginservice/builds.go +++ b/desktop/internal/pluginservice/builds.go @@ -243,24 +243,25 @@ func parseBuildSystems(manifest json.RawMessage) (map[string]BuildSystemDeclarat }) } for _, run := range raw.Run { - if run.ID != "" && run.Source == "provider" && run.Entrypoints { - system.EntrypointRuns = append(system.EntrypointRuns, BuildRunDeclaration{ID: run.ID, Name: run.Name, Execution: "application"}) - continue - } - if run.ID == "" || run.Source != "provider" || run.FromTargets == nil { + if run.ID == "" || run.Source != "provider" { continue } declaration := BuildRunDeclaration{ ID: run.ID, Name: run.Name, Execution: run.Execution, - Tags: append([]string(nil), run.FromTargets.Tags...), } if run.Debug != nil { declaration.DebugAdapter = run.Debug.Adapter declaration.DebugConfigurationType = run.Debug.ConfigurationType } - system.Runs = append(system.Runs, declaration) + if run.Entrypoints { + declaration.Execution = "application" + system.EntrypointRuns = append(system.EntrypointRuns, declaration) + } else if run.FromTargets != nil { + declaration.Tags = append([]string(nil), run.FromTargets.Tags...) + system.Runs = append(system.Runs, declaration) + } } if raw.Test != nil { system.Test = &BuildTestDeclaration{ diff --git a/desktop/internal/workbench/build.go b/desktop/internal/workbench/build.go index cf2216385..5149dff03 100644 --- a/desktop/internal/workbench/build.go +++ b/desktop/internal/workbench/build.go @@ -1963,9 +1963,11 @@ func (a *App) redebugProviderTarget(p *project, reference core.RunProviderRefere // buildRunDeclaration is the run declaration id of system. func buildRunDeclaration(system pluginservice.BuildSystemDeclaration, id string) (pluginservice.BuildRunDeclaration, bool) { - for _, run := range system.Runs { - if run.ID == id { - return run, true + for _, runs := range [][]pluginservice.BuildRunDeclaration{system.Runs, system.EntrypointRuns} { + for _, run := range runs { + if run.ID == id { + return run, true + } } } return pluginservice.BuildRunDeclaration{}, false diff --git a/desktop/internal/workbench/build_connection_debug.go b/desktop/internal/workbench/build_connection_debug.go index ef3db472f..dd3d45184 100644 --- a/desktop/internal/workbench/build_connection_debug.go +++ b/desktop/internal/workbench/build_connection_debug.go @@ -116,7 +116,7 @@ func (a *App) prepareConnectionDebug(p *project, configurationType string, start } // debugRunConfiguration debugs configuration id, whose run declares a public debug type: the -// configuration's saved launch options apply as for Run, with the service suspended for the +// configuration's saved launch options apply as for Run, with the debuggee suspended for the // selected adapter. func (a *App) debugRunConfiguration(p *project, id, configurationType string) { a.prepareConnectionDebug(p, configurationType, func(debug connectionDebug) { diff --git a/desktop/internal/workbench/build_connection_debug_test.go b/desktop/internal/workbench/build_connection_debug_test.go index 12bd4731d..5a83e5339 100644 --- a/desktop/internal/workbench/build_connection_debug_test.go +++ b/desktop/internal/workbench/build_connection_debug_test.go @@ -1,18 +1,23 @@ package workbench import ( + "context" "encoding/json" + "errors" "net" "os" "path/filepath" "runtime" + "slices" "strconv" "strings" "testing" "time" "github.com/1lck/Lithe-IDEA/desktop/internal/core" + "github.com/1lck/Lithe-IDEA/desktop/internal/editorview" "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" + "github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice" "github.com/1lck/Lithe-IDEA/desktop/internal/pluginstate" "github.com/1lck/Lithe-IDEA/desktop/internal/store" ) @@ -85,6 +90,11 @@ func registryAttachManifest(id string) string { // manifest), each with the copied test binary as its program, and puts the fixture build tool // and the fixture service on PATH. func connectionDebugHarness(t *testing.T, adapters map[string]string) *harness { + t.Helper() + return connectionDebugHarnessWithProvider(t, providerServiceDebugManifest, adapters) +} + +func connectionDebugHarnessWithProvider(t *testing.T, providerManifest string, adapters map[string]string) *harness { t.Helper() dir := t.TempDir() if err := os.WriteFile(filepath.Join(dir, settingsFile), []byte(`{"theme":"dark"}`), 0o600); err != nil { @@ -105,7 +115,7 @@ func connectionDebugHarness(t *testing.T, adapters map[string]string) *harness { t.Fatal(err) } } - install("dev.lithe.test.provider-tests", providerServiceDebugManifest, "provider") + install("dev.lithe.test.provider-tests", providerManifest, "provider") for id, manifest := range adapters { if runtime.GOOS == "windows" { manifest = strings.ReplaceAll(manifest, `"path": "adapter"`, `"path": "adapter.exe"`) @@ -166,8 +176,9 @@ func openServiceDebugProject(t *testing.T, h *harness) (*project, string) { // preparedDebug is the launchOptions.debug the fixture provider received. type preparedDebug struct { - Execution string `json:"execution"` - Debug *struct { + Execution string `json:"execution"` + Entrypoint string `json:"entrypoint"` + Debug *struct { AdapterID string `json:"adapterID"` ConfigurationType string `json:"configurationType"` Target struct { @@ -250,6 +261,130 @@ func TestTitleDebugAttachesTheRegisteredAdapterToAServiceRun(t *testing.T) { _ = listener.Close() } +func TestCurrentFileDebugSavesAndAttachesToTheDiscoveredEntrypoint(t *testing.T) { + const adapterID = "dev.lithe.test.registry-attach" + h := connectionDebugHarnessWithProvider(t, debugEntrypointsManifest(), map[string]string{adapterID: registryAttachManifest(adapterID)}) + gate := &captureDebugLaunch{Kernel: h.app.kernel, configurations: make(chan core.DebugLaunchConfiguration, 1)} + h.app.kernel = gate + root := entrypointWorkspace(t) + writeFixture(t, root, map[string]string{"debug.marker": "fixture\n"}) + h.open(root) + p := h.app.project + openSource(t, h, p, filepath.Join(root, "src", "Main.java")) + h.app.post(func(a *App) { + a.chooseTitleConfiguration(p, "") + a.titleDebug(p) + }) + configuration := awaitValueWithin(t, h, time.Minute, gate.configurations, "the attach to the discovered entrypoint") + prepared := readPreparedRun(t, root) + if prepared.Execution != "application" || prepared.Entrypoint != "example.Main" || prepared.Debug == nil { + t.Fatalf("the provider prepared %+v, want the application entrypoint with a debug target", prepared) + } + if configuration.Request != "attach" || !sameDebugPort(configuration.Arguments["port"], prepared.Debug.Target.Port) { + t.Fatalf("attach configuration = %+v, want the entrypoint's port %d", configuration, prepared.Debug.Target.Port) + } + saved := p.titleConfiguration() + if saved == nil || saved.Name != "Main" || len(p.runConfigs.list) != 1 { + t.Fatalf("selected configuration = %+v, configurations = %+v", saved, p.runConfigs.list) + } + h.app.post(func(a *App) { a.stopDebugByUser(p) }) + awaitStateWithin(t, h, time.Minute, "the stopped entrypoint", func() bool { return !p.build.active() && p.debug.activeSession() == nil }) +} + +func debugEntrypointsManifest() string { + manifest := strings.Replace(providerEntrypointsManifest, `"buildSystems": 0`, `"buildSystems": 2`, 1) + return strings.Replace(manifest, `"entrypoints": true`, `"entrypoints": true, "debug": {"configurationType": "fixture-attach"}`, 1) +} + +type entrypointDiscoveryKernel struct { + Kernel + discover func(context.Context, core.BuildPlanRequest) error +} + +func (k entrypointDiscoveryKernel) PlanBuildRunEntrypoints(ctx context.Context, manifest json.RawMessage, system string, request core.BuildPlanRequest) (core.BuildPlan, error) { + if err := k.discover(ctx, request); err != nil { + return core.BuildPlan{}, err + } + return k.Kernel.PlanBuildRunEntrypoints(ctx, manifest, system, request) +} + +func TestCurrentFileDebugFallsBackWhenProviderDiscoveryFails(t *testing.T) { + h := connectionDebugHarnessWithProvider(t, debugEntrypointsManifest(), nil) + failure := errors.New("fixture entrypoint discovery failed") + discovering := make(chan struct{}, 1) + finishDiscovery := make(chan struct{}) + h.app.kernel = entrypointDiscoveryKernel{ + Kernel: h.app.kernel, + discover: func(ctx context.Context, _ core.BuildPlanRequest) error { + discovering <- struct{}{} + select { + case <-finishDiscovery: + return failure + case <-ctx.Done(): + return ctx.Err() + } + }, + } + root := entrypointWorkspace(t) + h.open(root) + p := h.app.project + openSource(t, h, p, filepath.Join(root, "src", "Main.java")) + d := p.selectedDocument(p.docs.group) + h.app.post(func(a *App) { + a.chooseTitleConfiguration(p, "") + a.titleDebug(p) + }) + awaitValueWithin(t, h, 15*time.Second, discovering, "the provider discovery request") + h.app.post(func(a *App) { + d.actions = []pluginservice.EditorAction{{ + Title: "Debug editor entry", Icon: "debug", + Range: pluginservice.EditorActionRange{StartLine: 1, StartColumn: 1}, + }} + close(finishDiscovery) + }) + want := i18n.SharedFormat(h.app.language(), "desktop.editorActions.starting", map[string]string{"label": "Debug editor entry"}) + awaitStateWithin(t, h, 15*time.Second, "the editor debug action", func() bool { + notices := noticeTexts(h.app) + if slices.Contains(notices, failure.Error()) { + t.Fatalf("provider failure blocked editor debug: %v", notices) + } + return slices.Contains(notices, want) + }) +} + +func TestCurrentFileDebugSavesEditsBeforeDiscoveringAnEntrypoint(t *testing.T) { + h := connectionDebugHarnessWithProvider(t, debugEntrypointsManifest(), nil) + const modified = "package example;\n\npublic final class Main {}\n" + sourceAtDiscovery := make(chan string, 1) + h.app.kernel = entrypointDiscoveryKernel{ + Kernel: h.app.kernel, + discover: func(_ context.Context, request core.BuildPlanRequest) error { + data, err := os.ReadFile(filepath.Join(request.WorkspaceRoot, filepath.FromSlash(request.File))) + if err != nil { + return err + } + sourceAtDiscovery <- string(data) + return errors.New("fixture stops after reading the source") + }, + } + root := entrypointWorkspace(t) + h.open(root) + p := h.app.project + openSource(t, h, p, filepath.Join(root, "src", "Main.java")) + d := p.selectedDocument(p.docs.group) + data, _ := d.view.Text() + if err := d.view.ApplyEdits([]editorview.TextEdit{{Range: editorview.Range{End: len(data)}, Text: modified}}); err != nil { + t.Fatal(err) + } + h.app.post(func(a *App) { + a.chooseTitleConfiguration(p, "") + a.titleDebug(p) + }) + if got := awaitValueWithin(t, h, 15*time.Second, sourceAtDiscovery, "the source read by the provider"); got != modified { + t.Fatalf("provider read stale source: %q", got) + } +} + // sameDebugPort reports whether a DAP argument decoded from JSON is port. func sameDebugPort(value any, port int) bool { switch number := value.(type) { diff --git a/desktop/internal/workbench/editor_actions.go b/desktop/internal/workbench/editor_actions.go index 255810766..8d3bbba9b 100644 --- a/desktop/internal/workbench/editor_actions.go +++ b/desktop/internal/workbench/editor_actions.go @@ -346,8 +346,12 @@ func (a *App) editorActionMenuItems(c ui.C, p *project, d *document, menu *ui.Me // runCurrentFileEditorAction presents the same plugin invocation the gutter // offers. An absent cached action may mean its initial collection is still in flight. -func (a *App) runCurrentFileEditorAction(p *project, d *document, mode titleAction) { +func (a *App) runCurrentFileEditorAction(p *project, d *document, mode titleAction, discoveryErr error) { fallback := func(a *App) { + if discoveryErr != nil { + a.notifyError(discoveryErr.Error()) + return + } if mode == titleActionDebug { a.whenDebugAdaptersLoaded(p, func() { if p.selectedDocument(p.docs.group) == d { @@ -401,6 +405,11 @@ func (a *App) runCurrentFileEditorAction(p *project, d *document, mode titleActi return } if err != nil { + if discoveryErr != nil { + a.backgroundFailed("editor_actions", err) + fallback(a) + return + } a.operationFailed("title_run_current_file", err) a.notifyError(i18n.Shared(a.language(), "desktop.editorActions.failed")) return diff --git a/desktop/internal/workbench/runconfig.go b/desktop/internal/workbench/runconfig.go index d4a287469..b01cce80c 100644 --- a/desktop/internal/workbench/runconfig.go +++ b/desktop/internal/workbench/runconfig.go @@ -59,10 +59,11 @@ type runConfigState struct { // beforeLaunch is the start that waits for the user to allow the steps a project file // names before it (runconfig_before_launch.go). beforeLaunch *beforeLaunchConfirmation - // start is the configuration a save asked to run, started once the next refresh lists it - // so the run reads what was saved. - start string - // findingEntrypoint says Run on Current File is asking the build providers for the open + // start is the configuration a save asked to start once the next refresh lists it. + // startAction is Debug for a discovered debug entrypoint and defaults to Run. + start string + startAction titleAction + // findingEntrypoint says Current File is asking the build providers for the open // file's entrypoint (title_run_entrypoints.go), so another press does not ask again. findingEntrypoint bool // browser is the list that shows the configurations by scope and build system. @@ -167,7 +168,7 @@ func (a *App) finishRunConfigurations(p *project, generation uint64, resolved co a.loadRunConfigTypes(p, false) if err != nil { s.failure = err.Error() - s.reveal, s.start = "", "" + s.reveal, s.start, s.startAction = "", "", titleActionNone return } s.failure = "" @@ -204,10 +205,15 @@ func (a *App) finishRunConfigurations(p *project, generation uint64, resolved co } s.reveal = "" if start := s.start; start != "" { - s.start = "" + action := s.startAction + s.start, s.startAction = "", titleActionNone if s.configuration(start) != nil { s.selected = start - a.runConfiguration(p, start) + if action == titleActionDebug { + a.titleStart(p, action) + } else { + a.runConfiguration(p, start) + } } } if s.configuration(s.selected) == nil { diff --git a/desktop/internal/workbench/runconfig_dialog.go b/desktop/internal/workbench/runconfig_dialog.go index f97e44478..e98dff8af 100644 --- a/desktop/internal/workbench/runconfig_dialog.go +++ b/desktop/internal/workbench/runconfig_dialog.go @@ -1052,7 +1052,7 @@ func (a *App) applyRunConfigDialog(p *project, step runConfigDialogStep) { if id := d.selected; id != "" && !strings.HasPrefix(id, runConfigNewKey) { s.reveal, s.currentFile = id, false if step == runConfigDialogRun { - s.start = id + s.start, s.startAction = id, titleActionRun } } if step != runConfigDialogApply { diff --git a/desktop/internal/workbench/tests_provider_test.go b/desktop/internal/workbench/tests_provider_test.go index 0c9c4c337..2e717021c 100644 --- a/desktop/internal/workbench/tests_provider_test.go +++ b/desktop/internal/workbench/tests_provider_test.go @@ -256,7 +256,13 @@ func TestProviderTestRunner(t *testing.T) { } reply(request.ID, map[string]any{"format": "litheRunEntrypoints", "version": 0, "entrypoints": found}) case "lithe/build/prepareRun": - if entrypoints { + var params struct { + LaunchOptions preparedDebug `json:"launchOptions"` + } + if err := json.Unmarshal(request.Params, ¶ms); err != nil { + os.Exit(10) + } + if entrypoints && params.LaunchOptions.Debug == nil { if err := os.WriteFile("prepare-run.json", request.Params, 0o600); err != nil { os.Exit(9) } @@ -268,33 +274,25 @@ func TestProviderTestRunner(t *testing.T) { }) continue } - // service.marker selects the service fixture (build_connection_debug_test.go): the - // provider records what the host asked for and answers with a process that listens + // The provider records what the host asked for and answers with a process that listens // on the debug port the host chose, as a runtime suspended for a debugger does. - if _, err := os.Stat("service.marker"); err == nil { + _, serviceErr := os.Stat("service.marker") + if params.LaunchOptions.Debug != nil || serviceErr == nil { if err := os.WriteFile("prepare-run.json", request.Params, 0o600); err != nil { os.Exit(9) } - var params struct { - LaunchOptions struct { - Debug *struct { - Target struct { - Port int `json:"port"` - } `json:"target"` - } `json:"debug"` - } `json:"launchOptions"` - } - if err := json.Unmarshal(request.Params, ¶ms); err != nil { - os.Exit(10) - } port := 0 if params.LaunchOptions.Debug != nil { port = params.LaunchOptions.Debug.Target.Port } + directory := "lib" + if entrypoints { + directory = "." + } reply(request.ID, map[string]any{ "executable": map[string]any{"kind": "command", "value": "fixture-service"}, "arguments": []string{"-test.run=^TestFixtureServiceRunner$"}, - "workingDirectory": "lib", + "workingDirectory": directory, "environment": map[string]string{fixtureServicePortVariable: strconv.Itoa(port)}, }) continue diff --git a/desktop/internal/workbench/title_run.go b/desktop/internal/workbench/title_run.go index a43060719..688a04aed 100644 --- a/desktop/internal/workbench/title_run.go +++ b/desktop/internal/workbench/title_run.go @@ -240,8 +240,8 @@ func (a *App) decideTitleAction(p *project, action titleAction, trigger string) case "run": a.runConfiguration(p, configuration.ID) case "debug": - // A run that names a public debug type debugs as a service the registered adapter of - // that type attaches to; one that names its own plugin's adapter debugs a prepared program. + // A run that names a public debug type starts suspended so the registered + // adapter can attach. A run that names a native adapter prepares a launch. if run, ok := buildRunDeclaration(p.build.instances[index].System, reference.Run); ok && run.DebugAdapter == "" && run.DebugConfigurationType != "" { a.debugRunConfiguration(p, configuration.ID, run.DebugConfigurationType) return @@ -268,17 +268,3 @@ func (p *project) titleConfigurationFacts(index int, reference core.RunProviderR debuggable := ok && run.Debuggable() return &core.TitleConfiguration{Kind: "modelLoaded", Debuggable: &debuggable} } - -// performCurrentFileAction keeps build-provider runs on their existing path and -// offers the editor's Debug action before using the selected adapter directly. -func (a *App) performCurrentFileAction(p *project, action titleAction) { - if action == titleActionDebug { - if p.safeMode() { - a.askToTrustProject(p, func() { a.performCurrentFileAction(p, action) }) - return - } - a.runCurrentFileEditorAction(p, p.selectedDocument(p.docs.group), action) - return - } - a.runCurrentFileEntrypoint(p) -} diff --git a/desktop/internal/workbench/title_run_entrypoints.go b/desktop/internal/workbench/title_run_entrypoints.go index 8b588b4c6..1789c068b 100644 --- a/desktop/internal/workbench/title_run_entrypoints.go +++ b/desktop/internal/workbench/title_run_entrypoints.go @@ -10,10 +10,10 @@ import ( "time" "github.com/1lck/Lithe-IDEA/desktop/internal/core" - "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" + "github.com/1lck/Lithe-IDEA/desktop/internal/pluginservice" ) -// Run on Current File starts the entrypoint a build provider finds in the open file, such as a +// Run and Debug on Current File start the entrypoint a build provider finds in the open file, such as a // Java class with a main method that no build file names (#2104). A build system that declares // a run with entrypoints (buildProviders 1) answers lithe/build/runEntrypoints for a file; Core // plans the request and checks the answer. The first entrypoint is saved as a local run @@ -34,13 +34,13 @@ type entrypointFound struct { project core.BuildProject } -// runCurrentFileEntrypoint asks the build providers whose instance holds the open file for its -// entrypoints, innermost instance first, and runs the first one found. An instance's model is +// performCurrentFileAction asks the build providers whose instance holds the open file for its +// entrypoints, innermost instance first, and starts the first that supports the action. An instance's model is // loaded first, since the configuration names the model's project; a press waits for it as a // press on a configuration does. -func (a *App) runCurrentFileEntrypoint(p *project) { +func (a *App) performCurrentFileAction(p *project, action titleAction) { if p.safeMode() { - a.askToTrustProject(p, func() { a.runCurrentFileEntrypoint(p) }) + a.askToTrustProject(p, func() { a.performCurrentFileAction(p, action) }) return } s := &p.runConfigs @@ -49,25 +49,25 @@ func (a *App) runCurrentFileEntrypoint(p *project) { } doc := p.selectedDocument(p.docs.group) if doc == nil || doc.path == "" { - a.notifyInfo(i18n.Shared(a.language(), "desktop.title.currentFileNotRunnable")) + a.runCurrentFileEditorAction(p, doc, action, nil) return } relative, err := filepath.Rel(p.root, doc.path) if err != nil || relative == "." || strings.HasPrefix(relative, "..") { - a.notifyInfo(i18n.Shared(a.language(), "desktop.title.currentFileNotRunnable")) + a.runCurrentFileEditorAction(p, doc, action, nil) return } file := filepath.ToSlash(relative) if !p.build.loaded { - s.pending = titleActionRun + s.pending = action a.loadBuildSystems(p) return } - candidates := entrypointCandidates(p.build.instances, file) + candidates := entrypointCandidates(p.build.instances, file, action) for _, candidate := range candidates { instance := &p.build.instances[candidate.index] if instance.modelLoading || (!instance.modelLoaded && instance.modelError == "") { - s.pending = titleActionRun + s.pending = action if !instance.modelLoading { a.loadBuildModel(p, candidate.index) } @@ -76,42 +76,75 @@ func (a *App) runCurrentFileEntrypoint(p *project) { } candidates = slices.DeleteFunc(candidates, func(c entrypointCandidate) bool { return !c.instance.modelLoaded }) if len(candidates) == 0 { - a.runCurrentFileEditorAction(p, doc, titleActionRun) + a.runCurrentFileEditorAction(p, doc, action, nil) return } - a.performed("title.run.currentFile", surfaceToolbar) + if doc.dirty() || doc.saving || doc.conflicted { + s.findingEntrypoint = true + a.saveBeforeClose(p, doc, func(ok bool) { + s.findingEntrypoint = false + if ok && a.project == p && p.selectedDocument(p.docs.group) == doc { + a.performCurrentFileAction(p, action) + } + }) + return + } + if action == titleActionRun { + a.performed("title.run.currentFile", surfaceToolbar) + } s.findingEntrypoint = true kernel := a.kernel + documentPath, revision := doc.path, doc.view.Revision() a.background(p, buildRequestTimeout, func(ctx context.Context) func(*App) { - found, err := a.findEntrypoint(ctx, p, kernel, candidates, file) - if err == nil && found != nil { - var id string - id, err = a.saveEntrypointConfiguration(ctx, p, kernel, *found) - return func(a *App) { a.startEntrypointConfiguration(p, id, err) } - } + found, err := a.findEntrypoint(ctx, p, kernel, candidates, file, action) + cancelled := errors.Is(ctx.Err(), context.Canceled) return func(a *App) { - p.runConfigs.findingEntrypoint = false - if err != nil { + s.findingEntrypoint = false + // log-exempt: 用户取消:项目关闭或请求取消后没有入口需要启动。 + if cancelled { + return + } + if p.selectedDocument(p.docs.group) != doc || doc.path != documentPath || doc.view.Revision() != revision { + if err != nil { + a.backgroundFailed("title_run_current_file", err) + } + return + } + if found != nil { + s.findingEntrypoint = true + a.background(p, buildRequestTimeout, func(ctx context.Context) func(*App) { + id, err := a.saveEntrypointConfiguration(ctx, p, kernel, *found) + return func(a *App) { a.startEntrypointConfiguration(p, id, action, err) } + }) + return + } + if err != nil && action != titleActionDebug { a.operationFailed("title_run_current_file", err) a.notifyError(err.Error()) return } - if p.selectedDocument(p.docs.group) == doc { - a.runCurrentFileEditorAction(p, doc, titleActionRun) + if err != nil { + a.backgroundFailed("title_run_current_file", err) } + a.runCurrentFileEditorAction(p, doc, action, err) } }) } // entrypointCandidates are the instances that declare an entrypoint run and whose root holds // file, the innermost root first. -func entrypointCandidates(instances []buildInstance, file string) []entrypointCandidate { +func entrypointCandidates(instances []buildInstance, file string, action titleAction) []entrypointCandidate { var candidates []entrypointCandidate for index := range instances { instance := instances[index] if len(instance.System.EntrypointRuns) == 0 || !instance.System.Provider { continue } + if action == titleActionDebug && !slices.ContainsFunc(instance.System.EntrypointRuns, func(run pluginservice.BuildRunDeclaration) bool { + return run.DebugConfigurationType != "" + }) { + continue + } root := path.Clean(instance.Root) if root != "." && file != root && !strings.HasPrefix(file, root+"/") { continue @@ -127,7 +160,7 @@ func entrypointCandidates(instances []buildInstance, file string) []entrypointCa // findEntrypoint asks each candidate in turn and returns the first entrypoint whose project the // candidate's model lists. A candidate that fails does not stop the others; its error is // returned only when no candidate found anything. -func (a *App) findEntrypoint(ctx context.Context, p *project, kernel Kernel, candidates []entrypointCandidate, file string) (*entrypointFound, error) { +func (a *App) findEntrypoint(ctx context.Context, p *project, kernel Kernel, candidates []entrypointCandidate, file string, action titleAction) (*entrypointFound, error) { var failures []error for _, candidate := range candidates { entries, err := a.runEntrypoints(ctx, p, kernel, candidate.instance, file) @@ -136,6 +169,12 @@ func (a *App) findEntrypoint(ctx context.Context, p *project, kernel Kernel, can continue } for _, entry := range entries { + if action == titleActionDebug { + run, ok := buildRunDeclaration(candidate.instance.System, entry.Run) + if !ok || run.DebugConfigurationType == "" { + continue + } + } if project, ok := buildProjectByPath(candidate.instance.Model, entry.Project.Path); ok { return &entrypointFound{instance: candidate.instance, entry: entry, project: project}, nil } @@ -193,7 +232,7 @@ func (a *App) saveEntrypointConfiguration(ctx context.Context, p *project, kerne // startEntrypointConfiguration makes the saved configuration what the title bar runs and starts // it once the configurations are read again. -func (a *App) startEntrypointConfiguration(p *project, id string, err error) { +func (a *App) startEntrypointConfiguration(p *project, id string, action titleAction, err error) { s := &p.runConfigs s.findingEntrypoint = false if err != nil { @@ -202,7 +241,7 @@ func (a *App) startEntrypointConfiguration(p *project, id string, err error) { return } s.currentFile = false - s.reveal, s.start = id, id + s.reveal, s.start, s.startAction = id, id, action a.refreshRunConfigurations(p) } diff --git a/scripts/desktop/build-desktop-core.sh b/scripts/desktop/build-desktop-core.sh index 4729c335d..c802b1391 100755 --- a/scripts/desktop/build-desktop-core.sh +++ b/scripts/desktop/build-desktop-core.sh @@ -301,7 +301,7 @@ PINNED_CORE_REPOSITORY="https://github.com/Lithe-IDEA/Lithe-core-rust.git" # to another file name (rustup's cargo and rust-analyzer proxies) to the layout path, so probes and # launches start cargo, not rustup; #134 hardens remote connections and #136 logs the remote # connection lifecycle as structured log v2 events. -PINNED_CORE_REVISION="43e3f277c47033ca8c98e67e7da896a79fde595f" +PINNED_CORE_REVISION="39aa5fb51fcbc9c986536d1c48ab9665e4a68a62" PROFILE="debug" CHECKOUT_ONLY="no" while [[ $# -gt 0 ]]; do diff --git a/shared/platform-feature-matrix/features/run-current-file-entrypoint.json b/shared/platform-feature-matrix/features/run-current-file-entrypoint.json index f81175a47..97ab2a146 100644 --- a/shared/platform-feature-matrix/features/run-current-file-entrypoint.json +++ b/shared/platform-feature-matrix/features/run-current-file-entrypoint.json @@ -2,10 +2,10 @@ "id": "run-current-file-entrypoint", "area": "运行与调试", "group": "运行配置", - "capability": "标题栏选择“当前文件”后可运行当前源码。构建提供者找到入口时自动保存、选中并运行配置,同一入口复用已保存的配置,可编辑参数、环境变量和工作目录。构建提供者没有返回入口时,运行复用编辑器行号栏的 Run 动作,调试复用它的 Debug 选项或独立的 Debug 动作。", + "capability": "标题栏选择“当前文件”后可运行或调试当前源码。构建提供者找到支持所选操作的入口时,保存并选中运行配置,同一入口复用已保存的配置。声明公共调试类型的入口可用已安装的调试适配器启动。没有匹配的入口时,继续提供编辑器行号栏的 Run 或 Debug 动作。调试查找失败时,也会尝试当前文件的 Debug 动作。", "owner": "Run", - "notes": "Viva 共享实现。构建入口由提供者回答,Core 规划请求并校验答案。编辑器动作按源码位置选择并通过原有调用路径执行。显示通用的无入口提示前,先查看当前文档是否有可用的 Run 动作。", - "verification": "用合成构建提供者验证运行当前文件时保存并复用配置,无入口时提示原因。用合成编辑器动作提供者验证未声明构建入口的文件可从标题栏执行行号栏的动作。自动化入口在 title_run_entrypoints_test.go。", + "notes": "Viva 共享实现。构建入口由提供者回答,Core 规划请求并校验答案。运行和调试读取同一个配置的参数、环境变量和工作目录。编辑器动作按源码位置选择。", + "verification": "用合成构建提供者验证当前文件运行时保存并复用配置。用声明公共调试类型的入口验证首次点击调试会保存配置、携带入口类名并连接对应适配器。用编辑器动作提供者验证没有适用构建入口和调试查找失败时的回退。修改缓冲区里的入口后点击调试,确认构建提供者读取保存后的文件。", "macos": { "implementationStatus": "implemented", "verificationStatus": "pending", @@ -14,7 +14,8 @@ "desktop/internal/core/build_entrypoints.go", "desktop/internal/pluginservice/builds.go", "desktop/internal/workbench/title_run_entrypoints_test.go", - "desktop/internal/workbench/editor_actions.go" + "desktop/internal/workbench/editor_actions.go", + "desktop/internal/workbench/build_connection_debug_test.go" ] }, "windows": { @@ -25,7 +26,8 @@ "desktop/internal/core/build_entrypoints.go", "desktop/internal/pluginservice/builds.go", "desktop/internal/workbench/title_run_entrypoints_test.go", - "desktop/internal/workbench/editor_actions.go" + "desktop/internal/workbench/editor_actions.go", + "desktop/internal/workbench/build_connection_debug_test.go" ] } } diff --git a/shared/platform-feature-matrix/features/viva-title-run-debug.json b/shared/platform-feature-matrix/features/viva-title-run-debug.json index 60740c0d9..7a1bea10b 100644 --- a/shared/platform-feature-matrix/features/viva-title-run-debug.json +++ b/shared/platform-feature-matrix/features/viva-title-run-debug.json @@ -2,7 +2,7 @@ "id": "viva-title-run-debug", "area": "运行与调试", "group": "运行配置", - "capability": "标题栏右上角的运行配置下拉、运行和调试按钮:下拉列出构建提供者检测到的运行配置并可选择“当前文件”,运行按所选配置交给提供者启动并打开运行窗口,调试让提供者准备该运行声明的调试适配器后在调试窗口启动会话;声明了公共调试类型(`debug.configurationType`)的服务运行,宿主选出唯一一个已启用、能连接该类型的调试适配器(可来自其他插件),分配本机端口让提供者以等待调试器的方式启动服务,端口打开后连接调试器,调试会话和服务一起结束;没有或有多个匹配适配器时在准备前提示原因;插件安装、启用、停用或卸载后,项目重新读取调试适配器;运行在语言服务器里的调试适配器,要等语言服务器声明可执行命令后才请求调试端口(有的语言服务器在就绪后才动态注册命令);运行的进程(包括常驻服务)一直运行到自己退出、用户停止或项目关闭,只有运行前的准备受时限约束;配置尚未读出时按下会等读出后再执行;按下后做什么(等待、先加载模型、运行、调试或提示)由 Core 判定;没有运行配置时,下拉说明被隐藏的已保存配置数,“编辑运行配置…”提示原因而不是无反应;未声明名称的提供者运行以目标名命名;提供者准备运行时输出的构建日志(如编译错误)显示在该运行的控制台;声明了提供者进程、但用命令读取模型和依赖的构建系统按各自声明的来源读取", + "capability": "标题栏右上角的运行配置下拉、运行和调试按钮:下拉列出构建提供者检测到的运行配置并可选择“当前文件”,运行按所选配置交给提供者启动并打开运行窗口,调试让提供者准备该运行声明的调试适配器后在调试窗口启动会话;声明了公共调试类型(`debug.configurationType`)的服务或提供者应用运行,宿主选出唯一一个已启用、能连接该类型的调试适配器(可来自其他插件),分配本机端口让提供者以等待调试器的方式启动进程,端口打开后连接调试器,调试会话和进程一起结束;没有或有多个匹配适配器时在准备前提示原因;插件安装、启用、停用或卸载后,项目重新读取调试适配器;运行在语言服务器里的调试适配器,要等语言服务器声明可执行命令后才请求调试端口(有的语言服务器在就绪后才动态注册命令);运行的进程(包括常驻服务)一直运行到自己退出、用户停止或项目关闭,只有运行前的准备受时限约束;配置尚未读出时按下会等读出后再执行;按下后做什么(等待、先加载模型、运行、调试或提示)由 Core 判定;没有运行配置时,下拉说明被隐藏的已保存配置数,“编辑运行配置…”提示原因而不是无反应;未声明名称的提供者运行以目标名命名;提供者准备运行时输出的构建日志(如编译错误)显示在该运行的控制台;声明了提供者进程、但用命令读取模型和依赖的构建系统按各自声明的来源读取", "macos": { "implementationStatus": "implemented", "verificationStatus": "pending", @@ -48,6 +48,6 @@ ] }, "owner": "Run", - "verification": "打开带构建提供者的项目(测试内联的合成 Build Provider 即可):标题栏下拉列出检测到的运行配置并记住所选项;运行按钮启动所选配置并打开运行窗口;调试按钮对声明了调试适配器的运行准备并启动调试会话、打开调试窗口,未声明时提示该配置不能调试;声明了公共调试类型的服务运行(合成提供者 + 另一个合成插件的 attach 适配器),调试时提供者收到 `launchOptions.debug`(选中的适配器、类型、本机端口),服务监听后适配器以 attach 连接该端口,在调试窗口停止后服务结束、端口释放;没有或有两个匹配适配器时提示原因且不请求提供者;停用、再启用适配器所在插件后,调试适配器列表随之更新;运行超过准备时限的服务不会被结束(测试把时限缩短到 5 秒);语言服务器就绪后才声明命令时,宿主等到声明再请求端口,从不声明时在时限内结束;准备失败时结束“准备中”并显示原因;当前文件没有可运行的入口时提示原因(有入口时的运行见 run-current-file-entrypoint);项目没有运行配置时点“编辑运行配置…”提示原因,有旧版或其他提供方的已保存配置时说明隐藏了几条。提供者运行准备失败时,运行控制台显示提供者输出的构建日志;未声明名称的运行,配置名就是目标名。单元测试覆盖等待配置读出后运行、没有配置时编辑入口的提示和隐藏条数、当前文件提示、无适配器提示、不经构建提供者启动的配置提示、提供者准备请求、按名称选择适配器和拒绝插件未安装的适配器、准备运行时转发提供者输出、未命名运行按目标命名,声明提供者进程但用命令读取模型和写文件的依赖命令,以及按配置类型连接调试服务运行(选择、拒绝、停止联动)。", - "notes": "Viva 标题栏按钮此前只有外观。运行忙碌时仅提示,不排队;当前文件运行优先使用构建提供者的入口,其次使用编辑器动作。当前文件调试优先使用编辑器动作(run-current-file-entrypoint);同包适配器的提供者调试暂不携带运行配置的启动选项;按配置类型连接的服务运行调试一个项目同时只有一个调试会话。调试按钮可拖到左右侧栏,功能照旧,运行和运行配置下拉只留在标题栏(见 workbench-tool-window-docking)。macOS、Windows 原生窗口验收 pending。" + "verification": "打开带构建提供者的项目(测试内联的合成 Build Provider 即可):标题栏下拉列出检测到的运行配置并记住所选项;运行按钮启动所选配置并打开运行窗口;调试按钮对声明了调试适配器的运行准备并启动调试会话、打开调试窗口,未声明时提示该配置不能调试;声明了公共调试类型的服务或提供者应用运行(合成提供者 + 另一个合成插件的 attach 适配器),调试时提供者收到 `launchOptions.debug`(选中的适配器、类型、本机端口),服务监听后适配器以 attach 连接该端口,在调试窗口停止后服务结束、端口释放;没有或有两个匹配适配器时提示原因且不请求提供者;停用、再启用适配器所在插件后,调试适配器列表随之更新;运行超过准备时限的服务不会被结束(测试把时限缩短到 5 秒);语言服务器就绪后才声明命令时,宿主等到声明再请求端口,从不声明时在时限内结束;准备失败时结束“准备中”并显示原因;当前文件没有可运行的入口时提示原因(有入口时的运行和调试见 run-current-file-entrypoint);项目没有运行配置时点“编辑运行配置…”提示原因,有旧版或其他提供方的已保存配置时说明隐藏了几条。提供者运行准备失败时,运行控制台显示提供者输出的构建日志;未声明名称的运行,配置名就是目标名。单元测试覆盖等待配置读出后运行、没有配置时编辑入口的提示和隐藏条数、当前文件提示、无适配器提示、不经构建提供者启动的配置提示、提供者准备请求、按名称选择适配器和拒绝插件未安装的适配器、准备运行时转发提供者输出、未命名运行按目标命名,声明提供者进程但用命令读取模型和写文件的依赖命令,以及按配置类型连接调试服务运行(选择、拒绝、停止联动)。", + "notes": "Viva 标题栏按钮此前只有外观。运行忙碌时仅提示,不排队;当前文件运行优先使用构建提供者的入口,其次使用编辑器动作。当前文件调试优先使用声明了公共调试类型的构建入口,也可使用编辑器动作(run-current-file-entrypoint);同包适配器的提供者调试暂不携带运行配置的启动选项;按配置类型连接的运行调试一个项目同时只有一个调试会话。调试按钮可拖到左右侧栏,功能照旧,运行和运行配置下拉只留在标题栏(见 workbench-tool-window-docking)。macOS、Windows 原生窗口验收 pending。" }