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,55 @@
# Agent 笔记:当前文件运行(按需运行入口)

状态:已实现

## 先说结论

标题栏选“当前文件”后点运行,可以直接运行当前文件里的入口,例如没有在 POM 里写明主类、但带 `main` 方法的 Java 类(#2104):

- 插件在构建系统里声明入口运行 `{"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...`;测试源码不算,因为运行使用的运行时类路径不含测试类。不编译,回答很快。

## 问题

插件读取模型时不编译,也不会把每个 main 类都列成运行配置,否则下拉会被工具类淹没。于是 POM 里没写主类的 main 类没有运行配置,选“当前文件”也只能提示“需要运行配置”,用户只能去改 POM。

## 决策

- 由插件按文件回答入口,而不是宿主按语言判断:宿主和 Core 不认识 Java,任何构建插件都能用同一机制提供“运行这个文件”。
- 入口运行不绑定目标,配置引用只到项目。这样构建树不需要为每个可能的入口建目标,入口也能来自模型没有列出的类。
- 能力版本:`buildProviders` 和 `litheBuild` 都升到 1。旧宿主不声明 1,不会安装需要入口运行的插件版本;SDK 按宿主提供的版本协商最新的一个。
- 入口作为配置身份的一部分,同一模块里不同类各存一条配置,同一个类不会重复保存。
- 一个文件有多个入口时先运行第一个;选择菜单以后需要时再加。

## 考虑过的备选方案

- 模型读取时扫描源码把所有 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)。

## 适用范围

- `desktop/internal/workbench/title_run_entrypoints.go`
- `desktop/internal/workbench/title_run.go`
- `desktop/internal/workbench/build.go`(`runBuildTargetWith` 接受没有目标的运行)
- `desktop/internal/core/build_entrypoints.go`
- `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`
6 changes: 5 additions & 1 deletion desktop/internal/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ type BuildPlanRequest struct {
Refresh bool `json:"refresh,omitempty"`
Tool *BuildTool `json:"tool,omitempty"`
WorkspaceRoot string `json:"workspaceRoot,omitempty"`
// LaunchOptions are a run configuration's choices (buildProviders 10).
// LaunchOptions are a run configuration's choices.
LaunchOptions *RunLaunchOptions `json:"launchOptions,omitempty"`
// File is the workspace-relative source file build.runEntrypointsPlan asks about.
File string `json:"file,omitempty"`
}

// BuildProviderRequest is the typed request a provider-backed plan names.
Expand Down Expand Up @@ -277,6 +279,8 @@ type buildOutcome struct {
Discovery *BuildTestDiscovery `json:"discovery"`
// Tree answers build.modelTree.
Tree *BuildModelTree `json:"tree"`
// Entrypoints answers build.decodeRunEntrypoints.
Entrypoints *BuildRunEntrypoints `json:"entrypoints"`
}

