Skip to content

Commit d833d6a

Browse files
1lckclaude
andauthored
feat(build-providers): buildProviders 1 按源文件找运行入口(当前文件运行) (#89)
* feat(build-providers): buildProviders 1 按源文件找运行入口 Refs 1lck/Lithe-IDEA#2104 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Offer litheBuild 1 in engine tests; document runEntrypoints Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bb4832e commit d833d6a

35 files changed

Lines changed: 966 additions & 74 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ or removing a command fails until the table changes in the same commit.
535535
| `build.decodeRunPreparation` | Validate portable launch data before the native Run host starts a process |
536536
| `build.testDiscoveryPlan` | Plan opt-in Provider test discovery for an instance |
537537
| `build.decodeTestDiscovery` | Validate, deduplicate and order Provider test selectors |
538+
| `build.runEntrypointsPlan` | Plan a Provider request for the run entrypoints of one source file (`buildProviders` 1) |
539+
| `build.decodeRunEntrypoints` | Validate and deduplicate the run entrypoints a Provider named |
538540
| `build.dependencyPlan` | Expand the command that prints a project's dependency tree, or the request a build provider answers |
539541
| `build.decodeModel` | Decode model output or a provider result into projects, targets, tasks, content roots, option values, and run entries |
540542
| `build.decodeTestResults` | Decode a test run's reports and output into per-case results and a summary |
@@ -3672,7 +3674,11 @@ and Core preserves them unchanged.
36723674

36733675
Provider applications can be explicitly saved with `runConfig.createUserConfiguration`
36743676
using `{root,scope:"local",name,type:"buildProvider",build:{pluginID,buildSystemID,
3675-
root,run,projectID,targetID,userArguments?,execution?}}`. Core upserts the existing local
3677+
root,run,projectID,targetID,userArguments?,execution?},entrypoint?}`. An entrypoint run
3678+
(`buildProviders` 1) has an empty `targetID` and names `entrypoint`, the opaque
3679+
`launchOptions.entrypoint` a provider found with `build.decodeRunEntrypoints`; Core saves it as
3680+
`extensions.build-run.entrypoint`, and the entrypoint is part of the selector identity, so each
3681+
entrypoint is its own configuration. Core upserts the existing local
36763682
reference by selector identity and retains its existing ID (new references use `user:` IDs), unrelated configurations
36773683
and project toolchain defaults. `extensions.build` in Run v2 holds the typed
36783684
reference; optional execution is application/service/task and must match the current
@@ -4989,7 +4995,9 @@ and the directories are absolute host paths used only as substitution text.
49894995
| `build.testPlan` | plan fields plus `project?`, `target?`, `suite?`, `case?`, `userArguments?`, `projects?` (the model's project directories) | command plan or `provider: {method: "lithe/build/test", params}`, plus `results`: `null`, `{ decoder, reports }` with workspace-relative globs (for the selected project, or for the root and every listed project when none is selected), or command-only `{ decoder: "hostIntegration" }`. Command filters that templates cannot place, such as a case without its suite when the tool only addresses `Suite#case`, are `plugin.build.invalidSelection`; Provider filters remain separate typed values and must be declared supported |
49904996
| `build.testDiscoveryPlan` | common plan fields plus `refresh?`, `tool?`; no project/output file | `plan.provider` for `lithe/build/discoverTests`, only with `test.discovery: true`, using the same effective options and effect metadata |
49914997
| `build.decodeTestDiscovery` | common plan fields plus canonical `result` | `discovery.tests` with validated `{id, suite, case, project?, target?}` selectors; bounded at 10,000 items/32 MiB, deterministic and exactly runnable; malformed replies reject in full |
4992-
| `build.prepareRunPlan` | common plan fields, `run`, required `project`/`target`, `userArguments?`, `launchOptions?`, `workspaceRoot?` (required for an explicit launch cwd); no output file | `plan.provider` for `lithe/build/prepareRun`, selected declaration/tags/paths/options validated; typed launch options must match the run's declared execution kind, and a service or task run requires them |
4998+
| `build.runEntrypointsPlan` | common plan fields plus `file` (workspace-relative, inside the instance), `refresh?`, `tool?`; no project/output file | `plan.provider` for `lithe/build/runEntrypoints` with `file`, only when a run declares `entrypoints: true`; decoder `litheRunEntrypoints` |
4999+
| `build.decodeRunEntrypoints` | common plan fields plus canonical `result` | `entrypoints.entrypoints`: `[{run, name, entrypoint, project:{name, path}}]` in the provider's order without exact duplicates; at most 64; each names an entrypoint run, a 1–256 byte name, a 1–4,096 byte entrypoint and a project inside the instance; malformed replies reject in full with `plugin.build.invalidOutput` |
5000+
| `build.prepareRunPlan` | common plan fields, `run`, required `project`, `target` (required for a target run, refused for an entrypoint run), `userArguments?`, `launchOptions?`, `workspaceRoot?` (required for an explicit launch cwd); no output file | `plan.provider` for `lithe/build/prepareRun`, selected declaration/tags/paths/options validated; typed launch options must match the run's declared execution kind, a service or task run requires them, and an entrypoint run requires `launchOptions.entrypoint` and sends no `target` |
49935001
| `build.decodeRunPreparation` | `workspaceRoot`, instance `root`, raw `result`, same optional `launchOptions` | `launch` with portable `{executable:{kind,value},arguments,workingDirectory,environment}`; bounded strings and workspace directory containment, `plugin.build.invalidOutput` on invalid result |
49945002
| `build.prepareDebugPlan` | same selected fields as `build.prepareRunPlan`; run declares same-plugin `debug.adapter` | `plan.provider` for `lithe/build/prepareDebug`; no cross-package reference, and launch options are refused: they apply only to running a target |
49955003
| `build.decodeDebugPreparation` | `workspaceRoot`, instance `root`, selected `run`, raw `result` | `preparation` with declaration-owned `adapter`, normalized `root`, `program`, `arguments`, `workingDirectory`, `environment`; program is an existing regular file contained in the instance after canonicalization |

