Prepare: find installed JDKs; launcher shows the preparation error up top (#666)
A player installed Java 25 but JAVA_HOME still named an older JDK, so Prepare stopped with "Java 25 is needed", and the launcher showed that error at the foot of the page, out of sight until the window was enlarged. - horizon-prepare java::find: after JAVA_HOME, the PATH and macOS's java_home, look in the folders JDK installers use (Program Files vendors on Windows, /usr/lib/jvm on Linux, JavaVirtualMachines on macOS, ~/.jdks, SDKMAN), JDKs named for the pinned major first. - launcher: the preparation's progress and error sit right under the buttons instead of below the rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -56,9 +56,80 @@ fn javac_major(javac: &Path) -> Result<u32, String> {
|
||||
})
|
||||
}
|
||||
|
||||
/// 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<PathBuf> {
|
||||
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<PathBuf> {
|
||||
let mut homes: Vec<PathBuf> = 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 <major>` reports.
|
||||
/// ran them), else, on macOS, the JDK `/usr/libexec/java_home -v <major>` reports, else a JDK
|
||||
/// in the folders installers put them in (`jdk_parents`).
|
||||
pub fn find(major: i64) -> Result<Jdk, String> {
|
||||
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<Jdk, String> {
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user