@@ -8,24 +8,84 @@ pub(crate) fn daemon_binary_candidates() -> &'static [&'static str] {
88 }
99}
1010
11+ fn daemon_search_dirs ( executable_dir : & std:: path:: Path ) -> Vec < PathBuf > {
12+ let mut dirs = Vec :: new ( ) ;
13+
14+ let mut push_unique = |path : PathBuf | {
15+ if !dirs. iter ( ) . any ( |entry| entry == & path) {
16+ dirs. push ( path) ;
17+ }
18+ } ;
19+
20+ push_unique ( executable_dir. to_path_buf ( ) ) ;
21+
22+ #[ cfg( target_os = "macos" ) ]
23+ {
24+ if let Some ( contents_dir) = executable_dir. parent ( ) {
25+ push_unique ( contents_dir. join ( "Resources" ) ) ;
26+ }
27+ push_unique ( PathBuf :: from ( "/opt/homebrew/bin" ) ) ;
28+ push_unique ( PathBuf :: from ( "/usr/local/bin" ) ) ;
29+ }
30+
31+ #[ cfg( target_os = "linux" ) ]
32+ {
33+ push_unique ( PathBuf :: from ( "/usr/local/bin" ) ) ;
34+ push_unique ( PathBuf :: from ( "/usr/bin" ) ) ;
35+ push_unique ( PathBuf :: from ( "/usr/sbin" ) ) ;
36+ }
37+
38+ dirs
39+ }
40+
1141pub ( crate ) fn resolve_daemon_binary_path ( ) -> Result < PathBuf , String > {
42+ let mut attempted_paths: Vec < PathBuf > = Vec :: new ( ) ;
1243 let current_exe = std:: env:: current_exe ( ) . map_err ( |err| err. to_string ( ) ) ?;
1344 let parent = current_exe
1445 . parent ( )
1546 . ok_or_else ( || "Unable to resolve executable directory" . to_string ( ) ) ?;
1647 let candidate_names = daemon_binary_candidates ( ) ;
1748
18- for name in candidate_names {
19- let candidate = parent. join ( name) ;
20- if candidate. is_file ( ) {
21- return Ok ( candidate) ;
49+ if let Ok ( explicit_raw) = std:: env:: var ( "CODEX_MONITOR_DAEMON_PATH" ) {
50+ let explicit = explicit_raw. trim ( ) ;
51+ if !explicit. is_empty ( ) {
52+ let explicit_path = PathBuf :: from ( explicit) ;
53+ if explicit_path. is_file ( ) {
54+ return Ok ( explicit_path) ;
55+ }
56+ if explicit_path. is_dir ( ) {
57+ for name in candidate_names {
58+ let candidate = explicit_path. join ( name) ;
59+ if candidate. is_file ( ) {
60+ return Ok ( candidate) ;
61+ }
62+ attempted_paths. push ( candidate) ;
63+ }
64+ } else {
65+ attempted_paths. push ( explicit_path) ;
66+ }
2267 }
2368 }
2469
70+ for search_dir in daemon_search_dirs ( parent) {
71+ for name in candidate_names {
72+ let candidate = search_dir. join ( name) ;
73+ if candidate. is_file ( ) {
74+ return Ok ( candidate) ;
75+ }
76+ attempted_paths. push ( candidate) ;
77+ }
78+ }
79+
80+ let attempted = attempted_paths
81+ . iter ( )
82+ . map ( |path| path. display ( ) . to_string ( ) )
83+ . collect :: < Vec < _ > > ( )
84+ . join ( ", " ) ;
85+
2586 Err ( format ! (
26- "Unable to locate daemon binary in {} (tried: {})" ,
27- parent. display( ) ,
28- candidate_names. join( ", " )
87+ "Unable to locate daemon binary (tried: {})" ,
88+ attempted
2989 ) )
3090}
3191
0 commit comments