Skip to content

Commit 0d79e17

Browse files
authored
feat(build): allow buildSystems[].run[] to materialize declared runConfigurationTypes (#196)
* feat(build): add runConfigurationTypes 1 for model-generated declared run configurations * docs(plugins): record PR #196 in protocol changelog * refactor(build): keep runConfigurationTypes at version 0 for compatible extension
1 parent 3a32290 commit 0d79e17

16 files changed

Lines changed: 957 additions & 81 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,13 @@ pub fn build_dependency_plan_command<M: BuildManifest>(payload: Value) -> Result
410410

411411
/// `build.decodeModel`: the normalized model and its run entries.
412412
pub fn build_decode_model_command<M: BuildManifest>(payload: Value) -> Result<Value, CoreError> {
413-
with_build_system::<M, _>(
413+
with_manifest::<M, _>(
414414
payload,
415415
"build.decodeModel",
416416
"model",
417-
|build_system, request: &DecodeModelRequest| outputs::decode_model(build_system, request),
417+
|build_system, request: &DecodeModelRequest, manifest| {
418+
outputs::decode_model(manifest.id(), build_system, request)
419+
},
418420
)
419421
}
420422

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,14 @@ fn instance(
124124
if matches!(declaration.model, Some(ModelSource::Static { .. })) {
125125
let source = source_marker(declaration, workspace, root);
126126
match read_model(declaration, workspace, root, None, None, None).and_then(|model| {
127-
let value = model_value(declaration, &model, root, stored, source.as_deref())?;
127+
let value = model_value(
128+
owner.plugin_id,
129+
declaration,
130+
&model,
131+
root,
132+
stored,
133+
source.as_deref(),
134+
)?;
128135
Ok((model, value))
129136
}) {
130137
Ok((model, value)) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ impl BuildSystemDeclaration {
238238
self.validate_invocation(field, &option_ids, context)?;
239239
let model_templates = self.validate_tasks(field, &option_ids)?;
240240
self.validate_model(field, &option_ids)?;
241-
self.validate_run(field, &option_ids, &model_templates)?;
242241
self.validate_run_types(field, &option_ids)?;
242+
self.validate_run(field, &option_ids, &model_templates)?;
243243
self.validate_test(field, &option_ids)?;
244244
self.validate_diagnostics(field)?;
245245
self.validate_dependencies(field, &option_ids)?;

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

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ pub(crate) struct RunDeclaration {
3535
/// project. It applies to no target and is never listed per target.
3636
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
3737
pub(crate) entrypoints: bool,
38+
/// ID of a `runConfigurationTypes` entry in this build system. Each
39+
/// matching target materializes a generated configuration of that declared
40+
/// kind instead of a raw command or provider run, sharing the kind's icon,
41+
/// group and editor form.
42+
#[serde(default, skip_serializing_if = "Option::is_none")]
43+
pub(crate) configuration_type: Option<String>,
44+
/// Initial field values for `configurationType`, keyed by declared field
45+
/// ID. String values and `arguments` list items may reference target and
46+
/// project template variables.
47+
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
48+
pub(crate) values: BTreeMap<String, Value>,
3849
}
3950

4051
/// Native launch binds a local adapter; connection attach names a public registry type.
@@ -107,6 +118,71 @@ impl BuildSystemDeclaration {
107118
"Run IDs are unique identifiers that start with a letter.",
108119
));
109120
}
121+
if !run.values.is_empty() && run.configuration_type.is_none() {
122+
return Err(invalid(
123+
format!("{field}.values"),
124+
"Run field values require configurationType.",
125+
));
126+
}
127+
if let Some(type_id) = run.configuration_type.as_deref() {
128+
if run.source == TaskSource::Provider
129+
|| run.debug.is_some()
130+
|| run.entrypoints
131+
|| run.from_tasks.is_some()
132+
|| !run.arguments.is_empty()
133+
|| !run.extension.is_empty()
134+
|| run.execution.is_some()
135+
{
136+
return Err(invalid(
137+
&field,
138+
"A run entry with configurationType uses target tags, field values, and the declared type's command line.",
139+
));
140+
}
141+
let Some(targets) = &mut run.from_targets else {
142+
return Err(invalid(
143+
format!("{field}.fromTargets"),
144+
"A run entry with configurationType requires fromTargets.",
145+
));
146+
};
147+
if targets.tags.is_empty()
148+
|| targets.tags.len() > MAX_LIST
149+
|| targets.tags.iter().any(|tag| !is_tag(tag))
150+
{
151+
return Err(invalid(
152+
format!("{field}.fromTargets.tags"),
153+
"fromTargets needs one or more tag identifiers.",
154+
));
155+
}
156+
targets.tags = sorted_unique(std::mem::take(&mut targets.tags));
157+
let Some(run_type) = self
158+
.run_configuration_types
159+
.iter()
160+
.find(|declared| declared.id == type_id)
161+
else {
162+
return Err(invalid(
163+
format!("{field}.configurationType"),
164+
format!(
165+
"configurationType {type_id} is not declared in runConfigurationTypes."
166+
),
167+
));
168+
};
169+
let allowed = Allowed {
170+
root: true,
171+
project: Availability::Always,
172+
target: Availability::Always,
173+
..Allowed::none()
174+
};
175+
if let Some(name) = &run.name {
176+
template::validate_text(name, &allowed, &format!("{field}.name"))?;
177+
}
178+
validate_run_type_values(
179+
run_type,
180+
&run.values,
181+
&allowed,
182+
&format!("{field}.values"),
183+
)?;
184+
continue;
185+
}
110186
if run.debug.is_some() && run.source != TaskSource::Provider {
111187
return Err(invalid(
112188
&field,
@@ -259,3 +335,125 @@ impl BuildSystemDeclaration {
259335
Ok(())
260336
}
261337
}
338+
339+
/// Checks `values` of a `buildSystems[].run[]` entry that targets `run_type`.
340+
fn validate_run_type_values(
341+
run_type: &RunTypeDeclaration,
342+
values: &BTreeMap<String, Value>,
343+
allowed: &Allowed<'_>,
344+
field: &str,
345+
) -> Validation<()> {
346+
for (id, value) in values {
347+
let value_field = format!("{field}.{id}");
348+
let Some(declared) = run_type.fields.iter().find(|entry| entry.id == *id) else {
349+
return Err(invalid(
350+
value_field,
351+
format!(
352+
"{id} is not a field of runConfigurationType {}.",
353+
run_type.id
354+
),
355+
));
356+
};
357+
match (declared.kind, value) {
358+
(
359+
RunFieldKind::Text
360+
| RunFieldKind::Choice
361+
| RunFieldKind::File
362+
| RunFieldKind::Directory,
363+
Value::String(text),
364+
) => {
365+
if text.len() > MAX_TEXT || text.contains('\0') {
366+
return Err(invalid(value_field, "Field value is invalid."));
367+
}
368+
template::validate_text(text, allowed, &value_field)?;
369+
if declared.kind == RunFieldKind::Choice
370+
&& !text.contains("${")
371+
&& !declared.choices.iter().any(|choice| choice.value == *text)
372+
{
373+
return Err(invalid(
374+
value_field,
375+
"Choice field value must match one of the declared choices.",
376+
));
377+
}
378+
}
379+
(RunFieldKind::Checkbox, Value::Bool(_)) => {}
380+
(RunFieldKind::Arguments, Value::Array(items)) => {
381+
if items.len() > 64 {
382+
return Err(invalid(value_field, "Too many arguments in field value."));
383+
}
384+
for (item_index, item) in items.iter().enumerate() {
385+
let Some(text) = item.as_str() else {
386+
return Err(invalid(
387+
format!("{value_field}[{item_index}]"),
388+
"Arguments field values must be strings.",
389+
));
390+
};
391+
if text.len() > MAX_TEXT || text.contains('\0') {
392+
return Err(invalid(
393+
format!("{value_field}[{item_index}]"),
394+
"Field value is invalid.",
395+
));
396+
}
397+
template::validate_text(
398+
text,
399+
allowed,
400+
&format!("{value_field}[{item_index}]"),
401+
)?;
402+
}
403+
}
404+
_ => {
405+
return Err(invalid(
406+
value_field,
407+
"Field value does not match the declared field kind.",
408+
));
409+
}
410+
}
411+
}
412+
413+
// Ensure every visible required field without a default is supplied.
414+
let mut effective: BTreeMap<&str, Value> = BTreeMap::new();
415+
for declared in &run_type.fields {
416+
let visible = declared.visible_when.as_ref().is_none_or(|condition| {
417+
effective
418+
.get(condition.field.as_str())
419+
.is_some_and(|actual| *actual == condition.equals)
420+
});
421+
if !visible {
422+
continue;
423+
}
424+
let chosen = values
425+
.get(&declared.id)
426+
.cloned()
427+
.or_else(|| declared.default.clone())
428+
.or_else(|| {
429+
(declared.kind == RunFieldKind::Choice)
430+
.then(|| {
431+
declared
432+
.choices
433+
.first()
434+
.map(|choice| Value::String(choice.value.clone()))
435+
})
436+
.flatten()
437+
})
438+
.or_else(|| (declared.kind == RunFieldKind::Checkbox).then_some(Value::Bool(false)));
439+
let empty = match &chosen {
440+
None => true,
441+
Some(Value::String(text)) => text.trim().is_empty(),
442+
Some(Value::Array(items)) => items.is_empty(),
443+
Some(_) => false,
444+
};
445+
if declared.required && empty {
446+
return Err(invalid(
447+
format!("{field}.{}", declared.id),
448+
format!(
449+
"Required field {} of runConfigurationType {} must have a value or default.",
450+
declared.id, run_type.id
451+
),
452+
));
453+
}
454+
if let Some(chosen) = chosen {
455+
effective.insert(&declared.id, chosen);
456+
}
457+
}
458+
Ok(())
459+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,14 @@ pub(crate) fn source_marker(
178178

179179
/// The model with the run entries derived from it.
180180
pub(crate) fn model_value(
181+
plugin_id: &str,
181182
build_system: &BuildSystemDeclaration,
182183
model: &BuildModel,
183184
root: &str,
184185
options: &BTreeMap<String, Value>,
185186
source: Option<&str>,
186187
) -> Validation<Value> {
187-
let run = plans::run_entries(build_system, model, root, options, source)?;
188+
let run = plans::run_entries(plugin_id, build_system, model, root, options, source)?;
188189
let mut value =
189190
serde_json::to_value(model).map_err(|error| decode::invalid_output(error.to_string()))?;
190191
value["run"] = json!(run);
@@ -200,6 +201,7 @@ pub(crate) fn model_value(
200201

201202
/// `build.decodeModel`.
202203
pub(crate) fn decode_model(
204+
plugin_id: &str,
203205
build_system: &BuildSystemDeclaration,
204206
request: &DecodeModelRequest,
205207
) -> Validation<Value> {
@@ -215,6 +217,7 @@ pub(crate) fn decode_model(
215217
)?;
216218
let source = source_marker(build_system, &workspace, &root);
217219
model_value(
220+
plugin_id,
218221
build_system,
219222
&model,
220223
&root,

0 commit comments

Comments
 (0)