pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! Core dependency resolution logic for individual packages.

use super::parse::{parse_dep_spec, parse_pacman_si_conflicts, parse_pacman_si_deps};
use super::source::{determine_dependency_source, is_system_package};
use super::srcinfo::{fetch_srcinfo, parse_srcinfo_conflicts, parse_srcinfo_deps};
use super::status::determine_status;
use crate::logic::files::get_pkgbuild_from_cache;
use crate::logic::sandbox::{parse_pkgbuild_conflicts, parse_pkgbuild_deps};
use crate::state::modal::DependencyInfo;
use crate::state::types::Source;
use std::collections::{HashMap, HashSet};
use std::process::{Command, Stdio};

/// What: Batch fetch dependency lists for multiple official packages using `pacman -Si`.
///
/// Inputs:
/// - `names`: Package names to query (must be official packages, not local).
///
/// Output:
/// - `HashMap` mapping package name to its dependency list (`Vec<String>`).
///
/// Details:
/// - Batches queries into chunks of 50 to avoid command-line length limits.
/// - Parses multi-package `pacman -Si` output (packages separated by blank lines).
pub(super) fn batch_fetch_official_deps(names: &[&str]) -> HashMap<String, Vec<String>> {
    const BATCH_SIZE: usize = 50;
    let mut result_map = HashMap::new();

    for chunk in names.chunks(BATCH_SIZE) {
        let mut args = vec!["-Si"];
        args.extend(chunk.iter().copied());
        match Command::new("pacman")
            .args(&args)
            .env("LC_ALL", "C")
            .env("LANG", "C")
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
        {
            Ok(output) if output.status.success() => {
                let text = String::from_utf8_lossy(&output.stdout);
                // Parse multi-package output: packages are separated by blank lines
                let mut package_blocks = Vec::new();
                let mut current_block = String::new();
                for line in text.lines() {
                    if line.trim().is_empty() {
                        if !current_block.is_empty() {
                            package_blocks.push(current_block.clone());
                            current_block.clear();
                        }
                    } else {
                        current_block.push_str(line);
                        current_block.push('\n');
                    }
                }
                if !current_block.is_empty() {
                    package_blocks.push(current_block);
                }

                // Parse each block to extract package name and dependencies
                for block in package_blocks {
                    let dep_names = parse_pacman_si_deps(&block);
                    // Extract package name from block
                    if let Some(name_line) =
                        block.lines().find(|l| l.trim_start().starts_with("Name"))
                        && let Some((_, name)) = name_line.split_once(':')
                    {
                        let pkg_name = name.trim().to_string();
                        result_map.insert(pkg_name, dep_names);
                    }
                }
            }
            _ => {
                // If batch fails, fall back to individual queries (but don't do it here to avoid recursion)
                // The caller will handle individual queries
                break;
            }
        }
    }
    result_map
}

/// What: Check if a command is available in PATH.
///
/// Inputs:
/// - `cmd`: Command name to check.
///
/// Output:
/// - Returns true if the command exists and can be executed.
///
/// Details:
/// - Uses a simple version check to verify command availability.
fn is_command_available(cmd: &str) -> bool {
    Command::new(cmd)
        .args(["--version"])
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .output()
        .is_ok()
}

/// What: Check if a package name should be filtered out (virtual package or self-reference).
///
/// Inputs:
/// - `pkg_name`: Package name to check.
/// - `parent_name`: Name of the parent package (to detect self-references).
///
/// Output:
/// - Returns true if the package should be filtered out.
///
/// Details:
/// - Filters out .so files (virtual packages) and self-references.
#[allow(clippy::case_sensitive_file_extension_comparisons)]
fn should_filter_dependency(pkg_name: &str, parent_name: &str) -> bool {
    let pkg_lower = pkg_name.to_lowercase();
    pkg_name == parent_name
        || pkg_lower.ends_with(".so")
        || pkg_lower.contains(".so.")
        || pkg_lower.contains(".so=")
}

