bun_install 0.1.0

A Rust-native programmable browser runtime built on Servo and SpiderMonkey
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
//! Lockfile — in-memory representation of bun.lock / bun.lockb
//!
//! Ported from src/install/lockfile.zig

use core::cmp::Ordering;
use core::fmt;
use std::io::Write as _;

use bun_alloc::AllocError;
use bun_collections::{
    ArrayHashMap, ArrayIdentityContext, ArrayIdentityContextU64, DynamicBitSet,
    HashMap as BunHashMap, IdentityContext, LinearFifo, linear_fifo::DynamicBuffer,
};
use bun_core::fmt::PathSep;
use bun_core::{Error as BunError, Global, Output, err};
use bun_paths::{MAX_PATH_BYTES, PathBuffer, SEP, SEP_STR, platform, resolve_path};
// `bun_install` sits above `bun_resolver` in the crate graph (no cycle), so use
// the real resolver `FileSystem` directly — same as `PackageManager.rs`.
use crate::bun_json as JSON;
use bun_core::zstr;
use bun_core::{ZStr, strings};
use bun_dotenv as DotEnv;
use bun_perf::system_timer::Timer;
use bun_resolver::fs::{self as Fs, FileSystem};
use bun_semver::{self as Semver, ExternalString, String as SemverString};
use bun_sha_hmac as Crypto;
use bun_sys::{self as sys, Fd, File};

use crate::config_version::ConfigVersion;
use crate::migration;
use crate::package_manager::WorkspaceFilter;
use crate::package_manager_real::{
    Options as PackageManagerOptions, options::LogLevel, populate_manifest_cache,
};
use crate::resolution_real::{self as resolution, Resolution};
use crate::string_builder;
use crate::update_request::UpdateRequest;
use crate::{
    self as Install, DependencyID, ExternalSlice, Features, PackageID, PackageManager,
    PackageNameAndVersionHash, PackageNameHash, TruncatedPackageNameHash, dependency,
    dependency::Dependency, initialize_store, invalid_dependency_id, invalid_package_id,
    npm as Npm,
};

// ────────────────────────────────────────────────────────────────────────────
// Sub-module declarations — Zig basenames preserved per PORTING.md, hence
// explicit #[path] attrs for PascalCase / dotted file names.
// ────────────────────────────────────────────────────────────────────────────

#[path = "lockfile/Buffers.rs"]
pub mod buffers;
#[path = "lockfile/bun.lock.rs"]
pub mod bun_lock;
#[path = "lockfile/bun.lockb.rs"]
pub mod bun_lockb;
#[path = "lockfile/CatalogMap.rs"]
pub mod catalog_map;
#[path = "lockfile/lockfile_json_stringify_for_debugging.rs"]
pub mod lockfile_json_stringify_for_debugging;
#[path = "lockfile/OverrideMap.rs"]
pub mod override_map;
#[path = "lockfile/Package.rs"]
pub mod package;
#[path = "lockfile/Tree.rs"]
pub mod tree;
#[path = "lockfile/printer"]
pub mod printer_mods {
    #[path = "tree_printer.rs"]
    pub mod tree_printer;
    #[path = "Yarn.rs"]
    pub mod yarn;
}

// Sub-module re-exports
pub use self::buffers::Buffers;
use self::bun_lock as TextLockfile;
pub use self::bun_lockb as Serializer;
pub use self::catalog_map::CatalogMap;
pub use self::lockfile_json_stringify_for_debugging::json_stringify;
pub use self::override_map::OverrideMap;
pub use self::package::Package; // TODO(port): Zig was `Package(u64)` — generic instantiation
pub use self::tree::Tree;
pub use crate::padding_checker::assert_no_uninitialized_padding;
// Bring the derive-generated `items_*` column accessors (`PackageColumns` for
// `MultiArrayList<Package>`, `PackageColumns` for `Slice<Package>`) into scope.
use self::package::PackageColumns as _;

// Zig path-style associated types (`Dependency.Version`, `Resolution.Tag`,
// `String.Buf`/`String.Builder`) are module-level types in the Rust port.
// Alias them locally so the body reads like the spec.
type DependencyVersion = dependency::Version;
type ResolutionTag = resolution::Tag;
type SemverStringBuf<'a> = bun_semver::semver_string::Buf<'a>;
type SemverStringBuilder = bun_semver::semver_string::Builder;

// ────────────────────────────────────────────────────────────────────────────
// Type aliases / collection types
// ────────────────────────────────────────────────────────────────────────────

pub(crate) type PackageIDSlice = ExternalSlice<PackageID>;
pub type DependencySlice = ExternalSlice<Dependency>;
pub(crate) type DependencyIDSlice = ExternalSlice<DependencyID>;

pub(crate) type PackageIDList = Vec<PackageID>;
pub(crate) type DependencyList = Vec<Dependency>;
pub(crate) type DependencyIDList = Vec<DependencyID>;

pub(crate) type StringBuffer = Vec<u8>;
pub(crate) type ExternalStringBuffer = Vec<ExternalString>;

pub(crate) type NameHashMap = ArrayHashMap<PackageNameHash, SemverString, ArrayIdentityContextU64>;
/// Value is the exact byte string the key hash was computed from; lookups must
/// compare it since truncated hashes can collide. An empty value is the legacy
/// `bun.lockb` sentinel ("name unknown, hash-only match").
pub(crate) type TrustedDependenciesSet =
    ArrayHashMap<TruncatedPackageNameHash, Box<[u8]>, ArrayIdentityContext>;
pub(crate) type VersionHashMap =
    ArrayHashMap<PackageNameHash, Semver::Version, ArrayIdentityContextU64>;
pub(crate) type PatchedDependenciesMap =
    ArrayHashMap<PackageNameAndVersionHash, PatchedDep, ArrayIdentityContextU64>;

pub(crate) type StringPool = bun_semver::string::StringPool;
// Zig: `String.Builder.StringPool` — `bun_semver::semver_string::StringPool`.

pub(crate) type MetaHash = [u8; 32]; // Sha512T256.digest_length
pub(crate) const ZERO_HASH: MetaHash = [0u8; 32];

/// Result of `maybe_clone_filtering_root_packages`: either the input lockfile was
/// returned unchanged (borrowed), or a freshly-allocated cleaned lockfile is returned
/// (owned). Spec lockfile.zig returns a `*Lockfile` in both cases; Rust distinguishes
/// ownership so the caller can drop the `Box` when done.
pub enum Cleaned<'a> {
    /// No changes needed — caller's lockfile is returned as-is.
    Same(&'a mut Lockfile),
    /// A new lockfile was allocated by `clean`; caller owns it.
    New(Box<Lockfile>),
}

// TODO(port): std.io.FixedBufferStream([]u8) — replace with cursor over &mut [u8]
pub type Stream = bun_io::FixedBufferStream<Vec<u8>>;

/// Duck-typed surface that `Buffers::write_array`/`save` and
/// `Package::Serializer::save` expect of their `stream` parameter — Zig passes
/// `anytype` (lockfile/Buffers.zig:142, bun.lockb.zig). Expressed as a trait so
/// the Rust port can stay generic over the borrowck-reshaped `StreamType` in
/// `bun.lockb.rs` (which collapses stream + writer into one `&mut`).
pub trait PositionalStream {
    /// Zig: `try stream.getPos()` — current write position.
    fn get_pos(&self) -> Result<usize, BunError>;
    /// Zig: `stream.pwrite(bytes, index)` — positional write, returns bytes
    /// written (always `data.len()` for in-memory buffers).
    fn pwrite(&mut self, data: &[u8], index: usize) -> usize;
}

impl<'a> PositionalStream for Serializer::StreamType<'a> {
    #[inline]
    fn get_pos(&self) -> Result<usize, BunError> {
        Serializer::StreamType::get_pos(self)
    }
    #[inline]
    fn pwrite(&mut self, data: &[u8], index: usize) -> usize {
        Serializer::StreamType::pwrite(self, data, index)
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Lockfile struct
// ────────────────────────────────────────────────────────────────────────────

pub struct Lockfile {
    /// The version of the lockfile format, intended to prevent data corruption for format changes.
    pub format: FormatVersion,

    pub text_lockfile_version: bun_lock::Version,

    pub meta_hash: MetaHash,

    pub packages: PackageList,
    // TODO(port): Lockfile.Package.List is a MultiArrayList<Package>
    pub buffers: Buffers,

    /// name -> PackageID || [*]PackageID
    /// Not for iterating.
    pub package_index: PackageIndexMap,
    pub string_pool: StringPool,
    // std.mem.Allocator param — dropped per PORTING.md (global mimalloc)
    pub scratch: Scratch,

    pub scripts: Scripts,
    pub workspace_paths: NameHashMap,
    pub workspace_versions: VersionHashMap,

    /// Optional because `trustedDependencies` in package.json might be an
    /// empty list or it might not exist
    pub trusted_dependencies: Option<TrustedDependenciesSet>,
    pub patched_dependencies: PatchedDependenciesMap,
    pub overrides: OverrideMap,
    pub catalogs: CatalogMap,

    pub saved_config_version: Option<ConfigVersion>,

    /// `packages.len()` at the moment lockfile load (including npm/pnpm/yarn
    /// migration) finished. Packages with `id < this` were carried in from a
    /// lockfile and represent a user-pinned resolution; packages with
    /// `id >= this` were appended by manifest fetches in the current
    /// resolve session. `get_package_id` uses this to keep its
    /// order-independence guard from overriding lockfile pins. Set by
    /// `mark_loaded_packages`; defaults to `invalid_package_id` (no lockfile
    /// loaded → guard applies to nothing, equivalent to "all entries are
    /// session-appended").
    ///
    /// Runtime-only — never serialised.
    pub loaded_package_count: PackageID,

    /// `bit[id] == true` ⇔ package `id` was appended for a dependency whose
    /// version range was an exact `=X.Y.Z` (i.e. the user — root or workspace
    /// — pinned this exact version somewhere in the tree). `get_package_id`'s
    /// order-independence guard never blocks deduping to one of these: an
    /// exact pin is a deliberate choice, not an artifact of which manifest
    /// happened to land first. Runtime-only — never serialised; sized lazily
    /// in `mark_exact_pin`.
    pub exact_pinned: Vec<bool>,
}

/// Zig: `Lockfile.Package.List` — `MultiArrayList(Package)`.
pub(crate) type PackageList = self::package::List<u64>;

// ────────────────────────────────────────────────────────────────────────────
// DepSorter
// ────────────────────────────────────────────────────────────────────────────

pub(crate) struct DepSorter<'a> {
    pub lockfile: &'a Lockfile,
}

impl<'a> DepSorter<'a> {
    pub(crate) fn is_less_than(&self, l: DependencyID, r: DependencyID) -> bool {
        let deps_buf = self.lockfile.buffers.dependencies.as_slice();
        let string_buf = self.lockfile.buffers.string_bytes.as_slice();

        let l_dep = &deps_buf[l as usize];
        let r_dep = &deps_buf[r as usize];

        match l_dep.behavior.cmp(r_dep.behavior) {
            Ordering::Less => true,
            Ordering::Greater => false,
            Ordering::Equal => {
                strings::order(l_dep.name.slice(string_buf), r_dep.name.slice(string_buf))
                    == Ordering::Less
            }
        }
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Scripts
// ────────────────────────────────────────────────────────────────────────────

#[derive(Default)]
pub struct Scripts {
    pub preinstall: Vec<Box<[u8]>>,
    pub install: Vec<Box<[u8]>>,
    pub postinstall: Vec<Box<[u8]>>,
    pub preprepare: Vec<Box<[u8]>>,
    pub prepare: Vec<Box<[u8]>>,
    pub postprepare: Vec<Box<[u8]>>,
}

impl Scripts {
    pub const NAMES: [&'static str; 6] = [
        "preinstall",
        "install",
        "postinstall",
        "preprepare",
        "prepare",
        "postprepare",
    ];

    /// Indexed mutable access matching `NAMES` order — replaces Zig
    /// `@field(lockfile.scripts, Lockfile.Scripts.names[i])`.
    pub fn hook_mut(&mut self, i: usize) -> &mut Vec<Box<[u8]>> {
        match i {
            0 => &mut self.preinstall,
            1 => &mut self.install,
            2 => &mut self.postinstall,
            3 => &mut self.preprepare,
            4 => &mut self.prepare,
            5 => &mut self.postprepare,
            _ => unreachable!(),
        }
    }

    /// (name, &entries) in `NAMES` order — single source of truth for the name half.
    /// Rust has no `@field(self, name)`; the field-ref half stays hand-listed,
    /// but the string half is derived from `NAMES` so the literals exist exactly once.
    fn fields(&self) -> [(&'static str, &Vec<Box<[u8]>>); 6] {
        [
            (Self::NAMES[0], &self.preinstall),
            (Self::NAMES[1], &self.install),
            (Self::NAMES[2], &self.postinstall),
            (Self::NAMES[3], &self.preprepare),
            (Self::NAMES[4], &self.prepare),
            (Self::NAMES[5], &self.postprepare),
        ]
    }

    pub fn has_any(&self) -> bool {
        for (_, list) in self.fields() {
            if !list.is_empty() {
                return true;
            }
        }
        false
    }

    pub fn count(&self) -> usize {
        let mut res: usize = 0;
        for (_, list) in self.fields() {
            res += list.len();
        }
        res
    }
}

// `deinit` becomes `Drop` — body only frees owned fields → delete entirely; Vec<Box<[u8]>> drops automatically.

// ────────────────────────────────────────────────────────────────────────────
// LoadResult
// ────────────────────────────────────────────────────────────────────────────

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum LockfileFormat {
    Text,
    Binary,
}

impl LockfileFormat {
    pub fn filename(self) -> &'static ZStr {
        match self {
            LockfileFormat::Text => zstr!("bun.lock"),
            LockfileFormat::Binary => zstr!("bun.lockb"),
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum LoadStep {
    OpenFile,
    ReadFile,
    ParseFile,
    Migrating,
}

#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub enum Migrated {
    #[default]
    None,
    Npm,
    Yarn,
    Pnpm,
}

pub struct LoadResultErr {
    pub step: LoadStep,
    pub value: BunError,
    pub lockfile_path: &'static ZStr,
    pub format: LockfileFormat,
}

pub struct LoadResultOk<'a> {
    pub lockfile: &'a mut Lockfile,
    pub loaded_from_binary_lockfile: bool,
    pub migrated: Migrated,
    pub serializer_result: Serializer::SerializerLoadResult,
    pub format: LockfileFormat,
}

pub enum LoadResult<'a> {
    NotFound,
    Err(LoadResultErr),
    Ok(LoadResultOk<'a>),
}

impl<'a> LoadResult<'a> {
    pub fn loaded_from_text_lockfile(&self) -> bool {
        match self {
            LoadResult::NotFound => false,
            LoadResult::Err(err) => err.format == LockfileFormat::Text,
            LoadResult::Ok(ok) => ok.format == LockfileFormat::Text,
        }
    }

    pub fn loaded_from_binary_lockfile(&self) -> bool {
        match self {
            LoadResult::NotFound => false,
            LoadResult::Err(err) => err.format == LockfileFormat::Binary,
            LoadResult::Ok(ok) => ok.format == LockfileFormat::Binary,
        }
    }

    pub fn migrated_from_npm(&self) -> bool {
        match self {
            LoadResult::Ok(ok) => ok.migrated == Migrated::Npm,
            _ => false,
        }
    }

    pub fn save_format(&self, options: &PackageManagerOptions) -> LockfileFormat {
        match self {
            LoadResult::NotFound => {
                // saving a lockfile for a new project. default to text lockfile
                // unless saveTextLockfile is false in bunfig
                let save_text_lockfile = options.save_text_lockfile.unwrap_or(true);
                if save_text_lockfile {
                    LockfileFormat::Text
                } else {
                    LockfileFormat::Binary
                }
            }
            LoadResult::Err(err) => {
                // an error occurred, but we still loaded from an existing lockfile
                if let Some(save_text_lockfile) = options.save_text_lockfile {
                    if save_text_lockfile {
                        return LockfileFormat::Text;
                    }
                }
                err.format
            }
            LoadResult::Ok(ok) => {
                // loaded from an existing lockfile
                if let Some(save_text_lockfile) = options.save_text_lockfile {
                    if save_text_lockfile {
                        return LockfileFormat::Text;
                    }

                    if ok.migrated != Migrated::None {
                        return LockfileFormat::Binary;
                    }
                }

                if ok.migrated != Migrated::None {
                    return LockfileFormat::Text;
                }

                ok.format
            }
        }
    }

    /// configVersion and boolean for if the configVersion previously existed/needs to be saved to lockfile
    pub fn choose_config_version(&self) -> (ConfigVersion, bool) {
        match self {
            LoadResult::NotFound | LoadResult::Err(_) => (ConfigVersion::CURRENT, true),
            LoadResult::Ok(ok) => match ok.migrated {
                Migrated::None => {
                    if let Some(config_version) = ok.lockfile.saved_config_version {
                        return (config_version, false);
                    }

                    // existing bun project without configVersion
                    (ConfigVersion::V0, true)
                }
                Migrated::Pnpm => (ConfigVersion::V1, true),
                Migrated::Npm => (ConfigVersion::V0, true),
                Migrated::Yarn => (ConfigVersion::V0, true),
            },
        }
    }

    // Zig: `load_lockfile.ok` field projection (src/install/lockfile.zig).
    // Callers reach this only after `handleLoadLockfileErrors` has exited on
    // the `NotFound`/`Err` arms, so the variant is known-`Ok`.
    bun_core::enum_unwrap!(pub LoadResult, Ok => fn ok / ok_mut -> LoadResultOk<'a>);
}

// ────────────────────────────────────────────────────────────────────────────
// InstallResult
// ────────────────────────────────────────────────────────────────────────────

// ────────────────────────────────────────────────────────────────────────────
// Lockfile impl — load / clone / hoist / etc.
// ────────────────────────────────────────────────────────────────────────────

impl Lockfile {
    pub fn is_empty(&self) -> bool {
        self.packages.len() == 0
            || (self.packages.len() == 1 && self.packages.get(0).resolutions.len == 0)
    }

    pub fn load_from_cwd<'a, const ATTEMPT_LOADING_FROM_OTHER_LOCKFILE: bool>(
        &'a mut self,
        manager: Option<&mut PackageManager>,
        log: &mut bun_ast::Log,
    ) -> LoadResult<'a> {
        self.load_from_dir::<ATTEMPT_LOADING_FROM_OTHER_LOCKFILE>(Fd::cwd(), manager, log)
    }

    pub fn load_from_dir<'a, const ATTEMPT_LOADING_FROM_OTHER_LOCKFILE: bool>(
        &'a mut self,
        dir: Fd,
        manager: Option<&mut PackageManager>,
        log: &mut bun_ast::Log,
    ) -> LoadResult<'a> {
        // Zig: `bun.assert(FileSystem.instance_loaded);`
        // SAFETY: read of a process-global flag; matches Zig's bare global read.
        debug_assert!(Fs::INSTANCE_LOADED.load(core::sync::atomic::Ordering::Relaxed));

        let mut lockfile_format = LockfileFormat::Text;
        let file: File = 'file: {
            match File::openat(dir, zstr!("bun.lock"), sys::O::RDONLY, 0) {
                sys::Result::Ok(f) => break 'file f,
                sys::Result::Err(text_open_err) => {
                    if text_open_err.errno != sys::SystemErrno::ENOENT as u16 {
                        return LoadResult::Err(LoadResultErr {
                            step: LoadStep::OpenFile,
                            value: BunError::from(text_open_err),
                            lockfile_path: zstr!("bun.lock"),
                            format: LockfileFormat::Text,
                        });
                    }

                    lockfile_format = LockfileFormat::Binary;

                    match File::openat(dir, zstr!("bun.lockb"), sys::O::RDONLY, 0) {
                        sys::Result::Ok(f) => break 'file f,
                        sys::Result::Err(binary_open_err) => {
                            if binary_open_err.errno != sys::SystemErrno::ENOENT as u16 {
                                return LoadResult::Err(LoadResultErr {
                                    step: LoadStep::OpenFile,
                                    value: BunError::from(binary_open_err),
                                    lockfile_path: zstr!("bun.lockb"),
                                    format: LockfileFormat::Binary,
                                });
                            }

                            if ATTEMPT_LOADING_FROM_OTHER_LOCKFILE {
                                if let Some(pm) = manager {
                                    // Zig assigns `lockfile_format = .text` on `.ok` here,
                                    // but the local is dead past `return migrate_result` —
                                    // the format is carried inside the `LoadResult` itself.
                                    return migration::detect_and_load_other_lockfile(
                                        self, dir, pm, log,
                                    );
                                }
                            }

                            return LoadResult::NotFound;
                        }
                    }
                }
            }
        };

