bun_bundler 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
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
// ══════════════════════════════════════════════════════════════════════════
// `BundleV2` struct definition. `bun_resolver` is a direct dep, so
// `Transpiler` (which embeds `Resolver`) is referenceable here. Most method
// bodies live in the `bv2_impl` module below.
// ══════════════════════════════════════════════════════════════════════════

use core::ptr::NonNull;

use bun_collections::{ArrayHashMap, StringHashMap};
use bun_core::ThreadLock;

// `bake_types` / `dispatch` are canonically defined in `bv2_impl` below
// (the full versions); re-exported here so the crate-root `lib.rs` modules and
// the outer `BundleV2` struct see exactly the same types as the impl bodies.
pub use bv2_impl::api;
pub use bv2_impl::bake_types;
pub use bv2_impl::dispatch;
pub use bv2_impl::{
    CompileResult, CompileResultForSourceMap, CompileResultForSourceMapColumns, ContentHasher,
    DeclInfo, DeclInfoKind, EventLoop, ImportTracker, PartRange, StableRef, WrapKind,
    generic_path_with_pretty_initialized, target_from_hashbang,
};
pub use bv2_impl::{DevServerInput, DevServerOutput, ImportTrackerIterator, ImportTrackerStatus};
// Flatten the impl-body module into this file's namespace so external callers
// (`bun_runtime::cli::*`, `linker_context::*`) reference items as
// `bundle_v2::Foo` rather than naming the implementation submodule.
use self::bake_types as bake;
pub use bv2_impl::{
    BuildResult, BundleV2Result, CompletionStruct, DependenciesScanner, DependenciesScannerResult,
    EXTERNAL_FREE_VTABLE, OnDependenciesAnalyze, singleton,
};

pub use crate::DeferredBatchTask::DeferredBatchTask;
use crate::Graph::Graph;
use crate::PathToSourceIndexMap::PathToSourceIndexMap;
use crate::barrel_imports::RequestedExports;
use crate::cache::ExternalFreeFunction;
use crate::options::{self, Target};
use crate::transpiler::Transpiler;
use crate::{Index, IndexInt, LinkerContext};

// ── re-exports so callers can reference these via `bundle_v2::…` ──
/// `BundleThread` (BundleThread.zig) — owns the worker pool + completion
/// queue for `BundleV2`. Re-exported so callers reference `bundle_v2::BundleThread`.
pub use crate::BundleThread::BundleThread;
pub use crate::ParseTask;

/// `jsc::api::JSBundler::Plugin` — re-exported from the canonical def below.
pub use api::JSBundler::Plugin as JSBundlerPlugin;

/// `BundleV2.JSBundleCompletionTask` — re-exported from the canonical def below.
pub use bv2_impl::JSBundleCompletionTask;

/// `jsc::api::JSBundler::FileMap` — re-exported from the canonical def below.
pub use api::JSBundler::FileMap;

#[derive(Clone, Copy)]
pub struct PendingImport {
    pub to_source_index: Index,
    pub import_record_index: u32,
}

pub struct BundleV2<'a> {
    // PORT NOTE: Zig stored `*Transpiler` (and aliased the same pointer into
    // `ssr_transpiler` when SSR graph isn't separate). `ssr_transpiler` stays
    // `*mut` so the alias is legal; `transpiler` is `&'a mut` for ergonomic
    // field access throughout the bundler bodies.
    pub transpiler: &'a mut Transpiler<'a>,
    /// When Server Components is enabled, this is used for the client bundles
    /// and `transpiler` is used for the server bundles.
    ///
    /// `ParentRef` (not raw `NonNull`): set once in `init` (from `BakeOptions`
    /// or `initialize_client_transpiler`), the pointee is live for `'a`, and
    /// the read-only projection (`client_transpiler_ref`) is the common path —
    /// so the safe `Deref` removes the per-accessor `unsafe { p.as_ref() }`.
    /// The two `&mut` sites in `transpiler_for_target` go through the explicit
    /// `unsafe assume_mut` escape hatch.
    pub client_transpiler: Option<bun_ptr::ParentRef<Transpiler<'a>>>,
    /// Owns the storage backing `client_transpiler` when it was lazily created
    /// by `initialize_client_transpiler` (browser-target request from a
    /// server-side build). Stays `None` when `client_transpiler` is borrowed
    /// from `BakeOptions` (DevServer owns that one). Dropped in
    /// `deinit_without_freeing_arena` so the deep-cloned `BundleOptions` /
    /// `Resolver` global-heap fields are released — `arena.alloc` would leak
    /// them since bumpalo never runs `Drop`.
    pub owned_client_transpiler: Option<Box<Transpiler<'a>>>,
    /// See `bake.Framework.ServerComponents.separate_ssr_graph`.
    pub ssr_transpiler: *mut Transpiler<'a>,
    /// When Bun Bake is used, the resolved framework is passed here.
    pub framework: Option<bake::Framework>,
    pub graph: Graph<'a>,
    // `LinkerContext<'a>` borrows the same arena lifetime as `transpiler`
    // (Zig stored both as raw pointers into the bundler heap).
    pub linker: LinkerContext<'a>,
    // The hot reloader (`jsc::hot_reloader::NewHotReloader<BundleV2, …>`) owns the
    // boxed `Watcher`; bundler only ever calls `Watcher::add_file` on it.
    pub bun_watcher: Option<NonNull<bun_watcher::Watcher>>,
    pub plugins: Option<NonNull<JSBundlerPlugin>>,
    pub completion: Option<dispatch::CompletionHandle>,
    /// CYCLEBREAK GENUINE: erased `bake::DevServer` (see `dispatch::DevServerHandle`).
    /// Populated from `transpiler.options.dev_server` + the runtime-registered vtable at
    /// construction. All ~15 DevServer call sites go through this.
    pub dev_server: Option<dispatch::DevServerHandle>,
    /// In-memory files that can be used as entrypoints or imported.
    /// This is a pointer to the FileMap in the completion config.
    pub file_map: Option<&'a FileMap>,
    pub source_code_length: usize,

    /// There is a race condition where an onResolve plugin may schedule a task
    /// on the bundle thread before its parsing task completes.
    pub resolve_tasks_waiting_for_import_source_index: ArrayHashMap<IndexInt, Vec<PendingImport>>,

    /// Allocations not tracked by a threadlocal heap.
    pub free_list: Vec<Box<[u8]>>,

    /// See the comment in `Chunk.OutputPiece`.
    pub unique_key: u64,
    pub dynamic_import_entry_points: ArrayHashMap<IndexInt, ()>,
    pub has_on_parse_plugins: bool,

    pub finalizers: Vec<ExternalFreeFunction>,

    pub drain_defer_task: DeferredBatchTask,

    /// Set true by DevServer. Currently every usage of the transpiler (Bun.build
    /// and `bun build` CLI) runs at the top of an event loop. When this is true,
    /// a callback is executed after all work is complete (`finishFromBakeDevServer`).
    pub asynchronous: bool,
    pub thread_lock: ThreadLock,

    /// If false we can skip TLA validation and propagation.
    pub has_any_top_level_await_modules: bool,

    /// Barrel optimization: tracks which exports have been requested from each
    /// module encountered during barrel BFS. Keys are source indices. Values
    /// track requested export names for deduplication and cycle detection.
    /// Persists across calls to `scheduleBarrelDeferredImports` so cross-file
    /// deduplication is free.
    ///
    /// Indexed by `source_index` (dense `0..module_count`); a `Vec<Option<_>>`
    /// instead of the Zig `AutoArrayHashMap<u32, ...>` because the key space is
    /// dense and this is probed once per import in `on_parse_task_complete`
    /// (the main-thread parse-phase throughput limiter).
    pub requested_exports: Vec<Option<RequestedExports>>,
}

bun_core::declare_scope!(Bundle, visible);
bun_core::declare_scope!(scan_counter, visible);

/// `bundle_v2.zig` `ResolveQueue = std.StringArrayHashMap(*ParseTask)`.
/// Values are raw `*mut ParseTask` (arena-owned by `graph.heap`); the map only
/// dedups by path during a single `on_parse_task_complete` pass.
pub(crate) type ResolveQueue = StringHashMap<*mut ParseTask>;

/// `bundle_v2.zig:BakeOptions`.
pub struct BakeOptions<'a> {
    pub framework: bake::Framework,
    pub client_transpiler: NonNull<Transpiler<'a>>,
    pub ssr_transpiler: NonNull<Transpiler<'a>>,
    pub plugins: Option<NonNull<JSBundlerPlugin>>,
}

impl<'a> BundleV2<'a> {
    // ── raw-ptr accessors ─────────────────────────────────────────────────
    // PORT NOTE: `transpiler`/`ssr_transpiler` are `*mut` because Zig stored
    // aliased `*Transpiler` (same pointer in both slots when no SSR graph).
    // Callers go through these accessors so the unsafe deref is centralized.
    #[inline]
    pub fn transpiler(&self) -> &Transpiler<'a> {
        &*self.transpiler
    }
    #[inline]
    pub fn transpiler_mut(&mut self) -> &mut Transpiler<'a> {
        &mut *self.transpiler
    }

    #[inline]
    pub fn r#loop(&mut self) -> &mut EventLoop {
        &mut self.linker.r#loop
    }

    /// `switch (this.loop().*)` — `linker.loop` is a non-owning backref to the
    /// `AnyEventLoop` that owns this bundle pass and outlives it.
    #[inline]
    pub fn any_loop_mut(&mut self) -> &mut bun_event_loop::AnyEventLoop<'static> {
        // BACKREF deref centralised in `LinkerContext::any_loop_mut`.
        self.linker
            .any_loop_mut()
            .expect("BundleV2.linker.loop must be set before plugins run")
    }

    #[inline]
    pub fn dev_server_handle(&self) -> Option<&dispatch::DevServerHandle> {
        self.dev_server.as_ref()
    }

    /// Safe projection of the `client_transpiler` backref. Set once in `init`
    /// (from `BakeOptions` or `initialize_client_transpiler`); the pointee is
    /// live for `'a`.
    #[inline]
    pub fn client_transpiler_ref(&self) -> Option<&Transpiler<'a>> {
        self.client_transpiler.as_deref()
    }

    /// Safe projection of the `plugins` backref (opaque C++ `BunPlugin`).
    /// Set once in `init` from `BakeOptions` / completion config; live for the
    /// bundle pass.
    #[inline]
    pub fn plugins_ref(&self) -> Option<&JSBundlerPlugin> {
        // SAFETY: BACKREF — opaque C++ object owned by the completion task /
        // bake DevServer, outlives the bundle pass. All `&self` methods on it
        // are FFI calls that take `*const`.
        self.plugins.map(|p| unsafe { p.as_ref() })
    }

    /// Mutable projection of the `plugins` backref for FFI calls that take
    /// `*mut` (`drain_deferred`). The pointee is disjoint from `self` storage.
    #[inline]
    pub fn plugins_mut(&mut self) -> Option<&mut JSBundlerPlugin> {
        // SAFETY: BACKREF — see `plugins_ref`. `&mut self` ensures no other
        // `&JSBundlerPlugin` projection from this `BundleV2` overlaps.
        self.plugins.map(|mut p| unsafe { p.as_mut() })
    }

    /// Mutable projection of the `bun_watcher` backref for `Watcher::add_file`.
    /// Centralises the two open-coded `unsafe { ptr.as_mut() }` sites so the
    /// liveness/exclusivity argument lives in one place.
    #[inline]
    pub fn bun_watcher_mut(&mut self) -> Option<&mut bun_watcher::Watcher> {
        // SAFETY: BACKREF — heap-owned by hot_reloader / DevServer (set via
        // `install_bun_watcher`), live for the process under `--watch`. The
        // watcher storage is disjoint from `self`; `&mut self` excludes any
        // other safe projection from this `BundleV2`, and `add_file` is only
        // ever driven from the single bundle thread (`thread_lock`-asserted).
        self.bun_watcher.map(|mut p| unsafe { p.as_mut() })
    }

    #[inline]
    pub fn path_to_source_index_map(
        &mut self,
        target: options::Target,
    ) -> &mut PathToSourceIndexMap {
        self.graph.path_to_source_index_map(target)
    }

    pub fn transpiler_for_target(&mut self, target: options::Target) -> &mut Transpiler<'a> {
        // SAFETY: all three pointers are live for `'a` (set in `init`); the
        // `client_transpiler` arm is only reached when bake populated it.
        // bundle_v2.zig:247-263 — outside of server-components / dev-server,
        // the only case that doesn't return the main transpiler is a
        // browser-target request from a server-side build, which lazily
        // spins up a client transpiler.
        if !self.transpiler.options.server_components && self.linker.dev_server.is_none() {
            if target == Target::Browser && self.transpiler.options.target.is_server_side() {
                if let Some(p) = self.client_transpiler {
                    // SAFETY: client_transpiler is live for `'a` (set in `init`);
                    // pointer carries write provenance (constructed from `&mut`
                    // / `NonNull::from(&mut _)`), and `&mut self` excludes any
                    // overlapping `client_transpiler_ref()` borrow.
                    return unsafe { p.assume_mut() };
                }
                // bundle_v2.zig:250-252 — `client_transpiler orelse initializeClientTranspiler() catch panic`.
                return self.initialize_client_transpiler().unwrap_or_else(|e| {
                    panic!("Failed to initialize client transpiler: {}", e.name())
                });
            }
            return &mut *self.transpiler;
        }
        // SAFETY: all three pointers are live for `'a` (set in `init`); the
        // `client_transpiler` arm is only reached when bake populated it.
        unsafe {
            match target {
                Target::Browser => self.client_transpiler.unwrap().assume_mut(),
                Target::BakeServerComponentsSsr => &mut *self.ssr_transpiler,
                _ => &mut *self.transpiler,
            }
        }
    }

    // PORT NOTE: draft `on_parse_task_complete` / `deinit_without_freeing_arena`
    // removed — canonical bodies live in the later impl blocks below.
}
// ══════════════════════════════════════════════════════════════════════════
// `BundleV2` method bodies + supporting types.
// ══════════════════════════════════════════════════════════════════════════

pub mod bv2_impl {
    use super::ResolveQueue;
    use crate::IndexInt;
    use crate::mal_prelude::*;
    // This is Bun's JavaScript/TypeScript bundler
    //
    // A lot of the implementation is based on the Go implementation of esbuild. Thank you Evan Wallace.
    //
    // # Memory management
    //
    // Zig is not a managed language, so we have to be careful about memory management.
    // Manually freeing memory is error-prone and tedious, but garbage collection
    // is slow and reference counting incurs a performance penalty.
    //
    // Bun's bundler relies on mimalloc's threadlocal heaps as arena allocators.
    //
    // When a new thread is spawned for a bundling job, it is given a threadlocal
    // heap and all allocations are done on that heap. When the job is done, the
    // threadlocal heap is destroyed and all memory is freed.
    //
    // There are a few careful gotchas to keep in mind:
    //
    // - A threadlocal heap cannot allocate memory on a different thread than the one that
    //   created it. You will get a segfault if you try to do that.
    //
    // - Since the heaps are destroyed at the end of bundling, any globally shared
    //   references to data must NOT be allocated on a threadlocal heap.
    //
    //   For example, package.json and tsconfig.json read from the filesystem must be
    //   use the global arena (bun.default_allocator) because bun's directory
    //   entry cache and module resolution cache are globally shared across all
    //   threads.
    //
    //   Additionally, `LinkerContext`'s arena is also threadlocal.
    //
    // - Globally allocated data must be in a cache & reused, or we will create an infinite
    //   memory leak over time. To do that, we have a DirnameStore, FilenameStore, and the other
    //   data structures related to `BSSMap`. This still leaks memory, but not very
    //   much since it only allocates the first time around.
    //
    // In development, it is strongly recommended to use either a debug build of
    // mimalloc or Valgrind to help catch memory issues
    // To use a debug build of mimalloc:
    //
    //     make mimalloc-debug
    //

    use core::ffi::c_void;
    use core::ptr::NonNull;
    use std::io::Write as _;

    use crate::Graph::InputFileColumns;
    use crate::Index;
    use crate::JSAst;
    use crate::bun_fs as Fs;
    use crate::options_impl::TargetExt;
    use crate::transpiler::Transpiler;

    use crate::{bun_css, import_record};
    use bun_alloc::{AllocError, Arena as ThreadLocalArena};

    use bun_ast::server_component_boundary;
    use bun_ast::{Binding, E, Expr, G, S};
    use bun_ast::{ImportKind, ImportRecord};
    use bun_collections::{ArrayHashMap, DynamicBitSet, DynamicBitSetUnmanaged, VecExt};
    use bun_core::strings;
    use bun_core::{Error, FeatureFlags, Output};
    use bun_resolver::DataURL;
    use bun_resolver::fs::PathResolverExt as _;
    use bun_resolver::{self as _resolver, is_package_path};
    use bun_threading::ThreadPool as ThreadPoolLib;
    // TODO(b0): bake_types arrives from move-in (TYPE_ONLY Side/Graph/BuiltInModule/Framework → bundler)
    use self::bake_types as bake;

    /// CYCLEBREAK(b0) TYPE_ONLY: pure value types from bake that bundler needs without
    /// depending on the full DevServer. Move-in pass keeps these as the canonical defs;
    /// bun_bake (post tier-6 collapse: bun_runtime::bake) re-exports from here.
    pub mod bake_types {
        /// Mirrors src/bake/lib.zig `Side`.
        #[repr(u8)]
        #[derive(Copy, Clone, Eq, PartialEq, Debug, core::marker::ConstParamTy)]
        pub enum Side {
            Client = 0,
            Server = 1,
        }
        /// Mirrors src/bake/lib.zig `Graph`.
        #[repr(u8)]
        #[derive(Copy, Clone, Eq, PartialEq, Debug)]
        pub enum Graph {
            Client = 0,
            Server = 1,
            Ssr = 2,
        }
        /// Zig `@tagName(graph)` — used for the per-file `// path (target)` comment
        /// in postProcessJSChunk and friends.
        impl From<Graph> for &'static str {
            fn from(g: Graph) -> Self {
                match g {
                    Graph::Client => "client",
                    Graph::Server => "server",
                    Graph::Ssr => "ssr",
                }
            }
        }
        impl Side {
            pub fn graph(self) -> Graph {
                match self {
                    Side::Client => Graph::Client,
                    Side::Server => Graph::Server,
                }
            }
        }
        /// Mirrors src/bake/DevServer.zig `FileKind` (the type of `CacheEntry.kind`).
        #[repr(u8)]
        #[derive(Copy, Clone, Eq, PartialEq, Debug)]
        pub enum CacheKind {
            Unknown = 0,
            Js = 1,
            Asset = 2,
            Css = 3,
        }
        #[derive(Copy, Clone)]
        pub struct CacheEntry {
            pub kind: CacheKind,
        }
        /// Mirrors src/bake/DevServer.zig `ASSET_PREFIX` (= INTERNAL_PREFIX ++ "/asset" = "/_bun/asset").
        pub(crate) const ASSET_PREFIX: &str = "/_bun/asset";

        /// Mirrors src/bake/bake.zig:355 `BuiltInModule = union(enum)`. TYPE_ONLY moved
        /// down to bundler (T5); bake (in runtime, T6) constructs values of this type.
        pub enum BuiltInModule {
            Import(Box<[u8]>),
            Code(Box<[u8]>),
        }

        /// Mirrors src/bake/DevServer.zig `EntryPointList.Flags` (`packed struct(u8)`).
        #[repr(transparent)]
        #[derive(Copy, Clone, Default, Eq, PartialEq)]
        pub struct EntryPointFlags(pub u8);
        impl EntryPointFlags {
            pub const CLIENT: u8 = 1 << 0;
            pub const SERVER: u8 = 1 << 1;
            pub const SSR: u8 = 1 << 2;
            /// When set, `.CLIENT` is also set.
            pub const CSS: u8 = 1 << 3;
            #[inline]
            pub fn client(self) -> bool {
                self.0 & Self::CLIENT != 0
            }
            #[inline]
            pub fn server(self) -> bool {
                self.0 & Self::SERVER != 0
            }
            #[inline]
            pub fn ssr(self) -> bool {
                self.0 & Self::SSR != 0
            }
            #[inline]
            pub fn css(self) -> bool {
                self.0 & Self::CSS != 0
            }
        }

        /// Mirrors src/bake/DevServer.zig `EntryPointList`. TYPE_ONLY moved down; bundler
        /// reads `.set` (count/keys/values) in `enqueue_entry_points_dev_server`.
        #[derive(Default)]
        pub struct EntryPointList {
            pub set: bun_collections::StringArrayHashMap<EntryPointFlags>,
        }
        impl EntryPointList {
            pub fn empty() -> Self {
                Self {
                    set: bun_collections::StringArrayHashMap::new(),
                }
            }
        }

        /// Mirrors src/bake/bake.zig `Framework`. TYPE_ONLY subset of the fields
        /// the bundler/parser actually consult (see ParseTask.zig:1253
        /// `opts.framework = transpiler.options.framework`); `file_system_router_types`
        /// stays in T6 because only `bake::FrameworkRouter` reads it.
        // TODO(b0-genuine): remaining Framework field `file_system_router_types`
        // stays in T6; only bake::FrameworkRouter reads it.
        #[non_exhaustive]
        pub struct Framework {
            pub built_in_modules: bun_collections::StringArrayHashMap<BuiltInModule>,
            /// Mirrors `Framework.server_components`.
            pub server_components: Option<ServerComponents>,
            /// Mirrors `Framework.react_fast_refresh` — read by the parser
            /// (`js_parser/ast/Parser.rs:1997` resolves `framework.react_fast_refresh
            /// .import_source`) when `features.react_fast_refresh` is on.
            pub react_fast_refresh: Option<ReactFastRefresh>,
            /// Mirrors `Framework.is_built_in_react` — read by
            /// `linker_context::generateChunksInParallel` to gate `BakeExtra`.
            pub is_built_in_react: bool,
            /// Read by `entry_points.rs` (FallbackEntryPoint/ClientEntryPoint::generate).
            /// In Zig this lives on the legacy package_json `Framework`; the duck-typed
            /// `comptime TranspilerType` callers reach it through `options.framework.?`.
            pub client_css_in_js: crate::options::ClientCssInJs,
        }
        impl Framework {
            /// Construct the bundler-side TYPE_ONLY view. Called from
            /// `bun_runtime::bake::Framework::init_transpiler_with_options`
            /// (spec bake.zig:778 `out.options.framework = framework`); the
            /// runtime owns the canonical `bake.Framework` and projects the
            /// fields the bundler reads.
            pub fn new(
                built_in_modules: bun_collections::StringArrayHashMap<BuiltInModule>,
                server_components: Option<ServerComponents>,
                react_fast_refresh: Option<ReactFastRefresh>,
                is_built_in_react: bool,
            ) -> Self {
                Self {
                    built_in_modules,
                    server_components,
                    react_fast_refresh,
                    is_built_in_react,
                    client_css_in_js: crate::options::ClientCssInJs::default(),
                }
            }
        }
        /// Mirrors src/bake/bake.zig `Framework.ServerComponents` — full string
        /// surface so the parser-side projection (ParseTask.rs `run_with_source_code`)
        /// can forward user-configured `serverRegisterServerReference` /
        /// `clientRegisterServerReference` instead of hardcoding defaults.
        #[derive(Default, Clone)]
        pub struct ServerComponents {
            pub separate_ssr_graph: bool,
            pub server_runtime_import: Box<[u8]>,
            pub server_register_client_reference: Box<[u8]>,
            pub server_register_server_reference: Box<[u8]>,
            pub client_register_server_reference: Box<[u8]>,
        }
        /// Mirrors src/bake/bake.zig `Framework.ReactFastRefresh`.
        #[derive(Clone)]
        pub struct ReactFastRefresh {
            pub import_source: Box<[u8]>,
        }

        /// Mirrors src/bake/bake.zig:840 `HmrRuntime`. TYPE_ONLY moved down so the
        /// linker can splice the runtime preamble without depending on bun_bake.
        #[derive(Clone, Copy)]
        pub struct HmrRuntime {
            pub code: &'static [u8],
            /// Precomputed `\n` count — sourcemap generation skips this many lines.
            pub line_count: u32,
        }
        impl HmrRuntime {
            pub const fn init(code: &'static [u8]) -> Self {
                // const-fn line counter (mirrors `std.mem.count(u8, code, "\n")`).
                let mut n: u32 = 0;
                let mut i = 0usize;
                while i < code.len() {
                    if code[i] == b'\n' {
                        n += 1;
                    }
                    i += 1;
                }
                Self {
                    code,
                    line_count: n,
                }
            }
        }
        /// Alias used at the crate root (`crate::HmrRuntimeSide`); identical to `Side`.
        pub type HmrRuntimeSide = Side;

        /// Mirrors src/bake/bake.zig:855 `getHmrRuntime`. MOVE_DOWN bake→bundler:
        /// the codegen'd `bake.client.js` / `bake.server.js` are loaded via
        /// `bun_core::runtime_embed_file!` (same per-site `OnceLock<String>` cache
        /// `js_parser/runtime.rs` uses for `runtime.out.js`), so the storage lives
        /// HERE — no upward link to `bun_runtime`. `bun_runtime::bake` keeps its
        /// own `&'static ZStr` flavour for JSC/C++ handoff; this bundler-side copy
        /// only needs `&[u8]` for the chunk preamble + sourcemap line skip, so the
        /// NUL-termination dance is unnecessary. Per-side `OnceLock<HmrRuntime>`
        /// memoizes the `\n` count (Zig const-eval'd it on the `@embedFile` arm;
        /// `runtime_embed_file!` already caches the file load, this caches the
        /// `init` scan so repeat calls are a `Copy`).
        pub fn get_hmr_runtime(side: Side) -> HmrRuntime {
            static CLIENT: std::sync::OnceLock<HmrRuntime> = std::sync::OnceLock::new();
            static SERVER: std::sync::OnceLock<HmrRuntime> = std::sync::OnceLock::new();
            match side {
                Side::Client => *CLIENT.get_or_init(|| {
                    HmrRuntime::init(
                        bun_core::runtime_embed_file!(CodegenEager, "bake.client.js").as_bytes(),
                    )
                }),
                // Server runtime is loaded once; non-eager.
                Side::Server => *SERVER.get_or_init(|| {
                    HmrRuntime::init(
                        bun_core::runtime_embed_file!(Codegen, "bake.server.js").as_bytes(),
                    )
                }),
            }
        }

        /// Mirrors src/bake/bake.zig:936 `server_virtual_source` / :942 `client_virtual_source`.
        /// `bun_ast::Source` is not `const`-constructible (owns a `fs::Path`), so these
        /// are lazy statics. PERF(port): was `pub const` in Zig.
        pub(crate) static SERVER_VIRTUAL_SOURCE: std::sync::LazyLock<bun_ast::Source> =
            std::sync::LazyLock::new(|| {
                // Port of `Fs.Path.initForKitBuiltIn("bun", "bake/server")` (fs.zig:1992) —
                // inlined because `bun_paths::fs::Path<'static>` is the local TYPE_ONLY stub and
                // does not yet expose that constructor.
                bun_ast::Source {
                    path: bun_paths::fs::Path {
                        pretty: b"bun:bake/server",
                        text: b"_bun/bake/server",
                        namespace: b"bun",
                        is_disabled: false,
                        is_symlink: true,
                    },
                    index: bun_ast::Index(crate::Index::BAKE_SERVER_DATA.get()),
                    ..Default::default()
                }
            });
        pub(crate) static CLIENT_VIRTUAL_SOURCE: std::sync::LazyLock<bun_ast::Source> =
            std::sync::LazyLock::new(|| bun_ast::Source {
                path: bun_paths::fs::Path {
                    pretty: b"bun:bake/client",
                    text: b"_bun/bake/client",
                    namespace: b"bun",
                    is_disabled: false,
                    is_symlink: true,
                },
                index: bun_ast::Index(crate::Index::BAKE_CLIENT_DATA.get()),
                ..Default::default()
            });

        /// Canonical port of src/bake/production.zig:844 `EntryPointMap`.
        /// Lives in the bundler (lower tier) so both `bun_runtime::bake::production`
        /// and `BundleV2::generate_from_bake_production_cli` share ONE nominal type
        /// (PORTING.md §Layering). Router-integration methods (`InsertionHandler`)
        /// are added by `bun_runtime::bake` via a local trait impl.
        pub mod production {
            use super::Side;

            /// `OpaqueFileId` is the insertion index into `EntryPointMap.files`.
            /// This is the same newtype as `framework_router::OpaqueFileId`; the
            /// bake crate re-exports that one and converts via `.get()` only at
            /// the FFI boundary.
            #[repr(transparent)]
            #[derive(Copy, Clone, Eq, PartialEq, Hash)]
            pub struct OpaqueFileId(pub u32);
            impl OpaqueFileId {
                #[inline]
                pub const fn init(i: u32) -> Self {
                    Self(i)
                }
                #[inline]
                pub const fn get(self) -> u32 {
                    self.0
                }
            }

            /// `EntryPointMap.InputFile`. Zig packed `Side` into the slice-len word
            /// for a 16-byte key; the Rust `Hash`/`Eq` impls below are content-based
            /// (not byte-layout), so that packing is not load-bearing here — store a
            /// `RawSlice` instead and let `bun_ptr` encapsulate the unsafe re-borrow.
            /// `RawSlice<u8>: Send + Sync`, so no manual auto-trait impls are needed.
            #[derive(Copy, Clone)]
            pub struct InputFile {
                abs_path: bun_ptr::RawSlice<u8>,
                pub side: Side,
            }
            impl InputFile {
                #[inline]
                pub fn init(abs_path: &[u8], side: Side) -> Self {
                    Self {
                        abs_path: bun_ptr::RawSlice::new(abs_path),
                        side,
                    }
                }
                #[inline]
                pub fn abs_path(&self) -> &[u8] {
                    // Backing allocation is owned by `EntryPointMap.owned_paths`
                    // (duped on insert) and outlives every key stored in `files`.
                    self.abs_path.slice()
                }
            }
            impl core::hash::Hash for InputFile {
                fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
                    state.write(self.abs_path());
                    state.write_u8(self.side as u8);
                }
            }
            impl PartialEq for InputFile {
                fn eq(&self, other: &Self) -> bool {
                    self.side == other.side && self.abs_path() == other.abs_path()
                }
            }
            impl Eq for InputFile {}

            /// Value side is `OutputFile.Index` — left as a placeholder until the
            /// bundle is indexed (Zig leaves it `undefined`); the bundler never reads it.
            pub use crate::output_file::Index as OutputFileIndex;

            pub type EntryPointHashMap = bun_collections::ArrayHashMap<InputFile, OutputFileIndex>;