func (o buildOutcome) failure(command string) error {
Expand Down
86 changes: 86 additions & 0 deletions desktop/internal/core/build_entrypoints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package core

import (
"context"
"encoding/json"
)

// BuildRunEntrypoint is one entrypoint a provider found in a source file (buildProviders 1).
// Core checked that Run is an entrypoint run of the build system and that Project is inside
// the instance root; Entrypoint is opaque to the host and goes back as launchOptions.entrypoint.
type BuildRunEntrypoint struct {
Run string `json:"run"`
Name string `json:"name"`
Entrypoint string `json:"entrypoint"`
Project BuildSelection `json:"project"`
}

// BuildRunEntrypoints is Core's answer to build.decodeRunEntrypoints.
type BuildRunEntrypoints struct {
Entrypoints []BuildRunEntrypoint `json:"entrypoints"`
}

// BuildDecodeRunEntrypointsRequest carries the plan context the request ran with and the
// litheRunEntrypoints document the provider answered.
type BuildDecodeRunEntrypointsRequest struct {
Root string `json:"root"`
Options map[string]json.RawMessage `json:"options,omitempty"`
PluginDirectory string `json:"pluginDirectory,omitempty"`
WorkspaceStateDirectory string `json:"workspaceStateDirectory,omitempty"`
Result json.RawMessage `json:"result"`
}

// PlanBuildRunEntrypoints plans the provider request that names the entrypoints of
// request.File. Only a declaration with an entrypoint run accepts it; the request names a file,
// never a project or target.
func (c *Client) PlanBuildRunEntrypoints(ctx context.Context, manifest json.RawMessage, buildSystemID string, request BuildPlanRequest) (BuildPlan, error) {
return c.planBuild(ctx, "build.runEntrypointsPlan", manifest, buildSystemID, request)
}

// DecodeBuildRunEntrypoints validates a provider's entrypoints, in the provider's order without
// duplicates. Core rejects the whole document rather than offer a partial list.
func (c *Client) DecodeBuildRunEntrypoints(ctx context.Context, manifest json.RawMessage, buildSystemID string, request BuildDecodeRunEntrypointsRequest) ([]BuildRunEntrypoint, error) {
const command = "build.decodeRunEntrypoints"
var result buildOutcome
if err := c.Execute(ctx, command, struct {
Manifest json.RawMessage `json:"manifest"`
BuildSystemID string `json:"buildSystemID"`
Request BuildDecodeRunEntrypointsRequest `json:"request"`
}{manifest, buildSystemID, request}, &result); err != nil {
return nil, err
}
if err := result.failure(command); err != nil {
return nil, err
}
if result.Entrypoints == nil {
return nil, newParseError(command, "run entrypoints returned no entrypoints", "")
}
return result.Entrypoints.Entrypoints, nil
}

// RunEntrypointConfigurationRequest saves the configuration that starts one entrypoint: a
// local build provider configuration whose reference names no target and whose
// extensions.build-run.entrypoint is Entrypoint. Saving the same entrypoint again answers the
// configuration already saved.
type RunEntrypointConfigurationRequest struct {
Root string `json:"root"`
Name string `json:"name"`
Build RunProviderReference `json:"build"`
Entrypoint string `json:"entrypoint"`
Plugins []RunInstalledPlugin `json:"plugins,omitempty"`
}

// CreateRunEntrypointConfiguration returns the ID of the configuration that starts the
// entrypoint and the file changes that save it. Core writes nothing.
func (c *Client) CreateRunEntrypointConfiguration(ctx context.Context, request RunEntrypointConfigurationRequest) (string, []RunConfigChange, error) {
var result struct {
ID string `json:"id"`
Changes []RunConfigChange `json:"changes"`
}
err := c.Execute(ctx, "runConfig.createUserConfiguration", struct {
RunEntrypointConfigurationRequest
Scope string `json:"scope"`
Kind string `json:"type"`
}{request, "local", "buildProvider"}, &result)
return result.ID, result.Changes, err
}
4 changes: 2 additions & 2 deletions desktop/internal/i18n/shared.generated.go

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

7 changes: 5 additions & 2 deletions desktop/internal/pluginhost/pluginhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ type Capabilities map[string][]int
// - buildSystems and buildProviders: the Build window detects instances,
// loads provider models, resolves the ordered executable candidates Core
// returns, runs command and provider tasks, prepares application targets
// and discovers and runs provider tests. Run sessions a backend starts are
// owned by the host.
// and discovers and runs provider tests. Run on Current File asks the
// providers of version 1 for the file's entrypoints and saves the first as a
// run configuration. Run sessions a backend starts are owned by the host.
// - toolDefinitions: settings discovery, Build/Run selection and tool
// launches use the shared Core resolver.
// - commandInstallers: plugin management reports the commands a
Expand Down Expand Up @@ -167,6 +168,8 @@ func Declared() Capabilities {
// Version 1 adds module sidebars, field sections and entry lists, which the
// settings window's source forms render.
declared["sourceSettings"] = []int{0, 1}
// Version 1 adds entrypoint runs, which Run on Current File starts.
declared["buildProviders"] = []int{0, 1}
return declared
}

Expand Down
4 changes: 4 additions & 0 deletions desktop/internal/pluginhost/pluginhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func TestDeclaredServesTheBaselineOfEveryWiredCapability(t *testing.T) {
if name == "sourceSettings" {
wanted = []int{0, 1}
}
// Run on Current File starts the entrypoint runs version 1 adds.
if name == "buildProviders" {
wanted = []int{0, 1}
}
if versions := declared[name]; !slices.Equal(versions, wanted) {
t.Fatalf("%s = %v, want %v", name, versions, wanted)
}
Expand Down
9 changes: 9 additions & 0 deletions desktop/internal/pluginservice/builds.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type BuildSystemDeclaration struct {
Provider bool
Tasks []BuildTaskDeclaration
Runs []BuildRunDeclaration
// EntrypointRuns are the runs declared with entrypoints (buildProviders 1): the provider
// names the entrypoints a source file holds, and each starts in its project without a
// target. They never appear among Runs, which apply to targets.
EntrypointRuns []BuildRunDeclaration
// Test is the presentation part of the optional test declaration. Core
// still owns whether a selection can run; the window only needs to know
// that the system offers tests and on which scopes.
Expand Down Expand Up @@ -181,6 +185,7 @@ type rawBuildManifest struct {
Name string `json:"name"`
Source string `json:"source"`
Execution string `json:"execution"`
Entrypoints bool `json:"entrypoints"`
FromTargets *struct {
Tags []string `json:"tags"`
} `json:"fromTargets"`
Expand Down Expand Up @@ -235,6 +240,10 @@ 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 {
continue
}
Expand Down
5 changes: 5 additions & 0 deletions desktop/internal/workbench/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ type Kernel interface {
// BuildModelTree lays out a loaded model as the Build tool window's tree (build_tree.go).
BuildModelTree(ctx context.Context, manifest json.RawMessage, buildSystemID string, request core.BuildModelTreeRequest) (core.BuildModelTree, error)
DecodeBuildRun(ctx context.Context, manifest json.RawMessage, buildSystemID string, request core.BuildDecodeRunRequest) (core.BuildLaunch, error)
// PlanBuildRunEntrypoints, DecodeBuildRunEntrypoints and CreateRunEntrypointConfiguration
// run the entrypoint the current file holds (title_run_entrypoints.go).
PlanBuildRunEntrypoints(ctx context.Context, manifest json.RawMessage, buildSystemID string, request core.BuildPlanRequest) (core.BuildPlan, error)
DecodeBuildRunEntrypoints(ctx context.Context, manifest json.RawMessage, buildSystemID string, request core.BuildDecodeRunEntrypointsRequest) ([]core.BuildRunEntrypoint, error)
CreateRunEntrypointConfiguration(ctx context.Context, request core.RunEntrypointConfigurationRequest) (string, []core.RunConfigChange, error)
ToolchainRegistryOf(ctx context.Context, manifests []json.RawMessage) (core.ToolchainRegistry, error)
// CommandInstallerRequirements, PlanCommandInstaller and CommandInstallerDefaultDirectory
// read a plugin's commandInstallers[]: which systemCommand launches name which commands,
Expand Down
17 changes: 11 additions & 6 deletions desktop/internal/workbench/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1770,11 +1770,16 @@ func (a *App) runBuildTargetWith(p *project, op *buildOperation, instance *build
a.finishBuildError(p, op, errors.New(a.text.Build.ProjectMissing))
return
}
target, ok := buildTargetByID(instance.Model, targetID)
if !ok {
a.notifyError(a.text.Build.TargetMissing)
a.finishBuildError(p, op, errors.New(a.text.Build.TargetMissing))
return
// An entrypoint run names no target: it starts launchOptions.entrypoint in the project.
var selectedTarget *core.BuildSelection
if targetID != "" {
target, ok := buildTargetByID(instance.Model, targetID)
if !ok {
a.notifyError(a.text.Build.TargetMissing)
a.finishBuildError(p, op, errors.New(a.text.Build.TargetMissing))
return
}
selectedTarget = &core.BuildSelection{Name: target.Name, Path: target.Path, Tags: target.Tags, Properties: target.Properties}
}
captured := *instance
kernel := a.kernel
Expand All @@ -1790,7 +1795,7 @@ func (a *App) runBuildTargetWith(p *project, op *buildOperation, instance *build
WorkspaceRoot: p.root,
Run: runID,
Project: &core.BuildSelection{Name: project.Name, Path: project.Path, Properties: project.Properties},
Target: &core.BuildSelection{Name: target.Name, Path: target.Path, Tags: target.Tags, Properties: target.Properties},
Target: selectedTarget,
LaunchOptions: options,
}
a.addBuildToolHint(prepare, p, captured, &request)
Expand Down
3 changes: 3 additions & 0 deletions desktop/internal/workbench/runconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type runConfigState struct {
// 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
// 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.
browser runConfigBrowser
// currentFile says the user chose Current File in the title bar, so its Run and Debug act
Expand Down
Loading
Loading