        // Zig: `file.readToEnd(allocator).unwrap() catch |err| ...`.
        // The live `bun_sys::File::read_to_end` returns `Maybe<Vec<u8>>`
        // (fstat-presized, pread-from-0); map the error arm to `.read_file`.
        let buf = match file.read_to_end() {
            Ok(bytes) => bytes,
            Err(e) => {
                return LoadResult::Err(LoadResultErr {
                    step: LoadStep::ReadFile,
                    value: BunError::from(e),
                    lockfile_path: if lockfile_format == LockfileFormat::Text {
                        zstr!("bun.lock")
                    } else {
                        zstr!("bun.lockb")
                    },
                    format: lockfile_format,
                });
            }
        };

        if lockfile_format == LockfileFormat::Text {
            let source = bun_ast::Source::init_path_string_owned(b"bun.lock", buf);
            initialize_store();
            let bump = bun_alloc::Arena::new();
            let json = match JSON::parse_package_json_utf8(&source, log, &bump) {
                Ok(j) => j,
                Err(e) => {
                    return LoadResult::Err(LoadResultErr {
                        step: LoadStep::ParseFile,
                        value: e,
                        lockfile_path: zstr!("bun.lock"),
                        format: lockfile_format,
                    });
                }
            };

            if let Err(e) =
                TextLockfile::parse_into_binary_lockfile(self, json, &source, log, manager)
            {
                if matches!(e, TextLockfile::ParseError::OutOfMemory) {
                    bun_core::out_of_memory();
                }
                return LoadResult::Err(LoadResultErr {
                    step: LoadStep::ParseFile,
                    value: BunError::from(e),
                    lockfile_path: zstr!("bun.lock"),
                    format: lockfile_format,
                });
            }

            bun_core::analytics::Features::text_lockfile_inc();

            return LoadResult::Ok(LoadResultOk {
                lockfile: self,
                serializer_result: Serializer::SerializerLoadResult::default(),
                loaded_from_binary_lockfile: false,
                migrated: Migrated::None,
                format: lockfile_format,
            });
        }

        // TODO(port): borrowck — `self` is reborrowed inside `result` via &'a mut Lockfile.
        // PORT NOTE: reshaped for borrowck — the debug round-trip block below mutates `self`
        // through `result.ok.lockfile` which already holds the &mut. The `BUN_DEBUG_TEST_
        // TEXT_LOCKFILE` round-trip path (lockfile.zig:364-406) needs simultaneous access
        // to `manager` (already moved into the call above for `Option<&mut PackageManager>`)
        // and the `&mut Lockfile` inside `result`. Restoring it requires the
        // `manager.as_deref_mut()` reborrow which today's `Option<&mut PackageManager>`
        // surface forbids. Until reconciler-6, the debug round-trip is omitted.
        // TODO(port): re-enable BUN_DEBUG_TEST_TEXT_LOCKFILE round-trip once borrowck reshape lands.
        self.load_from_bytes(manager, buf, log)
    }