‎rust/crates/build/src/commands.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,34 @@ pub fn build_decode_test_discovery_command<M: BuildManifest>(
214214
)
215215
}
216216

217+
/// `build.runEntrypointsPlan`: asks the provider which entrypoints a file holds.
218+
pub fn build_run_entrypoints_plan_command<M: BuildManifest>(
219+
payload: Value,
220+
) -> Result<Value, CoreError> {
221+
with_build_system::<M, _>(
222+
payload,
223+
"build.runEntrypointsPlan",
224+
"plan",
225+
|build_system, request: &super::run_entrypoints::RunEntrypointsPlanRequest| {
226+
super::run_entrypoints::plan(build_system, request)
227+
},
228+
)
229+
}
230+
231+
/// `build.decodeRunEntrypoints`: the entrypoints the host may save and start.
232+
pub fn build_decode_run_entrypoints_command<M: BuildManifest>(
233+
payload: Value,
234+
) -> Result<Value, CoreError> {
235+
with_build_system::<M, _>(
236+
payload,
237+
"build.decodeRunEntrypoints",
238+
"entrypoints",
239+
|build_system, request: &super::run_entrypoints::DecodeRunEntrypointsRequest| {
240+
super::run_entrypoints::decode(build_system, request)
241+
},
242+
)
243+
}
244+
217245
/// `build.prepareRunPlan`: validates a provider-owned target launch request.
218246
pub fn build_prepare_run_plan_command<M: BuildManifest>(
219247
payload: Value,

‎rust/crates/build/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod options;
2929
mod outputs;
3030
mod plans;
3131
mod project_options;
32+
mod run_entrypoints;
3233
mod run_preparation;
3334
mod run_start;
3435
mod select;

‎rust/crates/build/src/manifest.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ pub fn validate_declarations(
177177
}
178178

179179
impl BuildSystemDeclaration {
180+
/// Whether a run starts entrypoints the provider finds in files, which
181+
/// `buildProviders` 1 introduced.
182+
pub fn has_run_entrypoints(&self) -> bool {
183+
self.run.iter().any(|run| run.entrypoints)
184+
}
185+
180186
/// Folder names every workspace hides at any depth while the plugin is
181187
/// enabled (`layout.hiddenDirectoryNames`), sorted and unique once validated.
182188
pub fn hidden_directory_names(&self) -> &[String] {

‎rust/crates/build/src/manifest/run.rs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ pub(crate) struct RunDeclaration {
3030
/// Metadata stored under `extensions.<buildSystemID>` of the entry.
3131
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
3232
pub(crate) extension: BTreeMap<String, String>,
33+
/// `buildProviders` 1: the provider names entrypoints in source files
34+
/// (`lithe/build/runEntrypoints`), and the run starts one of them in a
35+
/// project. It applies to no target and is never listed per target.
36+
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
37+
pub(crate) entrypoints: bool,
3338
}
3439

3540
/// Native launch binds a local adapter; connection attach names a public registry type.
@@ -108,6 +113,26 @@ impl BuildSystemDeclaration {
108113
"Debug preparation requires a provider run.",
109114
));
110115
}
116+
if run.entrypoints {
117+
if run.source != TaskSource::Provider
118+
|| run.from_targets.is_some()
119+
|| run.from_tasks.is_some()
120+
|| !run.arguments.is_empty()
121+
|| !run.extension.is_empty()
122+
|| run.execution.unwrap_or(BuildExecution::Application)
123+
!= BuildExecution::Application
124+
|| run.debug.is_some()
125+
|| run.name.as_ref().is_some_and(|name| {
126+
name.trim().is_empty() || name.len() > MAX_TEXT || name.contains("${")
127+
})
128+
{
129+
return Err(invalid(
130+
&field,
131+
"Entrypoint runs are provider applications without targets, templates or debug.",
132+
));
133+
}
134+
continue;
135+
}
111136
if run.source == TaskSource::Provider {
112137
let valid_targets = run.from_targets.as_ref().is_some_and(|from| {
113138
!from.tags.is_empty()
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//! Entrypoints a provider finds in one source file (`buildProviders` 1).
2+
//!
3+
//! A run declared with `entrypoints: true` starts an entrypoint the provider
4+
//! names, such as a class with a `main` method, in one of its projects. Core
5+
//! never reads the source: the provider answers `lithe/build/runEntrypoints`
6+
//! for a file and Core checks that every answer is something the run can
7+
//! start, so the host can save and start it as an ordinary provider run.
8+
9+
use super::decode::{invalid_output, MAX_OUTPUT_BYTES};
10+
use super::manifest::{BuildSystemDeclaration, TaskSource};
11+
use super::plans::{self, OutputPlanRequest, PlanBase, SelectedEntity};
12+
use lithe_plugin_model::Validation;
13+
use serde::Deserialize;
14+
use serde_json::{json, Value};
15+
16+
/// Version of the `litheRunEntrypoints` document a provider returns.
17+
const ENTRYPOINTS_VERSION: u64 = 0;
18+
19+
/// Most entrypoints one file may name.
20+
const MAX_ENTRYPOINTS: usize = 64;
21+
22+
/// Longest entrypoint name, in bytes.
23+
const MAX_NAME: usize = 256;
24+
25+
/// Longest opaque entrypoint, in bytes, as `launchOptions.entrypoint` allows.
26+
const MAX_ENTRYPOINT: usize = 4096;
27+
28+
/// The source file whose entrypoints the host asks for.
29+
#[derive(Debug, Deserialize)]
30+
#[serde(rename_all = "camelCase")]
31+
pub(crate) struct RunEntrypointsPlanRequest {
32+
#[serde(flatten)]
33+
pub(crate) base: OutputPlanRequest,
34+
/// Workspace-relative file inside the instance root.
35+
pub(crate) file: String,
36+
}
37+
38+
/// The plan's configuration and the provider's answer.
39+
#[derive(Debug, Deserialize)]
40+
#[serde(rename_all = "camelCase")]
41+
pub(crate) struct DecodeRunEntrypointsRequest {
42+
#[serde(flatten)]
43+
pub(crate) base: PlanBase,
44+
pub(crate) result: Value,
45+
}
46+
47+
/// One entrypoint as the provider names it.
48+
#[derive(Deserialize)]
49+
struct Entrypoint {
50+
run: String,
51+
name: String,
52+
entrypoint: String,
53+
project: SelectedEntity,
54+
}
55+
56+
fn has_entrypoint_run(build_system: &BuildSystemDeclaration) -> bool {
57+
build_system
58+
.run
59+
.iter()
60+
.any(|run| run.entrypoints && run.source == TaskSource::Provider)
61+
}
62+
63+
/// `build.runEntrypointsPlan`: asks the provider which entrypoints `file` holds.
64+
pub(crate) fn plan(
65+
build_system: &BuildSystemDeclaration,
66+
request: &RunEntrypointsPlanRequest,
67+
) -> Validation<Value> {
68+
if !has_entrypoint_run(build_system) {
69+
return Err(plans::unavailable(build_system, "run entrypoints"));
70+
}
71+
if request.base.project.is_some() || request.base.output_file.is_some() {
72+
return Err(plans::invalid_selection(
73+
"Run entrypoints are found by file, not by project.",
74+
));
75+
}
76+
let context = plans::PlanContext::new(build_system, &request.base.base)?;
77+
let file = super::decode::request_path(&request.file, "file")?;
78+
if super::decode::relative_to(&context.root, &file).is_none_or(|relative| relative == ".") {
79+
return Err(plans::invalid_selection(
80+
"The file is outside the build-system root.",
81+
));
82+
}
83+
let mut params = plans::provider_hints(&request.base)?;
84+
params.insert("file".to_string(), json!(file));
85+
let provider = build_system
86+
.provider
87+
.as_ref()
88+
.ok_or_else(|| plans::unavailable(build_system, "a provider"))?;
89+
Ok(context.provider_plan(
90+
provider,
91+
"lithe/build/runEntrypoints",
92+
"litheRunEntrypoints",
93+
params,
94+
))
95+
}
96+
97+
/// `build.decodeRunEntrypoints`: the entrypoints the host may save and start, in
98+
/// the provider's order without duplicates. A malformed answer is refused whole.
99+
pub(crate) fn decode(
100+
build_system: &BuildSystemDeclaration,
101+
request: &DecodeRunEntrypointsRequest,
102+
) -> Validation<Value> {
103+
if !has_entrypoint_run(build_system) {
104+
return Err(plans::unavailable(build_system, "run entrypoints"));
105+
}
106+
let context = plans::PlanContext::new(build_system, &request.base)?;
107+
let result = &request.result;
108+
if serde_json::to_vec(result).map_or(usize::MAX, |bytes| bytes.len()) > MAX_OUTPUT_BYTES
109+
|| result["format"] != "litheRunEntrypoints"
110+
|| result["version"] != ENTRYPOINTS_VERSION
111+
{
112+
return Err(invalid_output("Invalid run entrypoints document."));
113+
}
114+
let items = result["entrypoints"]
115+
.as_array()
116+
.filter(|items| items.len() <= MAX_ENTRYPOINTS)
117+
.ok_or_else(|| invalid_output("Run entrypoints hold at most 64 entries."))?;
118+
let mut entrypoints: Vec<Value> = Vec::new();
119+
for item in items {
120+
let entry: Entrypoint = serde_json::from_value(item.clone())
121+
.map_err(|_| invalid_output("Invalid run entrypoint."))?;
122+
let runnable = build_system.run.iter().any(|run| {
123+
run.id == entry.run && run.entrypoints && run.source == TaskSource::Provider
124+
});
125+
let name = entry.name.trim();
126+
if !runnable
127+
|| name.is_empty()
128+
|| name.len() > MAX_NAME
129+
|| name.chars().any(char::is_control)
130+
|| entry.entrypoint.is_empty()
131+
|| entry.entrypoint.len() > MAX_ENTRYPOINT
132+
|| entry.entrypoint.contains('\0')
133+
{
134+
return Err(invalid_output(
135+
"A run entrypoint names a run, a name or an entrypoint this build system cannot start.",
136+
));
137+
}
138+
let project =
139+
plans::selection(&entry.project, &context.root, None, "project").map_err(|_| {
140+
invalid_output("A run entrypoint's project is outside the build-system root.")
141+
})?;
142+
let decoded = json!({
143+
"run": entry.run,
144+
"name": name,
145+
"entrypoint": entry.entrypoint,
146+
"project": {"name": project.name, "path": project.path},
147+
});
148+
if !entrypoints.contains(&decoded) {
149+
entrypoints.push(decoded);
150+
}
151+
}
152+
Ok(json!({ "entrypoints": entrypoints }))
153+
}

‎rust/crates/build/src/run_preparation.rs‎

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ pub(crate) struct PrepareRunRequest {
159159
#[serde(flatten)]
160160
pub(crate) base: OutputPlanRequest,
161161
pub(crate) run: String,
162-
pub(crate) target: SelectedEntity,
162+
/// The target of a target run; an entrypoint run has none.
163+
#[serde(default)]
164+
pub(crate) target: Option<SelectedEntity>,
163165
#[serde(default)]
164166
pub(crate) user_arguments: Vec<String>,
165167
#[serde(default)]
@@ -219,29 +221,46 @@ pub(crate) fn plan(
219221
.as_ref()
220222
.ok_or_else(|| plans::invalid_selection("A run target needs its project."))?;
221223
let project = plans::selection(project, &context.root, None, "project")?;
222-
let target = plans::selection(
223-
&request.target,
224-
&context.root,
225-
Some(&project.path),
226-
"target",
227-
)?;
228-
if !declaration
229-
.from_targets
230-
.as_ref()
231-
.is_some_and(|from| target.tags.iter().any(|tag| from.tags.contains(tag)))
232-
{
233-
return Err(plans::invalid_selection(
234-
"This run does not apply to the selected target.",
235-
));
236-
}
224+
let target = if declaration.entrypoints {
225+
// An entrypoint run starts the provider's entrypoint in the project; it
226+
// has no target, and without an entrypoint there is nothing to start.
227+
if request.target.is_some()
228+
|| !request
229+
.launch_options
230+
.as_ref()
231+
.is_some_and(|options| options.entrypoint.is_some())
232+
{
233+
return Err(plans::invalid_selection(
234+
"An entrypoint run needs an entrypoint and no target.",
235+
));
236+
}
237+
None
238+
} else {
239+
let target = request
240+
.target
241+
.as_ref()
242+
.ok_or_else(|| plans::invalid_selection("A target run needs its target."))?;
243+
let target = plans::selection(target, &context.root, Some(&project.path), "target")?;
244+
if !declaration
245+
.from_targets
246+
.as_ref()
247+
.is_some_and(|from| target.tags.iter().any(|tag| from.tags.contains(tag)))
248+
{
249+
return Err(plans::invalid_selection(
250+
"This run does not apply to the selected target.",
251+
));
252+
}
253+
Some(target)
254+
};
237255
plans::check_argument_limits(&request.user_arguments)?;
238256
let mut params = plans::provider_hints(&request.base)?;
239257
params.insert("run".to_string(), json!(declaration.id));
240258
params.insert("userArguments".to_string(), json!(request.user_arguments));
241259
if let Some(options) = &request.launch_options {
242260
params.insert("launchOptions".to_string(), json!(options));
243261
}
244-
for (name, selected) in [("project", project), ("target", target)] {
262+
for (name, selected) in [("project", Some(project)), ("target", target)] {
263+
let Some(selected) = selected else { continue };
245264
params.insert(
246265
name.to_string(),
247266
json!({"name":selected.name,"path":selected.path,

‎rust/crates/core/src/protocol/command.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ pub enum BuildCommand {
472472
TestDiscoveryPlan,
473473
/// Validates provider-owned test selectors (`build.decodeTestDiscovery`).
474474
DecodeTestDiscovery,
475+
/// Asks a provider for the entrypoints of a file (`build.runEntrypointsPlan`).
476+
RunEntrypointsPlan,
477+
/// Validates the entrypoints a provider found (`build.decodeRunEntrypoints`).
478+
DecodeRunEntrypoints,
475479
/// Expands the command that prints a project's dependencies (`build.dependencyPlan`).
476480
DependencyPlan,
477481
/// Normalizes a build model and derives its run entries (`build.decodeModel`).
@@ -1177,6 +1181,8 @@ impl CoreCommand {
11771181
"build.decodeRunPreparation" => Some(Self::Build(BuildCommand::DecodeRunPreparation)),
11781182
"build.testDiscoveryPlan" => Some(Self::Build(BuildCommand::TestDiscoveryPlan)),
11791183
"build.decodeTestDiscovery" => Some(Self::Build(BuildCommand::DecodeTestDiscovery)),
1184+
"build.runEntrypointsPlan" => Some(Self::Build(BuildCommand::RunEntrypointsPlan)),
1185+
"build.decodeRunEntrypoints" => Some(Self::Build(BuildCommand::DecodeRunEntrypoints)),
11801186
"build.dependencyPlan" => Some(Self::Build(BuildCommand::DependencyPlan)),
11811187
"build.decodeModel" => Some(Self::Build(BuildCommand::DecodeModel)),
11821188
"build.decodeTestResults" => Some(Self::Build(BuildCommand::DecodeTestResults)),

0 commit comments

Comments
 (0)