From 16d17b19922ba636db506d28bdedd13ed88bed47 Mon Sep 17 00:00:00 2001 From: lick <2188718831@qq.com> Date: Mon, 16 Nov 2026 02:33:25 -0800 Subject: [PATCH] =?UTF-8?q?fix(toolchain):=20=E5=B7=A5=E5=85=B7=E9=93=BE?= =?UTF-8?q?=E5=80=99=E9=80=89=E5=88=97=E5=87=BA=20JAVA=5FHOME=20=E5=92=8C?= =?UTF-8?q?=20PATH=20=E4=B8=8A=E7=9A=84=E5=AE=89=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `toolchains.candidates` 的 installations 只包含插件 provider 报告的安装和 discovery 扫描结果。插件没有为某个平台声明扫描目录时(Java 插件在 Windows 和 Linux 上就是这样),只配了 JAVA_HOME 或 PATH 的 JDK 不会出现在列表里。 新建项目的选择器因此显示“未找到已安装的版本”,设置页也无法选中它,但运行时解析 (resolve 的 environment 和 command 层)其实能找到并使用它。 - candidates 接受可选的 commandHomes(与 resolve 相同的宿主输入)。shell 行在原有 环境变量行之外,新增 PATH 命令行(带 command 字段)。 - 检查通过的 shell home 以 provider host:shell 并入 installations,按规范化 home 去重;同一个 home 已被扫描或 provider 报告时,保留原来的归属。 - selected(自动层的选择)不变:shell home 只是可选项,不参与自动层的选择。 Refs 1lck/Lithe-IDEA#2032 Co-Authored-By: Claude Opus 5.5 --- rust/contracts/rust-core-api.md | 2 +- .../core/src/tests/toolchain_resolution.rs | 57 ++++++++++++++++++ .../toolchain/src/resolution/automatic.rs | 59 +++++++++++++++---- 3 files changed, 104 insertions(+), 14 deletions(-) diff --git a/rust/contracts/rust-core-api.md b/rust/contracts/rust-core-api.md index b73a629a..82fdcbba 100644 --- a/rust/contracts/rust-core-api.md +++ b/rust/contracts/rust-core-api.md @@ -495,7 +495,7 @@ or removing a command fails until the table changes in the same commit. | `toolchains.resolve` | Resolve the fixed/project/global/automatic layers from one generation-bound snapshot, retaining unmet requirements, unavailable providers with their cause, conflicts and terminal fixed-value failures | | `toolchains.checkVersion` | Match captured output with an enabled plugin definition's version expression and compare generic numeric components with an optional minimum; never starts a probe | | `toolchains.discoveryPlan` | Return platform-filtered definition-owned scan declarations, layouts, probes, PATH entries, login-shell home variables and budgets; never scans native directories | -| `toolchains.candidates` | List one tool's login-shell homes and host-scanned installations with checked executables, versions or skip reasons, and the automatic-layer choice; never starts a probe | +| `toolchains.candidates` | List one tool's login-shell homes (declared `environment` variables and, from the optional `commandHomes`, declared commands found on the PATH) and host-scanned installations with checked executables, versions or skip reasons, and the automatic-layer choice; never starts a probe. A shell home that checks out is also an `installations` entry with provider `host:shell`, once per canonical home, so a picker can offer every home the resolver may run; `selected` remains the automatic layer's own choice | | `toolchains.instantiateLaunch` | Expand an enabled tool definition's `launch` template for a selected executable, entry, runtime arguments and arguments; a part the template cannot carry is `plugin.toolchain.launchMismatch`; performs no I/O | | `run.decodeStart` | Validate a plugin backend's `lithe/host/run/start` params against its manifest and the workspace, and return the normalized start with temporary-file paths substituted | | `debug.decodeBackendConfiguration` | Validate a plugin backend's `lithe/host/debug/start` params against its manifest and return the qualified adapter, request, label and configuration | diff --git a/rust/crates/core/src/tests/toolchain_resolution.rs b/rust/crates/core/src/tests/toolchain_resolution.rs index 3df263a7..8730ca25 100644 --- a/rust/crates/core/src/tests/toolchain_resolution.rs +++ b/rust/crates/core/src/tests/toolchain_resolution.rs @@ -652,6 +652,63 @@ fn toolchain_candidates_list_versions_and_share_the_resolver_choice() { assert_eq!(resolve(&value)["outcome"]["selection"]["home"], HOME); } +#[test] +fn toolchain_candidates_offer_shell_homes_that_no_discovery_scan_covers() { + // A JDK reached only through JAVA_HOME or the PATH is still one the resolver runs. + let mut value = input(); + installation(&mut value, HOME, "21.0.4"); + installation(&mut value, OLD, "17.0.12"); + value["environment"]["JAVA_HOME"] = json!(OLD); + let mut listing = listing(&value); + listing["commandHomes"] = json!({"java":HOME}); + let result = candidates_command(listing.clone(), validated_manifest_json).unwrap(); + assert_eq!(result["status"], "ok", "{result}"); + let listed = &result["candidates"]; + assert_eq!( + listed["installations"], + json!([ + {"home":HOME,"executable":format!("{HOME}/bin/java"),"version":"21.0.4","provider":"host:shell"}, + {"home":OLD,"executable":format!("{OLD}/bin/java"),"version":"17.0.12","provider":"host:shell"} + ]) + ); + assert_eq!( + listed["shell"], + json!([ + {"candidateID":"home","variable":"JAVA_HOME","home":OLD, + "executable":format!("{OLD}/bin/java"),"version":"17.0.12"}, + {"candidateID":"shell","command":"java","home":HOME, + "executable":format!("{HOME}/bin/java"),"version":"21.0.4"} + ]) + ); + // Shell homes are choices, not the automatic layer's own installations. + assert_eq!(listed["selected"], Value::Null); + + // One home reached through both routes, or also scanned, is listed once and keeps + // the scan's attribution. + listing["commandHomes"] = json!({"java":OLD}); + listing["systemInstallations"] = json!([{"tool":"java","home":OLD}]); + let listed = + candidates_command(listing.clone(), validated_manifest_json).unwrap()["candidates"].clone(); + assert_eq!( + listed["installations"], + json!([{"home":OLD,"executable":format!("{OLD}/bin/java"),"version":"17.0.12","provider":"host:system"}]) + ); + + // A shell home that fails its check is reported but never offered. + listing["systemInstallations"] = json!([]); + listing["minimumVersion"] = json!("21"); + let listed = + candidates_command(listing.clone(), validated_manifest_json).unwrap()["candidates"].clone(); + assert_eq!(listed["installations"], json!([])); + assert_eq!(listed["shell"][0]["reason"], "versionUnsatisfied"); + + listing["commandHomes"] = json!({"java":7}); + assert_eq!( + candidates_command(listing, validated_manifest_json).unwrap()["status"], + "rejected" + ); +} + #[test] fn toolchain_candidates_keep_provider_attribution_and_count_one_home_once() { let mut value = input(); diff --git a/rust/crates/toolchain/src/resolution/automatic.rs b/rust/crates/toolchain/src/resolution/automatic.rs index 2b2ed459..daa38024 100644 --- a/rust/crates/toolchain/src/resolution/automatic.rs +++ b/rust/crates/toolchain/src/resolution/automatic.rs @@ -116,6 +116,12 @@ pub(crate) fn automatic_choice( /// resolving a directory: login-shell homes and the discovered installations with their /// checked executables, versions or skip reasons, and Core's automatic choice. Hosts /// probe only the executables returned here, then call again with `probes`. +/// +/// `shell` reports each declared `environment` variable and `command` the host found +/// (`environment`, `commandHomes`). A shell home that checks out is also an entry of +/// `installations` under the `host:shell` provider, once per canonical home: it is a +/// home the resolver would run, so a picker must be able to offer it even where no +/// discovery scan covers it. `selected` stays the automatic layer's own choice. pub(crate) fn candidates(input: &Value, validate_manifest: ManifestValidator) -> Validation { fields( input, @@ -135,6 +141,7 @@ pub(crate) fn candidates(input: &Value, validate_manifest: ManifestValidator) -> "fixed", "projectCodeAllowed", "environment", + "commandHomes", ], )?; let registry = definitions::registry(&input["registry"], validate_manifest)?; @@ -150,31 +157,53 @@ pub(crate) fn candidates(input: &Value, validate_manifest: ManifestValidator) -> { return Err(reject("projectCodeAllowed")); } - string_maps(input, &["environment"])?; + string_maps(input, &["environment", "commandHomes"])?; let mut shell = Vec::new(); + let mut shell_selections = Vec::new(); for candidate in definition["executables"] .as_array() .into_iter() .flatten() - .filter(|value| value["kind"] == "environment" && applicable(value, snapshot.platform)) + .filter(|value| applicable(value, snapshot.platform)) { - let Some(variable) = candidate["variable"].as_str() else { - continue; + // The same lookups the resolver's environment and command layers make. + let (source, home, layout) = match candidate["kind"].as_str() { + Some("environment") => { + let Some(variable) = candidate["variable"].as_str() else { + continue; + }; + let home = input["environment"][variable].as_str(); + ( + json!({"variable":variable}), + home, + candidate["path"].as_str(), + ) + } + Some("command") => { + let Some(command) = candidate["command"].as_str() else { + continue; + }; + let home = input["commandHomes"][command].as_str(); + (json!({"command":command}), home, None) + } + _ => continue, }; - let Some(home) = input["environment"][variable] - .as_str() - .filter(|value| !value.is_empty()) - else { + let Some(home) = home.filter(|value| !value.is_empty()) else { continue; }; - let layout = candidate["path"].as_str(); - let mut row = json!({"candidateID":candidate["id"],"variable":variable,"home":home}); + let mut row = json!({"candidateID":candidate["id"],"home":home}); + for (key, value) in source.as_object().into_iter().flatten() { + row[key] = value.clone(); + } if let Ok(checked) = snapshot.checked(home, layout) { row["home"] = checked["home"].clone(); row["executable"] = checked["executable"].clone(); } match snapshot.layout_selection(home, layout, "host:shell", "global") { - Ok(value) => row["version"] = value["version"].clone(), + Ok(value) => { + row["version"] = value["version"].clone(); + shell_selections.push(value); + } Err(code) => row["reason"] = json!(code), } shell.push(row); @@ -182,8 +211,12 @@ pub(crate) fn candidates(input: &Value, validate_manifest: ManifestValidator) -> let mut reported = provider_installations(input, tool, ®istry)?; reported.extend(system_installations(input, tool)?); let set = automatic_set(&snapshot, &reported)?; - let mut installations: Vec<_> = set - .eligible + let mut listed = set.eligible.clone(); + for selection in shell_selections { + let key = snapshot.key(selection["home"].as_str().unwrap_or(""))?; + listed.entry(key).or_insert(selection); + } + let mut installations: Vec<_> = listed .values() .map(|value| { json!({"home":value["home"],"executable":value["executable"],