diff --git a/rust/contracts/rust-core-api.md b/rust/contracts/rust-core-api.md index f261cc29..80cc2a79 100644 --- a/rust/contracts/rust-core-api.md +++ b/rust/contracts/rust-core-api.md @@ -711,7 +711,7 @@ or removing a command fails until the table changes in the same commit. | `runConfig.updateToolPreferences` | Replace only generic preferences in the local layer, preserving legacy defaults and unrelated metadata | | `runConfig.saveEditorChanges` | Prepare the file changes of the local and optional project layers for one editor save | | `runConfig.createUserConfiguration` | Validate a typed user configuration and return the file changes that save it | -| `runConfig.configurationTypes` | List the built-in run configuration types this platform can create | +| `runConfig.configurationTypes` | List the built-in run configuration types a user can create, with the availability of a kind this platform cannot run | | `runConfig.editorForm` | Describe the editor fields and current values of a built-in run configuration | | `runConfig.saveConfiguration` | Validate an editor form's values and return field errors or the file changes that save it | | `runConfig.deleteConfiguration` | Return the file changes that delete a saved user configuration | @@ -3642,14 +3642,16 @@ build run references). A saved configuration that cannot run carries `availability: {status: "unavailable", code, message}` instead of being dropped: `pluginMissing`, `pluginRepositoryMismatch` (the installed plugin comes from another repository), `pluginVersionUnsupported`, -`invalidConfiguration` (it fails validation, also reported as a diagnostic) or -`missingWorkingDirectory` (its working directory is gone; a generated entry -whose directory is gone is left out instead, with the same diagnostic). +`invalidConfiguration` (it fails validation, also reported as a diagnostic), +`unsupportedPlatform` (a built-in kind this platform cannot run, such as a +batch script outside Windows) or `missingWorkingDirectory` (its working +directory is gone; a generated entry whose directory is gone is left out +instead, with the same diagnostic). Omitting `plugins` skips the plugin checks. Hosts show unavailable entries with their message and do not start them; `runConfig.createLaunchPlan` and -`runConfig.providerLaunchInputs` refuse them. Entries of a built-in type the -platform cannot run, such as a batch script outside Windows, are left out with -the diagnostic `unsupportedPlatform`; their saved text is kept. +`runConfig.providerLaunchInputs` refuse them. An `unsupportedPlatform` entry +stays editable, so a project shared with Windows keeps its batch scripts +listed and editable elsewhere. Each configuration carries a `category` of `project` or `infrastructure`. `infrastructure` marks the external services the project runs against, such as @@ -3835,16 +3837,20 @@ becomes `bash -e`), else `bash`, both looked up on `PATH`. PowerShell uses `pwsh` outside Windows and `powershell` on Windows, with `-NoLogo -NoProfile`; batch uses `cmd /d`. A chosen interpreter drops the automatic arguments. -`runConfig.configurationTypes` accepts `{}` and returns `{types: [{provider}]}`, -the kinds a user can create on this platform in display order; batch is listed -only on Windows. +`runConfig.configurationTypes` accepts `{}` and returns `{types: [{provider, +availability?}]}`, every kind a user can create, in display order. Every +platform creates and edits every kind; a kind this platform cannot run carries +the `availability` its configurations resolve with (`unsupportedPlatform` for +batch outside Windows), so hosts can say so before the user saves one. `runConfig.editorForm` accepts `{root, provider?, configurationId?, values?}` and describes the editor of a new configuration of `provider` or of the saved `configurationId` (its provider must match `provider` when both are given). `values` holds the open editor's unsaved values; they replace the saved ones so automatic values follow the edit. It returns `{formVersion, provider, -configurationId, values, fields}`. `formVersion` is `1`; a host refuses a newer +configurationId, values, fields, availability?}`. `availability` is present +when this platform cannot run the kind, as in `runConfig.configurationTypes`; +the form still edits and saves it. `formVersion` is `1`; a host refuses a newer one. `values` maps field IDs to the current values. `fields` is the ordered list of `{id, kind, required?, placement?, automatic?, options?, visibleWhen?, note?}`: @@ -3866,7 +3872,7 @@ later Core adds fields without a host change. Script kinds show `name`, shows `name`, `storeAsProjectFile`, `program`, `programArguments`, `workingDirectory` and `environment`. An empty name saves the automatic name, the script or program file name. A saved entry that is not a built-in process -run, or a batch run outside Windows, is `not_supported`. +run is `not_supported`. `runConfig.saveConfiguration` accepts `{root, provider, configurationId?, values, plugins?}`. It replies `{status: "invalid", errors: [{field, code}]}` diff --git a/rust/crates/core/src/tests/before_launch_steps.rs b/rust/crates/core/src/tests/before_launch_steps.rs index 1bfacf75..311a1f55 100644 --- a/rust/crates/core/src/tests/before_launch_steps.rs +++ b/rust/crates/core/src/tests/before_launch_steps.rs @@ -286,6 +286,48 @@ fn the_editor_form_lists_candidates_and_checks_the_steps_it_saves() { assert_eq!(step_ids(&deploy), ["user:prepare", "user:serve"]); } +// Issue 1lck/Lithe-IDEA#2100: outside Windows a batch script is listed but +// cannot run, so the editor does not offer it as a step and a list that names +// it, such as one a Windows user saved in the project, is refused. +#[cfg(not(windows))] +#[test] +fn a_batch_script_outside_windows_is_no_step() { + let root = temporary_root("before-launch-batch"); + let _cleanup = Cleanup(root.clone()); + write_run_document( + &root, + ".lithe/run/configurations.yaml", + &json!({"version": 2, "configurations": [ + { + "id": "user:package", "name": "package", "provider": "process.batch", + "confidence": "declared", "extensions": {"process": {"script": "package.cmd"}} + }, + executable("user:app", "echo", &["user:package"]), + ]}), + ); + let form = call( + "runConfig.editorForm", + json!({"root": root, "configurationId": "user:app"}), + ); + let field = form["fields"] + .as_array() + .unwrap() + .iter() + .find(|field| field["id"] == "beforeLaunch") + .cloned() + .unwrap(); + assert_eq!(field["candidates"], json!([]), "{field}"); + let reply = call( + "runConfig.beforeLaunch", + json!({"root": root, "configurationId": "user:app"}), + ); + assert_eq!(reply["status"], "rejected", "{reply}"); + assert_eq!( + reply["refusal"], + json!({"code": "unavailable", "configurationId": "user:package"}) + ); +} + fn serde_yaml_value(text: &str) -> Value { serde_yaml_ng::from_str(text).expect("run documents are YAML") } diff --git a/rust/crates/core/src/tests/run_configuration.rs b/rust/crates/core/src/tests/run_configuration.rs index 429fdc87..d5faff7f 100644 --- a/rust/crates/core/src/tests/run_configuration.rs +++ b/rust/crates/core/src/tests/run_configuration.rs @@ -1739,12 +1739,29 @@ fn built_in_process_runs_are_created_edited_moved_and_deleted_through_the_editor .iter() .map(|entry| entry["provider"].as_str().unwrap()) .collect(); - let mut expected = vec!["process.shell", "process.powershell"]; - if cfg!(windows) { - expected.push("process.batch"); + // Every platform creates every kind; only batch says it does not run + // outside Windows. + assert_eq!( + providers, + [ + "process.shell", + "process.powershell", + "process.batch", + "process.executable" + ] + ); + for entry in types["data"]["types"].as_array().unwrap() { + let unavailable = entry["provider"] == "process.batch" && !cfg!(windows); + assert_eq!( + entry["availability"]["code"], + if unavailable { + serde_json::json!("unsupportedPlatform") + } else { + Value::Null + }, + "{entry}" + ); } - expected.push("process.executable"); - assert_eq!(providers, expected); // A new editor shows the automatic values the typed script implies. let form = call( @@ -1926,10 +1943,11 @@ fn built_in_process_runs_are_created_edited_moved_and_deleted_through_the_editor assert_eq!(again["ok"], false); } -/// Batch scripts run only on Windows: elsewhere they are left out of the -/// list with a diagnostic, and their editor is not offered. +/// Batch scripts run only on Windows. Elsewhere a saved one stays listed as +/// unavailable, so a project shared with Windows keeps it, and it can still be +/// created, edited and saved; launching it is refused. #[test] -fn batch_runs_are_unavailable_outside_windows() { +fn batch_runs_are_editable_but_unavailable_outside_windows() { if cfg!(windows) { return; } @@ -1943,22 +1961,44 @@ fn batch_runs_are_unavailable_outside_windows() { "extensions":{"process":{"script":"setup.cmd"}} }]}), ); + fs::write(root.join("setup.cmd"), "@echo off\r\necho setup\r\n").unwrap(); let resolved = run_config_command("runConfig.resolve", serde_json::json!({"root":root})); assert_eq!(resolved["ok"], true, "{resolved}"); - assert!(resolved["data"]["configurations"] - .as_array() - .unwrap() - .is_empty()); - assert_eq!( - resolved["data"]["diagnostics"][0]["code"], - "unsupportedPlatform" - ); + let listed = &resolved["data"]["configurations"][0]; + assert_eq!(listed["id"], "user:setup", "{resolved}"); + assert_eq!(listed["availability"]["status"], "unavailable"); + assert_eq!(listed["availability"]["code"], "unsupportedPlatform"); + assert_eq!(resolved["data"]["diagnostics"], serde_json::json!([])); + let form = run_config_command( + "runConfig.editorForm", + serde_json::json!({"root":root,"configurationId":"user:setup"}), + ); + assert_eq!(form["ok"], true, "{form}"); + assert_eq!(form["data"]["provider"], "process.batch"); + assert_eq!(form["data"]["values"]["script"], "setup.cmd"); + assert_eq!(form["data"]["availability"]["code"], "unsupportedPlatform"); + let new = run_config_command( "runConfig.editorForm", serde_json::json!({"root":root,"provider":"process.batch"}), ); - assert_eq!(form["ok"], false); - assert_eq!(form["error"]["code"], "not_supported", "{form}"); + assert_eq!(new["ok"], true, "{new}"); + assert_eq!(new["data"]["availability"]["code"], "unsupportedPlatform"); + + let mut values = form["data"]["values"].clone(); + values["scriptOptions"] = serde_json::json!("--fast"); + let saved = run_config_command( + "runConfig.saveConfiguration", + serde_json::json!({"root":root,"provider":"process.batch","configurationId":"user:setup","values":values}), + ); + assert_eq!(saved["ok"], true, "{saved}"); + assert_eq!(saved["data"]["status"], "saved", "{saved}"); + + let plan = run_config_command( + "runConfig.createLaunchPlan", + serde_json::json!({"root":root,"configurationId":"user:setup"}), + ); + assert_eq!(plan["ok"], false, "{plan}"); } /// A saved run whose working directory was removed stays listed as unavailable, diff --git a/rust/crates/execution/src/configuration/form.rs b/rust/crates/execution/src/configuration/form.rs index 7d1bf171..2f3f83cc 100644 --- a/rust/crates/execution/src/configuration/form.rs +++ b/rust/crates/execution/src/configuration/form.rs @@ -19,13 +19,20 @@ const FORM_VERSION: u32 = 1; /// Request for the configuration kinds a user can create on this platform. pub struct ConfigurationTypesRequest {} -/// The kinds of configuration a user can create here, in display order. -/// Batch scripts are listed only on Windows. +/// The kinds of configuration a user can create, in display order. Every +/// platform creates and edits every kind, so a project shared with Windows +/// keeps its batch scripts editable elsewhere; a kind this platform cannot +/// run carries the `availability` its configurations resolve with. pub fn configuration_types(_request: ConfigurationTypesRequest) -> Result { let types = Kind::ALL .into_iter() - .filter(|kind| kind.supported_on(Platform::HOST)) - .map(|kind| json!({ "provider": kind.provider() })) + .map(|kind| { + let mut item = json!({ "provider": kind.provider() }); + if let Some(availability) = process::platform_availability(kind, Platform::HOST) { + item["availability"] = json!(availability); + } + item + }) .collect::>(); Ok(json!({ "types": types })) } @@ -77,13 +84,17 @@ pub fn editor_form(request: EditorFormRequest) -> Result { let draft = Draft::read(kind, &values)?; let candidates = Resolved::read(&root, &layers)?.candidates(request.configuration_id.as_deref()); - Ok(json!({ + let mut form = json!({ "formVersion": FORM_VERSION, "provider": kind.provider(), "configurationId": request.configuration_id, "values": values, "fields": fields(kind, &root, &draft, candidates), - })) + }); + if let Some(availability) = process::platform_availability(kind, Platform::HOST) { + form["availability"] = json!(availability); + } + Ok(form) } #[derive(Debug, Deserialize)] @@ -277,22 +288,16 @@ pub fn delete_configuration(request: DeleteConfigurationRequest) -> Result Result { - let kind = Kind::from_provider(provider).ok_or_else(|| { + Kind::from_provider(provider).ok_or_else(|| { CoreError::new( ErrorCode::NotSupported, "Only built-in process runs have an editor form", ) .with_details(provider) - })?; - if !kind.supported_on(Platform::HOST) { - return Err(CoreError::new( - ErrorCode::NotSupported, - "Batch scripts run only on Windows", - )); - } - Ok(kind) + }) } /// Removes configuration `id` from a layer document; whether it was there. diff --git a/rust/crates/execution/src/configuration/process.rs b/rust/crates/execution/src/configuration/process.rs index f515a6c7..3b3fcf88 100644 --- a/rust/crates/execution/src/configuration/process.rs +++ b/rust/crates/execution/src/configuration/process.rs @@ -104,10 +104,22 @@ pub(crate) fn is_process_provider(provider: &str) -> bool { provider.starts_with("process.") } -/// Whether `platform` can run entries of `provider`. Providers that are not -/// built-in process runs are not judged here. -pub(crate) fn supported_on(provider: &str, platform: Platform) -> bool { - Kind::from_provider(provider).is_none_or(|kind| kind.supported_on(platform)) +/// Why `platform` cannot run entries of `provider`, absent when it can. +/// Providers that are not built-in process runs are not judged here. +pub(crate) fn provider_availability(provider: &str, platform: Platform) -> Option { + platform_availability(Kind::from_provider(provider)?, platform) +} + +/// Why `platform` cannot run `kind`, absent when it can: `unsupportedPlatform` +/// for a batch script outside Windows. Such an entry is still created, edited +/// and saved, so a project shared with Windows keeps it. +pub(crate) fn platform_availability(kind: Kind, platform: Platform) -> Option { + (!kind.supported_on(platform)).then(|| { + Availability::unavailable( + "unsupportedPlatform", + format!("{} runs only on Windows", kind.provider()), + ) + }) } /// The `extensions.process` choices of a built-in process run. diff --git a/rust/crates/execution/src/configuration/resolve.rs b/rust/crates/execution/src/configuration/resolve.rs index 12e99855..0f499952 100644 --- a/rust/crates/execution/src/configuration/resolve.rs +++ b/rust/crates/execution/src/configuration/resolve.rs @@ -57,17 +57,6 @@ pub(crate) fn resolve_layers( } let global_toolchain = local.get("toolchain").cloned(); let platform = Platform::HOST; - configurations.retain(|configuration| { - let supported = super::process::supported_on(&configuration.provider, platform); - if !supported { - diagnostics.push(json!({ - "id": configuration.id, - "code": "unsupportedPlatform", - "message": format!("{} runs only on Windows", configuration.provider) - })); - } - supported - }); for configuration in &mut configurations { configuration.plugin = layers .plugin_dependency(&configuration.id) @@ -117,6 +106,13 @@ pub(crate) fn resolve_layers( })); continue; } + // A built-in kind this platform cannot run, such as a batch script + // outside Windows, stays listed and editable but does not start. + if let Some(availability) = + super::process::provider_availability(&configuration.provider, platform) + { + configuration.availability.get_or_insert(availability); + } if !project_directory_exists(root, &configuration.cwd) { let message = format!("Working directory does not exist: {}", configuration.cwd); diagnostics.push(json!({