cargo_e/
e_command_builder.rs

1use regex::Regex;
2use std::collections::{HashMap, HashSet};
3use std::env;
4use std::io::Read;
5#[cfg(target_os = "windows")]
6use std::os::windows::process::CommandExt;
7use std::path::{Path, PathBuf};
8use std::process::Command;
9use std::sync::atomic::{AtomicBool, Ordering};
10use std::sync::mpsc::{channel, Sender};
11use std::time::SystemTime;
12use which::which;
13
14use crate::e_cargocommand_ext::CargoProcessResult;
15use crate::e_cargocommand_ext::{CargoCommandExt, CargoDiagnostic, CargoProcessHandle};
16use crate::e_eventdispatcher::{
17    CallbackResponse, CallbackType, CargoDiagnosticLevel, EventDispatcher, ThreadLocalContext,
18};
19use crate::e_runner::GLOBAL_CHILDREN;
20use crate::e_target::{CargoTarget, TargetKind, TargetOrigin};
21use std::sync::{Arc, Mutex};
22
23#[derive(Debug, Clone, PartialEq, Copy)]
24pub enum TerminalError {
25    NotConnected,
26    NoTerminal,
27    NoError,
28}
29
30impl Default for TerminalError {
31    fn default() -> Self {
32        TerminalError::NoError
33    }
34}
35
36/// A builder that constructs a Cargo command for a given target.
37#[derive(Clone, Debug)]
38pub struct CargoCommandBuilder {
39    pub target_name: String,
40    pub manifest_path: PathBuf,
41    pub args: Vec<String>,
42    pub subcommand: String,
43    pub pid: Option<u32>,
44    pub alternate_cmd: Option<String>,
45    pub execution_dir: Option<PathBuf>,
46    pub suppressed_flags: HashSet<String>,
47    pub stdout_dispatcher: Option<Arc<EventDispatcher>>,
48    pub stderr_dispatcher: Option<Arc<EventDispatcher>>,
49    pub progress_dispatcher: Option<Arc<EventDispatcher>>,
50    pub stage_dispatcher: Option<Arc<EventDispatcher>>,
51    pub terminal_error_flag: Arc<Mutex<bool>>,
52    pub sender: Option<Arc<Mutex<Sender<TerminalError>>>>,
53    pub diagnostics: Arc<Mutex<Vec<CargoDiagnostic>>>,
54    pub is_filter: bool,
55    pub use_cache: bool,
56    pub default_binary_is_runner: bool,
57    pub be_silent: bool,
58    pub detached: bool,
59    pub time_limit: Option<u32>,
60    pub detached_hold: Option<u32>,
61    pub detached_delay: Option<u32>,
62}
63
64impl std::fmt::Display for CargoCommandBuilder {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(
67            f,
68            "CargoCommandBuilder {{\n  target_name: {:?},\n  manifest_path: {:?},\n  args: {:?},\n  subcommand: {:?},\n  pid: {:?},\n  alternate_cmd: {:?},\n  execution_dir: {:?},\n  suppressed_flags: {:?},\n  is_filter: {:?}\n,\n  use_cache: {:?}\n}}",
69            self.target_name,
70            self.manifest_path,
71            self.args,
72            self.subcommand,
73            self.pid,
74            self.alternate_cmd,
75            self.execution_dir,
76            self.suppressed_flags,
77            self.is_filter,
78            self.use_cache,
79        )
80    }
81}
82impl Default for CargoCommandBuilder {
83    fn default() -> Self {
84        Self::new(
85            &String::new(),
86            &PathBuf::from("Cargo.toml"),
87            "run".into(),
88            false,
89            false,
90            false,
91            false,
92            false,
93        )
94    }
95}
96impl CargoCommandBuilder {
97    /// Creates a new, empty builder.
98    pub fn new(
99        target_name: &str,
100        manifest: &PathBuf,
101        subcommand: &str,
102        is_filter: bool,
103        use_cache: bool,
104        default_binary_is_runner: bool,
105        be_silent: bool,
106        detached: bool,
107    ) -> Self {
108        ThreadLocalContext::set_context(target_name, manifest.to_str().unwrap_or_default());
109        let (sender, _receiver) = channel::<TerminalError>();
110        let sender = Arc::new(Mutex::new(sender));
111        let mut builder = CargoCommandBuilder {
112            target_name: target_name.to_owned(),
113            manifest_path: manifest.clone(),
114            args: Vec::new(),
115            subcommand: subcommand.to_string(),
116            pid: None,
117            alternate_cmd: None,
118            execution_dir: None,
119            suppressed_flags: HashSet::new(),
120            stdout_dispatcher: None,
121            stderr_dispatcher: None,
122            progress_dispatcher: None,
123            stage_dispatcher: None,
124            terminal_error_flag: Arc::new(Mutex::new(false)),
125            sender: Some(sender),
126            diagnostics: Arc::new(Mutex::new(Vec::<CargoDiagnostic>::new())),
127            is_filter,
128            use_cache,
129            default_binary_is_runner,
130            be_silent,
131            detached,
132            time_limit: None,
133            detached_hold: None,
134            detached_delay: None,
135        };
136        builder.set_default_dispatchers();
137        builder
138    }
139
140    // Switch to passthrough mode when the terminal error is detected
141    fn switch_to_passthrough_mode<F>(self: Arc<Self>, on_spawn: F) -> anyhow::Result<u32>
142    where
143        F: FnOnce(u32, Arc<Mutex<CargoProcessHandle>>),
144    {
145        let mut command = self.build_command();
146
147        // Now, spawn the cargo process in passthrough mode
148        let cargo_process_handle = command.spawn_cargo_passthrough(Arc::clone(&self));
149        let pid = cargo_process_handle.pid;
150        // Notify observer
151        let cargo_process_handle = Arc::new(Mutex::new(cargo_process_handle));
152        on_spawn(pid, cargo_process_handle);
153
154        Ok(pid)
155    }
156
157    // Set up the default dispatchers, which includes error detection
158    fn set_default_dispatchers(&mut self) {
159        if !self.is_filter {
160            // If this is a filter, we don't need to set up dispatchers
161            return;
162        }
163        let sender = self.sender.clone().unwrap();
164
165        let mut stdout_dispatcher = EventDispatcher::new();
166        stdout_dispatcher.add_callback(
167            r"listening on",
168            Box::new(
169                |line: &str,
170                 _captures: Option<regex::Captures>,
171                 _state: std::sync::Arc<std::sync::atomic::AtomicBool>,
172                 stats: std::sync::Arc<std::sync::Mutex<crate::e_cargocommand_ext::CargoStats>>,
173                 _prior_response: Option<crate::e_eventdispatcher::CallbackResponse>|
174                 -> Option<crate::e_eventdispatcher::CallbackResponse> {
175                    println!("(STDOUT) Dispatcher caught: {}", line);
176                    // Use a regex to capture a URL from the line.
177                    // Move the regex construction outside the closure to avoid lifetime issues.
178                    static URL_REGEX: once_cell::sync::Lazy<Regex> =
179                        once_cell::sync::Lazy::new(|| Regex::new(r"(http://[^\s]+)").unwrap());
180                    if let Some(url_caps) = URL_REGEX.captures(line) {
181                        if let Some(url_match) = url_caps.get(1) {
182                            let url = url_match.as_str();
183                            // Call open::that on the captured URL.
184                            if let Err(e) = open::that_detached(url) {
185                                eprintln!("Failed to open URL: {}. Error: {}", url, e);
186                            } else {
187                                println!("Opened URL: {}", url);
188                            }
189                        }
190                    }
191                    let mut stats = stats.lock().unwrap();
192                    // Add debug statements to trace stats changes
193                    println!("[DEBUG] Locked stats: {:?}", *stats);
194                    if stats.build_finished_time.is_none() {
195                        let now = SystemTime::now();
196                        stats.build_finished_time = Some(now);
197                        // Add debug statements to trace stats changes
198                        println!(
199                            "[DEBUG] Updated stats.build_finished_time: {:?}",
200                            stats.build_finished_time
201                        );
202                    }
203                    None
204                },
205            )
206                as Box<
207                    dyn Fn(
208                            &str,
209                            Option<regex::Captures>,
210                            std::sync::Arc<std::sync::atomic::AtomicBool>,
211                            std::sync::Arc<std::sync::Mutex<crate::e_cargocommand_ext::CargoStats>>,
212                            Option<crate::e_eventdispatcher::CallbackResponse>,
213                        )
214                            -> Option<crate::e_eventdispatcher::CallbackResponse>
215                        + Send
216                        + Sync
217                        + 'static,
218                >,
219        );
220
221        stdout_dispatcher.add_callback(
222            r"BuildFinished",
223            Box::new(move |line, _captures, _state, stats, _prior_response| {
224                println!("******* {}", line);
225                let mut stats = stats.lock().unwrap();
226                // Add debug statements to trace stats changes
227                println!("[DEBUG] Locked stats: {:?}", *stats);
228                if stats.build_finished_time.is_none() {
229                    let now = SystemTime::now();
230                    stats.build_finished_time = Some(now);
231                    // Add debug statements to trace stats changes
232                    println!(
233                        "[DEBUG] Updated stats.build_finished_time: {:?}",
234                        stats.build_finished_time
235                    );
236                }
237                None
238            }),
239        );
240        stdout_dispatcher.add_callback(
241            r"server listening at:",
242            Box::new(move |line, _captures, state, stats, _prior_response| {
243                // If we're not already in multiline mode, this is the initial match.
244                if !state.load(Ordering::Relaxed) {
245                    println!("Matched 'server listening at:' in: {}", line);
246                    state.store(true, Ordering::Relaxed);
247                    Some(CallbackResponse {
248                        callback_type: CallbackType::Note, // Choose as appropriate
249                        message: Some(format!("Started multiline mode after: {}", line)),
250                        file: None,
251                        line: None,
252                        column: None,
253                        suggestion: None,
254                        terminal_status: None,
255                    })
256                } else {
257                    // We are in multiline mode; process subsequent lines.
258                    println!("Multiline callback received: {}", line);
259                    // Use a regex to capture a URL from the line.
260                    let url_regex = match Regex::new(r"(http://[^\s]+)") {
261                        Ok(regex) => regex,
262                        Err(e) => {
263                            eprintln!("Failed to create URL regex: {}", e);
264                            return None;
265                        }
266                    };
267                    if let Some(url_caps) = url_regex.captures(line) {
268                        let url = url_caps.get(1).unwrap().as_str();
269                        // Call open::that on the captured URL.
270                        match open::that_detached(url) {
271                            Ok(_) => println!("Opened URL: {}", url),
272                            Err(e) => eprintln!("Failed to open URL: {}. Error: {}", url, e),
273                        }
274                        let mut stats = stats.lock().unwrap();
275                        if stats.build_finished_time.is_none() {
276                            let now = SystemTime::now();
277                            stats.build_finished_time = Some(now);
278                        }
279                        // End multiline mode.
280                        state.store(false, Ordering::Relaxed);
281                        Some(CallbackResponse {
282                            callback_type: CallbackType::Note, // Choose as appropriate
283                            message: Some(format!("Captured and opened URL: {}", url)),
284                            file: None,
285                            line: None,
286                            column: None,
287                            suggestion: None,
288                            terminal_status: None,
289                        })
290                    } else {
291                        None
292                    }
293                }
294            }),
295        );
296
297        let mut stderr_dispatcher = EventDispatcher::new();
298
299        let suggestion_mode = Arc::new(AtomicBool::new(false));
300        let suggestion_regex = Regex::new(r"^\s*(\d+)\s*\|\s*(.*)$").unwrap();
301        let warning_location: Arc<Mutex<Option<CallbackResponse>>> = Arc::new(Mutex::new(None));
302        let pending_diag: Arc<Mutex<Option<CargoDiagnostic>>> = Arc::new(Mutex::new(None));
303        let diagnostic_counts: Arc<Mutex<HashMap<CargoDiagnosticLevel, usize>>> =
304            Arc::new(Mutex::new(HashMap::new()));
305
306        let pending_d = Arc::clone(&pending_diag);
307        let counts = Arc::clone(&diagnostic_counts);
308
309        let diagnostics_arc = Arc::clone(&self.diagnostics);
310        // Callback for Rust panic messages (e.g., "thread 'main' panicked at ...")
311        // To avoid lifetime issues, capture only the data needed by value (clone).
312        let pid_for_panic = self.pid;
313        stderr_dispatcher.add_callback(
314            r"^thread '([^']+)' panicked at (.+):(\d+):(\d+):$",
315            Box::new(
316                move |line, captures, multiline_flag, stats, prior_response| {
317                    multiline_flag.store(false, Ordering::Relaxed);
318
319                    if let Some(caps) = captures {
320                        multiline_flag.store(true, Ordering::Relaxed); // the next line is the panic message
321                        let thread = caps.get(1).map(|m| m.as_str()).unwrap_or("unknown");
322                        let message = caps.get(2).map(|m| m.as_str()).unwrap_or("unknown panic");
323                        let file = caps.get(3).map(|m| m.as_str()).unwrap_or("unknown file");
324                        let line_num = caps
325                            .get(4)
326                            .map(|m| m.as_str())
327                            .unwrap_or("0")
328                            .parse()
329                            .unwrap_or(0);
330                        let col_num = caps
331                            .get(5)
332                            .map(|m| m.as_str())
333                            .unwrap_or("0")
334                            .parse()
335                            .unwrap_or(0);
336                        println!("\n\n\n");
337                        println!("{}", line);
338                        // Use a global TTS instance via OnceCell for program lifetime
339
340                        #[cfg(feature = "uses_tts")]
341                        {
342                            let mut say_something = true;
343                            if let Some(cli) = crate::GLOBAL_CLI.get() {
344                                if cli.no_tts {
345                                    say_something = false;
346                                }
347                            }
348                            if say_something {
349                                let tts_mutex = crate::GLOBAL_TTS.get_or_init(|| {
350                                    std::sync::Mutex::new(
351                                        tts::Tts::default().expect("TTS engine failure"),
352                                    )
353                                });
354                                // Extract the filename without extension
355                                let filename = Path::new(message)
356                                    .file_stem()
357                                    .and_then(|s| s.to_str())
358                                    .unwrap_or("unknown file");
359                                let speech = format!(
360                                    "thread {} panic, {} line {}",
361                                    thread, filename, line_num
362                                );
363                                println!("TTS: {}", speech);
364                                crate::e_runner::wait_for_tts_to_finish(15000);
365                                let mut tts = tts_mutex.lock().expect("Failed to lock TTS mutex");
366                                let _ = tts.speak(&speech, false);
367                                drop(tts);
368                            }
369                        }
370
371                        println!(
372                            "Panic detected: thread='{}', message='{}', file='{}:{}:{}'",
373                            thread, message, file, line_num, col_num
374                        );
375                        println!("\n\n\n");
376                        Some(CallbackResponse {
377                            callback_type: CallbackType::Error,
378                            message: Some(format!(
379                                "thread '{}' panicked at {} ({}:{}:{})",
380                                thread, message, file, line_num, col_num
381                            )),
382                            file: Some(message.to_string()),
383                            line: Some(file.parse::<usize>().unwrap_or(0)),
384                            column: Some(line_num),
385                            suggestion: None,
386                            terminal_status: None,
387                        })
388                    } else {
389                        let context = ThreadLocalContext::get_context();
390                        let mut show_window = true;
391                        let mut say_something = true;
392                        if let Some(cli) = crate::GLOBAL_CLI.get() {
393                            if cli.no_window {
394                                show_window = false;
395                            }
396                            if cli.no_tts {
397                                say_something = false;
398                            }
399                        }
400                        if show_window {
401                            show_graphical_panic(
402                                line.to_string(),
403                                prior_response,
404                                PathBuf::from(&context.manifest_path),
405                                pid_for_panic.unwrap_or_default(),
406                                stats.clone(),
407                            );
408                            println!("[DEBUG] dispatch stats: {:?}", stats);
409                        }
410                        #[cfg(feature = "uses_tts")]
411                        {
412                            if say_something {
413                                let tts_mutex = crate::GLOBAL_TTS.get_or_init(|| {
414                                    std::sync::Mutex::new(
415                                        tts::Tts::default().expect("TTS engine failure"),
416                                    )
417                                });
418
419                                let speech = format!("panic says {}", line);
420                                println!("TTS: {}", speech);
421                                crate::e_runner::wait_for_tts_to_finish(15000);
422                                let mut tts = tts_mutex.lock().expect("Failed to lock TTS mutex");
423                                let _ = tts.speak(&speech, true);
424                            }
425                        }
426
427                        None
428                    }
429                },
430            ),
431        );
432
433        // Add a callback to detect "could not compile" errors
434        stderr_dispatcher.add_callback(
435            r"error: could not compile `(?P<crate_name>.+)` \((?P<due_to>.+)\) due to (?P<error_count>\d+) previous errors; (?P<warning_count>\d+) warnings emitted",
436            Box::new(|line, captures, _state, stats, _prior_response| {
437                println!("{}", line);
438            if let Some(caps) = captures {
439                // Extract dynamic fields from the error message
440                let crate_name = caps.name("crate_name").map(|m| m.as_str()).unwrap_or("unknown");
441                let due_to = caps.name("due_to").map(|m| m.as_str()).unwrap_or("unknown");
442                let error_count: usize = caps
443                .name("error_count")
444                .map(|m| m.as_str().parse().unwrap_or(0))
445                .unwrap_or(0);
446                let warning_count: usize = caps
447                .name("warning_count")
448                .map(|m| m.as_str().parse().unwrap_or(0))
449                .unwrap_or(0);
450
451                // Log the captured information (optional)
452                println!(
453                "Detected compilation failure: crate=`{}`, due_to=`{}`, errors={}, warnings={}",
454                crate_name, due_to, error_count, warning_count
455                );
456
457                // Set `is_could_not_compile` to true in the stats
458                let mut stats = stats.lock().unwrap();
459                stats.is_could_not_compile = true;
460            }
461            None // No callback response needed
462            }),
463        );
464
465        // Clone diagnostics_arc for this closure to avoid move
466        let diagnostics_arc_for_diag = Arc::clone(&diagnostics_arc);
467        stderr_dispatcher.add_callback(
468            r"^(?P<level>\w+)(\[(?P<error_code>E\d+)\])?:\s+(?P<msg>.+)$", // Regex for diagnostic line
469            Box::new(
470                move |_line, caps, _multiline_flag, _stats, _prior_response| {
471                    if let Some(caps) = caps {
472                        let mut counts = counts.lock().unwrap();
473                        // Create a PendingDiag and save the message
474                        let mut pending_diag = pending_d.lock().unwrap();
475                        let mut last_lineref = String::new();
476                        if let Some(existing_diag) = pending_diag.take() {
477                            let mut diags = diagnostics_arc_for_diag.lock().unwrap();
478                            last_lineref = existing_diag.lineref.clone();
479                            diags.push(existing_diag.clone());
480                        }
481                        log::trace!("Diagnostic line: {}", _line);
482                        let level = caps["level"].to_string(); // e.g., "warning", "error"
483                        let message = caps["msg"].to_string();
484                        // If the message contains "generated" followed by one or more digits,
485                        // then ignore this diagnostic by returning None.
486                        let re_generated = regex::Regex::new(r"generated\s+\d+").unwrap();
487                        if re_generated.is_match(&message) {
488                            log::trace!("Skipping generated diagnostic: {}", _line);
489                            return None;
490                        }
491
492                        let error_code = caps.name("error_code").map(|m| m.as_str().to_string());
493                        let diag_level = match level.as_str() {
494                            "error" => CargoDiagnosticLevel::Error,
495                            "warning" => CargoDiagnosticLevel::Warning,
496                            "help" => CargoDiagnosticLevel::Help,
497                            "note" => CargoDiagnosticLevel::Note,
498                            _ => {
499                                println!("Unknown diagnostic level: {}", level);
500                                return None; // Ignore unknown levels
501                            }
502                        };
503                        // Increment the count for this level
504                        *counts.entry(diag_level).or_insert(0) += 1;
505
506                        let current_count = counts.get(&diag_level).unwrap_or(&0);
507                        let diag = CargoDiagnostic {
508                            error_code: error_code.clone(),
509                            lineref: last_lineref.clone(),
510                            level: level.clone(),
511                            message,
512                            suggestion: None,
513                            help: None,
514                            note: None,
515                            uses_color: true,
516                            diag_num_padding: Some(2),
517                            diag_number: Some(*current_count),
518                        };
519
520                        // Save the new diagnostic
521                        *pending_diag = Some(diag);
522
523                        // Track the count of diagnostics for each level
524                        return Some(CallbackResponse {
525                            callback_type: CallbackType::LevelMessage, // Treat subsequent lines as warnings
526                            message: None,
527                            file: None,
528                            line: None,
529                            column: None,
530                            suggestion: None, // This is the suggestion part
531                            terminal_status: None,
532                        });
533                    } else {
534                        println!("No captures found in line: {}", _line);
535                        None
536                    }
537                },
538            ),
539        );
540        // Look-behind buffer for last 6 lines before backtrace
541        let look_behind = Arc::new(Mutex::new(Vec::<String>::new()));
542        {
543            let look_behind = Arc::clone(&look_behind);
544            // This callback runs for every stderr line to update the look-behind buffer
545            stderr_dispatcher.add_callback(
546                r"^(?P<msg>.*)$",
547                Box::new(move |line, _captures, _state, _stats, _prior_response| {
548                    let mut buf = look_behind.lock().unwrap();
549                    if line.trim().is_empty() {
550                        return None;
551                    }
552                    buf.push(line.to_string());
553                    if buf.len() > 6 {
554                        buf.remove(0);
555                    }
556                    None
557                }),
558            );
559        }
560
561        // --- Patch: Use look_behind before backtrace_lines in the note ---
562        {
563            let pending_diag = Arc::clone(&pending_diag);
564            let diagnostics_arc = Arc::clone(&diagnostics_arc);
565            let backtrace_mode = Arc::new(AtomicBool::new(false));
566            let backtrace_lines = Arc::new(Mutex::new(Vec::<String>::new()));
567            let look_behind = Arc::clone(&look_behind);
568            let stored_lines_behind = Arc::new(Mutex::new(Vec::<String>::new()));
569
570            // Enable backtrace mode when we see "stack backtrace:"
571            {
572                let backtrace_mode = Arc::clone(&backtrace_mode);
573                let backtrace_lines = Arc::clone(&backtrace_lines);
574                let stored_lines_behind = Arc::clone(&stored_lines_behind);
575                let look_behind = Arc::clone(&look_behind);
576                stderr_dispatcher.add_callback(
577                    r"stack backtrace:",
578                    Box::new(move |_line, _captures, _state, _stats, _prior_response| {
579                        backtrace_mode.store(true, Ordering::Relaxed);
580                        backtrace_lines.lock().unwrap().clear();
581                        // Save the current look_behind buffer into a shared stored_lines_behind for later use
582                        {
583                            let look_behind_buf = look_behind.lock().unwrap();
584                            let mut stored = stored_lines_behind.lock().unwrap();
585                            *stored = look_behind_buf.clone();
586                        }
587                        None
588                    }),
589                );
590            }
591
592            // Process backtrace lines, filter and summarize
593            {
594                let backtrace_mode = Arc::clone(&backtrace_mode);
595                let backtrace_lines = Arc::clone(&backtrace_lines);
596                let pending_diag = Arc::clone(&pending_diag);
597                let diagnostics_arc = Arc::clone(&diagnostics_arc);
598
599                // Regex for numbered backtrace line: "  0: type::path"
600                let re_number_type = Regex::new(r"^\s*(\d+):\s+(.*)$").unwrap();
601                // Regex for "at path:line"
602                let re_at_path = Regex::new(r"^\s*at\s+([^\s:]+):(\d+)").unwrap();
603
604                stderr_dispatcher.add_callback(
605                    r"^(?P<msg>.*)$",
606                    Box::new(
607                        move |mut line, _captures, _state, _stats, _prior_response| {
608                            if backtrace_mode.load(Ordering::Relaxed) {
609                                line = line.trim();
610                                // End of backtrace if empty line or new diagnostic/note
611                                if line.trim().is_empty()
612                                    || line.starts_with("note:")
613                                    || line.starts_with("error:")
614                                {
615                                    let mut bt_lines = Vec::new();
616                                    let mut skip_next = false;
617                                    let mut last_number_type: Option<(String, String)> = None;
618                                    for l in backtrace_lines.lock().unwrap().iter() {
619                                        if let Some(caps) = re_number_type.captures(l) {
620                                            // Save the (number, type) line, but don't push yet
621                                            last_number_type =
622                                                Some((caps[1].to_string(), caps[2].to_string()));
623                                            skip_next = true;
624                                        } else if skip_next && re_at_path.is_match(l) {
625                                            let path_caps = re_at_path.captures(l).unwrap();
626                                            let path = path_caps.get(1).unwrap().as_str();
627                                            let line_num = path_caps.get(2).unwrap().as_str();
628                                            if path.starts_with("/rustc")
629                                                || path.contains(".cargo")
630                                                || path.contains(".rustup")
631                                            {
632                                                // Skip both the number: type and the at line
633                                                // (do not push either)
634                                            } else {
635                                                // Push both the number: type and the at line, on the same line
636                                                if let Some((num, typ)) = last_number_type.take() {
637                                                    // Canonicalize the path if possible for better readability
638                                                    let path = match std::fs::canonicalize(path) {
639                                                        Ok(canon) => canon.display().to_string(),
640                                                        Err(_) => path.to_string(),
641                                                    };
642
643                                                    bt_lines.push(format!(
644                                                        "{}: {} @ {}:{}",
645                                                        num, typ, path, line_num
646                                                    ));
647                                                }
648                                            }
649                                            skip_next = false;
650                                        } else if let Some((num, typ)) = last_number_type.take() {
651                                            // If the previous number: type was not followed by an at line, push it
652                                            bt_lines.push(format!("{}: {}", num, typ));
653                                            if !l.trim().is_empty() {
654                                                bt_lines.push(l.clone());
655                                            }
656                                            skip_next = false;
657                                        } else if !l.trim().is_empty() {
658                                            bt_lines.push(l.clone());
659                                            skip_next = false;
660                                        }
661                                    }
662                                    if !bt_lines.is_empty() {
663                                        let mut pending_diag = pending_diag.lock().unwrap();
664                                        if let Some(ref mut diag) = *pending_diag {
665                                            // --- Insert stored_lines_behind lines before backtrace_lines ---
666                                            let stored_lines = {
667                                                let buf = stored_lines_behind.lock().unwrap();
668                                                buf.clone()
669                                            };
670                                            let note = diag.note.get_or_insert_with(String::new);
671                                            if !stored_lines.is_empty() {
672                                                note.push_str(&stored_lines.join("\n"));
673                                                note.push('\n');
674                                            }
675                                            note.push_str(&bt_lines.join("\n"));
676                                            let mut diags = diagnostics_arc.lock().unwrap();
677                                            diags.push(diag.clone());
678                                        }
679                                    }
680                                    backtrace_mode.store(false, Ordering::Relaxed);
681                                    backtrace_lines.lock().unwrap().clear();
682                                    return None;
683                                }
684
685                                // Only keep lines that are part of the backtrace
686                                if re_number_type.is_match(line) || re_at_path.is_match(line) {
687                                    backtrace_lines.lock().unwrap().push(line.to_string());
688                                }
689                                // Ignore further lines
690                                return None;
691                            }
692                            None
693                        },
694                    ),
695                );
696            }
697        }
698
699        // suggestion callback
700        {
701            let location_lock_clone = Arc::clone(&warning_location);
702            let suggestion_m = Arc::clone(&suggestion_mode);
703
704            // Suggestion callback that adds subsequent lines as suggestions
705            stderr_dispatcher.add_callback(
706                r"^(?P<msg>.*)$", // Capture all lines following the location
707                Box::new(
708                    move |line, _captures, _multiline_flag, _stats, _prior_response| {
709                        if suggestion_m.load(Ordering::Relaxed) {
710                            // Only process lines that match the suggestion format
711                            if let Some(caps) = suggestion_regex.captures(line.trim()) {
712                                // Capture the line number and code from the suggestion line
713                                // let line_num = caps[1].parse::<usize>().unwrap_or(0);
714                                let code = caps[2].to_string();
715
716                                // Lock the pending_diag to add the suggestion
717                                if let Ok(mut lock) = location_lock_clone.lock() {
718                                    if let Some(mut loc) = lock.take() {
719                                        // let file = loc.file.clone().unwrap_or_default();
720                                        // let col = loc.column.unwrap_or(0);
721
722                                        // Concatenate the suggestion line to the message
723                                        let mut msg = loc.message.unwrap_or_default();
724                                        msg.push_str(&format!("\n{}", code));
725
726                                        // Print the concatenated suggestion for debugging
727                                        // println!("daveSuggestion for {}:{}:{} - {}", file, line_num, col, msg);
728
729                                        // Update the location with the new concatenated message
730                                        loc.message = Some(msg.clone());
731                                        // println!("Updating location lock with new suggestion: {}", msg);
732                                        // Save the updated location back to shared state
733                                        // if let Ok(mut lock) = location_lock_clone.lock() {
734                                        // println!("Updating location lock with new suggestion: {}", msg);
735                                        lock.replace(loc);
736                                        // } else {
737                                        //     eprintln!("Failed to acquire lock for location_lock_clone");
738                                        // }
739                                    }
740                                    // return Some(CallbackResponse {
741                                    //     callback_type: CallbackType::Warning, // Treat subsequent lines as warnings
742                                    //     message: Some(msg.clone()),
743                                    //     file: Some(file),
744                                    //     line: Some(line_num),
745                                    //     column: Some(col),
746                                    //     suggestion: Some(msg),  // This is the suggestion part
747                                    //     terminal_status: None,
748                                    // });
749                                }
750                            }
751                        } else {
752                            // println!("Suggestion mode is not active. Ignoring line: {}", line);
753                        }
754
755                        None
756                    },
757                ),
758            );
759        }
760        {
761            let suggestion_m = Arc::clone(&suggestion_mode);
762            let pending_diag_clone = Arc::clone(&pending_diag);
763            let diagnostics_arc = Arc::clone(&self.diagnostics);
764            // Callback for handling when an empty line or new diagnostic is received
765            stderr_dispatcher.add_callback(
766                r"^\s*$", // Regex to capture empty line
767                Box::new(
768                    move |_line, _captures, _multiline_flag, _stats, _prior_response| {
769                        // println!("Empty line detected: {}", line);
770                        suggestion_m.store(false, Ordering::Relaxed);
771                        // End of current diagnostic: take and process it.
772                        if let Some(pending_diag) = pending_diag_clone.lock().unwrap().take() {
773                            //println!("{:?}", pending_diag);
774                            // Use diagnostics_arc instead of self.diagnostices
775                            let mut diags = diagnostics_arc.lock().unwrap();
776                            diags.push(pending_diag.clone());
777                        } else {
778                            // println!("No pending diagnostic to process.");
779                        }
780                        // Handle empty line scenario to end the current diagnostic processing
781                        // if let Some(pending_diag) = pending_diag_clone.lock().unwrap().take() {
782                        //     println!("{:?}", pending_diag);
783                        //     let mut diags = self.diagnostics.lock().unwrap();
784                        //     diags.push(pending_diag.clone());
785                        //                             // let diag = crate::e_eventdispatcher::convert_message_to_diagnostic(msg, &msg_str);
786                        //                             // diags.push(diag.clone());
787                        //                             // if let Some(ref sd) = stage_disp_clone {
788                        //                             //     sd.dispatch(&format!("Stage: Diagnostic occurred at {:?}", now));
789                        //                             // }
790                        //     // Handle the saved PendingDiag and its CallbackResponse
791                        //     // if let Some(callback_response) = pending_diag.callback_response {
792                        //     //     println!("End of Diagnostic: {:?}", callback_response);
793                        //     // }
794                        // } else {
795                        //     println!("No pending diagnostic to process.");
796                        // }
797
798                        None
799                    },
800                ),
801            );
802        }
803
804        // {
805        //     let pending_diag = Arc::clone(&pending_diag);
806        //     let location_lock = Arc::clone(&warning_location);
807        //     let suggestion_m = Arc::clone(&suggestion_mode);
808
809        // let suggestion_regex = Regex::new(r"^\s*(\d+)\s*\|\s*(.*)$").unwrap();
810
811        //     stderr_dispatcher.add_callback(
812        //     r"^\s*(\d+)\s*\|\s*(.*)$",  // Match suggestion line format
813        //     Box::new(move |line, _captures, _multiline_flag| {
814        //         if suggestion_m.load(Ordering::Relaxed) {
815        //             // Only process lines that match the suggestion format
816        //             if let Some(caps) = suggestion_regex.captures(line.trim()) {
817        //                 // Capture the line number and code from the suggestion line
818        //                 let line_num = caps[1].parse::<usize>().unwrap_or(0);
819        //                 let code = caps[2].to_string();
820
821        //                 // Lock the pending_diag to add the suggestion
822        //                 if let Some(mut loc) = location_lock.lock().unwrap().take() {
823        //                     println!("Suggestion line: {}", line);
824        //                     let file = loc.file.clone().unwrap_or_default();
825        //                     let col = loc.column.unwrap_or(0);
826
827        //                     // Concatenate the suggestion line to the message
828        //                     let mut msg = loc.message.unwrap_or_default();
829        //                     msg.push_str(&format!("\n{} | {}", line_num, code));  // Append the suggestion properly
830
831        //                     // Print the concatenated suggestion for debugging
832        //                     println!("Suggestion for {}:{}:{} - {}", file, line_num, col, msg);
833
834        //                     // Update the location with the new concatenated message
835        //                     loc.message = Some(msg.clone());
836
837        //                     // Save the updated location back to shared state
838        //                     location_lock.lock().unwrap().replace(loc);
839
840        //                     // return Some(CallbackResponse {
841        //                     //     callback_type: CallbackType::Warning, // Treat subsequent lines as warnings
842        //                     //     message: Some(msg.clone()),
843        //                     //     file: Some(file),
844        //                     //     line: Some(line_num),
845        //                     //     column: Some(col),
846        //                     //     suggestion: Some(msg),  // This is the suggestion part
847        //                     //     terminal_status: None,
848        //                     // });
849        //                 } else {
850        //                     println!("No location information available for suggestion line: {}", line);
851        //                 }
852        //             } else {
853        //                 println!("Suggestion line does not match expected format: {}", line);
854        //             }
855        //         } else {
856        //             println!("Suggestion mode is not active. Ignoring line: {}", line);
857        //         }
858
859        //         None
860        //     }),
861        // );
862
863        // }
864
865        {
866            let location_lock = Arc::clone(&warning_location);
867            let pending_diag = Arc::clone(&pending_diag);
868            let suggestion_mode = Arc::clone(&suggestion_mode);
869            stderr_dispatcher.add_callback(
870                r"^(?P<msg>.*)$", // Capture all lines following the location
871                Box::new(
872                    move |line, _captures, _multiline_flag, _stats, _prior_response| {
873                        // Lock the location to fetch the original diagnostic info
874                        if let Ok(location_guard) = location_lock.lock() {
875                            if let Some(loc) = location_guard.as_ref() {
876                                let file = loc.file.clone().unwrap_or_default();
877                                let line_num = loc.line.unwrap_or(0);
878                                let col = loc.column.unwrap_or(0);
879                                // println!("SUGGESTION: Suggestion for {}:{}:{} {}", file, line_num, col, line);
880
881                                // Only treat lines starting with | or numbers as suggestion lines
882                                if line.trim().starts_with('|')
883                                    || line.trim().starts_with(char::is_numeric)
884                                {
885                                    // Get the existing suggestion and append the new line
886                                    let suggestion = line.trim();
887
888                                    // Print the suggestion for debugging
889                                    // println!("Suggestion for {}:{}:{} - {}", file, line_num, col, suggestion);
890
891                                    // Lock the pending_diag and update its callback_response field
892                                    let mut pending_diag = match pending_diag.lock() {
893                                        Ok(lock) => lock,
894                                        Err(e) => {
895                                            eprintln!("Failed to acquire lock: {}", e);
896                                            return None; // Handle the error appropriately
897                                        }
898                                    };
899                                    if let Some(diag) = pending_diag.take() {
900                                        // If a PendingDiag already exists, update the existing callback response with the new suggestion
901                                        let mut diag = diag;
902
903                                        // Append the new suggestion to the existing one
904                                        if let Some(ref mut existing) = diag.suggestion {
905                                            diag.suggestion =
906                                                Some(format!("{}\n{}", existing, suggestion));
907                                        } else {
908                                            diag.suggestion = Some(suggestion.to_string());
909                                        }
910
911                                        // Update the shared state with the new PendingDiag
912                                        *pending_diag = Some(diag.clone());
913                                        return Some(CallbackResponse {
914                                            callback_type: CallbackType::Suggestion, // Treat subsequent lines as warnings
915                                            message: Some(
916                                                diag.clone().suggestion.clone().unwrap().clone(),
917                                            ),
918                                            file: Some(file),
919                                            line: Some(line_num),
920                                            column: Some(col),
921                                            suggestion: diag.clone().suggestion.clone(), // This is the suggestion part
922                                            terminal_status: None,
923                                        });
924                                    } else {
925                                        // println!("No pending diagnostic to process for suggestion line: {}", line);
926                                    }
927                                } else {
928                                    // If the line doesn't match the suggestion format, just return it as is
929                                    if line.trim().is_empty() {
930                                        // Ignore empty lines
931                                        suggestion_mode.store(false, Ordering::Relaxed);
932                                        return None;
933                                    }
934                                }
935                            } else {
936                                // println!("No location information available for suggestion line: {}", line);
937                            }
938                        }
939                        None
940                    },
941                ),
942            );
943        }
944
945        // 2) Location callback stores its response into that shared state
946        {
947            let pending_diag = Arc::clone(&pending_diag);
948            let warning_location = Arc::clone(&warning_location);
949            let location_lock = Arc::clone(&warning_location);
950            let suggestion_mode = Arc::clone(&suggestion_mode);
951            let manifest_path = self.manifest_path.clone();
952            stderr_dispatcher.add_callback(
953                // r"^\s*-->\s+(?P<file>[^:]+):(?P<line>\d+):(?P<col>\d+)$",
954                r"^\s*-->\s+(?P<file>.+?)(?::(?P<line>\d+))?(?::(?P<col>\d+))?\s*$",
955                Box::new(
956                    move |_line, caps, _multiline_flag, _stats, _prior_response| {
957                        log::trace!("Location line: {}", _line);
958                        // if multiline_flag.load(Ordering::Relaxed) {
959                        if let Some(caps) = caps {
960                            let file = caps["file"].to_string();
961                            let resolved_path = resolve_file_path(&manifest_path, &file);
962                            let file = resolved_path.to_str().unwrap_or_default().to_string();
963                            let line = caps["line"].parse::<usize>().unwrap_or(0);
964                            let column = caps["col"].parse::<usize>().unwrap_or(0);
965                            let resp = CallbackResponse {
966                                callback_type: CallbackType::Location,
967                                message: format!("{}:{}:{}", file, line, column).into(),
968                                file: Some(file.clone()),
969                                line: Some(line),
970                                column: Some(column),
971                                suggestion: None,
972                                terminal_status: None,
973                            };
974                            // Lock the pending_diag and update its callback_response field
975                            let mut pending_diag = pending_diag.lock().unwrap();
976                            if let Some(diag) = pending_diag.take() {
977                                // If a PendingDiag already exists, save the new callback response in the existing PendingDiag
978                                let mut diag = diag;
979                                diag.lineref = format!("{}:{}:{}", file, line, column); // Update the lineref
980                                                                                        // diag.save_callback_response(resp.clone()); // Save the callback response
981                                                                                        // Update the shared state with the new PendingDiag
982                                *pending_diag = Some(diag);
983                            }
984                            // Save it for the generic callback to see
985                            *warning_location.lock().unwrap() = Some(resp.clone());
986                            *location_lock.lock().unwrap() = Some(resp.clone());
987                            // Set suggestion mode to true as we've encountered a location line
988                            suggestion_mode.store(true, Ordering::Relaxed);
989                            return Some(resp.clone());
990                        } else {
991                            println!("No captures found in line: {}", _line);
992                        }
993                        // }
994                        None
995                    },
996                ),
997            );
998        }
999
1000        // // 3) Note callback — attach note to pending_diag
1001        {
1002            let pending_diag = Arc::clone(&pending_diag);
1003            stderr_dispatcher.add_callback(
1004                r"^\s*=\s*note:\s*(?P<msg>.+)$",
1005                Box::new(move |_line, caps, _state, _stats, _prior_response| {
1006                    if let Some(caps) = caps {
1007                        let mut pending_diag = pending_diag.lock().unwrap();
1008                        if let Some(ref mut resp) = *pending_diag {
1009                            // Prepare the new note with the blue prefix
1010                            let new_note = format!("note: {}", caps["msg"].to_string());
1011
1012                            // Append or set the note
1013                            if let Some(existing_note) = &resp.note {
1014                                // If there's already a note, append with newline and the new note
1015                                resp.note = Some(format!("{}\n{}", existing_note, new_note));
1016                            } else {
1017                                // If no existing note, just set the new note
1018                                resp.note = Some(new_note);
1019                            }
1020                        }
1021                    }
1022                    None
1023                }),
1024            );
1025        }
1026
1027        // 4) Help callback — attach help to pending_diag
1028        {
1029            let pending_diag = Arc::clone(&pending_diag);
1030            stderr_dispatcher.add_callback(
1031                r"^\s*(?:\=|\|)\s*help:\s*(?P<msg>.+)$", // Regex to match both '=' and '|' before help:
1032                Box::new(move |_line, caps, _state, _stats, _prior_response| {
1033                    if let Some(caps) = caps {
1034                        let mut pending_diag = pending_diag.lock().unwrap();
1035                        if let Some(ref mut resp) = *pending_diag {
1036                            // Create the new help message with the orange "h:" prefix
1037                            let new_help =
1038                                format!("\x1b[38;5;214mhelp: {}\x1b[0m", caps["msg"].to_string());
1039
1040                            // Append or set the help message
1041                            if let Some(existing_help) = &resp.help {
1042                                // If there's already a help message, append with newline
1043                                resp.help = Some(format!("{}\n{}", existing_help, new_help));
1044                            } else {
1045                                // If no existing help message, just set the new one
1046                                resp.help = Some(new_help);
1047                            }
1048                        }
1049                    }
1050                    None
1051                }),
1052            );
1053        }
1054
1055        stderr_dispatcher.add_callback(
1056    r"(?:\x1b\[[0-9;]*[A-Za-z])*\s*Serving(?:\x1b\[[0-9;]*[A-Za-z])*\s+at\s+(http://[^\s]+)",
1057    Box::new(|line, captures, _state, stats , _prior_response| {
1058        if let Some(caps) = captures {
1059            let url = caps.get(1).unwrap().as_str();
1060            let url = url.replace("0.0.0.0", "127.0.0.1");
1061            println!("(STDERR) Captured URL: {}", url);
1062            match open::that_detached(&url) {
1063                Ok(_) => println!("(STDERR) Opened URL: {}",&url),
1064                Err(e) => eprintln!("(STDERR) Failed to open URL: {}. Error: {:?}", url, e),
1065            }
1066             let mut stats = stats.lock().unwrap();
1067             if stats.build_finished_time.is_none() {
1068              let now = SystemTime::now();
1069             stats.build_finished_time = Some(now);
1070             }
1071            Some(CallbackResponse {
1072                callback_type: CallbackType::OpenedUrl, // Choose as appropriate
1073                message: Some(format!("Captured and opened URL: {}", url)),
1074                file: None,
1075                line: None,
1076                column: None,
1077                suggestion: None,
1078                terminal_status: None,
1079            })
1080        } else {
1081            println!("(STDERR) No URL captured in line: {}", line);
1082            None
1083        }
1084    }),
1085);
1086
1087        let finished_flag = Arc::new(AtomicBool::new(false));
1088
1089        // 0) Finished‐profile summary callback
1090        {
1091            let finished_flag = Arc::clone(&finished_flag);
1092            stderr_dispatcher.add_callback(
1093        r"^Finished\s+`(?P<profile>[^`]+)`\s+profile\s+\[(?P<opts>[^\]]+)\]\s+target\(s\)\s+in\s+(?P<dur>[0-9.]+s)$",
1094        Box::new(move |_line, caps, _multiline_flag, stats, _prior_response | {
1095            if let Some(caps) = caps {
1096                finished_flag.store(true, Ordering::Relaxed);
1097                let profile = &caps["profile"];
1098                let opts    = &caps["opts"];
1099                let dur     = &caps["dur"];
1100                             let mut stats = stats.lock().unwrap();
1101             if stats.build_finished_time.is_none() {
1102              let now = SystemTime::now();
1103             stats.build_finished_time = Some(now);
1104             }
1105                Some(CallbackResponse {
1106                    callback_type: CallbackType::Note,
1107                    message: Some(format!("Finished `{}` [{}] in {}", profile, opts, dur)),
1108                    file: None, line: None, column: None, suggestion: None, terminal_status: None,
1109                })
1110            } else {
1111                None
1112            }
1113        }),
1114    );
1115        }
1116
1117        let summary_flag = Arc::new(AtomicBool::new(false));
1118        {
1119            let summary_flag = Arc::clone(&summary_flag);
1120            stderr_dispatcher.add_callback(
1121    r"^(?P<level>warning|error):\s+`(?P<name>[^`]+)`\s+\((?P<otype>lib|bin)\)\s+generated\s+(?P<count>\d+)\s+(?P<kind>warnings|errors).*run\s+`(?P<cmd>[^`]+)`\s+to apply\s+(?P<fixes>\d+)\s+suggestions",
1122    Box::new(move |_line, caps, multiline_flag, _stats, _prior_response | {
1123        let summary_flag = Arc::clone(&summary_flag);
1124        if let Some(caps) = caps {
1125            summary_flag.store(true, Ordering::Relaxed);
1126            // Always start fresh
1127            multiline_flag.store(false, Ordering::Relaxed);
1128
1129            let level    = &caps["level"];
1130            let name     = &caps["name"];
1131            let otype    = &caps["otype"];
1132            let count: usize = caps["count"].parse().unwrap_or(0);
1133            let kind     = &caps["kind"];   // "warnings" or "errors"
1134            let cmd      = caps["cmd"].to_string();
1135            let fixes: usize = caps["fixes"].parse().unwrap_or(0);
1136
1137            println!("SUMMARIZATION CALLBACK {}",
1138                    &format!("{}: `{}` ({}) generated {} {}; run `{}` to apply {} fixes",
1139                    level, name, otype, count, kind, cmd, fixes));
1140            Some(CallbackResponse {
1141                callback_type: CallbackType::Note,  // treat as informational
1142                message: Some(format!(
1143                    "{}: `{}` ({}) generated {} {}; run `{}` to apply {} fixes",
1144                    level, name, otype, count, kind, cmd, fixes
1145                )),
1146                file: None,
1147                line: None,
1148                column: None,
1149                suggestion: Some(cmd),
1150                terminal_status: None,
1151            })
1152        } else {
1153            None
1154        }
1155    }),
1156    );
1157        }
1158
1159        // {
1160        //     let summary_flag = Arc::clone(&summary_flag);
1161        //     let finished_flag = Arc::clone(&finished_flag);
1162        //     let warning_location = Arc::clone(&warning_location);
1163        //     // Warning callback for stdout.
1164        //     stderr_dispatcher.add_callback(
1165        //         r"^warning:\s+(?P<msg>.+)$",
1166        //         Box::new(
1167        //             move |line: &str, captures: Option<regex::Captures>, multiline_flag: Arc<AtomicBool>| {
1168        //                             // If summary or finished just matched, skip
1169        //             if summary_flag.swap(false, Ordering::Relaxed)
1170        //                 || finished_flag.swap(false, Ordering::Relaxed)
1171        //             {
1172        //                 return None;
1173        //             }
1174
1175        //         // 2) If this line *matches* the warning regex, handle as a new warning
1176        //         if let Some(caps) = captures {
1177        //             let msg = caps.name("msg").unwrap().as_str().to_string();
1178        //                    // 1) If a location was saved, print file:line:col – msg
1179        //             // println!("*WARNING detected: {:?}", msg);
1180        //                 multiline_flag.store(true, Ordering::Relaxed);
1181        //         if let Some(loc) = warning_location.lock().unwrap().take() {
1182        //                 let file = loc.file.unwrap_or_default();
1183        //                 let line_num = loc.line.unwrap_or(0);
1184        //                 let col  = loc.column.unwrap_or(0);
1185        //                 println!("{}:{}:{} - {}", file, line_num, col, msg);
1186        //                 return Some(CallbackResponse {
1187        //                     callback_type: CallbackType::Warning,
1188        //                     message: Some(msg.to_string()),
1189        //                     file: None, line: None, column: None, suggestion: None, terminal_status: None,
1190        //                 });
1191        //         }
1192        //             return Some(CallbackResponse {
1193        //                 callback_type: CallbackType::Warning,
1194        //                 message: Some(msg),
1195        //                 file: None,
1196        //                 line: None,
1197        //                 column: None,
1198        //                 suggestion: None,
1199        //                 terminal_status: None,
1200        //             });
1201        //         }
1202
1203        //                 // 3) Otherwise, if we’re in multiline mode, treat as continuation
1204        //         if multiline_flag.load(Ordering::Relaxed) {
1205        //             let text = line.trim();
1206        //             if text.is_empty() {
1207        //                 multiline_flag.store(false, Ordering::Relaxed);
1208        //                 return None;
1209        //             }
1210        //             // println!("   - {:?}", text);
1211        //             return Some(CallbackResponse {
1212        //                 callback_type: CallbackType::Warning,
1213        //                 message: Some(text.to_string()),
1214        //                 file: None,
1215        //                 line: None,
1216        //                 column: None,
1217        //                 suggestion: None,
1218        //                 terminal_status: None,
1219        //             });
1220        //         }
1221        //                     None
1222        //             },
1223        //         ),
1224        //     );
1225        // }
1226
1227        stderr_dispatcher.add_callback(
1228            r"IO\(Custom \{ kind: NotConnected",
1229            Box::new(move |line, _captures, _state, _stats, _prior_response| {
1230                println!("(STDERR) Terminal error detected: {:?}", &line);
1231                let result = if line.contains("NotConnected") {
1232                    TerminalError::NoTerminal
1233                } else {
1234                    TerminalError::NoError
1235                };
1236                let sender = sender.lock().unwrap();
1237                sender.send(result).ok();
1238                Some(CallbackResponse {
1239                    callback_type: CallbackType::Warning, // Choose as appropriate
1240                    message: Some(format!("Terminal Error: {}", line)),
1241                    file: None,
1242                    line: None,
1243                    column: None,
1244                    suggestion: None,
1245                    terminal_status: None,
1246                })
1247            }),
1248        );
1249        stderr_dispatcher.add_callback(
1250            r".*",
1251            Box::new(|line, _captures, _state, _stats, _prior_response| {
1252                log::trace!("stdraw[{:?}]", line);
1253                None // We're just printing, so no callback response is needed.
1254            }),
1255        );
1256        // need to implement autosense/filtering for tool installers; TBD
1257        // stderr_dispatcher.add_callback(
1258        //     r"Command 'perl' not found\. Is perl installed\?",
1259        //     Box::new(|line, _captures, _state, stats| {
1260        //     println!("cargo e sees a perl issue; maybe a prompt in the future or auto-resolution.");
1261        //     crate::e_autosense::auto_sense_perl();
1262        //     None
1263        //     }),
1264        // );
1265        // need to implement autosense/filtering for tool installers; TBD
1266        // stderr_dispatcher.add_callback(
1267        //     r"Error configuring OpenSSL build:\s+Command 'perl' not found\. Is perl installed\?",
1268        //     Box::new(|line, _captures, _state, stats| {
1269        //     println!("Detected OpenSSL build error due to missing 'perl'. Attempting auto-resolution.");
1270        //     crate::e_autosense::auto_sense_perl();
1271        //     None
1272        //     }),
1273        // );
1274        self.stderr_dispatcher = Some(Arc::new(stderr_dispatcher));
1275
1276        // let mut progress_dispatcher = EventDispatcher::new();
1277        // progress_dispatcher.add_callback(r"Progress", Box::new(|line, _captures,_state| {
1278        //     println!("(Progress) {}", line);
1279        //     None
1280        // }));
1281        // self.progress_dispatcher = Some(Arc::new(progress_dispatcher));
1282
1283        // let mut stage_dispatcher = EventDispatcher::new();
1284        // stage_dispatcher.add_callback(r"Stage:", Box::new(|line, _captures, _state| {
1285        //     println!("(Stage) {}", line);
1286        //     None
1287        // }));
1288        // self.stage_dispatcher = Some(Arc::new(stage_dispatcher));
1289    }
1290
1291    pub fn run<F>(self: Arc<Self>, on_spawn: F) -> anyhow::Result<u32>
1292    where
1293        F: FnOnce(u32, Arc<Mutex<CargoProcessHandle>>),
1294    {
1295        if !self.is_filter {
1296            return self.switch_to_passthrough_mode(on_spawn);
1297        }
1298
1299        let mut command = self.build_command();
1300        let mut cargo_process_handle = command.spawn_cargo_capture(
1301            self.clone(),
1302            self.stdout_dispatcher.clone(),
1303            self.stderr_dispatcher.clone(),
1304            self.progress_dispatcher.clone(),
1305            self.stage_dispatcher.clone(),
1306            None,
1307        );
1308        cargo_process_handle.diagnostics = Arc::clone(&self.diagnostics);
1309        let pid = cargo_process_handle.pid;
1310
1311        // Wrap the handle in Arc<Mutex<>> for thread-safe sharing
1312        let cargo_process_handle = Arc::new(Mutex::new(cargo_process_handle));
1313
1314        // Notify observer
1315        on_spawn(pid, cargo_process_handle.clone());
1316
1317        if self.detached {
1318            let timeout = std::time::Duration::from_secs(self.time_limit.unwrap_or(0) as u64);
1319            let (tx, rx) = std::sync::mpsc::channel();
1320            let cargo_process_handle_clone = Arc::clone(&cargo_process_handle);
1321            let handle = std::thread::spawn(move || {
1322                let result = cargo_process_handle_clone.lock().unwrap().child.wait();
1323                let _ = tx.send(result);
1324            });
1325            // Wait for the thread to finish to ensure proper cleanup
1326            let _ = handle.join();
1327
1328            match rx.recv_timeout(timeout) {
1329                Ok(result) => { result?; },
1330                Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
1331                    eprintln!("Timeout reached for process with PID: {}", pid);
1332                    let _ = cargo_process_handle.lock().unwrap().kill();
1333                    return Err(anyhow::anyhow!("Timeout reached for process with PID: {}", pid));
1334                }
1335                Err(e) => {
1336                    return Err(anyhow::anyhow!("Thread join error: {}", e));
1337                }
1338            }
1339        }
1340
1341        Ok(pid)
1342    }
1343
1344    // pub fn run(self: Arc<Self>) -> anyhow::Result<u32> {
1345    //     // Build the command using the builder's configuration
1346    //     let mut command = self.build_command();
1347
1348    //     // Spawn the cargo process handle
1349    //     let cargo_process_handle = command.spawn_cargo_capture(
1350    //         self.stdout_dispatcher.clone(),
1351    //         self.stderr_dispatcher.clone(),
1352    //         self.progress_dispatcher.clone(),
1353    //         self.stage_dispatcher.clone(),
1354    //         None,
1355    //     );
1356    // let pid = cargo_process_handle.pid;
1357    // let mut global = GLOBAL_CHILDREN.lock().unwrap();
1358    // global.insert(pid, Arc::new(Mutex::new(cargo_process_handle)));
1359    //     Ok(pid)
1360    // }
1361
1362    pub fn wait(self: Arc<Self>, pid: Option<u32>) -> anyhow::Result<CargoProcessResult> {
1363        let mut global = GLOBAL_CHILDREN.lock().unwrap();
1364        if let Some(pid) = pid {
1365            // Lock the global list of processes and attempt to find the cargo process handle directly by pid
1366            if let Some(cargo_process_handle) = global.get_mut(&pid) {
1367                let mut cargo_process_handle = cargo_process_handle.lock().unwrap();
1368
1369                // Wait for the process to finish and retrieve the result
1370                // println!("Waiting for process with PID: {}", pid);
1371                // let result = cargo_process_handle.wait();
1372                // println!("Process with PID {} finished", pid);
1373                loop {
1374                    println!("Waiting for process with PID: {}", pid);
1375
1376                    // Attempt to wait for the process, but don't block indefinitely
1377                    let status = cargo_process_handle.child.try_wait()?;
1378
1379                    // If the status is `Some(status)`, the process has finished
1380                    if let Some(status) = status {
1381                        if status.code() == Some(101) {
1382                            println!("Process with PID {} finished with cargo error", pid);
1383                        }
1384
1385                        // Check the terminal error flag and update the result if there is an error
1386                        if *cargo_process_handle.terminal_error_flag.lock().unwrap()
1387                            != TerminalError::NoError
1388                        {
1389                            let terminal_error =
1390                                *cargo_process_handle.terminal_error_flag.lock().unwrap();
1391                            cargo_process_handle.result.terminal_error = Some(terminal_error);
1392                        }
1393
1394                        let final_diagnostics = {
1395                            let diag_lock = self.diagnostics.lock().unwrap();
1396                            diag_lock.clone()
1397                        };
1398                        cargo_process_handle.result.diagnostics = final_diagnostics.clone();
1399                        cargo_process_handle.result.exit_status = Some(status);
1400                        cargo_process_handle.result.end_time = Some(SystemTime::now());
1401                        let stats_clone = {
1402                            let stats = cargo_process_handle.stats.lock().unwrap();
1403                            stats.clone()
1404                        };
1405                        cargo_process_handle.result.stats = stats_clone;
1406                        cargo_process_handle.result.elapsed_time = Some(
1407                            cargo_process_handle
1408                                .result
1409                                .end_time
1410                                .unwrap()
1411                                .duration_since(cargo_process_handle.result.start_time.unwrap())
1412                                .unwrap(),
1413                        );
1414                        println!(
1415                            "Process with PID {} finished {:?} {}",
1416                            pid,
1417                            status,
1418                            final_diagnostics.len()
1419                        );
1420                        return Ok(cargo_process_handle.result.clone());
1421                        // return Ok(CargoProcessResult { exit_status: status, ..Default::default() });
1422                    }
1423
1424                    // Sleep briefly to yield control back to the system and avoid blocking
1425                    std::thread::sleep(std::time::Duration::from_secs(1));
1426                }
1427
1428                // Return the result
1429                // match result {
1430                //     Ok(res) => Ok(res),
1431                //     Err(e) => Err(anyhow::anyhow!("Failed to wait for cargo process: {}", e).into()),
1432                // }
1433            } else {
1434                Err(anyhow::anyhow!(
1435                    "Process handle with PID {} not found in GLOBAL_CHILDREN",
1436                    pid
1437                )
1438                .into())
1439            }
1440        } else {
1441            Err(anyhow::anyhow!("No PID provided for waiting on cargo process").into())
1442        }
1443    }
1444
1445    // pub fn run_wait(self: Arc<Self>) -> anyhow::Result<CargoProcessResult> {
1446    //     // Run the cargo command and get the process handle (non-blocking)
1447    //     let pid = self.clone().run()?; // adds to global list of processes
1448    //     let result = self.wait(Some(pid)); // Wait for the process to finish
1449    //     // Remove the completed process from GLOBAL_CHILDREN
1450    //     let mut global = GLOBAL_CHILDREN.lock().unwrap();
1451    //     global.remove(&pid);
1452
1453    //     result
1454    // }
1455
1456    // Runs the cargo command using the builder's configuration.
1457    // pub fn run(&self) -> anyhow::Result<CargoProcessResult> {
1458    //     // Build the command using the builder's configuration
1459    //     let mut command = self.build_command();
1460
1461    //     // Now use the `spawn_cargo_capture` extension to run the command
1462    //     let mut cargo_process_handle = command.spawn_cargo_capture(
1463    //         self.stdout_dispatcher.clone(),
1464    //         self.stderr_dispatcher.clone(),
1465    //         self.progress_dispatcher.clone(),
1466    //         self.stage_dispatcher.clone(),
1467    //         None,
1468    //     );
1469
1470    //     // Wait for the process to finish and retrieve the results
1471    //     cargo_process_handle.wait().context("Failed to execute cargo process")
1472    // }
1473
1474    /// Configure the command based on the target kind.
1475    pub fn with_target(mut self, target: &CargoTarget) -> Self {
1476        if !self.be_silent {
1477            if let Some(origin) = target.origin.clone() {
1478                println!("\nTarget origin: {:?}", origin);
1479            } else {
1480                println!("\nTarget origin is not set");
1481            }
1482        }
1483        match target.kind {
1484            TargetKind::Unknown | TargetKind::Plugin => {
1485                return self;
1486            }
1487            TargetKind::Bench => {
1488                // // To run benchmarks, use the "bench" command.
1489                //  let exe_path = match which("bench") {
1490                //     Ok(path) => path,
1491                //     Err(err) => {
1492                //         eprintln!("Error: 'trunk' not found in PATH: {}", err);
1493                //         return self;
1494                //     }
1495                // };
1496                // self.alternate_cmd = Some("bench".to_string())
1497                self.args.push("bench".into());
1498                self.args.push(target.name.clone());
1499            }
1500            TargetKind::Test => {
1501                self.args.push("test".into());
1502                // Pass the target's name as a filter to run specific tests.
1503                self.args.push(target.name.clone());
1504            }
1505            TargetKind::UnknownExample
1506            | TargetKind::UnknownExtendedExample
1507            | TargetKind::Example
1508            | TargetKind::ExtendedExample => {
1509                self.args.push(self.subcommand.clone());
1510                // Set execution_dir to the parent of the manifest path
1511                self.execution_dir = target.manifest_path.parent().map(|p| p.to_path_buf());
1512                //self.args.push("--message-format=json".into());
1513                self.args.push("--example".into());
1514                self.args.push(target.name.clone());
1515                // self.args.push("--manifest-path".into());
1516                // self.args.push(
1517                //     target
1518                //         .manifest_path
1519                //         .clone()
1520                //         .to_str()
1521                //         .unwrap_or_default()
1522                //         .to_owned(),
1523                // );
1524                self = self.with_required_features(&target.manifest_path, target);
1525            }
1526            TargetKind::UnknownBinary
1527            | TargetKind::UnknownExtendedBinary
1528            | TargetKind::Binary
1529            | TargetKind::ExtendedBinary => {
1530                // Set execution_dir to the parent of the manifest path
1531                self.execution_dir = target.manifest_path.parent().map(|p| p.to_path_buf());
1532                self.args.push(self.subcommand.clone());
1533                self.args.push("--bin".into());
1534                self.args.push(target.name.clone());
1535                // self.args.push("--manifest-path".into());
1536                // self.args.push(
1537                //     target
1538                //         .manifest_path
1539                //         .clone()
1540                //         .to_str()
1541                //         .unwrap_or_default()
1542                //         .to_owned(),
1543                // );
1544                self = self.with_required_features(&target.manifest_path, target);
1545            }
1546            TargetKind::Manifest => {
1547                self.suppressed_flags.insert("quiet".to_string());
1548                self.args.push(self.subcommand.clone());
1549                self.args.push("--manifest-path".into());
1550                self.args.push(
1551                    target
1552                        .manifest_path
1553                        .clone()
1554                        .to_str()
1555                        .unwrap_or_default()
1556                        .to_owned(),
1557                );
1558            }
1559            TargetKind::ManifestTauriExample => {
1560                self.suppressed_flags.insert("quiet".to_string());
1561                self.args.push(self.subcommand.clone());
1562                self.args.push("--example".into());
1563                self.args.push(target.name.clone());
1564                self.args.push("--manifest-path".into());
1565                self.args.push(
1566                    target
1567                        .manifest_path
1568                        .clone()
1569                        .to_str()
1570                        .unwrap_or_default()
1571                        .to_owned(),
1572                );
1573                self = self.with_required_features(&target.manifest_path, target);
1574            }
1575            TargetKind::ScriptScriptisto => {
1576                let exe_path = match which("scriptisto") {
1577                    Ok(path) => path,
1578                    Err(err) => {
1579                        eprintln!("Error: 'scriptisto' not found in PATH: {}", err);
1580                        return self;
1581                    }
1582                };
1583                self.alternate_cmd = Some(exe_path.as_os_str().to_string_lossy().to_string());
1584                let candidate_opt = match &target.origin {
1585                    Some(TargetOrigin::SingleFile(path))
1586                    | Some(TargetOrigin::DefaultBinary(path)) => Some(path),
1587                    _ => None,
1588                };
1589                if let Some(candidate) = candidate_opt {
1590                    self.alternate_cmd = Some(exe_path.as_os_str().to_string_lossy().to_string());
1591                    self.args.push(candidate.to_string_lossy().to_string());
1592                } else {
1593                    println!("No scriptisto origin found for: {:?}", target);
1594                }
1595            }
1596            TargetKind::ScriptRustScript => {
1597                let exe_path = match crate::e_installer::ensure_rust_script() {
1598                    Ok(p) => p,
1599                    Err(e) => {
1600                        eprintln!("{}", e);
1601                        return self;
1602                    }
1603                };
1604                let candidate_opt = match &target.origin {
1605                    Some(TargetOrigin::SingleFile(path))
1606                    | Some(TargetOrigin::DefaultBinary(path)) => Some(path),
1607                    _ => None,
1608                };
1609                if let Some(candidate) = candidate_opt {
1610                    self.alternate_cmd = Some(exe_path.as_os_str().to_string_lossy().to_string());
1611                    if self.is_filter {
1612                        self.args.push("-c".into()); // ask for cargo output
1613                    }
1614                    self.args.push(candidate.to_string_lossy().to_string());
1615                } else {
1616                    println!("No rust-script origin found for: {:?}", target);
1617                }
1618            }
1619            TargetKind::ManifestTauri => {
1620                // Only locate the Cargo.toml if self.manifest_path is empty
1621                let manifest_path = if self.manifest_path.as_os_str().is_empty() {
1622                    crate::locate_manifest(true).unwrap_or_else(|_| {
1623                        eprintln!("Error: Unable to locate Cargo.toml file.");
1624                        std::process::exit(1);
1625                    })
1626                } else {
1627                    self.manifest_path.clone().display().to_string()
1628                };
1629
1630                // Now, get the workspace parent from the manifest directory
1631                let manifest_dir = Path::new(&manifest_path)
1632                    .parent()
1633                    .unwrap_or(Path::new(".."));
1634
1635                // Ensure npm dependencies are handled at the workspace parent level
1636                let pnpm = crate::e_installer::check_pnpm_and_install(manifest_dir, self.be_silent)
1637                    .unwrap_or_else(|_| {
1638                        eprintln!("Error: Unable to check pnpm dependencies.");
1639                        PathBuf::new()
1640                    });
1641                if pnpm == PathBuf::new() {
1642                    crate::e_installer::check_npm_and_install(manifest_dir, self.be_silent)
1643                        .unwrap_or_else(|_| {
1644                            eprintln!("Error: Unable to check npm dependencies.");
1645                        });
1646                }
1647
1648                self.suppressed_flags.insert("quiet".to_string());
1649                // Helper closure to check for tauri.conf.json in a directory.
1650                let has_tauri_conf = |dir: &Path| -> bool { dir.join("tauri.conf.json").exists() };
1651
1652                // Helper closure to check for tauri.conf.json and package.json in a directory.
1653                let has_file = |dir: &Path, filename: &str| -> bool { dir.join(filename).exists() };
1654                // Try candidate's parent (if origin is SingleFile or DefaultBinary).
1655                let candidate_dir_opt = match &target.origin {
1656                    Some(TargetOrigin::SingleFile(path))
1657                    | Some(TargetOrigin::DefaultBinary(path)) => path.parent(),
1658                    _ => None,
1659                };
1660
1661                if let Some(candidate_dir) = candidate_dir_opt {
1662                    if has_tauri_conf(candidate_dir) {
1663                        if !self.be_silent {
1664                            println!("Using candidate directory: {}", candidate_dir.display());
1665                        }
1666                        self.execution_dir = Some(candidate_dir.to_path_buf());
1667                    } else if let Some(manifest_parent) = target.manifest_path.parent() {
1668                        if has_tauri_conf(manifest_parent) {
1669                            if !self.be_silent {
1670                                println!("Using manifest parent: {}", manifest_parent.display());
1671                            }
1672                            self.execution_dir = Some(manifest_parent.to_path_buf());
1673                        } else if let Some(grandparent) = manifest_parent.parent() {
1674                            if has_tauri_conf(grandparent) {
1675                                if !self.be_silent {
1676                                    println!(
1677                                        "Using manifest grandparent: {}",
1678                                        grandparent.display()
1679                                    );
1680                                }
1681                                self.execution_dir = Some(grandparent.to_path_buf());
1682                            } else {
1683                                if !self.be_silent {
1684                                    println!("No tauri.conf.json found in candidate, manifest parent, or grandparent; defaulting to manifest parent: {}", manifest_parent.display());
1685                                }
1686                                self.execution_dir = Some(manifest_parent.to_path_buf());
1687                            }
1688                        } else {
1689                            if !self.be_silent {
1690                                println!("No grandparent for manifest; defaulting to candidate directory: {}", candidate_dir.display());
1691                            }
1692                            self.execution_dir = Some(candidate_dir.to_path_buf());
1693                        }
1694                    } else {
1695                        if !self.be_silent {
1696                            println!(
1697                                "No manifest parent found for: {}",
1698                                target.manifest_path.display()
1699                            );
1700                        }
1701                    }
1702                    // Check for package.json and run npm ls if found.
1703                    if !self.be_silent {
1704                        println!("Checking for package.json in: {}", candidate_dir.display());
1705                    }
1706                    if has_file(candidate_dir, "package.json") {
1707                        crate::e_installer::check_npm_and_install(candidate_dir, self.be_silent)
1708                            .ok();
1709                    }
1710                } else if let Some(manifest_parent) = target.manifest_path.parent() {
1711                    if has_tauri_conf(manifest_parent) {
1712                        if !self.be_silent {
1713                            println!("Using manifest parent: {}", manifest_parent.display());
1714                        }
1715                        self.execution_dir = Some(manifest_parent.to_path_buf());
1716                    } else if let Some(grandparent) = manifest_parent.parent() {
1717                        if has_tauri_conf(grandparent) {
1718                            if !self.be_silent {
1719                                println!("Using manifest grandparent: {}", grandparent.display());
1720                            }
1721                            self.execution_dir = Some(grandparent.to_path_buf());
1722                        } else {
1723                            if !self.be_silent {
1724                                println!(
1725                                    "No tauri.conf.json found; defaulting to manifest parent: {}",
1726                                    manifest_parent.display()
1727                                );
1728                            }
1729                            self.execution_dir = Some(manifest_parent.to_path_buf());
1730                        }
1731                    }
1732                    // Check for package.json and run npm ls if found.
1733                    if !self.be_silent {
1734                        println!(
1735                            "Checking for package.json in: {}",
1736                            manifest_parent.display()
1737                        );
1738                    }
1739                    if has_file(manifest_parent, "package.json") {
1740                        crate::e_installer::check_npm_and_install(manifest_parent, self.be_silent)
1741                            .ok();
1742                    }
1743                    if has_file(Path::new("."), "package.json") {
1744                        crate::e_installer::check_npm_and_install(manifest_parent, self.be_silent)
1745                            .ok();
1746                    }
1747                } else {
1748                    if !self.be_silent {
1749                        println!(
1750                            "No manifest parent found for: {}",
1751                            target.manifest_path.display()
1752                        );
1753                    }
1754                }
1755                self.args.push("tauri".into());
1756                self.args.push("dev".into());
1757            }
1758            TargetKind::ManifestLeptos => {
1759                let readme_path = target
1760                    .manifest_path
1761                    .parent()
1762                    .map(|p| p.join("README.md"))
1763                    .filter(|p| p.exists())
1764                    .or_else(|| {
1765                        target
1766                            .manifest_path
1767                            .parent()
1768                            .map(|p| p.join("readme.md"))
1769                            .filter(|p| p.exists())
1770                    });
1771
1772                if let Some(readme) = readme_path {
1773                    if let Ok(mut file) = std::fs::File::open(&readme) {
1774                        let mut contents = String::new();
1775                        if file.read_to_string(&mut contents).is_ok()
1776                            && contents.contains("cargo leptos watch")
1777                        {
1778                            // Use cargo leptos watch
1779                            if !self.be_silent {
1780                                println!("Detected 'cargo leptos watch' in {}", readme.display());
1781                            }
1782                            crate::e_installer::ensure_leptos().unwrap_or_else(|_| {
1783                                eprintln!("Error: Unable to ensure leptos installation.");
1784                                PathBuf::new() // Return an empty PathBuf as a fallback
1785                            });
1786                            self.execution_dir =
1787                                target.manifest_path.parent().map(|p| p.to_path_buf());
1788
1789                            self.alternate_cmd = Some("cargo".to_string());
1790                            self.args.push("leptos".into());
1791                            self.args.push("watch".into());
1792                            self = self.with_required_features(&target.manifest_path, target);
1793                            if let Some(exec_dir) = &self.execution_dir {
1794                                if exec_dir.join("package.json").exists() {
1795                                    if !self.be_silent {
1796                                        println!(
1797                                            "Found package.json in execution directory: {}",
1798                                            exec_dir.display()
1799                                        );
1800                                    }
1801                                    crate::e_installer::check_npm_and_install(
1802                                        exec_dir,
1803                                        self.be_silent,
1804                                    )
1805                                    .ok();
1806                                }
1807                            }
1808                            return self;
1809                        }
1810                    }
1811                }
1812
1813                // fallback to trunk
1814                let exe_path = match crate::e_installer::ensure_trunk() {
1815                    Ok(p) => p,
1816                    Err(e) => {
1817                        eprintln!("{}", e);
1818                        return self;
1819                    }
1820                };
1821
1822                if !self.be_silent {
1823                    if let Some(manifest_parent) = target.manifest_path.parent() {
1824                        println!("Manifest path: {}", target.manifest_path.display());
1825                        println!(
1826                            "Execution directory (same as manifest folder): {}",
1827                            manifest_parent.display()
1828                        );
1829                        self.execution_dir = Some(manifest_parent.to_path_buf());
1830                    } else {
1831                        println!(
1832                            "No manifest parent found for: {}",
1833                            target.manifest_path.display()
1834                        );
1835                    }
1836                }
1837                if let Some(exec_dir) = &self.execution_dir {
1838                    if exec_dir.join("package.json").exists() {
1839                        if !self.be_silent {
1840                            println!(
1841                                "Found package.json in execution directory: {}",
1842                                exec_dir.display()
1843                            );
1844                        }
1845                        crate::e_installer::check_npm_and_install(exec_dir, self.be_silent).ok();
1846                    }
1847                }
1848                self.alternate_cmd = Some(exe_path.as_os_str().to_string_lossy().to_string());
1849                self.args.push("serve".into());
1850                self.args.push("--open".into());
1851                self.args.push("--color".into());
1852                self.args.push("always".into());
1853                self = self.with_required_features(&target.manifest_path, target);
1854            }
1855            TargetKind::ManifestDioxus => {
1856                // For Dioxus targets, print the manifest path and set the execution directory
1857                let exe_path = match crate::e_installer::ensure_dx() {
1858                    Ok(path) => path,
1859                    Err(e) => {
1860                        eprintln!("Error locating `dx`: {}", e);
1861                        return self;
1862                    }
1863                };
1864                // to be the same directory as the manifest.
1865                if !self.be_silent {
1866                    if let Some(manifest_parent) = target.manifest_path.parent() {
1867                        println!("Manifest path: {}", target.manifest_path.display());
1868                        println!(
1869                            "Execution directory (same as manifest folder): {}",
1870                            manifest_parent.display()
1871                        );
1872                        self.execution_dir = Some(manifest_parent.to_path_buf());
1873                    } else {
1874                        println!(
1875                            "No manifest parent found for: {}",
1876                            target.manifest_path.display()
1877                        );
1878                    }
1879                }
1880                self.alternate_cmd = Some(exe_path.as_os_str().to_string_lossy().to_string());
1881                self.args.push("serve".into());
1882                self = self.with_required_features(&target.manifest_path, target);
1883            }
1884            TargetKind::ManifestDioxusExample => {
1885                let exe_path = match crate::e_installer::ensure_dx() {
1886                    Ok(path) => path,
1887                    Err(e) => {
1888                        eprintln!("Error locating `dx`: {}", e);
1889                        return self;
1890                    }
1891                };
1892                // For Dioxus targets, print the manifest path and set the execution directory
1893                // to be the same directory as the manifest.
1894                if !self.be_silent {
1895                    if let Some(manifest_parent) = target.manifest_path.parent() {
1896                        println!("Manifest path: {}", target.manifest_path.display());
1897                        println!(
1898                            "Execution directory (same as manifest folder): {}",
1899                            manifest_parent.display()
1900                        );
1901                        self.execution_dir = Some(manifest_parent.to_path_buf());
1902                    } else {
1903                        println!(
1904                            "No manifest parent found for: {}",
1905                            target.manifest_path.display()
1906                        );
1907                    }
1908                }
1909                self.alternate_cmd = Some(exe_path.as_os_str().to_string_lossy().to_string());
1910                self.args.push("serve".into());
1911                self.args.push("--example".into());
1912                self.args.push(target.name.clone());
1913                self = self.with_required_features(&target.manifest_path, target);
1914            }
1915        }
1916        self
1917    }
1918
1919    /// Configure the command using CLI options.
1920    pub fn with_cli(mut self, cli: &crate::Cli) -> Self {
1921        if cli.quiet && !self.suppressed_flags.contains("quiet") {
1922            // Insert --quiet right after "run" if present.
1923            if let Some(pos) = self.args.iter().position(|arg| arg == &self.subcommand) {
1924                self.args.insert(pos + 1, "--quiet".into());
1925            } else {
1926                self.args.push("--quiet".into());
1927            }
1928        }
1929        if cli.release {
1930            // Insert --release right after the initial "run" command if applicable.
1931            // For example, if the command already contains "run", insert "--release" after it.
1932            if let Some(pos) = self.args.iter().position(|arg| arg == &self.subcommand) {
1933                self.args.insert(pos + 1, "--release".into());
1934            } else {
1935                // If not running a "run" command (like in the Tauri case), simply push it.
1936                self.args.push("--release".into());
1937            }
1938        }
1939        if cli.detached_hold.is_some() {
1940            self.detached_hold = cli.detached_hold;
1941        }
1942        if cli.detached_delay.is_some() {
1943            self.detached_delay = cli.detached_delay;
1944        }
1945        if cli.detached {
1946            self.detached = true;
1947        }
1948        // Append extra arguments (if any) after a "--" separator.
1949        if !cli.extra.is_empty() {
1950            self.args.push("--".into());
1951            self.args.extend(cli.extra.iter().cloned());
1952        }
1953        self
1954    }
1955    /// Append required features based on the manifest, target kind, and name.
1956    /// This method queries your manifest helper function and, if features are found,
1957    /// appends "--features" and the feature list.
1958    pub fn with_required_features(mut self, manifest: &PathBuf, target: &CargoTarget) -> Self {
1959        if !self.args.contains(&"--features".to_string()) {
1960            if let Some(features) = crate::e_manifest::get_required_features_from_manifest(
1961                manifest,
1962                &target.kind,
1963                &target.name,
1964            ) {
1965                self.args.push("--features".to_string());
1966                self.args.push(features);
1967            }
1968        }
1969        self
1970    }
1971
1972    /// Appends extra arguments to the command.
1973    pub fn with_extra_args(mut self, extra: &[String]) -> Self {
1974        if !extra.is_empty() {
1975            // Use "--" to separate Cargo arguments from target-specific arguments.
1976            self.args.push("--".into());
1977            self.args.extend(extra.iter().cloned());
1978        }
1979        self
1980    }
1981
1982    /// Builds the final vector of command-line arguments.
1983    pub fn build(self) -> Vec<String> {
1984        self.args
1985    }
1986
1987    pub fn is_compiler_target(&self) -> bool {
1988        let supported_subcommands = ["run", "build", "check", "leptos", "tauri"];
1989        if let Some(alternate) = &self.alternate_cmd {
1990            if alternate == "trunk" {
1991                return true;
1992            }
1993            if alternate != "cargo" {
1994                return false;
1995            }
1996        }
1997        if let Some(_) = self
1998            .args
1999            .iter()
2000            .position(|arg| supported_subcommands.contains(&arg.as_str()))
2001        {
2002            return true;
2003        }
2004        false
2005    }
2006
2007    pub fn injected_args(&self) -> (String, Vec<String>) {
2008        let mut new_args = self.args.clone();
2009        let supported_subcommands = [
2010            "run", "build", "test", "bench", "clean", "doc", "publish", "update",
2011        ];
2012
2013        if self.is_filter {
2014            if let Some(pos) = new_args
2015                .iter()
2016                .position(|arg| supported_subcommands.contains(&arg.as_str()))
2017            {
2018                // If the command is a supported subcommand like "cargo run", insert the JSON output format and color options.
2019                new_args.insert(pos + 1, "--message-format=json".into());
2020                new_args.insert(pos + 2, "--color".into());
2021                new_args.insert(pos + 3, "always".into());
2022            }
2023        }
2024
2025        let mut program = self.alternate_cmd.as_deref().unwrap_or("cargo").to_string();
2026
2027        if self.use_cache {
2028            #[cfg(target_os = "windows")]
2029            {
2030                // On Windows, we use the `cargo-e` executable.
2031                program = format!("{}.exe", self.target_name.clone());
2032            }
2033            #[cfg(not(target_os = "windows"))]
2034            {
2035                program = self.target_name.clone();
2036            }
2037            let debug_path = Path::new("target").join("debug").join(program.clone());
2038            let release_path = Path::new("target").join("release").join(program.clone());
2039            let release_examples_path = Path::new("target")
2040                .join("release")
2041                .join("examples")
2042                .join(program.clone());
2043            let debug_examples_path = Path::new("target")
2044                .join("debug")
2045                .join("examples")
2046                .join(program.clone());
2047            if release_path.exists() {
2048                program = release_path.to_string_lossy().to_string();
2049            } else if release_examples_path.exists() {
2050                program = release_examples_path.to_string_lossy().to_string();
2051            } else if debug_path.exists() {
2052                program = debug_path.to_string_lossy().to_string();
2053            } else if debug_examples_path.exists() {
2054                program = debug_examples_path.to_string_lossy().to_string();
2055            } else if Path::new(&program).exists() {
2056                // If the program exists in the current directory, use it.
2057                program = Path::new(&program).to_string_lossy().to_string();
2058            } else {
2059                program = self.alternate_cmd.as_deref().unwrap_or("cargo").to_string();
2060            }
2061            // new_args = vec![]
2062        }
2063
2064        if self.default_binary_is_runner {
2065            program = "cargo".to_string();
2066            new_args = vec![
2067                "run".to_string(),
2068                "--".to_string(),
2069                self.target_name.clone(),
2070            ];
2071        }
2072
2073        (program, new_args)
2074    }
2075
2076    pub fn print_command(&self) {
2077        let (program, new_args) = self.injected_args();
2078        println!("{} {}", program, new_args.join(" "));
2079    }
2080
2081    /// builds a std::process::Command.
2082    pub fn build_command(&self) -> Command {
2083        let (program, new_args) = self.injected_args();
2084
2085        let mut cmd = if self.detached {
2086            #[cfg(target_os = "windows")]
2087            {
2088                let mut detached_cmd = Command::new("cmd");
2089                // On Windows, to ensure the timeout is applied after the command runs, you should use the `timeout` command after the actual command and its arguments.
2090                // However, the Windows `cmd /c start` command does not natively support running a command and then a timeout in sequence directly.
2091                // Instead, you can chain commands using `&&` so that the timeout runs after the main command completes.
2092
2093                // Try to find "startt" using which::which
2094                let startt_path = which("startt").ok();
2095                if let Some(hold_time) = self.detached_hold {
2096                    println!(
2097                        "Running detached command with hold time: {} seconds",
2098                        hold_time
2099                    );
2100                    let cmdline = format!("{} {}", program, new_args.join(" "));
2101                    if let Some(startt) = startt_path {
2102                        // Use startt directly if found
2103                        detached_cmd = Command::new(startt);
2104                        //detached_cmd.creation_flags(0x00000008); // CREATE_NEW_CONSOLE
2105                        detached_cmd.args(&["/wait"]);
2106                        if let Some(_hold_time) = self.detached_hold {
2107                            detached_cmd.args(&["--detached-hold", &hold_time.to_string()]);
2108                        }
2109                        if let Some(delay_time) = self. detached_delay {
2110                            detached_cmd.args(&["--detached-delay", &delay_time.to_string()]);// &delay_time.to_string()]);
2111                        }
2112                        detached_cmd.args(&[&program]);
2113                        detached_cmd.args(&new_args);
2114                        println!("Using startt: {:?}", detached_cmd);
2115                        return detached_cmd;
2116                    } else {
2117                        // Fallback to cmd /c start /wait
2118                        // To enforce a timeout regardless of how cmdline exits, use PowerShell's Start-Process with -Wait and a timeout loop.
2119                        // This launches the process and then waits up to hold_time seconds, killing it if it exceeds the timeout.
2120                        // Note: This requires PowerShell to be available.
2121                        if let Some(hold_time) = self.detached_hold {
2122                            let ps_script = format!(
2123                                "Start-Process -NoNewWindow -Wait -FilePath cmd -ArgumentList '/c', '{}' ; $p = Get-Process -Name '{}' -ErrorAction SilentlyContinue; $t = 0; while ($p -and $t -lt {}) {{ Start-Sleep -Seconds 1; $t++; $p = Get-Process -Name '{}' -ErrorAction SilentlyContinue }}; if ($p) {{ $p | Stop-Process }}",
2124                                cmdline,
2125                                program,
2126                                hold_time,
2127                                program
2128                            );
2129                            detached_cmd = Command::new("powershell");
2130                            detached_cmd.args(&["-NoProfile", "-Command", &ps_script]);
2131                        } else {
2132                            detached_cmd.args(&["/c", "start", "/wait", "cmd", "/c", &cmdline]);
2133                        }
2134                        return detached_cmd;
2135                    }
2136                } else {
2137                    let cmdline = format!("{} {}", program, new_args.join(" "));
2138                    if let Some(startt) = startt_path {
2139                        // Use startt directly if found
2140                        detached_cmd = Command::new(startt);
2141                        detached_cmd.args(&[&program]);
2142                        detached_cmd.args(&new_args);
2143                        return detached_cmd;
2144                    } else {
2145                        // Fallback to cmd /c start /wait
2146                        detached_cmd.args(&["/c", "start", "/wait", "cmd", "/c", &cmdline]);
2147                    }
2148                }
2149                detached_cmd
2150            }
2151            #[cfg(target_os = "linux")]
2152            {
2153                let mut detached_cmd = Command::new("xterm");
2154                detached_cmd.args(&["-e", &program]);
2155                detached_cmd.args(&new_args);
2156                detached_cmd
2157            }
2158            #[cfg(target_os = "macos")]
2159            {
2160                let mut detached_cmd = Command::new("osascript");
2161                detached_cmd.args(&[
2162                    "-e",
2163                    &format!(
2164                        "tell application \"Terminal\" to do script \"{} {}; sleep {}; exit\"",
2165                        program,
2166                        new_args.join(" "),
2167                        self.detached_hold.unwrap_or(0)
2168                    ),
2169                ]);
2170                detached_cmd
2171            }
2172        } else {
2173            let mut cmd = Command::new(program);
2174            cmd.args(&new_args);
2175            cmd
2176        };
2177
2178        if let Some(dir) = &self.execution_dir {
2179            cmd.current_dir(dir);
2180        }
2181
2182        cmd
2183    }
2184    /// Runs the command and returns everything it printed (stdout + stderr),
2185    /// regardless of exit status.
2186    pub fn capture_output(&self) -> anyhow::Result<String> {
2187        // Build and run
2188        let mut cmd = self.build_command();
2189        let output = cmd
2190            .output()
2191            .map_err(|e| anyhow::anyhow!("Failed to spawn cargo process: {}", e))?;
2192
2193        // Decode both stdout and stderr lossily
2194        let mut all = String::new();
2195        all.push_str(&String::from_utf8_lossy(&output.stdout));
2196        all.push_str(&String::from_utf8_lossy(&output.stderr));
2197
2198        // Return the combined string, even if exit was !success
2199        Ok(all)
2200    }
2201}
2202
2203fn show_graphical_panic(
2204    line: String,
2205    prior_response: Option<CallbackResponse>,
2206    manifest_path: PathBuf,
2207    _window_for_pid: u32,
2208    _stats: std::sync::Arc<std::sync::Mutex<crate::e_cargocommand_ext::CargoStats>>,
2209) {
2210    if let Ok(e_window_path) = which("e_window") {
2211        // Compose a nice message for e_window's stdin
2212        // let stats = stats.lock().unwrap();
2213        // Compose a table with cargo-e and its version, plus panic info
2214        let cargo_e_version = env!("CARGO_PKG_VERSION");
2215
2216        let anchor: String = {
2217            // If there's no prior response, return an empty string.
2218            if prior_response.is_none() {
2219                String::new()
2220            } else {
2221                // Try to parse the line as "file:line:col"
2222                let prior = prior_response.as_ref().unwrap();
2223                let file = prior.file.as_deref().unwrap_or("");
2224                //let line_num = prior.line.map(|n| n.to_string()).unwrap_or_default();
2225                //let col_num = prior.column.map(|n| n.to_string()).unwrap_or_default();
2226
2227                let full_path = std::fs::canonicalize(file).unwrap_or_else(|_| {
2228                    // Remove the top folder from the file path if possible
2229                    let stripped_file = Path::new(file).components().skip(1).collect::<PathBuf>();
2230                    let fallback_path = stripped_file.clone();
2231                    std::fs::canonicalize(&fallback_path).unwrap_or_else(|_| {
2232                        let manifest_dir = manifest_path.parent().unwrap_or_else(|| {
2233                            eprintln!(
2234                                "Failed to determine parent directory for manifest: {:?}",
2235                                manifest_path
2236                            );
2237                            Path::new(".")
2238                        });
2239                        let parent_fallback_path = manifest_dir.join(file);
2240                        std::fs::canonicalize(&parent_fallback_path).unwrap_or_else(|_| {
2241                            eprintln!("Failed to resolve full path for: {} using ../", file);
2242                            let parent_fallback_path = manifest_dir.join(&stripped_file);
2243                            if parent_fallback_path.exists() {
2244                                parent_fallback_path
2245                            } else {
2246                                PathBuf::from(file)
2247                            }
2248                        })
2249                    })
2250                });
2251                let stripped_file = full_path.to_string_lossy().replace("\\\\?\\", "");
2252                let code_path = which("code").unwrap_or_else(|_| "code".to_string().into());
2253                String::from(format!(
2254                    "\nanchor: code {} {} {}|\"{}\" --goto \"{}:{}:{}\"\n",
2255                    stripped_file,
2256                    prior.line.unwrap_or(0),
2257                    prior.column.unwrap_or(0),
2258                    code_path.display(),
2259                    stripped_file,
2260                    prior.line.unwrap_or(0),
2261                    prior.column.unwrap_or(0)
2262                ))
2263            }
2264        };
2265        let context = ThreadLocalContext::get_context();
2266        let mut card = format!(
2267            "--title \"panic: {target}\" --width 400 --height 300\n\
2268                        target | {target} | string\n\
2269                        cargo-e | {version} | string\n\
2270                        \n\
2271                        panic: {target}\n{line}",
2272            target = context.target_name,
2273            version = cargo_e_version,
2274            line = line
2275        );
2276        if let Some(prior) = prior_response {
2277            if let Some(msg) = &prior.message {
2278                card = format!("{}\n{}", card, msg);
2279            }
2280        }
2281        if !anchor.is_empty() {
2282            card = format!("{}{}", card, anchor);
2283        }
2284        #[cfg(target_os = "windows")]
2285        let child = std::process::Command::new(e_window_path)
2286            .stdin(std::process::Stdio::piped())
2287            .creation_flags(0x00000008) // CREATE_NEW_CONSOLE
2288            .spawn();
2289        #[cfg(not(target_os = "windows"))]
2290        let child = std::process::Command::new(e_window_path)
2291            .stdin(std::process::Stdio::piped())
2292            .spawn();
2293        if let Ok(mut child) = child {
2294            if let Some(stdin) = child.stdin.as_mut() {
2295                use std::io::Write;
2296                let _ = stdin.write_all(card.as_bytes());
2297                let pid = child.id();
2298                // Add to global e_window pid list if available
2299
2300                if let Some(global) = crate::GLOBAL_EWINDOW_PIDS.get() {
2301                    global.insert(pid, pid);
2302                    log::trace!("Added pid {} to GLOBAL_EWINDOW_PIDS", pid);
2303                } else {
2304                    log::trace!("GLOBAL_EWINDOW_PIDS is not initialized");
2305                }
2306                std::mem::drop(child)
2307            }
2308        }
2309    }
2310}
2311
2312/// Resolves a file path by:
2313///   1. If the path is relative, try to resolve it relative to the current working directory.
2314///   2. If that file does not exist, try to resolve it relative to the parent directory of the manifest path.
2315///   3. Otherwise, return the original relative path.
2316pub(crate) fn resolve_file_path(manifest_path: &PathBuf, file_str: &str) -> PathBuf {
2317    let file_path = Path::new(file_str);
2318    if file_path.is_relative() {
2319        // 1. Try resolving relative to the current working directory.
2320        if let Ok(cwd) = env::current_dir() {
2321            let cwd_path = cwd.join(file_path);
2322            if cwd_path.exists() {
2323                return cwd_path;
2324            }
2325        }
2326        // 2. Try resolving relative to the parent of the manifest path.
2327        if let Some(manifest_parent) = manifest_path.parent() {
2328            let parent_path = manifest_parent.join(file_path);
2329            if parent_path.exists() {
2330                return parent_path;
2331            }
2332        }
2333        // 3. Neither existed; return the relative path as-is.
2334        return file_path.to_path_buf();
2335    }
2336    file_path.to_path_buf()
2337}
2338
2339// --- Example usage ---
2340#[cfg(test)]
2341mod tests {
2342    use crate::e_target::TargetOrigin;
2343
2344    use super::*;
2345
2346    #[test]
2347    fn test_command_builder_example() {
2348        let target_name = "my_example".to_string();
2349        let target = CargoTarget {
2350            name: "my_example".to_string(),
2351            display_name: "My Example".to_string(),
2352            manifest_path: "Cargo.toml".into(),
2353            kind: TargetKind::Example,
2354            extended: true,
2355            toml_specified: false,
2356            origin: Some(TargetOrigin::SingleFile(PathBuf::from(
2357                "examples/my_example.rs",
2358            ))),
2359        };
2360
2361        let extra_args = vec!["--flag".to_string(), "value".to_string()];
2362
2363        let manifest_path = PathBuf::from("Cargo.toml");
2364        let args = CargoCommandBuilder::new(
2365            &target_name,
2366            &manifest_path,
2367            "run",
2368            false,
2369            false,
2370            false,
2371            false,
2372            false,
2373        )
2374        .with_target(&target)
2375        .with_extra_args(&extra_args)
2376        .build();
2377
2378        // For an example target, we expect something like:
2379        // cargo run --example my_example --manifest-path Cargo.toml -- --flag value
2380        assert!(args.contains(&"--example".to_string()));
2381        assert!(args.contains(&"my_example".to_string()));
2382        assert!(args.contains(&"--".to_string()));
2383        assert!(args.contains(&"--flag".to_string()));
2384        assert!(args.contains(&"value".to_string()));
2385    }
2386}