diff --git a/rust/contracts/rust-core-api.md b/rust/contracts/rust-core-api.md index 70f4fd8c..c6d40cfb 100644 --- a/rust/contracts/rust-core-api.md +++ b/rust/contracts/rust-core-api.md @@ -5289,7 +5289,7 @@ user to trust the workspace before an action the user started. | `build.localizeOptions` | `{ language?, plugins: [{ manifest, localizations? }] }`, without the standard manifest/build-system wrapper | `{ manifests, localizations }`: one manifest per plugin in request order, with the `buildOptions` part of the first accepted translation file applied to `buildSystems[].options[]` (`displayName`, `description`, choice `title`), else as sent; one report per plugin that declares a build option when `language` is set, `{ pluginID, language, candidates, maximumBytes, readRequired, applied, rejected }` as in `plugins.settings`. Hosts send it without `localizations`, read the named candidates from the installed package, send it again with them, and detect with the returned manifests. A file that does not fit is refused whole as `plugin.build.invalidLocalization` | | `build.projectOptions` | `{ document }`, host-read persisted v1 Build options, without a manifest | `document`: validated storage with version 1, systems and optional `ignoredPaths`; options, instance defaults, root values, explicit empty selections and saved custom choices are retained. Missing system fields normalize to empty maps; malformed, unsupported or oversized documents reject with `plugin.build.invalidOptionsDocument`. This pure operation never reads or writes a file and does not replace declared option resolution. | | `build.resolveStoredOptions` | `{ document, local: { version: 1, document, emptySelections?: { : { : [optionID] } } } }`, without a manifest | `storage: { document, local, emptySelections, ignoredPaths: { patterns, source } }`: checked team/local merge; local values override matching keys, unrelated systems/roots/choices stay intact. `ignoredPaths` is the list `build.detect` uses: a local list replaces the team's whole (`source: "local"`), then the team's (`"team"`), then Core's default `[".*"]` (`"default"`). No filesystem I/O. Local documents are machine/workspace preferences, not team files or plugin-native configuration. Their storage syntax is [build-local-options-v1.schema.json](build-local-options-v1.schema.json), with additional cross-field membership and byte limits checked by Core. | -| `build.editStoredOption` | `{ document, local, root, optionID, value?, selectedChoices?, modelValues?, modelDefaults?, modelExcludePrefixes?, modelSelection? }` in the standard manifest/build-system envelope | `edit: { local, options }`: changes one declared local preference. Pass exactly one of raw `value` or positive `selectedChoices` for a multi-select. Explicit `value: null` removes the local override. When the checked set equals the defaults, Core clears the local override only if the edited root would inherit those defaults and sibling-only selections and empty markers would survive. See [the option edit fixture](../fixtures/build/option-edit-v1.json). Invalid or partially accepted edits reject before persistence. Workspace multi-selections preserve sibling-only values; explicit per-root empty markers apply only to workspace multi-select declarations. Platforms compare both native source revisions, atomically write only the local document and read back. | +| `build.editStoredOption` | `{ document, local, root, optionID, value?, selectedChoices?, modelValues?, modelDefaults?, modelExcludePrefixes?, modelSelection? }` in the standard manifest/build-system envelope | `edit: { local, options }`: changes one declared local preference. Pass exactly one of raw `value` or positive `selectedChoices` for a multi-select. Explicit `value: null` removes the local override. An `optionID` the declaration no longer has (a plugin update dropped it; `build.detect` names it in `droppedOptions`) accepts only `value: null`, which removes its local workspace value, instance default and this root's value, so a host can clear what no longer applies; anything else rejects with `plugin.build.invalidOption`. When the checked set equals the defaults, Core clears the local override only if the edited root would inherit those defaults and sibling-only selections and empty markers would survive. See [the option edit fixture](../fixtures/build/option-edit-v1.json). Invalid or partially accepted edits reject before persistence. Workspace multi-selections preserve sibling-only values; explicit per-root empty markers apply only to workspace multi-select declarations. Platforms compare both native source revisions, atomically write only the local document and read back. | | `build.editIgnoredPaths` | `{ document, local, patterns: [pattern] \| null }`, without a manifest; `patterns` is required | `edit: { local, ignoredPaths: { patterns, source } }`: the local document with its `ignoredPaths` replaced, or removed for `null` so the team list or the default applies again; `[]` is kept as an explicit choice to ignore nothing. A pattern uses `.gitignore` syntax relative to the workspace root and names directories only: as in one ignore file the last matching pattern decides, `!pattern` keeps a directory even if Git ignores it (the shared scan policy's caches and build outputs stay pruned), a pattern with a `/` before its end is anchored at the root and any other matches a directory name at any depth. At most 256 patterns of at most 1024 bytes; an empty or whitespace-padded entry, a comment, a lone `!`, a control character or an unparsable glob rejects with `plugin.build.invalidIgnoredPath` at `ignoredPaths[]`, the first entry not accepted (`ignoredPaths` when they cannot be compiled together). The team document is only read; an unusable list stored in either document makes it `plugin.build.invalidOptionsDocument`. Platforms publish the local document as for `build.editStoredOption`. | | `build.invocationPlan` | `{ root, platform: "macos" \| "linux" \| "windows" }` | `invocation`: `{ buildSystemID, root, executables, environmentInputs, runtimeDependencies, version, minimumVersion }` with project-file paths joined to `root`; every runtime dependency with a known probe, host-managed ones included, carries `version: { arguments }` | | `build.checkToolVersion` | `{ runtime?, output }` | `version`: `{ satisfied, version? }`; `runtime` checks that runtime dependency instead of the tool | diff --git a/rust/crates/build/src/stored_options.rs b/rust/crates/build/src/stored_options.rs index 6d3a3a83..09bbc33e 100644 --- a/rust/crates/build/src/stored_options.rs +++ b/rust/crates/build/src/stored_options.rs @@ -212,18 +212,28 @@ pub(super) fn edit(system: &BuildSystemDeclaration, request: &EditRequest) -> Va } let team = project_options::validate(request.document.clone())?; let mut local = validate_local(request.local.clone())?; - let option = system - .options - .iter() - .find(|option| option.id == request.option_id) - .ok_or_else(|| { - PluginRejection::new("plugin.build.invalidOption", "Unknown declared option") - })?; let facts = options::ModelOptionFacts { values: request.model_values.as_ref(), defaults: request.model_defaults.as_ref(), exclude_prefixes: request.model_exclude_prefixes.as_ref(), }; + let Some(option) = system + .options + .iter() + .find(|option| option.id == request.option_id) + else { + // A plugin update may drop an option the user had set (detection + // reports it in droppedOptions). Its value can only be removed, so + // the host can clear what no longer applies. + if request.value != Some(Value::Null) || request.selected_choices.is_some() { + return Err(PluginRejection::new( + "plugin.build.invalidOption", + "Unknown declared option", + )); + } + remove_undeclared(&mut local, &system.id, &request.root, &request.option_id); + return finish_edit(system, &team, local, request, facts); + }; let before = merge(&team, &local); let workspace_multi = option.scope == OptionScope::Workspace && option.kind == OptionKind::MultiSelect; @@ -356,6 +366,54 @@ pub(super) fn edit(system: &BuildSystemDeclaration, request: &EditRequest) -> Va roots.retain(|_, ids| !ids.is_empty()); } local.empty_selections.retain(|_, roots| !roots.is_empty()); + finish_edit(system, &team, local, request, facts) +} + +/// Removes the machine-local value of an option the declaration no longer +/// has, wherever this root reads it: the workspace value, the instance +/// default, this root's value, and an explicit empty selection. +fn remove_undeclared(local: &mut Local, system_id: &str, root: &str, option_id: &str) { + if let Some(target) = local.document["systems"].get_mut(system_id) { + for key in ["options", "instanceDefaults"] { + if let Some(values) = target[key].as_object_mut() { + values.remove(option_id); + } + } + if let Some(values) = target["instances"] + .get_mut(root) + .and_then(Value::as_object_mut) + { + values.remove(option_id); + } + } + if let Some(roots) = local.empty_selections.get_mut(system_id) { + for ids in roots.values_mut() { + ids.remove(option_id); + } + roots.retain(|_, ids| !ids.is_empty()); + } + local.empty_selections.retain(|_, roots| !roots.is_empty()); +} + +/// Prunes what the edit left empty, checks the local document again, and +/// describes the options as the edited root now reads them. +fn finish_edit( + system: &BuildSystemDeclaration, + team: &Value, + mut local: Local, + request: &EditRequest, + facts: options::ModelOptionFacts<'_>, +) -> Validation { + if local.document["systems"].get(&system.id).is_none() { + let local = validate_local(serde_json::to_value(local).map_err(|_| invalid())?)?; + let document = merge(team, &local); + let stored = effective(system, &document, &local, &request.root); + let resolved = options::resolve_with_facts(system, &stored, facts); + return Ok(json!({ + "local": local, + "options": options::describe_with_facts(system, &resolved, facts, request.model_selection.as_ref()) + })); + } // A no-op reset must not materialize an empty machine override. Limit // pruning to the edited system/root; dormant user data stays untouched. let target = &mut local.document["systems"][&system.id]; @@ -384,7 +442,7 @@ pub(super) fn edit(system: &BuildSystemDeclaration, request: &EditRequest) -> Va } // Revalidate aggregate bounds after insertion, without pruning unrelated data. let local = validate_local(serde_json::to_value(local).map_err(|_| invalid())?)?; - let document = merge(&team, &local); + let document = merge(team, &local); let stored = effective(system, &document, &local, &request.root); let resolved = options::resolve_with_facts(system, &stored, facts); Ok(json!({ diff --git a/rust/crates/core/src/tests/build_systems.rs b/rust/crates/core/src/tests/build_systems.rs index 220210fe..9c5fcd78 100644 --- a/rust/crates/core/src/tests/build_systems.rs +++ b/rust/crates/core/src/tests/build_systems.rs @@ -3680,6 +3680,37 @@ fn build_option_text_is_translated_in_the_manifest() { assert_eq!(plain, json!({"manifests": [manifest], "localizations": []})); } +/// A plugin update may drop an option the user had set; detection names it +/// in droppedOptions. Its stored value can be removed, so the host can clear +/// what no longer applies, but nothing else can be written for it. +#[test] +fn a_dropped_option_can_only_be_removed() { + use crate::build::build_edit_stored_option_command; + let fixture = option_edit_fixture(); + let local = json!({"version": 1, "document": {"version": 1, "systems": {"fixture": { + "options": {"javaHome": "/jdks/21", "profiles": ["dev"]}, + "instances": {"a": {"javaHome": "/jdks/17"}} + }}}}); + let edit = |value: Value| { + build_edit_stored_option_command::(json!({ + "manifest": fixture["manifest"], + "buildSystemID": "fixture", + "request": {"document": {"version": 1, "systems": {}}, "local": local, + "root": "a", "optionID": "javaHome", "value": value}, + })) + .unwrap() + }; + let removed = edit(Value::Null); + assert_eq!(removed["status"], "ok", "{removed}"); + let system = &removed["edit"]["local"]["document"]["systems"]["fixture"]; + assert_eq!(system["options"], json!({"profiles": ["dev"]}), "{removed}"); + assert!(system["instances"].get("a").is_none(), "{removed}"); + + let written = edit(json!("/jdks/21")); + assert_eq!(written["status"], "rejected", "{written}"); + assert_eq!(written["error"]["code"], "plugin.build.invalidOption"); +} + fn option_edit_fixture() -> Value { serde_json::from_str(include_str!( "../../../../fixtures/build/option-edit-v1.json"