From 08491bcda3433fca20b5e3f0c27e0077df967921 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 05:59:21 +0100 Subject: [PATCH] Java selection: keep Windows JDK homes free of the \\?\ prefix fs::canonicalize returns verbatim paths on Windows, and the chosen home goes into JAVA_HOME and the PATH of external preparers. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-prepare/src/java.rs | 36 +++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/crates/horizon-prepare/src/java.rs b/crates/horizon-prepare/src/java.rs index c5115be5e..d46cb9428 100644 --- a/crates/horizon-prepare/src/java.rs +++ b/crates/horizon-prepare/src/java.rs @@ -36,13 +36,31 @@ fn executable(dir: &Path, name: &str) -> PathBuf { }) } +/// `fs::canonicalize`, without the `\\?\` prefix it gives a Windows drive path: the +/// home goes into `JAVA_HOME` and the PATH of preparers, whose lookups don't take it. +fn real_path(path: PathBuf) -> PathBuf { + let real = fs::canonicalize(&path).unwrap_or(path); + if cfg!(windows) { + without_verbatim(real) + } else { + real + } +} + +fn without_verbatim(path: PathBuf) -> PathBuf { + match path.to_str().and_then(|s| s.strip_prefix(r"\\?\")) { + Some(rest) if rest.as_bytes().get(1) == Some(&b':') => PathBuf::from(rest), + _ => path, + } +} + /// Resolve compiler symlinks before picking its sibling runtime. The first `java` on the /// PATH may be an older, runtime-only install; it must not be paired with a newer `javac`. fn path_jdks(path: &OsStr) -> Vec { std::env::split_paths(path) .map(|dir| executable(&dir, "javac")) .filter(|javac| javac.is_file()) - .map(|javac| fs::canonicalize(&javac).unwrap_or(javac)) + .map(real_path) .filter_map(|javac| { let bin = javac.parent()?; // macOS's system tools are dispatchers, not a JDK home: JAVA_HOME=/usr @@ -362,6 +380,22 @@ fn run(mut command: Command, log: &fs::File) -> Result<(), String> { mod tests { use super::*; + #[test] + fn verbatim_drive_paths_lose_their_prefix() { + let jdk = r"C:\Program Files\Eclipse Adoptium\jdk-25\bin\javac.exe"; + assert_eq!( + without_verbatim(PathBuf::from(format!(r"\\?\{jdk}"))), + PathBuf::from(jdk) + ); + // A UNC share keeps its verbatim form: stripping it would leave no valid path. + let unc = r"\\?\UNC\server\jdk\bin\javac.exe"; + assert_eq!(without_verbatim(PathBuf::from(unc)), PathBuf::from(unc)); + assert_eq!( + without_verbatim(PathBuf::from("/usr/lib/jvm/bin/javac")), + PathBuf::from("/usr/lib/jvm/bin/javac") + ); + } + #[test] fn installed_jdks_prefer_the_pinned_major() { let dir = std::env::temp_dir().join(format!("horizon-prepare-jdks-{}", std::process::id()));