Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

Linux tray probe: log the loader's own reason, match it per name (GH #204)

purple3109b0ae7e94
diff --git a/src-tauri/src/system_tray.rs b/src-tauri/src/system_tray.rs
index ba5c0975..89f532e7 100644
--- a/src-tauri/src/system_tray.rs
+++ b/src-tauri/src/system_tray.rs
@@ -17,10 +17,18 @@ pub(crate) const APPINDICATOR_LIBS: &[&str] =
&["libayatana-appindicator3.so.1", "libappindicator3.so.1"];
/// The line a user reads in the terminal when LU started without a tray.
-pub(crate) const NO_TRAY_LOG: &str = "[Tray] No system tray: neither libayatana-appindicator3.so.1 \
- nor libappindicator3.so.1 could be loaded. LU runs without a tray icon, and closing the \
- window quits LU. Install libayatana-appindicator (Arch: libayatana-appindicator, \
- Debian/Ubuntu: libayatana-appindicator3-1, Fedora: libayatana-appindicator-gtk3) to get it back.";
+/// `reasons` is what the loader said for each name: "cannot open shared
+/// object file" means the package is missing, an "undefined symbol" means it
+/// is there but does not fit the libraries this build runs with.
+pub(crate) fn no_tray_log(reasons: &[String]) -> String {
+ format!(
+ "[Tray] No system tray: {}. LU runs without a tray icon, and closing the window \
+ quits LU. If the library is not installed, install libayatana-appindicator \
+ (Arch: libayatana-appindicator, Debian/Ubuntu: libayatana-appindicator3-1, \
+ Fedora: libayatana-appindicator-gtk3) to get it back.",
+ reasons.join("; ")
+ )
+}
/// Can the tray be built? `loads` answers whether one library name loads,
/// injected so every branch runs on a Mac too.
@@ -28,6 +36,18 @@ pub(crate) fn tray_available(linux: bool, loads: impl Fn(&str) -> bool) -> bool
!linux || APPINDICATOR_LIBS.iter().any(|name| loads(name))
}
+/// The loader's answer per name, turned into "tray or not" and the reasons
+/// for the log. A name counts only when ITS dlopen succeeded: the message of
+/// a failed one may name a different file (a dependency with an undefined
+/// symbol), so it is never matched against the name.
+pub(crate) fn probe_outcome(linux: bool, probed: Vec<(&str, Option<String>)>) -> (bool, Vec<String>) {
+ let available = tray_available(linux, |name| {
+ probed.iter().any(|(n, err)| *n == name && err.is_none())
+ });
+ let reasons = probed.into_iter().filter_map(|(_, err)| err).collect();
+ (available, reasons)
+}
+
/// Does the close button hide the window to the tray? Only when there is a
/// tray to bring it back from. Without one a hidden window could never be
/// shown again and the process would sit there with the GPU still full, so
@@ -37,36 +57,45 @@ pub(crate) fn close_hides_to_tray(tray_present: bool) -> bool {
}
/// The real loader, the same call `libloading` makes for the crate (dlopen
-/// with RTLD_NOW). The handle is closed again; the crate opens its own.
+/// with RTLD_NOW). None when the library loads (the handle is closed again,
+/// the crate opens its own), otherwise the loader's own message.
#[cfg(target_os = "linux")]
-pub(crate) fn dlopen_loads(name: &str) -> bool {
+pub(crate) fn dlopen_error(name: &str) -> Option<String> {
let Ok(c) = std::ffi::CString::new(name) else {
- return false;
+ return Some(format!("{name}: invalid name"));
};
- // SAFETY: a plain dlopen/dlclose pair on a NUL-terminated name. The
+ // SAFETY: a plain dlopen/dlclose pair on a NUL-terminated name, and
+ // dlerror read right after the failed call on the same thread. The
// library's constructors run, which the tray build would do anyway.
unsafe {
let handle = libc::dlopen(c.as_ptr(), libc::RTLD_NOW | libc::RTLD_LOCAL);
if handle.is_null() {
- return false;
+ let err = libc::dlerror();
+ if err.is_null() {
+ return Some(format!("{name}: cannot be loaded"));
+ }
+ return Some(std::ffi::CStr::from_ptr(err).to_string_lossy().into_owned());
}
libc::dlclose(handle);
}
- true
+ None
}
/// The answer for this process.
#[cfg(desktop)]
pub(crate) fn system_tray_available() -> bool {
#[cfg(target_os = "linux")]
- let available = tray_available(true, dlopen_loads);
+ let probed: Vec<(&str, Option<String>)> =
+ APPINDICATOR_LIBS.iter().map(|n| (*n, dlopen_error(n))).collect();
#[cfg(not(target_os = "linux"))]
- let available = tray_available(false, |_| true);
+ let probed: Vec<(&str, Option<String>)> = Vec::new();
+ let (available, reasons) = probe_outcome(cfg!(target_os = "linux"), probed);
if !available {
// println! as well as tracing, like the Wayland lines: a user who
// starts the AppImage from a terminal must see why there is no icon.
- println!("{NO_TRAY_LOG}");
- tracing::warn!(target: "tray", "{NO_TRAY_LOG}");
+ let line = no_tray_log(&reasons);
+ println!("{line}");
+ tracing::warn!(target: "tray", "{line}");
}
available
}
@@ -99,6 +128,35 @@ mod tests {
assert!(tray_available(false, |_| false));
}
+ #[test]
+ fn a_dependency_with_an_undefined_symbol_means_no_tray() {
+ // GH #204 on Arch with the package installed: the system dbusmenu
+ // needs a newer GLib than the AppImage carries.
+ let (available, reasons) = probe_outcome(
+ true,
+ vec![
+ ("libayatana-appindicator3.so.1", Some("/usr/lib/libdbusmenu-glib.so.4: undefined symbol: g_once_init_leave_pointer".into())),
+ ("libappindicator3.so.1", Some("libappindicator3.so.1: cannot open shared object file".into())),
+ ],
+ );
+ assert!(!available);
+ assert_eq!(reasons.len(), 2);
+ let (available, reasons) = probe_outcome(true, vec![("libayatana-appindicator3.so.1", None), ("libappindicator3.so.1", Some("x".into()))]);
+ assert!(available);
+ assert_eq!(reasons, vec!["x".to_string()]);
+ }
+
+ #[test]
+ fn the_log_line_carries_the_loaders_reason() {
+ let line = no_tray_log(&[
+ "/usr/lib/libdbusmenu-glib.so.4: undefined symbol: g_once_init_leave_pointer".into(),
+ "libappindicator3.so.1: cannot open shared object file: No such file or directory".into(),
+ ]);
+ assert!(line.starts_with("[Tray] No system tray: /usr/lib/libdbusmenu-glib.so.4: undefined symbol"));
+ assert!(line.contains("closing the window quits LU"));
+ assert!(line.contains("libayatana-appindicator3-1"));
+ }
+
#[test]
fn without_a_tray_the_close_button_quits() {
assert!(!close_hides_to_tray(false));
@@ -108,8 +166,9 @@ mod tests {
#[cfg(target_os = "linux")]
#[test]
fn the_real_loader_says_no_to_a_library_that_does_not_exist() {
- assert!(!dlopen_loads("liblu-p10-does-not-exist.so.1"));
- assert!(dlopen_loads("libc.so.6"));
+ let err = dlopen_error("liblu-p10-does-not-exist.so.1").expect("must not load");
+ assert!(err.contains("liblu-p10-does-not-exist.so.1"), "{err}");
+ assert_eq!(dlopen_error("libc.so.6"), None);
}
#[test]