    pub fn load_from_bytes<'a>(
        &'a mut self,
        pm: Option<&mut PackageManager>,
        buf: Vec<u8>,
        log: &mut bun_ast::Log,
    ) -> LoadResult<'a> {
        let mut stream = Stream::new(buf);
        // TODO(port): Stream{ .buffer = buf, .pos = 0 }

        self.format = FormatVersion::current();
        self.scripts = Scripts::default();
        self.trusted_dependencies = None;
        self.workspace_paths = NameHashMap::default();
        self.workspace_versions = VersionHashMap::default();
        self.overrides = OverrideMap::default();
        self.catalogs = CatalogMap::default();
        self.patched_dependencies = PatchedDependenciesMap::default();

        let load_result = match Serializer::load(self, &mut stream, log, pm) {
            Ok(r) => r,
            Err(e) => {
                return LoadResult::Err(LoadResultErr {
                    step: LoadStep::ParseFile,
                    value: e,
                    lockfile_path: zstr!("bun.lockb"),
                    format: LockfileFormat::Binary,
                });
            }
        };

        if cfg!(debug_assertions) {
            self.verify_data().expect("lockfile data is corrupt");
        }

        LoadResult::Ok(LoadResultOk {
            lockfile: self,
            serializer_result: load_result,
            loaded_from_binary_lockfile: true,
            migrated: Migrated::None,
            format: LockfileFormat::Binary,
        })
    }

    pub fn is_resolved_dependency_disabled(
        &self,
        dep_id: DependencyID,
        features: Features,
        meta: &package::Meta,
        cpu: Npm::Architecture,
        os: Npm::OperatingSystem,
    ) -> bool {
        if meta.is_disabled(cpu, os) {
            return true;
        }

        let dep = &self.buffers.dependencies[dep_id as usize];

        dep.behavior.is_bundled() || !dep.behavior.is_enabled(features)
    }

    /// This conditionally clones the lockfile with root packages marked as non-resolved
    /// that do not satisfy `Features`. The package may still end up installed even
    /// if it was e.g. in "devDependencies" and its a production install. In that case,
    /// it would be installed because another dependency or transient dependency needed it.
    ///
    /// Warning: This potentially modifies the existing lockfile in-place. That is
    /// safe to do because at this stage, the lockfile has already been saved to disk.
    /// Our in-memory representation is all that's left.
    pub fn maybe_clone_filtering_root_packages<'a>(
        old: &'a mut Lockfile,
        manager: &'a mut PackageManager,
        features: Features,
        exact_versions: bool,
        log_level: LogLevel,
    ) -> Result<Cleaned<'a>, BunError> {
        // TODO(port): narrow error set
        let old_packages = old.packages.slice();
        let old_dependencies_lists = old_packages.items_dependencies();
        let old_resolutions_lists = old_packages.items_resolutions();
        let old_resolutions = old_packages.items_resolution();
        let mut any_changes = false;
        let end: PackageID = old.packages.len() as PackageID; // @truncate

        // set all disabled dependencies of workspaces to `invalid_package_id`
        for package_id in 0..end as usize {
            if package_id != 0 && old_resolutions[package_id].tag != ResolutionTag::Workspace {
                continue;
            }

            let old_workspace_dependencies_list = old_dependencies_lists[package_id];
            let old_workspace_resolutions_list = old_resolutions_lists[package_id];

            let old_workspace_dependencies =
                old_workspace_dependencies_list.get(old.buffers.dependencies.as_slice());
            let old_workspace_resolutions =
                old_workspace_resolutions_list.mut_(old.buffers.resolutions.as_mut_slice());

            debug_assert_eq!(
                old_workspace_dependencies.len(),
                old_workspace_resolutions.len()
            );
            for (dependency, resolution) in old_workspace_dependencies
                .iter()
                .zip(old_workspace_resolutions.iter_mut())
            {
                if !dependency.behavior.is_enabled(features) && *resolution < end {
                    *resolution = invalid_package_id;
                    any_changes = true;
                }
            }
        }

        if !any_changes {
            return Ok(Cleaned::Same(old));
        }

        old.clean(manager, &mut [], exact_versions, log_level)
            .map(Cleaned::New)
    }

    fn preprocess_update_requests(
        old: &mut Lockfile,
        manager: &mut PackageManager,
        updates: &mut [UpdateRequest],
        exact_versions: bool,
    ) -> Result<(), BunError> {
        // TODO(port): narrow error set
        let workspace_package_id = manager
            .root_package_id
            .get(old, manager.workspace_name_hash);
        let root_deps_list: DependencySlice =
            old.packages.items_dependencies()[workspace_package_id as usize];

        if (root_deps_list.off as usize) < old.buffers.dependencies.len() {
            // PORT NOTE: split-borrow — `string_builder!` only takes
            // `old.buffers.string_bytes` + `old.string_pool`, leaving
            // `old.packages` / `old.buffers.{dependencies,resolutions}` free.
            let mut string_builder = string_builder!(old);

            {
                let root_deps: &[Dependency] =
                    root_deps_list.get(old.buffers.dependencies.as_slice());
                let old_resolutions_list =
                    old.packages.items_resolutions()[workspace_package_id as usize];
                let old_resolutions: &[PackageID] =
                    old_resolutions_list.get(old.buffers.resolutions.as_slice());
                let resolutions_of_yore: &[Resolution] = old.packages.items_resolution();
                let packages_len = old.packages.len();

                for update in updates.iter() {
                    if update.package_id == invalid_package_id {
                        debug_assert_eq!(root_deps.len(), old_resolutions.len());
                        for (dep, &old_resolution) in root_deps.iter().zip(old_resolutions.iter()) {
                            if dep.name_hash == SemverStringBuilder::string_hash(update.name) {
                                if old_resolution as usize >= packages_len {
                                    continue;
                                }
                                let res = resolutions_of_yore[old_resolution as usize];
                                if res.tag != ResolutionTag::Npm
                                    || update.version.tag != dependency::Tag::DistTag
                                {
                                    continue;
                                }

                                // TODO(dylan-conway): this will need to handle updating dependencies (exact, ^, or ~) and aliases

                                // PORT NOTE: Zig's `switch (exact_versions) { else => |exact| ... }` is just a
                                // way to capture a comptime-ish bool; in Rust we use it directly.
                                let npm_ver = res.npm().version;
                                let len = bun_core::fmt::count(format_args!(
                                    "{}{}",
                                    if exact_versions { "" } else { "^" },
                                    npm_ver.fmt(string_builder.string_bytes.as_slice()),
                                ));

                                if len >= SemverString::MAX_INLINE_LEN {
                                    string_builder.cap += len;
                                }
                            }
                        }
                    }
                }
            }

            string_builder.allocate()?;
            // Spec lockfile.zig:507 is `defer string_builder.clamp();` — runs once after the
            // entire second loop completes. A scopeguard would mutably capture
            // `string_builder`, conflicting with the `append` calls below. Call `clamp()`
            // explicitly at the end of this block instead (the inner loop has no `?` exits;
            // the only fallible call above is `allocate()`, which precedes this point).

            {
                let mut temp_buf = [0u8; 513];

                let root_deps: &mut [Dependency] =
                    root_deps_list.mut_(old.buffers.dependencies.as_mut_slice());
                let old_resolutions_list_lists = old.packages.items_resolutions();
                let old_resolutions_list =
                    old_resolutions_list_lists[workspace_package_id as usize];
                let old_resolutions: &[PackageID] =
                    old_resolutions_list.get(old.buffers.resolutions.as_slice());
                let resolutions_of_yore: &[Resolution] = old.packages.items_resolution();
                let packages_len = old.packages.len();

                for update in updates.iter_mut() {
                    if update.package_id == invalid_package_id {
                        debug_assert_eq!(root_deps.len(), old_resolutions.len());
                        for (dep, &old_resolution) in
                            root_deps.iter_mut().zip(old_resolutions.iter())
                        {
                            if dep.name_hash == SemverStringBuilder::string_hash(update.name) {
                                if old_resolution as usize >= packages_len {
                                    continue;
                                }
                                let res = resolutions_of_yore[old_resolution as usize];
                                if res.tag != ResolutionTag::Npm
                                    || update.version.tag != dependency::Tag::DistTag
                                {
                                    continue;
                                }

                                // TODO(dylan-conway): this will need to handle updating dependencies (exact, ^, or ~) and aliases

                                let npm_ver = res.npm().version;
                                let buf = {
                                    let mut cursor: &mut [u8] = &mut temp_buf[..];
                                    let start_len = cursor.len();
                                    if write!(
                                        cursor,
                                        "{}{}",
                                        if exact_versions { "" } else { "^" },
                                        npm_ver.fmt(string_builder.string_bytes.as_slice()),
                                    )
                                    .is_err()
                                    {
                                        // Zig: `catch break` — breaks the inner for-loop.
                                        break;
                                    }
                                    let written = start_len - cursor.len();
                                    &temp_buf[..written]
                                };

                                let external_version = string_builder.append::<ExternalString>(buf);
                                let sliced = external_version
                                    .value
                                    .sliced(string_builder.string_bytes.as_slice());
                                dep.version = dependency::parse(
                                    dep.name,
                                    dep.name_hash,
                                    sliced.slice,
                                    &sliced,
                                    None,
                                    &mut *manager,
                                )
                                .unwrap_or_default();
                            }
                        }
                    }

                    update.e_string = None;
                }
            }

            string_builder.clamp();
        }
        Ok(())
    }

    pub fn clean(
        &mut self,
        manager: &mut PackageManager,
        updates: &mut [UpdateRequest],
        exact_versions: bool,
        log_level: LogLevel,
    ) -> Result<Box<Lockfile>, BunError> {
        // TODO(port): narrow error set
        // This is wasteful, but we rarely log anything so it's fine.
        let mut log = bun_ast::Log::init();
        // defer { for (...) item.deinit(); log.deinit(); } — handled by Drop

        self.clean_with_logger(manager, updates, &mut log, exact_versions, log_level)
    }

    pub fn resolve_catalog_dependency(&self, dep: &Dependency) -> Option<DependencyVersion> {
        if dep.version.tag != dependency::Tag::Catalog {
            return Some(dep.version.clone());
        }

        let catalog_name = *dep.version.catalog();
        let catalog_dep = self.catalogs.get(self, catalog_name, dep.name)?;

        Some(catalog_dep.version)
    }

    /// Is this a direct dependency of the workspace root package.json?
    pub fn is_workspace_root_dependency(&self, id: DependencyID) -> bool {
        self.packages.items_dependencies()[0].contains(id)
    }

    /// Is this a direct dependency of the workspace the install is taking place in?
    pub fn is_root_dependency(&self, manager: &mut PackageManager, id: DependencyID) -> bool {
        // Zig: `manager: *PackageManager` — `RootPackageId::get` caches into `manager`.
        let root_id = manager
            .root_package_id
            .get(self, manager.workspace_name_hash);
        self.packages.items_dependencies()[root_id as usize].contains(id)
    }

    /// Is this a direct dependency of any workspace (including workspace root)?
    /// TODO make this faster by caching the workspace package ids
    pub fn is_workspace_dependency(&self, id: DependencyID) -> bool {
        self.get_workspace_pkg_if_workspace_dep(id) != invalid_package_id
    }

    pub fn get_workspace_pkg_if_workspace_dep(&self, id: DependencyID) -> PackageID {
        let packages = self.packages.slice();
        let resolutions = packages.items_resolution();
        let dependencies_lists = packages.items_dependencies();
        for (pkg_id, (resolution, dependencies)) in resolutions
            .iter()
            .zip(dependencies_lists.iter())
            .enumerate()
        {
            if resolution.tag != ResolutionTag::Workspace && resolution.tag != ResolutionTag::Root {
                continue;
            }
            if dependencies.contains(id) {
                return PackageID::try_from(pkg_id).expect("int cast");
            }
        }

        invalid_package_id
    }

    /// Does this tree id belong to a workspace (including workspace root)?
    /// TODO(dylan-conway) fix!
    pub fn is_workspace_tree_id(&self, id: tree::Id) -> bool {
        id == 0
            || self.buffers.dependencies[self.buffers.trees[id as usize].dependency_id as usize]
                .behavior
                .is_workspace()
    }

    /// Returns the package id of the workspace the install is taking place in.
    pub fn get_workspace_package_id(
        &self,
        workspace_name_hash: Option<PackageNameHash>,
    ) -> PackageID {
        if let Some(workspace_name_hash_) = workspace_name_hash {
            let packages = self.packages.slice();
            let name_hashes = packages.items_name_hash();
            let resolutions = packages.items_resolution();
            for (i, (res, name_hash)) in resolutions.iter().zip(name_hashes.iter()).enumerate() {
                if res.tag == ResolutionTag::Workspace && *name_hash == workspace_name_hash_ {
                    return PackageID::try_from(i).expect("int cast");
                }
            }

            // should not hit this, default to root just in case
            0
        } else {
            0
        }
    }

    // `#[inline(never)]` keeps the panic/format machinery from
    // `bun_core::output` (pulled in by the cold helpers below) out of callers;
    // the hot copy/remap loop stays in this body while the three cold sections
    // — update-request preprocessing, verbose timer reporting, and the
    // trusted/patched-dependency migration — are outlined so a no-change
    // `bun install` (install/fastify bench) does not page them in.
    #[inline(never)]
    pub fn clean_with_logger(
        &mut self,
        manager: &mut PackageManager,
        updates: &mut [UpdateRequest],
        log: &mut bun_ast::Log,
        exact_versions: bool,
        log_level: LogLevel,
    ) -> Result<Box<Lockfile>, BunError> {
        // TODO(port): narrow error set
        // Zig names the receiver `old`; alias `self` so the body reads
        // identically to the spec (lockfile.zig:637).
        let old: &mut Lockfile = self;
        // Zig: `var timer: std.time.Timer = undefined;` — model the
        // uninitialized sentinel with `Option`. Outlined cold: the verbose arm
        // is debug-only and drags in `Timer`/clock-syscall error formatting.
        let timer: Option<Timer> = if log_level.is_verbose() {
            Some(clean_verbose_timer_start()?)
        } else {
            None
        };

        let old_trusted_dependencies = old.trusted_dependencies.take();
        let old_scripts = core::mem::take(&mut old.scripts);
        // We will only shrink the number of packages here.
        // never grow

        // preinstall_state is used during installPackages. the indexes(package ids) need
        // to be remapped. Also ensure `preinstall_state` has enough capacity to contain
        // all packages. It's possible it doesn't because non-npm packages do not use
        // preinstall state before linking stage.
        manager.ensure_preinstall_state_list_capacity(old.packages.len());
        let preinstall_state = &mut manager.preinstall_state;
        let old_preinstall_state = preinstall_state.clone();
        preinstall_state.fill(Install::PreinstallState::Unknown);

        if !updates.is_empty() {
            clean_preprocess_update_requests_cold(old, manager, updates, exact_versions)?;
        }

        // Spec lockfile.zig:669: `var new = try old.allocator.create(Lockfile)` — caller owns
        // and later frees via `deinit`. PORTING.md §Forbidden patterns bans `Box::leak` to
        // satisfy a lifetime; return `Box<Lockfile>` so Drop reclaims it.
        let mut new: Box<Lockfile> = Box::default();
        new.string_pool
            .ensure_total_capacity(old.string_pool.capacity())?;
        new.package_index
            .ensure_total_capacity(old.package_index.capacity())?;
        new.packages.ensure_total_capacity(old.packages.len())?;
        new.buffers.preallocate(&old.buffers)?;
        new.patched_dependencies
            .ensure_total_capacity(old.patched_dependencies.count())?;

        // Zig: `old.scratch.dependency_list_queue.head = 0;` — reset the FIFO read
        // cursor without discarding capacity. `LinearFifo::head` is private; the
        // queue is always drained to empty before reuse here, so a `discard(count)`
        // resets `head` to 0 with the same observable effect (lockfile.zig:681).
        let queued = old.scratch.dependency_list_queue.readable_length();
        old.scratch.dependency_list_queue.discard(queued);

        {
            // PORT NOTE: reshaped for borrowck. Zig holds `&old.overrides` /
            // `&old.catalogs` while also passing `*Lockfile old` and
            // `*Lockfile new` (the latter aliased again inside `builder`).
            // The Rust signatures take `&Lockfile` for `old` and read `new`
            // through `builder.lockfile`, so the only conflict left is the
            // field-assign on `new.*` while `builder` borrows `new` — store
            // the results in temps and assign after `builder` drops.
            let old_buf = old.buffers.string_bytes.as_slice();
            let (mut builder, lf) = new.string_builder_split();
            old.overrides.count(old_buf, &mut builder);
            old.catalogs.count(old_buf, &mut builder);
            builder.allocate()?;
            *lf.overrides = old.overrides.clone(manager, old_buf, &mut builder)?;
            *lf.catalogs = old.catalogs.clone(manager, old_buf, &mut builder)?;
        }

        // Step 1. Recreate the lockfile with only the packages that are still alive
        let root = old.root_package().ok_or_else(|| err!("NoPackage"))?;

        let mut package_id_mapping = vec![invalid_package_id; old.packages.len()];
        let clone_queue_ = PendingResolutions::new();
        // PORT NOTE: explicit `&mut *` reborrows so `old`/`manager`/`new` are
        // released back to this scope once `cloner` is dropped.
        let mut cloner = Cloner {
            old: &mut *old,
            lockfile: &mut *new,
            mapping: &mut package_id_mapping,
            clone_queue: clone_queue_,
            log,
            old_preinstall_state,
            manager: &mut *manager,
            trees: tree::List::default(),
            trees_count: 1,
        };

        // try clone_queue.ensureUnusedCapacity(root.dependencies.len);
        let _ = root.clone(&mut cloner)?;

        // PORT NOTE: between here and `cloner.flush()`, `old`/`new`/`manager`
        // are live inside `cloner`. Reach them via `cloner.old` /
        // `cloner.lockfile` so borrowck sees disjoint field paths.
        {
            let old = &mut *cloner.old;
            let new = &mut *cloner.lockfile;

            // Clone workspace_paths and workspace_versions at the end.
            if old.workspace_paths.count() > 0 || old.workspace_versions.count() > 0 {
                new.workspace_paths
                    .ensure_total_capacity(old.workspace_paths.count())?;
                new.workspace_versions
                    .ensure_total_capacity(old.workspace_versions.count())?;

                // Field-level split borrow of `new` (string_bytes + string_pool).
                let mut workspace_paths_builder = string_builder!(new);

                // Sort by name for determinism
                // PORT NOTE: Zig defines a local `WorkspacePathSorter` struct; in Rust we use a closure.
                {
                    let string_buf = old.buffers.string_bytes.as_slice();
                    // `ArrayHashMap::sort` mirrors Zig's `entries.sort(ctx)` —
                    // `(keys, values, a, b) -> bool` (less-than).
                    old.workspace_paths.sort(|_keys, values, a, b| {
                        let left = values[a];
                        let right = values[b];
                        strings::order(left.slice(string_buf), right.slice(string_buf))
                            == Ordering::Less
                    });
                }

                let old_string_buf = old.buffers.string_bytes.as_slice();
                for path in old.workspace_paths.values() {
                    workspace_paths_builder.count(path.slice(old_string_buf));
                }
                let versions: &[Semver::Version] = old.workspace_versions.values();
                for version in versions {
                    version.count(old_string_buf, &mut workspace_paths_builder);
                }

                workspace_paths_builder.allocate()?;

                // SAFETY: capacity reserved by `ensure_total_capacity` above; every
                // slot in `0..old.count()` is overwritten by the copy/zip loops below
                // before `re_index()` reads them. Mirrors Zig `entries.len = n`.
                unsafe {
                    new.workspace_paths
                        .set_entries_len(old.workspace_paths.count())
                };

                debug_assert_eq!(
                    old.workspace_paths.values().len(),
                    new.workspace_paths.values().len()
                );
                for (src, dest) in old
                    .workspace_paths
                    .values()
                    .iter()
                    .zip(new.workspace_paths.values_mut().iter_mut())
                {
                    *dest =
                        workspace_paths_builder.append::<SemverString>(src.slice(old_string_buf));
                }
                new.workspace_paths
                    .keys_mut()
                    .copy_from_slice(old.workspace_paths.keys());

                new.workspace_versions
                    .ensure_total_capacity(old.workspace_versions.count())?;
                // SAFETY: capacity reserved immediately above; every slot is filled by
                // the zip loop below before `re_index()`. Mirrors Zig `entries.len = n`.
                unsafe {
                    new.workspace_versions
                        .set_entries_len(old.workspace_versions.count())
                };
                for (src, dest) in versions
                    .iter()
                    .zip(new.workspace_versions.values_mut().iter_mut())
                {
                    *dest = src.append(old_string_buf, &mut workspace_paths_builder);
                }

                new.workspace_versions
                    .keys_mut()
                    .copy_from_slice(old.workspace_versions.keys());

                workspace_paths_builder.clamp();

                new.workspace_versions.re_index()?;
                new.workspace_paths.re_index()?;
            }
        }

        // When you run `"bun add react"
        // This is where we update it in the lockfile from "latest" to "^17.0.2"
        cloner.flush()?;
        // `cloner` no longer needed — release the reborrows of `old`/`new`/`manager`.
        drop(cloner);

        new.trusted_dependencies = old_trusted_dependencies;
        new.scripts = old_scripts;
        new.meta_hash = old.meta_hash;

        if old.patched_dependencies.count() > 0 {
            clean_migrate_patched_dependencies_cold(old, &mut new)?;
        }

        // Don't allow invalid memory to happen
        if !updates.is_empty() {
            // `UpdateRequest.version_buf` is a raw `*const [u8]` (PORTING.md
            // type-map: `[]const u8` struct-field, ARENA-class). The slice
            // points into `new.buffers.string_bytes`; `new` is *returned* to
            // the caller below and `string_bytes` is finalized at this point
            // (cloner.flush() and the patched-dep StringBuilder have both
            // run), so the storage outlives every `UpdateRequest` the caller
            // threads it through. No lifetime extension — store the raw
            // (ptr, len) and let `UpdateRequest::version_buf()` reborrow at
            // each read site.
            let string_buf = new.buffers.string_bytes.as_slice();
            let string_buf_ptr = bun_ptr::RawSlice::new(string_buf);
            let slice = new.packages.slice();

            // updates might be applied to the root package.json or one
            // of the workspace package.json files.
            let workspace_package_id = manager
                .root_package_id
                .get(&new, manager.workspace_name_hash);

            let dep_list = slice.items_dependencies()[workspace_package_id as usize];
            let res_list = slice.items_resolutions()[workspace_package_id as usize];
            let workspace_deps: &[Dependency] = dep_list.get(new.buffers.dependencies.as_slice());
            let resolved_ids: &[PackageID] = res_list.get(new.buffers.resolutions.as_slice());

            'request_updated: for update in updates.iter_mut() {
                if update.package_id == invalid_package_id {
                    debug_assert_eq!(resolved_ids.len(), workspace_deps.len());
                    for (&package_id, dep) in resolved_ids.iter().zip(workspace_deps.iter()) {
                        if update.matches(dep, string_buf) {
                            if package_id as usize > new.packages.len() {
                                continue;
                            }
                            update.version_buf = string_buf_ptr;
                            update.version = dep.version.clone();
                            update.package_id = package_id;

                            continue 'request_updated;
                        }
                    }
                }
            }
        }

        if log_level.is_verbose() {
            clean_verbose_report_cold(old, &new, timer.as_ref());
        }

        Ok(new)
    }
}

// ────────────────────────────────────────────────────────────────────────────
// clean_with_logger cold helpers — outlined so the hot copy/remap loop in the
// main body is contiguous in `.text` and the install/fastify no-change bench
// does not fault in update-request rewriting, patched-dep migration, or the
// verbose timer/format machinery.
// ────────────────────────────────────────────────────────────────────────────

