diff --git a/src/backend/src/lib.rs b/src/backend/src/lib.rs index 2202301..246a901 100644 --- a/src/backend/src/lib.rs +++ b/src/backend/src/lib.rs @@ -183,25 +183,61 @@ "--parallel", "1" ]; - // Note: Tauri's sidecar() handles the library path (DLLs) if they are in the same folder - // as the sidecar binary in the App bundle. - // However, on Windows, setting the current_dir to the resource folder and - // adding it to PATH is the most robust way to ensure DLLs are found. let res_dir = app_handle.path().resource_dir().unwrap_or_default(); - let old_path = std::env::var("PATH").unwrap_or_default(); - let new_path = format!("{};{}", res_dir.display(), old_path); - - let mut sidecar = sidecar.args(args).env("PATH", new_path); - - // Set current directory to the resource folder so the sidecar can find DLLs "locally" - if res_dir.exists() { - sidecar = sidecar.current_dir(res_dir); + let exe_dir = std::env::current_exe() + .map(|p| p.parent().map(|parent| parent.to_path_buf()).unwrap_or_default()) + .unwrap_or_default(); + + // Diagnostic logging: List files in resource and exe directories + log::info!("Diagnostic: Resource Dir: {:?}", res_dir); + log::info!("Diagnostic: EXE Dir: {:?}", exe_dir); + + if let Ok(entries) = std::fs::read_dir(&res_dir) { + let files: Vec<_> = entries.filter_map(|e| e.ok().map(|entry| entry.file_name())).collect(); + log::info!("Diagnostic: Files in Resource Dir: {:?}", files); + } + if let Ok(entries) = std::fs::read_dir(&exe_dir) { + let files: Vec<_> = entries.filter_map(|e| e.ok().map(|entry| entry.file_name())).collect(); + log::info!("Diagnostic: Files in EXE Dir: {:?}", files); } - let (mut rx, _child) = match sidecar.spawn() { + // Comprehensive PATH candidates + let old_path = std::env::var("PATH").unwrap_or_default(); + let mut path_candidates = vec![ + res_dir.display().to_string(), + exe_dir.display().to_string(), + ]; + + // Also include potential nested bin folders + path_candidates.push(res_dir.join("bin").display().to_string()); + path_candidates.push(exe_dir.join("bin").display().to_string()); + // Also include parent (one level up from resources/exe dir) + if let Some(parent) = res_dir.parent() { + path_candidates.push(parent.display().to_string()); + } + if let Some(parent) = exe_dir.parent() { + path_candidates.push(parent.display().to_string()); + } + + let new_path = format!("{};{}", path_candidates.join(";"), old_path); + log::info!("Diagnostic: New Sidecar PATH: {}", new_path); + + let mut sidecar_cmd = sidecar.args(args).env("PATH", new_path); + + // Set current directory to the resource folder (where DLLs should be) + // Preference: res_dir > exe_dir + if res_dir.exists() { + log::info!("Setting sidecar current_dir to: {:?}", res_dir); + sidecar_cmd = sidecar_cmd.current_dir(&res_dir); + } else if exe_dir.exists() { + log::info!("Setting sidecar current_dir to: {:?}", exe_dir); + sidecar_cmd = sidecar_cmd.current_dir(&exe_dir); + } + + let (mut rx, _child) = match sidecar_cmd.spawn() { Ok(res) => res, Err(e) => { - eprintln!("CRITICAL: Failed to spawn sidecar: {}", e); + log::error!("CRITICAL: Failed to spawn sidecar: {}", e); return; } };