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
6 changes: 4 additions & 2 deletions rust/crates/build/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,13 @@ pub fn build_dependency_plan_command<M: BuildManifest>(payload: Value) -> Result

/// `build.decodeModel`: the normalized model and its run entries.
pub fn build_decode_model_command<M: BuildManifest>(payload: Value) -> Result<Value, CoreError> {
with_build_system::<M, _>(
with_manifest::<M, _>(
payload,
"build.decodeModel",
"model",
|build_system, request: &DecodeModelRequest| outputs::decode_model(build_system, request),
|build_system, request: &DecodeModelRequest, manifest| {
outputs::decode_model(manifest.id(), build_system, request)
},
)
}

Expand Down
9 changes: 8 additions & 1 deletion rust/crates/build/src/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@ fn instance(
if matches!(declaration.model, Some(ModelSource::Static { .. })) {
let source = source_marker(declaration, workspace, root);
match read_model(declaration, workspace, root, None, None, None).and_then(|model| {
let value = model_value(declaration, &model, root, stored, source.as_deref())?;
let value = model_value(
owner.plugin_id,
declaration,
&model,
root,
stored,
source.as_deref(),
)?;
Ok((model, value))
}) {
Ok((model, value)) => {
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/build/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ impl BuildSystemDeclaration {
self.validate_invocation(field, &option_ids, context)?;
let model_templates = self.validate_tasks(field, &option_ids)?;
self.validate_model(field, &option_ids)?;
self.validate_run(field, &option_ids, &model_templates)?;
self.validate_run_types(field, &option_ids)?;
self.validate_run(field, &option_ids, &model_templates)?;
self.validate_test(field, &option_ids)?;
self.validate_diagnostics(field)?;
self.validate_dependencies(field, &option_ids)?;
Expand Down
198 changes: 198 additions & 0 deletions rust/crates/build/src/manifest/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ pub(crate) struct RunDeclaration {
/// project. It applies to no target and is never listed per target.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub(crate) entrypoints: bool,
/// ID of a `runConfigurationTypes` entry in this build system. Each
/// matching target materializes a generated configuration of that declared
/// kind instead of a raw command or provider run, sharing the kind's icon,
/// group and editor form.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) configuration_type: Option<String>,
/// Initial field values for `configurationType`, keyed by declared field
/// ID. String values and `arguments` list items may reference target and
/// project template variables.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub(crate) values: BTreeMap<String, Value>,
}

/// Native launch binds a local adapter; connection attach names a public registry type.
Expand Down Expand Up @@ -107,6 +118,71 @@ impl BuildSystemDeclaration {
"Run IDs are unique identifiers that start with a letter.",
));
}
if !run.values.is_empty() && run.configuration_type.is_none() {
return Err(invalid(
format!("{field}.values"),
"Run field values require configurationType.",
));
}
if let Some(type_id) = run.configuration_type.as_deref() {
if run.source == TaskSource::Provider
|| run.debug.is_some()
|| run.entrypoints
|| run.from_tasks.is_some()
|| !run.arguments.is_empty()
|| !run.extension.is_empty()
|| run.execution.is_some()
{
return Err(invalid(
&field,
"A run entry with configurationType uses target tags, field values, and the declared type's command line.",
));
}
let Some(targets) = &mut run.from_targets else {
return Err(invalid(
format!("{field}.fromTargets"),
"A run entry with configurationType requires fromTargets.",
));
};
if targets.tags.is_empty()
|| targets.tags.len() > MAX_LIST
|| targets.tags.iter().any(|tag| !is_tag(tag))
{
return Err(invalid(
format!("{field}.fromTargets.tags"),
"fromTargets needs one or more tag identifiers.",
));
}
targets.tags = sorted_unique(std::mem::take(&mut targets.tags));
let Some(run_type) = self
.run_configuration_types
.iter()
.find(|declared| declared.id == type_id)
else {
return Err(invalid(
format!("{field}.configurationType"),
format!(
"configurationType {type_id} is not declared in runConfigurationTypes."
),
));
};
let allowed = Allowed {
root: true,
project: Availability::Always,
target: Availability::Always,
..Allowed::none()
};
if let Some(name) = &run.name {
template::validate_text(name, &allowed, &format!("{field}.name"))?;
}
validate_run_type_values(
run_type,
&run.values,
&allowed,
&format!("{field}.values"),
)?;
continue;
}
if run.debug.is_some() && run.source != TaskSource::Provider {
return Err(invalid(
&field,
Expand Down Expand Up @@ -259,3 +335,125 @@ impl BuildSystemDeclaration {
Ok(())
}
}

/// Checks `values` of a `buildSystems[].run[]` entry that targets `run_type`.
fn validate_run_type_values(
run_type: &RunTypeDeclaration,
values: &BTreeMap<String, Value>,
allowed: &Allowed<'_>,
field: &str,
) -> Validation<()> {
for (id, value) in values {
let value_field = format!("{field}.{id}");
let Some(declared) = run_type.fields.iter().find(|entry| entry.id == *id) else {
return Err(invalid(
value_field,
format!(
"{id} is not a field of runConfigurationType {}.",
run_type.id
),
));
};
match (declared.kind, value) {
(
RunFieldKind::Text
| RunFieldKind::Choice
| RunFieldKind::File
| RunFieldKind::Directory,
Value::String(text),
) => {
if text.len() > MAX_TEXT || text.contains('\0') {
return Err(invalid(value_field, "Field value is invalid."));
}
template::validate_text(text, allowed, &value_field)?;
if declared.kind == RunFieldKind::Choice
&& !text.contains("${")
&& !declared.choices.iter().any(|choice| choice.value == *text)
{
return Err(invalid(
value_field,
"Choice field value must match one of the declared choices.",
));
}
}
(RunFieldKind::Checkbox, Value::Bool(_)) => {}
(RunFieldKind::Arguments, Value::Array(items)) => {
if items.len() > 64 {
return Err(invalid(value_field, "Too many arguments in field value."));
}
for (item_index, item) in items.iter().enumerate() {
let Some(text) = item.as_str() else {
return Err(invalid(
format!("{value_field}[{item_index}]"),
"Arguments field values must be strings.",
));
};
if text.len() > MAX_TEXT || text.contains('\0') {
return Err(invalid(
format!("{value_field}[{item_index}]"),
"Field value is invalid.",
));
}
template::validate_text(
text,
allowed,
&format!("{value_field}[{item_index}]"),
)?;
}
}
_ => {
return Err(invalid(
value_field,
"Field value does not match the declared field kind.",
));
}
}
}

// Ensure every visible required field without a default is supplied.
let mut effective: BTreeMap<&str, Value> = BTreeMap::new();
for declared in &run_type.fields {
let visible = declared.visible_when.as_ref().is_none_or(|condition| {
effective
.get(condition.field.as_str())
.is_some_and(|actual| *actual == condition.equals)
});
if !visible {
continue;
}
let chosen = values
.get(&declared.id)
.cloned()
.or_else(|| declared.default.clone())
.or_else(|| {
(declared.kind == RunFieldKind::Choice)
.then(|| {
declared
.choices
.first()
.map(|choice| Value::String(choice.value.clone()))
})
.flatten()
})
.or_else(|| (declared.kind == RunFieldKind::Checkbox).then_some(Value::Bool(false)));
let empty = match &chosen {
None => true,
Some(Value::String(text)) => text.trim().is_empty(),
Some(Value::Array(items)) => items.is_empty(),
Some(_) => false,
};
if declared.required && empty {
return Err(invalid(
format!("{field}.{}", declared.id),
format!(
"Required field {} of runConfigurationType {} must have a value or default.",
declared.id, run_type.id
),
));
}
if let Some(chosen) = chosen {
effective.insert(&declared.id, chosen);
}
}
Ok(())
}
5 changes: 4 additions & 1 deletion rust/crates/build/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ pub(crate) fn source_marker(

/// The model with the run entries derived from it.
pub(crate) fn model_value(
plugin_id: &str,
build_system: &BuildSystemDeclaration,
model: &BuildModel,
root: &str,
options: &BTreeMap<String, Value>,
source: Option<&str>,
) -> Validation<Value> {
let run = plans::run_entries(build_system, model, root, options, source)?;
let run = plans::run_entries(plugin_id, build_system, model, root, options, source)?;
let mut value =
serde_json::to_value(model).map_err(|error| decode::invalid_output(error.to_string()))?;
value["run"] = json!(run);
Expand All @@ -200,6 +201,7 @@ pub(crate) fn model_value(

/// `build.decodeModel`.
pub(crate) fn decode_model(
plugin_id: &str,
build_system: &BuildSystemDeclaration,
request: &DecodeModelRequest,
) -> Validation<Value> {
Expand All @@ -215,6 +217,7 @@ pub(crate) fn decode_model(
)?;
let source = source_marker(build_system, &workspace, &root);
model_value(
plugin_id,
build_system,
&model,
&root,
Expand Down
Loading
Loading