muntjac 0.2.0

Translate uv.lock into Buck2 build rules
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
//! Merge multiple `FixupBody` instances into a single `ResolvedFixup`.
//!
//! S6 is local-only: `resolve_for_cell` runs `merge_into` over the
//! top-level body, then over each cfg section whose predicate matches.
//! S7 will add the community layer as an outer wrapper.

use std::collections::BTreeMap;
use std::path::PathBuf;

use pep508_rs::PackageName;

use crate::fixup::cfg::{CfgContext, CfgPredicate};
use crate::fixup::loader::FixupSet;
use crate::fixup::schema::{EntryPoints, FixupBody, FixupConfig};

#[derive(Debug, Clone, Default, PartialEq)]
pub struct ResolvedFixup {
    pub extra_deps: Vec<String>,
    pub omit_deps: Vec<String>,
    pub replace_deps: BTreeMap<String, String>,
    pub prefer_wheel: Option<String>,
    pub exclude_wheels: Vec<String>,
    pub overlay: Option<PathBuf>,
    pub entry_points: Option<EntryPoints>,
    pub visibility: Option<Vec<String>>,
    pub labels: Vec<String>,
    pub runtime_env: BTreeMap<String, String>,
}

/// Merge `rhs` into `lhs`. List-valued fields accumulate (no dedupe;
/// emitter dedupes after applying omit_deps). Scalar fields replace
/// when rhs is `Some`. Map fields extend (later keys win).
pub fn merge_into(lhs: &mut ResolvedFixup, rhs: &FixupBody) {
    lhs.extra_deps.extend(rhs.extra_deps.iter().cloned());
    lhs.omit_deps.extend(rhs.omit_deps.iter().cloned());
    lhs.replace_deps
        .extend(rhs.replace_deps.iter().map(|(k, v)| (k.clone(), v.clone())));
    if rhs.prefer_wheel.is_some() {
        lhs.prefer_wheel = rhs.prefer_wheel.clone();
    }
    lhs.exclude_wheels
        .extend(rhs.exclude_wheels.iter().cloned());
    if rhs.overlay.is_some() {
        lhs.overlay = rhs.overlay.clone();
    }
    if rhs.entry_points.is_some() {
        lhs.entry_points = rhs.entry_points.clone();
    }
    if rhs.visibility.is_some() {
        lhs.visibility = rhs.visibility.clone();
    }
    lhs.labels.extend(rhs.labels.iter().cloned());
    lhs.runtime_env
        .extend(rhs.runtime_env.iter().map(|(k, v)| (k.clone(), v.clone())));
}

/// Cross-layer merge. Community is contributed first; local overrides
/// scalar/Option fields and extends list/map fields.
///
/// Rules:
///   - Vec<String>: community ++ local, dedup preserved-first
///   - BTreeMap<String, String>: extend (local key overwrites community)
///   - Option<T>: local wins if Some, else community
pub fn merge_resolved(community: ResolvedFixup, local: ResolvedFixup) -> ResolvedFixup {
    ResolvedFixup {
        extra_deps: concat_dedup(community.extra_deps, local.extra_deps),
        omit_deps: concat_dedup(community.omit_deps, local.omit_deps),
        replace_deps: extend_map(community.replace_deps, local.replace_deps),
        prefer_wheel: local.prefer_wheel.or(community.prefer_wheel),
        exclude_wheels: concat_dedup(community.exclude_wheels, local.exclude_wheels),
        overlay: local.overlay.or(community.overlay),
        entry_points: local.entry_points.or(community.entry_points),
        visibility: local.visibility.or(community.visibility),
        labels: concat_dedup(community.labels, local.labels),
        runtime_env: extend_map(community.runtime_env, local.runtime_env),
    }
}

/// Concatenate two Vec<String>, dedup preserving first occurrence.
fn concat_dedup(a: Vec<String>, b: Vec<String>) -> Vec<String> {
    let mut seen = std::collections::HashSet::new();
    let mut out = Vec::with_capacity(a.len() + b.len());
    for s in a.into_iter().chain(b) {
        if seen.insert(s.clone()) {
            out.push(s);
        }
    }
    out
}

fn extend_map(
    mut a: BTreeMap<String, String>,
    b: BTreeMap<String, String>,
) -> BTreeMap<String, String> {
    a.extend(b);
    a
}