#[cold]
#[inline(never)]
fn clean_preprocess_update_requests_cold(
    old: &mut Lockfile,
    manager: &mut PackageManager,
    updates: &mut [UpdateRequest],
    exact_versions: bool,
) -> Result<(), BunError> {
    Lockfile::preprocess_update_requests(old, manager, updates, exact_versions)
}

#[cold]
#[inline(never)]
fn clean_verbose_timer_start() -> Result<Timer, BunError> {
    Timer::start()
}

#[cold]
#[inline(never)]
fn clean_verbose_report_cold(old: &Lockfile, new: &Lockfile, timer: Option<&Timer>) {
    Output::pretty_errorln(format_args!(
        "Clean lockfile: {} packages -> {} packages in {}\n",
        old.packages.len(),
        new.packages.len(),
        // SAFETY: only entered when `log_level.is_verbose()`, which set `timer = Some(..)`.
        bun_core::fmt::fmt_duration_one_decimal(timer.unwrap().read()),
    ));
}

#[cold]
#[inline(never)]
fn clean_migrate_patched_dependencies_cold(
    old: &Lockfile,
    new: &mut Lockfile,
) -> Result<(), BunError> {
    let mut builder = string_builder!(new);
    for patched_dep in old.patched_dependencies.values() {
        builder.count(patched_dep.path.slice(old.buffers.string_bytes.as_slice()));
    }
    builder.allocate()?;
    for (k, v) in old
        .patched_dependencies
        .keys()
        .iter()
        .zip(old.patched_dependencies.values().iter())
    {
        debug_assert!(!v.patchfile_hash_is_null);
        let mut patchdep = *v;
        patchdep.path = builder
            .append::<SemverString>(patchdep.path.slice(old.buffers.string_bytes.as_slice()));
        new.patched_dependencies.put(*k, patchdep)?;
    }
    Ok(())
}

// ────────────────────────────────────────────────────────────────────────────
// MetaHashFormatter
// ────────────────────────────────────────────────────────────────────────────

pub struct MetaHashFormatter<'a> {
    pub meta_hash: &'a MetaHash,
}

impl<'a> fmt::Display for MetaHashFormatter<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let remain: &[u8] = &self.meta_hash[..];

        // {X}-{x}-{X}-{x} — Zig's `{X}` on a slice prints uppercase hex, `{x}` lowercase.
        // TODO(port): verify byte-slice hex formatting matches Zig std.fmt exactly.
        write!(
            f,
            "{}-{}-{}-{}",
            bun_core::fmt::HexBytes::<false>(&remain[0..8]),
            bun_core::fmt::HexBytes::<true>(&remain[8..16]),
            bun_core::fmt::HexBytes::<false>(&remain[16..24]),
            bun_core::fmt::HexBytes::<true>(&remain[24..32]),
        )
    }
}

impl Lockfile {
    pub fn fmt_meta_hash(&self) -> MetaHashFormatter<'_> {
        MetaHashFormatter {
            meta_hash: &self.meta_hash,
        }
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Cloner
// ────────────────────────────────────────────────────────────────────────────

pub struct Cloner<'a> {
    pub clone_queue: PendingResolutions,
    pub lockfile: &'a mut Lockfile,
    pub old: &'a mut Lockfile,
    pub mapping: &'a mut [PackageID],
    pub trees: tree::List,
    pub trees_count: u32,
    pub log: &'a mut bun_ast::Log,
    pub old_preinstall_state: Vec<Install::PreinstallState>,
    pub manager: &'a mut PackageManager,
}

impl<'a> Cloner<'a> {
    pub fn flush(&mut self) -> Result<(), BunError> {
        let max_package_id = self.old.packages.len();
        while let Some(to_clone) = self.clone_queue.pop() {
            let mapping = self.mapping[to_clone.old_resolution as usize];
            if (mapping as usize) < max_package_id {
                self.lockfile.buffers.resolutions[to_clone.resolve_id as usize] = mapping;
                continue;
            }

            let old_package = *self.old.packages.get(to_clone.old_resolution as usize);

            // `Package::clone` reads/writes through `cloner` exclusively.
            let new_id = old_package.clone(self)?;
            self.lockfile.buffers.resolutions[to_clone.resolve_id as usize] = new_id;
        }

        // cloning finished, items in lockfile buffer might have a different order, meaning
        // package ids and dependency ids have changed
        self.manager
            .clear_cached_items_depending_on_lockfile_buffer();

        if self.lockfile.packages.len() != 0 {
            self.lockfile.resolve(self.log)?;
        }

        // capacity is used for calculating byte size
        // so we need to make sure it's exact
        if self.lockfile.packages.capacity() != self.lockfile.packages.len()
            && self.lockfile.packages.len() > 0
        {
            self.lockfile
                .packages
                .shrink_and_free(self.lockfile.packages.len());
        }
        Ok(())
    }
}

// ────────────────────────────────────────────────────────────────────────────
// resolve / filter / hoist
// ────────────────────────────────────────────────────────────────────────────

impl Lockfile {
    pub fn resolve(&mut self, log: &mut bun_ast::Log) -> Result<(), tree::SubtreeError> {
        self.hoist::<{ tree::BuilderMethod::Resolvable }>(log, None, true, &[], None)
    }

    pub fn filter(
        &mut self,
        log: &mut bun_ast::Log,
        manager: &mut PackageManager,
        install_root_dependencies: bool,
        workspace_filters: &[WorkspaceFilter],
        packages_to_install: Option<&[PackageID]>,
    ) -> Result<(), tree::SubtreeError> {
        self.hoist::<{ tree::BuilderMethod::Filter }>(
            log,
            Some(manager),
            install_root_dependencies,
            workspace_filters,
            packages_to_install,
        )
    }

    /// Sets `buffers.trees` and `buffers.hoisted_dependencies`
    // TODO(port): Zig uses `comptime method` to make several params conditionally `void`.
    // Rust const-generic enums need #[derive(ConstParamTy)] on Tree::BuilderMethod and the
    // value-level branching can't change param types. Consider two monomorphized fns.
    pub fn hoist<const METHOD: tree::BuilderMethod>(
        &mut self,
        log: &mut bun_ast::Log,
        // PORT NOTE: Zig used `comptime method` to make these params `void` for
        // non-`.filter` builds. `tree::Builder` stores them unconditionally
        // (Option/slice), so accept the concrete shapes for all `METHOD`s.
        manager: Option<&PackageManager>,
        install_root_dependencies: bool,
        workspace_filters: &[WorkspaceFilter],
        packages_to_install: Option<&[PackageID]>,
    ) -> Result<(), tree::SubtreeError> {
        let slice = self.packages.slice();

        // PORT NOTE: `tree::Builder` stores `lockfile: ParentRef<Lockfile>` so
        // the `&mut buffers.resolutions` split-borrow below can coexist with
        // the read-only lockfile view inside the builder (see Tree.rs note).
        // `ParentRef::new` captures `SharedReadOnly` provenance from `&*self`,
        // which is exactly what `Builder` needs (it only ever `Deref`s); the
        // `Builder` does not outlive this `&mut self` borrow.
        let lockfile_ref = bun_ptr::ParentRef::<Lockfile>::new(&*self);
        let mut builder = tree::Builder::<METHOD> {
            queue: tree::TreeFiller::init(),
            resolution_lists: slice.items_resolutions(),
            resolutions: self.buffers.resolutions.as_mut_slice(),
            dependencies: self.buffers.dependencies.as_slice(),
            log,
            lockfile: lockfile_ref,
            manager,
            install_root_dependencies,
            workspace_filters,
            packages_to_install,
            pending_optional_peers: Default::default(),
            list: Default::default(),
            sort_buf: Default::default(),
        };
        // TODO(port): Tree::Builder field set may differ; verify against the Zig.

        Tree::default().process_subtree(tree::ROOT_DEP_ID, tree::INVALID_ID, &mut builder)?;

        // This goes breadth-first
        while let Some(item) = builder.queue.read_item() {
            use tree::BuilderEntryColumns as _;
            // PORT NOTE: reshaped for borrowck — Zig indexes builder.list while passing &mut builder.
            let tree = builder.list.items_tree()[item.tree_id as usize];
            tree.process_subtree(item.dependency_id, item.hoist_root_id, &mut builder)?;
            // TODO(port): `tree` may need to be a reference into builder.list, not a copy.
        }

        let cleaned = builder.clean()?;
        self.buffers.trees = cleaned.trees;
        self.buffers.hoisted_dependencies = cleaned.dep_ids;
        Ok(())
    }
}

#[derive(Clone, Copy)]
pub struct PendingResolution {
    pub old_resolution: PackageID,
    pub resolve_id: PackageID,
    pub parent: PackageID,
}

pub(crate) type PendingResolutions = Vec<PendingResolution>;

// ────────────────────────────────────────────────────────────────────────────
// fetchNecessaryPackageMetadataAfterYarnOrPnpmMigration
// ────────────────────────────────────────────────────────────────────────────

