From bdc4b1ef98a160a96436a44e55cbc12af6f1702c Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Sun, 6 Dec 2026 04:57:15 +0800 Subject: [PATCH] fix(scaffold): use a discovered Go for New Project when none is on PATH (#8298) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New Project › Go still reported go is not installed when GOROOT was on Default and go wasn't on the app's PATH (started from the Dock or Finder), although Settings › Languages › Go lists the Go installs it finds. With no GOROOT chosen and no go on PATH, the wizard now uses the first detected Go install for the requirement check and the generator steps; a go on PATH still wins. Detection runs only for generators that run go. --- crates/ide-host/src/ops/scaffold.rs | 33 ++++++++++++++++++++----- crates/ide-scaffold/src/engine.rs | 11 ++++++++- crates/ide-scaffold/src/engine/tests.rs | 8 ++++++ crates/ide-scaffold/src/lib.rs | 2 +- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/crates/ide-host/src/ops/scaffold.rs b/crates/ide-host/src/ops/scaffold.rs index 4f47933cbd..e903bfada2 100644 --- a/crates/ide-host/src/ops/scaffold.rs +++ b/crates/ide-host/src/ops/scaffold.rs @@ -34,16 +34,36 @@ pub async fn scaffold_options( /// The programs a generator needs for `values`, and whether they are installed. #[command(trust = always)] pub async fn scaffold_requirements(host: &Host, generator: String, values: Values) -> IdeResult> { - let goroot = goroot(host); - blocking(move || ide_scaffold::requirements(&generator, &values, goroot.as_deref())).await + let configured = configured_goroot(host); + blocking(move || { + let goroot = goroot(configured, &generator, &values); + ide_scaffold::requirements(&generator, &values, goroot.as_deref()) + }) + .await } -/// The GOROOT chosen in the global settings (those of new projects). Without one, `go` comes -/// from `PATH`, where a version manager picks it for the target folder. -fn goroot(host: &Host) -> Option { +/// The GOROOT chosen in the global settings (those of new projects). +fn configured_goroot(host: &Host) -> Option { host.global_settings.read_effective(|s| ide_sdk::apply::absolute_setting_path(&s.languages.go.goroot)) } +/// The Go a generator that needs `go` runs: the GOROOT chosen in Settings; else `go` from `PATH`, +/// where a version manager picks it for the target folder; else, with no `go` on `PATH` (an app +/// started from the Dock or Finder), the first Go install found on this machine, as listed in +/// Settings › Languages › Go. Blocking: detection runs `go version`. +fn goroot(configured: Option, generator: &str, values: &Values) -> Option { + configured.or_else(|| { + if !ide_scaffold::needs_program(generator, values, "go") { + return None; + } + let path = ide_core::process::host_var_os("PATH").unwrap_or_default(); + if ide_core::process::find_executable("go", &path).is_some() { + return None; + } + ide_sdk::detect(ide_sdk::SdkKind::Go, None).into_iter().find_map(|sdk| sdk.home.map(IpcPath::into_path_buf)) + }) +} + /// Whether a project named `name` cannot be created in `location`: something other than an /// empty folder is already there. #[command(trust = always)] @@ -68,8 +88,9 @@ pub async fn scaffold_create( ) -> IdeResult { // Trusted because the user just created it; a failed or cancelled run leaves no files behind. let cancel = host.begin_request(request); - let goroot = goroot(host); + let configured = configured_goroot(host); let result = blocking(move || { + let goroot = goroot(configured, &req.generator, &req.values); ide_scaffold::create(&req, goroot.as_deref(), &cancel, &|e| { let _ = on_event.send(e); }) diff --git a/crates/ide-scaffold/src/engine.rs b/crates/ide-scaffold/src/engine.rs index 044d48de9a..c3f6b34b92 100644 --- a/crates/ide-scaffold/src/engine.rs +++ b/crates/ide-scaffold/src/engine.rs @@ -55,7 +55,7 @@ impl Toolchain { /// it and as `GOROOT`, as Run and gopls do, while that GOROOT still holds a `go`. fn new(def: &Def, values: &Values, goroot: Option<&Path>) -> Self { let search = search_path(); - let needs_go = def.requires.iter().any(|r| matches(&r.when, values) && render(&r.program, values) == "go"); + let needs_go = needs(def, values, "go"); let usable = |g: &&Path| ide_core::tool_path::find_command("go", g.join("bin").as_os_str(), g).is_some(); match goroot.filter(|_| needs_go).filter(usable) { Some(goroot) => Toolchain { @@ -151,6 +151,15 @@ fn requirement(r: &RequireDef, values: &Values, tools: &Toolchain) -> Requiremen Requirement { program, min, found: found.filter(|v| !v.is_empty()), satisfied, hint: r.hint.clone() } } +fn needs(def: &Def, values: &Values, program: &str) -> bool { + def.requires.iter().any(|r| matches(&r.when, values) && render(&r.program, values) == program) +} + +/// Whether generator `id` runs `program` for `values`. +pub fn needs_program(id: &str, values: &Values, program: &str) -> bool { + def(id).is_some_and(|def| needs(def, &with_defaults(def, "app", values), program)) +} + /// The programs `id` needs for `values`, and whether this machine has them. pub fn requirements(id: &str, values: &Values, goroot: Option<&Path>) -> IdeResult> { let def = def(id).ok_or_else(|| unknown(id))?; diff --git a/crates/ide-scaffold/src/engine/tests.rs b/crates/ide-scaffold/src/engine/tests.rs index 173db6d1f7..3a1550a6d6 100644 --- a/crates/ide-scaffold/src/engine/tests.rs +++ b/crates/ide-scaffold/src/engine/tests.rs @@ -702,3 +702,11 @@ fn an_unusable_go_chosen_in_settings_is_ignored() { let tools = Toolchain::new(&go, &values, Some(&goroot)); assert_eq!((tools.search, tools.goroot), (search_path(), None)); } + +/// New Project asks for a discovered Go only for the generators that run `go`. +#[test] +fn only_go_generators_need_go() { + assert!(needs_program("go", &Values::new(), "go")); + assert!(!needs_program("node", &Values::new(), "go")); + assert!(!needs_program("no-such-generator", &Values::new(), "go")); +} diff --git a/crates/ide-scaffold/src/lib.rs b/crates/ide-scaffold/src/lib.rs index 3884a9e34b..1764542e37 100644 --- a/crates/ide-scaffold/src/lib.rs +++ b/crates/ide-scaffold/src/lib.rs @@ -10,7 +10,7 @@ mod providers; mod template; pub use catalog::catalog; -pub use engine::{Emit, create, free_name, name_error, options, requirements, target_taken}; +pub use engine::{Emit, create, free_name, name_error, needs_program, options, requirements, target_taken}; pub use model::{ ChoiceOption, CreateRequest, Dependency, DependencyGroup, Field, FieldKind, GeneratorInfo, Requirement, ScaffoldEvent, Values,