diff --git a/crates/ide-sdk/src/java.rs b/crates/ide-sdk/src/java.rs index a06cdd087..889c43935 100644 --- a/crates/ide-sdk/src/java.rs +++ b/crates/ide-sdk/src/java.rs @@ -53,6 +53,12 @@ struct JavaProbe { static JAVA_PROBES: Mutex> = Mutex::new(BTreeMap::new()); +/// How long `java -version` may take before the JDK counts as unable to run. The first +/// run of a JDK can be slow: Rosetta translates an x86_64 JDK on Apple silicon, and +/// malware scans check a freshly downloaded one. Excluded, a project's JDK 8 leaves its +/// `JavaSE-1.8` to a newer JDK, and APIs JDK 8 has (`jdk.nashorn`) stop resolving. +const JAVA_PROBE_TIMEOUT: Duration = if cfg!(test) { Duration::from_secs(8) } else { Duration::from_secs(30) }; + /// Metadata can describe a JDK built for another architecture. Probe the executable /// before giving it to JDT, with a short cache shared by server and project selection. pub(crate) fn java_runs(java: &Path) -> bool { @@ -72,7 +78,7 @@ pub(crate) fn java_runs(java: &Path) -> bool { return probe.runnable; } } - let runnable = crate::exec::query(java, &["-version"], Duration::from_secs(5)).is_some(); + let runnable = crate::exec::query(java, &["-version"], JAVA_PROBE_TIMEOUT).is_some(); JAVA_PROBES.lock().unwrap_or_else(|e| e.into_inner()).insert( java.to_path_buf(), JavaProbe { modified, len: metadata.len(), permissions: metadata.permissions(), at: Instant::now(), runnable }, @@ -980,6 +986,26 @@ mod tests { assert_eq!(std::fs::read_to_string(&probes).unwrap().lines().count(), 1, "probed once"); } + #[cfg(unix)] + #[test] + fn a_jdk_8_slow_on_its_first_run_stays_the_java_8_runtime() { + let tmp = tempfile::tempdir().unwrap(); + let jdk8 = test_jdk(tmp.path(), 8, "rosetta8"); + let jdk25 = test_jdk(tmp.path(), 25, "native25"); + // Longer than the 5 seconds a first run under Rosetta used to be allowed. + std::fs::write( + java_executable(&jdk8.home), + "#!/bin/sh +/bin/sleep 6 +echo 'openjdk version 1.8' +", + ) + .unwrap(); + let rt = runtimes(&[jdk8.clone(), jdk25.clone()]); + assert_eq!(rt[0], json!({ "name": "JavaSE-1.8", "path": jdk8.home })); + assert_eq!(rt[1]["name"], "JavaSE-25"); + } + #[cfg(unix)] #[test] fn slow_java_probe_does_not_hold_the_cache_lock() {