impl Lockfile {
    pub fn fetch_necessary_package_metadata_after_yarn_or_pnpm_migration<
        const UPDATE_OS_CPU: bool,
    >(
        &mut self,
        manager: &mut PackageManager,
    ) -> Result<(), AllocError> {
        if populate_manifest_cache::populate_manifest_cache(
            manager,
            populate_manifest_cache::Packages::All,
        )
        .is_err()
        {
            return Ok(());
        }
        // Otherwise the command loading this lockfile would dedupe its own fetch of a manifest this pass failed to get.
        manager.network_dedupe_map.clear();

        let cache_ctx = manager.manifest_disk_cache_ctx();
        // PORT NOTE: heavy borrowck overlap — Zig calls
        // `manager.manifests.byNameHash(manager, …)` (manifests is a field of
        // manager) and then opens a `string_builder` on `manager.lockfile`
        // while still holding `&manifest`. Route the `manifests` projection
        // through a raw root so it can coexist with the lockfile borrows;
        // lockfile fields are taken from `self` (== `manager.lockfile`), never
        // re-derived via `manager_ptr`. BACKREF `mgr_ref` wraps the same root
        // for the read-only `options` projection so it goes through safe
        // `ParentRef::Deref`.
        let manager_ptr: *mut PackageManager = manager;
        let mgr_ref = bun_ptr::ParentRef::<PackageManager>::from(
            core::ptr::NonNull::new(manager_ptr).expect("derived from &mut, non-null"),
        );
        let mut pkgs = self.packages.slice();
        let len = pkgs.len();

        // PORT NOTE: Zig takes `pkgs.items(.bin)` / `pkgs.items(.meta)` as
        // simultaneous mutable column views; `split_mut()` yields disjoint
        // `&mut [_]` per column from one `&mut Slice` borrow.
        let self::package::PackageColumnsMut {
            name: pkg_names,
            name_hash: pkg_name_hashes,
            resolution: pkg_resolutions,
            bin: pkg_bins,
            meta,
            ..
        } = pkgs.split_mut();
        // PORT NOTE: Zig has two near-identical loops gated by `update_os_cpu`;
        // collapse to one loop and bind `pkg_metas` as an empty slice when the
        // const generic is false (Zig left it `undefined`).
        let pkg_metas: &mut [self::package::meta::Meta] =
            if UPDATE_OS_CPU { meta } else { &mut [] };

        for i in 0..len {
            let pkg_name = pkg_names[i];
            let pkg_name_hash = pkg_name_hashes[i];
            let pkg_res = pkg_resolutions[i];
            let pkg_bin = &mut pkg_bins[i];

            match pkg_res.tag {
                ResolutionTag::Npm => {
                    // `options` read via BACKREF `mgr_ref` (see hoisted note
                    // above); `manifests` and `lockfile` are non-overlapping
                    // fields and nothing below resizes/relocates `manifests`
                    // while `manifest` is held.
                    let scope = mgr_ref.options.scope_for_package_name(
                        pkg_name.slice(self.buffers.string_bytes.as_slice()),
                    );
                    // SAFETY: `manifests` projected from `manager_ptr`; the
                    // call holds only that disjoint field.
                    let Some(manifest) = unsafe { &mut (*manager_ptr).manifests }.by_name_hash(
                        cache_ctx,
                        scope,
                        pkg_name_hash,
                        Install::ManifestLoad::LoadFromMemoryFallbackToDisk,
                        false,
                    ) else {
                        continue;
                    };

                    let npm_ver = pkg_res.npm().version;
                    let Some(pkg) = manifest.find_by_version(npm_ver) else {
                        continue;
                    };

                    // PORT NOTE: Zig opens the builder on `manager.lockfile`,
                    // which is the same `*Lockfile` as `this`/`self`. Re-deriving a
                    // whole `&mut Lockfile` from `manager_ptr` here would create a
                    // second mutable reference aliasing `self` (UB) — go through
                    // `self` so the field-level borrows below stay disjoint from the
                    // live `pkg_*` column views (those point into the columnar heap
                    // allocation, not the `Lockfile` struct).
                    let mut builder = string_builder!(self);

                    let mut bin_extern_strings_count: u32 = 0;

                    bin_extern_strings_count += pkg.package.bin.count(
                        &manifest.string_buf,
                        &manifest.extern_strings_bin_entries,
                        &mut builder,
                    );

                    builder.allocate()?;
                    // Spec: `defer builder.clamp()` — call explicitly at end of block (no `?`
                    // exits between here and the clamp below).

                    let extern_strings_list = &mut self.buffers.extern_strings;
                    // PERF(port): was ensureUnusedCapacity
                    let start = extern_strings_list.len();
                    // Default-fill the tail so it is valid before `bin.clone`
                    // overwrites it (replaces `reserve` + raw `set_len`).
                    bun_core::vec::grow_default(
                        extern_strings_list,
                        bin_extern_strings_count as usize,
                    );
                    let new_len = extern_strings_list.len();

                    // PORT NOTE: Zig passes both `extern_strings_list.items` (full slice)
                    // and a tail subslice to `bin.clone()`; the full slice is only used to
                    // compute the tail's offset for `ExternalStringList::init`. In Rust the
                    // two views would alias, so `Bin::clone_with_buffers` takes the offset
                    // directly.
                    let extern_strings_slice = &mut extern_strings_list[start..new_len];

                    *pkg_bin = pkg.package.bin.clone_with_buffers(
                        &manifest.string_buf,
                        &manifest.extern_strings_bin_entries,
                        start as u32,
                        extern_strings_slice,
                        &mut builder,
                    );

                    builder.clamp();

                    if UPDATE_OS_CPU {
                        let pkg_meta = &mut pkg_metas[i];
                        // Update os/cpu metadata if not already set
                        if pkg_meta.os == Npm::OperatingSystem::ALL {
                            pkg_meta.os = pkg.package.os;
                        }
                        if pkg_meta.arch == Npm::Architecture::ALL {
                            pkg_meta.arch = pkg.package.cpu;
                        }
                    }
                }
                _ => {}
            }
        }
        Ok(())
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Printer
// ────────────────────────────────────────────────────────────────────────────

/// Port of `Lockfile.Printer` (src/install/lockfile.zig).
pub struct Printer<'a> {
    pub lockfile: &'a Lockfile,
    pub options: &'a PackageManagerOptions,
    pub successfully_installed: Option<&'a DynamicBitSet>,
    pub updates: &'a [UpdateRequest],
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum PrinterFormat {
    Yarn,
}

pub mod printer {
    pub use super::printer_mods::tree_printer as Tree;
    pub use super::printer_mods::yarn as Yarn;
}

impl<'a> Printer<'a> {
    #[cold]
    pub fn print(
        log: &mut bun_ast::Log,
        input_lockfile_path: &[u8],
        format: PrinterFormat,
    ) -> Result<(), BunError> {
        // TODO(port): narrow error set

        // We truncate longer than allowed paths. We should probably throw an error instead.
        let path = &input_lockfile_path[..input_lockfile_path.len().min(MAX_PATH_BYTES)];

        let mut lockfile_path_buf1 = PathBuffer::uninit();
        let mut lockfile_path_buf2 = PathBuffer::uninit();

        let mut lockfile_path: &ZStr = ZStr::EMPTY;
        // Track which buffer backs `lockfile_path` so the chdir NUL-terminate
        // step below can write into the *other* buffer. `resolve_path::z` does
        // `output[..n].copy_from_slice(input)` (→ `ptr::copy_nonoverlapping`);
        // passing a slice of `bufN` as `input` while taking `&mut bufN` as
        // `output` is UB on overlapping ranges and would also corrupt
        // `lockfile_path` (printed in the NotFound arm). Zig's
        // `bun.sys.chdir("", dirname)` accepts a non-sentinel slice directly,
        // so it never re-buffers — the hazard is Rust-port-specific.
        let mut path_in_buf2 = false;

        if !bun_paths::is_absolute(path) {
            // Zig `bun.getcwd` returns the slice; the Rust `bun_sys::getcwd`
            // returns the length written into the caller-owned buffer.
            let cwd_len = bun_sys::getcwd(&mut lockfile_path_buf1[..])?;
            let parts = [path];
            // PORT NOTE: reshaped for borrowck — copy `cwd` out of `buf1` so the
            // join can write into `buf2` while `cwd` borrows `buf1` only.
            let cwd = &lockfile_path_buf1[..cwd_len];
            let lockfile_path__len = resolve_path::join_abs_string_buf::<platform::Auto>(
                cwd,
                &mut lockfile_path_buf2.0,
                &parts,
            )
            .len();
            lockfile_path_buf2[lockfile_path__len] = 0;
            // SAFETY: NUL written at [len] above. Not `from_buf`: borrowck
            // can't see that the `path_in_buf2` flag picks the *other* buffer
            // for the chdir scratch write below, so the borrow must be detached.
            lockfile_path =
                unsafe { ZStr::from_raw(lockfile_path_buf2.as_ptr(), lockfile_path__len) };
            path_in_buf2 = true;
        } else if !path.is_empty() {
            lockfile_path_buf1[..path.len()].copy_from_slice(path);
            lockfile_path_buf1[path.len()] = 0;
            // SAFETY: NUL written at [len] above. See note above re. borrowck.
            lockfile_path = unsafe { ZStr::from_raw(lockfile_path_buf1.as_ptr(), path.len()) };
        }

        if !lockfile_path.as_bytes().is_empty() && lockfile_path.as_bytes()[0] == SEP {
            // Zig `bun.sys.chdir("", dirname)` — first arg is error-context
            // path; the Rust `bun_sys::chdir` takes the destination only.
            let dir = bun_paths::dirname(lockfile_path.as_bytes()).unwrap_or(SEP_STR.as_bytes());
            // NUL-terminate into the buffer that does NOT back `lockfile_path`
            // (see `path_in_buf2` note above). `buf1`'s cwd contents are dead
            // after the join, so it is free for reuse here.
            let dir_z = if path_in_buf2 {
                resolve_path::z(dir, &mut lockfile_path_buf1)
            } else {
                resolve_path::z(dir, &mut lockfile_path_buf2)
            };
            let _ = sys::chdir(dir_z);
        }

        // Zig: `_ = try FileSystem.init(null);` — bootstraps the resolver FS
        // singleton. `Printer::print` is an entry point (`bun bun.lockb`), so
        // the singleton may not exist yet.
        let _ = FileSystem::init(None)?;

        let mut lockfile = Box::<Lockfile>::default();

        let load_from_disk = lockfile.load_from_cwd::<false>(None, log);
        match load_from_disk {
            crate::lockfile::LoadResult::Err(cause) => {
                match cause.step {
                    crate::lockfile::LoadStep::OpenFile => Output::pretty_errorln(format_args!(
                        "<r><red>error<r> opening lockfile:<r> {}.",
                        cause.value.name()
                    )),
                    crate::lockfile::LoadStep::ParseFile => Output::pretty_errorln(format_args!(
                        "<r><red>error<r> parsing lockfile:<r> {}",
                        cause.value.name()
                    )),
                    crate::lockfile::LoadStep::ReadFile => Output::pretty_errorln(format_args!(
                        "<r><red>error<r> reading lockfile:<r> {}",
                        cause.value.name()
                    )),
                    crate::lockfile::LoadStep::Migrating => Output::pretty_errorln(format_args!(
                        "<r><red>error<r> while migrating lockfile:<r> {}",
                        cause.value.name()
                    )),
                }
                if log.errors > 0 {
                    // `IntoLogWrite` is implemented for `*mut bun_core::io::Writer`,
                    // not `&mut &mut Writer` — pass the raw vtable pointer.
                    let ew: *mut bun_core::io::Writer = Output::error_writer();
                    log.print(ew)?;
                }
                Global::crash();
            }
            crate::lockfile::LoadResult::NotFound => {
                Output::pretty_errorln(format_args!(
                    "<r><red>lockfile not found:<r> {}",
                    bun_core::fmt::QuotedFormatter {
                        text: lockfile_path.as_bytes()
                    },
                ));
                Global::crash();
            }
            crate::lockfile::LoadResult::Ok(_) => {}
        }

        let writer = Output::writer_buffered();
        match Self::print_with_lockfile(&lockfile, format, writer) {
            Ok(()) => {}
            Err(e) if e == err!("OutOfMemory") => bun_core::out_of_memory(),
            Err(e) if e == err!("BrokenPipe") || e == err!("WriteFailed") => return Ok(()),
            Err(e) => return Err(e),
        }
        Output::flush();
        Ok(())
    }

    pub fn print_with_lockfile<W: bun_io::Write>(
        lockfile: &Lockfile,
        format: PrinterFormat,
        writer: W,
    ) -> Result<(), BunError> {
        // TODO(port): narrow error set
        // `FileSystem::init` ran in the caller (`Printer::print`); this is the
        // process-static singleton (Zig `&FileSystem.instance`). Single-threaded
        // CLI path, no concurrent access.
        let fs = FileSystem::instance();
        let mut options = PackageManagerOptions {
            max_concurrent_lifecycle_scripts: 1,
            ..Default::default()
        };

        // PORT NOTE: reshaped for borrowck — capture the `'static` cwd slice
        // before borrowing `fs.fs` mutably.
        let top_level_dir = fs.top_level_dir;
        let entries_option = fs.fs.read_directory(top_level_dir, None, 0, true)?;
        let entries: &mut Fs::DirEntry = match entries_option {
            Fs::EntriesOption::Entries(e) => &mut **e,
            Fs::EntriesOption::Err(e) => return Err(e.canonical_error),
        };

        // PORTING.md §Forbidden patterns: never `Box::leak` — own `map`/`loader` as locals;
        // they live for the function scope (one-shot CLI path, matches lockfile.zig:1179-1183).
        let mut map = DotEnv::Map::init();
        let mut env_loader = DotEnv::Loader::init(&mut map);
        env_loader.quiet = true;

        env_loader.load_process()?;
        // `DotEnv::Loader::load` takes `impl DirEntryProbe` (bun_dotenv sits
        // below `bun_resolver` in the crate graph); `Fs::DirEntry` impls it.
        env_loader.load(
            &*entries,
            &[] as &[&[u8]],
            DotEnv::DotEnvFileSuffix::Production,
            false,
        )?;
        let mut log = bun_ast::Log::init();
        options.load(
            &mut log,
            &mut env_loader,
            None,
            None,
            crate::Subcommand::Install,
        )?;

        let mut printer = Printer {
            lockfile,
            options: &options,
            successfully_installed: None,
            updates: &[],
        };

        let mut writer = writer;
        match format {
            PrinterFormat::Yarn => {
                printer::Yarn::print(&mut printer, &mut writer)?;
            }
        }
        Ok(())
    }
}

// ────────────────────────────────────────────────────────────────────────────
// verifyData / saveToDisk / rootPackage / str / initEmpty
// ────────────────────────────────────────────────────────────────────────────

impl Lockfile {
    pub fn verify_data(&self) -> Result<(), BunError> {
        // TODO(port): narrow error set
        debug_assert!(self.format == FormatVersion::current());
        let mut i: usize = 0;
        while i < self.packages.len() {
            let package: Package = *self.packages.get(i);
            debug_assert!(self.str(&package.name).len() == package.name.len());
            debug_assert!(
                SemverStringBuilder::string_hash(self.str(&package.name)) == package.name_hash
            );
            debug_assert!(
                package
                    .dependencies
                    .get(self.buffers.dependencies.as_slice())
                    .len()
                    == package.dependencies.len as usize
            );
            debug_assert!(
                package
                    .resolutions
                    .get(self.buffers.resolutions.as_slice())
                    .len()
                    == package.resolutions.len as usize
            );
            debug_assert!(
                package
                    .resolutions
                    .get(self.buffers.resolutions.as_slice())
                    .len()
                    == package.dependencies.len as usize
            );
            let dependencies = package
                .dependencies
                .get(self.buffers.dependencies.as_slice());
            for dependency in dependencies {
                debug_assert!(self.str(&dependency.name).len() == dependency.name.len());
                debug_assert!(
                    SemverStringBuilder::string_hash(self.str(&dependency.name))
                        == dependency.name_hash
                );
            }
            i += 1;
        }
        Ok(())
    }

    pub fn save_to_disk(&mut self, load_result: &LoadResult<'_>, options: &PackageManagerOptions) {
        let save_format = load_result.save_format(options);
        if cfg!(debug_assertions) {
            if let Err(e) = self.verify_data() {
                Output::pretty_errorln(format_args!(
                    "<r><red>error:<r> failed to verify lockfile: {}",
                    e.name()
                ));
                Global::crash();
            }
            // Zig: `bun.assert(FileSystem.instance_loaded);`
            // SAFETY: read of a process-global flag; matches Zig's bare global read.
            debug_assert!(Fs::INSTANCE_LOADED.load(core::sync::atomic::Ordering::Relaxed));
        }

        let bytes: Vec<u8> = 'bytes: {
            if save_format == LockfileFormat::Text {
                let mut writer_buf: Vec<u8> = Vec::new();

                if let Err(_e) = TextLockfile::Stringifier::save_from_binary(
                    self,
                    load_result,
                    options,
                    &mut writer_buf,
                ) {
                    // error.WriteFailed -> OOM in Zig (Allocating writer)
                    bun_core::out_of_memory();
                }

                // writer.flush() catch error.WriteFailed -> OOM
                // (Vec<u8> writer needs no flush)

                break 'bytes writer_buf;
            }

            let mut bytes: Vec<u8> = Vec::new();

            let mut total_size: usize = 0;
            let mut end_pos: usize = 0;
            if let Err(e) =
                Serializer::save(self, options, &mut bytes, &mut total_size, &mut end_pos)
            {
                Output::err(e, "failed to serialize lockfile", format_args!(""));
                Global::crash();
            }
            if bytes.len() >= end_pos {
                bytes[end_pos..end_pos + core::mem::size_of::<usize>()]
                    .copy_from_slice(&total_size.to_ne_bytes());
            }
            break 'bytes bytes;
        };
        // defer bun.default_allocator.free(bytes) — Vec drops at scope end.

        let mut tmpname_buf = [0u8; 512];
        let mut base64_bytes = [0u8; 8];
        bun_core::csprng(&mut base64_bytes);
        let tmpname: &ZStr = {
            let mut cursor: &mut [u8] = &mut tmpname_buf[..];
            let start_len = cursor.len();
            if save_format == LockfileFormat::Text {
                write!(
                    cursor,
                    ".lock-{}.tmp\0",
                    bun_core::fmt::HexBytes::<true>(&base64_bytes)
                )
                .expect("unreachable");
            } else {
                write!(
                    cursor,
                    ".lockb-{}.tmp\0",
                    bun_core::fmt::HexBytes::<true>(&base64_bytes)
                )
                .expect("unreachable");
            }
            let written = start_len - cursor.len();
            ZStr::from_buf(&tmpname_buf, written - 1)
        };
        // TODO(port): Zig `{x}` on `&[8]u8` formats as lowercase hex of bytes; verify HexBytes matches.

        let file = match File::openat(Fd::cwd(), tmpname, sys::O::CREAT | sys::O::WRONLY, 0o777) {
            sys::Result::Err(e) => {
                Output::err(
                    e,
                    "failed to create temporary file to save lockfile",
                    format_args!(""),
                );
                Global::crash();
            }
            sys::Result::Ok(f) => f,
        };

        match file.write_all(&bytes) {
            sys::Result::Err(e) => {
                let _ = file.close(); // close error is non-actionable (Zig parity: discarded)
                let _ = sys::unlink(tmpname);
                Output::err(e, "failed to write lockfile", format_args!(""));
                Global::crash();
            }
            sys::Result::Ok(()) => {}
        }

        #[cfg(unix)]
        {
            // chmod 755 for binary, 644 for plaintext
            let mut filemode: sys::Mode = 0o755;
            if save_format == LockfileFormat::Text {
                filemode = 0o644;
            }
            match sys::fchmod(file.handle, filemode) {
                sys::Result::Err(e) => {
                    let _ = file.close(); // close error is non-actionable (Zig parity: discarded)
                    let _ = sys::unlink(tmpname);
                    Output::err(e, "failed to change lockfile permissions", format_args!(""));
                    Global::crash();
                }
                sys::Result::Ok(()) => {}
            }
        }

        if let Err(e) = file.close_and_move_to(tmpname, save_format.filename()) {
            bun_core::handle_error_return_trace(e);

            // note: file is already closed here.
            let _ = sys::unlink(tmpname);

            Output::err(
                e,
                "Failed to replace old lockfile with new lockfile on disk",
                format_args!(""),
            );
            Global::crash();
        }
    }

    pub fn root_package(&self) -> Option<Package> {
        if self.packages.len() == 0 {
            return None;
        }

        Some(*self.packages.get(0))
    }

    #[inline]
    pub fn str<'a, T: bun_semver::Slicable>(&'a self, slicable: &'a T) -> &'a [u8] {
        // PORT NOTE: Zig had compile-time guards rejecting by-value String/ExternalString.
        // In Rust we just take &T; the temporary-pointer hazard does not exist.
        slicable.slice(self.buffers.string_bytes.as_slice())
    }

    /// [`str`](Self::str) with the borrow detached from `self`.
    ///
    /// The install pipeline frequently needs to read a string out of
    /// `buffers.string_bytes` and then call back into `&mut PackageManager`
    /// (which owns the `Lockfile`). Zig's `[]const u8` carries no lifetime so
    /// the borrow conflict does not exist there; in Rust the caller would
    /// otherwise have to write `unsafe { detach_lifetime(self.str(x)) }` at
    /// every site. Consolidating that here keeps the SAFETY argument in one
    /// place.
    ///
    /// SAFETY (internal): `string_bytes` is append-only for the lifetime of a
    /// resolve/enqueue pass and is never reallocated while a detached slice is
    /// live (Zig invariant). The returned slice must not outlive the
    /// `Lockfile`.
    #[inline]
    pub fn str_detached<'a, T: bun_semver::Slicable>(&self, slicable: &T) -> &'a [u8] {
        // SAFETY: see doc comment — same invariant every prior call site
        // already relied on via `bun_ptr::detach_lifetime`.
        unsafe { bun_ptr::detach_lifetime(slicable.slice(self.buffers.string_bytes.as_slice())) }
    }

    /// Construct an empty Lockfile value (in-place equivalent of Zig `initEmpty`).
    pub fn init_empty(&mut self) {
        *self = Self::init_empty_value();
    }
}

impl Default for Lockfile {
    #[inline]
    fn default() -> Self {
        Self::init_empty_value()
    }
}

impl Lockfile {
    pub fn init_empty_value() -> Self {
        Lockfile {
            format: FormatVersion::current(),
            text_lockfile_version: bun_lock::Version::current(),
            packages: Default::default(),
            buffers: Buffers::default(),
            package_index: PackageIndexMap::default(),
            string_pool: StringPool::default(),
            scratch: Scratch::init(),
            scripts: Scripts::default(),
            trusted_dependencies: None,
            workspace_paths: NameHashMap::default(),
            workspace_versions: VersionHashMap::default(),
            overrides: OverrideMap::default(),
            catalogs: CatalogMap::default(),
            meta_hash: ZERO_HASH,
            patched_dependencies: PatchedDependenciesMap::default(),
            saved_config_version: None,
            // Fresh lockfile (no load): every package appended later is
            // session-appended, so the order-independence guard in
            // `get_package_id` applies from id 0.
            loaded_package_count: 0,
            exact_pinned: Vec::new(),
        }
    }