            #[derive(Default)]
            pub struct EntryPointMap {
                pub root: Box<[u8]>,
                /// `OpaqueFileId` is the insertion index into this map.
                pub files: EntryPointHashMap,
                /// Owned backing storage for the duped path bytes that `InputFile`
                /// keys point into (raw ptr+len). Mirrors Zig's `map.arena.dupe`
                /// against `bun.default_allocator` — kept here so the allocations
                /// drop with the map (PORTING.md §Forbidden: no `Box::leak`).
                pub owned_paths: Vec<Box<[u8]>>,
            }
            impl EntryPointMap {
                /// Mirrors `getOrPutEntryPoint`. Dupes `abs_path` on first insert
                /// (owned by `owned_paths`; `Box` heap address is stable across the
                /// move so the raw key pointer stays valid).
                pub fn get_or_put_entry_point(
                    &mut self,
                    abs_path: &[u8],
                    side: Side,
                ) -> Result<OpaqueFileId, bun_core::Error> {
                    let probe = InputFile::init(abs_path, side);
                    if let Some(index) = self.files.get_index(&probe) {
                        return Ok(OpaqueFileId::init(index as u32));
                    }
                    // Zig: `gop.key_ptr.* = InputFile.init(try map.arena.dupe(u8, abs_path), side);`
                    // The Zig `errdefer map.files.swapRemoveAt(gop.index)` only guards the
                    // `arena.dupe`, which is infallible in Rust, so no rollback needed.
                    let owned: Box<[u8]> = Box::<[u8]>::from(abs_path);
                    let key = InputFile::init(&owned, side);
                    self.owned_paths.push(owned);
                    let index = self.files.count();
                    // Value is the post-bundle output index; left as a placeholder until
                    // the bundle is indexed (production.zig:873 leaves it `undefined`).
                    self.files.put_no_clobber(key, OutputFileIndex::init(0))?;
                    Ok(OpaqueFileId::init(index as u32))
                }
            }
        }
    }
    // TODO(b0): jsc::api arrives from move-in (TYPE_ONLY → bundler)
    use self::api as jsc_api;

    /// CYCLEBREAK(b0) TYPE_ONLY: data-only halves of `jsc::api::JSBundler` and
    /// `jsc::api::BuildArtifact` that the bundler reads/constructs without touching
    /// JSC. The JS-thread halves (dispatch onto the JS event loop, `toJS`, plugin
    /// FFI bodies) stay in tier-6 (`bun_runtime::api`) and re-export these.
    pub mod api {
        /// Mirrors src/runtime/api/JSBundler.zig:1799 `BuildArtifact.OutputKind`.
        /// Canonical definition lives in `crate::options::OutputKind`; re-exported
        /// here so the documented CYCLEBREAK path `api::build_artifact::OutputKind`
        /// keeps resolving.
        pub mod build_artifact {
            pub use crate::options::OutputKind;
        }

        /// Mirrors src/runtime/api/JSBundler.zig:3 `JSBundler` — TYPE_ONLY subset.
        /// Exposed as a module (not a struct) so callers can write
        /// `api::JSBundler::Load` / `api::JSBundler::Resolve::MiniImportRecord`.
        #[allow(non_snake_case)]
        pub mod JSBundler {
            use super::super::BundleV2;
            use crate::options::{Loader, Target};
            use crate::options_impl::TargetExt;
            use crate::parse_task::ParseTask;
            use bun_ast::ImportKind;
            use bun_core::String as BunString;
            use bun_resolver::fs::PathResolverExt as _;

            // `Plugin = opaque {}` — backed by C++ `BunPlugin`. The bundler calls
            // `has_any_matches` / `match_on_load` / `match_on_resolve` directly
            // (no JSC types needed — only `BunString` / raw context ptrs). The
            // JSC-aware methods (`create`, `add_plugin`, `global_object`, …) are
            // added by `bun_runtime` via the `PluginJscExt` extension trait so
            // this crate stays free of `JSValue` / `JSGlobalObject`.
            bun_opaque::opaque_ffi! { pub struct Plugin; }
            unsafe extern "C" {
                // The three `safe fn`s below take only Rust references / by-value
                // scalars: every pointer the C++ side reads is guaranteed valid by
                // the type system, so there is no caller-side precondition left to
                // discharge (mirrors the `safe fn` pattern in `lolhtml_sys`).
                #[link_name = "JSBundlerPlugin__anyMatches"]
                safe fn JSBundlerPlugin__anyMatches(
                    this: &Plugin,
                    namespace: &mut BunString,
                    path: &mut BunString,
                    is_on_load: bool,
                ) -> bool;
                // `context` is an opaque cookie C++ round-trips back to a Rust
                // callback without dereferencing, so the only pointer validity
                // obligations are on `this`/`BunString` — discharged by `&`.
                #[link_name = "JSBundlerPlugin__matchOnLoad"]
                safe fn JSBundlerPlugin__matchOnLoad(
                    this: &mut Plugin,
                    namespace_string: &mut BunString,
                    path: &mut BunString,
                    context: *mut core::ffi::c_void,
                    default_loader: u8,
                    is_server_side: bool,
                );
                #[link_name = "JSBundlerPlugin__matchOnResolve"]
                safe fn JSBundlerPlugin__matchOnResolve(
                    this: &mut Plugin,
                    namespace_string: &mut BunString,
                    path: &mut BunString,
                    importer: &mut BunString,
                    context: *mut core::ffi::c_void,
                    kind: u8,
                );
                #[link_name = "JSBundlerPlugin__drainDeferred"]
                safe fn JSBundlerPlugin__drainDeferred(this: &mut Plugin, rejected: bool);
                #[link_name = "JSBundlerPlugin__hasOnBeforeParsePlugins"]
                safe fn JSBundlerPlugin__hasOnBeforeParsePlugins(this: &Plugin) -> i32;
                // `ctx`/`args`/`result` are opaque cookies the C++ side round-trips
                // to Rust-registered native-plugin callbacks without dereferencing
                // in `JSBundlerPlugin.cpp` itself (same posture as `matchOnLoad`
                // above); `&Plugin`/`&BunString` discharge the only direct C++-side
                // dereferences, and `should_continue_running` validity is upheld by
                // the `&Cell<i32>` borrow in the safe wrapper — no caller-side
                // precondition remains, so `safe fn`.
                #[link_name = "JSBundlerPlugin__callOnBeforeParsePlugins"]
                safe fn JSBundlerPlugin__callOnBeforeParsePlugins(
                    this: &Plugin,
                    ctx: *mut core::ffi::c_void,
                    namespace: &BunString,
                    path: &BunString,
                    args: *mut core::ffi::c_void,
                    result: *mut core::ffi::c_void,
                    should_continue_running: *mut i32,
                ) -> i32;
            }
            impl Plugin {
                /// `Plugin.drainDeferred` (JSBundler.zig) — resolve every onLoad
                /// `.defer()` promise. Zig wraps the FFI in `fromJSHostCallGeneric`
                /// for exception-scope tracking and returns `JSError!void`; the
                /// only bundler caller (`DeferredBatchTask::run_on_js_thread`) is
                /// `catch return`, so the void FFI call is the observable
                /// behaviour at this tier.
                pub fn drain_deferred(&mut self, rejected: bool) {
                    JSBundlerPlugin__drainDeferred(self, rejected)
                }

                #[inline]
                pub fn has_on_before_parse_plugins(&self) -> bool {
                    JSBundlerPlugin__hasOnBeforeParsePlugins(self) != 0
                }

                #[inline]
                pub fn call_on_before_parse_plugins(
                    &self,
                    ctx: *mut core::ffi::c_void,
                    namespace: &BunString,
                    path: &BunString,
                    args: *mut crate::parse_task::parse_worker::OnBeforeParseArguments,
                    result: *mut crate::parse_task::parse_worker::OnBeforeParseResult,
                    should_continue_running: &core::cell::Cell<i32>,
                ) -> i32 {
                    // `Cell<i32>` is repr(transparent) over `UnsafeCell<i32>`;
                    // `.as_ptr()` yields the `*mut i32` C++ expects, kept valid
                    // for the duration of the call by the `&Cell` borrow.
                    JSBundlerPlugin__callOnBeforeParsePlugins(
                        self,
                        ctx,
                        namespace,
                        path,
                        args.cast(),
                        result.cast(),
                        should_continue_running.as_ptr(),
                    )
                }

                pub fn has_any_matches(
                    &self,
                    path: &crate::bun_fs::Path,
                    is_on_load: bool,
                ) -> bool {
                    let mut namespace_string = if path.is_file() {
                        BunString::empty()
                    } else {
                        BunString::clone_utf8(path.namespace)
                    };
                    let mut path_string = BunString::clone_utf8(path.text);
                    JSBundlerPlugin__anyMatches(
                        self,
                        &mut namespace_string,
                        &mut path_string,
                        is_on_load,
                    )
                }

                pub fn match_on_load(
                    &mut self,
                    path: &[u8],
                    namespace: &[u8],
                    context: *mut core::ffi::c_void,
                    default_loader: Loader,
                    is_server_side: bool,
                ) {
                    let _tracer = bun_core::perf::trace("JSBundler.matchOnLoad");
                    let mut namespace_string = if namespace.is_empty() {
                        BunString::static_(b"file")
                    } else {
                        BunString::clone_utf8(namespace)
                    };
                    let mut path_string = BunString::clone_utf8(path);
                    JSBundlerPlugin__matchOnLoad(
                        self,
                        &mut namespace_string,
                        &mut path_string,
                        context,
                        default_loader as u8,
                        is_server_side,
                    );
                }

                pub fn match_on_resolve(
                    &mut self,
                    path: &[u8],
                    namespace: &[u8],
                    importer: &[u8],
                    context: *mut core::ffi::c_void,
                    import_record_kind: ImportKind,
                ) {
                    let _tracer = bun_core::perf::trace("JSBundler.matchOnResolve");
                    let mut namespace_string = if namespace.is_empty() || namespace == b"file" {
                        BunString::static_(b"file")
                    } else {
                        BunString::clone_utf8(namespace)
                    };
                    let mut path_string = BunString::clone_utf8(path);
                    let mut importer_string = BunString::clone_utf8(importer);
                    JSBundlerPlugin__matchOnResolve(
                        self,
                        &mut namespace_string,
                        &mut path_string,
                        &mut importer_string,
                        context,
                        import_record_kind as u8,
                    );
                }
            }

            /// Mirrors `JSBundler.FileMap` — virtual in-memory files for the build.
            /// The Zig value type is `jsc.Node.BlobOrStringOrBuffer` (T6); bundler
            /// only ever reads `.slice()`, so the moved-down map stores raw bytes.
            /// `bun_runtime`'s `from_js` parses JS values via `BlobOrStringOrBuffer`
            /// in async (owning-copy) mode and inserts the extracted bytes here.
            #[derive(Default)]
            pub struct FileMap {
                pub map: bun_collections::StringHashMap<Box<[u8]>>,
            }
            impl FileMap {
                pub fn get(&self, specifier: &[u8]) -> Option<&[u8]> {
                    if self.map.is_empty() {
                        return None;
                    }
                    #[cfg(not(windows))]
                    {
                        self.map.get(specifier).map(|b| b.as_ref())
                    }
                    #[cfg(windows)]
                    {
                        let mut buf = bun_paths::path_buffer_pool::get();
                        let normalized =
                            bun_paths::resolve_path::path_to_posix_buf(specifier, &mut **buf);
                        self.map.get(normalized).map(|b| b.as_ref())
                    }
                }
                #[inline]
                pub fn contains(&self, specifier: &[u8]) -> bool {
                    if self.map.is_empty() {
                        return false;
                    }
                    #[cfg(not(windows))]
                    {
                        self.map.contains_key(specifier)
                    }
                    #[cfg(windows)]
                    {
                        let mut buf = bun_paths::path_buffer_pool::get();
                        let normalized =
                            bun_paths::resolve_path::path_to_posix_buf(specifier, &mut **buf);
                        self.map.contains_key(normalized)
                    }
                }
                /// Returns a `resolver::Result` for a file in the map, or `None` if
                /// not found. Handles direct key matches and relative specifiers
                /// joined against `dirname(source_file)` (with Windows
                /// drive-letter / separator normalization).
                ///
                /// `arena` is the build's bump arena (`BundleV2::arena()`);
                /// the matched key is copied into it so the returned
                /// `bun_resolver::Result`'s `Path<'static>` borrows arena memory
                /// (lives for the entire build pass) instead of the map's key
                /// storage.
                pub fn resolve(
                    &self,
                    arena: &bun_alloc::Arena,
                    source_file: &[u8],
                    specifier: &[u8],
                ) -> Option<bun_resolver::Result> {
                    if self.map.is_empty() {
                        return None;
                    }

                    // SAFETY: ARENA — `arena` is the build-pass bump arena
                    // (never freed before the `Result` is consumed); detaching the
                    // borrow lifetime matches the established `Path<'static>`
                    // convention used throughout `bun_resolver` (PORTING.md
                    // §Lifetimes: ARENA → `&'bump T`).
                    let dupe = |key: &[u8]| -> &'static [u8] {
                        // SAFETY: see ARENA note above — bytes live in the build-pass arena.
                        unsafe { bun_ptr::detach_lifetime(arena.alloc_slice_copy(key)) }
                    };

                    // Direct key match (must use `getKey` to return the map-owned
                    // key, not the parameter).
                    #[cfg(not(windows))]
                    if let Some((key, _)) = self.map.get_key_value(specifier) {
                        return Some(Self::result_for_key(dupe(key.as_ref())));
                    }
                    #[cfg(windows)]
                    {
                        let mut buf = bun_paths::path_buffer_pool::get();
                        let normalized =
                            bun_paths::resolve_path::path_to_posix_buf(specifier, &mut **buf);
                        if let Some((key, _)) = self.map.get_key_value(normalized) {
                            return Some(Self::result_for_key(dupe(key.as_ref())));
                        }
                    }

                    // Also try joining a relative specifier against the importer's
                    // directory. Relative = not posix-absolute and not Windows
                    // drive-absolute (e.g. `C:/`).
                    if !specifier.is_empty() && !bun_paths::is_absolute_loose(specifier) {
                        // `source_file` may itself be relative (e.g. on Windows
                        // when the bundler stores paths relative to cwd).
                        let mut abs_source_buf = bun_paths::path_buffer_pool::get();
                        let abs_source_file: &[u8] = if bun_paths::is_absolute_loose(source_file) {
                            source_file
                        } else {
                            bun_resolver::fs::FileSystem::instance()
                                .abs_buf(&[source_file], &mut *abs_source_buf)
                        };

                        // Normalize `source_file` to forward slashes (Windows paths
                        // from the real filesystem may use backslashes).
                        let mut source_file_buf = bun_paths::path_buffer_pool::get();
                        let normalized_source_file = bun_paths::resolve_path::path_to_posix_buf::<u8>(
                            abs_source_file,
                            &mut **source_file_buf,
                        );

                        let mut buf = bun_paths::path_buffer_pool::get();
                        let source_dir = bun_paths::resolve_path::dirname::<
                            bun_paths::platform::Posix,
                        >(normalized_source_file);
                        // If `dirname` returns empty but the path has a drive
                        // letter, use the drive root.
                        let effective_source_dir: &[u8] = if source_dir.is_empty() {
                            if normalized_source_file.len() >= 3
                                && normalized_source_file[1] == b':'
                                && normalized_source_file[2] == b'/'
                            {
                                &normalized_source_file[0..3] // "C:/"
                            } else if !normalized_source_file.is_empty()
                                && normalized_source_file[0] == b'/'
                            {
                                b"/"
                            } else {
                                bun_resolver::fs::FileSystem::instance().top_level_dir
                            }
                        } else {
                            source_dir
                        };
                        // `.loose` preserves Windows drive letters; normalize
                        // separators in-place on Windows afterwards.
                        let joined_len = bun_paths::resolve_path::join_abs_string_buf::<
                            bun_paths::platform::Loose,
                        >(
                            effective_source_dir, &mut **buf, &[specifier]
                        )
                        .len();
                        if cfg!(windows) {
                            bun_paths::resolve_path::platform_to_posix_in_place::<u8>(
                                &mut buf[0..joined_len],
                            );
                        }
                        let joined = &buf[0..joined_len];
                        if let Some((key, _)) = self.map.get_key_value(joined) {
                            return Some(Self::result_for_key(dupe(key.as_ref())));
                        }
                    }

                    None
                }

                /// Build a `bun_resolver::Result` for a matched key. `key` must
                /// already satisfy `'static` — see [`resolve`], which copies the
                /// map-owned key into the build's bump arena before calling here so
                /// the resulting `Path<'static>` borrows arena memory rather than
                /// forging a `'static` from a map borrow.
                #[inline]
                fn result_for_key(key: &'static [u8]) -> bun_resolver::Result {
                    bun_resolver::Result {
                        path_pair: bun_resolver::PathPair {
                            primary: crate::bun_fs::Path::init_with_namespace(key, b"file"),
                            ..Default::default()
                        },
                        module_type: crate::options::ModuleType::Unknown,
                        ..Default::default()
                    }
                }
            }

            /// Mirrors `JSBundler.Resolve.MiniImportRecord` (zig:1242).
            #[derive(Clone, Default)]
            pub struct MiniImportRecord {
                pub kind: ImportKind,
                pub source_file: Box<[u8]>,
                pub namespace: Box<[u8]>,
                pub specifier: Box<[u8]>,
                pub importer_source_index: u32,
                pub import_record_index: u32,
                pub range: bun_ast::Range,
                pub original_target: Target,
            }

            /// Mirrors `JSBundler.Resolve.Value.success` payload.
            #[derive(Clone, Default)]
            pub struct ResolveSuccess {
                pub path: Box<[u8]>,
                pub namespace: Box<[u8]>,
                pub external: bool,
            }
            /// Mirrors `JSBundler.Resolve.Value` (`union(enum)`).
            #[derive(Default)]
            pub enum ResolveValue {
                Err(bun_ast::Msg),
                Success(ResolveSuccess),
                NoMatch,
                #[default]
                Pending,
                Consumed,
            }
            impl ResolveValue {
                #[inline]
                pub fn consume(&mut self) -> ResolveValue {
                    core::mem::replace(self, ResolveValue::Consumed)
                }
            }

            /// Mirrors `JSBundler.Resolve` (zig:1234). Both `js_task` and `task`
            /// are the real lower-tier `bun_event_loop` types, so `dispatch()` /
            /// `run_on_js_thread()` are implemented inherently (no T6 hook).
            pub struct Resolve {
                pub bv2: *mut BundleV2<'static>,
                pub import_record: MiniImportRecord,
                pub value: ResolveValue,
                pub js_task: bun_event_loop::AnyTask::AnyTask,
                /// `jsc.AnyEventLoop.Task` — intrusive node for the Mini-loop queue.
                pub task: bun_event_loop::AnyTaskWithExtraContext::AnyTaskWithExtraContext,
            }
            impl Default for Resolve {
                fn default() -> Self {
                    Self {
                    bv2: core::ptr::null_mut(),
                    import_record: MiniImportRecord::default(),
                    value: ResolveValue::Pending,
                    js_task: bun_event_loop::AnyTask::AnyTask::default(),
                    task: bun_event_loop::AnyTaskWithExtraContext::AnyTaskWithExtraContext::default(),
                }
                }
            }
            impl Resolve {
                pub fn init(bv2: &mut BundleV2<'_>, record: MiniImportRecord) -> Self {
                    Self {
                    // SAFETY: lifetime erased — Resolve is owned by the dispatch
                    // chain and never outlives `bv2` (mirrors Zig raw `*BundleV2`).
                    bv2: std::ptr::from_mut::<BundleV2<'_>>(bv2).cast::<BundleV2<'static>>(),
                    import_record: record,
                    value: ResolveValue::Pending,
                    js_task: bun_event_loop::AnyTask::AnyTask::default(),
                    task: bun_event_loop::AnyTaskWithExtraContext::AnyTaskWithExtraContext::default(),
                }
                }
                /// Hops to the JS thread to call the `onResolve` plugin chain.
                /// Zig spec (JSBundler.zig:1311):
                ///   `this.js_task = AnyTask.init(this);
                ///    bv2.jsLoopForPlugins().enqueueTaskConcurrent(
                ///      jsc.ConcurrentTask.create(this.js_task.task()))`
                pub fn dispatch(&mut self) {
                    self.js_task = bun_event_loop::AnyTask::AnyTask {
                        ctx: core::ptr::NonNull::new(
                            std::ptr::from_mut::<Self>(self).cast::<core::ffi::c_void>(),
                        ),
                        callback: Self::run_on_js_thread_wrap,
                    };
                    let task =
                        bun_event_loop::ConcurrentTask::ConcurrentTask::create(self.js_task.task());
                    // SAFETY: `bv2` is a valid backref set by `init`; plugins is
                    // Some (asserted by `enqueue_on_js_loop_for_plugins`).
                    unsafe { (*self.bv2).enqueue_on_js_loop_for_plugins(task) };
                }
                pub fn run_on_js_thread(&mut self) {
                    let kind = self.import_record.kind;
                    // PORT NOTE: reshaped for borrowck — capture the erased self
                    // pointer before borrowing fields immutably for the FFI call.
                    let self_ptr = std::ptr::from_mut::<Self>(self).cast::<core::ffi::c_void>();
                    // SAFETY: `bv2` is a valid backref set by `init`; the plugin
                    // storage is disjoint from `self`, so the `&mut JSBundlerPlugin`
                    // returned by `plugins_mut()` does not alias the
                    // `&self.import_record.*` borrows below.
                    unsafe { &mut *self.bv2 }
                        .plugins_mut()
                        .expect("plugins")
                        .match_on_resolve(
                            &self.import_record.specifier,
                            &self.import_record.namespace,
                            &self.import_record.source_file,
                            self_ptr,
                            kind,
                        );
                }
                fn run_on_js_thread_wrap(
                    ctx: *mut core::ffi::c_void,
                ) -> bun_event_loop::JsResult<()> {
                    // SAFETY: ctx was stored from `*mut Resolve` in `dispatch`.
                    unsafe { bun_ptr::callback_ctx::<Resolve>(ctx) }.run_on_js_thread();
                    Ok(())
                }
            }

            /// Mirrors `JSBundler.Load.Value.success` payload.
            #[derive(Clone, Default)]
            pub struct LoadSuccess {
                pub source_code: Box<[u8]>,
                pub loader: Loader,
            }
            /// Mirrors `JSBundler.Load.Value` (`union(enum)`).
            #[derive(Default)]
            pub enum LoadValue {
                Err(bun_ast::Msg),
                Success(LoadSuccess),
                #[default]
                Pending,
                NoMatch,
                Consumed,
            }
            impl LoadValue {
                #[inline]
                pub fn consume(&mut self) -> LoadValue {
                    core::mem::replace(self, LoadValue::Consumed)
                }
            }

            /// Mirrors `JSBundler.Load` (zig:1369).
            pub struct Load {
                pub bv2: *mut BundleV2<'static>,
                pub source_index: bun_ast::Index,
                pub default_loader: Loader,
                pub path: Box<[u8]>,
                pub namespace: Box<[u8]>,
                pub value: LoadValue,
                pub parse_task: bun_ptr::BackRef<ParseTask>,
                /// Faster path: skip the extra threadpool dispatch when the file is not found.
                pub was_file: bool,
                /// Defer may only be called once.
                pub called_defer: bool,
                pub js_task: bun_event_loop::AnyTask::AnyTask,
                /// `jsc.AnyEventLoop.Task` — intrusive node for the Mini-loop queue
                /// (used by `onDefer` to notify the bundler thread when it runs
                /// under a `MiniEventLoop`).
                pub task: bun_event_loop::AnyTaskWithExtraContext::AnyTaskWithExtraContext,
            }
            impl Load {
                pub fn init(bv2: &mut BundleV2<'_>, parse: &mut ParseTask) -> Self {
                    let default_loader = parse
                        .path
                        .loader(&bv2.transpiler.options.loaders)
                        .unwrap_or(Loader::Js);
                    Self {
                    bv2: std::ptr::from_mut::<BundleV2<'_>>(bv2).cast::<BundleV2<'static>>(),
                    parse_task: bun_ptr::BackRef::new_mut(parse),
                    source_index: parse.source_index,
                    default_loader,
                    value: LoadValue::Pending,
                    path: parse.path.text.to_vec().into_boxed_slice(),
                    namespace: parse.path.namespace.to_vec().into_boxed_slice(),
                    was_file: false,
                    called_defer: false,
                    js_task: bun_event_loop::AnyTask::AnyTask::default(),
                    task: bun_event_loop::AnyTaskWithExtraContext::AnyTaskWithExtraContext::default(),
                }
                }
                /// Raw backref to the owning `BundleV2`.
                ///
                /// No `&`/`&mut`-returning accessor is provided: the bundle is
                /// reachable from both the bundler thread and JS-thread plugin
                /// callbacks, and several callers (`on_load_async`,
                /// `on_load_from_js_loop`) need `&mut BundleV2` *alongside*
                /// `&mut Load`, which a borrowing accessor cannot express. Callers
                /// must keep the raw deref + SAFETY note locally.
                #[inline]
                pub fn bv2_ptr(&self) -> *mut BundleV2<'static> {
                    self.bv2
                }
                /// Shared access to the heap-allocated `ParseTask` this load wraps.
                ///
                /// `parse_task` is a `BackRef` set from `&mut ParseTask` in `init`
                /// (never null) and the task outlives the `Load` — it is only
                /// handed to the thread-pool *after* the plugin load resolves, so
                /// no concurrent mutation overlaps a `&` borrow here.
                #[inline]
                pub fn parse_task(&self) -> &ParseTask {
                    self.parse_task.get()
                }
                /// Exclusive access to the wrapped `ParseTask`.
                ///
                /// SAFETY (encapsulated): see `parse_task()`. `&mut self` guarantees
                /// the `Load` itself is uniquely borrowed; the `ParseTask` is not
                /// yet scheduled at any call site that uses this accessor.
                #[inline]
                pub fn parse_task_mut(&mut self) -> &mut ParseTask {
                    // SAFETY: see fn doc — exclusivity established by `&mut self`;
                    // backref liveness established by the `BackRef` invariant.
                    unsafe { self.parse_task.get_mut() }
                }
                #[inline]
                pub fn bake_graph(&self) -> crate::bake_types::Graph {
                    self.parse_task().known_target.bake_graph()
                }
                /// Hops to the JS thread to call the `onLoad` plugin chain.
                /// Zig spec (JSBundler.zig:1449):
                ///   `this.js_task = AnyTask.init(this);
                ///    let concurrent_task = jsc.ConcurrentTask.createFrom(&this.js_task);
                ///    bv2.jsLoopForPlugins().enqueueTaskConcurrent(concurrent_task)`
                pub fn dispatch(&mut self) {
                    self.js_task = bun_event_loop::AnyTask::AnyTask {
                        ctx: core::ptr::NonNull::new(
                            std::ptr::from_mut::<Self>(self).cast::<core::ffi::c_void>(),
                        ),
                        callback: Self::run_on_js_thread_wrap,
                    };
                    let concurrent_task =
                        bun_event_loop::ConcurrentTask::ConcurrentTask::create(self.js_task.task());
                    // SAFETY: `bv2` is a valid backref; plugins is Some (asserted
                    // by `enqueue_on_js_loop_for_plugins`).
                    unsafe {
                        (*self.bv2).enqueue_on_js_loop_for_plugins(concurrent_task);
                    }
                }
                pub fn run_on_js_thread(&mut self) {
                    let is_server_side = self.bake_graph() != crate::bake_types::Graph::Client;
                    let default_loader = self.default_loader;
                    // PORT NOTE: reshaped for borrowck — capture the erased self
                    // pointer before borrowing fields immutably for the FFI call.
                    let self_ptr = std::ptr::from_mut::<Self>(self).cast::<core::ffi::c_void>();
                    // SAFETY: `bv2` is a valid backref set by `init`; the plugin
                    // storage is disjoint from `self`, so the `&mut JSBundlerPlugin`
                    // returned by `plugins_mut()` does not alias the
                    // `&self.path` / `&self.namespace` borrows below.
                    unsafe { &mut *self.bv2 }
                        .plugins_mut()
                        .expect("plugins")
                        .match_on_load(
                            &self.path,
                            &self.namespace,
                            self_ptr,
                            default_loader,
                            is_server_side,
                        );
                }
                fn run_on_js_thread_wrap(
                    ctx: *mut core::ffi::c_void,
                ) -> bun_event_loop::JsResult<()> {
                    // SAFETY: ctx was stored from `*mut Load` in `dispatch`.
                    unsafe { bun_ptr::callback_ctx::<Load>(ctx) }.run_on_js_thread();
                    Ok(())
                }
            }
        }
    }

    /// `SavedFile` is a unit struct in Zig
    /// (src/bundler_jsc/output_file_jsc.zig:4) — its only member is `toJS`, which
    /// is JSC-bound and stays in T6. The bundler stores it as an `OutputFile` value
    /// tag, so a unit struct here is sufficient.
    pub mod saved_file {
        #[derive(Default, Clone, Copy)]
        pub struct SavedFile;
    }

    // ── crate-root re-exports for forward-refs left by move-out ───────────────
    pub use self::bake_types::{HmrRuntimeSide, get_hmr_runtime};

    /// `crate::bundle_v2::JSBundlerPlugin` — see BundleThread.rs.
    pub type JSBundlerPlugin = self::api::JSBundler::Plugin;
    pub type FileMap = self::api::JSBundler::FileMap;

    use bun_sourcemap as SourceMap;

    use crate::AstBuilder::AstBuilder;
    use crate::DeferredBatchTask::DeferredBatchTask;
    use crate::Graph::Graph;
    use crate::LinkerContext;
    use crate::PathToSourceIndexMap::PathToSourceIndexMap;
    use crate::ServerComponentParseTask::ServerComponentParseTask;
    use crate::barrel_imports;

    use crate::chunk::{self, Chunk};
    use crate::linker_graph::LinkerGraph;
    use crate::options::{self, Loader, Target};
    use crate::parse_task::{self, ParseTask};
    use crate::thread_pool::ThreadPool;

    pub use crate::BundleThread::BundleThread;

    bun_core::declare_scope!(part_dep_tree, visible);
    bun_core::declare_scope!(Bundle, visible);
    bun_core::declare_scope!(scan_counter, visible);
    bun_core::declare_scope!(ReachableFiles, visible);
    bun_core::declare_scope!(TreeShake, hidden);
    bun_core::declare_scope!(PartRanges, hidden);
    bun_core::declare_scope!(watcher, visible);

    pub use bun_js_printer::MangledProps;

    // ══════════════════════════════════════════════════════════════════════════
    // CYCLEBREAK §Dispatch — vtables/hooks for T6 GENUINE deps (jsc/bake/runtime).
    // Low tier (bundler) names no high-tier types. High tier (runtime) provides
    // static instances and registers hooks at init. See PORTING.md §Dispatch.
    // ══════════════════════════════════════════════════════════════════════════
    pub mod dispatch {
        pub use crate::{DevServerHandle, DevServerHandleKind};

        impl DevServerHandle {
            #[inline]
            pub fn put_or_overwrite_asset_erased<P>(
                &self,
                path: &P,
                contents: &[u8],
                content_hash: u64,
            ) -> Result<(), bun_core::Error> {
                self.put_or_overwrite_asset(
                    core::ptr::from_ref::<P>(path).cast::<()>(),
                    contents,
                    content_hash,
                )
            }
        }

        unsafe extern "Rust" {
            /// Defined `#[no_mangle]` in `bun_jsc::cached_bytecode`. Generic
            /// "generate JSC bytecode off the main JS thread" helper — marks the
            /// calling thread as bytecode-only, initializes JSC, generates, and
            /// returns an owned copy of the bytes. Definer-prefixed (`__bun_jsc_*`).
            /// All arguments are safe Rust types (no raw-pointer preconditions),
            /// so the link-time-resolved body upholds Rust's invariants on its own.
            safe fn __bun_jsc_generate_cached_bytecode(
                format: crate::options_impl::Format,
                source: &[u8],
                source_provider_url: &mut bun_core::String,
            ) -> Option<Box<[u8]>>;
        }

        unsafe extern "Rust" {
            /// Defined `#[no_mangle]` in `bun_jsc::hot_reloader`. Installs a
            /// `NewHotReloader<BundleV2, AnyEventLoop, true>` watcher on the given
            /// `BundleV2` (Zig: `Watcher.enableHotModuleReloading(this, null)` in
            /// `BundleV2.init` — bundle_v2.zig:994). The bundler can't name the
            /// reloader generic (T6), so this is a definer-prefixed extern hook.
            /// `'static` matches the impl-side signature; the sole caller
            /// (`bun build --watch`) leaks the `Box<BundleV2>` via
            /// `Box::into_raw` once `generate_from_cli` returns. The watcher is
            /// installed after the last fallible step in `BundleV2::init`, so the
            /// box is never dropped while the watcher holds a pointer to it.
            fn __bun_jsc_enable_hot_module_reloading_for_bundler(
                bv2: core::ptr::NonNull<super::BundleV2<'static>>,
            );
        }

        /// `Watcher.enableHotModuleReloading(this, null)` for `bun build --watch`.
        #[inline]
        pub(crate) fn enable_hot_module_reloading_for_bundler(bv2: *mut super::BundleV2<'_>) {
            let bv2 = core::ptr::NonNull::new(bv2.cast::<super::BundleV2<'static>>())
                .expect("BundleV2 watcher: bv2 is non-null");
            // SAFETY: link-time-resolved Rust-ABI fn in `bun_jsc::hot_reloader`.
            // Not `safe fn`: the callee dereferences `bv2`, so it must point to a
            // live `BundleV2` whose backing allocation outlives the watcher (sole
            // caller is `BundleV2::init`; the box is leaked on the success path —
            // see the watch-mode caveat above).
            unsafe { __bun_jsc_enable_hot_module_reloading_for_bundler(bv2) }
        }

        /// Bytecode generation entry point for the linker. Mirrors the Zig
        /// `jsc.VirtualMachine.is_bundler_thread_for_bytecode_cache = true;
        ///  jsc.initialize(false); jsc.CachedBytecode.generate(...)` sequence.
        #[inline]
        pub fn generate_cached_bytecode(
            format: crate::options_impl::Format,
            source: &[u8],
            source_provider_url: &mut bun_core::String,
        ) -> Option<Box<[u8]>> {
            __bun_jsc_generate_cached_bytecode(format, source, source_provider_url)
        }

        /// CYCLEBREAK GENUINE: `JSBundleCompletionTask` (JSBundler.zig) — the
        /// concrete struct lives in `bun_runtime` (its fields name `Config`/
        /// `Plugin`/`HTMLBundle::Route`). The bundler reads exactly two things
        /// from it (`result == .err` and `jsc_event_loop.enqueueTaskConcurrent`),
        /// so the high tier hands the bundler an erased owner + `&'static` vtable
        /// pair (same shape as [`DevServerHandle`]). PERF(port): was direct field
        /// access in Zig.
        pub struct CompletionDispatch {
            /// Zig: `completion.result == .err`
            pub result_is_err: unsafe fn(core::ptr::NonNull<super::JSBundleCompletionTask>) -> bool,
            /// Zig: `completion.jsc_event_loop.enqueueTaskConcurrent(task)` — folds
            /// the field access + enqueue so the bundler needn't name `*jsc.EventLoop`.
            pub enqueue_task_concurrent: unsafe fn(
                core::ptr::NonNull<super::JSBundleCompletionTask>,
                *mut bun_event_loop::ConcurrentTask::ConcurrentTask,
            ),
        }
        #[derive(Copy, Clone)]
        pub struct CompletionHandle {
            pub owner: core::ptr::NonNull<super::JSBundleCompletionTask>,
            pub vtable: &'static CompletionDispatch,
        }
        // SAFETY: erased `*mut JSBundleCompletionTask` backref — set by the JS
        // thread, read by the bundle thread; `enqueue_task_concurrent` is the only
        // cross-thread call and it goes through `jsc::EventLoop`'s lock-free queue.
        unsafe impl Send for CompletionHandle {}
        // Intentionally not `Sync`: the opaque owner (`JSBundleCompletionTask`)
        // is modeled as `!Sync`, and this wrapper exposes `result_is_err(&self)`
        // in addition to the lock-free enqueue path, so blanket `&CompletionHandle`
        // sharing across threads is not justified. The handle only needs to *move*
        // to the bundle thread (`Send`), not be shared. If a cross-thread `&` ever
        // becomes necessary, split out an enqueue-only wrapper and make only that
        // type `Sync`.
        impl CompletionHandle {
            #[inline]
            pub fn result_is_err(&self) -> bool {
                // SAFETY: vtable contract.
                unsafe { (self.vtable.result_is_err)(self.owner) }
            }
            #[inline]
            pub fn enqueue_task_concurrent(
                &self,
                task: core::ptr::NonNull<bun_event_loop::ConcurrentTask::ConcurrentTask>,
            ) {
                // SAFETY: vtable contract.
                unsafe { (self.vtable.enqueue_task_concurrent)(self.owner, task.as_ptr()) }
            }
        }
    }

    /// `bun.jsc.AnyEventLoop` — re-export the linker's alias
    /// (`Option<NonNull<bun_event_loop::AnyEventLoop>>`).
    pub use crate::linker_context_mod::EventLoop;

    // `JSBundleCompletionTask` (JSBundler.zig) — typed-ptr marker for
    // `BundleV2.completion`. The concrete struct lives in `bun_runtime` (its
    // fields name `Config`/`Plugin`/`HTMLBundle::Route`); the bundler only ever
    // holds a `NonNull<JSBundleCompletionTask>` inside [`dispatch::CompletionHandle`]
    // and never dereferences it. Nomicon opaque-FFI pattern: ZST with
    // `PhantomData<(*mut u8, PhantomPinned)>` so it is `!Send + !Sync + !Unpin`
    // and has no usable size/layout in this crate.
    bun_opaque::opaque_ffi! { pub struct JSBundleCompletionTask; }

    /// Erase `&[u8]` to `&'static [u8]` for storage in lifetime-erased
    /// `Path<'static>` slots (`ImportRecord.path`, `Graph.input_files`).
    ///
    /// # Safety
    /// Caller guarantees `s` is one of:
    ///   - a `'static` literal,
    ///   - interned in `FilenameStore`/`DirnameStore` (process-lifetime BSS lists),
    ///   - allocated from the bundle-pass arena (`BundleV2::arena()`), in which
    ///     case the returned reference is valid only for the bundle pass and the
    ///     consuming `Path` must not outlive it.
    /// All call sites in this file satisfy one of these; this is the documented
    /// arena-erasure convention (PORTING.md §Type Mapping: arena-owned struct
    /// fields use erased lifetimes).
    #[inline(always)]
    pub(crate) unsafe fn interned_slice(s: &[u8]) -> &'static [u8] {
        // SAFETY: upheld by caller per fn contract.
        unsafe { bun_ptr::detach_lifetime(s) }
    }
    /// Erase a resolver-borrowed `Path<'_>` to `'static`. Safe only because every
    /// caller passes paths whose backing bytes are arena-interned for the bundle's
    /// lifetime (see `interned_slice` / `dupe_alloc`).
    #[inline]
    pub(crate) fn path_as_static(p: &Fs::Path<'_>) -> Fs::Path<'static> {
        // SAFETY: caller contract above.
        unsafe { (*p).into_static() }
    }

    // Unified with the canonical definitions at the parent module level (this
    // avoids two distinct nominal `BundleV2`/`PendingImport`/`BakeOptions` types
    // that previously caused widespread "expected `BundleV2`, found `BundleV2`"
    // errors in cross-module call sites).
    pub use super::{BakeOptions, BundleV2, PendingImport};

    impl<'a> BundleV2<'a> {
        /// Zig: `jsLoopForPlugins().enqueueTaskConcurrent(task)`. The Rust port
        /// folds the lookup + enqueue so the bundler never dereferences
        /// `JSBundleCompletionTask` (its layout lives in `bun_runtime`); the
        /// `completion` handle carries the `&'static` vtable.
        /// PERF(port): was inline `switch (this.loop().*)` + direct field access.
        pub fn enqueue_on_js_loop_for_plugins(
            &mut self,
            task: NonNull<bun_event_loop::ConcurrentTask::ConcurrentTask>,
        ) {
            debug_assert!(self.plugins.is_some());
            if let Some(completion) = self.completion {
                // From Bun.build — `completion.jsc_event_loop.enqueueTaskConcurrent(task)`.
                completion.enqueue_task_concurrent(task);
                return;
            }
            // From bake where the loop running the bundle is also the loop running
            // the plugins (Zig: `switch (this.loop().*) { .js => |l| l, .mini => @panic }`).
            // `any_loop_mut` centralises the BACKREF deref of `linker.r#loop`.
            match &*self.any_loop_mut() {
                bun_event_loop::AnyEventLoop::Js { owner } => {
                    owner.enqueue_task_concurrent(task);
                }
                bun_event_loop::AnyEventLoop::Mini(_) => {
                    panic!("No JavaScript event loop for transpiler plugins to run on");
                }
            }
        }

        fn ensure_client_transpiler(&mut self) {
            if self.client_transpiler.is_none() {
                let _ = self
                    .initialize_client_transpiler()
                    .unwrap_or_else(|e: Error| {
                        panic!("Failed to initialize client transpiler: {}", e.name());
                    });
            }
        }

        pub fn initialize_client_transpiler(&mut self) -> Result<&mut Transpiler<'a>, Error> {
            // bundle_v2.zig:198-241.
            //
            // PORT NOTE: Zig does `client_transpiler.* = this_transpiler.*` (bitwise
            // struct copy into an arena slot — no destructors). The Rust port
            // builds a fresh owned `Transpiler` via `Transpiler::for_worker`
            // (per-field deep clone), mutates the browser-specific options with
            // ordinary assignment (every field is owned by the clone, so `Drop` on
            // the overwritten value is correct), then boxes it on the global heap
            // (NOT the bump arena — the clone holds `Box`/`Vec`/`MimallocArena`
            // fields that need `Drop` to run) and wires the self-referential
            // `linker`/`macro_context`. The box is parked on
            // `self.owned_client_transpiler` so `deinit_without_freeing_arena`
            // releases it.

            // `arena` is only the scratch param for `Transpiler::for_worker`; the
            // returned `Transpiler` itself is NOT placed in it.
            // SAFETY: `graph.heap` outlives the bundle pass; erase the `&self`
            // borrow so the `'a` widen inside `for_worker` doesn't keep `self`
            // borrowed.
            let arena: &'a bun_alloc::Arena =
                unsafe { bun_ptr::detach_lifetime_ref::<bun_alloc::Arena>(self.arena()) };

            let this_transpiler: &Transpiler<'a> = &*self.transpiler;
            let this_compile = this_transpiler.options.compile;
            let this_env = this_transpiler.env;

            // SAFETY: `self.transpiler` (and the data its `&'a` fields borrow)
            // outlives this `BundleV2<'a>`; `for_worker` widens those borrows to
            // the same `'a`.
            let mut ct: Transpiler<'a> =
                unsafe { Transpiler::for_worker(this_transpiler, arena, this_transpiler.log) };

            ct.options.target = Target::Browser;
            ct.options.main_fields = Target::Browser
                .default_main_fields()
                .iter()
                .map(|s| s.as_bytes().to_vec().into_boxed_slice())
                .collect();
            ct.options.conditions =
                options::ESMConditions::init(Target::Browser.default_conditions(), false, &[])?;

            // We need to make sure it has [hash] in the names so we don't get conflicts.
            if this_compile {
                ct.options.asset_naming = options::PathTemplate::ASSET
                    .data
                    .to_vec()
                    .into_boxed_slice();
                ct.options.chunk_naming = options::PathTemplate::CHUNK
                    .data
                    .to_vec()
                    .into_boxed_slice();
                ct.options.entry_naming = b"./[name]-[hash].[ext]".to_vec().into_boxed_slice();
                // Use "/" so that asset URLs in HTML are absolute (e.g. "/chunk-abc.js"
                // instead of "./chunk-abc.js"). Relative paths break when the HTML is
                // served from a nested route like "/foo/".
                ct.options.public_path = b"/".to_vec().into_boxed_slice();
            }

            // Move into a stable heap slot, then wire self-refs at the final
            // address. `Box` (global mimalloc heap) so `Drop` runs on the
            // deep-cloned `BundleOptions`/`Resolver` fields; `arena.alloc` would
            // leak them (bumpalo never drops).
            let mut boxed: Box<Transpiler<'a>> = Box::new(ct);
            // Zig: `setLog` / `setAllocator` / `linker.resolver = &resolver` /
            // `macro_context = MacroContext.init(transpiler)` /
            // `resolver.caches = CacheSet.init(alloc)` — all handled by
            // `for_worker` + `wire_after_move`.
            boxed.wire_after_move();

            // `configure_defines` early-returns on `options.defines_loaded` (cloned
            // as `true`); kept for spec parity.
            boxed.configure_defines()?;

            // Zig: `client_transpiler.resolver.opts = client_transpiler.options;` —
            // re-project the resolver subset now that `target`/`conditions` etc.
            // have been overwritten for the browser.
            boxed.sync_resolver_opts();
            // Zig: `client_transpiler.resolver.env_loader = client_transpiler.env;`
            boxed.resolver.env_loader = NonNull::new(this_env.cast());

            // Park the owning Box first, then derive both the published `NonNull`
            // and the returned `&mut` from its final resting place. Taking the
            // pointer *before* moving `boxed` into `self` would give it stale
            // provenance under Stacked Borrows (Box retags on move and asserts
            // uniqueness, invalidating any previously-derived raw pointer).
            self.owned_client_transpiler = Some(boxed);
            let ct: &mut Transpiler<'a> = self.owned_client_transpiler.as_deref_mut().unwrap();
            self.client_transpiler = Some(NonNull::from(&mut *ct).into());
            Ok(ct)
        }

        /// By calling this function, it implies that the returned log *will* be
        /// written to. For DevServer, this allocates a per-file log for the sources
        /// it is called on. Function must be called on the bundle thread.
        pub fn log_for_resolution_failures(
            &mut self,
            abs_path: &[u8],
            bake_graph: bake::Graph,
        ) -> &mut bun_ast::Log {
            if let Some(dev) = self.dev_server_handle() {
                // CYCLEBREAK GENUINE: DevServer → vtable. PERF(port): was inline switch.
                // SAFETY: owner is a live *mut DevServer per handle invariant.
                return unsafe { &mut *dev.log_for_resolution_failures(abs_path, bake_graph) };
            }
            // SAFETY: `transpiler.log` is set from a live `*mut Log` in `init` and
            // outlives `BundleV2`.
            self.transpiler.log_mut()
        }
    }

    pub struct ReachableFileVisitor<'a> {
        pub reachable: Vec<Index>,
        pub visited: DynamicBitSet,
        pub all_import_records: &'a mut [import_record::List<'a>],
        pub all_loaders: &'a [Loader],
        pub all_urls_for_css: &'a [&'a [u8]],
        pub redirects: &'a [u32],
        // PORT NOTE: Zig copied the map by value (cheap shallow copy). The Rust
        // `PathToSourceIndexMap` is `!Clone` and the field is unread in `visit`, so
        // store a raw backref to satisfy the struct shape without forcing `Clone`.
        pub redirect_map: *const PathToSourceIndexMap,
        pub dynamic_import_entry_points: &'a mut ArrayHashMap<IndexInt, ()>,
        /// Files which are Server Component Boundaries
        pub scb_bitset: Option<DynamicBitSetUnmanaged>,
        pub scb_list: server_component_boundary::Slice<'a>,

        /// Files which are imported by JS and inlined in CSS
        pub additional_files_imported_by_js_and_inlined_in_css: &'a mut DynamicBitSetUnmanaged,
        /// Files which are imported by CSS and inlined in CSS
        pub additional_files_imported_by_css_and_inlined: &'a mut DynamicBitSetUnmanaged,
    }

    impl<'a> ReachableFileVisitor<'a> {
        const MAX_REDIRECTS: usize = 64;

        // Find all files reachable from all entry points. This order should be
        // deterministic given that the entry point order is deterministic, since the
        // returned order is the postorder of the graph traversal and import record
        // order within a given file is deterministic.
        pub fn visit<const CHECK_DYNAMIC_IMPORTS: bool>(
            &mut self,
            source_index: Index,
            was_dynamic_import: bool,
        ) {
            if source_index.is_invalid() {
                return;
            }

            if self.visited.is_set(source_index.get() as usize) {
                if CHECK_DYNAMIC_IMPORTS {
                    if was_dynamic_import {
                        self.dynamic_import_entry_points
                            .put(source_index.get(), ())
                            .expect("unreachable");
                    }
                }
                return;
            }
            self.visited.set(source_index.get() as usize);

            if let Some(scb_bitset) = &self.scb_bitset {
                if scb_bitset.is_set(source_index.get() as usize) {
                    let scb_index = self
                        .scb_list
                        .get_index(source_index.get())
                        .expect("unreachable");
                    self.visit::<CHECK_DYNAMIC_IMPORTS>(
                        Index::init(self.scb_list.list.items_reference_source_index()[scb_index]),
                        false,
                    );
                    self.visit::<CHECK_DYNAMIC_IMPORTS>(
                        Index::init(self.scb_list.list.items_ssr_source_index()[scb_index]),
                        false,
                    );
                }
            }

            let is_js = self.all_loaders[source_index.get() as usize].is_javascript_like();
            let is_css = self.all_loaders[source_index.get() as usize].is_css();

            let import_record_list_id = source_index;
            // when there are no import records, v index will be invalid
            if (import_record_list_id.get() as usize) < self.all_import_records.len() {
                // PORT NOTE: reshaped for borrowck — split borrow of all_import_records
                let import_records_len =
                    self.all_import_records[import_record_list_id.get() as usize].len() as usize;
                for ir_idx in 0..import_records_len {
                    let import_record = &mut self.all_import_records
                        [import_record_list_id.get() as usize]
                        .as_mut_slice()[ir_idx];
                    let mut other_source = import_record.source_index;
                    if other_source.is_valid() {
                        let mut redirect_count: usize = 0;
                        while let Some(redirect_id) =
                            get_redirect_id(self.redirects[other_source.get() as usize])
                        {
                            // PORT NOTE: reshaped for borrowck — copy out the redirect target's
                            // (source_index, path) before re-borrowing `all_import_records` mutably.
                            let (other_src_idx, other_path) = {
                                let other_import_records =
                                    self.all_import_records[other_source.get() as usize].as_slice();
                                let other_import_record =
                                    &other_import_records[redirect_id as usize];
                                (other_import_record.source_index, other_import_record.path)
                            };
                            let import_record = &mut self.all_import_records
                                [import_record_list_id.get() as usize]
                                .as_mut_slice()[ir_idx];
                            import_record.source_index = other_src_idx;
                            import_record.path = other_path;
                            other_source = other_src_idx;
                            if redirect_count == Self::MAX_REDIRECTS {
                                import_record.path.is_disabled = true;
                                import_record.source_index = Index::INVALID;
                                break;
                            }

                            // Handle redirects to a builtin or external module
                            // https://github.com/oven-sh/bun/issues/3764
                            if !other_source.is_valid() {
                                break;
                            }
                            redirect_count += 1;
                        }

                        let import_record = &self.all_import_records
                            [import_record_list_id.get() as usize]
                            .as_slice()[ir_idx];
                        // Mark if the file is imported by JS and its URL is inlined for CSS
                        let is_inlined = import_record.source_index.is_valid()
                            && !self.all_urls_for_css[import_record.source_index.get() as usize]
                                .is_empty();
                        if is_js && is_inlined {
                            self.additional_files_imported_by_js_and_inlined_in_css
                                .set(import_record.source_index.get() as usize);
                        } else if is_css && is_inlined {
                            self.additional_files_imported_by_css_and_inlined
                                .set(import_record.source_index.get() as usize);
                        }

                        let next_source = import_record.source_index;
                        let kind_is_dynamic = import_record.kind == ImportKind::Dynamic;
                        self.visit::<CHECK_DYNAMIC_IMPORTS>(
                            next_source,
                            CHECK_DYNAMIC_IMPORTS && kind_is_dynamic,
                        );
                    }
                }

                // Redirects replace the source file with another file
                if let Some(redirect_id) =
                    get_redirect_id(self.redirects[source_index.get() as usize])
                {
                    let redirect_source_index = self.all_import_records
                        [source_index.get() as usize]
                        .as_slice()[redirect_id as usize]
                        .source_index
                        .get();
                    self.visit::<CHECK_DYNAMIC_IMPORTS>(
                        Index::source(redirect_source_index),
                        was_dynamic_import,
                    );
                    return;
                }
            }

            // Each file must come after its dependencies
            self.reachable.push(source_index);
            if CHECK_DYNAMIC_IMPORTS {
                if was_dynamic_import {
                    self.dynamic_import_entry_points
                        .put(source_index.get(), ())
                        .expect("unreachable");
                }
            }
        }
    }

    /// RAII guard returned by [`BundleV2::decrement_scan_counter_on_drop`].
    /// Decrements the bundle's pending-scan counter when dropped, mirroring Zig's
    /// `defer this.decrementScanCounter()` without holding a unique borrow across
    /// the body. Stores a raw pointer; caller guarantees the `BundleV2` outlives it.
    pub struct ScanCounterGuard {
        bv2: *mut BundleV2<'static>,
    }

    impl Drop for ScanCounterGuard {
        fn drop(&mut self) {
            // SAFETY: constructed from `&mut BundleV2` in
            // `decrement_scan_counter_on_drop`; the guard is a local that drops at
            // scope exit while the `BundleV2` it points to is still alive. The
            // lifetime is erased to `'static` only for storage — never observed.
            unsafe { (*self.bv2).decrement_scan_counter() };
        }
    }

    impl<'a> BundleV2<'a> {
        pub fn find_reachable_files(&mut self) -> Result<Box<[Index]>, Error> {
            // RAII guard — `Ctx` ends the span on Drop (Zig: `defer trace.end()`).
            let _trace = crate::perf::trace("Bundler.findReachableFiles");

            // Create a quick index for server-component boundaries.
            // We need to mark the generated files as reachable, or else many files will appear missing.
            // PERF(port): was stack-fallback
            let scb_bitset = if self.graph.server_component_boundaries.list.len() > 0 {
                Some(
                    self.graph
                        .server_component_boundaries
                        .slice()
                        .bit_set(self.graph.input_files.len())?,
                )
            } else {
                None
            };

            let mut additional_files_imported_by_js_and_inlined_in_css =
                DynamicBitSetUnmanaged::init_empty(self.graph.input_files.len())?;
            let mut additional_files_imported_by_css_and_inlined =
                DynamicBitSetUnmanaged::init_empty(self.graph.input_files.len())?;

            self.dynamic_import_entry_points = ArrayHashMap::new();

            // PORT NOTE: reshaped for borrowck — hoist the values that would
            // otherwise re-borrow `self`/`self.graph` while the visitor holds
            // disjoint column refs (Zig pulled multiple `items(.field)` columns at
            // once with no aliasing model).
            let redirect_map: *const PathToSourceIndexMap =
                std::ptr::from_ref(self.path_to_source_index_map(self.transpiler.options.target));
            // Always materialize a valid slice; when the boundary list is empty
            // this is a cheap `{ list: empty, map: &map }`. Avoids constructing a
            // null `&Map` via `mem::zeroed()` (UB even though it was never read
            // when `scb_bitset` is `None`).
            let scb_list = self.graph.server_component_boundaries.slice();

            // PORT NOTE: reshaped for borrowck — `Slice<T>` is a value-type
            // snapshot of column pointers (does not borrow `self.graph.ast`), so
            // `split_mut()` on the local can coexist with the shared borrows
            // below. The slab does not resize for the duration of this function.
            let mut ast_slice = self.graph.ast.slice();
            let all_import_records: &mut [import_record::List<'_>] =
                ast_slice.split_mut().import_records;
            let all_urls_for_css = self.graph.ast.items_url_for_css();

            let mut visitor = ReachableFileVisitor {
                reachable: Vec::with_capacity(self.graph.entry_points.len() + 1),
                visited: DynamicBitSet::init_empty(self.graph.input_files.len())?,
                redirects: self.graph.ast.items_redirect_import_record_index(),
                all_import_records,
                all_loaders: self.graph.input_files.items_loader(),
                all_urls_for_css,
                redirect_map,
                dynamic_import_entry_points: &mut self.dynamic_import_entry_points,
                scb_bitset,
                scb_list,
                additional_files_imported_by_js_and_inlined_in_css:
                    &mut additional_files_imported_by_js_and_inlined_in_css,
                additional_files_imported_by_css_and_inlined:
                    &mut additional_files_imported_by_css_and_inlined,
            };

            // If we don't include the runtime, __toESM or __toCommonJS will not get
            // imported and weird things will happen
            visitor.visit::<false>(Index::RUNTIME, false);

            if self.transpiler.options.code_splitting {
                for entry_point in self.graph.entry_points.iter().copied() {
                    visitor.visit::<true>(entry_point, false);
                }
            } else {
                for entry_point in self.graph.entry_points.iter().copied() {
                    visitor.visit::<false>(entry_point, false);
                }
            }

            if cfg!(debug_assertions) && ReachableFiles.is_visible() {
                bun_core::scoped_log!(
                    ReachableFiles,
                    "Reachable count: {} / {}",
                    visitor.reachable.len(),
                    self.graph.input_files.len()
                );
                let sources = self.graph.input_files.items_source();
                let targets = self.graph.ast.items_target();
                for idx in visitor.reachable.iter() {
                    let source = &sources[idx.get() as usize];
                    bun_core::scoped_log!(
                        ReachableFiles,
                        "reachable file: #{} {} ({}) target=.{}",
                        source.index.0,
                        bun_core::fmt::quote(source.path.pretty),
                        bstr::BStr::new(&source.path.text),
                        <&'static str>::from(targets[idx.get() as usize]),
                    );
                }
            }

            // PORT NOTE: reshaped for borrowck — release the visitor's `&mut`
            // borrows on the two bitsets and `input_files` columns before the
            // cleanup loop reads them.
            let ReachableFileVisitor { reachable, .. } = visitor;

            // PORT NOTE: reshaped for borrowck — three disjoint mutable SoA
            // columns via `split_mut()` on a value-type `Slice` snapshot.
            let mut input_files_slice = self.graph.input_files.slice();
            let input_files_cols = input_files_slice.split_mut();
            let additional_files: &mut [bun_alloc::AstVec<crate::AdditionalFile>] =
                input_files_cols.additional_files;
            let unique_keys: &mut [Box<[u8], bun_alloc::AstAlloc>] =
                input_files_cols.unique_key_for_additional_file;
            let content_hashes: &mut [u64] = input_files_cols.content_hash_for_additional_file;
            for (index, url_for_css) in all_urls_for_css.iter().enumerate() {
                if !url_for_css.is_empty() {
                    // We like to inline additional files in CSS if they fit a size threshold
                    // If we do inline a file in CSS, and it is not imported by JS, then we don't need to copy the additional file into the output directory
                    if additional_files_imported_by_css_and_inlined.is_set(index)
                        && !additional_files_imported_by_js_and_inlined_in_css.is_set(index)
                    {
                        additional_files[index].clear_retaining_capacity();
                        unique_keys[index] = bun_alloc::AstAlloc::vec().into_boxed_slice();
                        content_hashes[index] = 0;
                    }
                }
            }

            Ok(reachable.into_boxed_slice())
        }

        fn is_done(&mut self) -> bool {
            self.thread_lock.assert_locked();

            if self.graph.pending_items == 0 {
                let this: *mut Self = self;
                // PORT NOTE: reshaped for borrowck — Zig passed `&self.graph` and
                // `self` to the same call. Take a raw ptr so the two `&mut` don't
                // overlap from rustc's view.
                // SAFETY: `drain_deferred_tasks` only touches `self.graph.deferred_*`
                // fields and the `BundleV2` callback surface; no aliasing UB.
                if unsafe { (*this).graph.drain_deferred_tasks(&mut *this) } {
                    return false;
                }
                return true;
            }

            false
        }

        pub fn wait_for_parse(&mut self) {
            // bundle_v2.zig:488-491 — `this.loop().tick(this, &isDone)`.
            //
            // PORT NOTE: `tick_raw` (not `tick`) — `is_done` reborrows `*ctx` as
            // `&mut BundleV2`, and `BundleV2` (via `linker.r#loop`) owns the
            // `AnyEventLoop` slot, so holding `&mut AnyEventLoop` across the
            // callback would be a Stacked-Borrows violation.
            let self_ptr: *mut Self = self;
            let any_loop = self
                .r#loop()
                .expect("event loop not initialized for waitForParse")
                .as_ptr();
            // SAFETY: `any_loop` points into `self.linker.r#loop`, valid for the
            // duration of this call; `self_ptr` is the live `&mut self`. The
            // callback's `'static` lifetime erasure mirrors the Zig
            // `*anyopaque` cast — `is_done` only touches by-value fields.
            unsafe {
                bun_event_loop::AnyEventLoop::tick_raw(any_loop, self_ptr.cast(), |ctx| {
                    (*ctx.cast::<BundleV2<'static>>()).is_done()
                });
            }
            bun_core::scoped_log!(
                Bundle,
                "Parsed {} files, producing {} ASTs",
                self.graph.input_files.len(),
                self.graph.ast.len()
            );
        }

        /// `BUN_THREADPOOL_STATS=1` instrumentation hook — dump aggregate worker
        /// idle/busy time since the previous call. No-op when env var unset.
        #[inline]
        pub fn dump_pool_stats(&self, label: &str) {
            self.graph.pool().worker_pool().dump_stats(label);
        }

        pub fn scan_for_secondary_paths(&mut self) {
            if !self.graph.has_any_secondary_paths {
                // Assert the boolean is accurate.
                #[cfg(debug_assertions)]
                for secondary_path in self.graph.input_files.items_secondary_path() {
                    if !secondary_path.is_empty() {
                        panic!("secondary_path is not empty");
                    }
                }
                // No dual package hazard. Do nothing.
                return;
            }

            // Now that all files have been scanned, look for packages that are imported
            // both with "import" and "require". Rewrite any imports that reference the
            // "module" package.json field to the "main" package.json field instead.
            //
            // This attempts to automatically avoid the "dual package hazard" where a
            // package has both a CommonJS module version and an ECMAScript module
            // version and exports a non-object in CommonJS (often a function). If we
            // pick the "module" field and the package is imported with "require" then
            // code expecting a function will crash.
            //
            // PORT NOTE: reshaped for borrowck — Zig pulled the mutable
            // `import_records` column alongside shared columns. `split_mut()` on a
            // value-type `Slice` snapshot yields the one mutable column without
            // borrowing `self.graph.ast`; read the per-target map through the
            // disjoint `build_graphs` field instead of the `&mut self` accessor.
            let mut ast_slice = self.graph.ast.slice();
            let ast_import_records: &mut [import_record::List<'_>] =
                ast_slice.split_mut().import_records;
            let targets = self.graph.ast.items_target();
            let max_valid_source_index = Index::init(self.graph.input_files.len());
            let secondary_paths = self.graph.input_files.items_secondary_path();
            let sources = self.graph.input_files.items_source();

            debug_assert_eq!(ast_import_records.len(), targets.len());
            for (ast_import_record_list, target) in
                ast_import_records.iter_mut().zip(targets.iter())
            {
                let import_records = ast_import_record_list.as_mut_slice();
                let path_to_source_index_map = &self.graph.build_graphs[*target];
                for import_record in import_records.iter_mut() {
                    let source_index = import_record.source_index.get();
                    if source_index >= max_valid_source_index.get() {
                        continue;
                    }
                    let secondary_path: &[u8] = &secondary_paths[source_index as usize];
                    if !secondary_path.is_empty() {
                        let Some(secondary_source_index) =
                            path_to_source_index_map.get(secondary_path)
                        else {
                            continue;
                        };
                        import_record.source_index = Index::init(secondary_source_index);
                        // Keep path in sync for determinism, diagnostics, and dev tooling.
                        import_record.path = sources[secondary_source_index as usize].path;
                    }
                }
            }
        }

        /// This runs on the Bundle Thread.
        pub fn run_resolver(
            &mut self,
            import_record: &jsc_api::JSBundler::MiniImportRecord,
            target: options::Target,
        ) {
            // PORT NOTE: reshaped for borrowck — Zig held a `*Transpiler` raw pointer alongside
            // other `this.*` accesses. `transpiler_for_target` borrows `&mut self`, so launder
            // through a raw pointer to keep `*self` available below.
            // SAFETY: the returned `&mut Transpiler` lives for `'a` (set in `init`), is not
            // invalidated by anything called here, and Zig aliased it identically.
            let transpiler: *mut Transpiler<'a> = self.transpiler_for_target(target);
            let source_dir =
                Fs::PathName::init(&import_record.source_file).dir_with_trailing_slash();

            // Check the FileMap first for in-memory files
            if let Some(file_map) = self.file_map {
                if let Some(_file_map_result) = file_map.resolve(
                    self.arena(),
                    &import_record.source_file,
                    &import_record.specifier,
                ) {
                    let file_map_result = _file_map_result;
                    let mut path_primary = file_map_result.path_pair.primary;
                    // PORT NOTE: reshaped for borrowck — `get_or_put` borrows `*self` mutably via
                    // `self.graph`; capture the slot as `*mut u32` so subsequent `self.*` calls
                    // type-check. SAFETY: `path_to_source_index_map(target)` is not mutated again
                    // until after the last `*value_ptr` access below.
                    let (found_existing, value_ptr): (bool, *mut u32) = {
                        let entry = self
                            .path_to_source_index_map(target)
                            .get_or_put(path_primary.text)
                            .expect("oom");
                        (
                            entry.found_existing,
                            std::ptr::from_mut::<u32>(entry.value_ptr),
                        )
                    };
                    if !found_existing {
                        let loader: Loader = 'brk: {
                            let record: &mut ImportRecord =
                                &mut self.graph.ast.items_import_records_mut()
                                    [import_record.importer_source_index as usize]
                                    .as_mut_slice()
                                    [import_record.import_record_index as usize];
                            if let Some(out_loader) = record.loader {
                                break 'brk out_loader;
                            }
                            // SAFETY: see `transpiler` note above.
                            break 'brk Fs::Path::init(path_primary.text)
                                .loader(unsafe { &(*transpiler).options.loaders })
                                .unwrap_or(Loader::File);
                        };
                        // For virtual files, use the path text as-is (no relative path computation needed).
                        path_primary.pretty = self.arena().alloc_slice_copy(path_primary.text);
                        let mut tmp_source = bun_ast::Source {
                            path: path_as_static(&path_primary),
                            contents: std::borrow::Cow::Borrowed(&b""[..]),
                            ..Default::default()
                        };
                        let idx = self
                            .enqueue_parse_task(
                                &file_map_result,
                                &mut tmp_source,
                                loader,
                                import_record.original_target,
                            )
                            .expect("oom");
                        // SAFETY: see `value_ptr` note above.
                        unsafe { *value_ptr = idx };
                        let record: &mut ImportRecord =
                            &mut self.graph.ast.items_import_records_mut()
                                [import_record.importer_source_index as usize]
                                .as_mut_slice()
                                [import_record.import_record_index as usize];
                        record.source_index = Index::init(idx);
                    } else {
                        let record: &mut ImportRecord =
                            &mut self.graph.ast.items_import_records_mut()
                                [import_record.importer_source_index as usize]
                                .as_mut_slice()
                                [import_record.import_record_index as usize];
                        // SAFETY: see `value_ptr` note above.
                        record.source_index = Index::init(unsafe { *value_ptr });
                    }
                    return;
                }
            }

            let mut had_busted_dir_cache = false;
            let resolve_result: _resolver::Result = loop {
                // SAFETY: see `transpiler` note above.
                match unsafe { &mut *transpiler }.resolver.resolve(
                    source_dir,
                    &import_record.specifier,
                    import_record.kind,
                ) {
                    Ok(r) => break r,
                    Err(err) => {
                        // Only perform directory busting when hot-reloading is enabled
                        if err == bun_core::err!("ModuleNotFound") {
                            if let Some(dev) = &self.dev_server {
                                if !had_busted_dir_cache {
                                    // Only re-query if we previously had something cached.
                                    // SAFETY: see `transpiler` note above.
                                    if unsafe { &mut *transpiler }
                                        .resolver
                                        .bust_dir_cache_from_specifier(
                                            &import_record.source_file,
                                            &import_record.specifier,
                                        )
                                    {
                                        had_busted_dir_cache = true;
                                        continue;
                                    }
                                }

                                // Tell Bake's Dev Server to wait for the file to be imported.
                                dev.track_resolution_failure(
                                    &import_record.source_file,
                                    &import_record.specifier,
                                    target.bake_graph(),
                                    self.graph.input_files.items_loader()
                                        [import_record.importer_source_index as usize],
                                )
                                .expect("oom");

                                // Turn this into an invalid AST, so that incremental mode skips it when printing.
                                // SAFETY: truncating to len 0 never exposes uninitialized elements.
                                unsafe {
                                    self.graph.ast.items_parts_mut()
                                        [import_record.importer_source_index as usize]
                                        .set_len((0) as usize)
                                };
                            }
                        }

                        let handles_import_errors;
                        // PORT NOTE: reshaped for borrowck — `log_for_resolution_failures` borrows
                        // `&mut self`; the returned log is backed by either a DevServer-owned slot or
                        // `*self.transpiler.log` (both raw-pointer-derived), so detach the lifetime
                        // so `self.graph.*` / `self.transpiler.*` reads below type-check.
                        // SAFETY: log lives in DevServer / transpiler, disjoint from `self.graph`.
                        let log: &mut bun_ast::Log = unsafe {
                            bun_ptr::detach_lifetime_mut(self.log_for_resolution_failures(
                                &import_record.source_file,
                                target.bake_graph(),
                            ))
                        };

                        {
                            let record: &mut ImportRecord =
                                &mut self.graph.ast.items_import_records_mut()
                                    [import_record.importer_source_index as usize]
                                    .as_mut_slice()
                                    [import_record.import_record_index as usize];
                            handles_import_errors = record
                                .flags
                                .contains(bun_ast::ImportRecordFlags::HANDLES_IMPORT_ERRORS);

                            // Disable failing packages from being printed.
                            // This may cause broken code to write.
                            // However, doing this means we tell them all the resolve errors
                            // Rather than just the first one.
                            record.path.is_disabled = true;
                        }
                        let source: Option<&bun_ast::Source> = Some(
                            &self.graph.input_files.items_source()
                                [import_record.importer_source_index as usize],
                        );

                        if err == bun_core::err!("ModuleNotFound") {
                            let add_error = bun_ast::Log::add_resolve_error_with_text_dupe;
                            let path_to_use = &import_record.specifier;

                            if !handles_import_errors
                                && !self.transpiler.options.ignore_module_resolution_errors
                            {
                                if is_package_path(&import_record.specifier) {
                                    if target == Target::Browser
                                        && options::is_node_builtin(path_to_use)
                                    {
                                        add_error(
                                            log,
                                            source,
                                            import_record.range,
                                            format_args!(
                                                "Browser build cannot {} Node.js module: \"{}\". To use Node.js builtins, set target to 'node' or 'bun'",
                                                bstr::BStr::new(import_record.kind.error_label()),
                                                bstr::BStr::new(path_to_use)
                                            ),
                                            path_to_use,
                                            import_record.kind,
                                        );
                                    } else {
                                        add_error(
                                            log,
                                            source,
                                            import_record.range,
                                            format_args!(
                                                "Could not resolve: \"{}\". Maybe you need to \"bun install\"?",
                                                bstr::BStr::new(path_to_use)
                                            ),
                                            path_to_use,
                                            import_record.kind,
                                        );
                                    }
                                } else {
                                    add_error(
                                        log,
                                        source,
                                        import_record.range,
                                        format_args!(
                                            "Could not resolve: \"{}\"",
                                            bstr::BStr::new(path_to_use)
                                        ),
                                        path_to_use,
                                        import_record.kind,
                                    );
                                }
                            }
                        }
                        // assume other errors are already in the log
                        return;
                    }
                }
            };
            let mut resolve_result = resolve_result;

            let out_source_index: Option<Index>;

            // PORT NOTE(borrowck): Zig held a `*Fs.Path` into `resolve_result` while
            // also reading other fields and re-borrowing `self`. Rust borrowck rejects
            // that, so we clone the active path out and operate on an owned value.
            let mut path: Fs::Path<'static> = match resolve_result.path() {
                Some(p) => *p,
                None => {
                    let record: &mut ImportRecord = &mut self.graph.ast.items_import_records_mut()
                        [import_record.importer_source_index as usize]
                        .as_mut_slice()[import_record.import_record_index as usize];
                    // Disable failing packages from being printed.
                    // This may cause broken code to write.
                    // However, doing this means we tell them all the resolve errors
                    // Rather than just the first one.
                    record.path.is_disabled = true;
                    return;
                }
            };

            if resolve_result.flags.is_external() {
                return;
            }

            if path.pretty.as_ptr() == path.text.as_ptr() {
                // TODO: outbase
                let rel = bun_paths::resolve_path::relative_platform::<
                    bun_paths::resolve_path::platform::Loose,
                    false,
                >(
                    bun_resolver::fs::FileSystem::get().top_level_dir, path.text
                );
                // SAFETY: arena outlives the bundle pass; raw-pointer detour erases the
                // `&self` lifetime so the resulting `&'static [u8]` doesn't pin `self`.
                path.pretty =
                    unsafe { bun_ptr::detach_lifetime(self.arena().alloc_slice_copy(rel)) };
            }
            path.assert_pretty_is_valid();
            path.assert_file_path_is_absolute();

            // PORT NOTE(borrowck): split Zig's `getOrPut` into get-then-put so the map
            // borrow doesn't span `enqueue_parse_task` (which needs `&mut self`).
            if let Some(existing) = self.path_to_source_index_map(target).get(path.text) {
                out_source_index = Some(Index::init(existing));
            } else {
                path = self
                    .path_with_pretty_initialized(&path, target)
                    .expect("oom");
                // PORT NOTE: Zig wrote through `path.* = …` (a `*Fs.Path` into
                // `resolve_result.path_pair`); the borrowck-reshape above cloned
                // `path` out, so write the prettified path back so
                // `ParseTask::init(&resolve_result, ..)` (via `enqueue_parse_task`)
                // sees the relativized `pretty`.
                if let Some(p) = resolve_result.path() {
                    *p = path;
                }
                let loader: Loader = 'brk: {
                    let record: &ImportRecord = &self.graph.ast.items_import_records()
                        [import_record.importer_source_index as usize]
                        .as_slice()[import_record.import_record_index as usize];
                    if let Some(out_loader) = record.loader {
                        break 'brk out_loader;
                    }
                    // SAFETY: see `transpiler` note above.
                    break 'brk path
                        .loader(unsafe { &(*transpiler).options.loaders })
                        .unwrap_or(Loader::File);
                    // HTML is only allowed at the entry point.
                };
                let mut tmp_source = bun_ast::Source {
                    path: path_as_static(&path.dupe_alloc().expect("oom")),
                    contents: std::borrow::Cow::Borrowed(&b""[..]),
                    ..Default::default()
                };
                let idx = self
                    .enqueue_parse_task(
                        &resolve_result,
                        &mut tmp_source,
                        loader,
                        import_record.original_target,
                    )
                    .expect("oom");
                self.path_to_source_index_map(target)
                    .put(path.text, idx)
                    .expect("oom");
                out_source_index = Some(Index::init(idx));

                if let Some(secondary) = &resolve_result.path_pair.secondary {
                    if !secondary.is_disabled && !strings::eql_long(secondary.text, path.text, true)
                    {
                        self.graph.input_files.items_secondary_path_mut()[idx as usize] =
                            bun_alloc::AstAlloc::vec_from_slice(secondary.text);
                        // Ensure the determinism pass runs.
                        self.graph.has_any_secondary_paths = true;
                    }
                }

                // For non-javascript files, make all of these files share indices.
                // For example, it is silly to bundle index.css depended on by client+server twice.
                // It makes sense to separate these for JS because the target affects DCE
                if self.transpiler.options.server_components && !loader.is_javascript_like() {
                    // PORT NOTE: reshaped for borrowck — cannot hold two `&mut` into
                    // `self.graph` simultaneously, so re-derive the map per insert.
                    let key_text: Box<[u8]> = path.text.to_vec().into_boxed_slice();
                    let main_target = self.transpiler.options.target;
                    let separate_ssr = self
                        .framework
                        .as_ref()
                        .unwrap()
                        .server_components
                        .as_ref()
                        .unwrap()
                        .separate_ssr_graph;
                    let (ta, tb) = match target {
                        Target::Browser => (main_target, Target::BakeServerComponentsSsr),
                        Target::BakeServerComponentsSsr => (main_target, Target::Browser),
                        _ => (Target::Browser, Target::BakeServerComponentsSsr),
                    };
                    self.path_to_source_index_map(ta)
                        .put(&key_text, idx)
                        .expect("oom");
                    if separate_ssr {
                        self.path_to_source_index_map(tb)
                            .put(&key_text, idx)
                            .expect("oom");
                    }
                }
            }

            if let Some(source_index) = out_source_index {
                let record: &mut ImportRecord = &mut self.graph.ast.items_import_records_mut()
                    [import_record.importer_source_index as usize]
                    .as_mut_slice()[import_record.import_record_index as usize];
                record.source_index = source_index;
            }
        }

        pub fn enqueue_file_from_dev_server_incremental_graph_invalidation(
            &mut self,
            path_slice: &[u8],
            target: options::Target,
        ) -> Result<(), Error> {
            // TODO: plugins with non-file namespaces
            // PORT NOTE(borrowck): split Zig's `getOrPut` into get-then-put so the map
            // borrow doesn't span the resolver / `&mut self` calls below.
            if self
                .path_to_source_index_map(target)
                .get(path_slice)
                .is_some()
            {
                return Ok(());
            }
            let mut result = match self
                .transpiler_for_target(target)
                .resolve_entry_point(path_slice)
            {
                Ok(r) => r,
                Err(_) => return Ok(()),
            };
            let mut path = result.path_pair.primary;
            self.increment_scan_counter();
            let source_index = Index::source(self.graph.input_files.len() as u32);
            let loader = path
                .loader(&self.transpiler.options.loaders)
                .unwrap_or(Loader::File);

            path = self.path_with_pretty_initialized(&path, target)?;
            path.assert_pretty_is_valid();
            // PORT NOTE: see `enqueue_entry_item` — write the prettified path back
            // into `result` so `ParseTask::init(&result, ..)` reads the relativized
            // `pretty` (Zig mutated `result.path_pair.primary` in place via `path.*`).
            result.path_pair.primary = path;
            self.path_to_source_index_map(target)
                .put(path_slice, source_index.get())
                .expect("oom");
            let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // OOM/capacity: Zig aborts; port keeps fire-and-forget

            self.graph.input_files.append(crate::Graph::InputFile {
                source: bun_ast::Source {
                    path: path_as_static(&path),
                    contents: std::borrow::Cow::Borrowed(&b""[..]),
                    index: bun_ast::Index(source_index.get()),
                    ..Default::default()
                },
                loader,
                side_effects: result.primary_side_effects_data,
                ..Default::default()
            })?;
            // Arena-owned (Zig: `arena.create(ParseTask)`); freed on heap reset.
            let task_val = ParseTask::init(&result, source_index, self);
            // SAFETY: arena outlives the bundle pass; reborrow `*mut` as `&mut`.
            let task: &mut ParseTask = self.arena_create(task_val);
            task.loader = Some(loader);
            task.task.node.next = core::ptr::null_mut();
            task.tree_shaking = self.linker.options.tree_shaking;
            task.known_target = target;
            {
                let t = self.transpiler_for_target(target);
                task.jsx.development = match t.options.force_node_env {
                    options::ForceNodeEnv::Development => true,
                    options::ForceNodeEnv::Production => false,
                    options::ForceNodeEnv::Unspecified => t.options.jsx.development,
                };
            }

            // Handle onLoad plugins as entry points
            if !self.enqueue_on_load_plugin_if_needed(task) {
                if loader.should_copy_for_bundling() {
                    let additional_files: &mut bun_alloc::AstVec<crate::AdditionalFile> =
                        &mut self.graph.input_files.items_additional_files_mut()
                            [source_index.get() as usize];
                    additional_files
                        .push(crate::AdditionalFile::SourceIndex(task.source_index.get()));
                    self.graph.input_files.items_side_effects_mut()[source_index.get() as usize] =
                        bun_ast::SideEffects::NoSideEffectsPureData;
                    self.graph.estimated_file_loader_count += 1;
                }

                self.graph.pool().schedule(task);
            }
            Ok(())
        }

        pub fn enqueue_entry_item(
            &mut self,
            resolve: &mut _resolver::Result,
            is_entry_point: bool,
            target: options::Target,
        ) -> Result<Option<IndexInt>, Error> {
            let result = &mut *resolve;
            // PORT NOTE(borrowck): clone the active path out so we don't hold a `&mut`
            // into `result` across the `&mut self` calls below.
            let mut path: Fs::Path<'static> = match result.path() {
                Some(p) => *p,
                None => return Ok(None),
            };

            path.assert_file_path_is_absolute();
            // PORT NOTE(borrowck): split Zig's `getOrPut` into get-then-put.
            if self
                .path_to_source_index_map(target)
                .get(path.text)
                .is_some()
            {
                return Ok(None);
            }
            self.increment_scan_counter();
            let source_index = Index::source(self.graph.input_files.len() as u32);

            let loader = path
                .loader(&self.transpiler.options.loaders)
                .unwrap_or(Loader::File);

            // SAFETY: `path_with_pretty_initialized` allocates into `self.graph.heap`, which
            // outlives the bundle pass; erase the arena lifetime back to the resolver's
            // `Path<'static>` alias so `path` doesn't keep `self` borrowed.
            path = unsafe {
                self.path_with_pretty_initialized(&path, target)?
                    .into_static()
            };
            path.assert_pretty_is_valid();
            // PORT NOTE: intern via `dupe_alloc` BEFORE writing back into `result` /
            // the path-to-source-index map. Zig didn't need this — its dev-server
            // `EntryPointList` keys borrow `dev.server_graph.bundled_files.keys()`
            // (DevServer-owned), and `genericPathWithPrettyInitialized` returns the
            // input `Path` unchanged for `node`-namespace built-ins (e.g.
            // `bun-framework-react/server.tsx`), so `path.text` stayed a borrow of
            // long-lived storage. The Rust port rebuilds a fresh
            // `bake_types::EntryPointList` with `Box<[u8]>` keys (DevServer.rs:3027)
            // that drops as soon as `enqueue_entry_points_dev_server` returns;
            // `resolve_with_framework` then lifetime-erases that key into the
            // returned `Path`, so without interning here `ParseTask.path.text` (and
            // the map key) would dangle once the entry-point list is freed —
            // surfacing as "Failed to load bundled module
            // 'bun-framework-react/server.tsx'" when the worker can no longer match
            // `built_in_modules`.
            path = path.dupe_alloc().expect("oom");
            // PORT NOTE: Zig's `var path = result.path()` is a `*Fs.Path` *into*
            // `result.path_pair`, so the `path.* = pathWithPrettyInitialized(...)`
            // assignment mutates the resolver result in place. The borrowck-reshape
            // above cloned `path` out, which left `result.path_pair` with the
            // unrelativized `pretty` — and `ParseTask::init(&result, ..)` reads
            // exactly that field, so the source comment header lost its
            // `top_level_dir`-relative path. Write the prettified path back here.
            if let Some(p) = result.path() {
                *p = path;
            }
            self.path_to_source_index_map(target)
                .put(path.text, source_index.get())
                .expect("oom");
            let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // OOM/capacity: Zig aborts; port keeps fire-and-forget

            let side_effects = result.primary_side_effects_data;
            self.graph.input_files.append(crate::Graph::InputFile {
                source: bun_ast::Source {
                    path: path_as_static(&path),
                    contents: std::borrow::Cow::Borrowed(&b""[..]),
                    index: bun_ast::Index(source_index.get()),
                    ..Default::default()
                },
                loader,
                side_effects,
                ..Default::default()
            })?;
            // Arena-owned (Zig: `arena.create(ParseTask)`); freed on heap reset.
            let task_val = ParseTask::init(result, source_index, self);
            // SAFETY: arena outlives the bundle pass; reborrow `*mut` as `&mut`.
            let task: &mut ParseTask = self.arena_create(task_val);
            task.loader = Some(loader);
            task.task.node.next = core::ptr::null_mut();
            task.tree_shaking = self.linker.options.tree_shaking;
            task.is_entry_point = is_entry_point;
            task.known_target = target;
            {
                let bundler = self.transpiler_for_target(target);
                task.jsx.development = match bundler.options.force_node_env {
                    options::ForceNodeEnv::Development => true,
                    options::ForceNodeEnv::Production => false,
                    options::ForceNodeEnv::Unspecified => bundler.options.jsx.development,
                };
            }

            // Handle onLoad plugins as entry points
            if !self.enqueue_on_load_plugin_if_needed(task) {
                if loader.should_copy_for_bundling() {
                    let additional_files: &mut bun_alloc::AstVec<crate::AdditionalFile> =
                        &mut self.graph.input_files.items_additional_files_mut()
                            [source_index.get() as usize];
                    additional_files
                        .push(crate::AdditionalFile::SourceIndex(task.source_index.get()));
                    self.graph.input_files.items_side_effects_mut()[source_index.get() as usize] =
                        bun_ast::SideEffects::NoSideEffectsPureData;
                    self.graph.estimated_file_loader_count += 1;
                }

                self.graph.pool().schedule(task);
            }

            self.graph
                .entry_points
                .push(bun_ast::Index::init(source_index.get()));

            Ok(Some(source_index.get()))
        }

        /// `heap` is not freed when `deinit`ing the BundleV2
        pub fn init(
            transpiler: &'a mut Transpiler<'a>,
            bake_options: Option<BakeOptions<'a>>,
            _alloc: &bun_alloc::Arena,
            event_loop: EventLoop,
            cli_watch_flag: bool,
            // Raw `NonNull` (not `&mut`): the JS-API path threads `WorkPool::get()`
            // (a `&'static` from `OnceLock`, concurrently read by workers) through
            // here into `ThreadPool::init`, which stores it as `*mut`. Creating a
            // `&mut` along the way would violate Stacked Borrows.
            thread_pool: Option<NonNull<ThreadPoolLib>>,
            heap: &'a ThreadLocalArena,
        ) -> Result<Box<BundleV2<'a>>, Error> {
            // TODO(port): arena-allocate self via bump.alloc — Box::new is wrong arena (Zig: arena.create(@This()) on arena)
            transpiler.env().load_tracy();

            transpiler.options.mark_builtins_as_external =
                transpiler.options.target.is_bun() || transpiler.options.target == Target::Node;
            transpiler.resolver.opts.mark_builtins_as_external =
                transpiler.options.target.is_bun() || transpiler.options.target == Target::Node;

            // SAFETY: aliased *mut for `ssr_transpiler` (Zig stored both as raw ptrs).
            let ssr_alias: *mut Transpiler<'a> = std::ptr::from_mut(transpiler);
            let mut this = Box::new(BundleV2 {
                transpiler,
                client_transpiler: None,
                owned_client_transpiler: None,
                ssr_transpiler: ssr_alias,
                framework: None,
                graph: Graph {
                    pool: bun_ptr::BackRef::from(NonNull::<ThreadPool>::dangling()), // set below
                    heap,
                    kit_referenced_server_data: false,
                    kit_referenced_client_data: false,
                    ..Graph::new(heap)
                },
                linker: LinkerContext {
                    r#loop: event_loop,
                    graph: LinkerGraph::default(),
                    ..Default::default()
                },
                bun_watcher: None,
                plugins: None,
                completion: None,
                dev_server: None,
                file_map: None,
                source_code_length: 0,
                thread_lock: bun_core::ThreadLock::init_locked(),
                resolve_tasks_waiting_for_import_source_index: ArrayHashMap::new(),
                free_list: Vec::new(),
                unique_key: 0,
                dynamic_import_entry_points: ArrayHashMap::new(),
                has_on_parse_plugins: false,
                finalizers: Vec::new(),
                drain_defer_task: DeferredBatchTask::default(),
                asynchronous: false,
                has_any_top_level_await_modules: false,
                requested_exports: Vec::new(),
            });
            if let Some(bo) = bake_options {
                this.client_transpiler = Some(bo.client_transpiler.into());
                this.ssr_transpiler = bo.ssr_transpiler.as_ptr();
                let separate_ssr = bo
                    .framework
                    .server_components
                    .as_ref()
                    .map(|sc| sc.separate_ssr_graph)
                    .unwrap_or(false);
                this.framework = Some(bo.framework);
                this.linker.framework = this.framework.as_ref().map(bun_ptr::BackRef::new);
                this.plugins = bo.plugins;
                if this.transpiler.options.server_components {
                    debug_assert!(
                        this.client_transpiler_ref()
                            .unwrap()
                            .options
                            .server_components
                    );
                    if separate_ssr {
                        // SAFETY: `separate_ssr` ⇒ `ssr_transpiler` was set in `init` and lives for `'a`.
                        debug_assert!(unsafe { (*this.ssr_transpiler).options.server_components });
                    }
                }
            }
            // PORT NOTE: Zig wired `heap.arena()` into `transpiler.arena` /
            // `resolver.arena` / `linker.arena` / `log.msgs.arena`. The
            // Rust `Transpiler<'a>`/`Resolver<'a>` store `&'a Arena` and `Log.msgs`
            // is a `Vec` (global alloc), so only `linker.graph.bump` needs the
            // backref into the now-stable `this.graph.heap` slot.
            this.linker.graph.bump = bun_ptr::BackRef::new(this.graph.heap);
            this.transpiler.log_mut().clone_line_text = true;

            // We don't expose an option to disable this. Bake forbids tree-shaking
            // since every export must is always exist in case a future module
            // starts depending on it.
            if this.transpiler.options.output_format == options::Format::InternalBakeDev {
                this.transpiler.options.tree_shaking = false;
                this.transpiler.resolver.opts.tree_shaking = false;
            } else {
                this.transpiler.options.tree_shaking = true;
                this.transpiler.resolver.opts.tree_shaking = true;
            }

            // BACKREF: `LinkerContext<'a>.resolver` is `ParentRef<Resolver<'a>>`;
            // the resolver lives in `transpiler` which outlives `self` (same `'a`).
            this.linker.resolver = Some(bun_ptr::ParentRef::new(&this.transpiler.resolver));
            this.linker.graph.code_splitting = this.transpiler.options.code_splitting;

            this.linker.options.minify_syntax = this.transpiler.options.minify_syntax;
            this.linker.options.minify_identifiers = this.transpiler.options.minify_identifiers;
            this.linker.options.minify_whitespace = this.transpiler.options.minify_whitespace;
            this.linker.options.emit_dce_annotations = this.transpiler.options.emit_dce_annotations;
            this.linker.options.ignore_dce_annotations =
                this.transpiler.options.ignore_dce_annotations;
            // SAFETY: `transpiler.options.{banner,footer,public_path,metafile_*}` are
            // owned by the `'a`-lifetime `Transpiler` which outlives `this.linker`;
            // `LinkerOptions` stores `&'static [u8]` as an arena-erased lifetime
            // (see `interned_slice` contract — these are bundle-pass-interned).
            this.linker.options.banner = unsafe { interned_slice(&this.transpiler.options.banner) };
            // SAFETY: same `'a`-owned `Transpiler` field as `banner` above.
            this.linker.options.footer = unsafe { interned_slice(&this.transpiler.options.footer) };
            this.linker.options.css_chunking = this.transpiler.options.css_chunking;
            this.linker.options.compile_to_standalone_html =
                this.transpiler.options.compile_to_standalone_html;
            this.linker.options.source_maps = this.transpiler.options.source_map;
            this.linker.options.tree_shaking = this.transpiler.options.tree_shaking;
            // SAFETY: same `'a`-owned `Transpiler` field as `banner` above.
            this.linker.options.public_path =
                unsafe { interned_slice(&this.transpiler.options.public_path) };
            this.linker.options.target = this.transpiler.options.target;
            this.linker.options.output_format = this.transpiler.options.output_format;
            this.linker.options.generate_bytecode_cache = this.transpiler.options.bytecode;
            this.linker.options.compile = this.transpiler.options.compile;
            this.linker.options.metafile = this.transpiler.options.metafile;
            // SAFETY: same `'a`-owned `Transpiler` field as `banner` above.
            this.linker.options.metafile_json_path =
                unsafe { interned_slice(&this.transpiler.options.metafile_json_path) };
            // SAFETY: same `'a`-owned `Transpiler` field as `banner` above.
            this.linker.options.metafile_markdown_path =
                unsafe { interned_slice(&this.transpiler.options.metafile_markdown_path) };

            this.linker.dev_server = this.dev_server;

            // Arena-owned (Zig: `arena.create(ThreadPool)`). Coerce to `*mut`
            // immediately so the `&this` borrow from `arena()` ends before
            // `ThreadPool::init` takes `&mut this`.
            let pool: *mut ThreadPool =
                std::ptr::from_mut(this.arena().alloc(ThreadPool::default()));
            // errdefer this.graph.heap.deinit() — Drop handles arena teardown.

            // SAFETY: arena slot is live for the bundle pass; the default value
            // written above has no Drop, so overwriting via `*pool = ...` is fine.
            unsafe {
                *pool = ThreadPool::init(&*this, thread_pool)?;
            }
            this.graph.pool =
                bun_ptr::BackRef::from(NonNull::new(pool).expect("arena allocation is non-null"));
            // Install the watcher only after `ThreadPool::init()` has succeeded —
            // the `?` above is the last early-return in this fn, so the watcher's
            // raw `*mut BundleV2` can't outlive the box it points at. (Zig installs
            // it before `pool.* = try .init(..)`, but the Rust caller drops the box
            // on every error path until `generate_from_cli` leaks it.)
            if cli_watch_flag {
                // CYCLEBREAK GENUINE: hot_reloader is T6; runtime constructs the
                // `dispatch::WatcherHandle` (erased owner + `&'static WatcherVTable`)
                // via this extern hook and writes `bun_watcher` (Zig:
                // `Watcher.enableHotModuleReloading(this, null)` — bundle_v2.zig:994).
                dispatch::enable_hot_module_reloading_for_bundler(core::ptr::from_mut(&mut *this));
            }
            // `Graph::pool` wraps the `BackRef` deref; `start()` takes `&self`.
            this.graph.pool().start();
            Ok(this)
        }

        pub fn arena(&self) -> &'a bun_alloc::Arena {
            self.graph.heap
        }

        /// Allocate `value` into the bundler's arena (`self.graph.heap`) and return
        /// a `&'r mut T` whose lifetime is decoupled from `&self`. Mirrors Zig
        /// `arena.create(T)` — the arena owns the slab and reclaims it on
        /// `deinit_without_freeing_arena` / `heap.reset()`. The unbounded `'r`
        /// releases the `&self` borrow at the call site so callers can immediately
        /// reborrow `&mut self` (PORTING.md §Allocators: `bump.alloc(init)` →
        /// `&'bump mut T`).
        ///
        /// SAFETY (encapsulated): the arena slab is pinned and outlives every
        /// `&mut T` handed out here (freed only at `heap.reset()` after all
        /// callers are done); each call returns a fresh disjoint slot, so the
        /// resulting `&mut T` is unique.
        #[inline]
        #[allow(clippy::mut_from_ref)]
        fn arena_create<'r, T>(&self, value: T) -> &'r mut T {
            // SAFETY: arena slot is fresh + pinned for the bundle pass; see fn doc.
            unsafe { bun_ptr::detach_lifetime_mut(self.arena().alloc(value)) }
        }

        pub fn increment_scan_counter(&mut self) {
            self.thread_lock.assert_locked();
            self.graph.pending_items += 1;
            bun_core::scoped_log!(
                scan_counter,
                ".pending_items + 1 = {}",
                self.graph.pending_items
            );
        }

        pub fn decrement_scan_counter(&mut self) {
            self.thread_lock.assert_locked();
            self.graph.pending_items -= 1;
            bun_core::scoped_log!(
                scan_counter,
                ".pending_items - 1 = {}",
                self.graph.pending_items
            );
            self.on_after_decrement_scan_counter();
        }

        pub fn on_after_decrement_scan_counter(&mut self) {
            if self.asynchronous && self.is_done() {
                let dev = self
                    .dev_server
                    .unwrap_or_else(|| panic!("No dev server attached in asynchronous bundle job"));
                self.finish_from_bake_dev_server(&dev).expect("oom");
            }
        }

        /// RAII form of Zig's `defer this.decrementScanCounter()`. Captures `self` as
        /// a raw pointer so the returned guard does not hold a `&mut` borrow for the
        /// rest of the scope; the caller must ensure `self` outlives the guard.
        pub fn decrement_scan_counter_on_drop(&mut self) -> ScanCounterGuard {
            ScanCounterGuard {
                bv2: std::ptr::from_mut::<BundleV2<'a>>(self).cast::<BundleV2<'static>>(),
            }
        }

        // PORT NOTE: split because data type varies by variant — cannot express `switch(variant)`-typed param with const-generic enum on stable
        // TODO(port): comptime variant enum param + dependent data type — split into three monomorphic fns
        pub fn enqueue_entry_points_normal<P: AsRef<[u8]>>(
            &mut self,
            data: &[P],
        ) -> Result<(), Error> {
            self.enqueue_entry_points_common()?;
            // (variant != .dev_server)
            self.reserve_source_indexes_for_bake()?;

            // Setup entry points
            let num_entry_points = data.len();
            self.graph.entry_points.reserve(num_entry_points);
            self.graph
                .input_files
                .ensure_unused_capacity(num_entry_points)?;

            for entry_point in data {
                let entry_point: &[u8] = entry_point.as_ref();
                if self.enqueue_entry_point_on_resolve_plugin_if_needed(
                    entry_point,
                    self.transpiler.options.target,
                ) {
                    continue;
                }

                // Check FileMap first for in-memory entry points
                if let Some(file_map) = self.file_map {
                    if let Some(file_map_result) = file_map.resolve(self.arena(), b"", entry_point)
                    {
                        let _ = self.enqueue_entry_item(
                            &mut { file_map_result },
                            true,
                            self.transpiler.options.target,
                        )?;
                        continue;
                    }
                }

                // no plugins were matched
                let mut resolved = match self.transpiler.resolve_entry_point(entry_point) {
                    Ok(r) => r,
                    Err(_) => continue,
                };

                let target = 'brk: {
                    let main_target = self.transpiler.options.target;
                    if main_target.is_server_side() {
                        if let Some(path) = resolved.path_const() {
                            if let Some(loader) = path.loader(&self.transpiler.options.loaders) {
                                if loader == Loader::Html {
                                    self.ensure_client_transpiler();
                                    break 'brk Target::Browser;
                                }
                            }
                        }
                    }
                    break 'brk main_target;
                };
                let _ = self.enqueue_entry_item(&mut resolved, true, target)?;
            }
            Ok(())
        }

        pub fn enqueue_entry_points_dev_server(
            &mut self,
            files: &bake_types::EntryPointList,
            css_data: &mut ArrayHashMap<Index, CssEntryPointMeta>,
        ) -> Result<(), Error> {
            self.enqueue_entry_points_common()?;
            debug_assert!(self.dev_server.is_some());

            let num_entry_points = files.set.count();
            self.graph.entry_points.reserve(num_entry_points);
            self.graph
                .input_files
                .ensure_unused_capacity(num_entry_points)?;

            debug_assert_eq!(files.set.keys().len(), files.set.values().len());
            for (abs_path, flags) in files.set.keys().iter().zip(files.set.values().iter()) {
                // Ensure we have the proper conditions set for client-side entrypoints.
                // SAFETY: Zig stores `transpiler` as a raw `*Transpiler` across the loop body;
                // mirror with `*mut` so it doesn't keep `self` borrowed through the plugin
                // dispatch / dev_server calls below.
                let transpiler: *mut Transpiler<'a> =
                    if flags.client() && !flags.server() && !flags.ssr() {
                        std::ptr::from_mut(self.transpiler_for_target(Target::Browser))
                    } else {
                        &raw mut *self.transpiler
                    };
                let server_target = self.transpiler.options.target;

                struct TargetCheck {
                    should_dispatch: bool,
                    target: options::Target,
                }
                let targets_to_check = [
                    TargetCheck {
                        should_dispatch: flags.client(),
                        target: Target::Browser,
                    },
                    TargetCheck {
                        should_dispatch: flags.server(),
                        target: server_target,
                    },
                    TargetCheck {
                        should_dispatch: flags.ssr(),
                        target: Target::BakeServerComponentsSsr,
                    },
                ];

                let mut any_plugin_matched = false;
                for target_info in &targets_to_check {
                    if target_info.should_dispatch {
                        if self.enqueue_entry_point_on_resolve_plugin_if_needed(
                            abs_path,
                            target_info.target,
                        ) {
                            any_plugin_matched = true;
                        }
                    }
                }

                if any_plugin_matched {
                    continue;
                }

                // Fall back to normal resolution if no plugins matched
                // SAFETY: `transpiler` points at one of self's transpilers, live for `'a`.
                let mut resolved = match unsafe { &mut *transpiler }.resolve_entry_point(abs_path) {
                    Ok(r) => r,
                    Err(err) => {
                        let dev = self.dev_server.expect("unreachable");
                        dev.handle_parse_task_failure(
                            err,
                            if flags.client() {
                                bake::Graph::Client
                            } else {
                                bake::Graph::Server
                            },
                            abs_path,
                            // SAFETY: `transpiler` points at one of self's transpilers, live for `'a`.
                            unsafe { (*transpiler).log }.cast_const(),
                            std::ptr::from_mut(self),
                        )
                        .expect("oom");
                        // SAFETY: `transpiler.log` is the `'a`-owned log; sole writer here.
                        unsafe { (*(*transpiler).log).reset() };
                        continue;
                    }
                };

                if flags.client() {
                    'brk: {
                        let Some(source_index) =
                            self.enqueue_entry_item(&mut resolved, true, Target::Browser)?
                        else {
                            break 'brk;
                        };
                        if flags.css() {
                            css_data.put_no_clobber(
                                Index::init(source_index),
                                CssEntryPointMeta {
                                    imported_on_server: false,
                                },
                            )?;
                        }
                    }
                }
                if flags.server() {
                    let _ = self.enqueue_entry_item(
                        &mut resolved,
                        true,
                        self.transpiler.options.target,
                    )?;
                }
                if flags.ssr() {
                    let _ = self.enqueue_entry_item(
                        &mut resolved,
                        true,
                        Target::BakeServerComponentsSsr,
                    )?;
                }
            }
            Ok(())
        }

        pub fn enqueue_entry_points_bake_production(
            &mut self,
            data: &bake_types::production::EntryPointMap,
        ) -> Result<(), Error> {
            self.enqueue_entry_points_common()?;
            self.reserve_source_indexes_for_bake()?;

            let num_entry_points = data.files.count();
            self.graph.entry_points.reserve(num_entry_points);
            self.graph
                .input_files
                .ensure_unused_capacity(num_entry_points)?;

            for key in data.files.keys() {
                let abs_path = key.abs_path();
                let target = match key.side {
                    bake::Side::Client => Target::Browser,
                    bake::Side::Server => self.transpiler.options.target,
                };

                if self.enqueue_entry_point_on_resolve_plugin_if_needed(abs_path, target) {
                    continue;
                }

                // no plugins matched
                let mut resolved = match self.transpiler.resolve_entry_point(abs_path) {
                    Ok(r) => r,
                    Err(_) => continue,
                };

                // TODO: wrap client files so the exports arent preserved.
                let Some(_) = self.enqueue_entry_item(&mut resolved, true, target)? else {
                    continue;
                };
            }
            Ok(())
        }

        /// Common prelude shared by all enqueue_entry_points_* variants: add the runtime task.
        fn enqueue_entry_points_common(&mut self) -> Result<(), Error> {
            // Add the runtime
            let rt = ParseTask::get_runtime_source(self.transpiler.options.target);
            self.graph.input_files.append(crate::Graph::InputFile {
                source: rt.source,
                loader: Loader::Js,
                side_effects: bun_ast::SideEffects::NoSideEffectsPureData,
                ..Default::default()
            })?;

            // try this.graph.entry_points.append(arena, Index.runtime);
            let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // OOM/capacity: Zig aborts; port keeps fire-and-forget
            self.path_to_source_index_map(self.transpiler.options.target)
                .put(&b"bun:wrap"[..], Index::RUNTIME.get())
                .expect("oom");
            // SAFETY: arena (`self.graph.heap`) outlives the bundle pass; coerce the
            // `&mut ParseTask` to `*mut` immediately so the `&self` borrow from
            // `arena()` ends before we take `&mut self` below.
            let runtime_parse_task: *mut ParseTask = self.arena().alloc(rt.parse_task);
            // SAFETY: freshly arena-allocated above; no other references exist yet.
            unsafe {
                // BACKREF — lifetime erased per ParseTask::ctx convention.
                (*runtime_parse_task).ctx = Some(bun_ptr::ParentRef::from_raw_mut(
                    std::ptr::from_mut(self).cast::<BundleV2<'static>>(),
                ));
                (*runtime_parse_task).tree_shaking = true;
                (*runtime_parse_task).loader = Some(Loader::Js);
            }
            self.increment_scan_counter();
            self.graph.pool().schedule(runtime_parse_task);
            Ok(())
        }

        fn clone_ast(&mut self) -> Result<(), Error> {
            let _trace = crate::perf::trace("Bundler.cloneAST");
            // TODO(port): bun.safety.alloc.assertEq
            self.linker.graph.ast = self.graph.ast.clone()?;

            for module_scope in self.linker.graph.ast.items_module_scope_mut() {
                // `children` are arena-allocated `StoreRef<Scope>`s; we re-point
                // their `parent` BACKREF at the cloned module scope. `StoreRef`'s
                // safe `DerefMut` replaces the open-coded `unsafe { child.as_mut() }`.
                let parent_ptr = bun_ast::StoreRef::from(NonNull::from(&mut *module_scope));
                for child in module_scope.children.slice_mut() {
                    child.parent = Some(parent_ptr);
                }

                if FeatureFlags::HELP_CATCH_MEMORY_ISSUES {
                    /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */
                }

                module_scope.generated = module_scope.generated.clone();
            }

            // Some parts of the AST are owned by worker allocators at this point.
            // Transfer ownership to the graph heap.
            self.linker.graph.take_ast_ownership(self.graph.heap);
            Ok(())
        }

        /// This generates the two asts for 'bun:bake/client' and 'bun:bake/server'. Both are generated
        /// at the same time in one pass over the SCB list.
        pub fn process_server_component_manifest_files(&mut self) -> Result<(), AllocError> {
            // If a server components is not configured, do nothing
            let Some(fw) = &self.framework else {
                return Ok(());
            };
            let Some(sc) = &fw.server_components else {
                return Ok(());
            };

            if !self.graph.kit_referenced_server_data && !self.graph.kit_referenced_client_data {
                return Ok(());
            }

            // SAFETY: arena (`self.graph.heap`) outlives the bundle pass; erase the
            // `&self` borrow so `server`/`client` don't keep `*self` borrowed across
            // the `self.graph.ast.set(...)` calls at the end of this function.
            let alloc: &'static bun_alloc::Arena =
                unsafe { bun_ptr::detach_lifetime_ref::<bun_alloc::Arena>(self.arena()) };

            let hmr = self.transpiler.options.hot_module_reloading;
            let mut server = AstBuilder::init(alloc, &bake::SERVER_VIRTUAL_SOURCE, hmr)?;
            let mut client = AstBuilder::init(alloc, &bake::CLIENT_VIRTUAL_SOURCE, hmr)?;

            let mut server_manifest_props: Vec<G::Property> = Vec::new();
            let mut client_manifest_props: Vec<G::Property> = Vec::new();

            let scbs = self.graph.server_component_boundaries.list.slice();
            let named_exports_array = self.graph.ast.items_named_exports();

            let id_string = server.new_expr(E::EString {
                data: b"id".into(),
                ..Default::default()
            });
            let name_string = server.new_expr(E::EString {
                data: b"name".into(),
                ..Default::default()
            });
            let chunks_string = server.new_expr(E::EString {
                data: b"chunks".into(),
                ..Default::default()
            });
            let specifier_string = server.new_expr(E::EString {
                data: b"specifier".into(),
                ..Default::default()
            });
            let empty_array = server.new_expr(E::Array::default());

            for ((r#use, source_id), ssr_index) in scbs
                .items_use_directive()
                .iter()
                .zip(scbs.items_source_index().iter())
                .zip(scbs.items_ssr_source_index().iter())
            {
                if *r#use == bun_ast::UseDirective::Client {
                    // TODO: this file is being generated far too early. we
                    // don't know which exports are dead and which exports are
                    // live. Tree-shaking figures that out. However, tree-shaking
                    // happens after import binding, which would require this ast.
                    //
                    // The plan: change this to generate a stub ast which only has
                    // `export const serverManifest = undefined;`, and then
                    // re-generate this file later with the properly decided
                    // manifest. However, I will probably reconsider how this
                    // manifest is being generated when I write the whole
                    // "production build" part of Bake.

                    let keys = named_exports_array[*source_id as usize].keys();
                    // PORT NOTE: `G::Property: !Clone` — build via iterator instead of `vec![v; n]`.
                    let mut client_manifest_items: Box<[G::Property]> =
                        (0..keys.len()).map(|_| G::Property::default()).collect();

                    if !sc.separate_ssr_graph {
                        bun_core::todo_panic!("separate_ssr_graph=false");
                    }

                    // SAFETY: arena slice — `alloc` (== `self.graph.heap`) outlives
                    // the produced AST. See `interned_slice` contract.
                    let astr = |s: &[u8]| -> &'static [u8] { unsafe { interned_slice(s) } };

                    let client_path = server.new_expr(E::EString {
                        data: astr(
                            alloc.alloc_slice_copy(
                                format!(
                                    "{}",
                                    chunk::UniqueKey {
                                        prefix: self.unique_key,
                                        kind: chunk::QueryKind::Scb,
                                        index: *source_id
                                    }
                                )
                                .as_bytes(),
                            ),
                        )
                        .into(),
                        ..Default::default()
                    });
                    let ssr_path = server.new_expr(E::EString {
                        data: astr(
                            alloc.alloc_slice_copy(
                                format!(
                                    "{}",
                                    chunk::UniqueKey {
                                        prefix: self.unique_key,
                                        kind: chunk::QueryKind::Scb,
                                        index: *ssr_index
                                    }
                                )
                                .as_bytes(),
                            ),
                        )
                        .into(),
                        ..Default::default()
                    });

                    debug_assert_eq!(keys.len(), client_manifest_items.len());
                    for (export_name_string, client_item) in
                        keys.iter().zip(client_manifest_items.iter_mut())
                    {
                        let server_key_string = astr(
                            alloc.alloc_slice_copy(
                                format!(
                                    "{}#{}",
                                    chunk::UniqueKey {
                                        prefix: self.unique_key,
                                        kind: chunk::QueryKind::Scb,
                                        index: *source_id
                                    },
                                    bstr::BStr::new(export_name_string),
                                )
                                .as_bytes(),
                            ),
                        );
                        let export_name = server.new_expr(E::EString {
                            data: astr(export_name_string).into(),
                            ..Default::default()
                        });

                        // write dependencies on the underlying module, not the proxy
                        server_manifest_props.push(G::Property {
                            key: Some(server.new_expr(E::EString {
                                data: server_key_string.into(),
                                ..Default::default()
                            })),
                            value: Some(server.new_expr(E::Object {
                                properties: bun_ast::g::PropertyList::from_owned_slice(Box::new([
                                    G::Property {
                                        key: Some(id_string),
                                        value: Some(client_path),
                                        ..Default::default()
                                    },
                                    G::Property {
                                        key: Some(name_string),
                                        value: Some(export_name),
                                        ..Default::default()
                                    },
                                    G::Property {
                                        key: Some(chunks_string),
                                        value: Some(empty_array),
                                        ..Default::default()
                                    },
                                ])),
                                ..Default::default()
                            })),
                            ..Default::default()
                        });
                        *client_item = G::Property {
                            key: Some(export_name),
                            value: Some(server.new_expr(E::Object {
                                properties: bun_ast::g::PropertyList::from_owned_slice(Box::new([
                                    G::Property {
                                        key: Some(name_string),
                                        value: Some(export_name),
                                        ..Default::default()
                                    },
                                    G::Property {
                                        key: Some(specifier_string),
                                        value: Some(ssr_path),
                                        ..Default::default()
                                    },
                                ])),
                                ..Default::default()
                            })),
                            ..Default::default()
                        };
                    }

                    client_manifest_props.push(G::Property {
                        key: Some(client_path),
                        value: Some(server.new_expr(E::Object {
                            properties: bun_ast::g::PropertyList::from_owned_slice(
                                client_manifest_items,
                            ),
                            ..Default::default()
                        })),
                        ..Default::default()
                    });
                } else {
                    bun_core::todo_panic!("\"use server\"");
                }
            }

            let server_manifest_ref =
                server.new_symbol(bun_ast::symbol::Kind::Other, b"serverManifest")?;
            let server_manifest_value = server.new_expr(E::Object {
                properties: bun_ast::g::PropertyList::move_from_list(server_manifest_props),
                ..Default::default()
            });
            server.append_stmt(S::Local {
                kind: bun_ast::s::Kind::KConst,
                decls: bun_ast::g::DeclList::from_owned_slice(Box::new([G::Decl {
                    binding: Binding::alloc(
                        alloc,
                        bun_ast::b::Identifier {
                            r#ref: server_manifest_ref,
                        },
                        bun_ast::Loc::EMPTY,
                    ),
                    value: Some(server_manifest_value),
                }])),
                is_export: true,
                ..Default::default()
            })?;
            let ssr_manifest_ref =
                server.new_symbol(bun_ast::symbol::Kind::Other, b"ssrManifest")?;
            let ssr_manifest_value = server.new_expr(E::Object {
                properties: bun_ast::g::PropertyList::move_from_list(client_manifest_props),
                ..Default::default()
            });
            server.append_stmt(S::Local {
                kind: bun_ast::s::Kind::KConst,
                decls: bun_ast::g::DeclList::from_owned_slice(Box::new([G::Decl {
                    binding: Binding::alloc(
                        alloc,
                        bun_ast::b::Identifier {
                            r#ref: ssr_manifest_ref,
                        },
                        bun_ast::Loc::EMPTY,
                    ),
                    value: Some(ssr_manifest_value),
                }])),
                is_export: true,
                ..Default::default()
            })?;

            let server_ast: JSAst = server.to_bundled_ast(Target::Bun)?;
            let client_ast: JSAst = client.to_bundled_ast(Target::Browser)?;
            self.graph
                .ast
                .set(Index::BAKE_SERVER_DATA.get() as usize, server_ast);
            self.graph
                .ast
                .set(Index::BAKE_CLIENT_DATA.get() as usize, client_ast);
            Ok(())
        }

        pub fn enqueue_parse_task(
            &mut self,
            resolve_result: &_resolver::Result,
            source: &mut bun_ast::Source,
            loader: Loader,
            known_target: options::Target,
        ) -> Result<IndexInt, AllocError> {
            let source_index = Index::init(u32::try_from(self.graph.ast.len()).expect("int cast"));
            let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // OOM/capacity: Zig aborts; port keeps fire-and-forget

            self.graph.input_files.append(crate::Graph::InputFile {
                source: core::mem::take(source),
                loader,
                side_effects: loader.side_effects(),
                ..Default::default()
            })?;
            // PORT NOTE: `ParseTask::init` takes `bun_ast::Index`; both Index newtypes
            // are `repr(transparent)` u32 so reconstruct via `.get()`.
            // Arena-owned (Zig: `arena.create(ParseTask)`); freed on heap reset.
            let task_val = ParseTask::init(
                resolve_result,
                bun_ast::Index::init(source_index.get()),
                self,
            );
            // SAFETY: arena outlives the bundle pass; reborrow `*mut` as `&mut`.
            let task: &mut ParseTask = self.arena_create(task_val);
            task.loader = Some(loader);
            task.jsx = self.transpiler_for_target(known_target).options.jsx.clone();
            task.task.node.next = core::ptr::null_mut();
            task.io_task.node.next = core::ptr::null_mut();
            task.tree_shaking = self.linker.options.tree_shaking;
            task.known_target = known_target;

            self.increment_scan_counter();

            // Handle onLoad plugins
            if !self.enqueue_on_load_plugin_if_needed(task) {
                if loader.should_copy_for_bundling() {
                    let additional_files: &mut bun_alloc::AstVec<crate::AdditionalFile> =
                        &mut self.graph.input_files.items_additional_files_mut()
                            [source_index.get() as usize];
                    additional_files
                        .push(crate::AdditionalFile::SourceIndex(task.source_index.get()));
                    self.graph.input_files.items_side_effects_mut()[source_index.get() as usize] =
                        bun_ast::SideEffects::NoSideEffectsPureData;
                    self.graph.estimated_file_loader_count += 1;
                }

                self.graph.pool().schedule(task);
            }

            Ok(source_index.get())
        }

        pub fn enqueue_parse_task2(
            &mut self,
            source: &mut bun_ast::Source,
            loader: Loader,
            known_target: options::Target,
        ) -> Result<IndexInt, AllocError> {
            let source_index = Index::init(u32::try_from(self.graph.ast.len()).expect("int cast"));
            let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // OOM/capacity: Zig aborts; port keeps fire-and-forget

            self.graph.input_files.append(crate::Graph::InputFile {
                source: core::mem::take(source),
                loader,
                side_effects: loader.side_effects(),
                ..Default::default()
            })?;
            // `core::mem::take` moved the real `Source` into `graph.input_files`,
            // leaving `*source` as `Default`. Read path/contents back from the
            // graph's stored copy (where the data now lives for the rest of the
            // bundle pass) so the `ParseTask` below sees the actual source bytes —
            // matches Zig, which copies `source.*` by value and then reads the
            // still-intact original.
            let stored = &self.graph.input_files.items_source()[source_index.get() as usize];
            // PORT NOTE: Zig had a single `fs.Path`; Rust split it into
            // `bun_paths::fs::Path<'static>` (on `Source`) and `bun_resolver::fs::Path`
            // (on `ParseTask`). Convert field-by-field — `pretty`/`namespace` MUST
            // be preserved here (the SCB `separate_ssr_graph=false` caller passes a
            // source whose path went through `path_with_pretty_initialized`, and
            // `ParseTask::run` builds the `Source` from `task.path` then swaps it
            // back into `input_files`, so dropping `pretty` would surface the
            // absolute path as the dev-server module key).
            let task_path: Fs::Path<'static> = stored.path;
            // SAFETY: `graph.input_files` owns `stored.contents` for the bundle
            // pass (arena lifetime); erase the borrow to `'static` to fit
            // `ContentsOrFd::Contents`. See `interned_slice` contract.
            let contents: &'static [u8] = unsafe { interned_slice(stored.contents()) };
            // Compute borrow-heavy fields up front so the `&self` borrow taken by
            // `arena()` doesn't overlap `&mut self` uses inside the literal.
            let jsx = if known_target == Target::BakeServerComponentsSsr
                && !self
                    .framework
                    .as_ref()
                    .unwrap()
                    .server_components
                    .as_ref()
                    .unwrap()
                    .separate_ssr_graph
            {
                self.transpiler.options.jsx.clone()
            } else {
                self.transpiler_for_target(known_target).options.jsx.clone()
            };
            let tree_shaking = self.linker.options.tree_shaking;
            // SAFETY: arena (`self.graph.heap`) outlives the bundle pass; coerce the
            // `&mut ParseTask` to `*mut` immediately so the `&self` borrow from
            // `arena()` ends before we take `&mut self` below.
            let task: *mut ParseTask = self.arena().alloc(ParseTask {
                path: task_path,
                contents_or_fd: parse_task::ContentsOrFd::Contents(contents),
                side_effects: bun_ast::SideEffects::HasSideEffects,
                jsx,
                source_index: bun_ast::Index::init(source_index.get()),
                module_type: options::ModuleType::Unknown,
                emit_decorator_metadata: false, // TODO
                package_version: bun_ast::StoreStr::EMPTY,
                loader: Some(loader),
                tree_shaking,
                known_target,
                ..Default::default()
            });
            // SAFETY: `task` was just arena-allocated above; no other references exist yet.
            unsafe {
                // BACKREF — lifetime erased per ParseTask::ctx convention.
                (*task).ctx = Some(bun_ptr::ParentRef::from_raw_mut(
                    std::ptr::from_mut(self).cast::<BundleV2<'static>>(),
                ));
                (*task).task.node.next = core::ptr::null_mut();
                (*task).io_task.node.next = core::ptr::null_mut();
            }

            self.increment_scan_counter();

            // Handle onLoad plugins
            // SAFETY: `task` lives in the bundle-pass arena; sole reference until scheduled.
            if !self.enqueue_on_load_plugin_if_needed(unsafe { &mut *task }) {
                if loader.should_copy_for_bundling() {
                    let additional_files: &mut bun_alloc::AstVec<crate::AdditionalFile> =
                        &mut self.graph.input_files.items_additional_files_mut()
                            [source_index.get() as usize];
                    additional_files.push(crate::AdditionalFile::SourceIndex(source_index.get()));
                    self.graph.input_files.items_side_effects_mut()[source_index.get() as usize] =
                        bun_ast::SideEffects::NoSideEffectsPureData;
                    self.graph.estimated_file_loader_count += 1;
                }

                self.graph.pool().schedule(task);
            }
            Ok(source_index.get())
        }

        /// Enqueue a ServerComponentParseTask.
        /// `source_without_index` is copied and assigned a new source index. That index is returned.
        pub fn enqueue_server_component_generated_file(
            &mut self,
            data: crate::ServerComponentParseTask::Data,
            source_without_index: bun_ast::Source,
        ) -> Result<IndexInt, AllocError> {
            let mut new_source = source_without_index;
            let source_index = self.graph.input_files.len();
            new_source.index = bun_ast::Index(source_index as u32);
            // PORT NOTE: `bun_ast::Source: !Clone` — manually dup the (all-Clone) fields.
            let task_source = bun_ast::Source {
                path: new_source.path,
                contents: new_source.contents.clone(),
                contents_is_recycled: new_source.contents_is_recycled,
                identifier_name: new_source.identifier_name.clone(),
                index: new_source.index,
            };
            self.graph.input_files.append(crate::Graph::InputFile {
                source: new_source,
                loader: Loader::Js,
                side_effects: bun_ast::SideEffects::HasSideEffects,
                ..Default::default()
            })?;
            let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // OOM/capacity: Zig aborts; port keeps fire-and-forget

            // PORT NOTE: `bun.new(ServerComponentParseTask, …)` — heap-owned by the
            // worker pool; freed via `bun.destroy` in `on_complete` after the
            // result posts back to the bundle thread.
            let task = bun_core::heap::into_raw(Box::new(ServerComponentParseTask {
                data,
                // Lifetime-erase `'a` → `'static` for the BACKREF (matches Zig `*BundleV2`).
                // `NonNull::from(&mut *self)` carries write provenance for `assume_mut`
                // in `on_complete`; `ParentRef::from(NonNull)` is the safe wrapper.
                ctx: Some(bun_ptr::ParentRef::from(
                    core::ptr::NonNull::from(&mut *self).cast::<BundleV2<'static>>(),
                )),
                source: task_source,
                // `..Default::default()` supplies `task: ThreadPoolTask { callback: task_callback_wrap }`.
                ..Default::default()
            }));

            self.increment_scan_counter();

            // SAFETY: `task` is the just-allocated arena box; sole reference here.
            self.graph
                .pool()
                .worker_pool()
                .schedule(bun_threading::thread_pool::Batch::from(unsafe {
                    core::ptr::addr_of_mut!((*task).task)
                }));

            Ok(u32::try_from(source_index).expect("int cast"))
        }
    }

    pub struct DependenciesScanner {
        pub ctx: *mut (),
        pub entry_points: Box<[Box<[u8]>]>,
        pub on_fetch: fn(ctx: *mut (), result: &mut DependenciesScannerResult) -> Result<(), Error>,
    }

    pub struct DependenciesScannerResult<'r, 'a> {
        pub dependencies: bun_collections::StringSet,
        pub reachable_files: &'r [Index],
        pub bundle_v2: &'r mut BundleV2<'a>,
    }

    /// Callback contract for [`DependenciesScanner`]. Each call site's local
    /// `Analyzer` struct implements this; [`DependenciesScanner::new`] erases the
    /// concrete type behind a monomorphized trampoline — Rust's analogue of Zig's
    /// one-liner `.onFetch = @ptrCast(&Analyzer.onAnalyze)` (bundle_v2.zig:1492).
    pub trait OnDependenciesAnalyze {
        fn on_analyze(
            &mut self,
            result: &mut DependenciesScannerResult<'_, '_>,
        ) -> Result<(), Error>;
    }

    impl DependenciesScanner {
        /// Type-erase `analyzer` into the `(ctx, on_fetch)` pair. The returned
        /// scanner borrows `*analyzer` for its lifetime: caller must keep
        /// `analyzer` alive and exclusively owned until the scan completes
        /// (mirrors Zig's stack-local `Analyzer` + `*anyopaque` ctx pattern).
        pub fn new<A: OnDependenciesAnalyze>(
            analyzer: &mut A,
            entry_points: Box<[Box<[u8]>]>,
        ) -> Self {
            fn trampoline<A: OnDependenciesAnalyze>(
                ctx: *mut (),
                result: &mut DependenciesScannerResult,
            ) -> Result<(), Error> {
                // SAFETY: `ctx` was set from `&mut *analyzer` in `new`; the caller
                // contract guarantees `*analyzer` outlives the scanner and is not
                // otherwise borrowed, so reconstituting `&mut A` here is exclusive.
                let analyzer = unsafe { &mut *ctx.cast::<A>() };
                analyzer.on_analyze(result)
            }
            Self {
                ctx: core::ptr::from_mut(analyzer).cast::<()>(),
                entry_points,
                on_fetch: trampoline::<A>,
            }
        }
    }

    impl<'a> BundleV2<'a> {
        pub fn get_all_dependencies(
            &mut self,
            reachable_files: &[Index],
            fetcher: &DependenciesScanner,
        ) -> Result<(), Error> {
            // Find all external dependencies from reachable files
            let mut external_deps = bun_collections::StringSet::new();

            let import_records = self.graph.ast.items_import_records();

            for source_index in reachable_files {
                let records: &[ImportRecord] =
                    import_records[source_index.get() as usize].as_slice();
                for record in records {
                    if !record.source_index.is_valid()
                        && record.tag == bun_ast::ImportRecordTag::None
                    {
                        let path = &record.path.text;
                        // External dependency
                        if !path.is_empty()
                        // Check for either node or bun builtins
                        // We don't use the list from .bun because that includes third-party packages in some cases.
                        && !bun_resolve_builtins::HardcodedModule::Alias::has(path, Target::Node, Default::default())
                        && !path.starts_with(b"bun:")
                        && path != b"bun"
                        {
                            if strings::is_npm_package_name_ignore_length(path) {
                                external_deps.insert(path)?;
                            }
                        }
                    }
                }
            }
            let mut result = DependenciesScannerResult {
                dependencies: external_deps,
                bundle_v2: self,
                reachable_files,
            };
            (fetcher.on_fetch)(fetcher.ctx, &mut result)
        }

        pub fn generate_from_cli(
            transpiler: &'a mut Transpiler<'a>,
            alloc: &'a bun_alloc::Arena,
            event_loop: EventLoop,
            enable_reloading: bool,
            reachable_files_count: &mut usize,
            minify_duration: &mut u64,
            source_code_size: &mut u64,
            fetcher: Option<&DependenciesScanner>,
        ) -> Result<BuildResult, Error> {
            let mut this = BundleV2::init(
                transpiler,
                None,
                alloc,
                event_loop,
                enable_reloading,
                None,
                alloc,
            )?;
            this.unique_key = generate_unique_key();

            // Wrap so every exit path (incl. `?`) hits the cleanup below.
            let result = (|| -> Result<BuildResult, Error> {
                if this.transpiler.log().has_errors() {
                    return Err(bun_core::err!("BuildFailed"));
                }

                let entry_points: *const [Box<[u8]>] =
                    &raw const *this.transpiler.options.entry_points;
                // SAFETY: `transpiler.options.entry_points` is borrowed only for the duration
                // of `enqueue_entry_points_normal`, which never frees/reallocates it; raw-ptr
                // sidestep for the `&mut self` overlap (Zig stored both as raw `*Transpiler`).
                this.enqueue_entry_points_normal(unsafe { &*entry_points })?;

                if this.transpiler.log().has_errors() {
                    return Err(bun_core::err!("BuildFailed"));
                }

                this.wait_for_parse();
                this.dump_pool_stats("parse");

                *minify_duration = (((bun_core::time::nano_timestamp() as i64)
                    - (bun_core::start_time() as i64))
                    / (bun_core::time::NS_PER_MS as i64)) as u64;
                *source_code_size = this.source_code_length as u64;

                if this.transpiler.log().has_errors() {
                    return Err(bun_core::err!("BuildFailed"));
                }

                this.scan_for_secondary_paths();

                this.process_server_component_manifest_files()?;

                let reachable_files = this.find_reachable_files()?;
                *reachable_files_count = reachable_files.len().saturating_sub(1); // - 1 for the runtime

                this.process_files_to_copy(&reachable_files)?;

                this.add_server_component_boundaries_as_extra_entry_points()?;

                this.clone_ast()?;

                // SAFETY: `LinkerContext::link` takes `bundle` as a raw `*mut BundleV2` and only
                // touches fields disjoint from `this.linker` (`graph`, `transpiler`,
                // `dynamic_import_entry_points`, scalar reads) via `addr_of_mut!`/place
                // projection, so the `&mut this.linker` receiver and `*bundle_ptr` never produce
                // overlapping `&mut`. (Zig stored all as raw ptrs — bundle_v2.zig:1939.)
                let mut chunks = unsafe {
                    let bundle_ptr: *mut BundleV2 = &raw mut *this;
                    // `Graph::entry_points: Vec<Index>` and `link()` takes `&[Index]` —
                    // both are `crate::Index` (= `bun_ast::Index`), so no cast is needed.
                    let ep = (*bundle_ptr).graph.entry_points.as_slice();
                    // Spec passes `this.graph.server_component_boundaries` by value-copy
                    // (Zig struct copy), leaving the original intact for
                    // `StaticRouteVisitor` (generateChunksInParallel) to read via
                    // `parse_graph`. Borrow — do NOT `take`, which would empty the
                    // graph slot and drop the moved-out `MultiArrayList` heap inside
                    // `load()` (ASAN use-after-poison / wrong `fully_static`).
                    let scbs = &(*bundle_ptr).graph.server_component_boundaries;
                    // Project `.linker` via `bundle_ptr` (not `this.linker`) so no
                    // second `Box::deref_mut` retag invalidates `ep`/`scbs` (SB).
                    (*bundle_ptr)
                        .linker
                        .link(bundle_ptr, ep, scbs, &reachable_files)?
                };
                this.dump_pool_stats("link");

                // Do this at the very end, after processing all the imports/exports so that we can follow exports as needed.
                if let Some(fetch) = fetcher {
                    this.get_all_dependencies(&reachable_files, fetch)?;
                    return Ok(BuildResult {
                        output_files: Vec::new(),
                        metafile: None,
                        metafile_markdown: None,
                    });
                }

                let output_files = crate::linker_context_mod::generate_chunks_in_parallel::<false>(
                    &mut this.linker,
                    &mut chunks,
                )?;
                this.dump_pool_stats("print");

                // Generate metafile if requested (CLI writes files in build_command.zig)
                let metafile: Option<Box<[u8]>> = if this.linker.options.metafile {
                    match crate::linker_context::metafile_builder::generate(
                        &mut this.linker,
                        &mut chunks,
                    ) {
                        Ok(m) => Some(m),
                        Err(err) => {
                            Output::warn(format_args!("Failed to generate metafile: {}", err));
                            None
                        }
                    }
                } else {
                    None
                };

                // Markdown is generated later in build_command.zig for CLI
                Ok(BuildResult {
                    output_files,
                    metafile,
                    metafile_markdown: None,
                })
            })();

            // Under `--watch` the watcher thread holds `*mut BundleV2` (via the
            // reloader's `ctx`) and dereferences it in `on_file_update` after this
            // function returns. In Zig the `BundleV2` is arena-allocated and the
            // arena is never freed (the caller diverges into `exitOrWatch`); in
            // Rust it's `Box`-allocated, so leak it here to match the spec lifetime.
            // Bounded leak: the next file change `execve()`s the process anyway.
            if enable_reloading {
                let _ = Box::into_raw(this);
            } else {
                this.deinit_without_freeing_arena();
            }

            result
        }

        /// Build only the parse graph for the given entry points and return the
        /// BundleV2 instance. No linking or code generation is performed; this is
        /// used by `bun test --changed` to walk import records and compute which
        /// test entry points transitively depend on a given set of source files.
        ///
        /// The caller owns the returned BundleV2. Dupe anything needed out of
        /// the graph and then call `deinit_without_freeing_arena()` — in the
        /// Rust port the AST columns (`Vec<Symbol>` / `Vec<Part>` / …) live on
        /// the global heap, not in `graph.heap`, so leaving the bundle alive is
        /// no longer the bounded arena leak the Zig original described. The
        /// worker pool is owned (created with `thread_pool: None`), so tearing
        /// it down does not touch the runtime VM's parse threads.
        pub fn scan_module_graph_from_cli(
            transpiler: &'a mut Transpiler<'a>,
            alloc: &'a bun_alloc::Arena,
            event_loop: EventLoop,
            entry_points: &[&[u8]],
        ) -> Result<Box<BundleV2<'a>>, Error> {
            let mut this = BundleV2::init(transpiler, None, alloc, event_loop, false, None, alloc)?;
            this.unique_key = generate_unique_key();

            if this.transpiler.log().has_errors() {
                return Err(bun_core::err!("BuildFailed"));
            }

            // enqueueEntryPoints schedules the runtime task before any fallible
            // allocation. If a later allocation fails we must still drain the
            // pool so workers aren't left holding pointers into the caller's
            // stack-allocated Transpiler.
            if let Err(err) = this.enqueue_entry_points_normal(entry_points) {
                this.wait_for_parse();
                return Err(err);
            }

            // Even if entry point resolution produced errors we still wait for
            // all enqueued parse tasks to finish so the graph is consistent.
            this.wait_for_parse();

            Ok(this)
        }

        pub fn generate_from_bake_production_cli(
            entry_points: &bake_types::production::EntryPointMap,
            server_transpiler: &'a mut Transpiler<'a>,
            bake_options: BakeOptions<'a>,
            alloc: &'a bun_alloc::Arena,
            event_loop: EventLoop,
        ) -> Result<Vec<options::OutputFile>, Error> {
            let mut this = BundleV2::init(
                server_transpiler,
                Some(bake_options),
                alloc,
                event_loop,
                false,
                None,
                alloc,
            )?;
            this.unique_key = generate_unique_key();

            // Wrap so every exit path hits the cleanup below; `chunks` must drop
            // inside the closure, before `deinit_without_freeing_arena()`.
            let result = (|| -> Result<Vec<options::OutputFile>, Error> {
                if this.transpiler.log().has_errors() {
                    return Err(bun_core::err!("BuildFailed"));
                }

                this.enqueue_entry_points_bake_production(entry_points)?;

                if this.transpiler.log().has_errors() {
                    return Err(bun_core::err!("BuildFailed"));
                }

                this.wait_for_parse();

                if this.transpiler.log().has_errors() {
                    return Err(bun_core::err!("BuildFailed"));
                }

                this.scan_for_secondary_paths();

                this.process_server_component_manifest_files()?;

                let reachable_files = this.find_reachable_files()?;

                this.process_files_to_copy(&reachable_files)?;

                this.add_server_component_boundaries_as_extra_entry_points()?;

                this.clone_ast()?;

                // SAFETY: see `generate_from_cli` — raw-ptr borrow sidestep for
                // `link` takes a raw `*mut BundleV2` and only touches fields disjoint
                // from `this.linker`.
                let mut chunks = unsafe {
                    let bundle_ptr: *mut BundleV2 = &raw mut *this;
                    let ep = (*bundle_ptr).graph.entry_points.as_slice();
                    // Spec: value-copy (original preserved for `StaticRouteVisitor`).
                    // Borrow — do NOT `take` (see `generate_from_cli`).
                    let scbs = &(*bundle_ptr).graph.server_component_boundaries;
                    // Project `.linker` via `bundle_ptr` so no second `Box::deref_mut`
                    // retag invalidates `ep`/`scbs` (SB hygiene).
                    (*bundle_ptr)
                        .linker
                        .link(bundle_ptr, ep, scbs, &reachable_files)?
                };

                if chunks.is_empty() {
                    return Ok(Vec::new());
                }

                crate::linker_context_mod::generate_chunks_in_parallel::<false>(
                    &mut this.linker,
                    &mut chunks,
                )
            })();

            this.deinit_without_freeing_arena();

            result
        }

        pub fn add_server_component_boundaries_as_extra_entry_points(
            &mut self,
        ) -> Result<(), Error> {
            // Prepare server component boundaries. Each boundary turns into two
            // entry points, a client entrypoint and a server entrypoint.
            //
            // TODO: This should be able to group components by the user specified
            // entry points. This way, using two component files in a route does not
            // create two separate chunks. (note: bake passes each route as an entrypoint)
            {
                let scbs = self.graph.server_component_boundaries.slice();
                self.graph.entry_points.reserve(scbs.list.len() * 2);
                debug_assert_eq!(
                    scbs.list.items_source_index().len(),
                    scbs.list.items_ssr_source_index().len()
                );
                for (original_index, ssr_index) in scbs
                    .list
                    .items_source_index()
                    .iter()
                    .zip(scbs.list.items_ssr_source_index().iter())
                {
                    for idx in [*original_index, *ssr_index] {
                        self.graph.entry_points.push(bun_ast::Index::init(idx)); // PERF(port): was assume_capacity
                    }
                }
            }
            Ok(())
        }

        pub fn process_files_to_copy(&mut self, reachable_files: &[Index]) -> Result<(), Error> {
            if self.graph.estimated_file_loader_count > 0 {
                // PORT NOTE: Zig per-file `arena` column dropped — Box owns its alloc.
                // SAFETY: MultiArrayList columns are disjoint backing storage; raw-ptr
                // sidestep so we can hold several read-only column slices, one mutable
                // column slice (`additional_files`), and call `transpiler_for_target`
                // (which needs `&mut self`) inside the loop. Zig accessed all of these
                // as raw `.items(.field)` slices with no borrow-checking.
                let self_ptr: *mut Self = self;
                // SAFETY: see PORT NOTE above — disjoint MultiArrayList columns,
                // raw-ptr sidestep for split-borrow against `transpiler_for_target`
                // inside the loop. All six column derefs share the same invariant.
                let (
                    unique_key_for_additional_files,
                    content_hashes_for_additional_files,
                    sources,
                    targets,
                    additional_files,
                    loaders,
                ) = unsafe {
                    (
                        (*self_ptr)
                            .graph
                            .input_files
                            .items_unique_key_for_additional_file(),
                        (*self_ptr)
                            .graph
                            .input_files
                            .items_content_hash_for_additional_file(),
                        (*self_ptr).graph.input_files.items_source_mut(),
                        (*self_ptr).graph.ast.items_target(),
                        (*self_ptr).graph.input_files.items_additional_files_mut(),
                        (*self_ptr).graph.input_files.items_loader(),
                    )
                };
                let mut additional_output_files: Vec<options::OutputFile> = Vec::new();

                for reachable_source in reachable_files {
                    let index = reachable_source.get() as usize;
                    let key: &[u8] = &unique_key_for_additional_files[index];
                    if !key.is_empty() {
                        let mut template: options::PathTemplate =
                            if self.graph.html_imports.server_source_indices.len() != 0
                                && self.transpiler.options.asset_naming.is_empty()
                            {
                                options::PathTemplate::ASSET_WITH_TARGET.into()
                            } else {
                                options::PathTemplate::ASSET.into()
                            };

                        let target = targets[index];
                        // SAFETY: see `self_ptr` note above — `transpiler_for_target` needs
                        // `&mut self` only to pick between two stored `*mut Transpiler`s; it
                        // never touches `graph.input_files`.
                        let asset_naming = unsafe {
                            &(*self_ptr)
                                .transpiler_for_target(target)
                                .options
                                .asset_naming
                        };
                        if !asset_naming.is_empty() {
                            template.data.clone_from(asset_naming);
                        }

                        let source = &mut sources[index];

                        let output_path: Box<[u8]> = {
                            // TODO: outbase
                            let pathname =
                                Fs::PathName::init(bun_paths::resolve_path::relative_platform::<
                                    bun_paths::resolve_path::platform::Loose,
                                    false,
                                >(
                                    &self.transpiler.options.root_dir,
                                    source.path.text,
                                ));

                            template.placeholder.name = pathname.base.to_vec().into_boxed_slice();
                            template.placeholder.dir = pathname.dir.to_vec().into_boxed_slice();
                            let mut ext: &[u8] = pathname.ext;
                            if !ext.is_empty() && ext[0] == b'.' {
                                ext = &ext[1..];
                            }
                            template.placeholder.ext = ext.to_vec().into_boxed_slice();

                            if template.needs(options::PlaceholderField::Hash) {
                                template.placeholder.hash =
                                    Some(content_hashes_for_additional_files[index]);
                            }

                            if template.needs(options::PlaceholderField::Target) {
                                template.placeholder.target = <&'static str>::from(target)
                                    .as_bytes()
                                    .to_vec()
                                    .into_boxed_slice();
                            }
                            let mut v = Vec::new();
                            template.print(&mut v).expect("oom");
                            v.into_boxed_slice()
                        };

                        let loader = loaders[index];

                        // Zig hands the existing `source.contents` buffer to the
                        // OutputFile (with its allocator) — no copy. Mirror that by
                        // moving the contents out instead of `to_vec()`-cloning,
                        // which is prohibitively expensive for large assets.
                        let contents_len = source.contents.len();
                        let contents = match core::mem::take(&mut source.contents) {
                            std::borrow::Cow::Owned(v) => v.into_boxed_slice(),
                            std::borrow::Cow::Borrowed(b) => Box::<[u8]>::from(b),
                        };

                        additional_output_files.push(options::OutputFile::init(
                            crate::output_file::Options {
                                source_index: crate::output_file::Index::init(index as u32)
                                    .to_optional(),
                                data: crate::output_file::OptionsData::Buffer { data: contents },
                                size: Some(contents_len),
                                output_path,
                                input_path: source.path.text.to_vec().into_boxed_slice(),
                                input_loader: Loader::File,
                                output_kind: crate::options::OutputKind::Asset,
                                loader,
                                hash: Some(content_hashes_for_additional_files[index]),
                                side: Some(crate::options::Side::Client),
                                entry_point_index: None,
                                is_executable: false,
                                ..Default::default()
                            },
                        ));
                        additional_files[index].push(crate::AdditionalFile::OutputFile(
                            (additional_output_files.len() - 1) as u32,
                        ));
                    }
                }

                self.graph.additional_output_files = additional_output_files;
            }
            Ok(())
        }

        pub fn on_load_async(&mut self, load: &mut jsc_api::JSBundler::Load) {
            // Dispatch to the loop that *owns* `BundleV2` (Zig: `switch (this.loop().*)`).
            // For `Bun.build` this is a Mini loop running on the bundler thread, so
            // `on_load` must land there — not on the JS plugin loop — or it will
            // mutate `graph` / allocate from `graph.heap` off-thread.
            match self.any_loop_mut() {
                bun_event_loop::AnyEventLoop::Js { owner } => {
                    owner.enqueue_task_concurrent(
                        bun_event_loop::ConcurrentTask::ConcurrentTask::from_callback(
                            std::ptr::from_mut(load),
                            on_load_from_js_loop_raw,
                        ),
                    );
                }
                bun_event_loop::AnyEventLoop::Mini(mini) => {
                    // SAFETY: `load` is a valid &mut for the duration of the enqueue;
                    // the mini loop dispatches `on_load_mini` on the bundler thread.
                    unsafe {
                        mini.enqueue_task_concurrent_with_extra_ctx::<jsc_api::JSBundler::Load, BundleV2<'static>>(
                            std::ptr::from_mut(load),
                            on_load_mini,
                            core::mem::offset_of!(jsc_api::JSBundler::Load, task),
                        );
                    }
                }
            }
        }

        pub fn on_resolve_async(&mut self, resolve: &mut jsc_api::JSBundler::Resolve) {
            // See `on_load_async` — must dispatch on the bundler's own loop.
            match self.any_loop_mut() {
                bun_event_loop::AnyEventLoop::Js { owner } => {
                    owner.enqueue_task_concurrent(
                        bun_event_loop::ConcurrentTask::ConcurrentTask::from_callback(
                            std::ptr::from_mut(resolve),
                            on_resolve_from_js_loop_raw,
                        ),
                    );
                }
                bun_event_loop::AnyEventLoop::Mini(mini) => {
                    // SAFETY: `resolve` is a valid &mut for the duration of the enqueue;
                    // the mini loop dispatches `on_resolve_mini` on the bundler thread.
                    unsafe {
                        mini.enqueue_task_concurrent_with_extra_ctx::<jsc_api::JSBundler::Resolve, BundleV2<'static>>(
                            std::ptr::from_mut(resolve),
                            on_resolve_mini,
                            core::mem::offset_of!(jsc_api::JSBundler::Resolve, task),
                        );
                    }
                }
            }
        }
    }

    fn on_load_mini(load: *mut jsc_api::JSBundler::Load, this: *mut BundleV2<'static>) {
        // SAFETY: callback contract — `load` is the ctx passed to
        // `enqueue_task_concurrent_with_extra_ctx`; `this` is the BundleV2 the
        // mini loop's `tick` supplies as ParentContext.
        BundleV2::on_load(unsafe { &mut *load }, unsafe { &mut *this });
    }

    fn on_resolve_mini(resolve: *mut jsc_api::JSBundler::Resolve, this: *mut BundleV2<'static>) {
        // SAFETY: see `on_load_mini`.
        BundleV2::on_resolve(unsafe { &mut *resolve }, unsafe { &mut *this });
    }

    pub(crate) fn on_load_from_js_loop(load: &mut jsc_api::JSBundler::Load) {
        // SAFETY: `bv2` is a live backref set in `Load::init`.
        let bv2 = unsafe { &mut *load.bv2 };
        BundleV2::on_load(load, bv2);
    }

    fn on_load_from_js_loop_raw(
        load: *mut jsc_api::JSBundler::Load,
    ) -> bun_event_loop::JsResult<()> {
        // SAFETY: `load` is a valid pointer set up by `from_callback`.
        on_load_from_js_loop(unsafe { &mut *load });
        Ok(())
    }

    impl<'a> BundleV2<'a> {
        pub fn on_load(load: &mut jsc_api::JSBundler::Load, this: &mut BundleV2) {
            // `Load` is arena-allocated (no Drop); free its owned heap fields on every exit path.
            struct LoadDeinitGuard(*mut jsc_api::JSBundler::Load);
            impl Drop for LoadDeinitGuard {
                fn drop(&mut self) {
                    // SAFETY: `self.0` is the live `&mut Load`; the guard drops before that borrow ends.
                    unsafe {
                        let l = &mut *self.0;
                        drop(core::mem::take(&mut l.path));
                        drop(core::mem::take(&mut l.namespace));
                        drop(core::mem::replace(
                            &mut l.value,
                            jsc_api::JSBundler::LoadValue::Consumed,
                        ));
                    }
                }
            }
            let _load_deinit = LoadDeinitGuard(std::ptr::from_mut(load));
            bun_core::scoped_log!(
                Bundle,
                "onLoad: ({}, {:?})",
                load.source_index.get(),
                core::mem::discriminant(&load.value)
            );
            // PORT NOTE: `helpCatchMemoryIssues` was a mimalloc TLH probe; bumpalo has no equivalent.
            let _ = FeatureFlags::HELP_CATCH_MEMORY_ISSUES;
            // `log_mut()` returns an unbounded `&mut Log` (backref to the
            // arena/DevServer-owned log) so the `&mut this.graph.*` reborrows
            // below type-check without per-use-site `unsafe { &mut *log }`.
            let log = this.transpiler.log_mut();

            // TODO: watcher

            match load.value.consume() {
                jsc_api::JSBundler::LoadValue::NoMatch => {
                    let source =
                        &this.graph.input_files.items_source()[load.source_index.get() as usize];
                    // If it's a file namespace, we should run it through the parser like normal.
                    // The file could be on disk.
                    if source.path.is_file() {
                        this.graph.pool().schedule(load.parse_task_mut());
                        return;
                    }

                    // When it's not a file, this is a build error and we should report it.
                    // we have no way of loading non-files.
                    let _ = log.add_error_fmt(
                        Some(source),
                        bun_ast::Loc::EMPTY,
                        format_args!(
                            "Module not found {} in namespace {}",
                            bun_core::fmt::quote(source.path.pretty),
                            bun_core::fmt::quote(source.path.namespace),
                        ),
                    );

                    // An error occurred, prevent spinning the event loop forever
                    this.decrement_scan_counter();
                }
                jsc_api::JSBundler::LoadValue::Success(code) => {
                    // `code`: LoadSuccess { source_code, loader }
                    // When a plugin returns a file loader, we always need to populate additional_files
                    let should_copy_for_bundling = code.loader.should_copy_for_bundling();
                    if should_copy_for_bundling {
                        let source_index = load.source_index;
                        let additional_files: &mut bun_alloc::AstVec<crate::AdditionalFile> =
                            &mut this.graph.input_files.items_additional_files_mut()
                                [source_index.get() as usize];
                        let _ = additional_files
                            .push(crate::AdditionalFile::SourceIndex(source_index.get()));
                        this.graph.input_files.items_side_effects_mut()
                            [source_index.get() as usize] =
                            bun_ast::SideEffects::NoSideEffectsPureData;
                        this.graph.estimated_file_loader_count += 1;
                    }
                    this.graph.input_files.items_loader_mut()[load.source_index.get() as usize] =
                        code.loader;
                    // For copied assets keep the bytes Owned in `source.contents`
                    // so `process_files_to_copy` can `mem::take` them zero-copy
                    // (it would otherwise clone the whole asset). For everything
                    // else, park them in `free_list` (drained at the very end of
                    // `deinit_without_freeing_arena`) and borrow, so the per-file
                    // teardown loop is a no-op for the common no-plugin path.
                    // SAFETY: the boxed slice is heap-stable under either owner
                    // for as long as the parser holds the borrow.
                    let source_code: &'static [u8] =
                        unsafe { bun_ptr::detach_lifetime_ref::<[u8]>(&*code.source_code) };
                    this.graph.input_files.items_source_mut()[load.source_index.get() as usize]
                        .contents = if should_copy_for_bundling {
                        std::borrow::Cow::Owned(code.source_code.into_vec())
                    } else {
                        this.free_list.push(code.source_code);
                        std::borrow::Cow::Borrowed(source_code)
                    };
                    this.graph.input_files.items_flags_mut()[load.source_index.get() as usize]
                        .insert(crate::Graph::InputFileFlags::IS_PLUGIN_FILE);
                    let parse_task = load.parse_task_mut();
                    parse_task.loader = Some(code.loader);
                    parse_task.contents_or_fd = parse_task::ContentsOrFd::Contents(source_code);
                    this.graph.pool().schedule(parse_task);

                    if this.bun_watcher.is_some() {
                        'add_watchers: {
                            if !this.should_add_watcher_plugin(&load.namespace, &load.path) {
                                break 'add_watchers;
                            }

                            // TODO: support explicit watchFiles array. this is not done
                            // right now because DevServer requires a table to map
                            // watched files and dirs to their respective dependants.
                            let fd = if bun_watcher::REQUIRES_FILE_DESCRIPTORS {
                                let mut buf = bun_paths::path_buffer_pool::get();
                                // PORT NOTE: Zig used `std.posix.toPosixPath` (copy + NUL-
                                // terminate); on kqueue platforms paths are already
                                // posix-separated so `z()` alone suffices.
                                match bun_sys::open(
                                    bun_paths::resolve_path::z(load.path.as_ref(), &mut *buf),
                                    bun_watcher::WATCH_OPEN_FLAGS,
                                    0,
                                ) {
                                    bun_sys::Result::Ok(fd) => fd,
                                    bun_sys::Result::Err(_) => break 'add_watchers,
                                }
                            } else {
                                bun_sys::Fd::INVALID
                            };

                            // Zig: `_ = this.bun_watcher.?.addFile(...) catch {};`
                            let _ = this.bun_watcher_mut().unwrap().add_file::<true>(
                                fd,
                                &load.path,
                                bun_wyhash::hash(load.path.as_ref()) as u32,
                                bun_watcher::Loader(code.loader as u8),
                                bun_sys::Fd::INVALID,
                                None,
                            );
                        }
                    }
                }
                jsc_api::JSBundler::LoadValue::Err(msg) => {
                    if let Some(dev) = this.dev_server {
                        let source = &this.graph.input_files.items_source()
                            [load.source_index.get() as usize];
                        // A stack-allocated Log object containing the singular message
                        let kind = msg.kind;
                        let temp_log = bun_ast::Log {
                            clone_line_text: false,
                            errors: (kind == bun_ast::Kind::Err) as u32,
                            warnings: (kind == bun_ast::Kind::Warn) as u32,
                            msgs: vec![msg],
                            ..Default::default()
                        };
                        dev.handle_parse_task_failure(
                            bun_core::err!("Plugin"),
                            load.bake_graph(),
                            source.path.key_for_incremental_graph(),
                            &raw const temp_log,
                            this,
                        )
                        .expect("oom");
                    } else {
                        let kind = msg.kind;
                        log.msgs.push(msg);
                        log.errors += (kind == bun_ast::Kind::Err) as u32;
                        log.warnings += (kind == bun_ast::Kind::Warn) as u32;
                    }

                    // An error occurred, prevent spinning the event loop forever
                    this.decrement_scan_counter();
                }
                jsc_api::JSBundler::LoadValue::Pending
                | jsc_api::JSBundler::LoadValue::Consumed => unreachable!(),
            }
        }
    }

    pub(crate) fn on_resolve_from_js_loop(resolve: &mut jsc_api::JSBundler::Resolve) {
        // SAFETY: `bv2` is a live backref set in `Resolve::init`.
        let bv2 = unsafe { &mut *resolve.bv2 };
        BundleV2::on_resolve(resolve, bv2);
    }

    fn on_resolve_from_js_loop_raw(
        resolve: *mut jsc_api::JSBundler::Resolve,
    ) -> bun_event_loop::JsResult<()> {
        // SAFETY: `resolve` is a valid pointer set up by `from_callback`.
        on_resolve_from_js_loop(unsafe { &mut *resolve });
        Ok(())
    }

    impl<'a> BundleV2<'a> {
        pub fn on_resolve(resolve: &mut jsc_api::JSBundler::Resolve, this: &mut BundleV2) {
            // Zig: `defer this.decrementScanCounter()`. RAII guard captures `this`
            // as a raw pointer so it does not hold a unique borrow across the body.
            let _dec_guard = this.decrement_scan_counter_on_drop();
            // `Resolve` is arena-allocated (no Drop); free its owned heap fields on every exit path.
            struct ResolveDeinitGuard(*mut jsc_api::JSBundler::Resolve);
            impl Drop for ResolveDeinitGuard {
                fn drop(&mut self) {
                    // SAFETY: `self.0` is the live `&mut Resolve`; the guard drops before that borrow ends.
                    unsafe {
                        let r = &mut *self.0;
                        drop(core::mem::take(&mut r.import_record));
                        drop(core::mem::replace(
                            &mut r.value,
                            jsc_api::JSBundler::ResolveValue::Consumed,
                        ));
                    }
                }
            }
            let _resolve_deinit = ResolveDeinitGuard(std::ptr::from_mut(resolve));
            bun_core::scoped_log!(
                Bundle,
                "onResolve: ({}:{}, {:?})",
                bstr::BStr::new(&resolve.import_record.namespace),
                bstr::BStr::new(&resolve.import_record.specifier),
                core::mem::discriminant(&resolve.value)
            );

            // PORT NOTE: `helpCatchMemoryIssues` was a mimalloc TLH probe; bumpalo has no equivalent.
            let _ = FeatureFlags::HELP_CATCH_MEMORY_ISSUES;

            match resolve.value.consume() {
                jsc_api::JSBundler::ResolveValue::NoMatch => {
                    // If it's a file namespace, we should run it through the resolver like normal.
                    //
                    // The file could be on disk.
                    if resolve.import_record.namespace.as_ref() == b"file" {
                        if resolve.import_record.kind == ImportKind::EntryPointBuild {
                            let target = resolve.import_record.original_target;
                            let Ok(resolved) = this
                                .transpiler_for_target(target)
                                .resolve_entry_point(&resolve.import_record.specifier)
                            else {
                                return;
                            };
                            let mut resolved = resolved;
                            let Ok(source_index) =
                                this.enqueue_entry_item(&mut resolved, true, target)
                            else {
                                return;
                            };

                            // Store the original entry point name for virtual entries that fall back to file resolution
                            if let Some(idx) = source_index {
                                let _ = this
                                    .graph
                                    .entry_point_original_names
                                    .put(idx, &resolve.import_record.specifier);
                            }
                            return;
                        }

                        this.run_resolver(
                            &resolve.import_record,
                            resolve.import_record.original_target,
                        );
                        return;
                    }

                    // SAFETY: Zig's `logForResolutionFailures` returns `*Log` (raw ptr).
                    // Holding the `&mut bun_ast::Log` borrow would alias `&this.graph`
                    // below; detach the lifetime so borrowck releases `this`. The log
                    // lives in `this.transpiler`/`this.framework`, disjoint from
                    // `graph.input_files`.
                    let log: &mut bun_ast::Log = unsafe {
                        bun_ptr::detach_lifetime_mut(this.log_for_resolution_failures(
                            &resolve.import_record.source_file,
                            resolve.import_record.original_target.bake_graph(),
                        ))
                    };

                    // When it's not a file, this is an error and we should report it.
                    //
                    // We have no way of loading non-files.
                    if resolve.import_record.kind == ImportKind::EntryPointBuild {
                        let _ = log.add_error_fmt(
                            None,
                            bun_ast::Loc::EMPTY,
                            format_args!(
                                "Module not found {} in namespace {}",
                                bun_core::fmt::quote(&resolve.import_record.specifier),
                                bun_core::fmt::quote(&resolve.import_record.namespace),
                            ),
                        );
                    } else {
                        let source = &this.graph.input_files.items_source()
                            [resolve.import_record.importer_source_index as usize];
                        let _ = log.add_range_error_fmt(
                            Some(source),
                            resolve.import_record.range,
                            format_args!(
                                "Module not found {} in namespace {}",
                                bun_core::fmt::quote(&resolve.import_record.specifier),
                                bun_core::fmt::quote(&resolve.import_record.namespace),
                            ),
                        );
                    }
                }
                jsc_api::JSBundler::ResolveValue::Success(result) => {
                    let mut out_source_index: Option<Index> = None;
                    if !result.external {
                        // SAFETY: `result.{path,namespace}` are `Box<[u8]>` whose heap
                        // allocations are moved into `this.free_list` below (in the
                        // `!found_existing` branch) and thus outlive `BundleV2`. Erase
                        // to `'static` so `Fs::Path<'static>` can borrow them across
                        // `path_with_pretty_initialized` / `ParseTask` (mirrors Zig's
                        // untracked-slice ownership). In the `found_existing`/`external`
                        // branches `path` is dead before the boxes drop, so the dangling
                        // `'static` is never observed.
                        let (result_path_static, result_ns_static): (&'static [u8], &'static [u8]) = unsafe {
                            (
                                &*std::ptr::from_ref::<[u8]>(result.path.as_ref()),
                                &*std::ptr::from_ref::<[u8]>(result.namespace.as_ref()),
                            )
                        };
                        let mut path = Fs::Path::init(result_path_static);
                        if result.namespace.is_empty() || result.namespace.as_ref() == b"file" {
                            path.namespace = b"file";
                        } else {
                            path.namespace = result_ns_static;
                        }

                        // SAFETY: `GetOrPutResult` borrows `&mut this` for its whole
                        // lifetime, blocking the `free_list`/`graph` accesses below.
                        // Capture `value_ptr` as a raw ptr + `found_existing` and drop
                        // the borrow; the map entry is not rehashed before we write
                        // through `value_ptr` (no intervening map mutation).
                        let (value_ptr, found_existing) = {
                            let existing = this
                                .path_to_source_index_map(resolve.import_record.original_target)
                                .get_or_put(path.text)
                                .expect("oom");
                            (
                                std::ptr::from_mut(existing.value_ptr),
                                existing.found_existing,
                            )
                        };
                        if !found_existing {
                            // Move (not clone) — `path` keeps borrowing the heap bytes via the
                            // `'static` erasure above; `Box<[u8]>` heap data does not relocate
                            // when the Box itself is moved into the Vec.
                            this.free_list.push(result.namespace);
                            this.free_list.push(result.path);
                            path = this
                                .path_with_pretty_initialized(
                                    &path,
                                    resolve.import_record.original_target,
                                )
                                .expect("oom");
                            // PORT NOTE: `GetOrPutResult` has no `key_ptr` — `get_or_put` already
                            // duped the key into the map (see PathToSourceIndexMap.rs).

                            // We need to parse this
                            let source_index =
                                Index::init(u32::try_from(this.graph.ast.len()).expect("int cast"));
                            // SAFETY: map slot from `get_or_put` above; map not mutated since.
                            unsafe { *value_ptr = source_index.get() };
                            out_source_index = Some(source_index);
                            let _ = this.graph.ast.append(JSAst::empty_in(this.graph.heap)); // OOM/capacity: Zig aborts; port keeps fire-and-forget
                            let loader = path
                                .loader(&this.transpiler.options.loaders)
                                .unwrap_or(Loader::File);

                            this.graph
                                .input_files
                                .append(crate::Graph::InputFile {
                                    source: bun_ast::Source {
                                        // PORT NOTE: Zig assigned `path` (Fs.Path) directly;
                                        // shim to the field-identical `bun_paths::fs::Path<'static>`.
                                        path: path_as_static(&path),
                                        contents: std::borrow::Cow::Borrowed(&b""[..]),
                                        index: bun_ast::Index(source_index.get()),
                                        ..Default::default()
                                    },
                                    loader,
                                    side_effects: bun_ast::SideEffects::HasSideEffects,
                                    ..Default::default()
                                })
                                .expect("unreachable");
                            let task_val = ParseTask {
                                // SAFETY: write provenance from `ptr::from_mut`; outlives the task.
                                ctx: Some(unsafe {
                                    bun_ptr::ParentRef::from_raw_mut(
                                        std::ptr::from_mut::<BundleV2>(this)
                                            .cast::<BundleV2<'static>>(),
                                    )
                                }),
                                path,
                                // unknown at this point:
                                contents_or_fd: parse_task::ContentsOrFd::Fd {
                                    dir: bun_sys::Fd::INVALID,
                                    file: bun_sys::Fd::INVALID,
                                },
                                side_effects: bun_ast::SideEffects::HasSideEffects,
                                jsx: this
                                    .transpiler_for_target(resolve.import_record.original_target)
                                    .options
                                    .jsx
                                    .clone(),
                                source_index: bun_ast::Index::init(source_index.get()),
                                module_type: options::ModuleType::Unknown,
                                loader: Some(loader),
                                tree_shaking: this.linker.options.tree_shaking,
                                known_target: resolve.import_record.original_target,
                                ..Default::default()
                            };
                            // Arena-owned (Zig: `arena.create(ParseTask)`).
                            // SAFETY: arena outlives the bundle pass.
                            let task: &mut ParseTask = this.arena_create(task_val);
                            task.task.node.next = core::ptr::null_mut();
                            task.io_task.node.next = core::ptr::null_mut();
                            this.increment_scan_counter();

                            if !this.enqueue_on_load_plugin_if_needed(task) {
                                if loader.should_copy_for_bundling() {
                                    let additional_files: &mut bun_alloc::AstVec<
                                        crate::AdditionalFile,
                                    > = &mut this.graph.input_files.items_additional_files_mut()
                                        [source_index.get() as usize];
                                    additional_files.push(crate::AdditionalFile::SourceIndex(
                                        task.source_index.get(),
                                    ));
                                    this.graph.input_files.items_side_effects_mut()
                                        [source_index.get() as usize] =
                                        bun_ast::SideEffects::NoSideEffectsPureData;
                                    this.graph.estimated_file_loader_count += 1;
                                }

                                this.graph.pool().schedule(task);
                            }
                        } else {
                            // SAFETY: map slot from `get_or_put` above; map not mutated since.
                            out_source_index = Some(Index::init(unsafe { *value_ptr }));
                            // PORT NOTE: Zig freed result.{namespace,path} here; Rust drops below.
                            drop(result.namespace);
                            drop(result.path);
                        }
                    } else {
                        drop(result.namespace);
                        drop(result.path);
                    }

                    if let Some(source_index) = out_source_index {
                        if resolve.import_record.kind == ImportKind::EntryPointBuild {
                            this.graph
                                .entry_points
                                .push(bun_ast::Index::init(source_index.get()));

                            // Store the original entry point name for virtual entries
                            // This preserves the original name for output file naming
                            let _ = this
                                .graph
                                .entry_point_original_names
                                .put(source_index.get(), &resolve.import_record.specifier);
                        } else {
                            let source_import_records =
                                &mut this.graph.ast.items_import_records_mut()
                                    [resolve.import_record.importer_source_index as usize];
                            if source_import_records.len() as u32
                                <= resolve.import_record.import_record_index
                            {
                                let entry = this
                                    .resolve_tasks_waiting_for_import_source_index
                                    .get_or_put(resolve.import_record.importer_source_index)
                                    .expect("oom");
                                if !entry.found_existing {
                                    *entry.value_ptr = Vec::new();
                                }
                                let _ = entry.value_ptr.push(PendingImport {
                                    to_source_index: source_index,
                                    import_record_index: resolve.import_record.import_record_index,
                                });
                            } else {
                                let import_record: &mut ImportRecord = &mut source_import_records
                                    .as_mut_slice()
                                    [resolve.import_record.import_record_index as usize];
                                import_record.source_index = source_index;
                            }
                        }
                    }
                }
                jsc_api::JSBundler::ResolveValue::Err(err) => {
                    let log = this.log_for_resolution_failures(
                        &resolve.import_record.source_file,
                        resolve.import_record.original_target.bake_graph(),
                    );
                    let kind = err.kind;
                    log.msgs.push(err.clone());
                    log.errors += (kind == bun_ast::Kind::Err) as u32;
                    log.warnings += (kind == bun_ast::Kind::Warn) as u32;
                }
                jsc_api::JSBundler::ResolveValue::Pending
                | jsc_api::JSBundler::ResolveValue::Consumed => unreachable!(),
            }
            // resolve is dropped here (defer resolve.deinit())
        }

        pub fn deinit_without_freeing_arena(&mut self) {
            {
                // We do this first to make it harder for any dangling pointers to data to be used in there.
                let on_parse_finalizers = core::mem::take(&mut self.finalizers);
                for finalizer in &on_parse_finalizers {
                    finalizer.call();
                }
                drop(on_parse_finalizers);
            }

            // Plugin file/asset-loader bytes that `process_files_to_copy` will
            // `mem::take` are stored as `Cow::Owned` so that handoff is zero-copy.
            // Everything else is `Cow::Borrowed` (file reads land in the worker
            // arena; non-asset plugin bytes are owned by `free_list`), so this loop
            // is N×branch with no work for plugin-free bundles.
            for s in self.graph.input_files.items_source_mut() {
                if matches!(s.contents, std::borrow::Cow::Owned(_)) {
                    s.contents = std::borrow::Cow::Borrowed(b"");
                }
            }

            // Zig spec (bundle_v2.zig:2229): `defer { this.graph.{ast,input_files,
            // entry_points,entry_point_original_names}.deinit(this.allocator()) }`.
            // In Zig those `MultiArrayList`s only free their slab — every per-element
            // payload (file contents, quoted source-map JSON, line-offset tables, …)
            // lives in `this.graph.heap` / a per-worker `mi_heap_t`, so the caller's
            // `defer heap.deinit()` bulk-frees them. The Rust port now matches that:
            // `LinkerGraph.File.line_offset_table`
            // is `List<AstAlloc>` (slab + `columns_for_non_ascii` payloads in the
            // worker AST heap, see `compute_line_offsets`), and every
            // `MultiArrayList<BundledAst>` / `JSMeta` / `InputFile` column —
            // `quoted_source_contents`, `Part`s, `NamedImport`s, `Scope`s,
            // `ImportData`/`ExportData`, `ArrayHashMap` buckets — is `AstAlloc`-
            // backed. The slab-only `MultiArrayList::drop` strands nothing on the
            // global heap; `mi_heap_destroy` on the AST arenas reclaims all of it.
            //
            // Only `css` still needs an explicit pass: `BundlerStyleSheet` is the
            // CSS crate's tree-of-`Vec`s/`Box`es and is not `AstAlloc`-parameterised
            // (that refactor would touch every `CssRuleList`/selector/declaration
            // type). The arena-allocated stylesheet never has `Drop` run by the
            // slab. For JS-only bundles every `css` slot is `None`, so this loop
            // is N×branch with no work; only CSS entries pay a real drop. The
            // macro takes only one side (`linker.graph.ast` is a bitwise SoA
            // `memcpy` of `graph.ast`), and `CssChunk::asts` `forget()`s its
            // aliases, so this is the unique drop.
            {
                macro_rules! take_ast_cols {
                    ($ast:expr) => {{
                        let ast = $ast;
                        for v in ast.items_css_mut() {
                            if let Some(css_ref) = v.take() {
                                // SAFETY: live arena pointer; dropped exactly once.
                                unsafe { core::ptr::drop_in_place(css_ref.as_ptr()) };
                            }
                        }
                    }};
                }
                if self.linker.graph.ast.len() != 0 {
                    take_ast_cols!(&mut self.linker.graph.ast);
                } else {
                    take_ast_cols!(&mut self.graph.ast);
                }
            }

            // Drop the lazily-created client transpiler (if any) before tearing
            // down workers — matches the .zig spec ordering where the arena slot
            // is invalidated ahead of `pool.workers_assignments` so no worker can
            // observe a half-torn-down transpiler. Clear the `client_transpiler`
            // alias first so it never dangles past the Box drop; in the
            // `BakeOptions`-borrowed path `owned_client_transpiler` is `None` and
            // the DevServer-owned pointer is left untouched.
            if let Some(ct) = self.owned_client_transpiler.as_deref_mut() {
                // `wire_after_move` boxed a higher-tier
                // `bun_js_parser_jsc::Macro::MacroContext` behind
                // `macro_context.data`; the parser-level struct has no `Drop`
                // (and can't — `RuntimeTranspilerStore` bytewise-clones it),
                // so the `Box<Transpiler>` drop below would strand it.
                if let Some(ctx) = ct.macro_context.take() {
                    ctx.deinit();
                }
                self.client_transpiler = None;
                self.owned_client_transpiler = None;
            }

            // bundle_v2.zig:1426-1437 — worker-assignment teardown.
            let pool = self.graph.pool_mut();
            {
                let mut assignments = pool.workers_assignments.lock();
                if assignments.count() > 0 {
                    for worker in assignments.values() {
                        // SAFETY: worker ptrs are live until `deinit_soon`.
                        unsafe { (**worker).deinit_soon() };
                    }
                    pool.worker_pool().wake_for_idle_events();
                }
                // `ThreadPool` is arena-allocated; the arena bulk-free won't
                // run its `Drop`, so release the map's backing storage here.
                assignments.clear_and_free();
            }
            pool.deinit();

            for free in self.free_list.drain(..) {
                drop(free);
            }
        }

        pub fn run_from_js_in_new_thread(
            &mut self,
            entry_points: &[&[u8]],
        ) -> Result<BuildResult, Error> {
            self.unique_key = generate_unique_key();

            if self.transpiler.log().errors > 0 {
                return Err(bun_core::err!("BuildFailed"));
            }

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            self.enqueue_entry_points_normal(entry_points)?;

            // We must wait for all the parse tasks to complete, even if there are errors.
            self.wait_for_parse();

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            if self.transpiler.log().errors > 0 {
                return Err(bun_core::err!("BuildFailed"));
            }

            self.scan_for_secondary_paths();

            self.process_server_component_manifest_files()?;

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            self.clone_ast()?;

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            let reachable_files = self.find_reachable_files()?;

            self.process_files_to_copy(&reachable_files)?;

            self.add_server_component_boundaries_as_extra_entry_points()?;

            // SAFETY: see `generate_from_cli` — repr(transparent) Index slice cast +
            // raw-ptr borrow sidestep for `&mut self.linker` / `&mut *self`.
            let mut chunks = unsafe {
                let bundle_ptr: *mut BundleV2 = self;
                let ep = (*bundle_ptr).graph.entry_points.as_slice();
                // Spec: value-copy (original preserved for `StaticRouteVisitor`).
                // Borrow — do NOT `take` (see `generate_from_cli`).
                let scbs = &(*bundle_ptr).graph.server_component_boundaries;
                // Project `.linker` via `bundle_ptr` so no `&mut *self` reborrow
                // retag invalidates `ep`/`scbs` (SB hygiene).
                (*bundle_ptr)
                    .linker
                    .link(bundle_ptr, ep, scbs, &reachable_files)?
            };

            if self.transpiler.log().errors > 0 {
                return Err(bun_core::err!("BuildFailed"));
            }

            let mut output_files = crate::linker_context_mod::generate_chunks_in_parallel::<false>(
                &mut self.linker,
                &mut chunks,
            )?;

            // Generate metafile if requested
            let metafile: Option<Box<[u8]>> = if self.linker.options.metafile {
                match crate::linker_context::metafile_builder::generate(
                    &mut self.linker,
                    &mut chunks,
                ) {
                    Ok(m) => Some(m),
                    Err(err) => {
                        Output::warn(format_args!("Failed to generate metafile: {}", err.name()));
                        None
                    }
                }
            } else {
                None
            };

            // Generate markdown if metafile was generated and path specified
            let metafile_markdown: Option<Box<[u8]>> = match &metafile {
                Some(mf) if !self.linker.options.metafile_markdown_path.is_empty() => {
                    match crate::linker_context::metafile_builder::generate_markdown(mf) {
                        Ok(m) => Some(m),
                        Err(err) => {
                            Output::warn(format_args!(
                                "Failed to generate metafile markdown: {}",
                                err
                            ));
                            None
                        }
                    }
                }
                _ => None,
            };

            // Write metafile outputs to disk and add them as OutputFiles.
            // Metafile paths are relative to outdir, like all other output files.
            // `LinkerContext::resolver()` wraps the `*mut Resolver` backref deref.
            let outdir = &self.linker.resolver().opts.output_dir;
            if !self.linker.options.metafile_json_path.is_empty() {
                if let Some(mf) = &metafile {
                    write_metafile_output(
                        &mut output_files,
                        outdir,
                        self.linker.options.metafile_json_path,
                        mf,
                        crate::options::OutputKind::MetafileJson,
                    )?;
                }
            }
            if !self.linker.options.metafile_markdown_path.is_empty() {
                if let Some(md) = &metafile_markdown {
                    write_metafile_output(
                        &mut output_files,
                        outdir,
                        self.linker.options.metafile_markdown_path,
                        md,
                        crate::options::OutputKind::MetafileMarkdown,
                    )?;
                }
            }

            Ok(BuildResult {
                output_files,
                metafile,
                metafile_markdown,
            })
        }
    }

    /// Writes a metafile (JSON or markdown) to disk and appends it to the output_files list.
    /// Metafile paths are relative to outdir, like all other output files.
    fn write_metafile_output(
        output_files: &mut Vec<options::OutputFile>,
        outdir: &[u8],
        file_path: &[u8],
        content: &[u8],
        output_kind: crate::options::OutputKind,
    ) -> Result<(), Error> {
        if !outdir.is_empty() {
            // Open the output directory and write the metafile relative to it.
            // PORT NOTE: Zig used `bun.FD.cwd().makeOpenPath()` +
            // `NodeFS.writeFileWithPathBuffer`. Route through `bun_sys::File`.
            let mut buf = bun_paths::path_buffer_pool::get();
            let joined = bun_paths::resolve_path::join_string_buf::<
                bun_paths::resolve_path::platform::Auto,
            >(&mut buf.0[..], &[outdir, file_path]);
            // Create parent directories if needed (relative to outdir).
            let parent = bun_paths::resolve_path::dirname::<bun_paths::resolve_path::platform::Loose>(
                joined,
            );
            if !parent.is_empty() {
                let _ = bun_sys::mkdir_recursive(parent);
            }
            let mut zbuf = bun_paths::path_buffer_pool::get();
            let joined_z = bun_paths::resolve_path::z(joined, &mut zbuf);
            match bun_sys::File::write_file(bun_core::Fd::cwd(), joined_z, content) {
                Ok(()) => {}
                Err(err) => {
                    Output::warn(format_args!(
                        "Failed to write metafile to '{}': {}",
                        bstr::BStr::new(file_path),
                        err
                    ));
                }
            }
        }

        // Add as OutputFile so it appears in result.outputs
        let is_json = output_kind == crate::options::OutputKind::MetafileJson;
        output_files.push(options::OutputFile::init(crate::output_file::Options {
            loader: if is_json { Loader::Json } else { Loader::File },
            input_loader: if is_json { Loader::Json } else { Loader::File },
            input_path: Box::<[u8]>::from(if is_json {
                b"metafile.json".as_slice()
            } else {
                b"metafile.md".as_slice()
            }),
            output_path: Box::<[u8]>::from(file_path),
            data: crate::output_file::OptionsData::Saved(content.len()),
            output_kind,
            is_executable: false,
            side: None,
            entry_point_index: None,
            ..Default::default()
        }));
        Ok(())
    }

    impl<'a> BundleV2<'a> {
        fn should_add_watcher_plugin(&self, namespace: &[u8], path: &[u8]) -> bool {
            namespace == b"file" && bun_paths::is_absolute(path) && self.should_add_watcher(path)
        }

        fn should_add_watcher(&self, path: &[u8]) -> bool {
            if self.dev_server.is_some() {
                strings::index_of(path, b"/node_modules/").is_none()
                    && (if cfg!(windows) {
                        strings::index_of(path, b"\\node_modules\\").is_none()
                    } else {
                        true
                    })
            } else {
                true // `bun build --watch` has always watched node_modules
            }
        }

        /// Dev Server uses this instead to run a subset of the transpiler, and to run it asynchronously.
        pub fn start_from_bake_dev_server(
            &mut self,
            bake_entry_points: &bake_types::EntryPointList,
        ) -> Result<DevServerInput, Error> {
            self.unique_key = generate_unique_key();

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            let mut ctx = DevServerInput {
                css_entry_points: ArrayHashMap::new(),
            };
            self.enqueue_entry_points_dev_server(bake_entry_points, &mut ctx.css_entry_points)?;

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            Ok(ctx)
        }

        // TODO(b0-genuine): body has deep DevServer field access (current_bundle.start_data,
        // css_entry_points, etc.). After tier-6 collapse this fn should be HOISTED into
        // bun_runtime::bake (which can name DevServer concretely) and call back into BundleV2
        // helpers. Until then the entry-point fields are reached through the vtable.
        pub fn finish_from_bake_dev_server(
            &mut self,
            dev_server: &dispatch::DevServerHandle,
        ) -> Result<(), AllocError> {
            // SAFETY: DevServer guarantees `current_bundle` is Some during finish (DevServer.zig:2237).
            // The vtable slot returns `*mut ()` derived from `&mut dev.current_bundle.?.start_data`;
            // DevServer holds it exclusively for the duration of finalize, so the `&mut DevServerInput`
            // here is mut-valid and unaliased until this fn returns.
            let start = unsafe {
                &mut *dev_server
                    .current_bundle_start_data()
                    .cast::<DevServerInput>()
            };

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            self.clone_ast().map_err(|_| AllocError)?;

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            self.dynamic_import_entry_points = ArrayHashMap::new();
            let mut html_files: ArrayHashMap<Index, ()> = ArrayHashMap::new();

            // Separate non-failing files into two lists: JS and CSS
            let js_reachable_files: &[Index] = 'reachable_files: {
                let mut css_total_files: Vec<Index> = Vec::with_capacity(self.graph.css_file_count);
                start.css_entry_points.reserve(self.graph.css_file_count);
                let mut js_files: Vec<Index> =
                    Vec::with_capacity(self.graph.ast.len() - self.graph.css_file_count - 1);

                let asts = self.graph.ast.slice();
                let css_asts = asts.items_css();
                // PORT NOTE: SoA columns are physically disjoint slabs but rustc cannot
                // see that through `&Slice`. Route the two columns we mutate (`parts`,
                // `import_records`) through `split_raw()` (root-provenance `*mut [T]`,
                // no `&mut` intermediate) so the per-index `&mut` does not conflict
                // with the `&asts` reads (`css`, `target`). Mirrors the pattern at
                // `find_reachable_files` (~L1457). The slab does not resize for the
                // duration of this loop and no other `&mut` to these columns exists.
                let ast_raw = asts.split_raw();
                let parts_col: *mut bun_ast::PartList = ast_raw.parts.cast::<bun_ast::PartList>();
                let import_records_col: *mut import_record::List =
                    ast_raw.import_records.cast::<import_record::List>();

                let input_files = self.graph.input_files.slice();
                let loaders = input_files.items_loader();
                let sources = input_files.items_source();
                // TODO(port): multi-zip iteration over MultiArrayList slices [1..]
                for index in 1..self.graph.ast.len() {
                    // SAFETY: `index < ast.len()`; see PORT NOTE above for column aliasing.
                    let part_list = unsafe { &mut *parts_col.add(index) };
                    // SAFETY: `index < ast.len()`; see PORT NOTE above for column aliasing.
                    let import_records = unsafe { &mut *import_records_col.add(index) };
                    let maybe_css = &css_asts[index];
                    let target = asts.items_target()[index];
                    // Dev Server proceeds even with failed files.
                    // These files are filtered out via the lack of any parts.
                    //
                    // Actual empty files will contain a part exporting an empty object.
                    if part_list.len() != 0 {
                        if maybe_css.is_some() {
                            // CSS has restrictions on what files can be imported.
                            // This means the file can become an error after
                            // resolution, which is not usually the case.
                            css_total_files
                                .push(Index::init(u32::try_from(index).expect("int cast"))); // PERF(port): was assume_capacity
                            let mut log = bun_ast::Log::init();
                            if LinkerContext::scan_css_imports(
                                u32::try_from(index).expect("int cast"),
                                import_records.as_slice(),
                                // PORT NOTE: `scan_css_imports` takes the column as a raw
                                // `*const` slice (the scanImportsAndExports caller holds raw
                                // SoA pointers); it only reads via `is_none()`. Zig spec
                                // (`LinkerContext.zig:496`) types this `[]const ?*...`.
                                std::ptr::from_ref(css_asts),
                                sources,
                                loaders,
                                &mut log,
                            ) == crate::linker_context_mod::ScanCssImportsResult::Errors
                            {
                                // TODO: it could be possible for a plugin to change
                                // the type of loader from whatever it was into a
                                // css-compatible loader.
                                dev_server
                                    .handle_parse_task_failure(
                                        bun_core::err!("InvalidCssImport"),
                                        bake::Graph::Client,
                                        sources[index].path.text,
                                        &raw const log,
                                        self,
                                    )
                                    .map_err(|_| AllocError)?;
                                // Since there is an error, do not treat it as a
                                // valid CSS chunk.
                                let _ = start.css_entry_points.swap_remove(&Index::init(
                                    u32::try_from(index).expect("int cast"),
                                ));
                            }
                        } else {
                            // HTML files are special cased because they correspond
                            // to routes in DevServer. They have a JS chunk too,
                            // derived off of the import record list.
                            if loaders[index] == Loader::Html {
                                html_files.put(
                                    Index::init(u32::try_from(index).expect("int cast")),
                                    (),
                                )?;
                            } else {
                                js_files.push(Index::init(u32::try_from(index).expect("int cast"))); // PERF(port): was assume_capacity

                                // Part liveness for HMR is seeded after `linker.load`
                                // (every part of every JS file is marked live).
                            }

                            // Discover all CSS roots.
                            for record in import_records.as_mut_slice() {
                                if !record.source_index.is_valid() {
                                    continue;
                                }
                                if loaders[record.source_index.get() as usize] != Loader::Css {
                                    continue;
                                }
                                // SAFETY: `source_index < ast.len()` (validated above); read
                                // via the raw column ptr so we don't reborrow `asts.parts()`
                                // while `import_records` (a sibling column) is held `&mut`.
                                if unsafe {
                                    (*parts_col.add(record.source_index.get() as usize)).len()
                                } == 0
                                {
                                    record.source_index = Index::INVALID;
                                    continue;
                                }

                                let gop = start
                                    .css_entry_points
                                    .get_or_put(record.source_index)
                                    .expect("oom");
                                if target != Target::Browser {
                                    *gop.value_ptr = CssEntryPointMeta {
                                        imported_on_server: true,
                                    };
                                } else if !gop.found_existing {
                                    *gop.value_ptr = CssEntryPointMeta {
                                        imported_on_server: false,
                                    };
                                }
                            }
                        }
                    } else {
                        // Treat empty CSS files for removal.
                        let _ = start
                            .css_entry_points
                            .swap_remove(&Index::init(u32::try_from(index).expect("int cast")));
                    }
                }

                // Find CSS entry points. Originally, this was computed up front, but
                // failed files do not remember their loader, and plugins can
                // asynchronously decide a file is CSS.
                let css = asts.items_css();
                for entry_point in &self.graph.entry_points {
                    if css[entry_point.get() as usize].is_some() {
                        start.css_entry_points.put(
                            Index::init(entry_point.get()),
                            CssEntryPointMeta {
                                imported_on_server: false,
                            },
                        )?;
                    }
                }

                // TODO(port): leak js_files into arena — Zig returned .items
                // SAFETY: `alloc_slice_copy` returns into the bundler arena which outlives
                // this function. Erase the `&self` lifetime via `*const` so the borrow on
                // `self.arena()` does not extend across the `&mut self` calls below
                // (arena-erasure convention; see also `path.pretty` ~L4770).
                break 'reachable_files unsafe {
                    &*std::ptr::from_ref::<[Index]>(self.arena().alloc_slice_copy(&js_files))
                };
            };

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            // HMR skips most of the linker! All linking errors are converted into
            // runtime errors to avoid a more complicated dependency graph. For
            // example, if you remove an exported symbol, we only rebuild the
            // changed file, then detect the missing export at runtime.
            //
            // Additionally, notice that we run this code generation even if we have
            // files that failed. This allows having a large build graph (importing
            // a new npm dependency), where one file that fails doesnt prevent the
            // passing files to get cached in the incremental graph.

            // The linker still has to be initialized as code generation expects
            // much of its state to be valid memory, even if empty.
            // SAFETY: `LinkerContext::load` takes `bundle` as a raw `*mut BundleV2` and only
            // touches fields disjoint from `self.linker` (`graph`, `transpiler`,
            // `dynamic_import_entry_points`) via `addr_of_mut!`, so the `&mut self.linker`
            // receiver and `*bundle_ptr` never produce overlapping `&mut`. Both Index newtypes
            // are `#[repr(transparent)]` u32 — see `generate_from_cli` for the slice cast.
            unsafe {
                let bundle_ptr: *mut BundleV2 = self;
                let ep = (*bundle_ptr).graph.entry_points.as_slice();
                // Spec: value-copy (original preserved). Borrow — do NOT `take`.
                let scbs = &(*bundle_ptr).graph.server_component_boundaries;
                // Project `.linker` via `bundle_ptr` so no `&mut *self` reborrow
                // retag invalidates `ep`/`scbs` (SB hygiene).
                (*bundle_ptr)
                    .linker
                    .load(bundle_ptr, ep, scbs, js_reachable_files)
                    .map_err(|_| AllocError)?;
            }

            // HMR skips tree-shaking, so size and seed the part-liveness bitsets
            // here: every part of every JS file is considered live.
            {
                let parts_col = self.linker.graph.ast.items_parts();
                let mut parts_live: Vec<bun_collections::AutoBitSet> =
                    Vec::with_capacity(parts_col.len());
                for parts in parts_col {
                    parts_live.push(bun_collections::AutoBitSet::init_empty(parts.len())?);
                }
                for &idx in js_reachable_files {
                    parts_live[idx.get() as usize].set_all(true);
                }
                self.linker.graph.parts_live = parts_live;
            }

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            // Compute line offset tables and quoted contents, used in source maps.
            // Quoted contents will be default-allocated
            if cfg!(debug_assertions) {
                for idx in js_reachable_files {
                    debug_assert!(self.graph.ast.items_parts()[idx.get() as usize].len() != 0); // will create a memory leak
                }
            }
            self.linker.compute_data_for_source_map(js_reachable_files);
            // TODO(port): errdefer { bun.outOfMemory() } — caller cannot recover

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            // Generate chunks
            let js_part_ranges = self
                .arena()
                .alloc_slice_fill_default::<crate::PartRange>(js_reachable_files.len());
            let parts = self.graph.ast.items_parts();
            debug_assert_eq!(js_reachable_files.len(), js_part_ranges.len());
            for (source_index, part_range) in
                js_reachable_files.iter().zip(js_part_ranges.iter_mut())
            {
                *part_range = crate::PartRange {
                    source_index: *source_index,
                    part_index_begin: 0,
                    part_index_end: parts[source_index.get() as usize].len() as u32,
                };
            }

            // PORT NOTE: `Chunk: !Default` (Vec fields). Allocate via Vec then
            // leak into the arena.
            let mut chunks: Vec<Chunk> =
                Vec::with_capacity(1 + start.css_entry_points.count() + html_files.count());

            // First is a chunk to contain all JavaScript modules.
            chunks.push(Chunk {
                entry_point: chunk::EntryPoint::entry_point(0, 0),
                content: chunk::Content::Javascript(chunk::JavaScriptChunk {
                    files_in_chunk_order: js_reachable_files
                        .iter()
                        .map(|i| i.get())
                        .collect::<Vec<u32>>()
                        .into_boxed_slice(),
                    parts_in_chunk_in_order: js_part_ranges.to_vec().into_boxed_slice(),
                    ..Default::default()
                }),
                output_source_map: SourceMap::SourceMapPieces::init(),
                ..Chunk::default()
            });

            // Then all the distinct CSS bundles (these are JS->CSS, not CSS->CSS)
            for entry_point in start.css_entry_points.keys() {
                let order = crate::linker_context::find_imported_files_in_css_order::find_imported_files_in_css_order(&mut self.linker, self.graph.heap, &[*entry_point]);
                let order_len = order.len() as usize;
                chunks.push(Chunk {
                    entry_point: chunk::EntryPoint::non_entry_point(
                        entry_point.get(),
                        entry_point.get(),
                    ),
                    content: chunk::Content::Css(chunk::CssChunk {
                        imports_in_chunk_in_order: order,
                        asts: (0..order_len)
                            .map(|_| bun_css::BundlerStyleSheet::empty())
                            .collect::<Vec<_>>()
                            .into_boxed_slice(),
                    }),
                    output_source_map: SourceMap::SourceMapPieces::init(),
                    ..Chunk::default()
                });
            }

            // Then all HTML files
            for source_index in html_files.keys() {
                chunks.push(Chunk {
                    entry_point: chunk::EntryPoint::non_entry_point(
                        source_index.get(),
                        source_index.get(),
                    ),
                    content: chunk::Content::Html,
                    output_source_map: SourceMap::SourceMapPieces::init(),
                    ..Chunk::default()
                });
            }
            // Arena-owned (Zig allocates `chunks` from `this.arena()`); the
            // `DevServerOutput` lifetime is documented as "tied to the bundler's
            // arena". `alloc_slice_fill_iter` moves each `Chunk` into the bump.
            let chunks: *mut [Chunk] =
                std::ptr::from_mut::<[Chunk]>(self.arena().alloc_slice_fill_iter(chunks));
            // SAFETY: arena outlives this fn and the `DevServerOutput` it produces.
            let chunks: &mut [Chunk] = unsafe { &mut *chunks };

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            crate::linker_context_mod::generate_chunks_in_parallel::<true>(
                &mut self.linker,
                chunks,
            )
            .map_err(|_| AllocError)?;
            // TODO(port): errdefer { bun.outOfMemory() } — caller cannot recover

            /* arena: help_catch_memory_issues — no-op (mimalloc TLH check) */

            dev_server
                .finalize_bundle(
                    self,
                    &mut DevServerOutput {
                        chunks,
                        css_file_list: core::mem::take(&mut start.css_entry_points),
                        html_files,
                    },
                )
                .map_err(|_| AllocError)
        }

        pub fn enqueue_on_resolve_plugin_if_needed(
            &mut self,
            source_index: IndexInt,
            import_record: &ImportRecord,
            source_file: &[u8],
            import_record_index: u32,
            original_target: options::Target,
        ) -> bool {
            if let Some(plugins) = self.plugins_ref() {
                // PORT NOTE: `ImportRecord.path` is `bun_paths::fs::Path`; `has_any_matches`
                // takes the structurally-identical `bun_resolver::fs::Path`. Rebuild the
                // resolver-crate variant from the same backing slices (Zig has a single
                // `Fs.Path` type — the FFI side only reads `.text` / `.namespace`).
                let match_path = Fs::Path::init_with_namespace(
                    import_record.path.text,
                    import_record.path.namespace,
                );
                if plugins.has_any_matches(&match_path, false) {
                    // This is where onResolve plugins are enqueued
                    bun_core::scoped_log!(
                        Bundle,
                        "enqueue onResolve: {}:{}",
                        bstr::BStr::new(&import_record.path.namespace),
                        bstr::BStr::new(&import_record.path.text)
                    );
                    self.increment_scan_counter();

                    // Arena-owned (Zig: `arena.create(Resolve)`); the dispatch
                    // chain holds the raw `*mut Resolve` until the JS thread calls
                    // back, at which point the bundle pass is still alive.
                    // SAFETY: arena outlives the bundle pass.
                    let resolve: &mut jsc_api::JSBundler::Resolve =
                        self.arena_create(jsc_api::JSBundler::Resolve::default());
                    *resolve = jsc_api::JSBundler::Resolve::init(
                        self,
                        jsc_api::JSBundler::MiniImportRecord {
                            kind: import_record.kind,
                            source_file: source_file.into(),
                            namespace: import_record.path.namespace.into(),
                            specifier: import_record.path.text.to_vec().into_boxed_slice(),
                            importer_source_index: source_index,
                            import_record_index,
                            range: import_record.range,
                            original_target,
                        },
                    );

                    resolve.dispatch();
                    return true;
                }
            }

            false
        }

        pub fn enqueue_entry_point_on_resolve_plugin_if_needed(
            &mut self,
            entry_point: &[u8],
            target: options::Target,
        ) -> bool {
            if let Some(plugins) = self.plugins_ref() {
                let mut temp_path = Fs::Path::init(entry_point);
                temp_path.namespace = b"file";
                if plugins.has_any_matches(&temp_path, false) {
                    bun_core::scoped_log!(
                        Bundle,
                        "Entry point '{}' plugin match",
                        bstr::BStr::new(entry_point)
                    );

                    // Arena-owned (Zig: `arena.create(Resolve)`).
                    // SAFETY: arena outlives the bundle pass.
                    let resolve: &mut jsc_api::JSBundler::Resolve =
                        self.arena_create(jsc_api::JSBundler::Resolve::default());
                    self.increment_scan_counter();

                    *resolve = jsc_api::JSBundler::Resolve::init(
                        self,
                        jsc_api::JSBundler::MiniImportRecord {
                            kind: ImportKind::EntryPointBuild,
                            source_file: Box::default(), // No importer for entry points
                            namespace: (&b"file"[..]).into(),
                            specifier: entry_point.into(),
                            importer_source_index: u32::MAX, // Sentinel value for entry points
                            import_record_index: 0,
                            range: bun_ast::Range::NONE,
                            original_target: target,
                        },
                    );

                    resolve.dispatch();
                    return true;
                }
            }
            false
        }

        pub fn enqueue_on_load_plugin_if_needed(&mut self, parse: &mut ParseTask) -> bool {
            let had_matches = self.enqueue_on_load_plugin_if_needed_impl(parse);
            if had_matches {
                return true;
            }

            if parse.path.namespace == b"dataurl" {
                let Ok(maybe_data_url) = DataURL::parse(parse.path.text) else {
                    return false;
                };
                let Some(data_url) = maybe_data_url else {
                    return false;
                };
                let Ok(maybe_decoded) = data_url.decode_data() else {
                    return false;
                };
                // Zig: `this.free_list.append(decoded); parse.contents_or_fd = .{ .contents = decoded };`
                // — the SAME allocation is both tracked for free at `deinit` and
                // borrowed as the parse-task contents. `free_list` owns it for the
                // bundle's lifetime; `ParseTask` is strictly shorter-lived, so the
                // raw-slice borrow is sound. No clone, no leak.
                self.free_list.push(maybe_decoded.into_boxed_slice());
                // SAFETY: `free_list` is append-only until `deinit_without_freeing_arena`
                // (after all ParseTasks have completed); the `Box<[u8]>` is heap-stable.
                let decoded: &'static [u8] =
                    unsafe { bun_ptr::detach_lifetime_ref::<[u8]>(self.free_list.last().unwrap()) };
                parse.contents_or_fd = parse_task::ContentsOrFd::Contents(decoded);
                parse.loader = Some(match data_url.decode_mime_type().category {
                    bun_http_types::MimeType::Category::Javascript => Loader::Js,
                    bun_http_types::MimeType::Category::Css => Loader::Css,
                    bun_http_types::MimeType::Category::Json => Loader::Json,
                    _ => parse.loader.unwrap_or(Loader::File),
                });
            }

            false
        }

        pub fn enqueue_on_load_plugin_if_needed_impl(&mut self, parse: &mut ParseTask) -> bool {
            if let Some(plugins) = self.plugins_ref() {
                if plugins.has_any_matches(&parse.path, true) {
                    // This is where onLoad plugins are enqueued
                    bun_core::scoped_log!(
                        Bundle,
                        "enqueue onLoad: {}:{}",
                        bstr::BStr::new(&parse.path.namespace),
                        bstr::BStr::new(&parse.path.text)
                    );
                    // Arena-owned (Zig: `arena.create(Load)`); the dispatch
                    // chain holds the raw `*mut Load` until the JS thread calls back.
                    let load_val = jsc_api::JSBundler::Load::init(self, parse);
                    // SAFETY: arena outlives the bundle pass.
                    let load: &mut jsc_api::JSBundler::Load = self.arena_create(load_val);
                    load.dispatch();
                    return true;
                }
            }

            false
        }

        fn path_with_pretty_initialized(
            &self,
            path: &Fs::Path<'static>,
            target: options::Target,
        ) -> Result<Fs::Path<'static>, Error> {
            // SAFETY: arena outlives the bundle pass; erase the `&self` lifetime so the
            // returned `Path<'static>` doesn't keep `self` borrowed (borrowck).
            let bump: &'static bun_alloc::Arena =
                unsafe { bun_ptr::detach_lifetime_ref::<bun_alloc::Arena>(self.arena()) };
            let out = generic_path_with_pretty_initialized(
                path,
                target,
                self.transpiler.fs().top_level_dir,
                bump,
            )?;
            Ok(out)
        }

        fn reserve_source_indexes_for_bake(&mut self) -> Result<(), Error> {
            let Some(fw) = &self.framework else {
                return Ok(());
            };
            if fw.server_components.is_none() {
                return Ok(());
            }

            // Call this after
            debug_assert!(self.graph.input_files.len() == 1);
            debug_assert!(self.graph.ast.len() == 1);

            self.graph.ast.ensure_unused_capacity(2)?;
            self.graph.input_files.ensure_unused_capacity(2)?;

            // PORT NOTE: Zig copied `bake.server_virtual_source` by value. The Rust
            // statics are `LazyLock<Source>` and `Source` is not `Clone`, so rebuild
            // an owned `Source` from the static's clonable fields (`path`, `index`).
            let server_source = bun_ast::Source {
                path: bake::SERVER_VIRTUAL_SOURCE.path,
                index: bake::SERVER_VIRTUAL_SOURCE.index,
                ..Default::default()
            };
            let client_source = bun_ast::Source {
                path: bake::CLIENT_VIRTUAL_SOURCE.path,
                index: bake::CLIENT_VIRTUAL_SOURCE.index,
                ..Default::default()
            };

            // OOM/capacity: Zig aborts; port keeps fire-and-forget
            let _ = self.graph.input_files.append(crate::Graph::InputFile {
                source: server_source,
                loader: Loader::Js,
                side_effects: bun_ast::SideEffects::NoSideEffectsPureData,
                ..Default::default()
            }); // PERF(port): was assume_capacity
            // OOM/capacity: Zig aborts; port keeps fire-and-forget
            let _ = self.graph.input_files.append(crate::Graph::InputFile {
                source: client_source,
                loader: Loader::Js,
                side_effects: bun_ast::SideEffects::NoSideEffectsPureData,
                ..Default::default()
            }); // PERF(port): was assume_capacity

            debug_assert!(
                self.graph.input_files.items_source()[Index::BAKE_SERVER_DATA.get() as usize]
                    .index
                    .0
                    == Index::BAKE_SERVER_DATA.get()
            );
            debug_assert!(
                self.graph.input_files.items_source()[Index::BAKE_CLIENT_DATA.get() as usize]
                    .index
                    .0
                    == Index::BAKE_CLIENT_DATA.get()
            );

            let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // PERF(port): was assume_capacity
            let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // PERF(port): was assume_capacity
            Ok(())
        }

        // See barrel_imports.rs for barrel optimization implementation.
        // PORT NOTE: Zig `pub usingnamespace`-style method aliases. `pub use` is not
        // permitted in `impl` blocks; the underlying fns live in `barrel_imports` and
        // take `&mut BundleV2` directly — callers reach them as free functions.
        // (was: pub use barrel_imports::{apply_barrel_optimization, schedule_barrel_deferred_imports})

        /// Returns true when barrel optimization is enabled. Barrel optimization
        /// can apply to any package with sideEffects: false or listed in
        /// optimize_imports, so it is always enabled during bundling.
        fn is_barrel_optimization_enabled(&self) -> bool {
            true
        }

        // TODO: remove ResolveQueue
        //
        // Moving this to the Bundle thread was a significant perf improvement on Linux for first builds
        //
        // The problem is that module resolution has many mutexes.
        // The downside is cached resolutions are faster to do in threads since they only lock very briefly.
        fn run_resolution_for_parse_task(
            parse_result: &mut parse_task::Result,
            this: &mut BundleV2,
        ) -> ResolveQueue {
            let result = match &mut parse_result.value {
                parse_task::ResultValue::Success(r) => r,
                _ => unreachable!(),
            };
            // Capture these before resolveImportRecords, since on error we overwrite
            // parse_result.value (invalidating the `result` pointer).
            let source_index = result.source.index;
            let target = result.ast.target;
            let mut resolve_result = this.resolve_import_records(&mut ResolveImportRecordCtx {
                import_records: &mut result.ast.import_records,
                source: &result.source,
                loader: result.loader,
                target,
            });

            if let Some(err) = resolve_result.last_error {
                bun_core::scoped_log!(Bundle, "failed with error: {}", err.name());
                resolve_result.resolve_queue.clear();

                // Preserve the parsed import_records on the graph so any plugin
                // onResolve tasks already dispatched for *other* records in this
                // same file can still dereference
                // `graph.ast.items(.import_records)[importer_source_index]` when
                // they complete. Without this, the graph entry stays at
                // JSAst.empty and the deferred plugin callback index-out-of-
                // bounds crashes in BundleV2.onResolve / runResolver. The linker
                // never runs because `transpiler.log.errors > 0` aborts the
                // build before link time, so saving the AST is safe.
                let result_heap = *result.ast.import_records.allocator();
                this.graph.ast.items_import_records_mut()[source_index.0 as usize] =
                    core::mem::replace(
                        &mut result.ast.import_records,
                        bun_alloc::ArenaVec::new_in(result_heap),
                    );

                // Move the CSS stylesheet onto the graph row so teardown can find
                // and drop it — the `Success` arm that would normally do this is skipped.
                this.graph.ast.items_css_mut()[source_index.0 as usize] = result.ast.css.take();

                parse_result.value = parse_task::ResultValue::Err(parse_task::ResultError {
                    err,
                    step: crate::parse_task::Step::Resolve,
                    log: bun_ast::Log::init(),
                    source_index: bun_ast::Index(source_index.0),
                    target,
                });
            }

            resolve_result.resolve_queue
        }
    }

    pub struct ResolveImportRecordCtx<'a> {
        pub import_records: &'a mut [ImportRecord],
        pub source: &'a bun_ast::Source,
        pub loader: Loader,
        pub target: options::Target,
    }

    pub struct ResolveImportRecordResult {
        pub resolve_queue: ResolveQueue,
        pub last_error: Option<Error>,
    }

    impl<'a> BundleV2<'a> {
        /// Resolve all unresolved import records for a module. Skips records that
        /// are already resolved (valid source_index), unused, or internal.
        /// Returns a resolve queue of new modules to schedule, plus any fatal error.
        /// Used by both initial parse resolution and barrel un-deferral.
        pub fn resolve_import_records(
            &mut self,
            ctx: &mut ResolveImportRecordCtx,
        ) -> ResolveImportRecordResult {
            let source = ctx.source;
            let loader = ctx.loader;
            let source_dir = source.path.source_dir();
            let mut estimated_resolve_queue_count: usize = 0;
            for import_record in ctx.import_records.iter_mut() {
                if import_record
                    .flags
                    .contains(bun_ast::ImportRecordFlags::IS_INTERNAL)
                {
                    import_record.tag = bun_ast::ImportRecordTag::Runtime;
                    import_record.source_index = Index::RUNTIME;
                }

                // For non-dev-server builds, barrel-deferred records need their
                // source_index cleared so they don't get linked. For dev server,
                // skip this — is_unused is also set by ConvertESMExportsForHmr
                // deduplication, and clearing those source_indices breaks module
                // identity (e.g., __esModule on ESM namespace objects).
                if import_record
                    .flags
                    .contains(bun_ast::ImportRecordFlags::IS_UNUSED)
                    && self.dev_server.is_none()
                {
                    import_record.source_index = Index::INVALID;
                }

                estimated_resolve_queue_count += (!(import_record
                    .flags
                    .contains(bun_ast::ImportRecordFlags::IS_INTERNAL)
                    || import_record
                        .flags
                        .contains(bun_ast::ImportRecordFlags::IS_UNUSED)
                    || import_record.source_index.is_valid()))
                    as usize;
            }
            let mut resolve_queue = ResolveQueue::default();
            resolve_queue.reserve(estimated_resolve_queue_count);

            let mut last_error: Option<Error> = None;

            'outer: for (i, import_record) in ctx.import_records.iter_mut().enumerate() {
                // Preserve original import specifier before resolution modifies path
                if import_record.original_path.is_empty() {
                    import_record.original_path = import_record.path.text;
                }

                if
                // Don't resolve TypeScript types
                import_record.flags.contains(bun_ast::ImportRecordFlags::IS_UNUSED)
                // Don't resolve the runtime
                || import_record.flags.contains(bun_ast::ImportRecordFlags::IS_INTERNAL)
                // Don't resolve pre-resolved imports
                || import_record.source_index.is_valid()
                {
                    continue;
                }

                if let Some(fw) = &self.framework {
                    if fw.server_components.is_some() {
                        // PERF(port): was comptime bool dispatch — profile if hot.
                        let is_server = ctx.target.is_server_side();
                        let src = if is_server {
                            &bake::SERVER_VIRTUAL_SOURCE
                        } else {
                            &bake::CLIENT_VIRTUAL_SOURCE
                        };
                        if import_record.path.text == src.path.pretty {
                            if self.dev_server.is_some() {
                                import_record.flags.insert(
                                    bun_ast::ImportRecordFlags::IS_EXTERNAL_WITHOUT_SIDE_EFFECTS,
                                );
                                import_record.source_index = Index::INVALID;
                            } else {
                                if is_server {
                                    self.graph.kit_referenced_server_data = true;
                                } else {
                                    self.graph.kit_referenced_client_data = true;
                                }
                                import_record.path.namespace = b"bun";
                                import_record.source_index = Index::source(src.index.0);
                            }
                            continue;
                        }
                    }
                }

                if import_record.path.text == b"bun:wrap" {
                    import_record.path.namespace = b"bun";
                    import_record.tag = bun_ast::ImportRecordTag::Runtime;
                    import_record.path.text = b"wrap";
                    import_record.source_index = Index::RUNTIME;
                    continue;
                }

                if ctx.target.is_bun() {
                    if let Some(replacement) = bun_resolve_builtins::HardcodedModule::Alias::get(
                        import_record.path.text,
                        Target::Bun,
                        bun_resolve_builtins::HardcodedModule::Cfg {
                            rewrite_jest_for_tests: self.transpiler.options.rewrite_jest_for_tests,
                        },
                    ) {
                        // When bundling node builtins, remove the "node:" prefix.
                        // This supports special use cases where the bundle is put
                        // into a non-node module resolver that doesn't support
                        // node's prefix. https://github.com/oven-sh/bun/issues/18545
                        import_record.path.text =
                            if replacement.node_builtin && !replacement.node_only_prefix {
                                &replacement.path.as_bytes()[5..]
                            } else {
                                replacement.path.as_bytes()
                            };
                        import_record.tag = replacement.tag;
                        import_record.source_index = Index::INVALID;
                        import_record
                            .flags
                            .insert(bun_ast::ImportRecordFlags::IS_EXTERNAL_WITHOUT_SIDE_EFFECTS);
                        continue;
                    }

                    if import_record.path.text.starts_with(b"bun:") {
                        let new_text: &'static [u8] = &import_record.path.text[b"bun:".len()..];
                        import_record.path = bun_paths::fs::Path::init(new_text);
                        import_record.path.namespace = b"bun";
                        import_record.source_index = Index::INVALID;
                        import_record
                            .flags
                            .insert(bun_ast::ImportRecordFlags::IS_EXTERNAL_WITHOUT_SIDE_EFFECTS);

                        // don't link bun
                        continue;
                    }
                }

                // By default, we treat .sqlite files as external.
                if import_record.loader == Some(Loader::Sqlite) {
                    import_record
                        .flags
                        .insert(bun_ast::ImportRecordFlags::IS_EXTERNAL_WITHOUT_SIDE_EFFECTS);
                    continue;
                }

                if import_record.loader == Some(Loader::SqliteEmbedded) {
                    import_record
                        .flags
                        .insert(bun_ast::ImportRecordFlags::IS_EXTERNAL_WITHOUT_SIDE_EFFECTS);
                }

                if self.enqueue_on_resolve_plugin_if_needed(
                    source.index.0,
                    import_record,
                    source.path.text,
                    i as u32,
                    ctx.target,
                ) {
                    continue;
                }

                // PORT NOTE: borrowck — `transpiler_for_target` returns `&mut Transpiler`
                // tied to `&mut self`, but the underlying storage is raw `*mut Transpiler`
                // backrefs valid for `'a` (see `init`). Compute the raw ptr first, then
                // deref once, so the `&mut self` borrow doesn't span the rest of the loop
                // body (Zig held all of these as raw ptrs and aliased freely).
                let (transpiler_ptr, bake_graph, target): (
                    *mut Transpiler<'a>,
                    bake::Graph,
                    options::Target,
                ) = if import_record.tag == bun_ast::ImportRecordTag::BakeResolveToSsrGraph {
                    if self.framework.is_none() {
                        self.log_for_resolution_failures(source.path.text, bake::Graph::Ssr).add_error_fmt(
                            Some(source),
                            import_record.range.loc,
                            format_args!("The 'bunBakeGraph' import attribute cannot be used outside of a Bun Bake bundle"),
                        );
                        continue;
                    }

                    let is_supported = self.framework.as_ref().unwrap().server_components.is_some()
                        && self
                            .framework
                            .as_ref()
                            .unwrap()
                            .server_components
                            .as_ref()
                            .unwrap()
                            .separate_ssr_graph;
                    if !is_supported {
                        self.log_for_resolution_failures(source.path.text, bake::Graph::Ssr).add_error_fmt(
                            Some(source),
                            import_record.range.loc,
                            format_args!("Framework does not have a separate SSR graph to put this import into"),
                        );
                        continue;
                    }

                    (
                        self.ssr_transpiler,
                        bake::Graph::Ssr,
                        Target::BakeServerComponentsSsr,
                    )
                } else {
                    (
                        std::ptr::from_mut::<Transpiler<'a>>(
                            self.transpiler_for_target(ctx.target),
                        ),
                        ctx.target.bake_graph(),
                        ctx.target,
                    )
                };
                // SAFETY: see PORT NOTE above — raw `*mut Transpiler` lives for `'a`.
                let transpiler: &mut Transpiler<'a> = unsafe { &mut *transpiler_ptr };

                // Check the FileMap first for in-memory files
                if let Some(file_map) = self.file_map {
                    if let Some(_file_map_result) =
                        file_map.resolve(self.arena(), source.path.text, import_record.path.text)
                    {
                        let mut file_map_result = _file_map_result;
                        let mut path_primary = file_map_result.path_pair.primary;
                        let import_record_loader = import_record.loader.unwrap_or_else(|| {
                            Fs::Path::init(path_primary.text)
                                .loader(&transpiler.options.loaders)
                                .unwrap_or(Loader::File)
                        });
                        import_record.loader = Some(import_record_loader);

                        if let Some(id) =
                            self.path_to_source_index_map(target).get(path_primary.text)
                        {
                            import_record.source_index = Index::init(id);
                            continue;
                        }

                        let resolve_entry =
                            resolve_queue.get_or_put(path_primary.text).expect("oom");
                        if resolve_entry.found_existing {
                            // SAFETY: arena-allocated `ParseTask` stored in the queue; arena outlives the pass.
                            import_record.path =
                                path_as_static(&unsafe { &**resolve_entry.value_ptr }.path);
                            continue;
                        }

                        // For virtual files, use the path text as-is (no relative path computation needed).
                        // SAFETY: arena outlives the bundle pass; raw-pointer detour erases the
                        // `&self` lifetime so the resulting `&'static [u8]` doesn't pin `self`
                        // (otherwise `path_primary: Path<'static>` forces `&self: 'static`,
                        // cascading borrow conflicts into every `&mut self` call below).
                        path_primary.pretty = unsafe {
                            bun_ptr::detach_lifetime(
                                self.arena().alloc_slice_copy(path_primary.text),
                            )
                        };
                        import_record.path = path_as_static(&path_primary);
                        let _ = path_primary.text; // key already interned by get_or_put
                        bun_core::scoped_log!(
                            Bundle,
                            "created ParseTask from FileMap: {}",
                            bstr::BStr::new(&path_primary.text)
                        );
                        file_map_result.path_pair.primary = path_primary;
                        // Arena-owned (Zig: `arena.create(ParseTask)`).
                        let resolve_task_val =
                            ParseTask::init(&file_map_result, bun_ast::Index::INVALID, self);
                        // SAFETY: arena outlives the bundle pass.
                        let resolve_task: &mut ParseTask = self.arena_create(resolve_task_val);
                        resolve_task.known_target = target;
                        // Use transpiler JSX options, applying force_node_env like the disk path does
                        resolve_task.jsx = transpiler.options.jsx.clone();
                        resolve_task.jsx.development = match transpiler.options.force_node_env {
                            options::ForceNodeEnv::Development => true,
                            options::ForceNodeEnv::Production => false,
                            options::ForceNodeEnv::Unspecified => {
                                transpiler.options.jsx.development
                            }
                        };
                        resolve_task.loader = Some(import_record_loader);
                        resolve_task.tree_shaking = transpiler.options.tree_shaking;
                        resolve_task.side_effects = bun_ast::SideEffects::HasSideEffects;
                        *resolve_entry.value_ptr = resolve_task;
                        continue;
                    }
                }

                let mut had_busted_dir_cache = false;
                let resolve_result: _resolver::Result = 'inner: loop {
                    match transpiler.resolver.resolve_with_framework(
                        source_dir,
                        import_record.path.text,
                        import_record.kind,
                    ) {
                        Ok(r) => break r,
                        Err(err) => {
                            // PORT NOTE: borrowck — `log_for_resolution_failures` returns
                            // `&mut Log` tied to `&mut self`, but it's always a raw-ptr
                            // deref (DevServer vtable or `transpiler.log`). Detach via
                            // `*mut` so later `self.*` reads don't conflict.
                            // SAFETY: log lives in DevServer/transpiler, disjoint from `self.graph`.
                            let log: &mut bun_ast::Log = unsafe {
                                &mut *std::ptr::from_mut::<bun_ast::Log>(
                                    self.log_for_resolution_failures(source.path.text, bake_graph),
                                )
                            };

                            // Only perform directory busting when hot-reloading is enabled
                            if err == bun_core::err!("ModuleNotFound") {
                                if self.bun_watcher.is_some() {
                                    if !had_busted_dir_cache {
                                        bun_core::scoped_log!(
                                            watcher,
                                            "busting dir cache {} -> {}",
                                            bstr::BStr::new(&source.path.text),
                                            bstr::BStr::new(&import_record.path.text)
                                        );
                                        // Only re-query if we previously had something cached.
                                        if transpiler.resolver.bust_dir_cache_from_specifier(
                                            source.path.text,
                                            import_record.path.text,
                                        ) {
                                            had_busted_dir_cache = true;
                                            continue 'inner;
                                        }
                                    }
                                    if let Some(dev) = self.dev_server {
                                        // Tell DevServer about the resolution failure.
                                        dev.track_resolution_failure(
                                            source.path.text,
                                            import_record.path.text,
                                            ctx.target.bake_graph(), // use the source file target not the altered one
                                            loader,
                                        )
                                        .expect("oom");
                                    }
                                }
                            }

                            // Disable failing packages from being printed.
                            // This may cause broken code to write.
                            // However, doing this means we tell them all the resolve errors
                            // Rather than just the first one.
                            import_record.path.is_disabled = true;

                            if err == bun_core::err!("ModuleNotFound") {
                                let add_error = bun_ast::Log::add_resolve_error_with_text_dupe;

                                if !import_record
                                    .flags
                                    .contains(bun_ast::ImportRecordFlags::HANDLES_IMPORT_ERRORS)
                                    && !self.transpiler.options.ignore_module_resolution_errors
                                {
                                    last_error = Some(err);
                                    if is_package_path(import_record.path.text) {
                                        if ctx.target == Target::Browser
                                            && options::is_node_builtin(import_record.path.text)
                                        {
                                            add_error(
                                                log,
                                                Some(source),
                                                import_record.range,
                                                format_args!(
                                                    "Browser build cannot {} Node.js builtin: \"{}\"{}",
                                                    bstr::BStr::new(
                                                        import_record.kind.error_label()
                                                    ),
                                                    bstr::BStr::new(&import_record.path.text),
                                                    if self.dev_server.is_none() {
                                                        ". To use Node.js builtins, set target to 'node' or 'bun'"
                                                    } else {
                                                        ""
                                                    },
                                                ),
                                                import_record.path.text,
                                                import_record.kind,
                                            );
                                        } else if !ctx.target.is_bun()
                                            && (import_record.path.text == b"bun"
                                                || import_record.path.text.starts_with(b"bun:"))
                                        {
                                            add_error(
                                                log,
                                                Some(source),
                                                import_record.range,
                                                format_args!(
                                                    "Browser build cannot {} Bun builtin: \"{}\"{}",
                                                    bstr::BStr::new(
                                                        import_record.kind.error_label()
                                                    ),
                                                    bstr::BStr::new(&import_record.path.text),
                                                    if self.dev_server.is_none() {
                                                        ". When bundling for Bun, set target to 'bun'"
                                                    } else {
                                                        ""
                                                    },
                                                ),
                                                import_record.path.text,
                                                import_record.kind,
                                            );
                                        } else {
                                            add_error(
                                                log,
                                                Some(source),
                                                import_record.range,
                                                format_args!(
                                                    "Could not resolve: \"{}\". Maybe you need to \"bun install\"?",
                                                    bstr::BStr::new(&import_record.path.text)
                                                ),
                                                import_record.path.text,
                                                import_record.kind,
                                            );
                                        }
                                    } else {
                                        #[cfg(windows)]
                                        let mut buf = bun_paths::path_buffer_pool::get();
                                        let specifier_to_use: &[u8] = if loader == Loader::Html
                                            && import_record.path.text.starts_with(
                                                Fs::FileSystem::instance().top_level_dir,
                                            ) {
                                            let specifier_to_use = &import_record.path.text
                                                [Fs::FileSystem::instance().top_level_dir.len()..];
                                            #[cfg(windows)]
                                            {
                                                &*bun_paths::resolve_path::path_to_posix_buf::<u8>(
                                                    specifier_to_use,
                                                    &mut *buf,
                                                )
                                            }
                                            #[cfg(not(windows))]
                                            {
                                                specifier_to_use
                                            }
                                        } else {
                                            import_record.path.text
                                        };
                                        add_error(
                                            log,
                                            Some(source),
                                            import_record.range,
                                            format_args!(
                                                "Could not resolve: \"{}\"",
                                                bstr::BStr::new(specifier_to_use)
                                            ),
                                            specifier_to_use,
                                            import_record.kind,
                                        );
                                    }
                                }
                            } else {
                                // assume other errors are already in the log
                                last_error = Some(err);
                            }
                            continue 'outer;
                        }
                    }
                };
                let mut resolve_result = resolve_result;
                // if there were errors, lets go ahead and collect them all
                if last_error.is_some() {
                    continue;
                }

                // PORT NOTE: borrowck — Zig `Result.path()` returns `?*Path` (raw),
                // letting the loop body keep reading other `resolve_result` fields
                // (`.flags`, `.path_pair`, `.primary_side_effects_data`, `.jsx`).
                // The Rust port returns `Option<&mut Path>`, which would lock the
                // whole struct. Detach via raw ptr to mirror the Zig aliasing.
                let path: &mut Fs::Path = match resolve_result.path() {
                    // SAFETY: `resolve_result` outlives this borrow; see PORT NOTE above.
                    Some(p) => unsafe { bun_ptr::detach_lifetime_mut::<Fs::Path>(p) },
                    None => {
                        import_record.path.is_disabled = true;
                        import_record.source_index = Index::INVALID;
                        continue;
                    }
                };

                if resolve_result.flags.is_external() {
                    if resolve_result.flags.is_external_and_rewrite_import_path()
                        && !strings::eql_long(
                            resolve_result.path_pair.primary.text,
                            import_record.path.text,
                            true,
                        )
                    {
                        import_record.path = path_as_static(&resolve_result.path_pair.primary);
                    }
                    import_record.flags.set(
                        bun_ast::ImportRecordFlags::IS_EXTERNAL_WITHOUT_SIDE_EFFECTS,
                        resolve_result.primary_side_effects_data
                            != bun_ast::SideEffects::HasSideEffects,
                    );
                    continue;
                }

                if let Some(dev_server) = self.dev_server_handle() {
                    'brk: {
                        if path.loader(&self.transpiler.options.loaders) == Some(Loader::Html)
                            && (import_record.loader.is_none()
                                || import_record.loader.unwrap() == Loader::Html)
                        {
                            // This use case is currently not supported. This error
                            // blocks an assertion failure because the DevServer
                            // reserves the HTML file's spot in IncrementalGraph for the
                            // route definition.
                            let log =
                                self.log_for_resolution_failures(source.path.text, bake_graph);
                            log.add_range_error_fmt(
                                Some(source),
                                import_record.range,
                                format_args!("Browser builds cannot import HTML files."),
                            );
                            continue 'outer;
                        }

                        if loader == Loader::Css {
                            // Do not use cached files for CSS.
                            break 'brk;
                        }

                        import_record.source_index = Index::INVALID;

                        if let Some(entry) = dev_server.is_file_cached(path.text, bake_graph) {
                            let rel = bun_paths::resolve_path::relative_platform::<
                                bun_paths::resolve_path::platform::Loose,
                                false,
                            >(
                                self.transpiler.fs().top_level_dir, path.text
                            );
                            if loader == Loader::Html && entry.kind == bake_types::CacheKind::Asset
                            {
                                // Overload `path.text` to point to the final URL
                                // This information cannot be queried while printing because a lock wouldn't get held.
                                let hash = dev_server
                                    .asset_hash(path.text)
                                    .expect("cached asset not found");
                                import_record.path.text = path.text;
                                import_record.path.namespace = b"file";
                                // SAFETY: `alloc_str` returns into the bundler arena which
                                // outlives this `ImportRecord`. See `interned_slice` contract.
                                import_record.path.pretty = unsafe {
                                    interned_slice(
                                        self.arena()
                                            .alloc_str(&format!(
                                                "{}/{:016x}{}",
                                                bake_types::ASSET_PREFIX,
                                                hash,
                                                bstr::BStr::new(bun_paths::extension(path.text)),
                                            ))
                                            .as_bytes(),
                                    )
                                };
                                import_record.path.is_disabled = false;
                            } else {
                                import_record.path.text = path.text;
                                import_record.path.pretty = rel;
                                import_record.path = path_as_static(
                                    &self
                                        .path_with_pretty_initialized(path, target)
                                        .expect("oom"),
                                );
                                if loader == Loader::Html
                                    || entry.kind == bake_types::CacheKind::Css
                                {
                                    import_record.path.is_disabled = true;
                                }
                            }
                            continue 'outer;
                        }
                    }
                }

                let import_record_loader = 'brk: {
                    let resolved_loader = import_record.loader.unwrap_or_else(|| {
                        path.loader(&transpiler.options.loaders)
                            .unwrap_or(Loader::File)
                    });
                    // When an HTML file references a URL asset (e.g. <link rel="manifest" href="./manifest.json" />),
                    // the file must be copied to the output directory as-is. If the resolved loader would
                    // parse/transform the file (e.g. .json, .toml) rather than copy it, force the .file loader
                    // so that `shouldCopyForBundling()` returns true and the asset is emitted.
                    // Only do this for HTML sources — CSS url() imports should retain their original behavior.
                    if loader == Loader::Html
                        && import_record.kind == ImportKind::Url
                        && !resolved_loader.should_copy_for_bundling()
                        && !resolved_loader.is_javascript_like()
                        && !resolved_loader.is_css()
                        && resolved_loader != Loader::Html
                    {
                        break 'brk Loader::File;
                    }
                    break 'brk resolved_loader;
                };
                import_record.loader = Some(import_record_loader);

                let is_html_entrypoint = import_record_loader == Loader::Html
                    && target.is_server_side()
                    && self.dev_server.is_none();

                if let Some(id) = self.path_to_source_index_map(target).get(path.text) {
                    if self.dev_server.is_some() && loader != Loader::Html {
                        import_record.path =
                            self.graph.input_files.items_source()[id as usize].path;
                    } else {
                        import_record.source_index = Index::init(id);
                    }
                    continue;
                }

                if is_html_entrypoint {
                    import_record.kind = ImportKind::HtmlManifest;
                }

                let resolve_entry = resolve_queue.get_or_put(path.text).expect("oom");
                if resolve_entry.found_existing {
                    // SAFETY: arena-allocated `ParseTask` stored in the queue; arena outlives the pass.
                    import_record.path =
                        path_as_static(&unsafe { &**resolve_entry.value_ptr }.path);
                    continue;
                }

                *path = self
                    .path_with_pretty_initialized(path, target)
                    .expect("oom");

                import_record.path = path_as_static(path);
                // key already interned by get_or_put — no key_ptr on StringHashMapGetOrPut
                bun_core::scoped_log!(Bundle, "created ParseTask: {}", bstr::BStr::new(&path.text));
                // Arena-owned (Zig: `arena.create(ParseTask)`).
                let resolve_task_val =
                    ParseTask::init(&resolve_result, bun_ast::Index::INVALID, self);
                // SAFETY: arena outlives the bundle pass.
                let resolve_task: &mut ParseTask = self.arena_create(resolve_task_val);

                resolve_task.known_target = if import_record.kind == ImportKind::HtmlManifest {
                    Target::Browser
                } else {
                    target
                };

                resolve_task.jsx = resolve_result.jsx.clone();
                resolve_task.jsx.development = match transpiler.options.force_node_env {
                    options::ForceNodeEnv::Development => true,
                    options::ForceNodeEnv::Production => false,
                    options::ForceNodeEnv::Unspecified => transpiler.options.jsx.development,
                };

                resolve_task.loader = Some(import_record_loader);
                resolve_task.tree_shaking = transpiler.options.tree_shaking;
                *resolve_entry.value_ptr = resolve_task;
                if let Some(secondary) = &resolve_result.path_pair.secondary {
                    if !secondary.is_disabled
                        && !core::ptr::eq(secondary, path)
                        && !strings::eql_long(secondary.text, path.text, true)
                    {
                        resolve_task.secondary_path_for_commonjs_interop = Some(*secondary);
                    }
                }

                if is_html_entrypoint {
                    self.generate_server_html_module(path, target, import_record, path.text)
                        .expect("unreachable");
                }
            }

            ResolveImportRecordResult {
                resolve_queue,
                last_error,
            }
        }

        /// Process a resolve queue: create input file slots and schedule parse tasks.
        /// Returns the number of newly scheduled tasks (for pending_items accounting).
        pub fn process_resolve_queue(
            &mut self,
            resolve_queue: &ResolveQueue,
            target: options::Target,
            importer_source_index: IndexInt,
        ) -> i32 {
            let mut diff: i32 = 0;
            // PORT NOTE: reshaped for borrowck — Zig freely aliased `graph` and the
            // path map across the loop body. Here we (a) capture a raw self ptr for
            // ParseTask.ctx, (b) hoist dev_server check, and (c) scope the map
            // borrow to the get_or_put so later `self.graph.*` writes don't overlap.
            // SAFETY: write provenance from `ptr::from_mut`; outlives every ParseTask.
            let self_ptr: Option<bun_ptr::ParentRef<BundleV2<'static>>> = Some(unsafe {
                bun_ptr::ParentRef::from_raw_mut(
                    std::ptr::from_mut::<Self>(self).cast::<BundleV2<'static>>(),
                )
            });
            let dev_server_is_none = self.dev_server.is_none();
            for (key, value) in resolve_queue.iter() {
                let value: *mut ParseTask = *value;
                // SAFETY: ParseTask was arena-allocated in `resolve_import_records`;
                // the arena outlives this loop.
                let value = unsafe { &mut *value };
                let loader = value.loader.unwrap_or_else(|| {
                    value
                        .path
                        .loader(&self.transpiler.options.loaders)
                        .unwrap_or(Loader::File)
                });
                let is_html_entrypoint =
                    loader == Loader::Html && target.is_server_side() && dev_server_is_none;
                // Select map and perform get_or_put, capturing the slot as a raw ptr
                // so the &mut on self.graph is released before we touch other fields.
                let (found_existing, value_ptr): (bool, *mut IndexInt) = {
                    let map: &mut PathToSourceIndexMap = if is_html_entrypoint {
                        self.graph.path_to_source_index_map(Target::Browser)
                    } else {
                        self.graph.path_to_source_index_map(target)
                    };
                    let existing = map.get_or_put(key).expect("oom");
                    (
                        existing.found_existing,
                        std::ptr::from_mut::<IndexInt>(existing.value_ptr),
                    )
                };

                if !found_existing {
                    let new_task: &mut ParseTask = value;
                    let mut new_input_file = crate::Graph::InputFile {
                        source: bun_ast::Source::init_empty_file(new_task.path.text),
                        side_effects: new_task.side_effects,
                        secondary_path: if let Some(secondary_path) =
                            &new_task.secondary_path_for_commonjs_interop
                        {
                            bun_alloc::AstAlloc::vec_from_slice(secondary_path.text)
                        } else {
                            bun_alloc::AstAlloc::vec()
                        },
                        ..Default::default()
                    };

                    self.graph.has_any_secondary_paths = self.graph.has_any_secondary_paths
                        || !new_input_file.secondary_path.is_empty();

                    new_input_file.source.index =
                        bun_ast::Index(self.graph.input_files.len() as u32);
                    new_input_file.source.path = path_as_static(&new_task.path);
                    new_input_file.loader = loader;
                    let new_source_index: u32 = new_input_file.source.index.0;
                    new_task.source_index = bun_ast::Index(new_source_index);
                    new_task.ctx = self_ptr;
                    // SAFETY: value_ptr points into PathToSourceIndexMap storage; no
                    // intervening insert into that map has occurred since get_or_put.
                    unsafe {
                        *value_ptr = new_task.source_index.get();
                    }

                    diff += 1;

                    self.graph
                        .input_files
                        .append(new_input_file)
                        .expect("unreachable");
                    let _ = self.graph.ast.append(JSAst::empty_in(self.graph.heap)); // OOM/capacity: Zig aborts; port keeps fire-and-forget

                    if is_html_entrypoint {
                        self.ensure_client_transpiler();
                        self.graph
                            .entry_points
                            .push(bun_ast::Index(new_source_index));
                    }

                    if self.enqueue_on_load_plugin_if_needed(new_task) {
                        continue;
                    }

                    if loader.should_copy_for_bundling() {
                        let additional_files: &mut bun_alloc::AstVec<crate::AdditionalFile> =
                            &mut self.graph.input_files.items_additional_files_mut()
                                [importer_source_index as usize];
                        additional_files.push(crate::AdditionalFile::SourceIndex(
                            new_task.source_index.get(),
                        ));
                        self.graph.input_files.items_side_effects_mut()
                            [new_task.source_index.get() as usize] =
                            bun_ast::SideEffects::NoSideEffectsPureData;
                        self.graph.estimated_file_loader_count += 1;
                    }

                    self.graph.pool().schedule(new_task);
                } else {
                    if loader.should_copy_for_bundling() {
                        // SAFETY: value_ptr is valid (see above).
                        let existing_idx = unsafe { *value_ptr };
                        let additional_files: &mut bun_alloc::AstVec<crate::AdditionalFile> =
                            &mut self.graph.input_files.items_additional_files_mut()
                                [importer_source_index as usize];
                        additional_files.push(crate::AdditionalFile::SourceIndex(existing_idx));
                        self.graph.estimated_file_loader_count += 1;
                    }

                    // ParseTask is arena-allocated; the slab itself is reclaimed on
                    // arena reset, but its heap-owned fields (path/jsx clones) need
                    // their destructors run now (Zig: `value.deinit()`).
                    // SAFETY: `value` is a live arena slot; not used after this.
                    unsafe { core::ptr::drop_in_place(value) };
                }
            }
            diff
        }
    }

    /// Argument struct for `patch_import_record_source_indices` — pulled out so the
    /// borrow of `import_records` (a column of `graph.ast`) doesn't overlap the
    /// `&mut self` the body needs for `path_to_source_index_map`.
    #[derive(Clone, Copy)]
    pub struct PatchImportRecordsCtx<'a> {
        pub source_index: Index,
        pub source_path: &'a [u8],
        pub loader: Loader,
        pub target: options::Target,
        pub redirect_import_record_index: u32,
        /// When true, always save source indices regardless of dev_server/loader.
        /// Used for barrel un-deferral where records must always be connected.
        pub force_save: bool,
    }

    impl Default for PatchImportRecordsCtx<'_> {
        fn default() -> Self {
            Self {
                source_index: Index::INVALID,
                source_path: b"",
                loader: Loader::File,
                target: Target::Browser,
                redirect_import_record_index: u32::MAX,
                force_save: false,
            }
        }
    }

    impl<'a> BundleV2<'a> {
        /// Patch source_index on import records from pathToSourceIndexMap and
        /// resolve_tasks_waiting_for_import_source_index. Called after
        /// processResolveQueue has registered new modules.
        pub fn patch_import_record_source_indices(
            &mut self,
            import_records: &mut import_record::List,
            ctx: PatchImportRecordsCtx,
        ) {
            // PORT NOTE: Zig aliased `const graph = &this.graph;`. Borrowck rejects
            // holding that across the `&mut self.graph.build_graphs[...]` borrow
            // below, so address the disjoint `self.graph.*` fields directly instead.
            let input_file_loaders = self.graph.input_files.items_loader();
            let save_import_record_source_index = ctx.force_save
                || self.dev_server.is_none()
                || ctx.loader == Loader::Html
                || ctx.loader.is_css();

            if let Some(idx) = self
                .resolve_tasks_waiting_for_import_source_index
                .get_index(&ctx.source_index.get())
            {
                let (_, value) = self
                    .resolve_tasks_waiting_for_import_source_index
                    .swap_remove_at(idx);
                for to_assign in value.slice() {
                    if save_import_record_source_index
                        || input_file_loaders[to_assign.to_source_index.get() as usize].is_css()
                    {
                        import_records.as_mut_slice()[to_assign.import_record_index as usize]
                            .source_index = to_assign.to_source_index;
                    }
                }
                drop(value);
            }

            // Inlined `self.path_to_source_index_map(ctx.target)` (== `&mut self.graph.build_graphs[target]`)
            // so borrowck sees it as disjoint from `self.graph.input_files` above.
            let path_to_source_index_map = &mut self.graph.build_graphs[ctx.target];
            for (i, record) in import_records.as_mut_slice().iter_mut().enumerate() {
                if let Some(source_index) = path_to_source_index_map.get_path(&record.path) {
                    if save_import_record_source_index
                        || input_file_loaders[source_index as usize].is_css()
                    {
                        record.source_index.0 = source_index;
                    }

                    if let Some(compare) = get_redirect_id(ctx.redirect_import_record_index) {
                        if compare == i as u32 {
                            let _ = path_to_source_index_map.put(ctx.source_path, source_index); // OOM-only Result (Zig: catch unreachable)
                        }
                    }
                }
            }
        }

        fn generate_server_html_module(
            &mut self,
            path: &Fs::Path,
            target: options::Target,
            import_record: &mut ImportRecord,
            path_text: &[u8],
        ) -> Result<(), Error> {
            // 1. Create the ast right here
            // 2. Create a separate "virutal" module that becomes the manifest later on.
            // 3. Add it to the graph
            // PORT NOTE: Zig aliased `graph = &this.graph;` — re-borrow `self.graph`
            // at each use so the `self.*` method calls below don't conflict.
            let heap = self.graph.heap;
            let empty_html_file_source: &mut bun_ast::Source = self.arena_create(bun_ast::Source {
                path: path_as_static(path),
                index: bun_ast::Index(self.graph.input_files.len() as u32),
                contents: std::borrow::Cow::Borrowed(&b""[..]),
                ..Default::default()
            });
            let mut js_parser_options = bun_js_parser::ParserOptions::init(
                self.transpiler_for_target(target).options.jsx.clone(),
                Loader::Html,
            );
            js_parser_options.bundle = true;

            // SAFETY: `alloc_str` returns a `&mut str` into the bundler arena, which
            // outlives this AST. `E::EString.data` is `&'static [u8]` per the
            // arena-erasure convention. See `interned_slice` contract.
            let unique_key: &'static [u8] = unsafe {
                interned_slice(
                    self.arena()
                        .alloc_str(&format!(
                            "{}",
                            chunk::UniqueKey {
                                prefix: self.unique_key,
                                kind: chunk::QueryKind::HtmlImport,
                                index: self.graph.html_imports.server_source_indices.len() as u32,
                            },
                        ))
                        .as_bytes(),
                )
            };

            // Extract raw pointers so the `&mut self` borrow from
            // `transpiler_for_target` doesn't overlap `self.arena()` below.
            // SAFETY: `define`/`log` live for `'a` (owned by the Transpiler /
            // BACKREF set in `BundleV2::init`).
            let (define_ptr, log_ptr): (*mut bun_js_parser::Define, *mut bun_ast::Log) = {
                let transpiler = self.transpiler_for_target(target);
                (&raw mut *transpiler.options.define, transpiler.log)
            };

            let ast_for_html_entrypoint = JSAst::init(
                bun_js_parser::new_lazy_export_ast(
                    heap,
                    // SAFETY: `define`/`log` live for `'a` (owned by the Transpiler).
                    unsafe { &mut *define_ptr },
                    js_parser_options,
                    // SAFETY: `define`/`log` live for `'a` (owned by the Transpiler).
                    unsafe { &mut *log_ptr },
                    Expr::init(
                        E::EString {
                            data: unique_key.into(),
                            ..Default::default()
                        },
                        bun_ast::Loc::EMPTY,
                    ),
                    empty_html_file_source,
                    // We replace this runtime API call's ref later via .link on the Symbol.
                    b"__jsonParse",
                )?
                .ok_or_else(|| bun_core::err!("ParserError"))?,
            );

            let fake_input_file = crate::Graph::InputFile {
                source: empty_html_file_source.clone(),
                side_effects: bun_ast::SideEffects::NoSideEffectsPureData,
                ..Default::default()
            };

            let fake_source_index = fake_input_file.source.index;
            self.graph.input_files.append(fake_input_file)?;
            let _ = self.graph.ast.append(ast_for_html_entrypoint); // OOM/capacity: Zig aborts; port keeps fire-and-forget

            import_record.source_index = Index::init(fake_source_index.0);
            let _ = self
                .path_to_source_index_map(target)
                .put(path_text, fake_source_index.0); // OOM-only Result (Zig: catch unreachable)
            self.graph
                .html_imports
                .server_source_indices
                .push(fake_source_index.0);
            self.ensure_client_transpiler();
            Ok(())
        }
    }

    impl<'a> BundleV2<'a> {
        pub fn on_notify_defer(&mut self) {
            self.thread_lock.assert_locked();
            self.graph.deferred_pending += 1;
            self.decrement_scan_counter();
        }

        pub fn on_notify_defer_mini(_: &mut jsc_api::JSBundler::Load, this: &mut BundleV2) {
            this.on_notify_defer();
        }

        pub fn on_parse_task_complete(parse_result: &mut parse_task::Result, this: &mut BundleV2) {
            let _trace = crate::perf::trace("Bundler.onParseTaskComplete");
            // PORT NOTE: Zig aliased `const graph = &this.graph;`. Borrowck rejects
            // holding that across the `this.*` method calls below (each takes
            // `&mut BundleV2`), so re-borrow `this.graph` at each use site instead.
            if parse_result.external.function.is_some() {
                let source = match &parse_result.value {
                    parse_task::ResultValue::Empty { source_index } => source_index.get(),
                    parse_task::ResultValue::Err(data) => data.source_index.get(),
                    parse_task::ResultValue::Success(val) => val.source.index.0,
                };
                let loader: Loader = this.graph.input_files.items_loader()[source as usize];
                // PORT NOTE: `InputFile.arena` column dropped in the Rust port;
                // stash the finalizer regardless so plugin-owned bytes are freed.
                let _ = loader;
                this.finalizers
                    .push(core::mem::take(&mut parse_result.external));
            }

            // defer bun.default_allocator.destroy(parse_result) — caller owns Box and drops at end
            // TODO(port): parse_result is heap-allocated by worker; reconstruct heap::take at scope exit

            let mut diff: i32 = -1;
            // PORT NOTE: Zig used `defer { graph.pending_items += diff; … }` —
            // hoisted to tail position (see end of fn) so the closure doesn't
            // double-borrow `graph`/`this`.

            let mut resolve_queue = ResolveQueue::default();
            let mut process_log = true;

            if matches!(parse_result.value, parse_task::ResultValue::Success(_)) {
                barrel_imports::apply_barrel_optimization(this, parse_result);
                resolve_queue = Self::run_resolution_for_parse_task(parse_result, this);
                if matches!(parse_result.value, parse_task::ResultValue::Err(_)) {
                    process_log = false;
                }
            }

            // To minimize contention, watchers are appended on the bundle thread.
            if this.bun_watcher.is_some() {
                if parse_result.watcher_data.fd != bun_sys::Fd::INVALID {
                    let source_index = match &parse_result.value {
                        parse_task::ResultValue::Empty { source_index } => source_index.get(),
                        parse_task::ResultValue::Err(data) => data.source_index.get(),
                        parse_task::ResultValue::Success(val) => val.source.index.0,
                    };
                    // PORT NOTE: borrowck — read source path/loader before
                    // `should_add_watcher(&self)` so the column borrow is released.
                    let source_path = this.graph.input_files.items_source()[source_index as usize]
                        .path
                        .text;
                    let loader = this.graph.input_files.items_loader()[source_index as usize];
                    if this.should_add_watcher(source_path) {
                        // PORT NOTE: const generic `CLONE_FILE_PATH = isWindows`
                        // matches `cfg!(windows)` at compile time.
                        let _ = this
                            .bun_watcher_mut()
                            .unwrap()
                            .add_file::<{ cfg!(windows) }>(
                                parse_result.watcher_data.fd,
                                source_path,
                                bun_wyhash::hash(source_path) as u32,
                                bun_watcher::Loader(loader as u8),
                                parse_result.watcher_data.dir_fd,
                                None,
                            );
                    }
                }
            }

            match &mut parse_result.value {
                parse_task::ResultValue::Empty {
                    source_index: empty_source_index,
                } => {
                    let empty_idx = (*empty_source_index).get() as usize;
                    this.graph.input_files.items_side_effects_mut()[empty_idx] =
                        bun_ast::SideEffects::NoSideEffectsEmptyAst;
                    if cfg!(debug_assertions) {
                        bun_core::scoped_log!(
                            Bundle,
                            "onParse({}, {}) = empty",
                            empty_idx,
                            bstr::BStr::new(
                                &this.graph.input_files.items_source()[empty_idx].path.text
                            )
                        );
                    }
                }
                parse_task::ResultValue::Success(result) => {
                    // SAFETY: `transpiler.log` is a live BACKREF set in BundleV2::init.
                    result
                        .log
                        .clone_to_with_recycled(this.transpiler.log_mut(), true);

                    this.has_any_top_level_await_modules = this.has_any_top_level_await_modules
                        || !result.ast.top_level_await_keyword.is_empty();

                    // Warning: `input_files` and `ast` arrays may resize in this function call
                    // It is not safe to cache slices from them.
                    let result_source_index = result.source.index.0 as usize;
                    core::mem::swap(
                        &mut this.graph.input_files.items_source_mut()[result_source_index],
                        &mut result.source,
                    );
                    // `on_load` (copy-for-bundling path) parks plugin asset bytes
                    // as `Cow::Owned` directly in this slot and gives the ParseTask
                    // a borrowed alias. The full-Source swap just moved that owner
                    // into `result.source`; move it back so `parse_worker::on_complete`'s
                    // `drop(heap::take(result))` doesn't free the buffer
                    // `process_files_to_copy` will later `mem::take`.
                    if matches!(result.source.contents, std::borrow::Cow::Owned(_)) {
                        core::mem::swap(
                            &mut this.graph.input_files.items_source_mut()[result_source_index]
                                .contents,
                            &mut result.source.contents,
                        );
                    }
                    // PORT NOTE: Zig kept `source` as a stable pointer into the SoA.
                    // Borrowck forbids holding `&input_files.source[i]` while writing
                    // other `input_files` columns through the MultiArrayList accessor
                    // methods (each takes `&mut input_files`), so copy out the
                    // `'static` path text now and re-borrow `source` per-use below.
                    let source_path_text: &'static [u8] = this.graph.input_files.items_source()
                        [result_source_index]
                        .path
                        .text;
                    this.source_code_length += if result_source_index != 0 {
                        this.graph.input_files.items_source()[result_source_index]
                            .contents
                            .len()
                    } else {
                        0
                    };

                    this.graph
                        .input_files
                        .items_unique_key_for_additional_file_mut()[result_source_index] =
                        bun_alloc::AstAlloc::vec_from_slice(
                            result.unique_key_for_additional_file.slice(),
                        )
                        .into_boxed_slice();
                    this.graph
                        .input_files
                        .items_content_hash_for_additional_file_mut()[result_source_index] =
                        result.content_hash_for_additional_file;
                    if !result.unique_key_for_additional_file.is_empty()
                        && result.loader.should_copy_for_bundling()
                    {
                        if let Some(dev) = this.dev_server {
                            let source =
                                &this.graph.input_files.items_source()[result_source_index];
                            dev.put_or_overwrite_asset_erased(
                                &source.path,
                                // SAFETY: when shouldCopyForBundling is true, the
                                // contents are allocated by bun.default_allocator
                                &source.contents,
                                result.content_hash_for_additional_file,
                            )
                            .expect("oom");
                        }
                    }

                    // Record which loader we used for this file
                    this.graph.input_files.items_loader_mut()[result_source_index] = result.loader;

                    bun_core::scoped_log!(
                        Bundle,
                        "onParse({}, {}) = {} imports, {} exports",
                        result_source_index,
                        bstr::BStr::new(source_path_text),
                        result.ast.import_records.len() as usize,
                        result.ast.named_exports.count()
                    );

                    if result.ast.css.is_some() {
                        this.graph.css_file_count += 1;
                    }

                    diff += this.process_resolve_queue(
                        &resolve_queue,
                        result.ast.target,
                        result_source_index as IndexInt,
                    );

                    let result_heap = *result.ast.import_records.allocator();
                    let mut import_records = core::mem::replace(
                        &mut result.ast.import_records,
                        bun_alloc::ArenaVec::new_in(result_heap),
                    );
                    this.patch_import_record_source_indices(
                        &mut import_records,
                        PatchImportRecordsCtx {
                            source_index: Index::init(result_source_index as IndexInt),
                            source_path: source_path_text,
                            loader: result.loader,
                            target: result.ast.target,
                            redirect_import_record_index: result.ast.redirect_import_record_index,
                            force_save: false,
                        },
                    );

                    // Set is_export_star_target for barrel optimization.
                    // In dev server mode, source_index is not saved on JS import
                    // records, so fall back to resolving via the path map.
                    // PORT NOTE: split-borrow `Graph` fields directly so the
                    // `&build_graphs[target]` lookup doesn't lock out
                    // `input_files.items_flags_mut()` (disjoint columns).
                    let result_ast_target = result.ast.target;
                    for star_record_idx in result.ast.export_star_import_records.iter() {
                        if (*star_record_idx as usize) < import_records.len() as usize {
                            let star_ir = &import_records.as_slice()[*star_record_idx as usize];
                            let resolved_index = if star_ir.source_index.is_valid() {
                                star_ir.source_index.get()
                            } else if let Some(idx) =
                                this.graph.build_graphs[result_ast_target].get_path(&star_ir.path)
                            {
                                idx
                            } else {
                                continue;
                            };
                            this.graph.input_files.items_flags_mut()[resolved_index as usize] |=
                                crate::Graph::InputFileFlags::IS_EXPORT_STAR_TARGET;
                        }
                    }
                    result.ast.import_records = import_records;

                    // PORT NOTE: Zig reads `result.ast.named_exports` /
                    // `result.source` *after* `graph.ast.set(…)` (Zig structs are
                    // value types so the `set` is a shallow copy). The Rust port
                    // moves `result.ast` into `graph.ast` and swapped `result.source`
                    // earlier, so snapshot the data the use-directive block needs
                    // *before* the move. Only paid for files that hit the SCB gate.
                    let named_exports_for_scb = if result.use_directive != crate::UseDirective::None
                        && {
                            let separate = this
                                .framework
                                .as_ref()
                                .unwrap()
                                .server_components
                                .as_ref()
                                .unwrap()
                                .separate_ssr_graph;
                            let is_client = result.use_directive == crate::UseDirective::Client;
                            let is_browser = result_ast_target == Target::Browser;
                            if separate {
                                is_client == is_browser
                            } else {
                                is_client != is_browser
                            }
                        } {
                        Some(result.ast.named_exports.clone().expect("oom"))
                    } else {
                        None
                    };

                    let result_heap = *result.ast.parts.allocator();
                    this.graph.ast.set(
                        result_source_index,
                        core::mem::replace(&mut result.ast, JSAst::empty_in(result_heap)),
                    );

                    // Barrel optimization: eagerly record import requests and
                    // un-defer barrel records that are now needed.
                    if this.is_barrel_optimization_enabled() {
                        diff += barrel_imports::schedule_barrel_deferred_imports(
                            this,
                            result_source_index as IndexInt,
                            result_ast_target,
                        )
                        .expect("oom");
                    }

                    if let Some(named_exports) = named_exports_for_scb {
                        if result.use_directive == crate::UseDirective::Server {
                            bun_core::todo_panic!("\"use server\"");
                        }

                        let separate_ssr_graph = this
                            .framework
                            .as_ref()
                            .unwrap()
                            .server_components
                            .as_ref()
                            .unwrap()
                            .separate_ssr_graph;

                        // PORT NOTE: `result.source` was swapped into
                        // `graph.input_files` earlier; re-borrow it from the SoA.
                        // `.clone()` materializes the value-copy Zig got for free.
                        let source_loader: Loader =
                            this.graph.input_files.items_loader()[result_source_index];

                        let (reference_source_index, ssr_index) = if separate_ssr_graph {
                            // Enqueue two files, one in server graph, one in ssr graph.
                            let other_source =
                                this.graph.input_files.items_source()[result_source_index].clone();
                            let scb_source =
                                this.graph.input_files.items_source()[result_source_index].clone();
                            let reference_source_index = this
                                .enqueue_server_component_generated_file(
                                    crate::ServerComponentParseTask::Data::ClientReferenceProxy(
                                        crate::ServerComponentParseTask::ReferenceProxy {
                                            other_source,
                                            named_exports,
                                        },
                                    ),
                                    scb_source,
                                )
                                .expect("oom");

                            let mut ssr_source =
                                this.graph.input_files.items_source()[result_source_index].clone();
                            // PORT NOTE: `path_with_pretty_initialized` takes/returns
                            // `Fs::Path` (`bun_resolver::fs::Path`); bridge through
                            // `fs_path_from_logger`/`fs_path_to_logger` until the
                            // three `Path` mirrors unify.
                            ssr_source.path.pretty = ssr_source.path.text;
                            ssr_source.path = path_as_static(
                                &this
                                    .path_with_pretty_initialized(
                                        &ssr_source.path,
                                        Target::BakeServerComponentsSsr,
                                    )
                                    .expect("oom"),
                            );
                            let ssr_index = this
                                .enqueue_parse_task2(
                                    &mut ssr_source,
                                    source_loader,
                                    Target::BakeServerComponentsSsr,
                                )
                                .expect("oom");

                            (reference_source_index, ssr_index)
                        } else {
                            // Enqueue only one file
                            let mut server_source =
                                this.graph.input_files.items_source()[result_source_index].clone();
                            server_source.path.pretty = server_source.path.text;
                            let server_target = this.transpiler.options.target;
                            server_source.path = path_as_static(
                                &this
                                    .path_with_pretty_initialized(
                                        &server_source.path,
                                        server_target,
                                    )
                                    .expect("oom"),
                            );
                            let server_index = this
                                .enqueue_parse_task2(
                                    &mut server_source,
                                    source_loader,
                                    Target::Browser,
                                )
                                .expect("oom");

                            (server_index, Index::INVALID.get())
                        };

                        this.graph
                            .path_to_source_index_map(result_ast_target)
                            .put(source_path_text, reference_source_index)
                            .expect("oom");

                        this.graph
                            .server_component_boundaries
                            .put(
                                result_source_index as IndexInt,
                                result.use_directive,
                                reference_source_index,
                                ssr_index,
                            )
                            .expect("oom");
                    }
                }
                parse_task::ResultValue::Err(err) => {
                    if cfg!(feature = "debug_logs") {
                        bun_core::scoped_log!(Bundle, "onParse() = err");
                    }

                    if process_log {
                        if let Some(dev_server) = this.dev_server {
                            // Copy out the `'static` path slice so the `input_files`
                            // borrow ends before we coerce `this` to `*mut _`.
                            let abs_path: &'static [u8] = this.graph.input_files.items_source()
                                [err.source_index.get() as usize]
                                .path
                                .text;
                            dev_server
                                .handle_parse_task_failure(
                                    err.err,
                                    err.target.bake_graph(),
                                    abs_path,
                                    &raw const err.log,
                                    std::ptr::from_mut(this),
                                )
                                .expect("oom");
                        } else if !err.log.msgs.is_empty() {
                            // SAFETY: `transpiler.log` is a live BACKREF set in BundleV2::init.
                            err.log
                                .clone_to_with_recycled(this.transpiler.log_mut(), true);
                        } else {
                            // PORT NOTE: Zig used `@tagName(err.step)`.
                            let step_name = match err.step {
                                crate::parse_task::Step::Pending => "pending",
                                crate::parse_task::Step::ReadFile => "read_file",
                                crate::parse_task::Step::Parse => "parse",
                                crate::parse_task::Step::Resolve => "resolve",
                            };
                            // SAFETY: `transpiler.log` is a live BACKREF set in BundleV2::init.
                            this.transpiler.log_mut().add_error_fmt(
                                None,
                                bun_ast::Loc::EMPTY,
                                format_args!(
                                    "{} while {}",
                                    bstr::BStr::new(err.err.name()),
                                    step_name
                                ),
                            );
                        }
                    }

                    if cfg!(debug_assertions) && this.dev_server.is_some() {
                        debug_assert!(
                            this.graph.ast.items_parts()[err.source_index.get() as usize].len()
                                == 0
                        );
                    }
                }
            }

            // `defer { graph.pending_items += diff; if diff < 0 on_after_decrement }`
            bun_core::scoped_log!(
                scan_counter,
                "in parse task .pending_items += {} = {}\n",
                diff,
                i32::try_from(this.graph.pending_items).expect("int cast") + diff
            );
            this.graph.pending_items =
                u32::try_from(i32::try_from(this.graph.pending_items).expect("int cast") + diff)
                    .expect("int cast");
            if diff < 0 {
                this.on_after_decrement_scan_counter();
            }
        }

        /// To satisfy the interface from NewHotReloader()
        pub fn get_loaders(&mut self) -> &mut options::LoaderHashTable {
            &mut self.transpiler.options.loaders
        }

        /// To satisfy the interface from NewHotReloader()
        pub fn bust_dir_cache(&mut self, path: &[u8]) -> bool {
            self.transpiler.resolver.bust_dir_cache(path)
        }
    }

    pub use crate::AdditionalFile;
    pub use bun_core::cheap_prefix_normalizer;

    #[derive(Clone, Copy, Default)]
    pub struct PartRange {
        pub source_index: Index,
        pub part_index_begin: u32,
        pub part_index_end: u32,
    }

    #[repr(C, packed)]
    #[derive(Clone, Copy)]
    pub struct StableRef {
        pub stable_source_index: IndexInt,
        pub r#ref: bun_ast::Ref,
    }

    impl StableRef {
        pub fn is_less_than(_: (), a: StableRef, b: StableRef) -> bool {
            let (a_idx, b_idx) = (a.stable_source_index, b.stable_source_index);
            a_idx < b_idx
                || (a_idx == b_idx && { a.r#ref }.inner_index() < { b.r#ref }.inner_index())
        }
    }

    impl PartialEq for StableRef {
        #[inline]
        fn eq(&self, other: &Self) -> bool {
            let (a_idx, a_ref) = (self.stable_source_index, self.r#ref);
            let (b_idx, b_ref) = (other.stable_source_index, other.r#ref);
            a_idx == b_idx && a_ref == b_ref
        }
    }
    impl Eq for StableRef {}
    impl Ord for StableRef {
        #[inline]
        fn cmp(&self, other: &Self) -> core::cmp::Ordering {
            let (a_idx, a_ref) = (self.stable_source_index, self.r#ref);
            let (b_idx, b_ref) = (other.stable_source_index, other.r#ref);
            (a_idx, a_ref.inner_index()).cmp(&(b_idx, b_ref.inner_index()))
        }
    }
    impl PartialOrd for StableRef {
        #[inline]
        fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
            Some(self.cmp(other))
        }
    }

    #[derive(Clone, Copy, Default, PartialEq, Eq)]
    pub struct ImportTracker {
        pub source_index: Index,
        pub name_loc: bun_ast::Loc,
        pub import_ref: bun_ast::Ref,
    }

    #[repr(u8)]
    #[derive(Clone, Copy, PartialEq, Eq)]
    pub enum DeclInfoKind {
        Declared,
        Lexical,
    }
    #[derive(Clone)]
    pub struct DeclInfo {
        pub name: Box<[u8]>,
        pub kind: DeclInfoKind,
    }

    pub enum CompileResult {
        Javascript {
            source_index: IndexInt,
            result: bun_js_printer::PrintResult,
            decls: Box<[DeclInfo]>,
        },
        Css {
            result: Result<Box<[u8]>, bun_core::Error>,
            source_index: IndexInt,
            source_map: Option<bun_sourcemap::Chunk>,
        },
        Html {
            source_index: IndexInt,
            code: Box<[u8]>,
            script_injection_offset: u32,
        },
    }

    impl CompileResult {
        pub fn source_index(&self) -> IndexInt {
            match self {
                CompileResult::Javascript { source_index, .. }
                | CompileResult::Css { source_index, .. }
                | CompileResult::Html { source_index, .. } => *source_index,
            }
        }

        pub fn code(&self) -> &[u8] {
            match self {
                CompileResult::Javascript { result, .. } => match result {
                    bun_js_printer::PrintResult::Result(r) => &r.code,
                    bun_js_printer::PrintResult::Err(_) => b"",
                },
                CompileResult::Css { result, .. } => match result {
                    Ok(v) => v,
                    Err(_) => b"",
                },
                CompileResult::Html { code, .. } => code,
            }
        }

        pub fn into_code(self) -> Box<[u8]> {
            match self {
                CompileResult::Javascript { result, .. } => match result {
                    bun_js_printer::PrintResult::Result(r) => r.code,
                    bun_js_printer::PrintResult::Err(_) => Box::default(),
                },
                CompileResult::Css { result, .. } => result.unwrap_or_default(),
                CompileResult::Html { code, .. } => code,
            }
        }

        pub fn source_map_chunk(&self) -> Option<&bun_sourcemap::Chunk> {
            match self {
                CompileResult::Javascript { result, .. } => match result {
                    bun_js_printer::PrintResult::Result(r) => r.source_map.as_ref(),
                    bun_js_printer::PrintResult::Err(_) => None,
                },
                CompileResult::Css { source_map, .. } => source_map.as_ref(),
                CompileResult::Html { .. } => None,
            }
        }
    }

    impl Clone for CompileResult {
        fn clone(&self) -> Self {
            match self {
                CompileResult::Javascript {
                    source_index,
                    result,
                    decls,
                } => CompileResult::Javascript {
                    source_index: *source_index,
                    result: match result {
                        bun_js_printer::PrintResult::Result(r) => {
                            bun_js_printer::PrintResult::Result(
                                bun_js_printer::PrintResultSuccess {
                                    code: r.code.clone(),
                                    source_map: r.source_map.clone(),
                                },
                            )
                        }
                        bun_js_printer::PrintResult::Err(e) => bun_js_printer::PrintResult::Err(*e),
                    },
                    decls: decls.clone(),
                },
                CompileResult::Css {
                    result,
                    source_index,
                    source_map,
                } => CompileResult::Css {
                    result: result.clone(),
                    source_index: *source_index,
                    source_map: source_map.clone(),
                },
                CompileResult::Html {
                    source_index,
                    code,
                    script_injection_offset,
                } => CompileResult::Html {
                    source_index: *source_index,
                    code: code.clone(),
                    script_injection_offset: *script_injection_offset,
                },
            }
        }
    }

    impl Default for CompileResult {
        fn default() -> Self {
            CompileResult::Javascript {
                source_index: 0,
                result: bun_js_printer::PrintResult::Result(bun_js_printer::PrintResultSuccess {
                    code: Box::new([]),
                    source_map: None,
                }),
                decls: Box::new([]),
            }
        }
    }

    pub struct CompileResultForSourceMap {
        pub source_map_chunk: bun_sourcemap::Chunk,
        pub generated_offset: bun_sourcemap::LineColumnOffset,
        pub source_index: u32,
    }

    bun_collections::multi_array_columns! {
        pub trait CompileResultForSourceMapColumns for CompileResultForSourceMap {
            source_map_chunk: bun_sourcemap::Chunk,
            generated_offset: bun_sourcemap::LineColumnOffset,
            source_index: u32,
        }
    }

    #[derive(Default)]
    pub struct ContentHasher {
        pub hasher: bun_hash::XxHash64Streaming,
    }
    bun_core::declare_scope!(ContentHasher, hidden);
    impl ContentHasher {
        pub(crate) fn write(&mut self, bytes: &[u8]) {
            bun_core::scoped_log!(
                ContentHasher,
                "HASH_UPDATE {}:\n{}\n----------\n",
                bytes.len(),
                bstr::BStr::new(bytes)
            );
            self.hasher.update(&(bytes.len() as u64).to_ne_bytes());
            self.hasher.update(bytes);
        }
        pub(crate) fn run(bytes: &[u8]) -> u64 {
            let mut h = ContentHasher::default();
            h.write(bytes);
            h.digest()
        }
        pub(crate) fn write_ints(&mut self, i: &[u32]) {
            bun_core::scoped_log!(ContentHasher, "HASH_UPDATE: {:?}\n", i);
            self.hasher.update(bytemuck::cast_slice::<u32, u8>(i));
        }
        pub(crate) fn digest(&self) -> u64 {
            self.hasher.digest()
        }
    }

    #[repr(u8)]
    #[derive(Clone, Copy, PartialEq, Eq, Default)]
    pub enum WrapKind {
        #[default]
        None = 0,
        Cjs,
        Esm,
    }

    pub fn target_from_hashbang(buffer: &[u8]) -> Option<options::Target> {
        const HB: &[u8] = b"#!/usr/bin/env bun";
        if buffer.len() > HB.len() && buffer.starts_with(HB) {
            match buffer[HB.len()] {
                b'\n' | b' ' => return Some(options::Target::Bun),
                _ => {}
            }
        }
        None
    }

    pub fn generic_path_with_pretty_initialized(
        path: &bun_paths::fs::Path<'static>,
        target: options::Target,
        top_level_dir: &[u8],
        _bump: &bun_alloc::Arena,
    ) -> Result<bun_paths::fs::Path<'static>, bun_core::Error> {
        use crate::bun_fs::PathResolverExt as _;
        use crate::bun_node_fallbacks;
        use bun_io::Write as _;

        let mut buf = bun_paths::path_buffer_pool::get();

        let is_node = path.namespace == b"node";
        if is_node
            && (strings::has_prefix(path.text, bun_node_fallbacks::IMPORT_PATH)
                || !bun_paths::is_absolute(path.text))
        {
            return Ok(*path);
        }

        if path.is_file() || is_node {
            let mut buf2 = bun_paths::path_buffer_pool::get();
            let rel = bun_paths::resolve_path::relative_platform_buf::<
                bun_paths::resolve_path::platform::Loose,
                false,
            >(&mut **buf2, top_level_dir, path.text);
            let mut path_clone: crate::bun_fs::Path<'_> = *path;
            if target == options::Target::BakeServerComponentsSsr {
                let mut fbs = bun_io::FixedBufferStream::new_mut(&mut buf.0[..]);
                let _ = fbs.write_all(b"ssr:");
                let _ = fbs.write_all(rel);
                let written = fbs.pos;
                path_clone.pretty = &buf.0[..written];
            } else {
                path_clone.pretty = rel;
            }
            path_clone.dupe_alloc_fix_pretty()
        } else {
            let mut path_clone: crate::bun_fs::Path<'_> = *path;
            let mut fbs = bun_io::FixedBufferStream::new_mut(&mut buf.0[..]);
            if target == options::Target::BakeServerComponentsSsr {
                let _ = fbs.write_all(b"ssr:");
            }
            let _ = write_escaped_namespace(&mut fbs, path_clone.namespace);
            let _ = fbs.write_all(b":");
            let _ = fbs.write_all(path_clone.text);
            let written = fbs.pos;
            path_clone.pretty = &buf.0[..written];
            path_clone.dupe_alloc_fix_pretty()
        }
    }

    fn write_escaped_namespace<W: bun_io::Write + ?Sized>(
        w: &mut W,
        slice: &[u8],
    ) -> bun_io::Result {
        let mut rest = slice;
        while let Some(i) = strings::index_of_char(rest, b':') {
            w.write_all(&rest[..i as usize])?;
            w.write_all(b"::")?;
            rest = &rest[i as usize + 1..];
        }
        w.write_all(rest)
    }

    #[repr(u8)]
    #[derive(Clone, Copy, PartialEq, Eq, Default)]
    pub enum ImportTrackerStatus {
        /// The imported file has no matching export
        #[default]
        NoMatch,

        /// The imported file has a matching export
        Found,

        /// The imported file is CommonJS and has unknown exports
        Cjs,

        /// The import is missing but there is a dynamic fallback object
        DynamicFallback,

        /// The import is missing but there is a dynamic fallback object
        /// and the file was originally CommonJS.
        DynamicFallbackInteropDefault,

        /// The import was treated as a CommonJS import but the file is known to have no exports
        CjsWithoutExports,

        /// The imported file was disabled by mapping it to false in the "browser"
        /// field of package.json
        Disabled,

        /// The imported file is external and has unknown exports
        External,

        /// This is a missing re-export in a TypeScript file, so it's probably a type
        ProbablyTypescriptType,
    }

    /// `bundle_v2.zig:ImportTracker.Iterator`.
    ///
    /// `import_data` is a raw slice into
    /// `graph.meta[i].resolved_exports[..].potentially_ambiguous_export_star_refs`.
    /// The graph SoA is never reallocated during `match_import_with_export`, so
    /// the pointer stays valid for the iterator's lifetime; the caller only reads
    /// `.data` from each entry.
    pub struct ImportTrackerIterator {
        pub status: ImportTrackerStatus,
        pub value: crate::ImportTracker,
        /// Backref into the link-graph SoA (`graph.meta[..].resolved_exports[..].
        /// potentially_ambiguous_export_star_refs`). `BackRef` (not `*const [T]`)
        /// so the single read site in `match_import_with_export` is a safe `Deref`;
        /// the pointee slab is never reallocated while the iterator is live.
        pub import_data: bun_ptr::BackRef<[crate::ImportData]>,
    }

    impl Default for ImportTrackerIterator {
        fn default() -> Self {
            Self {
                status: ImportTrackerStatus::default(),
                value: crate::ImportTracker::default(),
                import_data: bun_ptr::BackRef::new(&[] as &[crate::ImportData]),
            }
        }
    }

    fn get_redirect_id(id: u32) -> Option<u32> {
        if id == u32::MAX {
            return None;
        }
        Some(id)
    }

    #[derive(Clone, Copy, Default)]
    pub struct CssEntryPointMeta {
        /// When this is true, a stub file is added to the Server's IncrementalGraph
        pub imported_on_server: bool,
    }

    /// The lifetime of this structure is tied to the bundler's arena
    pub struct DevServerInput {
        pub css_entry_points: ArrayHashMap<Index, CssEntryPointMeta>,
    }

    /// The lifetime of this structure is tied to the bundler's arena
    pub struct DevServerOutput<'a> {
        pub chunks: &'a mut [Chunk],
        pub css_file_list: ArrayHashMap<Index, CssEntryPointMeta>,
        pub html_files: ArrayHashMap<Index, ()>,
    }

    impl<'a> DevServerOutput<'a> {
        pub fn js_pseudo_chunk(&mut self) -> &mut Chunk {
            &mut self.chunks[0]
        }

        pub fn css_chunks(&mut self) -> &mut [Chunk] {
            &mut self.chunks[1..][..self.css_file_list.count()]
        }

        pub fn html_chunks(&mut self) -> &mut [Chunk] {
            &mut self.chunks[1 + self.css_file_list.count()..][..self.html_files.count()]
        }
    }

    pub fn generate_unique_key() -> u64 {
        let key = bun_core::fast_random() & 0x0FFFFFFF_FFFFFFFF_u64;
        // without this check, putting unique_key in an object key would
        // sometimes get converted to an identifier. ensuring it starts
        // with a number forces that optimization off.
        if cfg!(debug_assertions) {
            let mut buf = [0u8; 16];
            let written = {
                let mut cursor = &mut buf[..];
                write!(cursor, "{:016x}", key).expect("unreachable");
                16 - cursor.len()
            };
            let hex = &buf[..written];
            match hex[0] {
                b'0'..=b'9' => {}
                _ => Output::panic(format_args!(
                    "unique key is a valid identifier: {}",
                    bstr::BStr::new(hex)
                )),
            }
        }
        key
    }

    struct ExternalFreeFunctionAllocator {
        free_callback: unsafe extern "C" fn(*mut c_void),
        context: *mut c_void,
    }

    impl ExternalFreeFunctionAllocator {
        // TODO(refactor): could implement `bun_alloc::Allocator` instead of the manual vtable.

        fn free(ext_free_function: *mut c_void, _: &mut [u8], _: bun_alloc::Alignment, _: usize) {
            // SAFETY: ptr was created by ExternalFreeFunctionAllocator::create
            let info: &mut ExternalFreeFunctionAllocator =
                unsafe { &mut *ext_free_function.cast::<ExternalFreeFunctionAllocator>() };
            // SAFETY: free_callback is a valid C fn provided by plugin
            unsafe { (info.free_callback)(info.context) };
            // SAFETY: info was heap-allocated in create()
            drop(unsafe { bun_core::heap::take(info) });
        }
    }

    /// `pub` so `bun_runtime::allocators::register_safety_vtables` can push the
    /// address into the `bun_safety` registry (Zig spec: `bun.bundle_v2.
    /// allocatorHasPointer` is one of the `safety/alloc.zig:hasPtr` arms).
    pub static EXTERNAL_FREE_VTABLE: bun_alloc::AllocatorVTable = bun_alloc::AllocatorVTable {
        alloc: |_, _, _, _| core::ptr::null_mut(),
        resize: |_, _, _, _, _| false,
        remap: |_, _, _, _, _| core::ptr::null_mut(),
        free: |ctx, buf, a, ra| ExternalFreeFunctionAllocator::free(ctx, buf, a, ra),
    };

    // LAYERING: `BuildResult` / `BundleV2Result` are defined once in
    // `BundleThread.rs` (the trait that consumes them lives there). The previous
    // duplicate here meant `CompletionStruct::set_result` and `BundleV2::
    // run_from_js_in_new_thread` named two distinct types with identical fields.
    // Re-export the canonical defs so `bundle_v2::` and `BundleThread::` paths
    // resolve to the same nominal type.
    pub use crate::BundleThread::{BuildResult, BundleV2Result, CompletionStruct, singleton};

    // re-exports
    pub use crate::HTMLScanner::HTMLScanner;
    pub use crate::IndexStringMap::IndexStringMap;
    pub use bun_ast::Loc;

    // C++ binding for lazy metafile getter (defined in BundlerMetafile.cpp)
    // Uses jsc.conv (SYSV_ABI on Windows x64) for proper calling convention
    // Sets up metafile object with { json: <lazy parsed>, markdown?: string }

    // ported from: src/bundler/bundle_v2.zig
}