/// Resolve a package's fixup for one cell. Returns the merged `ResolvedFixup`.
/// Predicate parse failures cause that one section to be silently skipped
/// (parse errors are reported at load time, not here).
pub fn resolve_for_cell(config: &FixupConfig, ctx: &CfgContext<'_>) -> ResolvedFixup {
    let mut out = ResolvedFixup::default();
    merge_into(&mut out, &config.top);
    for (predicate_str, body) in &config.cfg_sections {
        let Ok(predicate) = CfgPredicate::parse(predicate_str) else {
            continue; // parse error reported at load time, not here
        };
        if predicate.evaluate(ctx) {
            merge_into(&mut out, body);
        }
    }
    out
}

/// Two-layer fixup facade. Owns the community and local `FixupSet`s
/// and exposes a single `.resolve(pkg, ctx)` entry point that hides
/// the layering from the emitter.
#[derive(Debug, Default, Clone)]
pub struct EffectiveFixups {
    pub community: FixupSet,
    pub local: FixupSet,
}

impl EffectiveFixups {
    /// Load both layers per the registry configuration and the
    /// `allow_local_overrides` flag.
    ///
    /// - `RegistryConfig::None`        → community is empty.
    /// - `RegistryConfig::FileUrl(p)`  → community loaded from `<p>/packages/`.
    /// - `RegistryConfig::Git { url, rev }` → calls `fetch_into_cache(url, rev, offline)`
    ///   to obtain a local working tree, then loads community from that tree.
    ///   When `offline=true`, the fetch skips network access and uses any cached
    ///   copy (failing if none exists).
    /// - `allow_local_overrides=false` → local is empty regardless of disk state.
    pub fn load(
        registry: &crate::fixup::RegistryConfig,
        third_party_dir: &std::path::Path,
        allow_local_overrides: bool,
        offline: bool,
    ) -> Result<Self, crate::fixup::FixupError> {
        let community = match registry {
            crate::fixup::RegistryConfig::None => FixupSet::default(),
            crate::fixup::RegistryConfig::FileUrl(registry_dir) => {
                crate::fixup::load_community(registry_dir)?
            }
            crate::fixup::RegistryConfig::Git { url, rev } => {
                let resolved = crate::fixup::fetch_into_cache(url, rev.as_deref(), offline)?;
                crate::fixup::load_community(&resolved.working_tree)?
            }
        };

        let local = if allow_local_overrides {
            crate::fixup::load_local(third_party_dir)?
        } else {
            FixupSet::default()
        };

        Ok(Self { community, local })
    }