    /// Snapshot `packages.len()` as the "loaded from lockfile" watermark.
    /// Call exactly once after `load_from_cwd` (including npm/pnpm/yarn
    /// migration) before any manifest-driven `append_package`.
    #[inline]
    pub fn mark_loaded_packages(&mut self) {
        self.loaded_package_count = self.packages.len() as PackageID;
    }

    /// Record that package `id` was appended via an exact-version dependency
    /// (`=X.Y.Z`). See the `exact_pinned` field doc.
    #[inline]
    pub fn mark_exact_pin(&mut self, id: PackageID) {
        let i = id as usize;
        if self.exact_pinned.len() <= i {
            self.exact_pinned.resize(i + 1, false);
        }
        self.exact_pinned[i] = true;
    }

    pub fn get_package_id(
        &self,
        name_hash: u64,
        // If non-null, attempt to use an existing package
        // that satisfies this version range.
        version: Option<&DependencyVersion>,
        resolution: &Resolution,
    ) -> Option<PackageID> {
        let entry = self.package_index.get(&name_hash)?;
        let resolutions: &[Resolution] = self.packages.items_resolution();
        // Borrow the `npm` arm's `Semver::Group` (not `Copy` — owns a linked
        // list head). `version` is held by-value for the whole fn body so the
        // borrow is sound; Zig's by-value copy is replaced with a `&Group`.
        let npm_version = match version {
            Some(v) if v.tag == dependency::Tag::Npm => Some(&v.npm().version),
            _ => None,
        };
        // Order-independence guard for the `satisfies` fallback below: when the
        // caller already knows the manifest's best-match version (the npm
        // `resolution` it passes), only dedupe to an existing entry whose
        // version is at least that. Without this, the result depends on which
        // sibling's manifest happened to land first — `*` deduping to a
        // previously-appended `1.0.2` instead of resolving to `2.0.2` is the
        // long-standing "text lockfile is hoisted" flake. Lockfile-pinned deps
        // are kept out of this codepath by `Diff::generate`'s
        // satisfies-preserves-mapping rule (which keeps the resolution slot
        // populated so the early return in `get_or_put_resolved_package` fires
        // before we get here).
        let resolved_npm_floor = if resolution.tag == ResolutionTag::Npm {
            Some(resolution.npm().version)
        } else {
            None
        };
        let buf = self.buffers.string_bytes.as_slice();

        let loaded_watermark = self.loaded_package_count;
        let exact_pinned = self.exact_pinned.as_slice();
        let try_satisfies_dedupe = |id: PackageID| -> bool {
            let existing = &resolutions[id as usize];
            if existing.tag != ResolutionTag::Npm {
                return false;
            }
            let Some(npm_v) = npm_version else {
                return false;
            };
            let existing_ver = existing.npm().version;
            if !npm_v.satisfies(existing_ver, buf, buf) {
                return false;
            }
            // Order-independence guard. We refuse to dedupe a wide range to a
            // *lower* existing entry only when ALL of the following hold:
            //   - the entry was appended in this resolve session
            //     (lockfile-loaded entries are the user's existing pin),
            //   - the entry was NOT appended for an exact-`=X.Y.Z` dependency
            //     (an exact pin anywhere in the tree is a deliberate choice,
            //     not a network-order artefact — `dragon test 2` /
            //     "dependency from root satisfies range from dependency"),
            //   - the manifest's best-match is a *different major* (within a
            //     major, deduping to an older patch is the long-standing
            //     behaviour and the worst case is still ^-compatible).
            // What this leaves is exactly the cross-parent network-order
            // flake: a wide range (`*`, `>=X`) collapsing onto a sibling's
            // *range-resolved* lower major depending on whose manifest landed
            // first ("text lockfile is hoisted").
            if id >= loaded_watermark && !exact_pinned.get(id as usize).copied().unwrap_or(false) {
                if let Some(floor) = resolved_npm_floor {
                    if existing_ver.order(floor, buf, buf) == Ordering::Less
                        && existing_ver.major != floor.major
                    {
                        return false;
                    }
                }
            }
            true
        };

        match entry {
            PackageIndexEntry::Id(id) => {
                if cfg!(debug_assertions) {
                    debug_assert!((*id as usize) < resolutions.len());
                }

                if resolutions[*id as usize].eql(resolution, buf, buf) {
                    return Some(*id);
                }

                if try_satisfies_dedupe(*id) {
                    return Some(*id);
                }
            }
            PackageIndexEntry::Ids(ids) => {
                for &id in ids.iter() {
                    if cfg!(debug_assertions) {
                        debug_assert!((id as usize) < resolutions.len());
                    }

                    if resolutions[id as usize].eql(resolution, buf, buf) {
                        return Some(id);
                    }

                    if try_satisfies_dedupe(id) {
                        return Some(id);
                    }
                }
            }
        }

        None
    }

    /// Appends `pkg` to `this.packages`, and adds to `this.package_index`.
    ///
    /// PORT NOTE: Zig takes `string_buf: []const u8` as a separate parameter
    /// (always `lockfile.buffers.string_bytes.items`). In Rust that aliases the
    /// `&mut self` borrow, so read it from `self` and split borrows at the
    /// field level (`package_index` / `packages` / `buffers.string_bytes` are
    /// disjoint).
    pub fn append_package_dedupe(&mut self, pkg: &mut Package) -> Result<PackageID, AllocError> {
        let entry = self.package_index.get_or_put(pkg.name_hash)?;

        if !entry.found_existing {
            let new_id: PackageID = PackageID::try_from(self.packages.len()).expect("int cast");
            pkg.meta.id = new_id;
            self.packages.append(*pkg)?;
            *entry.value_ptr = PackageIndexEntry::Id(new_id);
            return Ok(new_id);
        }

        let buf = self.buffers.string_bytes.as_slice();
        let mut resolutions = self.packages.items_resolution();

        match entry.value_ptr {
            PackageIndexEntry::Id(existing_id) => {
                let existing_id = *existing_id;
                if pkg
                    .resolution
                    .eql(&resolutions[existing_id as usize], buf, buf)
                {
                    pkg.meta.id = existing_id;
                    return Ok(existing_id);
                }

                let new_id: PackageID = PackageID::try_from(self.packages.len()).expect("int cast");
                pkg.meta.id = new_id;
                self.packages.append(*pkg)?;

                resolutions = self.packages.items_resolution();

                let pair = if pkg
                    .resolution
                    .order(&resolutions[existing_id as usize], buf, buf)
                    == Ordering::Greater
                {
                    [new_id, existing_id]
                } else {
                    [existing_id, new_id]
                };
                let mut ids = PackageIDList::with_capacity(8);
                ids.extend_from_slice(&pair);

                *entry.value_ptr = PackageIndexEntry::Ids(ids);

                Ok(new_id)
            }
            PackageIndexEntry::Ids(existing_ids) => {
                for &existing_id in existing_ids.iter() {
                    if pkg
                        .resolution
                        .eql(&resolutions[existing_id as usize], buf, buf)
                    {
                        pkg.meta.id = existing_id;
                        return Ok(existing_id);
                    }
                }

                let new_id: PackageID = PackageID::try_from(self.packages.len()).expect("int cast");
                pkg.meta.id = new_id;
                self.packages.append(*pkg)?;

                resolutions = self.packages.items_resolution();

                for i in 0..existing_ids.len() {
                    let existing_id = existing_ids[i];
                    if pkg
                        .resolution
                        .order(&resolutions[existing_id as usize], buf, buf)
                        == Ordering::Greater
                    {
                        existing_ids.insert(i, new_id);
                        return Ok(new_id);
                    }
                }

                existing_ids.push(new_id);

                Ok(new_id)
            }
        }
    }

    pub fn get_or_put_id(
        &mut self,
        id: PackageID,
        name_hash: PackageNameHash,
    ) -> Result<(), AllocError> {
        let gpe = self.package_index.get_or_put(name_hash)?;

        if gpe.found_existing {
            let index: &mut PackageIndexEntry = gpe.value_ptr;

            match index {
                PackageIndexEntry::Id(existing_id) => {
                    let existing_id = *existing_id;
                    let resolutions = self.packages.items_resolution();
                    let buf = self.buffers.string_bytes.as_slice();

                    let pair = if resolutions[id as usize].order(
                        &resolutions[existing_id as usize],
                        buf,
                        buf,
                    ) == Ordering::Greater
                    {
                        [id, existing_id]
                    } else {
                        [existing_id, id]
                    };
                    let mut ids = PackageIDList::with_capacity(8);
                    ids.extend_from_slice(&pair);

                    *index = PackageIndexEntry::Ids(ids);
                }
                PackageIndexEntry::Ids(existing_ids) => {
                    let resolutions = self.packages.items_resolution();
                    let buf = self.buffers.string_bytes.as_slice();

                    for i in 0..existing_ids.len() {
                        let existing_id = existing_ids[i];
                        if resolutions[id as usize].order(
                            &resolutions[existing_id as usize],
                            buf,
                            buf,
                        ) == Ordering::Greater
                        {
                            existing_ids.insert(i, id);
                            return Ok(());
                        }
                    }

                    // append to end because it's the smallest or equal to the smallest
                    existing_ids.push(id);
                }
            }
        } else {
            *gpe.value_ptr = PackageIndexEntry::Id(id);
        }
        Ok(())
    }

    pub fn append_package(&mut self, package_: &Package) -> Result<Package, AllocError> {
        let id: PackageID = self.packages.len() as PackageID; // @truncate
        self.append_package_with_id(package_, id)
    }

    pub fn append_package_with_id(
        &mut self,
        package_: &Package,
        id: PackageID,
    ) -> Result<Package, AllocError> {
        // Zig's `defer` reads `package_` (the original arg) for the assertion.
        let name_hash = package_.name_hash;
        let resolution = package_.resolution;

        let mut package = *package_;
        package.meta.id = id;
        self.packages.append(package)?;
        self.get_or_put_id(id, name_hash)?;

        if cfg!(debug_assertions) {
            debug_assert!(self.get_package_id(name_hash, None, &resolution).is_some());
        }

        Ok(package)
    }

    #[inline]
    pub fn string_builder(&mut self) -> StringBuilder<'_> {
        StringBuilder {
            len: 0,
            cap: 0,
            off: 0,
            ptr: None,
            string_bytes: &mut self.buffers.string_bytes,
            string_pool: &mut self.string_pool,
        }
    }

    /// Borrowck-approved disjoint split: returns a fresh `StringBuilder`
    /// (borrowing `buffers.string_bytes` + `string_pool`) alongside mutable
    /// references to every other `Lockfile` field a caller might need while
    /// the builder is live. Use this instead of routing through
    /// `*mut Lockfile` + `unsafe { &mut (*ptr).field }` reborrows.
    #[inline]
    pub fn string_builder_split(&mut self) -> (StringBuilder<'_>, LockfileFields<'_>) {
        let Buffers {
            string_bytes,
            dependencies,
            resolutions,
            extern_strings,
            trees,
            hoisted_dependencies,
        } = &mut self.buffers;
        (
            StringBuilder {
                len: 0,
                cap: 0,
                off: 0,
                ptr: None,
                string_bytes,
                string_pool: &mut self.string_pool,
            },
            LockfileFields {
                packages: &mut self.packages,
                dependencies,
                resolutions,
                extern_strings,
                trees,
                hoisted_dependencies,
                package_index: &mut self.package_index,
                overrides: &mut self.overrides,
                catalogs: &mut self.catalogs,
                workspace_paths: &mut self.workspace_paths,
                workspace_versions: &mut self.workspace_versions,
                patched_dependencies: &mut self.patched_dependencies,
                trusted_dependencies: &mut self.trusted_dependencies,
                scripts: &mut self.scripts,
            },
        )
    }

    pub fn string_buf(&mut self) -> SemverStringBuf<'_> {
        SemverStringBuf {
            bytes: &mut self.buffers.string_bytes,
            pool: &mut self.string_pool,
        }
        // TODO(port): String.Buf API in bun_semver — Zig also passed `allocator`.
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Scratch
// ────────────────────────────────────────────────────────────────────────────

pub struct Scratch {
    pub duplicate_checker_map: DuplicateCheckerMap,
    pub dependency_list_queue: DependencyQueue,
}

pub(crate) type DuplicateCheckerMap =
    BunHashMap<PackageNameHash, bun_ast::Loc, IdentityContext<PackageNameHash>>;
pub(crate) type DependencyQueue = LinearFifo<DependencySlice, DynamicBuffer<DependencySlice>>;

impl Scratch {
    pub(crate) fn init() -> Scratch {
        Scratch {
            dependency_list_queue: DependencyQueue::init(),
            duplicate_checker_map: DuplicateCheckerMap::default(),
        }
    }
}

impl Default for Scratch {
    fn default() -> Self {
        // Zig field defaults are `undefined`; we initialize properly.
        Self::init()
    }
}

// ────────────────────────────────────────────────────────────────────────────
// LockfileFields — disjoint split borrow alongside StringBuilder
// ────────────────────────────────────────────────────────────────────────────

/// Mutable references to every `Lockfile` field that is *disjoint* from the
/// two columns a `StringBuilder` borrows (`buffers.string_bytes` +
/// `string_pool`). Returned by `Lockfile::string_builder_split()` so callers
/// can hold a live builder and still touch `packages` / `buffers.dependencies`
/// / `overrides` / … without raw-pointer reborrow gymnastics.
pub struct LockfileFields<'a> {
    pub packages: &'a mut PackageList,
    pub dependencies: &'a mut DependencyList,
    pub resolutions: &'a mut PackageIDList,
    pub extern_strings: &'a mut ExternalStringBuffer,
    pub trees: &'a mut tree::List,
    pub hoisted_dependencies: &'a mut DependencyIDList,
    pub package_index: &'a mut PackageIndexMap,
    pub overrides: &'a mut OverrideMap,
    pub catalogs: &'a mut CatalogMap,
    pub workspace_paths: &'a mut NameHashMap,
    pub workspace_versions: &'a mut VersionHashMap,
    pub patched_dependencies: &'a mut PatchedDependenciesMap,
    pub trusted_dependencies: &'a mut Option<TrustedDependenciesSet>,
    pub scripts: &'a mut Scripts,
}

// ────────────────────────────────────────────────────────────────────────────
// StringBuilder
// ────────────────────────────────────────────────────────────────────────────

/// PORT NOTE: Zig stored `lockfile: *Lockfile` and reached `.buffers.string_bytes`
/// / `.string_pool` through it. In Rust that coarse `&mut Lockfile` borrow
/// blocks every caller from touching disjoint fields (`packages`, `buffers
/// .dependencies`, …) while a builder is alive. Hold the two fields the
/// builder actually mutates so callers can split-borrow at the field level.
pub struct StringBuilder<'a> {
    pub len: usize,
    pub cap: usize,
    pub off: usize,
    pub ptr: Option<*mut u8>,
    pub string_bytes: &'a mut Vec<u8>,
    pub string_pool: &'a mut StringPool,
}

/// Construct a `StringBuilder` with a *field-level* split borrow of `$lockfile`
/// (`buffers.string_bytes` + `string_pool`). Use this — not the
/// `Lockfile::string_builder()` method — at sites that also need to read other
/// `$lockfile` fields while the builder is live; the method form borrows the
/// whole struct.
#[macro_export]
macro_rules! string_builder {
    ($lockfile:expr) => {
        $crate::lockfile_real::StringBuilder {
            len: 0,
            cap: 0,
            off: 0,
            ptr: None,
            string_bytes: &mut $lockfile.buffers.string_bytes,
            string_pool: &mut $lockfile.string_pool,
        }
    };
}

