diff --git a/crates/horizon-prepare/src/java.rs b/crates/horizon-prepare/src/java.rs index 860f4835a..d54ef0949 100644 --- a/crates/horizon-prepare/src/java.rs +++ b/crates/horizon-prepare/src/java.rs @@ -56,9 +56,80 @@ fn javac_major(javac: &Path) -> Result { }) } +/// The folders JDK installers and tools put JDKs in, each JDK one folder below (#666: a +/// player installed Java 25 but JAVA_HOME and the PATH still named an older one). +fn jdk_parents() -> Vec { + let var = |name: &str| { + std::env::var_os(name) + .filter(|v| !v.is_empty()) + .map(PathBuf::from) + }; + let home = var(if cfg!(windows) { "USERPROFILE" } else { "HOME" }); + let mut parents = Vec::new(); + if cfg!(windows) { + let vendors = [ + "Eclipse Adoptium", + "Java", + "Microsoft", + "Zulu", + "BellSoft", + "Amazon Corretto", + "Semeru", + "OpenJDK", + ]; + for base in ["ProgramFiles", "ProgramW6432"].into_iter().filter_map(var) { + parents.extend(vendors.iter().map(|v| base.join(v))); + } + } else if cfg!(target_os = "macos") { + parents.push(PathBuf::from("/Library/Java/JavaVirtualMachines")); + parents.extend( + home.iter() + .map(|h| h.join("Library/Java/JavaVirtualMachines")), + ); + } else { + parents.extend(["/usr/lib/jvm", "/usr/java", "/usr/local/lib/jvm"].map(PathBuf::from)); + } + parents.extend( + home.iter() + .flat_map(|h| [h.join(".jdks"), h.join(".sdkman/candidates/java")]), + ); + parents +} + +/// The JDKs (folders with `bin/javac`) one level below `parents`; a macOS bundle's home is +/// its `Contents/Home`. Those named for `major` come first, then the others newest name first. +fn installed_jdks(parents: &[PathBuf], major: i64) -> Vec { + let mut homes: Vec = parents + .iter() + .filter_map(|parent| fs::read_dir(parent).ok()) + .flatten() + .flatten() + .map(|entry| { + let bundle = entry.path().join("Contents/Home"); + if bundle.is_dir() { + bundle + } else { + entry.path() + } + }) + .filter(|home| executable(&home.join("bin"), "javac").is_file()) + .collect(); + let named = |home: &Path| { + home.iter().any(|part| { + part.to_string_lossy() + .split(|c: char| !c.is_ascii_digit()) + .any(|n| n == major.to_string()) + }) + }; + homes.sort_by(|a, b| named(b).cmp(&named(a)).then_with(|| b.cmp(a))); + homes.dedup(); + homes +} + /// Finds a JDK with `javac` of at least `major`, the version research-lock.json pins: /// `JAVA_HOME` when it holds one, else `javac` and `java` on the PATH (as the Python preparer -/// ran them), else, on macOS, the JDK `/usr/libexec/java_home -v ` reports. +/// ran them), else, on macOS, the JDK `/usr/libexec/java_home -v ` reports, else a JDK +/// in the folders installers put them in (`jdk_parents`). pub fn find(major: i64) -> Result { let need = format!( "Java {major} is needed (a JDK with javac, e.g. Eclipse Temurin {major}). \ @@ -93,6 +164,14 @@ pub fn find(major: i64) -> Result { } } } + for home in installed_jdks(&jdk_parents(), major) { + let bin = home.join("bin"); + candidates.push(( + executable(&bin, "javac"), + executable(&bin, "java"), + "an installed JDK", + )); + } let mut problems = Vec::new(); for (javac, java, from) in candidates { if !javac.is_file() || !java.is_file() { @@ -177,3 +256,49 @@ fn run(mut command: Command, log: &fs::File) -> Result<(), String> { std::thread::sleep(Duration::from_millis(50)); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn installed_jdks_prefer_the_pinned_major() { + let dir = std::env::temp_dir().join(format!("horizon-prepare-jdks-{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + let parent = dir.join("Eclipse Adoptium"); + for jdk in [ + "jdk-21.0.5.11-hotspot", + "jdk-25.0.1.8-hotspot", + "jdk-26.0.0-hotspot", + "jre-25.0.1", + ] { + let bin = parent.join(jdk).join("bin"); + fs::create_dir_all(&bin).unwrap(); + if jdk.starts_with("jdk") { + fs::write(executable(&bin, "javac"), b"").unwrap(); + } + } + // A macOS bundle keeps its home in Contents/Home. + let bundle = dir.join("JavaVirtualMachines/temurin-25.jdk/Contents/Home"); + fs::create_dir_all(bundle.join("bin")).unwrap(); + fs::write(executable(&bundle.join("bin"), "javac"), b"").unwrap(); + let found = installed_jdks( + &[ + parent.clone(), + dir.join("JavaVirtualMachines"), + dir.join("missing"), + ], + 25, + ); + assert_eq!( + found, + [ + bundle, + parent.join("jdk-25.0.1.8-hotspot"), + parent.join("jdk-26.0.0-hotspot"), + parent.join("jdk-21.0.5.11-hotspot"), + ] + ); + fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/launcher/src/main.rs b/launcher/src/main.rs index e6f9c12a1..3b2d0f32b 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -1508,8 +1508,9 @@ impl Launcher { .flex_col() .gap(px(32.)) .child(hero) - .child(actions) - .child(rows); + .child(actions); + // The preparation's progress and error right under the buttons, where the player + // looks after pressing Prepare, not below the fold (#666). if self.preparing { let last = self.prepare_log.back().cloned().unwrap_or_default(); page = page.child( @@ -1534,6 +1535,7 @@ impl Launcher { if let Some(e) = self.prepare_error.clone() { page = page.child(notice(e, &t)); } + page = page.child(rows); if let Some(e) = self.client_error.clone() { page = page.child(notice(e, &t)); }