diff --git a/crates/ide-host/src/lib.rs b/crates/ide-host/src/lib.rs index 64c015edd..7cd79f91f 100644 --- a/crates/ide-host/src/lib.rs +++ b/crates/ide-host/src/lib.rs @@ -253,6 +253,16 @@ impl Host { self.project(window)?.require_trust() } + /// [`Self::require_trust`] for IDE-wide toolchain work (SDK detection, tool installs) that + /// also runs from a window without a project, such as Settings opened from the Welcome + /// screen: there is no project there to trust. + pub fn require_trust_if_project(&self, window: &str) -> IdeResult<()> { + match self.project(window) { + Ok(project) => project.require_trust(), + Err(_) => Ok(()), + } + } + /// The window that owns what `window` starts. pub fn owner_window<'a>(&self, window: &'a str) -> &'a str { (self.owner)(window) diff --git a/crates/ide-host/src/ops/sdk.rs b/crates/ide-host/src/ops/sdk.rs index 155ee2937..973fe6218 100644 --- a/crates/ide-host/src/ops/sdk.rs +++ b/crates/ide-host/src/ops/sdk.rs @@ -34,9 +34,9 @@ pub struct ProjectInterpreter { } /// Installed interpreters / toolchains of `kind`; project-local ones first. -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn sdk_detect(host: &Host, window: &str, kind: SdkKind) -> IdeResult> { - host.require_trust(window)?; + host.require_trust_if_project(window)?; let project = host.project(window).ok(); if kind == SdkKind::Jdk { host.apply_added_jdks(); @@ -63,10 +63,10 @@ pub async fn sdk_detect(host: &Host, window: &str, kind: SdkKind) -> IdeResult IdeResult> { // Newest first. The open project is skipped: `sdk_detect` lists its own. - host.require_trust(window)?; + host.require_trust_if_project(window)?; let current = host.project(window).ok().map(|p| p.root.clone()); let config = host.config_dir.clone(); // Probing runs the project's interpreter (and its settings may name any program): only @@ -111,9 +111,9 @@ pub async fn sdk_python_project_interpreters(host: &Host, window: &str) -> IdeRe } /// An existing Python executable or virtualenv folder as an SDK; `None` if Python cannot run. -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn sdk_python_interpreter(host: &Host, window: &str, path: PathBuf) -> IdeResult> { - host.require_trust(window)?; + host.require_trust_if_project(window)?; let root = host.project(window).ok().map(|p| p.root.clone()); tokio::task::spawn_blocking(move || Ok(ide_sdk::python_interpreter_sdk(&path, root.as_deref()))) .await @@ -320,7 +320,7 @@ pub async fn sdk_locate_tools( } /// Creates a Python environment with `tool` (`venv` / `uv` / `poetry`); output streams through `on_output`. -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn sdk_python_create_venv( host: &Host, window: &str, @@ -330,7 +330,7 @@ pub async fn sdk_python_create_venv( request: RequestId, on_output: Channel, ) -> IdeResult { - host.require_trust(window)?; + host.require_trust_if_project(window)?; let cancel = host.begin_project_request(window, request); let result = tokio::task::spawn_blocking(move || { ide_sdk::python::create_venv(tool, &base, &location, &cancel, &|line| { @@ -343,14 +343,14 @@ pub async fn sdk_python_create_venv( result? } -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn sdk_python_packages( host: &Host, window: &str, python: PathBuf, request: RequestId, ) -> IdeResult> { - host.require_trust(window)?; + host.require_trust_if_project(window)?; let cancel = host.begin_project_request(window, request); let result = tokio::task::spawn_blocking(move || ide_sdk::python::list_packages(&python, &cancel)).await.map_err(join_err); @@ -360,7 +360,7 @@ pub async fn sdk_python_packages( /// Installs (`uninstall = false`) or removes a package; output streams through `on_output`. #[allow(clippy::too_many_arguments)] -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn sdk_python_pip( host: &Host, window: &str, @@ -371,7 +371,7 @@ pub async fn sdk_python_pip( request: RequestId, on_output: Channel, ) -> IdeResult<()> { - host.require_trust(window)?; + host.require_trust_if_project(window)?; let project_root = host.project(window).ok().map(|p| p.root.clone()); let cancel = host.begin_project_request(window, request); let result = tokio::task::spawn_blocking(move || { diff --git a/crates/ide-host/src/ops/tools.rs b/crates/ide-host/src/ops/tools.rs index ba2603737..8d5428c8b 100644 --- a/crates/ide-host/src/ops/tools.rs +++ b/crates/ide-host/src/ops/tools.rs @@ -11,7 +11,7 @@ use std::path::PathBuf; /// The installer, reporting the executables chosen in the window's settings. fn tools(host: &Host, window: &str) -> IdeResult { - host.require_trust(window)?; + host.require_trust_if_project(window)?; let data = host.local_data_dir.clone(); let (settings, root) = match host.project(window) { Ok(project) => (project.settings.effective(), project.root.clone()), @@ -56,7 +56,7 @@ fn reconfigure_servers(host: &Host) { } /// Status of every known language server / debug adapter and the languages using it. -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn tools_list(host: &Host, window: &str) -> IdeResult> { let tools = tools(host, window)?; let langs = languages(host, window); @@ -65,7 +65,7 @@ pub async fn tools_list(host: &Host, window: &str) -> IdeResult> { /// Installs (or reinstalls) a tool into `/tools`, streaming output through /// `on_event`. Cancel with `cancel_request(request)`. -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn tools_install( host: &Host, window: &str, @@ -93,7 +93,7 @@ pub async fn tools_install( /// Newer versions of the tools the IDE installed (release downloads and npm packages). /// Cancel with `cancel_request(request)`. -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn tools_updates(host: &Host, window: &str, request: RequestId) -> IdeResult> { let tools = tools(host, window)?; let cancel = host.begin_request(request); @@ -103,7 +103,7 @@ pub async fn tools_updates(host: &Host, window: &str, request: RequestId) -> Ide } /// Removes a tool the IDE installed. -#[command(trust = trusted)] +#[command(trust = outside_restricted)] pub async fn tools_uninstall(host: &Host, window: &str, name: String) -> IdeResult { let tools = tools(host, window)?; let langs = languages(host, window); diff --git a/crates/ide-host/tests/trust.rs b/crates/ide-host/tests/trust.rs index c7089548f..b791ed2f4 100644 --- a/crates/ide-host/tests/trust.rs +++ b/crates/ide-host/tests/trust.rs @@ -45,6 +45,9 @@ async fn a_commands_trust_class_decides_where_it_runs() { assert_eq!(allowed("main", class), in_restricted, "{class:?} in Restricted Mode"); assert_eq!(allowed("welcome", class), without_project, "{class:?} without a project"); } + // Toolchain detection runs from Settings opened on the Welcome screen, but not in Restricted Mode. + assert!(ide_host::ops::sdk::sdk_detect(&host, "welcome", ide_sdk::SdkKind::Zig).await.is_ok()); + assert!(ide_host::ops::sdk::sdk_detect(&host, "main", ide_sdk::SdkKind::Zig).await.is_err()); // These requests validate the vendor before any network access, independently of project trust. let error = ide_host::ops::tools::jdk_download_releases(&host, "unknown-vendor".into(), ide_core::RequestId(901)) .await diff --git a/src-tauri/tests/commands.snap b/src-tauri/tests/commands.snap index f18998065..f1456b902 100644 --- a/src-tauri/tests/commands.snap +++ b/src-tauri/tests/commands.snap @@ -536,7 +536,7 @@ scaffold_options always project_host bin scaffold_requirements always project_host bindings async sdk_container_python trusted project_host bindings sync sdk_container_python_refresh trusted project_host bindings async -sdk_detect trusted project_host bindings async +sdk_detect outside_restricted project_host bindings async sdk_jdk_at trusted project_host bindings async sdk_locate_tools trusted project_host bindings async sdk_php_composer_install trusted project_host bindings async @@ -544,12 +544,12 @@ sdk_php_effective trusted project_host bin sdk_php_language_level trusted project_host bindings async sdk_php_xdebug trusted project_host bindings async sdk_project_libraries trusted project_host bindings async -sdk_python_create_venv trusted project_host bindings async +sdk_python_create_venv outside_restricted project_host bindings async sdk_python_effective trusted project_host bindings sync -sdk_python_interpreter trusted project_host bindings async -sdk_python_packages trusted project_host bindings async -sdk_python_pip trusted project_host bindings async -sdk_python_project_interpreters trusted project_host bindings async +sdk_python_interpreter outside_restricted project_host bindings async +sdk_python_packages outside_restricted project_host bindings async +sdk_python_pip outside_restricted project_host bindings async +sdk_python_project_interpreters outside_restricted project_host bindings async search_start always project_host bindings async search_validate always project_host bindings sync set_recent_project_groups always desktop bindings sync @@ -595,10 +595,10 @@ term_resize always project_host bin term_spawn always project_host bindings async term_store_image always project_host bindings async term_write always project_host bindings sync -tools_install trusted project_host bindings async -tools_list trusted project_host bindings async -tools_uninstall trusted project_host bindings async -tools_updates trusted project_host bindings async +tools_install outside_restricted project_host bindings async +tools_list outside_restricted project_host bindings async +tools_uninstall outside_restricted project_host bindings async +tools_updates outside_restricted project_host bindings async undo_workspace_edit always project_host bindings async user_font_data always desktop - async vcs_add_to_ignore trusted project_host bindings async diff --git a/src/features/settings/__tests__/sdkDetectFailure.test.tsx b/src/features/settings/__tests__/sdkDetectFailure.test.tsx new file mode 100644 index 000000000..8c3f68d40 --- /dev/null +++ b/src/features/settings/__tests__/sdkDetectFailure.test.tsx @@ -0,0 +1,29 @@ +import { act, renderHook } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; + +vi.mock("@/platform/ipc", async (original) => { + const actual = await original(); + return { + ...actual, + isTauri: () => true, + commands: { + ...actual.commands, + sdkDetect: () => + Promise.resolve({ status: "error", error: { kind: "unavailable", message: "detection failed", data: null } }), + }, + }; +}); + +import { detectSdks } from "../languages/api"; +import { useDetected } from "../languages/components"; + +describe("useDetected", () => { + it("stops loading when detection fails", async () => { + const { result } = renderHook(() => useDetected("jdk")); + expect(result.current.loading).toBe(true); + await act(() => detectSdks("jdk")); + expect(result.current.loading).toBe(false); + expect(result.current.list).toEqual([]); + expect(result.current.error).toContain("detection failed"); + }); +}); diff --git a/src/features/settings/languages/components.tsx b/src/features/settings/languages/components.tsx index 698ad57c4..a5dd08aaf 100644 --- a/src/features/settings/languages/components.tsx +++ b/src/features/settings/languages/components.tsx @@ -35,7 +35,8 @@ export function useDetected(kind: SdkKind): { list: Sdk[]; loading: boolean; err useEffect(() => { void detectSdks(kind); }, [kind]); - return { list: list ?? [], loading: loading || !list, error, refresh: () => void detectSdks(kind, true) }; + // A failed detection is over too: it shows no choices instead of loading forever. + return { list: list ?? [], loading: loading || (!list && !error), error, refresh: () => void detectSdks(kind, true) }; } export interface SdkFieldProps {