/// Trait implemented by `String` and `ExternalString` to support generic `append*`.
/// Replaces Zig's `comptime Type: type` switch. Canonical def lives in
/// `bun_semver::semver_string`; re-exported under the local name so generic
/// bounds in this module (`append<T: StringBuilderType>`) are unchanged.
pub use bun_semver::semver_string::BuilderStringType as StringBuilderType;

impl<'a> StringBuilder<'a> {
    #[inline]
    pub fn count(&mut self, slice: &[u8]) {
        self.assert_not_allocated();

        if SemverString::can_inline(slice) {
            return;
        }
        self._count_with_hash(slice, SemverStringBuilder::string_hash(slice));
    }

    #[inline]
    pub fn count_with_hash(&mut self, slice: &[u8], hash: u64) {
        self.assert_not_allocated();

        if SemverString::can_inline(slice) {
            return;
        }
        self._count_with_hash(slice, hash);
    }

    #[inline]
    fn assert_not_allocated(&self) {
        if cfg!(debug_assertions) {
            if self.ptr.is_some() {
                Output::panic(format_args!(
                    "StringBuilder.count called after StringBuilder.allocate. This is a bug in Bun. Please make sure to call StringBuilder.count before allocating."
                ));
            }
        }
    }

    #[inline]
    fn _count_with_hash(&mut self, slice: &[u8], hash: u64) {
        self.assert_not_allocated();

        if !self.string_pool.contains(hash) {
            self.cap += slice.len();
        }
    }

    pub fn allocated_slice(&self) -> &[u8] {
        // `allocate()` resized `string_bytes` to `off + cap` and recorded `off`,
        // so the region is addressable by safe indexing — no need for the cached
        // raw `ptr`.
        if self.ptr.is_some() {
            &self.string_bytes[self.off..self.off + self.cap]
        } else {
            b""
        }
    }

    pub fn clamp(&mut self) {
        if cfg!(debug_assertions) {
            debug_assert!(self.cap >= self.len);
            // assert that no other builder was allocated while this builder was being used
            debug_assert!(self.string_bytes.len() == self.off + self.cap);
        }

        let excess = self.cap - self.len;

        if excess > 0 {
            let new_len = self.string_bytes.len() - excess;
            self.string_bytes.truncate(new_len);
        }
    }

    pub fn allocate(&mut self) -> Result<(), AllocError> {
        let string_bytes = &mut *self.string_bytes;
        let prev_len = string_bytes.len();
        // Zero-extend rather than `set_len` over uninit: `as_slice()` callers
        // (lockfile.rs:2624/2644/2649/2668, dependency.rs:327, CatalogMap.rs:173)
        // read the full slice including the not-yet-appended tail. Matches the
        // `grow_default` precedent at :1578.
        string_bytes.resize(prev_len + self.cap, 0);
        self.off = prev_len;
        // SAFETY: `string_bytes` was just resized to `prev_len + self.cap`, so
        // offsetting its base pointer by `prev_len` stays within the allocation.
        self.ptr = Some(unsafe { string_bytes.as_mut_ptr().add(prev_len) });
        self.len = 0;
        Ok(())
    }

    #[inline]
    pub fn append<T: StringBuilderType>(&mut self, slice: &[u8]) -> T {
        self.append_with_hash::<T>(slice, SemverStringBuilder::string_hash(slice))
    }

    /// SlicedString is not supported due to inline strings.
    pub fn append_without_pool<T: StringBuilderType>(&mut self, slice: &[u8], hash: u64) -> T {
        if SemverString::can_inline(slice) {
            return T::from_init(self.string_bytes.as_slice(), slice, hash);
        }
        if cfg!(debug_assertions) {
            debug_assert!(self.len <= self.cap); // didn't count everything
            debug_assert!(self.ptr.is_some()); // must call allocate first
        }

        // `allocate()` resized `string_bytes` to `off + cap`; write via safe
        // indexing instead of the cached raw `ptr` + `copy_nonoverlapping`.
        let start = self.off + self.len;
        let end = start + slice.len();
        self.string_bytes[start..end].copy_from_slice(slice);
        let final_slice = &self.string_bytes[start..end];
        self.len += slice.len();

        if cfg!(debug_assertions) {
            debug_assert!(self.len <= self.cap);
        }

        T::from_init(self.string_bytes.as_slice(), final_slice, hash)
    }

    pub fn append_with_hash<T: StringBuilderType>(&mut self, slice: &[u8], hash: u64) -> T {
        if SemverString::can_inline(slice) {
            return T::from_init(self.string_bytes.as_slice(), slice, hash);
        }

        if cfg!(debug_assertions) {
            debug_assert!(self.len <= self.cap); // didn't count everything
            debug_assert!(self.ptr.is_some()); // must call allocate first
        }

        let string_entry = self.string_pool.get_or_put(hash).expect("unreachable");
        if !string_entry.found_existing {
            // See `append_without_pool` — safe indexing into the region
            // `allocate()` already resized.
            let start = self.off + self.len;
            let end = start + slice.len();
            self.string_bytes[start..end].copy_from_slice(slice);
            let final_slice = &self.string_bytes[start..end];
            self.len += slice.len();

            *string_entry.value_ptr = SemverString::init(self.string_bytes.as_slice(), final_slice);
        }

        if cfg!(debug_assertions) {
            debug_assert!(self.len <= self.cap);
        }

        T::from_pooled(*string_entry.value_ptr, hash)
    }
}

// ─── StringBuilder trait wiring ─────────────────────────────────────────────
//
// Several callees (`Version::count`/`append`, `Dependency::count`/`clone`,
// `Bin::count`/`clone`, `Scripts::clone`/`count`) accept the builder via a
// duck-typed trait that mirrors Zig's `comptime StringBuilder: type`. Wire
// `lockfile::StringBuilder` into each so `Package` can pass `&mut builder`
// straight through.

impl<'a> bun_semver::StringBuilder for StringBuilder<'a> {
    #[inline]
    fn count(&mut self, slice_: &[u8]) {
        StringBuilder::count(self, slice_)
    }
    #[inline]
    fn append<T: bun_semver::semver_string::BuilderStringType>(&mut self, slice_: &[u8]) -> T {
        StringBuilder::append::<T>(self, slice_)
    }
}

// `crate::dependency::StringBuilderLike` impl lives in `dependency.rs` next to
// the trait definition (it needs `string_bytes()` access to the lockfile
// buffers). `bin_real::StringBuilder` is now a re-export of
// `bun_semver::StringBuilder`, so the impl above covers it; `package::scripts`
// takes `&mut StringBuilder<'_>` concretely, so no adapter trait is needed
// there either.

// ────────────────────────────────────────────────────────────────────────────
// PackageIndex
// ────────────────────────────────────────────────────────────────────────────

pub mod package_index {
    use super::*;

    pub type Map = BunHashMap<PackageNameHash, Entry, IdentityContext<PackageNameHash>>;
    // TODO(port): Zig uses load factor 80; bun_collections::HashMap should match.

    #[repr(u8)]
    pub enum Tag {
        Id = 0,
        Ids = 1,
    }

    pub enum Entry {
        Id(PackageID),
        Ids(PackageIDList),
    }

    impl Default for Entry {
        /// Zig: `union(PackageIndex.Tag) { id, ids }` zero-initialises to `.id = 0`.
        /// `HashMap::get_or_put` needs a `Default` to fill the value slot before
        /// the caller writes the real `Entry::Id(..)` / `Entry::Ids(..)`.
        #[inline]
        fn default() -> Self {
            Entry::Id(0)
        }
    }
}

pub use package_index::Entry as PackageIndexEntry;
pub use package_index::Map as PackageIndexMap;

// ────────────────────────────────────────────────────────────────────────────
// FormatVersion
// ────────────────────────────────────────────────────────────────────────────

/// Spec lockfile.zig: `enum(u32) { v0, v1, v2, v3, _ }` — non-exhaustive. The binary
/// lockfile serializer reads this u32 directly from disk; an exhaustive Rust enum would
/// make deserializing a future v4+ lockfile instant UB (transmute-to-enum with an
/// invalid discriminant). PORTING.md §Forbidden patterns: never transmute disk data
/// into an exhaustive enum. Represent as a transparent u32 with associated consts so
/// unknown values round-trip and can be compared against `current()` for a graceful
/// version-mismatch error.
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct FormatVersion(pub u32);

impl FormatVersion {
    pub const V0: Self = Self(0);
    /// bun v0.0.x - bun v0.1.6
    pub const V1: Self = Self(1);
    /// bun v0.1.7+
    /// This change added tarball URLs to npm-resolved packages
    pub const V2: Self = Self(2);
    /// Changed semver major/minor/patch to each use u64 instead of u32
    pub const V3: Self = Self(3);

    pub const fn current() -> Self {
        FormatVersion::V3
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Drop (deinit)
// ────────────────────────────────────────────────────────────────────────────

// `deinit` only frees owned fields → handled by Drop on each field type.
// No explicit Drop impl needed.

// ────────────────────────────────────────────────────────────────────────────
// EqlSorter
// ────────────────────────────────────────────────────────────────────────────

pub(crate) struct EqlSorter<'a> {
    pub string_buf: &'a [u8],
    pub pkg_names: &'a [SemverString],
}

/// Basically placement id
#[derive(Clone, Copy)]
pub(crate) struct PathToId {
    pub pkg_id: PackageID,
    /// Borrows a `Box<[u8]>` parked in `tree_paths` for the duration of
    /// `Lockfile::eql` — `RawSlice` carries the outlives-holder invariant.
    pub tree_path: bun_ptr::RawSlice<u8>,
}

impl<'a> EqlSorter<'a> {
    pub(crate) fn order(&self, l: PathToId, r: PathToId) -> Ordering {
        let l_path = l.tree_path.slice();
        let r_path = r.tree_path.slice();
        // they exist in the same tree, name can't be the same so string compare.
        strings::order(l_path, r_path).then_with(|| {
            let l_name = self.pkg_names[l.pkg_id as usize];
            let r_name = self.pkg_names[r.pkg_id as usize];
            l_name.order(r_name, self.string_buf, self.string_buf)
        })
    }
}

impl Lockfile {
    /// A placement of `r` bound past `r_loaded_package_count` was rebound after loading: a change.
    pub fn eql(&self, r: &Lockfile, r_loaded_package_count: usize) -> Result<bool, AllocError> {
        // Zig names the receiver `l`; alias `self` so the body matches the
        // spec verbatim (lockfile.zig:1798).
        let l: &Lockfile = self;
        let l_hoisted_deps = l.buffers.hoisted_dependencies.as_slice();
        let r_hoisted_deps = r.buffers.hoisted_dependencies.as_slice();
        let l_string_buf = l.buffers.string_bytes.as_slice();
        let r_string_buf = r.buffers.string_bytes.as_slice();

        let l_len = l_hoisted_deps.len();
        let r_len = r_hoisted_deps.len();

        if l_len != r_len {
            return Ok(false);
        }

        let mut sort_buf: Vec<PathToId> = Vec::with_capacity(l_len + r_len);

        let mut path_buf = PathBuffer::uninit();
        // Zig: `var depth_buf: Tree.DepthBuf = undefined;`
        let mut depth_buf: tree::DepthBuf = tree::depth_buf_uninit();

        // Track owned tree-path allocations so they outlive the sort and are freed at scope end.
        let mut tree_paths: Vec<Box<[u8]>> = Vec::new();

        for l_tree in l.buffers.trees.iter() {
            let (rel_path, _) = tree::relative_path_and_depth::<{ tree::IteratorPathStyle::PkgPath }>(
                l.buffers.trees.as_slice(),
                l.buffers.dependencies.as_slice(),
                l.buffers.string_bytes.as_slice(),
                l_tree.id,
                &mut path_buf,
                &mut depth_buf,
            );
            let tree_path: Box<[u8]> = Box::<[u8]>::from(rel_path.as_bytes());
            let tree_path_ptr = bun_ptr::RawSlice::new(&tree_path[..]);
            tree_paths.push(tree_path);
            for &l_dep_id in l_tree.dependencies.get(l_hoisted_deps) {
                if l_dep_id == invalid_dependency_id {
                    continue;
                }
                let l_pkg_id = l.buffers.resolutions[l_dep_id as usize];
                if l_pkg_id == invalid_package_id {
                    continue;
                }
                sort_buf.push(PathToId {
                    pkg_id: l_pkg_id,
                    tree_path: tree_path_ptr,
                });
            }
        }
        let l_count = sort_buf.len();

        for r_tree in r.buffers.trees.iter() {
            let (rel_path, _) = tree::relative_path_and_depth::<{ tree::IteratorPathStyle::PkgPath }>(
                r.buffers.trees.as_slice(),
                r.buffers.dependencies.as_slice(),
                r.buffers.string_bytes.as_slice(),
                r_tree.id,
                &mut path_buf,
                &mut depth_buf,
            );
            let tree_path: Box<[u8]> = Box::<[u8]>::from(rel_path.as_bytes());
            let tree_path_ptr = bun_ptr::RawSlice::new(&tree_path[..]);
            tree_paths.push(tree_path);
            for &r_dep_id in r_tree.dependencies.get(r_hoisted_deps) {
                if r_dep_id == invalid_dependency_id {
                    continue;
                }
                let r_pkg_id = r.buffers.resolutions[r_dep_id as usize];
                if r_pkg_id == invalid_package_id {
                    continue;
                }
                if r_pkg_id as usize >= r_loaded_package_count {
                    return Ok(false);
                }
                sort_buf.push(PathToId {
                    pkg_id: r_pkg_id,
                    tree_path: tree_path_ptr,
                });
            }
        }
        let (l_buf, r_buf) = sort_buf.split_at_mut(l_count);

        if l_buf.len() != r_buf.len() {
            return Ok(false);
        }

        let l_pkgs = l.packages.slice();
        let r_pkgs = r.packages.slice();
        let l_pkg_names = l_pkgs.items_name();
        let r_pkg_names = r_pkgs.items_name();

        {
            let sorter = EqlSorter {
                pkg_names: l_pkg_names,
                string_buf: l_string_buf,
            };
            l_buf.sort_unstable_by(|a, b| sorter.order(*a, *b));
        }

        {
            let sorter = EqlSorter {
                pkg_names: r_pkg_names,
                string_buf: r_string_buf,
            };
            r_buf.sort_unstable_by(|a, b| sorter.order(*a, *b));
        }

        let l_pkg_name_hashes = l_pkgs.items_name_hash();
        let l_pkg_resolutions = l_pkgs.items_resolution();
        let l_pkg_bins = l_pkgs.items_bin();
        let l_pkg_scripts = l_pkgs.items_scripts();
        let r_pkg_name_hashes = r_pkgs.items_name_hash();
        let r_pkg_resolutions = r_pkgs.items_resolution();
        let r_pkg_bins = r_pkgs.items_bin();
        let r_pkg_scripts = r_pkgs.items_scripts();

        let l_extern_strings = l.buffers.extern_strings.as_slice();
        let r_extern_strings = r.buffers.extern_strings.as_slice();

        debug_assert_eq!(l_buf.len(), r_buf.len());
        for (l_ids, r_ids) in l_buf.iter().zip(r_buf.iter()) {
            let l_pkg_id = l_ids.pkg_id as usize;
            let r_pkg_id = r_ids.pkg_id as usize;
            if l_pkg_name_hashes[l_pkg_id] != r_pkg_name_hashes[r_pkg_id] {
                return Ok(false);
            }
            let l_res = l_pkg_resolutions[l_pkg_id];
            let r_res = r_pkg_resolutions[r_pkg_id];

            if l_res.tag == ResolutionTag::Uninitialized
                || r_res.tag == ResolutionTag::Uninitialized
            {
                if l_res.tag != r_res.tag {
                    return Ok(false);
                }
            } else if !l_res.eql(&r_res, l_string_buf, r_string_buf) {
                return Ok(false);
            }

            if !crate::bin::Bin::eql(
                &l_pkg_bins[l_pkg_id],
                &r_pkg_bins[r_pkg_id],
                l_string_buf,
                l_extern_strings,
                r_string_buf,
                r_extern_strings,
            ) {
                return Ok(false);
            }

            if !package::Scripts::eql(
                &l_pkg_scripts[l_pkg_id],
                &r_pkg_scripts[r_pkg_id],
                l_string_buf,
                r_string_buf,
            ) {
                return Ok(false);
            }
        }

        Ok(true)
    }

