Merge main into the launcher removal branch

Main moved the removal to a thread in #1715 (Msg::Removed, the removing
row, the selection), which is this branch's UI half: main's version is
kept. What stays is backend::remove's retry on a file Windows still
holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 09:38:45 +01:00
129 changed files with 9415 additions and 2253 deletions
+17 -20
View File
@@ -1034,21 +1034,18 @@ fn download_for_owner(
}
}
/// A JDK of at least `major` for the preparation. `None` when horizon-prepare finds one by
/// itself (JAVA_HOME, the PATH, an installed JDK): the preparation then runs as before, in an
/// unchanged environment (a JDK found on the PATH can be a stub such as macOS's
/// /usr/bin/javac, whose folder is no Java home: `JAVA_HOME=/usr` makes it call itself
/// forever). Else the home of the Java the official launcher downloaded into one of the
/// `games` folders, else of Minecraft's own Java downloaded from Mojang into Horizon's game
/// folder, the runtime the version JSON at `manifest` names (Mojang's runtimes are whole JDKs).
/// The home of a JDK whose compiler and runtime support `major`. Forward even an installed
/// JDK to external preparers: older versions may otherwise rediscover incompatible PATH tools.
/// Else try the official launcher's Java in `games`, then download the runtime named by
/// `manifest` into Horizon's game folder (Mojang's runtimes are whole JDKs).
fn java(
major: i64,
games: &[PathBuf],
manifest: &Path,
log: &dyn Fn(String),
) -> Result<Option<PathBuf>, String> {
if horizon_prepare::java::find(major, &[]).is_ok() {
return Ok(None);
) -> Result<PathBuf, String> {
if let Ok(jdk) = horizon_prepare::java::find(major, &[]) {
return Ok(jdk.home());
}
let mut runtimes: Vec<PathBuf> = Vec::new();
for home in games
@@ -1060,7 +1057,7 @@ fn java(
}
}
let missing = match horizon_prepare::java::find(major, &runtimes) {
Ok(jdk) => return Ok(Some(jdk.home())),
Ok(jdk) => return Ok(jdk.home()),
Err(e) => e,
};
let component = read_json(manifest)
@@ -1085,7 +1082,7 @@ fn java(
let home = mojang::java_runtime(&component, &runtime, 8, &fetch, log).map_err(|e| {
format!("{missing}\n • Horizon also tried to download Minecraft's own Java from Mojang, but: {e}")
})?;
horizon_prepare::java::find(major, &[home]).map(|jdk| Some(jdk.home()))
horizon_prepare::java::find(major, &[home]).map(|jdk| jdk.home())
}
/// The dynamic loader glibc programs on x86-64 Linux start through.
@@ -1208,14 +1205,12 @@ pub fn preparer(
import: !reused,
minecraft_dir: Some(minecraft.clone()),
server_jar: (!reused).then_some(jar),
java_home: java_home.clone(),
java_home: Some(java_home.clone()),
};
return Ok((Preparer::InProcess(options), minecraft));
}
let mut cmd = Command::new(binary);
if let Some(home) = &java_home {
with_java(&mut cmd, home);
}
with_java(&mut cmd, &java_home);
cmd.current_dir(&dir).arg("--root").arg(&dir);
if !reused {
cmd.args(["--prepare", "--server-jar"]).arg(&jar);
@@ -1224,9 +1219,7 @@ pub fn preparer(
return Ok((Preparer::Process(cmd), minecraft));
}
let mut cmd = Command::new(python.unwrap());
if let Some(home) = &java_home {
with_java(&mut cmd, home);
}
with_java(&mut cmd, &java_home);
cmd.current_dir(&dir)
.args(["-u", "tools/prepare_native_client.py"]);
if !reused {
@@ -2182,7 +2175,7 @@ pub enum Msg {
Links(Links),
Progress(String, f32),
Installed(String, Result<(), String>),
/// A removal finished off the UI thread.
/// A version's files are gone (or could not be deleted).
Removed(String, Result<(), String>),
Line(Job, String),
Exit(Job, Result<(), String>),
@@ -2350,6 +2343,10 @@ impl Settings {
#[path = "backend/release_loading_tests.rs"]
mod release_loading_tests;
#[cfg(test)]
#[path = "backend/java_tests.rs"]
mod java_tests;
#[cfg(test)]
mod tests {
use super::*;
+85
View File
@@ -0,0 +1,85 @@
use super::*;
#[test]
fn selected_jdk_reaches_external_preparers() {
const CHILD: &str = "HORIZON_LAUNCHER_JAVA_TEST";
if let Some(root) = std::env::var_os(CHILD) {
let root = PathBuf::from(root);
let home = java(25, &[], &root.join("missing.json"), &|_| {}).unwrap();
assert_eq!(
fs::canonicalize(&home).unwrap(),
fs::canonicalize(root.join("jdk25")).unwrap()
);
// An older preparer invokes Java tools by name; both must now resolve to this JDK.
for name in ["java", "javac"] {
let mut cmd = crate::process::command(name);
with_java(&mut cmd, &home);
let output = cmd.arg("-version").output().unwrap();
assert!(output.status.success());
let text = String::from_utf8_lossy(&output.stdout).into_owned()
+ &String::from_utf8_lossy(&output.stderr);
assert!(text.contains("25.0.1"), "{name}: {text}");
assert_eq!(
cmd.get_envs()
.find(|(key, _)| *key == "JAVA_HOME")
.unwrap()
.1,
Some(home.as_os_str())
);
}
return;
}
let root = std::env::temp_dir().join(format!("horizon-launcher-java-{}", std::process::id()));
fs::create_dir_all(&root).unwrap();
let executable =
|bin: &Path, name: &str| bin.join(format!("{name}{}", std::env::consts::EXE_SUFFIX));
let probe = executable(&root, "probe");
let source = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../crates/horizon-prepare/tests/support/java_probe.rs");
let output = crate::process::command("rustc")
.arg(source)
.arg("-o")
.arg(&probe)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
for (home, compiler, runtime) in [
("jre21", None, "21.0.1"),
("jdk25", Some("25.0.1"), "25.0.1"),
] {
let bin = root.join(home).join("bin");
fs::create_dir_all(&bin).unwrap();
for (name, version) in [("javac", compiler), ("java", Some(runtime))] {
if let Some(version) = version {
fs::copy(&probe, executable(&bin, name)).unwrap();
fs::write(bin.join(format!("{name}.version")), version).unwrap();
}
}
}
let output = crate::process::command(std::env::current_exe().unwrap())
.args([
"--exact",
"backend::java_tests::selected_jdk_reaches_external_preparers",
"--nocapture",
])
.env(CHILD, &root)
.env_remove("JAVA_HOME")
.env(
"PATH",
std::env::join_paths([root.join("jre21/bin"), root.join("jdk25/bin")]).unwrap(),
)
.output()
.unwrap();
assert!(
output.status.success(),
"{}\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
fs::remove_dir_all(root).unwrap();
}
+59 -24
View File
@@ -144,8 +144,8 @@ struct Launcher {
releases: Load<Vec<Release>>,
installed: Vec<String>,
installing: Option<(String, f32)>,
/// The version being deleted: the removal runs off the UI thread, so the window has to know
/// which row is busy until `Msg::Removed` arrives.
/// The version being deleted on a background thread. It is already out of `installed`, so
/// nothing can launch, select or prepare it, and its row can't install or remove it again.
removing: Option<String>,
install_error: Option<(String, String)>,
confirm_remove: Option<String>,
@@ -419,7 +419,7 @@ impl Launcher {
let Some(release) = self.updates.horizon.clone() else {
return;
};
if self.installing.is_some() {
if self.installing.is_some() || self.removing.as_ref() == Some(&release.tag) {
return;
}
let select =
@@ -520,16 +520,6 @@ impl Launcher {
self.bar = (self.bar.1, p);
self.installing = Some((tag, p));
}
Msg::Removed(tag, result) => {
self.removing = None;
self.installed = backend::installed();
self.settings.selected = Settings::load()
.selected
.or_else(|| self.installed.first().cloned());
if let Err(e) = result {
self.install_error = Some((tag, e));
}
}
Msg::Installed(tag, result) => {
self.installing = None;
self.bar = (0., 0.);
@@ -556,6 +546,17 @@ impl Launcher {
Err(e) => self.install_error = Some((tag, e)),
}
}
Msg::Removed(tag, result) => {
if self.removing.as_ref() == Some(&tag) {
self.removing = None;
}
self.installed = backend::installed();
self.settings.selected =
selection_after_removal(&self.installed, Settings::load().selected);
if let Err(e) = result {
self.install_error = Some((tag, e));
}
}
Msg::Updates(Ok(updates)) => {
self.update_error = None;
self.updates = updates;
@@ -720,7 +721,7 @@ impl Launcher {
}
fn install(&mut self, release: &Release) {
if release.asset.is_none() {
if release.asset.is_none() || self.removing.as_ref() == Some(&release.tag) {
return;
}
let (release, tx) = (release.clone(), self.tx.clone());
@@ -738,13 +739,22 @@ impl Launcher {
});
}
/// Deletes an installed version on a background thread: a version is thousands of files and
/// the window must keep drawing meanwhile. `Msg::Removed` ends it.
fn remove(&mut self, tag: &str) {
// Same shape as install: the folder is hundreds of megabytes and Windows may hold a file
// for a moment, so the deletion goes to a thread instead of stalling the window.
let (tag, tx) = (tag.to_string(), self.tx.clone());
self.removing = Some(tag.clone());
self.confirm_remove = None;
if self.removing.is_some()
|| self.installing.as_ref().is_some_and(|(t, _)| t == tag)
|| !self.installed.iter().any(|t| t == tag)
{
return;
}
self.install_error = None;
self.removing = Some(tag.to_string());
self.installed.retain(|t| t != tag);
self.settings.selected =
selection_after_removal(&self.installed, self.settings.selected.take());
let (tag, tx) = (tag.to_string(), self.tx.clone());
std::thread::spawn(move || {
let result = backend::remove_version(&tag);
let _ = tx.send(Msg::Removed(tag, result));
@@ -1174,6 +1184,14 @@ fn segments<T: Copy + PartialEq + 'static>(
}))
}
/// The selected version once one has been removed: the saved choice if it is still installed,
/// else the newest left.
fn selection_after_removal(installed: &[String], saved: Option<String>) -> Option<String> {
saved
.filter(|s| installed.contains(s))
.or_else(|| installed.first().cloned())
}
fn disabled(b: Stateful<Div>) -> Stateful<Div> {
b.opacity(theme::DISABLED).cursor_default().tab_stop(false)
}
@@ -2937,9 +2955,9 @@ impl Launcher {
let keep = |i: usize| {
let tag = &releases[i].tag;
self.installing.as_ref().is_some_and(|(t, _)| t == tag)
|| self.removing.as_deref() == Some(tag.as_str())
|| self.install_error.as_ref().is_some_and(|(t, _)| t == tag)
|| self.confirm_remove.as_ref() == Some(tag)
|| self.removing.as_ref() == Some(tag)
};
let list = self.long_list.clone().column(
&self.content_scroll.handle,
@@ -2976,11 +2994,14 @@ impl Launcher {
.child(format!("{:.0}%", p * 100.)),
)
.into_any_element()
} else if self.removing.as_deref() == Some(tag.as_str()) {
// No Use or Remove while the folder is going.
text(theme::CAPTION)
.text_color(t.muted)
.child("Removing…")
} else if self.removing.as_ref() == Some(&tag) {
div()
.flex()
.items_center()
.gap(px(10.))
.w(px(180.))
.child(progress_bar(None, &t))
.child(text(theme::MICRO).text_color(t.muted).child("Removing…"))
.into_any_element()
} else if installed {
let confirming = self.confirm_remove.as_ref() == Some(&tag);
@@ -3610,6 +3631,20 @@ mod tests {
assert!(hit.iter().any(|b| b.action().partial_eq(&CloseWindow)));
}
#[test]
fn the_selection_survives_a_removal_or_moves_to_the_newest_left() {
let left = ["v0.2.49".to_string(), "v0.2.48".to_string()];
let pick =
|saved: Option<&str>| super::selection_after_removal(&left, saved.map(String::from));
assert_eq!(pick(Some("v0.2.48")).as_deref(), Some("v0.2.48"));
assert_eq!(pick(Some("v0.2.47")).as_deref(), Some("v0.2.49"));
assert_eq!(pick(None).as_deref(), Some("v0.2.49"));
assert_eq!(
super::selection_after_removal(&[], Some("v0.2.47".into())),
None
);
}
#[test]
fn the_app_menu_has_quit() {
let menus = super::menus();