fix(scaffold): use a discovered Go for New Project when none is on PATH (#8298)

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.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-06 04:57:15 +08:00
1 джерело b4f4b103c6
коміт bdc4b1ef98
4 files changed
+46 -8

No files matched your search

+27 -6
Переглянути файл
@@ -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<Vec<Requirement>> {
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<PathBuf> {
/// The GOROOT chosen in the global settings (those of new projects).
fn configured_goroot(host: &Host) -> Option<PathBuf> {
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<PathBuf>, generator: &str, values: &Values) -> Option<PathBuf> {
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<IpcPath> {
// 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);
})
+10 -1
Переглянути файл
@@ -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<Vec<Requirement>> {
let def = def(id).ok_or_else(|| unknown(id))?;
+8
Переглянути файл
@@ -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"));
}
+1 -1
Переглянути файл
@@ -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,