    pub fn has_meta_hash_changed(
        &mut self,
        print_name_version_string: bool,
        packages_len: usize,
    ) -> Result<bool, BunError> {
        // TODO(port): narrow error set
        let previous_meta_hash = self.meta_hash;
        self.meta_hash = self.generate_meta_hash(print_name_version_string, packages_len)?;
        Ok(!strings::eql_long(
            &previous_meta_hash,
            &self.meta_hash,
            false,
        ))
    }

    pub fn generate_meta_hash(
        &self,
        print_name_version_string: bool,
        packages_len: usize,
    ) -> Result<MetaHash, BunError> {
        // TODO(port): narrow error set
        if packages_len <= 1 {
            return Ok(ZERO_HASH);
        }

        let mut string_builder = bun_core::StringBuilder::default();
        let names: &[SemverString] = &self.packages.items_name()[..packages_len];
        let resolutions: &[Resolution] = &self.packages.items_resolution()[..packages_len];
        let bytes = self.buffers.string_bytes.as_slice();
        let mut alphabetized_names: Vec<PackageID> = vec![0; packages_len.saturating_sub(1)];

        const HASH_PREFIX: &[u8] =
            b"\n-- BEGIN SHA512/256(`${alphabetize(name)}@${order(version)}`) --\n";
        const HASH_SUFFIX: &[u8] = b"-- END HASH--\n";
        string_builder.cap += HASH_PREFIX.len() + HASH_SUFFIX.len();
        {
            let mut i: usize = 1;

            while i + 16 < packages_len {
                // PORT NOTE: Zig used `inline while` to unroll 16 iterations. Plain loop here.
                // PERF(port): was comptime-unrolled inner loop — profile if it shows up on a hot path.
                for j in 0..16usize {
                    alphabetized_names[(i + j) - 1] = (i + j) as PackageID; // @truncate
                    // posix path separators because we only use posix in the lockfile
                    string_builder.fmt_count(format_args!(
                        "{}@{}\n",
                        bstr::BStr::new(names[i + j].slice(bytes)),
                        resolutions[i + j].fmt(bytes, PathSep::Posix)
                    ));
                }
                i += 16;
            }

            while i < packages_len {
                alphabetized_names[i - 1] = i as PackageID; // @truncate
                // posix path separators because we only use posix in the lockfile
                string_builder.fmt_count(format_args!(
                    "{}@{}\n",
                    bstr::BStr::new(names[i].slice(bytes)),
                    resolutions[i].fmt(bytes, PathSep::Posix)
                ));
                i += 1;
            }
        }

        const SCRIPTS_BEGIN: &[u8] = b"\n-- BEGIN SCRIPTS --\n";
        const SCRIPTS_END: &[u8] = b"\n-- END SCRIPTS --\n";
        let mut has_scripts = false;

        for (field_name, scripts) in self.scripts.fields() {
            for script in scripts.iter() {
                if !script.is_empty() {
                    string_builder.fmt_count(format_args!(
                        "{}: {}\n",
                        field_name,
                        bstr::BStr::new(script)
                    ));
                    has_scripts = true;
                }
            }
        }

        if has_scripts {
            string_builder.count(SCRIPTS_BEGIN);
            string_builder.count(SCRIPTS_END);
        }

        {
            let alphabetizer = package::Alphabetizer::<u64> {
                names: names.into(),
                buf: bytes.into(),
                resolutions: resolutions.into(),
            };
            alphabetized_names.sort_unstable_by(|a, b| alphabetizer.order(*a, *b));
        }

        string_builder.allocate().expect("unreachable");
        let _ = string_builder.append(HASH_PREFIX);

        for &i in alphabetized_names.iter() {
            let _ = string_builder.fmt(format_args!(
                "{}@{}\n",
                bstr::BStr::new(names[i as usize].slice(bytes)),
                resolutions[i as usize].fmt(bytes, PathSep::Any)
            ));
        }

        if has_scripts {
            let _ = string_builder.append(SCRIPTS_BEGIN);
            for (field_name, scripts) in self.scripts.fields() {
                for script in scripts.iter() {
                    if !script.is_empty() {
                        let _ = string_builder.fmt(format_args!(
                            "{}: {}\n",
                            field_name,
                            bstr::BStr::new(script)
                        ));
                    }
                }
            }
            let _ = string_builder.append(SCRIPTS_END);
        }

        let _ = string_builder.append(HASH_SUFFIX);

        let len = string_builder.len;
        let alphabetized_name_version_string = &string_builder.allocated_slice()[..len];
        if print_name_version_string {
            Output::flush();
            Output::disable_buffering();
            Output::writer()
                .write_all(alphabetized_name_version_string)
                .expect("unreachable");
            Output::enable_buffering();
        }

        let mut digest = ZERO_HASH;
        // SAFETY: engine is null (default).
        unsafe {
            Crypto::SHA512_256::hash(
                alphabetized_name_version_string,
                &mut digest,
                core::ptr::null_mut(),
            )
        };

        Ok(digest)
    }

    pub fn resolve_package_from_name_and_version(
        &self,
        package_name: &[u8],
        version: &DependencyVersion,
    ) -> Option<PackageID> {
        let name_hash = SemverStringBuilder::string_hash(package_name);
        let entry = self.package_index.get(&name_hash)?;
        let buf = self.buffers.string_bytes.as_slice();

        match version.tag {
            dependency::Tag::Npm => {
                // SAFETY: tag checked == .npm above; `npm` is the active
                // `dependency::Value` union field. Same for `Resolution.value`
                // below — Zig reads `.npm` unconditionally on this path.
                let npm_group = &version.npm().version;
                match entry {
                    PackageIndexEntry::Id(id) => {
                        let resolutions = self.packages.items_resolution();

                        if cfg!(debug_assertions) {
                            debug_assert!((*id as usize) < resolutions.len());
                        }
                        let res_ver = resolutions[*id as usize].npm().version;
                        if npm_group.satisfies(res_ver, buf, buf) {
                            return Some(*id);
                        }
                    }
                    PackageIndexEntry::Ids(ids) => {
                        let resolutions = self.packages.items_resolution();

                        for &id in ids.iter() {
                            if cfg!(debug_assertions) {
                                debug_assert!((id as usize) < resolutions.len());
                            }
                            let res_ver = resolutions[id as usize].npm().version;
                            if npm_group.satisfies(res_ver, buf, buf) {
                                return Some(id);
                            }
                        }
                    }
                }
            }
            _ => {}
        }

        None
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Default trusted dependencies
// ────────────────────────────────────────────────────────────────────────────

const MAX_DEFAULT_TRUSTED_DEPENDENCIES: usize = 512;

/// Sorted list of default trusted dependency names.
///
/// Zig builds this at comptime from `default-trusted-dependencies.txt` via
/// `@embedFile` + tokenize + sort. Rust cannot tokenize/sort at const time, so
/// we embed the file with `include_str!` and build the sorted slice on first
/// access. Kept alphabetical so `bun pm trusted --default` need not re-sort.
pub static DEFAULT_TRUSTED_DEPENDENCIES_LIST: std::sync::LazyLock<Vec<&'static [u8]>> =
    std::sync::LazyLock::new(|| {
        // Zig: @embedFile("./default-trusted-dependencies.txt")
        const DATA: &str = include_str!("default-trusted-dependencies.txt");
        // Zig: std.mem.tokenizeAny(u8, data, " \r\n\t")
        let mut names: Vec<&'static [u8]> = DATA
            .split([' ', '\r', '\n', '\t'])
            .filter(|s| !s.is_empty())
            .map(str::as_bytes)
            .collect();
        // Zig: std.sort.pdq with std.mem.order(u8, ..) == .lt
        names.sort_unstable();
        debug_assert!(
            names.len() <= MAX_DEFAULT_TRUSTED_DEPENDENCIES,
            "default-trusted-dependencies.txt is too large, please increase \
             'MAX_DEFAULT_TRUSTED_DEPENDENCIES' in lockfile.rs"
        );
        names
    });

/// The default list of trusted dependencies is a static hashmap.
///
/// Zig builds a comptime `StaticHashMap` keyed by truncated-u32 string-hash.
/// Rust populates the same `StaticHashMap` lazily on first access from the
/// build.rs-generated list. The hash is `String.Builder.stringHash(s) as u32`
/// so entries match `Lockfile.trusted_dependencies` keys.
pub mod default_trusted_dependencies {
    use super::{
        DEFAULT_TRUSTED_DEPENDENCIES_LIST, MAX_DEFAULT_TRUSTED_DEPENDENCIES, SemverStringBuilder,
    };
    use bun_collections::static_hash_map::{
        Entry, HashContext, HashMapMixin, StaticHashMap, static_slots,
    };
    use std::sync::LazyLock;

    const SLOTS: usize = static_slots(MAX_DEFAULT_TRUSTED_DEPENDENCIES);

    pub(crate) struct TrustedDepHashCtx;
    impl HashContext<&'static [u8]> for TrustedDepHashCtx {
        #[inline]
        fn ctx_hash(s: &&'static [u8]) -> u64 {
            // truncate to u32 because Lockfile.trustedDependencies uses the same u32 string hash
            (SemverStringBuilder::string_hash(s) as u32) as u64
        }
        #[inline]
        fn ctx_eql(a: &&'static [u8], b: &&'static [u8]) -> bool {
            *a == *b
        }
    }

    type Map = StaticHashMap<
        &'static [u8],
        (),
        TrustedDepHashCtx,
        MAX_DEFAULT_TRUSTED_DEPENDENCIES,
        SLOTS,
    >;

    static MAP: LazyLock<Box<Map>> = LazyLock::new(|| {
        let mut map = Box::<Map>::default();
        for &dep in DEFAULT_TRUSTED_DEPENDENCIES_LIST.iter() {
            debug_assert!(map.len < MAX_DEFAULT_TRUSTED_DEPENDENCIES);
            let entry = map.get_or_put_assume_capacity(dep);
            debug_assert!(!entry.found_existing);
            *entry.value_ptr = ();
        }
        map
    });

    /// Iterate populated entries (Zig: `default_trusted_dependencies.entries`).
    pub(crate) fn entries() -> impl Iterator<Item = &'static Entry<&'static [u8], ()>> {
        MAP.entries.iter().filter(|e| !e.is_empty())
    }

    /// Zig: `default_trusted_dependencies.hasWithHash`.
    #[inline]
    pub(crate) fn has_with_hash(hash: u64) -> bool {
        MAP.has_with_hash(hash)
    }

    /// Zig: `default_trusted_dependencies.has(name)`.
    ///
    /// Open-coded `hasContext` so the lookup key can borrow with any lifetime,
    /// not just `'static`.
    pub(crate) fn has(name: &[u8]) -> bool {
        let hash = (SemverStringBuilder::string_hash(name) as u32) as u64;
        for entry in &MAP.entries[(hash >> MAP.shift) as usize..] {
            if entry.hash >= hash {
                return entry.hash == hash && entry.key == name;
            }
        }
        unreachable!()
    }
}

impl Lockfile {
    pub fn has_trusted_dependency(
        &self,
        alias: &[u8],
        pkg_name: &[u8],
        resolution: &Resolution,
    ) -> bool {
        if let Some(trusted_dependencies) = &self.trusted_dependencies {
            let trusted_name = if resolution.tag == ResolutionTag::Npm {
                pkg_name
            } else {
                alias
            };
            let hash = SemverStringBuilder::string_hash(trusted_name) as u32;
            let name_is_trusted = match trusted_dependencies.get(&hash) {
                Some(name) => !name.is_empty() && **name == *trusted_name,
                None => false,
            };
            if !name_is_trusted {
                return false;
            }
            if resolution.tag == ResolutionTag::Npm {
                return true;
            }
            return self.declared_by_root_or_workspace(alias, resolution);
        }

        // Only allow default trusted dependencies for npm packages. Check the
        // resolved package's real name, not the dependency alias, so an
        // `npm:`-aliased package can't inherit trust from a default-trusted
        // name.
        if resolution.tag != ResolutionTag::Npm || !default_trusted_dependencies::has(pkg_name) {
            return false;
        }

        let buf = self.buffers.string_bytes.as_slice();
        let npm = resolution.npm();
        let url = npm.url.slice(buf);
        if url.is_empty() {
            return true;
        }
        let registry = PackageManager::get()
            .scope_for_package_name(pkg_name)
            .url
            .href();
        let Ok(canonical_url) = crate::extract_tarball::build_url_with_printer(
            registry,
            &strings::StringOrTinyString::init(pkg_name),
            npm.version,
            buf,
            |args| -> Result<Vec<u8>, std::io::Error> {
                let mut out: Vec<u8> = Vec::new();
                out.write_fmt(args)?;
                Ok(out)
            },
        ) else {
            return false;
        };
        url == canonical_url.as_slice()
    }

    fn declared_by_root_or_workspace(&self, alias: &[u8], resolution: &Resolution) -> bool {
        let buf = self.buffers.string_bytes.as_slice();
        let packages = self.packages.slice();
        let resolutions = packages.items_resolution();
        let dependencies_lists = packages.items_dependencies();
        for (pkg_resolution, dependencies) in resolutions.iter().zip(dependencies_lists.iter()) {
            if pkg_resolution.tag != ResolutionTag::Workspace
                && pkg_resolution.tag != ResolutionTag::Root
            {
                continue;
            }
            for dep_id in dependencies.begin()..dependencies.end() {
                let dep = &self.buffers.dependencies[dep_id as usize];
                if dep.name.slice(buf) != alias {
                    continue;
                }
                let package_id = self.buffers.resolutions[dep_id as usize];
                if package_id == invalid_package_id || package_id as usize >= resolutions.len() {
                    continue;
                }
                if resolutions[package_id as usize].eql(resolution, buf, buf) {
                    return true;
                }
            }
        }

        false
    }
}

// ────────────────────────────────────────────────────────────────────────────
// PatchedDep
// ────────────────────────────────────────────────────────────────────────────

#[repr(C)]
#[derive(Clone, Copy)]
pub struct PatchedDep {
    /// e.g. "patches/is-even@1.0.0.patch"
    pub path: SemverString,
    _padding: [u8; 7],
    pub patchfile_hash_is_null: bool,
    /// the hash of the patch file contents
    __patchfile_hash: u64,
}

impl Default for PatchedDep {
    fn default() -> Self {
        PatchedDep {
            path: SemverString::default(),
            _padding: [0; 7],
            patchfile_hash_is_null: true,
            __patchfile_hash: 0,
        }
    }
}

impl PatchedDep {
    /// Construct with just `path` set (Zig: `.{ .path = path }`). Exists because
    /// the explicit-padding / private-hash fields make the `..Default::default()`
    /// struct-update form unusable from sibling modules.
    pub fn with_path(path: SemverString) -> Self {
        Self {
            path,
            ..Default::default()
        }
    }

    pub fn set_patchfile_hash(&mut self, val: Option<u64>) {
        self.patchfile_hash_is_null = val.is_none();
        self.__patchfile_hash = val.unwrap_or(0);
    }

    pub fn patchfile_hash(&self) -> Option<u64> {
        if self.patchfile_hash_is_null {
            None
        } else {
            Some(self.__patchfile_hash)
        }
    }
}

// ported from: src/install/lockfile.zig