From e07a2833f067d55819ad9bb02a446aeb1e4612e5 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Wed, 2 Dec 2026 13:28:36 +0800 Subject: [PATCH] fix(sdk): add a JDK from disk in Settings opened from the Welcome screen sdk_jdk_at was a trusted command, which needs an open trusted project, so Add JDK from Disk on the Welcome screen always failed and the error was shown as 'not a valid home for JDK'. SDKs are IDE-wide: sdk_jdk_at, sdk_flutter_at, sdk_locate_tools and sdk_php_xdebug now run outside Restricted Mode like sdk_detect (a restricted project still refuses them). A failed check now shows its error instead of calling the folder invalid. Fixes #7361 --- crates/ide-host/src/ops/sdk.rs | 21 +++++++++++-------- src-tauri/tests/commands.snap | 8 +++---- src/bindings.ts | 15 +++++++++---- .../__tests__/addJdkFromDisk.test.tsx | 13 ++++++++++++ .../languages/ProjectStructurePage.tsx | 11 ++++++++-- 5 files changed, 49 insertions(+), 19 deletions(-) diff --git a/crates/ide-host/src/ops/sdk.rs b/crates/ide-host/src/ops/sdk.rs index ab74a72add..859784b568 100644 --- a/crates/ide-host/src/ops/sdk.rs +++ b/crates/ide-host/src/ops/sdk.rs @@ -204,15 +204,15 @@ pub fn sdk_php_effective(host: &Host, window: &str) -> IdeResult, ) -> IdeResult { - host.require_trust(window)?; + host.require_trust_if_project(window)?; let cwd = cwd.or_else(|| host.project(window).ok().map(|p| p.root.clone())); blocking(move || ide_sdk::php::xdebug(&php, cwd.as_deref())).await } @@ -349,14 +349,15 @@ pub async fn sdk_project_libraries( } /// Looks up `names` (bare command names): `node_modules/.bin` above `near`, then the search path. -#[command(trust = trusted)] +/// Also from Settings on the Welcome screen (the PHP interpreter). +#[command(trust = outside_restricted)] pub async fn sdk_locate_tools( host: &Host, window: &str, names: Vec, near: Option, ) -> IdeResult { - host.require_trust(window)?; + host.require_trust_if_project(window)?; blocking_value(move || LocatedTools { tools: ide_sdk::locate::locate_tools(&names, near.as_deref()), compile_dir: ide_sdk::locate::compile_dir().into(), @@ -475,16 +476,18 @@ pub async fn sdk_python_pip( result } -/// The JDK whose home is `path`, as the SDK list shows it; `None` when it is no JDK home. -#[command(trust = trusted)] +/// The JDK whose home is `path`, as the SDK list shows it; `None` when it is no JDK home. SDKs +/// are IDE-wide, so Add JDK from Disk works from the Welcome screen as well (#7361). +#[command(trust = outside_restricted)] pub async fn sdk_jdk_at(path: String) -> IdeResult> { // A macOS bundle too (Add JDK from Disk…). blocking_value(move || ide_sdk::java::jdk_at(std::path::Path::new(&path)).map(|j| ide_sdk::jdk_sdk(&j))).await } /// `flutter --version --machine` of the Flutter SDK at `path`, or of the `flutter` on the -/// search path when `path` is empty; `None` when there is no Flutter SDK. -#[command(trust = trusted)] +/// search path when `path` is empty; `None` when there is no Flutter SDK. Also from Settings on +/// the Welcome screen, as the SDK list. +#[command(trust = outside_restricted)] pub async fn sdk_flutter_at( host: &Host, window: &str, diff --git a/src-tauri/tests/commands.snap b/src-tauri/tests/commands.snap index c318907b84..5f7902737d 100644 --- a/src-tauri/tests/commands.snap +++ b/src-tauri/tests/commands.snap @@ -720,13 +720,13 @@ scope_files always project_host bin sdk_container_python trusted project_host bindings async sdk_container_python_refresh trusted project_host bindings async sdk_detect outside_restricted project_host bindings async -sdk_flutter_at trusted project_host bindings async -sdk_jdk_at trusted project_host bindings async -sdk_locate_tools trusted project_host bindings async +sdk_flutter_at outside_restricted project_host bindings async +sdk_jdk_at outside_restricted project_host bindings async +sdk_locate_tools outside_restricted project_host bindings async sdk_php_composer_install trusted project_host bindings async sdk_php_effective trusted project_host bindings sync sdk_php_language_level trusted project_host bindings async -sdk_php_xdebug trusted project_host bindings async +sdk_php_xdebug outside_restricted project_host bindings async sdk_project_libraries trusted project_host bindings async sdk_python_create_venv outside_restricted project_host bindings async sdk_python_effective trusted project_host bindings async diff --git a/src/bindings.ts b/src/bindings.ts index 1307354753..875240f9d8 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -2087,7 +2087,8 @@ export const commands = { sdkDetect: (kind: SdkKind) => typedError(__TAURI_INVOKE("sdk_detect", { kind })), /** * `flutter --version --machine` of the Flutter SDK at `path`, or of the `flutter` on the - * search path when `path` is empty; `None` when there is no Flutter SDK. + * search path when `path` is empty; `None` when there is no Flutter SDK. Also from Settings on + * the Welcome screen, as the SDK list. */ sdkFlutterAt: (path: string) => typedError<{ /** SDK root (not part of Flutter's answer). */ @@ -2096,7 +2097,10 @@ export const commands = { channel?: string | null, dartSdkVersion?: string | null, } | null, IdeError>(__TAURI_INVOKE("sdk_flutter_at", { path })), - /** The JDK whose home is `path`, as the SDK list shows it; `None` when it is no JDK home. */ + /** + * The JDK whose home is `path`, as the SDK list shows it; `None` when it is no JDK home. SDKs + * are IDE-wide, so Add JDK from Disk works from the Welcome screen as well (#7361). + */ sdkJdkAt: (path: string) => typedError<{ /** * Executable (`python`, `node`, `pnpm`) or directory (Rust toolchain bin @@ -2116,7 +2120,10 @@ export const commands = { /** Rust standard library sources, Go default GOPATH. */ extra: IpcPath | null, } | null, IdeError>(__TAURI_INVOKE("sdk_jdk_at", { path })), - /** Looks up `names` (bare command names): `node_modules/.bin` above `near`, then the search path. */ + /** + * Looks up `names` (bare command names): `node_modules/.bin` above `near`, then the search path. + * Also from Settings on the Welcome screen (the PHP interpreter). + */ sdkLocateTools: (names: string[], near: string | null) => typedError(__TAURI_INVOKE("sdk_locate_tools", { names, near })), /** The Composer project `path` belongs to, when its dependencies need `composer install`. */ sdkPhpComposerInstall: (path: string) => typedError<{ @@ -2129,7 +2136,7 @@ export const commands = { sdkPhpLanguageLevel: (path: string) => typedError(__TAURI_INVOKE("sdk_php_language_level", { path })), /** * The version of the PHP interpreter `php` and of the Xdebug it loads, run in `cwd` (default: the - * project root). + * project root). Also from Settings on the Welcome screen. */ sdkPhpXdebug: (php: string, cwd: string | null) => typedError(__TAURI_INVOKE("sdk_php_xdebug", { php, cwd })), /** diff --git a/src/features/settings/__tests__/addJdkFromDisk.test.tsx b/src/features/settings/__tests__/addJdkFromDisk.test.tsx index 7ea850b2ec..eb11c1fab2 100644 --- a/src/features/settings/__tests__/addJdkFromDisk.test.tsx +++ b/src/features/settings/__tests__/addJdkFromDisk.test.tsx @@ -86,6 +86,19 @@ describe("SDKs › Add JDK from Disk…", () => { expect(added() ?? []).toEqual([]); }); + it("shows why the check failed instead of calling the folder invalid (#7361)", async () => { + const { commands } = await import("@/platform/ipc"); + vi.mocked(commands.sdkJdkAt).mockResolvedValueOnce({ + status: "error", + error: { kind: "untrusted", message: "This project is not trusted" }, + } as never); + open.mockResolvedValue("/opt/jdks/acme-17"); + await act(async () => addMenuItem("Add JDK from Disk…")); + expect(screen.getByRole("alert")).toHaveTextContent("This project is not trusted"); + expect(screen.getByRole("alert")).not.toHaveTextContent("not a valid home"); + expect(added() ?? []).toEqual([]); + }); + it("keeps detected SDKs from being removed", async () => { await act(async () => useSdks.setState({ byKind: { jdk: [JDK] } })); fireEvent.click(screen.getByRole("option", { name: /Acme 17\.0\.9/ })); diff --git a/src/features/settings/languages/ProjectStructurePage.tsx b/src/features/settings/languages/ProjectStructurePage.tsx index 6f170145e7..4c058e0eb9 100644 --- a/src/features/settings/languages/ProjectStructurePage.tsx +++ b/src/features/settings/languages/ProjectStructurePage.tsx @@ -30,7 +30,7 @@ import { T, } from "@/ui"; import { SettingsFormContext } from "@/workbench/contributions"; -import { call, commands, isTauri, nextRequestId, type FileEntry } from "@/platform/ipc"; +import { call, commands, isTauri, nextRequestId, type FileEntry, type Sdk } from "@/platform/ipc"; import { mergeLibraryGroups } from "@/platform/libraries"; import type { BuildModel, LibraryGroup, WebArtifact } from "@/bindings"; import { relativePath, useProject } from "@/platform/project"; @@ -1926,7 +1926,14 @@ function SdksSection() { setAddError(null); const picked = await choosePath({ directory: true, title: tx("Select Home Directory for JDK") }); if (!picked) return; - const sdk = await call(commands.sdkJdkAt(picked)).catch(() => null); + let sdk: Sdk | null; + try { + sdk = await call(commands.sdkJdkAt(picked)); + } catch (e) { + // Not a verdict on the directory: say what failed instead of calling it invalid. + setAddError(e instanceof Error ? e.message : String(e)); + return; + } if (!sdk) { setAddError(tx`The selected directory is not a valid home for JDK: ${tildify(picked)}`); return;