/// What: Convert a dependency spec into a `DependencyInfo` record.
///
/// Inputs:
/// - `dep_spec`: Dependency specification string (may include version requirements).
/// - `parent_name`: Name of the package that requires this dependency.
/// - `installed`: Set of locally installed packages.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades.
///
/// Output:
/// - Returns Some(DependencyInfo) if the dependency should be included, None if filtered.
///
/// Details:
/// - Parses the dependency spec, filters out virtual packages and self-references,
///   and determines status, source, and system package flags.
fn process_dependency_spec(
    dep_spec: &str,
    parent_name: &str,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Option<DependencyInfo> {
    let (pkg_name, version_req) = parse_dep_spec(dep_spec);

    if should_filter_dependency(&pkg_name, parent_name) {
        if pkg_name == parent_name {
            tracing::debug!("Skipping self-reference: {} == {}", pkg_name, parent_name);
        } else {
            tracing::debug!("Filtering out virtual package: {}", pkg_name);
        }
        return None;
    }

    let status = determine_status(&pkg_name, &version_req, installed, provided, upgradable);
    let (source, is_core) = determine_dependency_source(&pkg_name, installed);
    let is_system = is_core || is_system_package(&pkg_name);

    Some(DependencyInfo {
        name: pkg_name,
        version: version_req,
        status,
        source,
        required_by: vec![parent_name.to_string()],
        depends_on: Vec::new(),
        is_core,
        is_system,
    })
}

/// What: Process a list of dependency specs into `DependencyInfo` records.
///
/// Inputs:
/// - `dep_specs`: Vector of dependency specification strings.
/// - `parent_name`: Name of the package that requires these dependencies.
/// - `installed`: Set of locally installed packages.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades.
///
/// Output:
/// - Returns a vector of `DependencyInfo` records (filtered).
///
/// Details:
/// - Processes each dependency spec and collects valid dependencies.
fn process_dependency_specs(
    dep_specs: Vec<String>,
    parent_name: &str,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Vec<DependencyInfo> {
    dep_specs
        .into_iter()
        .filter_map(|dep_spec| {
            process_dependency_spec(&dep_spec, parent_name, installed, provided, upgradable)
        })
        .collect()
}

/// What: Resolve dependencies for a local package using pacman -Qi.
///
/// Inputs:
/// - `name`: Package name.
/// - `installed`: Set of locally installed packages.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades.
///
/// Output:
/// - Returns a vector of `DependencyInfo` records or an error string.
///
/// Details:
/// - Uses pacman -Qi to get dependency information for locally installed packages.
fn resolve_local_package_deps(
    name: &str,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Result<Vec<DependencyInfo>, String> {
    tracing::debug!("Running: pacman -Qi {} (local package)", name);
    let output = Command::new("pacman")
        .args(["-Qi", name])
        .env("LC_ALL", "C")
        .env("LANG", "C")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .map_err(|e| {
            tracing::error!("Failed to execute pacman -Qi {}: {}", name, e);
            format!("pacman -Qi failed: {e}")
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        tracing::warn!(
            "pacman -Qi {} failed with status {:?}: {}",
            name,
            output.status.code(),
            stderr
        );
        return Ok(Vec::new());
    }

    let text = String::from_utf8_lossy(&output.stdout);
    tracing::debug!("pacman -Qi {} output ({} bytes)", name, text.len());

    let dep_names = parse_pacman_si_deps(&text);
    tracing::debug!(
        "Parsed {} dependency names from pacman -Qi output",
        dep_names.len()
    );

    Ok(process_dependency_specs(
        dep_names, name, installed, provided, upgradable,
    ))
}

/// What: Resolve dependencies for an official package using pacman -Si.
///
/// Inputs:
/// - `name`: Package name.
/// - `repo`: Repository name (for logging).
/// - `installed`: Set of locally installed packages.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades.
///
/// Output:
/// - Returns a vector of `DependencyInfo` records or an error string.
///
/// Details:
/// - Uses pacman -Si to get dependency information for official packages.
fn resolve_official_package_deps(
    name: &str,
    repo: &str,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Result<Vec<DependencyInfo>, String> {
    tracing::debug!("Running: pacman -Si {} (repo: {})", name, repo);
    let output = Command::new("pacman")
        .args(["-Si", name])
        .env("LC_ALL", "C")
        .env("LANG", "C")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .map_err(|e| {
            tracing::error!("Failed to execute pacman -Si {}: {}", name, e);
            format!("pacman -Si failed: {e}")
        })?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        tracing::error!(
            "pacman -Si {} failed with status {:?}: {}",
            name,
            output.status.code(),
            stderr
        );
        return Err(format!("pacman -Si failed for {name}: {stderr}"));
    }

    let text = String::from_utf8_lossy(&output.stdout);
    tracing::debug!("pacman -Si {} output ({} bytes)", name, text.len());

    let dep_names = parse_pacman_si_deps(&text);
    tracing::debug!(
        "Parsed {} dependency names from pacman -Si output",
        dep_names.len()
    );

    Ok(process_dependency_specs(
        dep_names, name, installed, provided, upgradable,
    ))
}

/// What: Try to resolve dependencies using an AUR helper (paru or yay).
///
/// Inputs:
/// - `helper`: Helper command name ("paru" or "yay").
/// - `name`: Package name.
/// - `installed`: Set of locally installed packages.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades.
///
/// Output:
/// - Returns Some(Vec<DependencyInfo>) if successful, None otherwise.
///
/// Details:
/// - Executes helper -Si command and parses the output for dependencies.
fn try_helper_resolution(
    helper: &str,
    name: &str,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Option<Vec<DependencyInfo>> {
    tracing::debug!("Trying {} -Si {} for dependency resolution", helper, name);
    let output = Command::new(helper)
        .args(["-Si", name])
        .env("LC_ALL", "C")
        .env("LANG", "C")
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .ok()?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        tracing::debug!(
            "{} -Si {} failed (will try other methods): {}",
            helper,
            name,
            stderr.trim()
        );
        return None;
    }

    let text = String::from_utf8_lossy(&output.stdout);
    tracing::debug!("{} -Si {} output ({} bytes)", helper, name, text.len());
    let dep_names = parse_pacman_si_deps(&text);

    if dep_names.is_empty() {
        return None;
    }

    tracing::info!(
        "Using {} to resolve runtime dependencies for {} (will fetch .SRCINFO for build-time deps)",
        helper,
        name
    );

    let deps = process_dependency_specs(dep_names, name, installed, provided, upgradable);
    Some(deps)
}

/// What: Enhance dependency list with .SRCINFO data.
///
/// Inputs:
/// - `name`: Package name.
/// - `deps`: Existing dependency list to enhance.
/// - `installed`: Set of locally installed packages.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades.
///
/// Output:
/// - Returns the enhanced dependency list.
///
/// Details:
/// - Fetches and parses .SRCINFO to add missing depends entries.
fn enhance_with_srcinfo(
    name: &str,
    mut deps: Vec<DependencyInfo>,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Vec<DependencyInfo> {
    let srcinfo_text = match fetch_srcinfo(name, Some(10)) {
        Ok(text) => text,
        Err(e) => {
            tracing::warn!(
                "Could not fetch .SRCINFO for {}: {} (build-time dependencies will be missing)",
                name,
                e
            );
            return deps;
        }
    };

    tracing::debug!("Successfully fetched .SRCINFO for {}", name);
    let (srcinfo_depends, srcinfo_makedepends, srcinfo_checkdepends, srcinfo_optdepends) =
        parse_srcinfo_deps(&srcinfo_text);

    tracing::debug!(
        "Parsed .SRCINFO: {} depends, {} makedepends, {} checkdepends, {} optdepends",
        srcinfo_depends.len(),
        srcinfo_makedepends.len(),
        srcinfo_checkdepends.len(),
        srcinfo_optdepends.len()
    );

    let existing_dep_names: HashSet<String> = deps.iter().map(|d| d.name.clone()).collect();

    deps.extend(
        srcinfo_depends
            .into_iter()
            .filter_map(|dep_spec| {
                process_dependency_spec(&dep_spec, name, installed, provided, upgradable)
            })
            .filter(|dep_info| !existing_dep_names.contains(&dep_info.name)),
    );

    tracing::info!(
        "Enhanced dependency list with .SRCINFO data: total {} dependencies",
        deps.len()
    );
    deps
}

/// What: Fallback to cached PKGBUILD for dependency resolution.
///
/// Inputs:
/// - `name`: Package name.
/// - `installed`: Set of locally installed packages.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades.
///
/// Output:
/// - Returns a vector of `DependencyInfo` records if `PKGBUILD` is found, empty vector otherwise.
///
/// Details:
/// - Attempts to use cached PKGBUILD when .SRCINFO is unavailable (offline fallback).
fn fallback_to_pkgbuild(
    name: &str,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Vec<DependencyInfo> {
    let Some(pkgbuild_text) = get_pkgbuild_from_cache(name) else {
        tracing::debug!(
            "No cached PKGBUILD available for {} (offline, no dependencies resolved)",
            name
        );
        return Vec::new();
    };

    tracing::info!(
        "Using cached PKGBUILD for {} to resolve dependencies (offline fallback)",
        name
    );
    let (pkgbuild_depends, _, _, _) = parse_pkgbuild_deps(&pkgbuild_text);

    let deps = process_dependency_specs(pkgbuild_depends, name, installed, provided, upgradable);
    tracing::info!(
        "Resolved {} dependencies from cached PKGBUILD for {}",
        deps.len(),
        name
    );
    deps
}

/// What: Resolve dependencies for an AUR package.
///
/// Inputs:
/// - `name`: Package name.
/// - `installed`: Set of locally installed packages.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades.
///
/// Output:
/// - Returns a vector of `DependencyInfo` records.
///
/// Details:
/// - Tries paru/yay first, then falls back to .SRCINFO and cached PKGBUILD.
fn resolve_aur_package_deps(
    name: &str,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Vec<DependencyInfo> {
    tracing::debug!(
        "Attempting to resolve AUR package: {} (will skip if not found)",
        name
    );

    // Try paru first
    let (mut deps, mut used_helper) = if is_command_available("paru")
        && let Some(helper_deps) =
            try_helper_resolution("paru", name, installed, provided, upgradable)
    {
        (helper_deps, true)
    } else {
        (Vec::new(), false)
    };

    // Try yay if paru didn't work
    if !used_helper
        && is_command_available("yay")
        && let Some(helper_deps) =
            try_helper_resolution("yay", name, installed, provided, upgradable)
    {
        deps = helper_deps;
        used_helper = true;
    }

    if !used_helper {
        tracing::debug!(
            "Skipping AUR API for {} - paru/yay failed or not available (likely not a real package)",
            name
        );
    }

    // Always try to enhance with .SRCINFO
    deps = enhance_with_srcinfo(name, deps, installed, provided, upgradable);

    // Fallback to PKGBUILD if no dependencies were found
    if !used_helper && deps.is_empty() {
        deps = fallback_to_pkgbuild(name, installed, provided, upgradable);
    }

    deps
}

/// What: Resolve direct dependency metadata for a single package.
///
/// Inputs:
/// - `name`: Package identifier whose dependencies should be enumerated.
/// - `source`: Source enum describing whether the package is official or AUR.
/// - `installed`: Set of locally installed packages for status determination.
/// - `provided`: Set of package names provided by installed packages.
/// - `upgradable`: Set of packages flagged for upgrades, used to detect stale dependencies.
///
/// Output:
/// - Returns a vector of `DependencyInfo` records or an error string when resolution fails.
///
/// Details:
/// - Invokes pacman or AUR helpers depending on source, filtering out virtual entries and self references.
pub(super) fn resolve_package_deps(
    name: &str,
    source: &Source,
    installed: &HashSet<String>,
    provided: &HashSet<String>,
    upgradable: &HashSet<String>,
) -> Result<Vec<DependencyInfo>, String> {
    let deps = match source {
        Source::Official { repo, .. } => {
            if repo == "local" {
                resolve_local_package_deps(name, installed, provided, upgradable)?
            } else {
                resolve_official_package_deps(name, repo, installed, provided, upgradable)?
            }
        }
        Source::Aur => resolve_aur_package_deps(name, installed, provided, upgradable),
    };

    tracing::debug!("Resolved {} dependencies for package {}", deps.len(), name);
    Ok(deps)
}

/// What: Fetch conflicts for a package from pacman or AUR sources.
///
/// Inputs:
/// - `name`: Package identifier.
/// - `source`: Source enum describing whether the package is official or AUR.
///
/// Output:
/// - Returns a vector of conflicting package names, or empty vector on error.
///
/// Details:
/// - For official packages, uses `pacman -Si` to get conflicts.
/// - For AUR packages, tries paru/yay first, then falls back to .SRCINFO.
pub(super) fn fetch_package_conflicts(name: &str, source: &Source) -> Vec<String> {
    match source {
        Source::Official { repo, .. } => {
            // Handle local packages specially - use pacman -Qi instead of -Si
            if repo == "local" {
                tracing::debug!("Running: pacman -Qi {} (local package, conflicts)", name);
                if let Ok(output) = Command::new("pacman")
                    .args(["-Qi", name])
                    .env("LC_ALL", "C")
                    .env("LANG", "C")
                    .stdin(Stdio::null())
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped())
                    .output()
                    && output.status.success()
                {
                    let text = String::from_utf8_lossy(&output.stdout);
                    return parse_pacman_si_conflicts(&text);
                }
                return Vec::new();
            }

            // Use pacman -Si to get conflicts
            tracing::debug!("Running: pacman -Si {} (conflicts)", name);
            if let Ok(output) = Command::new("pacman")
                .args(["-Si", name])
                .env("LC_ALL", "C")
                .env("LANG", "C")
                .stdin(Stdio::null())
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .output()
                && output.status.success()
            {
                let text = String::from_utf8_lossy(&output.stdout);
                return parse_pacman_si_conflicts(&text);
            }
            Vec::new()
        }
        Source::Aur => {
            // Try paru/yay first
            let has_paru = Command::new("paru")
                .args(["--version"])
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .output()
                .is_ok();

            let has_yay = Command::new("yay")
                .args(["--version"])
                .stdin(Stdio::null())
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .output()
                .is_ok();

            if has_paru {
                tracing::debug!("Trying paru -Si {} for conflicts", name);
                if let Ok(output) = Command::new("paru")
                    .args(["-Si", name])
                    .env("LC_ALL", "C")
                    .env("LANG", "C")
                    .stdin(Stdio::null())
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped())
                    .output()
                    && output.status.success()
                {
                    let text = String::from_utf8_lossy(&output.stdout);
                    let conflicts = parse_pacman_si_conflicts(&text);
                    if !conflicts.is_empty() {
                        return conflicts;
                    }
                }
            }

            if has_yay {
                tracing::debug!("Trying yay -Si {} for conflicts", name);
                if let Ok(output) = Command::new("yay")
                    .args(["-Si", name])
                    .env("LC_ALL", "C")
                    .env("LANG", "C")
                    .stdin(Stdio::null())
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped())
                    .output()
                    && output.status.success()
                {
                    let text = String::from_utf8_lossy(&output.stdout);
                    let conflicts = parse_pacman_si_conflicts(&text);
                    if !conflicts.is_empty() {
                        return conflicts;
                    }
                }
            }

            // Fall back to .SRCINFO
            if let Ok(srcinfo_text) = fetch_srcinfo(name, Some(10)) {
                tracing::debug!("Using .SRCINFO for conflicts of {}", name);
                let conflicts = parse_srcinfo_conflicts(&srcinfo_text);
                if !conflicts.is_empty() {
                    return conflicts;
                }
            }

            // Fall back to cached PKGBUILD if .SRCINFO didn't have conflicts or wasn't available
            if let Some(pkgbuild_text) = get_pkgbuild_from_cache(name) {
                tracing::debug!("Using cached PKGBUILD for conflicts of {}", name);
                let conflicts = parse_pkgbuild_conflicts(&pkgbuild_text);
                if !conflicts.is_empty() {
                    tracing::info!(
                        "Found {} conflicts from PKGBUILD for {}",
                        conflicts.len(),
                        name
                    );
                    return conflicts;
                }
            }

            Vec::new()
        }
    }
}

#[cfg(all(test, unix))]
mod tests {
    use super::*;
    use std::fs;
    use std::io::Write;
    use std::os::unix::fs::PermissionsExt;
    use tempfile::tempdir;

    struct PathGuard {
        original: Option<String>,
    }

    impl PathGuard {
        fn push(dir: &std::path::Path) -> Self {
            let original = std::env::var("PATH").ok();
            // If PATH is missing or empty, use a default system PATH
            let base_path = original
                .as_ref()
                .filter(|p| !p.is_empty())
                .map_or("/usr/bin:/bin:/usr/local/bin", String::as_str);
            let mut new_path = dir.display().to_string();
            new_path.push(':');
            new_path.push_str(base_path);
            unsafe {
                std::env::set_var("PATH", &new_path);
            }
            Self { original }
        }
    }

    impl Drop for PathGuard {
        fn drop(&mut self) {
            if let Some(ref orig) = self.original {
                // Only restore if the original PATH was valid (not empty)
                if orig.is_empty() {
                    // If original was empty, restore to a default system PATH
                    unsafe {
                        std::env::set_var("PATH", "/usr/bin:/bin:/usr/local/bin");
                    }
                } else {
                    unsafe {
                        std::env::set_var("PATH", orig);
                    }
                }
            } else {
                // If PATH was missing, set a default system PATH
                unsafe {
                    std::env::set_var("PATH", "/usr/bin:/bin:/usr/local/bin");
                }
            }
        }
    }

    fn write_executable(dir: &std::path::Path, name: &str, body: &str) {
        let path = dir.join(name);
        let mut file = fs::File::create(&path).expect("create stub");
        file.write_all(body.as_bytes()).expect("write stub");
        let mut perms = fs::metadata(&path).expect("meta").permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&path, perms).expect("chmod stub");
    }

    #[test]
    /// What: Confirm official dependency resolution consumes the pacman stub output and filters virtual entries.
    ///
    /// Inputs:
    /// - Staged `pacman` shell script that prints a crafted `-Si` response including `.so` and versioned dependencies.
    ///
    /// Output:
    /// - Dependency vector contains only the real packages with preserved version requirements and `required_by` set.
    ///
    /// Details:
    /// - Guards against regressions in parsing logic for the pacman path while isolating the function from system binaries via PATH overrides.
    fn resolve_official_uses_pacman_si_stub() {
        let dir = tempdir().expect("tempdir");
        let _test_guard = crate::global_test_mutex_lock();
        // Ensure PATH is in a clean state before modifying it
        if std::env::var("PATH").is_err() {
            unsafe { std::env::set_var("PATH", "/usr/bin:/bin:/usr/local/bin") };
        }
        let _guard = PathGuard::push(dir.path());
        // Small delay to ensure PATH is propagated to child processes
        std::thread::sleep(std::time::Duration::from_millis(10));
        write_executable(
            dir.path(),
            "pacman",
            r#"#!/bin/sh
if [ "$1" = "--version" ]; then
exit 0
fi
if [ "$1" = "-Si" ]; then
cat <<'EOF'
Name            : pkg
Depends On      : dep1 libplaceholder.so other>=1.2
EOF
exit 0
fi
exit 1
"#,
        );

        let installed = HashSet::new();
        let upgradable = HashSet::new();
        let provided = HashSet::new();
        let deps = resolve_package_deps(
            "pkg",
            &Source::Official {
                repo: "extra".into(),
                arch: "x86_64".into(),
            },
            &installed,
            &provided,
            &upgradable,
        )
        .expect("resolve succeeds");

        assert_eq!(deps.len(), 2);
        let mut names: Vec<&str> = deps.iter().map(|d| d.name.as_str()).collect();
        names.sort_unstable();
        assert_eq!(names, vec!["dep1", "other"]);

        let other = deps
            .iter()
            .find(|d| d.name == "other")
            .expect("other present");
        assert_eq!(other.version, ">=1.2");
        assert_eq!(other.required_by, vec!["pkg".to_string()]);
    }

    #[test]
    /// What: Verify the AUR branch leverages the helper stub output and skips self-referential dependencies.
    ///
    /// Inputs:
    /// - PATH-injected `paru` script responding to `--version` and `-Si`, plus inert stubs for `yay` and `pacman`.
    ///
    /// Output:
    /// - Dependency list reflects helper-derived entries while omitting the package itself.
    ///
    /// Details:
    /// - Ensures helper discovery short-circuits the API fallback and that parsing behaves consistently for AUR responses.
    fn resolve_aur_prefers_paru_stub_and_skips_self() {
        let dir = tempdir().expect("tempdir");
        let _test_guard = crate::global_test_mutex_lock();
        // Ensure PATH is in a clean state before modifying it
        if std::env::var("PATH").is_err() {
            unsafe { std::env::set_var("PATH", "/usr/bin:/bin:/usr/local/bin") };
        }
        let _guard = PathGuard::push(dir.path());
        // Enable curl PATH lookup override so our fake curl is used instead of /usr/bin/curl
        unsafe { std::env::set_var("PACSEA_CURL_PATH", "1") };
        // Small delay to ensure PATH is propagated to child processes
        std::thread::sleep(std::time::Duration::from_millis(10));
        write_executable(
            dir.path(),
            "paru",
            r#"#!/bin/sh
if [ "$1" = "--version" ]; then
exit 0
fi
if [ "$1" = "-Si" ]; then
cat <<'EOF'
Name            : pkg
Depends On      : pkg helper extra>=2.0
EOF
exit 0
fi
exit 1
"#,
        );
        write_executable(
            dir.path(),
            "yay",
            "#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then exit 0; fi\nexit 1\n",
        );
        write_executable(
            dir.path(),
            "pacman",
            "#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then exit 0; fi\nexit 1\n",
        );
        write_executable(
            dir.path(),
            "curl",
            "#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then exit 0; fi\nexit 1\n",
        );

        let installed = HashSet::new();
        let upgradable = HashSet::new();
        let provided = HashSet::new();
        let deps = resolve_package_deps("pkg", &Source::Aur, &installed, &provided, &upgradable)
            .expect("resolve succeeds");

        // Clean up env var
        unsafe { std::env::remove_var("PACSEA_CURL_PATH") };

        assert_eq!(deps.len(), 2);
        let mut names: Vec<&str> = deps.iter().map(|d| d.name.as_str()).collect();
        names.sort_unstable();
        assert_eq!(names, vec!["extra", "helper"]);
        let extra = deps
            .iter()
            .find(|d| d.name == "extra")
            .expect("extra present");
        assert_eq!(extra.version, ">=2.0");
        assert_eq!(extra.required_by, vec!["pkg".to_string()]);
    }
}