@@ -273,6 +273,23 @@ fn initialize_database(app: &tauri::AppHandle) -> Result<(), Box<dyn std::error:
273273 } else {
274274 warn ! ( "Backend directory not found in any of these locations: {:?}" , possible_backend_paths) ;
275275 warn ! ( "Database will be created on first use when backend is available." ) ;
276+
277+ // For packaged apps, create an empty database file so the directory structure is correct
278+ // The backend executable will handle migrations when it starts
279+ if let Some ( parent) = db_path. parent ( ) {
280+ if let Err ( e) = std:: fs:: create_dir_all ( parent) {
281+ warn ! ( "Failed to create database directory: {}" , e) ;
282+ } else {
283+ // Create an empty database file - SQLite will initialize it properly when first accessed
284+ if !db_path. exists ( ) {
285+ if let Err ( e) = std:: fs:: File :: create ( & db_path) {
286+ warn ! ( "Failed to create database file: {}" , e) ;
287+ } else {
288+ info ! ( "Created empty database file at: {:?}" , db_path) ;
289+ }
290+ }
291+ }
292+ }
276293 }
277294
278295 Ok ( ( ) )
@@ -825,26 +842,109 @@ pub fn run() {
825842 let exe_path = std:: env:: current_exe ( ) . unwrap_or_default ( ) ;
826843 let exe_dir = exe_path. parent ( ) . unwrap_or ( std:: path:: Path :: new ( "." ) ) ;
827844
845+ info ! ( "Looking for bundled backend executable..." ) ;
846+ info ! ( "Executable path: {:?}" , exe_path) ;
847+ info ! ( "Executable directory: {:?}" , exe_dir) ;
848+
828849 let mut possible_exe_paths: Vec < PathBuf > = vec ! [ ] ;
829850
830851 // Try Tauri resource resolution (for bundled resources)
831- if let Ok ( resource_dir) = app_handle. path ( ) . resource_dir ( ) {
832- possible_exe_paths. push ( resource_dir. join ( "backend-server.exe" ) ) ;
833- possible_exe_paths. push ( resource_dir. join ( "backend-server" ) ) ;
852+ match app_handle. path ( ) . resource_dir ( ) {
853+ Ok ( resource_dir) => {
854+ info ! ( "Resource directory resolved: {:?}" , resource_dir) ;
855+ // Check if resource directory exists
856+ if resource_dir. exists ( ) {
857+ info ! ( "Resource directory exists, listing contents:" ) ;
858+ if let Ok ( entries) = std:: fs:: read_dir ( & resource_dir) {
859+ for entry in entries. flatten ( ) {
860+ info ! ( " - {:?}" , entry. path( ) ) ;
861+ }
862+ }
863+ } else {
864+ warn ! ( "Resource directory does not exist: {:?}" , resource_dir) ;
865+ }
866+ possible_exe_paths. push ( resource_dir. join ( "backend-server.exe" ) ) ;
867+ possible_exe_paths. push ( resource_dir. join ( "backend-server" ) ) ;
868+ }
869+ Err ( e) => {
870+ warn ! ( "Could not resolve resource directory: {}" , e) ;
871+ }
872+ }
873+
874+ // Also try resolving the resource directly using Tauri's resolve method
875+ // This might work better in some bundle configurations
876+ // Note: In Tauri v2, resolve might work differently, so we try both approaches
877+ if let Ok ( resource_path) = app_handle. path ( ) . resolve ( "backend-server" , tauri:: path:: BaseDirectory :: Resource ) {
878+ info ! ( "Resolved resource path (backend-server): {:?}" , resource_path) ;
879+ possible_exe_paths. push ( resource_path) ;
880+ }
881+ if let Ok ( resource_path) = app_handle. path ( ) . resolve ( "backend-server.exe" , tauri:: path:: BaseDirectory :: Resource ) {
882+ info ! ( "Resolved resource path (backend-server.exe): {:?}" , resource_path) ;
883+ possible_exe_paths. push ( resource_path) ;
884+ }
885+
886+ // For Linux AppImages, resources might be in a different location
887+ // AppImages extract to a temporary directory, and resources are in usr/lib or usr/share
888+ #[ cfg( target_os = "linux" ) ]
889+ {
890+ // Check AppImage extraction directory structure
891+ if let Ok ( appimage_path) = std:: env:: var ( "APPIMAGE" ) {
892+ info ! ( "Running as AppImage: {}" , appimage_path) ;
893+ if let Ok ( appdir) = std:: env:: var ( "APPDIR" ) {
894+ info ! ( "AppImage APPDIR: {}" , appdir) ;
895+ let appdir_path = PathBuf :: from ( & appdir) ;
896+ possible_exe_paths. push ( appdir_path. join ( "usr" ) . join ( "lib" ) . join ( "backend-server" ) ) ;
897+ possible_exe_paths. push ( appdir_path. join ( "usr" ) . join ( "share" ) . join ( "backend-server" ) ) ;
898+ possible_exe_paths. push ( appdir_path. join ( "resources" ) . join ( "backend-server" ) ) ;
899+ }
900+ }
901+
902+ // For DEB packages, resources are typically in /usr/lib or /usr/share
903+ // Check if we're in a system installation
904+ if exe_dir. starts_with ( "/usr" ) {
905+ possible_exe_paths. push ( PathBuf :: from ( "/usr/lib/budget-planer/backend-server" ) ) ;
906+ possible_exe_paths. push ( PathBuf :: from ( "/usr/share/budget-planer/backend-server" ) ) ;
907+ possible_exe_paths. push ( PathBuf :: from ( "/usr/lib/com.budgetplaner/backend-server" ) ) ;
908+ possible_exe_paths. push ( PathBuf :: from ( "/usr/share/com.budgetplaner/backend-server" ) ) ;
909+ }
834910 }
835911
836912 // Add paths relative to executable (fallback)
913+ // For standalone binaries, resources might be next to the executable
837914 possible_exe_paths. push ( exe_dir. join ( "backend-server.exe" ) ) ;
838915 possible_exe_paths. push ( exe_dir. join ( "backend-server" ) ) ;
839916 possible_exe_paths. push ( exe_dir. join ( "resources" ) . join ( "backend-server.exe" ) ) ;
840917 possible_exe_paths. push ( exe_dir. join ( "resources" ) . join ( "backend-server" ) ) ;
841918
919+ // For Linux, also check lib and share directories relative to executable
920+ // This is common for Linux applications and standalone binaries
921+ #[ cfg( target_os = "linux" ) ]
922+ {
923+ possible_exe_paths. push ( exe_dir. join ( "lib" ) . join ( "backend-server" ) ) ;
924+ possible_exe_paths. push ( exe_dir. join ( "share" ) . join ( "backend-server" ) ) ;
925+ possible_exe_paths. push ( exe_dir. join ( "usr" ) . join ( "lib" ) . join ( "backend-server" ) ) ;
926+ possible_exe_paths. push ( exe_dir. join ( "usr" ) . join ( "share" ) . join ( "backend-server" ) ) ;
927+ }
928+
842929 // Also check parent directories (for nested bundle structures)
843930 if let Some ( parent) = exe_dir. parent ( ) {
844931 possible_exe_paths. push ( parent. join ( "backend-server.exe" ) ) ;
845932 possible_exe_paths. push ( parent. join ( "backend-server" ) ) ;
846933 possible_exe_paths. push ( parent. join ( "resources" ) . join ( "backend-server.exe" ) ) ;
847934 possible_exe_paths. push ( parent. join ( "resources" ) . join ( "backend-server" ) ) ;
935+
936+ #[ cfg( target_os = "linux" ) ]
937+ {
938+ possible_exe_paths. push ( parent. join ( "lib" ) . join ( "backend-server" ) ) ;
939+ possible_exe_paths. push ( parent. join ( "share" ) . join ( "backend-server" ) ) ;
940+ }
941+ }
942+
943+ // Log all paths being checked
944+ info ! ( "Checking the following paths for backend executable:" ) ;
945+ for path in & possible_exe_paths {
946+ let exists = path. exists ( ) ;
947+ info ! ( " {:?} - {}" , path, if exists { "EXISTS" } else { "not found" } ) ;
848948 }
849949
850950 let bundled_exe = possible_exe_paths. iter ( ) . find ( |p| p. exists ( ) ) . cloned ( ) ;
0 commit comments