fix(settings): detect SDKs and install tools from Settings without a project (#4184)

SDK combos in Settings opened from the Welcome screen loaded forever: sdk_detect and the interpreter/tool commands were Trusted and called require_trust, which refuse a window without a project, and useDetected kept loading after a failed detection. They are now OutsideRestricted with Host::require_trust_if_project (still refused in Restricted Mode; commands that take a project path keep the strict check), and a failed detection ends loading.

Closes #4110
This commit is contained in:
l0ng-ai
2026-11-18 16:34:49 +08:00
committed by GitHub
parent aebd26bd7d
commit 5e4f187107
7 changed files with 71 additions and 28 deletions
+10
View File
@@ -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)
+12 -12
View File
@@ -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<Vec<Sdk>> {
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<V
}
/// The Python interpreters of the recently opened projects, each with its project.
#[command(trust = trusted)]
#[command(trust = outside_restricted)]
pub async fn sdk_python_project_interpreters(host: &Host, window: &str) -> IdeResult<Vec<ProjectInterpreter>> {
// 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<Option<Sdk>> {
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<String>,
) -> IdeResult<Sdk> {
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<Vec<PythonPackage>> {
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<String>,
) -> 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 || {
+5 -5
View File
@@ -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<Tools> {
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<Vec<ToolInfo>> {
let tools = tools(host, window)?;
let langs = languages(host, window);
@@ -65,7 +65,7 @@ pub async fn tools_list(host: &Host, window: &str) -> IdeResult<Vec<ToolInfo>> {
/// Installs (or reinstalls) a tool into `<app-data>/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<Vec<ToolUpdate>> {
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<ToolInfo> {
let tools = tools(host, window)?;
let langs = languages(host, window);
+3
View File
@@ -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
+10 -10
View File
@@ -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
@@ -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<typeof import("@/platform/ipc")>();
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");
});
});
@@ -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 {