From fe334bf3a2ee344ca88aa86978937ea8719ccf83 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 16 Nov 2026 12:14:14 -0300 Subject: [PATCH 1/2] fix(gp): keep other languages' pack errors and say damaged kept packs in gp.geolibre (#2224) Putting a pack in use cleared every language's load error, so a damaged kept pack for another language went unsaid. Now only the activated language's error is cleared. gp.geolibre also checks the kept packs not in use (without putting any in use), so a damaged one is said before its language is asked for. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/gp.rs | 1 + crates/geolibre/src/langpack.rs | 44 ++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/gp.rs b/crates/engine/src/cmd/gp.rs index 95d33c3ce..58c77c297 100644 --- a/crates/engine/src/cmd/gp.rs +++ b/crates/engine/src/cmd/gp.rs @@ -156,6 +156,7 @@ pub fn geolibre_passed_over() -> Option { fn geolibre_languages() -> Value { #[cfg(all(feature = "geolibre", not(target_arch = "wasm32")))] { + rustgis_geolibre::langpack::check_kept(); let errors: serde_json::Map = rustgis_geolibre::langpack::load_errors().into_iter().map(|(locale, e)| (locale, Value::String(e))).collect(); json!({"available": rustgis_geolibre::langpack::LOCALES, "installed": rustgis_geolibre::langpack::kept(), diff --git a/crates/geolibre/src/langpack.rs b/crates/geolibre/src/langpack.rs index 145b19dc4..c52562fe9 100644 --- a/crates/geolibre/src/langpack.rs +++ b/crates/geolibre/src/langpack.rs @@ -323,6 +323,14 @@ fn tools_key(tools: &[ToolSpec]) -> (usize, usize) { /// Puts `pack` in use (`None`: none), in place of the one in use. pub fn activate(pack: Option) { + // Only the language put in use is no longer in error: another language's damaged pack stays + // said until it's replaced or removed. + if let Some(locale) = pack.as_ref().map(|p| p.locale.clone()) + && let Ok(mut e) = LOAD_ERRORS.lock() + && let Some(e) = e.as_mut() + { + e.remove(&locale); + } if let Ok(mut a) = ACTIVE.lock() { *a = pack.map(|pack| { let tools = published(); @@ -332,9 +340,6 @@ pub fn activate(pack: Option) { if let Ok(mut m) = MISSING.lock() { *m = None; } - if let Ok(mut e) = LOAD_ERRORS.lock() { - *e = None; - } REVISION.fetch_add(1, Ordering::Relaxed); } @@ -430,6 +435,25 @@ fn file(locale: &str) -> Result { dir().map(|d| d.join(format!("{locale}.json"))).ok_or_else(|| "can't find the app's data folder".into()) } +/// Reads every kept pack not in use and not already known to be damaged, and records why one +/// can't be used, without putting any in use: `gp.geolibre` says a damaged pack even when nothing +/// asked for its language yet. One read of about 2 MB per kept language. +#[cfg(not(target_arch = "wasm32"))] +pub fn check_kept() { + let Ok(_lifecycle) = LIFECYCLE.lock() else { return }; + let active = active_locale(); + for locale in kept() { + if active.as_deref() == Some(locale.as_str()) || load_error(&locale).is_some() { + continue; + } + if let Err(e) = load(&locale) + && let Ok(mut errors) = LOAD_ERRORS.lock() + { + errors.get_or_insert_with(Default::default).insert(locale, e); + } + } +} + /// The languages whose packs are kept, sorted. #[cfg(not(target_arch = "wasm32"))] pub fn kept() -> Vec { @@ -688,6 +712,20 @@ mod tests { assert!(!in_use("pt")); assert!(load_error("pt").is_some_and(|e| e.contains("JSON")), "{:?}", load_error("pt")); assert_eq!(load_errors().len(), 1); + // Putting another language's pack in use leaves the damaged one said. + std::fs::write(&src, pack_text("fr")).unwrap(); + import(&src).unwrap(); + assert!(in_use("fr")); + assert!(load_error("pt").is_some(), "pt's error kept after fr is put in use"); + // A damaged kept pack nothing asked for yet is said by a check that puts nothing in use. + if let Ok(mut e) = LOAD_ERRORS.lock() { + *e = None; + } + assert!(load_errors().is_empty()); + check_kept(); + assert_eq!(load_errors().iter().map(|(l, _)| l.as_str()).collect::>(), ["pt"]); + assert_eq!(active_locale().as_deref(), Some("fr"), "the check changes nothing in use"); + remove("fr").unwrap(); remove("pt").unwrap(); assert!(kept().is_empty() && load_error("pt").is_none() && !in_use("pt")); assert!(remove("pt").is_err(), "nothing left to remove"); From 3cd8a8cc93916c7d0791225946b24d21ffed03d8 Mon Sep 17 00:00:00 2001 From: elpidioassinaturas Date: Mon, 16 Nov 2026 13:18:05 -0300 Subject: [PATCH 2/2] fix(gp): a removal waits for a kept-pack check in progress (#2224) remove() didn't take LIFECYCLE, so it could delete a pack between check_kept()'s failed read and the error it records, leaving an error for a pack no longer kept. The kept-pack test holds the lock as a check would and expects the removal to wait. Co-Authored-By: Claude Opus 5.5 --- crates/geolibre/src/langpack.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/geolibre/src/langpack.rs b/crates/geolibre/src/langpack.rs index c52562fe9..4951ba850 100644 --- a/crates/geolibre/src/langpack.rs +++ b/crates/geolibre/src/langpack.rs @@ -560,6 +560,9 @@ pub fn import(path: &std::path::Path) -> Result<(String, usize), String> { /// Deletes the pack kept for `locale`, and stops using it. #[cfg(not(target_arch = "wasm32"))] pub fn remove(locale: &str) -> Result<(), String> { + // With `check_kept`: a read that failed just before the file went can't record an error for + // a pack no longer kept. + let _lifecycle = LIFECYCLE.lock().map_err(|_| "the tool translations are in an unusable state".to_string())?; let path = file(locale)?; if !path.is_file() { return Err(format!("no tool translations are kept for '{locale}'")); @@ -725,7 +728,14 @@ mod tests { check_kept(); assert_eq!(load_errors().iter().map(|(l, _)| l.as_str()).collect::>(), ["pt"]); assert_eq!(active_locale().as_deref(), Some("fr"), "the check changes nothing in use"); - remove("fr").unwrap(); + // A removal waits for a check in progress (it holds `LIFECYCLE`), so the check can't + // record an error for a pack already gone. + let check = LIFECYCLE.lock().unwrap(); + let removal = std::thread::spawn(|| remove("fr")); + std::thread::sleep(std::time::Duration::from_millis(200)); + assert!(!removal.is_finished(), "remove ran during a check"); + drop(check); + removal.join().unwrap().unwrap(); remove("pt").unwrap(); assert!(kept().is_empty() && load_error("pt").is_none() && !in_use("pt")); assert!(remove("pt").is_err(), "nothing left to remove");