    /// Resolve a package's fixup for one cell. Always returns a
    /// `ResolvedFixup` — default if neither layer has the package.
    ///
    /// Algorithm (per design spec §4.1):
    ///   1. Look up local first; if it has `replace_community = true`,
    ///      drop the community layer for this package.
    ///   2. Fully resolve each layer via `resolve_for_cell`.
    ///   3. Cross-layer merge via `merge_resolved`.
    pub fn resolve(&self, pkg: &PackageName, ctx: &CfgContext<'_>) -> ResolvedFixup {
        let local_cfg = self.local.get(pkg);
        let community_cfg = match local_cfg {
            Some(c) if c.replace_community => None,
            _ => self.community.get(pkg),
        };

        let community_resolved = community_cfg
            .map(|cfg| resolve_for_cell(cfg, ctx))
            .unwrap_or_default();
        let local_resolved = local_cfg
            .map(|cfg| resolve_for_cell(cfg, ctx))
            .unwrap_or_default();

        merge_resolved(community_resolved, local_resolved)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fixup::cfg::CfgContext;
    use pep440_rs::Version;
    use std::str::FromStr;

    static ENV_GUARD: std::sync::Mutex<()> = std::sync::Mutex::new(());

    fn empty_ctx() -> (Version, Version) {
        (
            Version::from_str("1.0").unwrap(),
            Version::from_str("3.12").unwrap(),
        )
    }

    #[test]
    fn merge_lists_accumulate() {
        let mut acc = ResolvedFixup::default();
        let a = FixupBody {
            extra_deps: vec!["//a:b".into()],
            ..Default::default()
        };
        let b = FixupBody {
            extra_deps: vec!["//c:d".into()],
            ..Default::default()
        };
        merge_into(&mut acc, &a);
        merge_into(&mut acc, &b);
        assert_eq!(acc.extra_deps, vec!["//a:b", "//c:d"]);
    }

    #[test]
    fn merge_scalars_replace_when_some() {
        let mut acc = ResolvedFixup::default();
        let a = FixupBody {
            overlay: Some("first".into()),
            ..Default::default()
        };
        let b = FixupBody {
            overlay: Some("second".into()),
            ..Default::default()
        };
        let c = FixupBody {
            overlay: None,
            ..Default::default()
        };
        merge_into(&mut acc, &a);
        merge_into(&mut acc, &b);
        merge_into(&mut acc, &c);
        assert_eq!(acc.overlay.as_deref(), Some(std::path::Path::new("second")));
    }

    #[test]
    fn merge_maps_extend() {
        let mut acc = ResolvedFixup::default();
        let mut a_rd = BTreeMap::new();
        a_rd.insert("numpy".into(), "//company/numpy:numpy".into());
        let mut b_rd = BTreeMap::new();
        b_rd.insert("numpy".into(), "//new/numpy:numpy".into());
        b_rd.insert("scipy".into(), "//company/scipy:scipy".into());

        merge_into(
            &mut acc,
            &FixupBody {
                replace_deps: a_rd,
                ..Default::default()
            },
        );
        merge_into(
            &mut acc,
            &FixupBody {
                replace_deps: b_rd,
                ..Default::default()
            },
        );

        assert_eq!(
            acc.replace_deps.get("numpy").map(|s| s.as_str()),
            Some("//new/numpy:numpy")
        );
        assert_eq!(
            acc.replace_deps.get("scipy").map(|s| s.as_str()),
            Some("//company/scipy:scipy")
        );
    }

    #[test]
    fn resolve_picks_matching_cfg_sections() {
        let (v, py) = empty_ctx();
        let ctx = CfgContext {
            package_version: &v,
            python_version: py,
            target_os: "linux",
            target_arch: "x86_64",
            target_env: "gnu",
        };
        let config = FixupConfig {
            top: FixupBody {
                extra_deps: vec!["//base:dep".into()],
                ..Default::default()
            },
            cfg_sections: vec![
                (
                    "target_os = \"linux\"".into(),
                    FixupBody {
                        extra_deps: vec!["//linux:dep".into()],
                        ..Default::default()
                    },
                ),
                (
                    "target_os = \"macos\"".into(),
                    FixupBody {
                        extra_deps: vec!["//macos:dep".into()],
                        ..Default::default()
                    },
                ),
            ],
            replace_community: false,
        };
        let resolved = resolve_for_cell(&config, &ctx);
        assert_eq!(resolved.extra_deps, vec!["//base:dep", "//linux:dep"]);
    }

    #[test]
    fn resolve_with_no_matches_yields_top_only() {
        let (v, py) = empty_ctx();
        let ctx = CfgContext {
            package_version: &v,
            python_version: py,
            target_os: "macos",
            target_arch: "aarch64",
            target_env: "",
        };
        let config = FixupConfig {
            top: FixupBody {
                extra_deps: vec!["//base:dep".into()],
                ..Default::default()
            },
            cfg_sections: vec![(
                "target_os = \"linux\"".into(),
                FixupBody {
                    extra_deps: vec!["//linux:dep".into()],
                    ..Default::default()
                },
            )],
            replace_community: false,
        };
        let resolved = resolve_for_cell(&config, &ctx);
        assert_eq!(resolved.extra_deps, vec!["//base:dep"]);
    }

    #[test]
    fn merge_resolved_extra_deps_community_first_dedup() {
        let c = ResolvedFixup {
            extra_deps: vec!["//c:base".into(), "//shared:dep".into()],
            ..Default::default()
        };
        let l = ResolvedFixup {
            extra_deps: vec!["//shared:dep".into(), "//l:base".into()],
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(
            merged.extra_deps,
            vec!["//c:base", "//shared:dep", "//l:base"]
        );
    }

    #[test]
    fn merge_resolved_omit_deps_community_first_dedup() {
        let c = ResolvedFixup {
            omit_deps: vec!["one".into(), "two".into()],
            ..Default::default()
        };
        let l = ResolvedFixup {
            omit_deps: vec!["two".into(), "three".into()],
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(merged.omit_deps, vec!["one", "two", "three"]);
    }

    #[test]
    fn merge_resolved_replace_deps_local_wins_on_key_collision() {
        let mut c_rd = std::collections::BTreeMap::new();
        c_rd.insert("foo".to_string(), "//c:foo".to_string());
        c_rd.insert("only-community".to_string(), "//c:only".to_string());
        let c = ResolvedFixup {
            replace_deps: c_rd,
            ..Default::default()
        };
        let mut l_rd = std::collections::BTreeMap::new();
        l_rd.insert("foo".to_string(), "//l:foo".to_string());
        l_rd.insert("only-local".to_string(), "//l:only".to_string());
        let l = ResolvedFixup {
            replace_deps: l_rd,
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(
            merged.replace_deps.get("foo").map(|s| s.as_str()),
            Some("//l:foo")
        );
        assert_eq!(
            merged
                .replace_deps
                .get("only-community")
                .map(|s| s.as_str()),
            Some("//c:only")
        );
        assert_eq!(
            merged.replace_deps.get("only-local").map(|s| s.as_str()),
            Some("//l:only")
        );
    }

    #[test]
    fn merge_resolved_prefer_wheel_local_some_wins() {
        let c = ResolvedFixup {
            prefer_wheel: Some("sha256:aaa".into()),
            ..Default::default()
        };
        let l = ResolvedFixup {
            prefer_wheel: Some("sha256:bbb".into()),
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(merged.prefer_wheel.as_deref(), Some("sha256:bbb"));
    }

    #[test]
    fn merge_resolved_prefer_wheel_community_kept_when_local_none() {
        let c = ResolvedFixup {
            prefer_wheel: Some("sha256:aaa".into()),
            ..Default::default()
        };
        let l = ResolvedFixup::default();
        let merged = super::merge_resolved(c, l);
        assert_eq!(merged.prefer_wheel.as_deref(), Some("sha256:aaa"));
    }

    #[test]
    fn merge_resolved_exclude_wheels_concat_dedup() {
        let c = ResolvedFixup {
            exclude_wheels: vec!["*win32*".into()],
            ..Default::default()
        };
        let l = ResolvedFixup {
            exclude_wheels: vec!["*win32*".into(), "*cuda*".into()],
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(merged.exclude_wheels, vec!["*win32*", "*cuda*"]);
    }

    #[test]
    fn merge_resolved_overlay_local_some_wins() {
        let c = ResolvedFixup {
            overlay: Some(std::path::PathBuf::from("c-overlay")),
            ..Default::default()
        };
        let l = ResolvedFixup {
            overlay: Some(std::path::PathBuf::from("l-overlay")),
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(
            merged.overlay.as_deref(),
            Some(std::path::Path::new("l-overlay"))
        );
    }

    #[test]
    fn merge_resolved_entry_points_local_some_wins() {
        let c = ResolvedFixup {
            entry_points: Some(crate::fixup::EntryPoints::Named(vec!["c-bin".into()])),
            ..Default::default()
        };
        let l = ResolvedFixup {
            entry_points: Some(crate::fixup::EntryPoints::Named(vec!["l-bin".into()])),
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        match merged.entry_points {
            Some(crate::fixup::EntryPoints::Named(names)) => {
                assert_eq!(names, vec!["l-bin".to_string()]);
            }
            other => panic!("expected Named, got {:?}", other),
        }
    }

    #[test]
    fn merge_resolved_visibility_local_some_wins() {
        let c = ResolvedFixup {
            visibility: Some(vec!["//c:...".into()]),
            ..Default::default()
        };
        let l = ResolvedFixup {
            visibility: Some(vec!["//l:...".into()]),
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(merged.visibility, Some(vec!["//l:...".to_string()]));
    }

    #[test]
    fn merge_resolved_labels_concat_dedup() {
        let c = ResolvedFixup {
            labels: vec!["tag-a".into(), "tag-b".into()],
            ..Default::default()
        };
        let l = ResolvedFixup {
            labels: vec!["tag-b".into(), "tag-c".into()],
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(merged.labels, vec!["tag-a", "tag-b", "tag-c"]);
    }

    #[test]
    fn merge_resolved_runtime_env_local_overrides_key() {
        let mut c_re = std::collections::BTreeMap::new();
        c_re.insert("FOO".to_string(), "c".to_string());
        c_re.insert("BAR".to_string(), "c".to_string());
        let c = ResolvedFixup {
            runtime_env: c_re,
            ..Default::default()
        };
        let mut l_re = std::collections::BTreeMap::new();
        l_re.insert("FOO".to_string(), "l".to_string());
        l_re.insert("BAZ".to_string(), "l".to_string());
        let l = ResolvedFixup {
            runtime_env: l_re,
            ..Default::default()
        };
        let merged = super::merge_resolved(c, l);
        assert_eq!(merged.runtime_env.get("FOO").map(|s| s.as_str()), Some("l"));
        assert_eq!(merged.runtime_env.get("BAR").map(|s| s.as_str()), Some("c"));
        assert_eq!(merged.runtime_env.get("BAZ").map(|s| s.as_str()), Some("l"));
    }

    #[test]
    fn resolve_section_order_preserved() {
        // Later cfg sections override earlier scalar values.
        let (v, py) = empty_ctx();
        let ctx = CfgContext {
            package_version: &v,
            python_version: py,
            target_os: "linux",
            target_arch: "x86_64",
            target_env: "gnu",
        };
        let config = FixupConfig {
            top: FixupBody {
                overlay: Some("top".into()),
                ..Default::default()
            },
            cfg_sections: vec![
                (
                    "target_os = \"linux\"".into(),
                    FixupBody {
                        overlay: Some("first".into()),
                        ..Default::default()
                    },
                ),
                (
                    "target_arch = \"x86_64\"".into(),
                    FixupBody {
                        overlay: Some("second".into()),
                        ..Default::default()
                    },
                ),
            ],
            replace_community: false,
        };
        let resolved = resolve_for_cell(&config, &ctx);
        assert_eq!(
            resolved.overlay.as_deref(),
            Some(std::path::Path::new("second"))
        );
    }

    fn ctx_linux() -> CfgContext<'static> {
        use pep440_rs::Version;
        // We need static lifetime; use leaking for test ergonomics.
        let v: &'static Version = Box::leak(Box::new(Version::from_str("1.0").unwrap()));
        let py: Version = Version::from_str("3.12").unwrap();
        CfgContext {
            package_version: v,
            python_version: py,
            target_os: "linux",
            target_arch: "x86_64",
            target_env: "gnu",
        }
    }

    fn fixup_with(extra_deps: Vec<&str>) -> crate::fixup::FixupConfig {
        crate::fixup::FixupConfig {
            top: crate::fixup::FixupBody {
                extra_deps: extra_deps.into_iter().map(String::from).collect(),
                ..Default::default()
            },
            cfg_sections: vec![],
            replace_community: false,
        }
    }

    #[test]
    fn effective_fixups_resolve_returns_default_when_neither_layer_has_pkg() {
        use crate::fixup::loader::FixupSet;
        let eff = super::EffectiveFixups {
            community: FixupSet::default(),
            local: FixupSet::default(),
        };
        let pkg = pep508_rs::PackageName::from_str("missing").unwrap();
        let rf = eff.resolve(&pkg, &ctx_linux());
        assert!(rf.extra_deps.is_empty());
        assert!(rf.replace_deps.is_empty());
        assert!(rf.overlay.is_none());
    }

    #[test]
    fn effective_fixups_resolve_community_only() {
        use crate::fixup::loader::FixupSet;
        let mut comm = std::collections::BTreeMap::new();
        let pkg = pep508_rs::PackageName::from_str("pkg").unwrap();
        comm.insert(pkg.clone(), fixup_with(vec!["//c:base"]));
        let eff = super::EffectiveFixups {
            community: FixupSet::from_map_for_test(comm),
            local: FixupSet::default(),
        };
        let rf = eff.resolve(&pkg, &ctx_linux());
        assert_eq!(rf.extra_deps, vec!["//c:base"]);
    }

    #[test]
    fn effective_fixups_resolve_local_only() {
        use crate::fixup::loader::FixupSet;
        let mut loc = std::collections::BTreeMap::new();
        let pkg = pep508_rs::PackageName::from_str("pkg").unwrap();
        loc.insert(pkg.clone(), fixup_with(vec!["//l:base"]));
        let eff = super::EffectiveFixups {
            community: FixupSet::default(),
            local: FixupSet::from_map_for_test(loc),
        };
        let rf = eff.resolve(&pkg, &ctx_linux());
        assert_eq!(rf.extra_deps, vec!["//l:base"]);
    }

    #[test]
    fn effective_fixups_resolve_both_layers_concat() {
        use crate::fixup::loader::FixupSet;
        let pkg = pep508_rs::PackageName::from_str("pkg").unwrap();
        let mut comm = std::collections::BTreeMap::new();
        comm.insert(pkg.clone(), fixup_with(vec!["//c:base"]));
        let mut loc = std::collections::BTreeMap::new();
        loc.insert(pkg.clone(), fixup_with(vec!["//l:base"]));
        let eff = super::EffectiveFixups {
            community: FixupSet::from_map_for_test(comm),
            local: FixupSet::from_map_for_test(loc),
        };
        let rf = eff.resolve(&pkg, &ctx_linux());
        assert_eq!(rf.extra_deps, vec!["//c:base", "//l:base"]);
    }

    #[test]
    fn effective_fixups_resolve_replace_community_drops_community() {
        use crate::fixup::loader::FixupSet;
        let pkg = pep508_rs::PackageName::from_str("pkg").unwrap();
        let mut comm = std::collections::BTreeMap::new();
        comm.insert(pkg.clone(), fixup_with(vec!["//c:never-merged"]));

        let mut loc = std::collections::BTreeMap::new();
        let local_cfg = crate::fixup::FixupConfig {
            top: crate::fixup::FixupBody {
                extra_deps: vec!["//l:only".to_string()],
                ..Default::default()
            },
            cfg_sections: vec![],
            replace_community: true,
        };
        loc.insert(pkg.clone(), local_cfg);

        let eff = super::EffectiveFixups {
            community: FixupSet::from_map_for_test(comm),
            local: FixupSet::from_map_for_test(loc),
        };
        let rf = eff.resolve(&pkg, &ctx_linux());
        assert_eq!(rf.extra_deps, vec!["//l:only"]); // community discarded
    }

    #[test]
    fn effective_fixups_resolve_replace_community_with_empty_local_yields_empty() {
        use crate::fixup::loader::FixupSet;
        let pkg = pep508_rs::PackageName::from_str("pkg").unwrap();
        let mut comm = std::collections::BTreeMap::new();
        comm.insert(pkg.clone(), fixup_with(vec!["//c:base"]));

        let mut loc = std::collections::BTreeMap::new();
        let local_cfg = crate::fixup::FixupConfig {
            replace_community: true,
            ..Default::default()
        };
        loc.insert(pkg.clone(), local_cfg);

        let eff = super::EffectiveFixups {
            community: FixupSet::from_map_for_test(comm),
            local: FixupSet::from_map_for_test(loc),
        };
        let rf = eff.resolve(&pkg, &ctx_linux());
        assert!(rf.extra_deps.is_empty());
        assert!(rf.overlay.is_none());
    }

    #[test]
    fn effective_fixups_load_none_yields_empty_community() {
        use crate::fixup::RegistryConfig;
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let eff = super::EffectiveFixups::load(&RegistryConfig::None, tmp.path(), true, false)
            .expect("loads");
        assert!(eff.community.is_empty());
        assert!(eff.local.is_empty()); // no fixups/ subdir
    }

    #[test]
    fn effective_fixups_load_file_url_walks_packages() {
        use crate::fixup::RegistryConfig;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let registry_dir = tmp.path().join("registry");
        let tpd = tmp.path().join("third-party/python");
        std::fs::create_dir_all(registry_dir.join("packages/pillow")).unwrap();
        std::fs::write(
            registry_dir.join("packages/pillow/fixups.toml"),
            r#"extra_deps = ["//c:libjpeg"]"#,
        )
        .unwrap();
        std::fs::create_dir_all(&tpd).unwrap();

        let registry = RegistryConfig::FileUrl(registry_dir);
        let eff = super::EffectiveFixups::load(&registry, &tpd, true, false).expect("loads");
        let pillow = pep508_rs::PackageName::from_str("pillow").unwrap();
        assert!(eff.community.get(&pillow).is_some());
    }

    #[test]
    fn effective_fixups_load_file_url_missing_packages_errors() {
        use crate::fixup::RegistryConfig;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let registry = RegistryConfig::FileUrl(tmp.path().to_path_buf());
        let err = super::EffectiveFixups::load(&registry, tmp.path(), true, false).unwrap_err();
        match err {
            crate::fixup::FixupError::RegistryPathNotFound { .. } => {}
            other => panic!("expected RegistryPathNotFound, got {:?}", other),
        }
    }

    #[test]
    fn effective_fixups_load_allow_local_overrides_false_skips_local() {
        use crate::fixup::RegistryConfig;
        use tempfile::TempDir;

        let tmp = TempDir::new().unwrap();
        let tpd = tmp.path().join("third-party/python");
        std::fs::create_dir_all(tpd.join("fixups/pillow")).unwrap();
        std::fs::write(
            tpd.join("fixups/pillow/fixups.toml"),
            r#"extra_deps = ["//local:pillow"]"#,
        )
        .unwrap();

        let eff =
            super::EffectiveFixups::load(&RegistryConfig::None, &tpd, false, false).expect("loads");
        assert!(eff.local.is_empty());
    }

    #[test]
    fn effective_fixups_load_git_fetches_from_bare_repo() {
        use crate::fixup::RegistryConfig;
        use tempfile::TempDir;

        let _g = ENV_GUARD.lock().unwrap();
        let cache_tmp = TempDir::new().unwrap();
        unsafe {
            std::env::set_var("MUNTJAC_CACHE_HOME", cache_tmp.path());
        }

        let src_tmp = TempDir::new().unwrap();
        let src = src_tmp.path();
        std::fs::create_dir_all(src.join("packages/pkg-a")).unwrap();
        std::fs::write(
            src.join("packages/pkg-a/fixups.toml"),
            "extra_deps = [\"//x:y\"]\n",
        )
        .unwrap();

        let bare_tmp = TempDir::new().unwrap();
        let bare = bare_tmp.path().join("registry.git");

        // Inline bare-repo construction (same shape as the helper in tests/common/git_fixture.rs).
        let work = TempDir::new().unwrap();
        for entry in walkdir::WalkDir::new(src) {
            let entry = entry.unwrap();
            let rel = entry.path().strip_prefix(src).unwrap();
            let dst = work.path().join(rel);
            if entry.file_type().is_dir() {
                std::fs::create_dir_all(&dst).unwrap();
            } else if entry.file_type().is_file() {
                if let Some(p) = dst.parent() {
                    std::fs::create_dir_all(p).unwrap();
                }
                std::fs::copy(entry.path(), &dst).unwrap();
            }
        }
        let run_git = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(work.path())
                .env("GIT_AUTHOR_NAME", "muntjac-test")
                .env("GIT_AUTHOR_EMAIL", "test@example.com")
                .env("GIT_AUTHOR_DATE", "1970-01-01T00:00:00Z")
                .env("GIT_COMMITTER_NAME", "muntjac-test")
                .env("GIT_COMMITTER_EMAIL", "test@example.com")
                .env("GIT_COMMITTER_DATE", "1970-01-01T00:00:00Z")
                .output()
                .unwrap()
        };
        assert!(run_git(&["init", "-q", "-b", "main"]).status.success());
        assert!(run_git(&["add", "-A"]).status.success());
        assert!(run_git(&["commit", "-q", "-m", "initial"]).status.success());
        assert!(
            run_git(&["clone", "-q", "--bare", ".", bare.to_str().unwrap()])
                .status
                .success()
        );

        let url = format!("file://{}", bare.display());
        let registry = RegistryConfig::Git {
            url: url.clone(),
            rev: None,
        };

        let tpd_tmp = TempDir::new().unwrap();
        let eff = super::EffectiveFixups::load(&registry, tpd_tmp.path(), true, false)
            .expect("loads via git arm");

        let pkg_a = pep508_rs::PackageName::from_str("pkg-a").unwrap();
        assert!(eff.community.get(&pkg_a).is_some());

        unsafe {
            std::env::remove_var("MUNTJAC_CACHE_HOME");
        }
    }
}