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,57 @@
# Agent 笔记:运行配置的启动前步骤

状态:已实现

## 先说结论

运行配置可以列出启动前步骤(#1616,对应 IntelliJ IDEA 的 “Before launch: Run Another Configuration”):

- 配置文件里写 `beforeLaunch: [{configuration: <id>}]`,最多 16 项。每一项只能引用内置进程运行(`process.shell`、`process.powershell`、`process.batch`、`process.executable`),不能引用自己。
- 一层(项目文件或 `local.yaml`)写了 `beforeLaunch` 就整体替换下层的列表;`local.yaml` 写空列表可以关掉项目文件里的步骤。
- 宿主启动前调用 Core 的 `runConfig.beforeLaunch`。Core 展开嵌套步骤(被引用的配置自己的步骤先运行),展开后最多 32 步;引用不存在、不能作为步骤、在本机不可用、成环、过长时返回 `rejected` 和原因码。
- 只要展开过程中用到了项目文件写的列表,Core 就返回确认指纹:对列表和这些步骤启动的命令定义求 sha256。宿主第一次运行前弹框列出步骤和命令,用户点“运行步骤”后把指纹存进应用自己的存储 `run-before-launch-approvals.json`(按项目根目录,每个项目最多 64 个)。命令或列表一改,指纹就变,会再问一次。
- 步骤按顺序在被启动配置的 Run 控制台里运行,每一步前写一行暗色标题。某一步退出码不是 0,就写出步骤名和退出码,不启动配置。停止配置会结束正在运行的步骤。
- 内置运行编辑器(Core 描述的表单,字段类型 `beforeLaunch`,候选列表由 Core 给出)和构建提供者运行编辑器都可以添加、上移、下移、移除步骤。构建提供者编辑器只有列表改动时才发送 `beforeLaunch`,不会把解析后的列表抄进本地层。

## 问题

用户要在启动服务前先跑生成代码、编译前端、拉起依赖等命令。原来只能手动先点一个配置、等它结束再点另一个;也没有地方表达“这个配置依赖那几个命令”。IDEA 用户习惯在运行配置里配置启动前任务。

同时,项目文件是仓库内容,打开别人的仓库后点运行就执行仓库指定的任意命令,等于让仓库在用户机器上执行代码而不经确认。

## 决策

- 步骤只引用已经存在的内置进程运行,不在 `beforeLaunch` 里内联命令。这样每个步骤都能单独编辑、单独运行,展开、校验和命令定义都由 Core 统一处理,宿主只负责按顺序启动进程。
- 校验、展开和指纹都放在 Core:三端行为一致,宿主不需要知道层级合并规则。保存时 Core 也用同一套规则检查(`check_edit`),成环在保存时报 `conflict`。
- 确认记录放在应用存储而不是项目里,仓库不能替用户批准自己的步骤。指纹包含命令定义,所以仓库更新步骤命令后会重新询问。用户自己 `local.yaml` 里的列表不询问。
- 步骤的进程用准备阶段的上下文启动(`op.bind`),停止配置时上下文取消,进程随之结束;每一步受 `runPreparationTimeout` 限制。
- 步骤失败的提示在窗口线程上直接写进控制台(`writeBuildOutputNow`),然后结束操作。用 `streamBuildOutput` 会异步投递,到达时操作已经结束,提示会被丢弃。

## 考虑过的备选方案

- 支持 IDEA 的全部启动前任务类型(Build、Run Maven Goal、Launch Web Browser 等)。先做“运行另一个配置”,其他类型由插件的 `runOptions` 扩展点以后补充(#2100 中尚未实现的部分)。
- 步骤并行运行。依赖顺序是用户最常见的需求,并行会让失败原因和输出交错,先保持顺序执行。
- 每次运行都询问项目文件里的步骤。会打断正常工作流,用户很快就会习惯性点确认;按指纹记住一次,命令改变时再问,兼顾安全和效率。

## 后果

- 用户可以在一个配置里串起准备命令,一键运行。
- 打开不信任的仓库时,第一次运行会看到将要执行的命令列表。
- 以后新增步骤类型时,要在 Core 的 `before_launch.rs` 加类型并纳入指纹,在宿主 `runBeforeLaunchStep` 加执行方式;确认框必须能展示新类型会做什么。

## 验证

- Core:`cargo test -p lithe-core before_launch`(`core/src/tests/before_launch_steps.rs` 覆盖展开、确认指纹、拒绝原因、编辑校验和本地层清空)。
- 宿主:`./scripts/desktop/desktop-go.sh test ./internal/workbench/ -run 'BeforeLaunch|TestRunConfigFormKeepsTheStepList' -count=1`,用真实 Core 和系统 shell/cmd 运行步骤,覆盖顺序、确认与记忆、改动后重新确认、取消、失败退出码和停止。

## 适用范围

- `desktop/internal/workbench/runconfig_before_launch.go`
- `desktop/internal/workbench/runconfig.go`
- `desktop/internal/workbench/runconfig_process.go`
- `desktop/internal/workbench/runconfig_form.go`
- `desktop/internal/workbench/runconfig_form_view.go`
- `desktop/internal/workbench/runconfig_edit.go`
- `desktop/internal/workbench/runconfig_view.go`
- `desktop/internal/core/run_before_launch.go`
- Core:`rust/crates/execution/src/configuration/before_launch.rs`
44 changes: 44 additions & 0 deletions desktop/internal/core/run_before_launch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package core

import "context"

// RunBeforeLaunch is what `runConfig.beforeLaunch` answers: Status "ok" with the
// steps to run in order and, when a project file names them, the Confirmation to
// ask for; or "rejected" with the Refusal of the step that cannot run.
type RunBeforeLaunch struct {
Status string `json:"status"`
Steps []RunBeforeLaunchItem `json:"steps"`
Confirmation *RunBeforeLaunchConfirmation `json:"confirmation"`
Refusal *RunBeforeLaunchRefusal `json:"refusal"`
}

// RunBeforeLaunchItem is one run to start before the configuration: its ID, its
// name and a line naming what it starts, shown in the confirmation.
type RunBeforeLaunchItem struct {
ConfigurationID string `json:"configurationId"`
Name string `json:"name"`
Summary string `json:"summary"`
}

// RunBeforeLaunchConfirmation identifies a list of steps a project file names; the
// host asks once per Fingerprint before running it.
type RunBeforeLaunchConfirmation struct {
Fingerprint string `json:"fingerprint"`
}

// RunBeforeLaunchRefusal is why the steps cannot run: Code is notFound,
// notAllowed, unavailable, cycle or tooLong, about step ConfigurationID.
type RunBeforeLaunchRefusal struct {
Code string `json:"code"`
ConfigurationID string `json:"configurationId"`
}

// RunBeforeLaunch expands the built-in process runs configurationID starts first.
func (c *Client) RunBeforeLaunch(ctx context.Context, root, configurationID string) (RunBeforeLaunch, error) {
var result RunBeforeLaunch
err := c.Execute(ctx, "runConfig.beforeLaunch", struct {
Root string `json:"root"`
ConfigurationID string `json:"configurationId"`
}{root, configurationID}, &result)
return result, err
}
11 changes: 11 additions & 0 deletions desktop/internal/core/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ type RunConfiguration struct {
// Availability is set when the saved configuration cannot run, such as when
// the plugin it needs is not installed. The host shows it and never starts it.
Availability *RunAvailability `json:"availability,omitempty"`
// BeforeLaunch names the built-in process runs started, in order, before this one.
BeforeLaunch []RunBeforeLaunchStep `json:"beforeLaunch,omitempty"`
}

// RunBeforeLaunchStep is one step of a configuration's beforeLaunch: the built-in
// process run to start first.
type RunBeforeLaunchStep struct {
Configuration string `json:"configuration"`
}

// RunPluginDependency is the plugin a configuration needs: its ID, and what the
Expand Down Expand Up @@ -265,6 +273,9 @@ type RunOptionsEdit struct {
Environment map[string]string `json:"environment"`
// ProviderRunOptions replaces the build provider choices of the selected layer when set.
ProviderRunOptions json.RawMessage `json:"providerRunOptions,omitempty"`
// BeforeLaunch replaces the steps of the selected layer when set; an empty list
// removes them (and in the local layer hides the project's).
BeforeLaunch *[]string `json:"beforeLaunch,omitempty"`
// Plugins is the enabled plugin list of ResolveRunConfigurations, so a plugin
// document Core writes records the installed plugin.
Plugins []RunInstalledPlugin `json:"plugins,omitempty"`
Expand Down
8 changes: 8 additions & 0 deletions desktop/internal/core/run_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ type RunEditorField struct {
Options []string `json:"options,omitempty"`
VisibleWhen *RunFieldCondition `json:"visibleWhen,omitempty"`
Note string `json:"note,omitempty"`
// Candidates are the configurations a beforeLaunch field may name.
Candidates []RunConfigurationName `json:"candidates,omitempty"`
}

// RunConfigurationName is a run configuration's ID and name.
type RunConfigurationName struct {
ID string `json:"id"`
Name string `json:"name"`
}

// RunFieldCondition shows a field only while Field has the value Equals.
Expand Down
44 changes: 44 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.

2 changes: 2 additions & 0 deletions desktop/internal/workbench/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ type Kernel interface {
SaveRunConfiguration(ctx context.Context, request core.RunConfigurationSave) (core.RunConfigurationSaved, error)
DeleteRunConfiguration(ctx context.Context, root, configurationID string, plugins []core.RunInstalledPlugin) ([]core.RunConfigChange, error)
CreateRunLaunchPlan(ctx context.Context, root, configurationID, inlineScriptBase string) (core.RunLaunchPlan, error)
// RunBeforeLaunch expands the steps a configuration runs before it starts (runconfig_before_launch.go).
RunBeforeLaunch(ctx context.Context, root, configurationID string) (core.RunBeforeLaunch, error)
RunArgumentText(ctx context.Context, lists [][]string) ([]string, error)
SetDebugBreakpoints(ctx context.Context, sessionID, sourcePath string, breakpoints []core.DebugSourceBreakpoint) (core.DebugUpdate, error)
ExecuteDebug(ctx context.Context, sessionID, operationID, command string, threadID *int64, granularity string) (core.DebugUpdate, error)
Expand Down
12 changes: 12 additions & 0 deletions desktop/internal/workbench/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,18 @@ func (a *App) streamBuildOutput(p *project, op *buildOperation, text string) {
})
}

// writeBuildOutputNow adds text to op's console on the window thread at once, for a line the
// window writes right before it ends op: a posted line would arrive after op ended and be dropped.
func (a *App) writeBuildOutputNow(p *project, op *buildOperation, text string) {
if text == "" {
return
}
op.decoder.deliver(text, func(chunk outputChunk, failure error) {
a.reportOutputStyleFailure(failure)
a.applyBuildOutput(p, op, chunk)
})
}

// applyBuildOutput adds decoded output to op's console. Output of an operation the window no
// longer runs is dropped, so it never lands in a later execution's console.
func (a *App) applyBuildOutput(p *project, op *buildOperation, chunk outputChunk) {
Expand Down
1 change: 1 addition & 0 deletions desktop/internal/workbench/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ func (a *App) overlays(c ui.C) {
a.drawRunConfigEditor(c, p)
a.drawRunConfigForm(c, p)
a.drawRunConfigDelete(c, p)
a.drawBeforeLaunchConfirmation(c, p)
a.buildOptionsDialog(c, p)
a.drawStartupSweep(c)
a.drawProjectReplace(c, p)
Expand Down
16 changes: 14 additions & 2 deletions desktop/internal/workbench/runconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type runConfigState struct {
newMenu bool
// deleting is the user configuration whose delete waits for confirmation.
deleting *runConfigDeletion
// 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
Expand Down Expand Up @@ -286,6 +289,16 @@ func (a *App) startRunConfigurationWith(p *project, id string, debug *connection
a.endBuildOperation(p, op, "failed", nil, "unsaved documents could not be saved", "")
return
}
// The steps a configuration names run first; Run and Debug both run them.
a.runBeforeLaunch(p, op, id, func() { a.prepareConfiguredRun(p, op, id, reference, index, debug) })
})
return op, nil
}

// prepareConfiguredRun asks Core for the saved launch options of configuration id and has the
// build provider prepare and start the run.
func (a *App) prepareConfiguredRun(p *project, op *buildOperation, id string, reference core.RunProviderReference, index int, debug *connectionDebug) {
{
root, kernel := p.root, a.kernel
a.background(p, buildRequestTimeout, func(ctx context.Context) func(*App) {
prepare, release := op.bind(ctx)
Expand Down Expand Up @@ -316,8 +329,7 @@ func (a *App) startRunConfigurationWith(p *project, id string, debug *connection
a.runBuildTargetWith(p, op, &p.build.instances[index], inputs.Reference.ProjectID, inputs.Reference.TargetID, inputs.Reference.Run, options)
}
})
})
return op, nil
}
}

// buildInstanceIndex returns the detected build instance a provider reference names, or -1.
Expand Down
Loading
Loading