mise 2026.9.4

Dev tools, env vars, and tasks in one CLI
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
use crate::request_exit;
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{
    collections::{BTreeMap, BTreeSet, HashSet},
    sync::atomic::Ordering,
};

use crate::backend::Backend;
use crate::cli::args::{BackendArg, ToolArg};
use crate::cli::exec::Exec;
use crate::config::{CommandWrapper, Config, Settings, load_command_wrappers};
use crate::file::display_path;
use crate::lock_file::LockFile;
use crate::toolset::{ResolveOptions, ToolVersion, Toolset, ToolsetBuilder};
use crate::{backend, dirs, env, fake_asdf, file};
use color_eyre::eyre::{Result, bail, eyre};
use eyre::WrapErr;
#[cfg(windows)]
use indoc::formatdoc;
use itertools::Itertools;
use path_absolutize::Absolutize;
use tokio::task::JoinSet;

#[cfg(any(windows, test))]
const NATIVE_SHIM_MARKER: &[u8] = include_bytes!("../crates/mise-shim/native-shim-marker");
const GENERATED_SHELL_SHIM_HEADER: &str = "#!/bin/sh\n# mise generated shim\n";
#[cfg(any(windows, test))]
const GENERATED_WINDOWS_CMD_SHIM_HEADER: &str = "@echo off\r\nrem mise generated shim\r\n";
#[cfg(any(windows, test))]
const GENERATED_WINDOWS_BASH_SHIM_HEADER: &str = "#!/bin/bash\n# mise generated shim\n";
const SHIM_SCRIPT_INSPECTION_LIMIT: u64 = 16 * 1024;

pub(crate) const TASK_TOOL_ARGS_ENV: &str = "__MISE_TASK_TOOL_ARGS";

#[derive(serde::Deserialize, serde::Serialize)]
struct TaskToolArg {
    backend: String,
    version: Option<String>,
    options: crate::toolset::ToolVersionOptions,
}

/// Preserve runtime-only task tool requests for a shim process. A task's `tools`
/// entries are not part of the config that a shim reloads, so without this context
/// a bootstrap shim cannot find a lazy provider declared only on the task.
pub(crate) fn task_tool_args_env(tools: &[ToolArg]) -> Result<Option<String>> {
    let tools = tools
        .iter()
        .map(|tool| TaskToolArg {
            backend: tool.ba.short.clone(),
            version: tool.version.clone(),
            options: tool
                .tvr
                .as_ref()
                .map(|request| request.options())
                .unwrap_or_default(),
        })
        .collect_vec();
    if tools.is_empty() {
        Ok(None)
    } else {
        Ok(Some(serde_json::to_string(&tools)?))
    }
}

pub(crate) fn task_tool_args_from_env() -> Result<Vec<ToolArg>> {
    let Ok(serialized) = env::var(TASK_TOOL_ARGS_ENV) else {
        return Ok(vec![]);
    };
    serde_json::from_str::<Vec<TaskToolArg>>(&serialized)?
        .into_iter()
        .map(|tool| {
            let input = tool.version.as_ref().map_or_else(
                || tool.backend.clone(),
                |version| format!("{}@{version}", tool.backend),
            );
            let mut arg: ToolArg = input.parse()?;
            if !tool.options.is_empty() {
                Arc::make_mut(&mut arg.ba).set_opts(Some(tool.options));
            }
            arg.tvr = arg
                .version
                .as_ref()
                .map(|version| {
                    crate::toolset::ToolRequest::new(
                        arg.ba.clone(),
                        version,
                        crate::toolset::ToolSource::Argument,
                    )
                })
                .transpose()?;
            Ok(arg)
        })
        .collect()
}

// executes as if it was a shim if the command is not "mise", e.g.: "node"
pub(crate) async fn handle_shim() -> Result<()> {
    // TODO: instead, check if bin is in shims dir
    let bin_name = *env::MISE_BIN_NAME;
    if env::is_mise_binary(bin_name) || cfg!(test) {
        return Ok(());
    }
    #[cfg(windows)]
    {
        let shim_path = invoked_shim_path();
        if env::var_path(env::MISE_SHIM_PATH_ENV)
            .as_ref()
            .is_some_and(|previous| {
                file::paths_eq(
                    &file::canonicalize_or_self(previous),
                    &file::canonicalize_or_self(&shim_path),
                )
            })
        {
            bail!(
                "recursive shim invocation detected for {bin_name}: {}",
                display_path(&shim_path)
            );
        }
        *env::MISE_SHIM_PATH.write().unwrap() = Some(shim_path.clone());
        env::set_var(env::MISE_SHIM_PATH_ENV, &shim_path);
    }
    let mut config = Config::get().await?;
    let mut args = env::ARGS.read().unwrap().clone();
    env::PREFER_OFFLINE.store(true, Ordering::Relaxed);
    trace!("shim[{bin_name}] args: {}", args.join(" "));
    let (bin, ts, wrapper) = which_shim(&mut config, &env::MISE_BIN_NAME, &args).await?;
    args[0] = bin.to_string_lossy().to_string();
    if let Some(wrapper) = &wrapper {
        args.splice(1..1, wrapper.args().iter().cloned());
    }
    env::set_var("__MISE_SHIM", "1");
    let exec = Exec {
        tool: vec![],
        c: None,
        command: Some(args),
        jobs: None,
        raw: false,
        no_deps: true, // Skip deps for shims to avoid performance impact
        fresh_env: false,
        deny_all: false,
        deny_read: false,
        deny_write: false,
        deny_net: false,
        deny_env: false,
        allow_read: vec![],
        allow_write: vec![],
        allow_net: vec![],
        allow_env: vec![],
    };
    time!("shim exec");
    if let Some(wrapper) = wrapper {
        exec.run_with_command_wrapper(
            config,
            ts,
            wrapper
                .env()
                .iter()
                .map(|(key, value)| (key.clone(), value.clone()))
                .collect(),
        )
        .await?;
    } else {
        exec.run_with_toolset(config, ts).await?;
    }
    Err(request_exit(0))
}

#[cfg(windows)]
fn invoked_shim_path() -> PathBuf {
    let argv0 = PathBuf::from(&*env::ARGV0);
    if argv0.is_absolute() {
        return argv0;
    }
    if argv0.components().count() > 1 {
        return argv0
            .absolutize()
            .map(|path| path.into_owned())
            .unwrap_or(argv0);
    }
    which::which(&argv0)
        .ok()
        .or_else(|| std::env::current_exe().ok())
        .unwrap_or(argv0)
}

async fn which_shim(
    config: &mut Arc<Config>,
    bin_name: &str,
    args: &[String],
) -> Result<(PathBuf, Toolset, Option<CommandWrapper>)> {
    // Shell completion invokes `usage complete-word` through the `usage` shim.
    // It should use the installed CLI or fail locally, never resolve a floating
    // tool version or auto-install over the network while the user is pressing
    // tab. On Windows the shim is invoked as `usage.exe`, so strip the platform
    // executable suffix before comparing.
    let shim_name = command_name_without_exe_suffix(bin_name);
    let is_usage = if cfg!(windows) {
        shim_name.eq_ignore_ascii_case("usage")
    } else {
        shim_name == "usage"
    };
    let completion_offline = is_usage && args.get(1).is_some_and(|arg| arg == "complete-word");
    let resolve_options = if completion_offline {
        ResolveOptions {
            offline: true,
            ..Default::default()
        }
    } else {
        ResolveOptions::default()
    };
    let task_tools = task_tool_args_from_env()?;
    let mut ts = ToolsetBuilder::new()
        .with_args(&task_tools)
        .with_resolve_options(resolve_options)
        .build(config)
        .await?;
    let wrappers = load_command_wrappers(
        &config.config_files,
        ts.versions.values().flat_map(|versions| &versions.requests),
    )?;
    validate_wrapper_names(wrappers.keys())?;
    let wrapper = if cfg!(macos) {
        wrappers
            .iter()
            .find(|(name, _)| command_names_eq(name, shim_name))
            .map(|(_, wrapper)| wrapper)
    } else {
        wrappers.get(shim_name)
    };
    if let Some(wrapper) = wrapper {
        if command_names_eq(wrapper.command(), shim_name) {
            bail!("command wrapper for {shim_name} cannot delegate to itself");
        }
        trace!("shim[{bin_name}] WRAPPER command: {}", wrapper.command());
        return Ok((PathBuf::from(wrapper.command()), ts, Some(wrapper.clone())));
    }
    // A configured tool may intentionally override an executable bundled by another installed
    // tool (for example, a pinned npm overrides Node's npm). Install a missing provider declared
    // by the registry before resolving an incidental installed provider.
    if !completion_offline
        && Settings::get().not_found_auto_install
        && ts
            .should_install_missing_registry_bin_provider(config, shim_name)
            .await?
    {
        for tv in ts
            .install_missing_bin(config, shim_name)
            .await?
            .unwrap_or_default()
        {
            let p = tv.backend()?;
            if let Some(bin) =
                backend_which_shim(p.as_ref(), config, &tv, shim_name, bin_name).await?
            {
                trace!(
                    "shim[{bin_name}] REGISTRY ToolVersion: {tv} bin: {bin}",
                    bin = display_path(&bin)
                );
                return Ok((bin, ts, None));
            }
        }
    }
    for lookup_name in [shim_name, bin_name].into_iter().unique() {
        if let Some((p, tv)) = ts.which(config, lookup_name).await
            && let Some(bin) = p.which(config, &tv, lookup_name).await?
        {
            trace!(
                "shim[{bin_name}] ToolVersion: {tv} bin: {bin}",
                bin = display_path(&bin)
            );
            return Ok((bin, ts, None));
        }
    }
    // Lazy tools are explicit fallback providers. They install on first shim use even when
    // general not-found auto-install is disabled, but only after configured/project providers
    // and already-installed tools have had a chance to win.
    if !completion_offline && ts.has_missing_lazy_bin_provider(config, shim_name).await? {
        for tv in ts
            .install_missing_lazy_bin(config, shim_name)
            .await?
            .unwrap_or_default()
        {
            let backend = tv.backend()?;
            if let Some(bin) =
                backend_which_shim(backend.as_ref(), config, &tv, shim_name, bin_name).await?
            {
                trace!(
                    "shim[{bin_name}] LAZY ToolVersion: {tv} bin: {bin}",
                    bin = display_path(&bin)
                );
                return Ok((bin, ts, None));
            }
        }
    }
    // Auto-installing here would download a tool over the network; skip it for
    // offline completion so `usage complete-word` fails locally instead.
    if !completion_offline && Settings::get().not_found_auto_install {
        for tv in ts
            .install_missing_bin(config, shim_name)
            .await?
            .unwrap_or_default()
        {
            let p = tv.backend()?;
            if let Some(bin) =
                backend_which_shim(p.as_ref(), config, &tv, shim_name, bin_name).await?
            {
                trace!(
                    "shim[{bin_name}] NOT_FOUND ToolVersion: {tv} bin: {bin}",
                    bin = display_path(&bin)
                );
                return Ok((bin, ts, None));
            }
        }
    }
    // fallback for "system"
    if Settings::get().not_found_system_fallback {
        let mise_bin = file::canonicalize_or_self(&env::MISE_BIN);
        for path in &*env::PATH {
            if file::is_mise_shims_dir(path) || file::is_command_wrapper_dir(path) {
                continue;
            }
            let bin = path.join(bin_name);
            if bin.is_file() && file::is_executable(&bin) {
                if file::is_active_mise_shim(&bin) {
                    continue;
                }
                // Skip if this binary is a mise shim (symlink pointing to the mise binary)
                if file::canonicalize_cached(&bin).is_some_and(|bin| bin == mise_bin) {
                    continue;
                }
                trace!("shim[{bin_name}] SYSTEM {bin}", bin = display_path(&bin));
                return Ok((bin, ts, None));
            }
        }
    }
    let mut tvs = ts.list_rtvs_with_bin(config, shim_name).await?;
    if tvs.is_empty() && shim_name != bin_name {
        tvs = ts.list_rtvs_with_bin(config, bin_name).await?;
    }
    match err_no_version_set(config, ts, shim_name, tvs).await {
        Ok(_) => unreachable!("err_no_version_set always returns an error"),
        Err(err) => Err(err),
    }
}

async fn backend_which_shim(
    backend: &dyn Backend,
    config: &Arc<Config>,
    tv: &ToolVersion,
    shim_name: &str,
    bin_name: &str,
) -> Result<Option<PathBuf>> {
    // The extensionless name preserves normal Windows extension expansion (including `.cmd`),
    // while the original name is required for dotted stems such as `python3.12.exe`.
    for lookup_name in [shim_name, bin_name].into_iter().unique() {
        if let Some(bin) = backend.which(config, tv, lookup_name).await? {
            return Ok(Some(bin));
        }
    }
    Ok(None)
}

/// Build the actionable, `which_shim`-style resolution error for a bin that a
/// shim failed to resolve while dispatching through `mise x` (the exe-mode shim
/// on Windows, invoked with `__MISE_SHIM_PATH` set). Without this, that path
/// surfaces the opaque `cannot find binary path`; symlink shims already get
/// this message directly from `which_shim`. See discussion #11183.
#[cfg(not(test))]
pub(crate) async fn err_shim_not_found(bin_name: &str) -> color_eyre::Report {
    // Windows exe shims are invoked as `<tool>.exe`; name `<tool>` in the message.
    let bin_stem = bin_name
        .strip_suffix(std::env::consts::EXE_SUFFIX)
        .unwrap_or(bin_name);
    let build = async {
        let config = Config::get().await?;
        let ts = ToolsetBuilder::new().build(&config).await?;
        let tvs = ts.list_rtvs_with_bin(&config, bin_stem).await?;
        // err_no_version_set always returns Err; map its Ok arm defensively.
        err_no_version_set(&config, ts, bin_stem, tvs)
            .await
            .map(|_| eyre!("cannot find binary path: {bin_stem}"))
    };
    match build.await {
        Ok(report) | Err(report) => report,
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ShimScope {
    User,
    System,
    Both,
}

/// The shim farms that serve as the lazy-install fallback boundary on PATH: the user
/// farm, plus the system farm when it exists as separate storage.
pub(crate) fn shim_farm_dirs() -> Vec<PathBuf> {
    let user_shims = dirs::shims();
    let system_shims = dirs::system_shims();
    let mut dirs = vec![user_shims];
    if system_shims.is_dir() && !file::storage_paths_eq(&dirs[0], &system_shims) {
        dirs.push(system_shims);
    }
    dirs
}

/// Add bootstrap shims for missing lazy declarations without pruning either farm.
///
/// A `lazy = true` entry written by hand has no shim until something rebuilds the farm.
/// An interactive shell still recovers, because its not-found handler installs the
/// tool, but a task or `mise x` child has no such handler and fails with "command not
/// found" (discussion #12678). `missing` is the toolset's already-computed missing
/// version list. Once every lazy declaration is installed there is nothing left to
/// write, and the function returns before locating the mise binary, so that steady
/// state costs only the option checks.
pub(crate) fn ensure_lazy_shims(missing: &[ToolVersion]) -> Result<()> {
    let mut bins_by_dir = BTreeMap::<PathBuf, Vec<String>>::new();
    let mut lazy_bins_error = None;
    for tv in missing {
        if tv.request.options().lazy != Some(true) {
            continue;
        }
        let bins = match tv.request.lazy_bins() {
            Ok(Some(bins)) => bins,
            Ok(None) => continue,
            Err(err) => {
                // One malformed lazy declaration must not prevent bootstrap shims
                // from being written for every other declaration in the toolset.
                lazy_bins_error.get_or_insert(err);
                continue;
            }
        };
        let shims_dir = if shim_scope_contains_request(ShimScope::System, &tv.request) {
            dirs::system_shims()
        } else {
            dirs::shims()
        };
        bins_by_dir.entry(shims_dir).or_default().extend(bins);
    }
    if !bins_by_dir.is_empty() {
        // Locating the mise binary walks PATH, so defer it until a declaration
        // actually needs a shim.
        let mise_bin = mise_bin_for_shims().absolutize()?.into_owned();
        for (shims_dir, bins) in bins_by_dir {
            let shims = bins
                .iter()
                .flat_map(|bin| platform_shim_names(&mise_bin, bin))
                .collect::<BTreeSet<String>>();
            match write_bootstrap_shims(&mise_bin, &shims_dir, &shims, false)? {
                None => {}
                // A shared farm such as `/usr/local/bin` may belong to root. No
                // command can write a bootstrap shim there for this user, so
                // warning on every `mise env`, `mise x` and `mise run` would only
                // be noise. Lazy tools in that farm still install through the
                // not-found handler or an explicit `mise install`.
                Some(err) => {
                    debug!(
                        "skipping bootstrap shims in {}: {err:#}",
                        display_path(&shims_dir)
                    );
                }
            }
        }
    }
    if let Some(err) = lazy_bins_error {
        Err(err)
    } else {
        Ok(())
    }
}

fn write_bootstrap_shims(
    mise_bin: &Path,
    shims_dir: &Path,
    shims: &BTreeSet<String>,
    _prune_stale_windows_variants: bool,
) -> Result<Option<eyre::Report>> {
    if let Err(err) = file::create_dir_all(shims_dir) {
        if is_permission_denied(&err) {
            return Ok(Some(err));
        }
        return Err(err);
    }
    // Lock failures come from the cache rather than the target farm and must
    // remain visible to the user.
    let _lock = LockFile::new(shims_dir).lock()?;

    #[cfg(windows)]
    if _prune_stale_windows_variants {
        remove_stale_windows_shim_variants(shims_dir, shims)?;
    }

    #[cfg(windows)]
    validate_windows_shim_source(mise_bin)?;

    for shim in shims {
        let path = shims_dir.join(shim);
        if !path.exists()
            && let Err(err) = add_shim(mise_bin, &path, shim)
        {
            if is_permission_denied(&err) {
                return Ok(Some(err));
            }
            return Err(err);
        }
    }
    Ok(None)
}

#[cfg(windows)]
fn remove_stale_windows_shim_variants(shims_dir: &Path, desired: &BTreeSet<String>) -> Result<()> {
    let stems = desired
        .iter()
        .map(|shim| {
            Path::new(shim)
                .with_extension("")
                .to_string_lossy()
                .to_string()
        })
        .collect::<BTreeSet<_>>();
    for stem in stems {
        for variant in [stem.clone(), format!("{stem}.cmd"), format!("{stem}.exe")] {
            if !desired.contains(&variant) {
                remove_shim_with_rename_fallback(&shims_dir.join(variant))?;
            }
        }
    }
    Ok(())
}

#[cfg(windows)]
fn validate_windows_shim_source(mise_bin: &Path) -> Result<()> {
    match effective_shim_mode(mise_bin).as_ref() {
        "exe" => {
            let source =
                find_mise_shim_bin(mise_bin).ok_or_else(|| eyre!("mise-shim.exe not found"))?;
            fs::File::open(&source)
                .wrap_err_with(|| eyre!("Failed to open shim source {}", display_path(&source)))?;
        }
        "hardlink" => {
            fs::metadata(mise_bin).wrap_err_with(|| {
                eyre!("Failed to access shim source {}", display_path(mise_bin))
            })?;
        }
        _ => {}
    }
    Ok(())
}

fn is_permission_denied(err: &eyre::Report) -> bool {
    err.chain().any(|cause| {
        cause.downcast_ref::<std::io::Error>().is_some_and(|io| {
            matches!(
                io.kind(),
                std::io::ErrorKind::PermissionDenied | std::io::ErrorKind::ReadOnlyFilesystem
            )
        })
    })
}

pub(crate) async fn reshim_for(
    config: &Arc<Config>,
    ts: &Toolset,
    force: bool,
    requested_scope: ShimScope,
) -> Result<()> {
    let user_shims = dirs::shims();
    let system_shims = dirs::system_shims();
    let collocated = file::storage_paths_eq(&user_shims, &system_shims);
    let scope = if collocated {
        ShimScope::Both
    } else {
        requested_scope
    };
    let shims_dir = match requested_scope {
        ShimScope::User | ShimScope::Both => user_shims,
        ShimScope::System => system_shims,
    };
    let _lock = LockFile::new(&shims_dir)
        .with_callback(|l| {
            trace!("reshim callback {}", l.display());
        })
        .lock();

    let mise_bin = mise_bin_for_shims();
    let mise_bin = mise_bin.absolutize()?; // relative paths don't work as shims

    #[cfg(windows)]
    let shim_mode = effective_shim_mode(&mise_bin);
    #[cfg(not(windows))]
    let shim_mode = String::new();
    let shim_mode_changed = cfg!(windows) && {
        let mode_file = shims_dir.join(".mode");
        mode_file
            .exists()
            .then(|| fs::read_to_string(&mode_file).unwrap_or_default())
            .is_some_and(|prev| prev.trim() != shim_mode)
    };
    // On Windows, "exe"/"hardlink" shims are literal copies of the mise(-shim)
    // binary, so they go stale when mise is updated (by self-update or an
    // external package manager) until a forced reshim. Track the mise version
    // that generated the shims in a `.version` marker (mirroring `.mode`) and
    // rebuild from scratch whenever it changes. The marker is written by
    // whichever binary runs reshim, so after an update the new binary stamps
    // the new version. See discussion #10022.
    let shim_version = env!("CARGO_PKG_VERSION");
    let shim_version_changed = cfg!(windows) && {
        let version_file = shims_dir.join(".version");
        let prev = fs::read_to_string(&version_file).ok();
        shim_version_stale(prev.as_deref(), shim_version, &shim_mode)
    };
    let full_rebuild = force || shim_mode_changed || shim_version_changed;
    file::create_dir_all(&shims_dir)?;

    let dedicated = is_dedicated_shims_dir(&shims_dir);
    let (desired, shims_to_stage, known_owned, prune_entries) = if full_rebuild {
        let desired = get_desired_shims(config, &mise_bin, ts, scope, force).await?;
        let shims_to_stage = desired.iter().cloned().collect();
        if dedicated {
            (desired, shims_to_stage, HashSet::new(), HashSet::new())
        } else {
            let actual = get_actual_shims(&mise_bin, &shims_dir).await?;
            let prune_entries = actual.owned.difference(&desired).cloned().collect();
            (desired, shims_to_stage, actual.owned, prune_entries)
        }
    } else {
        let diffs = get_shim_diffs(config, &mise_bin, ts, &shims_dir, scope, false).await?;
        (
            diffs.desired,
            diffs.missing,
            diffs.owned,
            diffs.extra.into_iter().collect(),
        )
    };
    let staging = stage_shim_farm(
        &shims_dir,
        &mise_bin,
        scope,
        &shims_to_stage,
        &shim_mode,
        shim_version,
    )
    .await?;
    publish_staged_shim_farm(
        &shims_dir,
        &mise_bin,
        staging,
        desired,
        known_owned,
        prune_entries,
        full_rebuild && dedicated,
    )?;

    if matches!(requested_scope, ShimScope::User | ShimScope::Both) {
        sync_command_wrapper_shims(config, ts, &mise_bin, full_rebuild)?;
    }

    Ok(())
}

async fn stage_shim_farm(
    shims_dir: &Path,
    mise_bin: &Path,
    scope: ShimScope,
    desired: &BTreeSet<String>,
    shim_mode: &str,
    shim_version: &str,
) -> Result<tempfile::TempDir> {
    let staging = tempfile::Builder::new()
        .prefix(".mise-shims-stage-")
        .tempdir_in(shims_dir)
        .wrap_err_with(|| {
            format!(
                "failed to create shim staging directory in {}",
                display_path(shims_dir)
            )
        })?;
    write_shim_metadata(staging.path(), shim_mode, shim_version)?;
    for shim in desired {
        add_shim(mise_bin, &staging.path().join(shim), shim)?;
    }
    add_plugin_shims(staging.path(), scope).await?;
    Ok(staging)
}

fn write_shim_metadata(shims_dir: &Path, shim_mode: &str, shim_version: &str) -> Result<()> {
    if cfg!(windows) {
        // Written for every shim mode even though `.version` is only consulted
        // for exe/hardlink shims. Keeping both markers current makes later mode
        // transitions deterministic.
        file::write(shims_dir.join(".mode"), shim_mode)?;
        file::write(shims_dir.join(".version"), shim_version)?;
    }
    Ok(())
}

async fn add_plugin_shims(shims_dir: &Path, scope: ShimScope) -> Result<()> {
    if !matches!(scope, ShimScope::User | ShimScope::Both) {
        return Ok(());
    }
    let mut jset = JoinSet::new();
    for plugin in backend::list() {
        let shims_dir = shims_dir.to_path_buf();
        jset.spawn(async move {
            if let Ok(files) = dirs::PLUGINS.join(plugin.id()).join("shims").read_dir() {
                for bin in files {
                    let bin = bin?;
                    let bin_name = bin.file_name().into_string().unwrap();
                    let symlink_path = shims_dir.join(bin_name);
                    make_shim(&bin.path(), &symlink_path).await?;
                }
            }
            Ok(())
        });
    }
    jset.join_all()
        .await
        .into_iter()
        .collect::<Result<Vec<_>>>()?;
    Ok(())
}

fn publish_staged_shim_farm(
    shims_dir: &Path,
    mise_bin: &Path,
    staging: tempfile::TempDir,
    mut desired: HashSet<String>,
    known_owned: HashSet<String>,
    prune_entries: HashSet<String>,
    prune_unmanaged: bool,
) -> Result<()> {
    let mut entries = staging
        .path()
        .read_dir()
        .wrap_err_with(|| {
            format!(
                "failed to read staged shim directory: {}",
                display_path(staging.path())
            )
        })?
        .collect::<std::io::Result<Vec<_>>>()?;
    // Publish metadata last. If publication is interrupted, an old marker makes
    // the next reshim retry instead of treating a partially published farm as
    // current.
    entries.sort_by_key(|entry| is_hidden_shim_name(&entry.file_name()));
    desired.extend(
        entries
            .iter()
            .filter_map(|entry| entry.file_name().into_string().ok()),
    );
    for entry in entries {
        let source = entry.path();
        let destination = shims_dir.join(entry.file_name());
        let destination_exists = destination.exists() || destination.is_symlink();
        let destination_owned = is_hidden_shim_name(&entry.file_name())
            || known_owned.contains(&entry.file_name().to_string_lossy().into_owned())
            || (destination_exists
                && (is_mise_shim(&destination, mise_bin)?
                    || symlink_target_names_mise(&destination)?));
        if destination_exists
            && !prune_unmanaged
            && !destination_owned
            && !files_identical(&source, &destination).unwrap_or(false)
        {
            warn!(
                "not replacing unmanaged file in shims directory: {}",
                display_path(&destination)
            );
            continue;
        }
        if cfg!(windows) && destination_exists {
            remove_shim_with_rename_fallback(&destination)?;
        }
        // Rename replaces a same-named file atomically. A publication error
        // therefore leaves that live shim untouched; an interrupted rebuild can
        // leave a mixture of old and new shims, but never evacuates the farm.
        fs::rename(&source, &destination).wrap_err_with(|| {
            format!(
                "failed to publish shim {} to {}",
                display_path(&source),
                display_path(&destination)
            )
        })?;
    }

    // A shared executable directory can contain arbitrary user and package
    // manager files. Only prune entries that can be identified as mise shims.
    // Mise's default, dedicated farms retain their historical full cleanup.
    for shim in list_shims_in(shims_dir)?.difference(&desired) {
        let path = shims_dir.join(shim);
        if prune_unmanaged || prune_entries.contains(shim) {
            if cfg!(windows) {
                remove_shim_with_rename_fallback(&path)?;
            } else {
                file::remove_all(&path)?;
            }
        }
    }

    if let Err(err) = staging.close() {
        warn!("failed to remove shim staging directory: {err}");
    }
    Ok(())
}

fn is_dedicated_shims_dir(path: &Path) -> bool {
    matches_unredirected_dedicated_dir(path, &dirs::DATA.join("shims"))
        || matches_unredirected_dedicated_dir(path, &env::MISE_SYSTEM_DATA_DIR.join("shims"))
}

fn matches_unredirected_dedicated_dir(path: &Path, dedicated: &Path) -> bool {
    if !file::paths_eq(path, dedicated) {
        return false;
    }
    let Some((parent, file_name)) = path.parent().zip(path.file_name()) else {
        return false;
    };
    match (dunce::canonicalize(path), dunce::canonicalize(parent)) {
        (Ok(resolved), Ok(resolved_parent)) => {
            file::paths_eq(&resolved, &resolved_parent.join(file_name))
        }
        _ => false,
    }
}

fn files_identical(a: &Path, b: &Path) -> Result<bool> {
    if a.is_symlink() || b.is_symlink() {
        return Ok(a.is_symlink() && b.is_symlink() && fs::read_link(a)? == fs::read_link(b)?);
    }
    if !a.is_file() || !b.is_file() {
        return Ok(false);
    }
    if fs::metadata(a)?.len() != fs::metadata(b)?.len() {
        return Ok(false);
    }
    Ok(fs::read(a)? == fs::read(b)?)
}

fn read_file_prefix(path: &Path) -> Result<Vec<u8>> {
    let mut contents = Vec::new();
    fs::File::open(path)?
        .take(SHIM_SCRIPT_INSPECTION_LIMIT)
        .read_to_end(&mut contents)?;
    Ok(contents)
}

fn is_mise_dispatcher_name(name: &str) -> bool {
    if cfg!(windows) {
        name.eq_ignore_ascii_case("mise") || name.eq_ignore_ascii_case("mise.exe")
    } else {
        name == "mise"
    }
}

fn resolved_symlink_target(path: &Path) -> Result<Option<PathBuf>> {
    if !path.is_symlink() {
        return Ok(None);
    }
    let target = fs::read_link(path)?;
    Ok(Some(if target.is_absolute() {
        target
    } else {
        path.parent().unwrap_or_else(|| Path::new(".")).join(target)
    }))
}

fn symlink_target_names_mise(path: &Path) -> Result<bool> {
    Ok(resolved_symlink_target(path)?.is_some_and(|target| {
        target
            .file_name()
            .and_then(|name| name.to_str())
            .is_some_and(is_mise_dispatcher_name)
    }))
}

fn is_mise_shim(path: &Path, mise_bin: &Path) -> Result<bool> {
    if path
        .file_name()
        .and_then(|name| name.to_str())
        .is_some_and(is_mise_dispatcher_name)
    {
        // A package manager may install mise itself as a symlink in the shared
        // directory. It is the dispatcher, not one of its shims.
        return Ok(false);
    }
    if path.is_symlink() {
        let target = resolved_symlink_target(path)?.expect("symlink target");
        return Ok(file::paths_eq(
            &file::canonicalize_or_self(&target),
            &file::canonicalize_or_self(mise_bin),
        ));
    }

    #[cfg(windows)]
    {
        if !path.is_file() {
            return Ok(false);
        }
        let parent = path.parent().unwrap_or_else(|| Path::new("."));
        if is_dedicated_shims_dir(parent) {
            // Preserve the existing upgrade behavior in mise's dedicated
            // farms. Native copies from an older mise cannot be identified by
            // their contents after mise-shim.exe changes. Use the same
            // unredirected check as pruning so a junction to a shared bin
            // directory never grants ownership of every regular file there.
            return Ok(true);
        }

        let is_script = path.extension().is_none()
            || path
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("cmd"));
        let contents = if is_script {
            read_file_prefix(path).unwrap_or_default()
        } else {
            Vec::new()
        };
        if is_generated_shell_shim(&contents)
            || is_generated_windows_file_shim_contents(path, &contents)
        {
            return Ok(true);
        }
        let matches_mise = files_identical(path, mise_bin).unwrap_or(false);
        let matches_launcher = find_mise_shim_bin(mise_bin)
            .is_some_and(|launcher| files_identical(path, &launcher).unwrap_or(false));
        if matches_mise || matches_launcher {
            return Ok(true);
        }
        let contents = if is_script {
            contents
        } else {
            fs::read(path).unwrap_or_default()
        };
        return Ok(has_mise_native_shim_fingerprint(&contents));
    }

    #[cfg(not(windows))]
    {
        if !path.is_file() {
            return Ok(false);
        }
        Ok(is_generated_shell_shim(
            &read_file_prefix(path).unwrap_or_default(),
        ))
    }
}

#[cfg(any(windows, test))]
fn bytes_contain(haystack: &[u8], needle: &[u8]) -> bool {
    !needle.is_empty()
        && haystack
            .windows(needle.len())
            .any(|window| window == needle)
}

fn is_generated_shell_shim(contents: &[u8]) -> bool {
    contents.starts_with(GENERATED_SHELL_SHIM_HEADER.as_bytes()) || is_legacy_plugin_shim(contents)
}

fn is_legacy_plugin_shim(contents: &[u8]) -> bool {
    let Ok(contents) = std::str::from_utf8(contents) else {
        return false;
    };
    let mut lines = contents.lines();
    lines.next() == Some("#!/bin/sh")
        && lines
            .next()
            .is_some_and(|line| line.starts_with("export ASDF_DATA_DIR=") && line.len() > 21)
        && lines
            .next()
            .is_some_and(|line| line.starts_with("export PATH=\"") && line.ends_with(":$PATH\""))
        && lines
            .next()
            .is_some_and(|line| line.starts_with("mise x -- ") && line.ends_with(" \"$@\""))
        && lines.next().is_none()
}

#[cfg(any(windows, test))]
fn has_mise_native_shim_fingerprint(contents: &[u8]) -> bool {
    bytes_contain(contents, NATIVE_SHIM_MARKER)
        // Transition shims made by mise versions predating the stable marker.
        || (bytes_contain(
            contents,
            b"mise-shim: failed to determine executable path",
        ) && bytes_contain(contents, b"mise-shim: failed to execute mise"))
        || (bytes_contain(contents, b"__MISE_SHIM_PATH")
            && bytes_contain(contents, b"recursive shim invocation detected")
            && bytes_contain(contents, b"mise x --"))
}

#[cfg(test)]
fn is_generated_windows_file_shim(path: &Path) -> bool {
    fs::read(path).is_ok_and(|contents| is_generated_windows_file_shim_contents(path, &contents))
}

#[cfg(any(windows, test))]
fn is_generated_windows_file_shim_contents(path: &Path, contents: &[u8]) -> bool {
    let Some(name) = path.file_stem().and_then(|name| name.to_str()) else {
        return false;
    };
    if path
        .extension()
        .is_some_and(|ext| ext.eq_ignore_ascii_case("cmd"))
    {
        contents.starts_with(GENERATED_WINDOWS_CMD_SHIM_HEADER.as_bytes())
            || contents == windows_file_shim_body(name).as_bytes()
            || is_legacy_windows_cmd_shim(contents)
    } else if path.extension().is_none() {
        if contents.starts_with(GENERATED_WINDOWS_BASH_SHIM_HEADER.as_bytes()) {
            return true;
        }
        #[cfg(windows)]
        return contents == bash_shim_script(name).as_bytes();
        #[cfg(not(windows))]
        return false;
    } else {
        false
    }
}

#[cfg(any(windows, test))]
fn is_legacy_windows_cmd_shim(contents: &[u8]) -> bool {
    let normalized = String::from_utf8_lossy(contents).replace("\r\n", "\n");
    matches!(
        normalized.as_str(),
        "@echo off\nsetlocal\nmise x -- %*\n" | "@echo off\nsetlocal\nmise x -- %*"
    )
}

/// Create wrappers needed by a runtime toolset without removing another task's wrappers.
pub(crate) fn ensure_command_wrapper_shims(config: &Config, ts: &Toolset) -> Result<()> {
    let wrappers = load_command_wrappers(
        &config.config_files,
        ts.versions.values().flat_map(|versions| &versions.requests),
    )?;
    validate_wrapper_names(wrappers.keys())?;
    if wrappers.is_empty() {
        return Ok(());
    }
    let mise_bin = mise_bin_for_shims().absolutize()?.into_owned();
    let shims = wrappers
        .keys()
        .flat_map(|name| platform_shim_names(&mise_bin, name))
        .collect();
    if let Some(error) =
        write_bootstrap_shims(&mise_bin, &dirs::COMMAND_WRAPPERS, &shims, cfg!(windows))?
    {
        return Err(error);
    }
    Ok(())
}

fn sync_command_wrapper_shims(
    config: &Config,
    ts: &Toolset,
    mise_bin: &Path,
    force: bool,
) -> Result<()> {
    let wrappers = load_command_wrappers(
        &config.config_files,
        ts.versions.values().flat_map(|versions| &versions.requests),
    )?;
    validate_wrapper_names(wrappers.keys())?;
    if wrappers.is_empty() {
        if cfg!(windows) {
            remove_shims_individually(&dirs::COMMAND_WRAPPERS)?;
        } else {
            file::remove_all(&*dirs::COMMAND_WRAPPERS)?;
        }
        return Ok(());
    }
    if force {
        if cfg!(windows) {
            remove_shims_individually(&dirs::COMMAND_WRAPPERS)?;
        } else {
            file::remove_all(&*dirs::COMMAND_WRAPPERS)?;
        }
    }
    file::create_dir_all(&*dirs::COMMAND_WRAPPERS)?;

    let mut desired = HashSet::new();
    for name in wrappers.keys() {
        desired.extend(platform_shim_names(mise_bin, name));
    }
    let actual = list_shims_in(&dirs::COMMAND_WRAPPERS)?;
    for shim in desired.difference(&actual) {
        let path = dirs::COMMAND_WRAPPERS.join(shim);
        if cfg!(windows) && path.exists() {
            remove_shim_with_rename_fallback(&path)?;
        }
        add_shim(mise_bin, &path, shim)?;
    }
    for shim in actual.difference(&desired) {
        let path = dirs::COMMAND_WRAPPERS.join(shim);
        if cfg!(windows) {
            remove_shim_with_rename_fallback(&path)?;
        } else {
            file::remove_all(&path)?;
        }
    }
    Ok(())
}

pub(crate) fn command_names_eq(a: &str, b: &str) -> bool {
    if cfg!(macos) {
        a.to_lowercase() == b.to_lowercase()
    } else {
        a == b
    }
}

pub(crate) fn command_name_without_exe_suffix(bin_name: &str) -> &str {
    let suffix = std::env::consts::EXE_SUFFIX;
    if suffix.is_empty() {
        return bin_name;
    }
    let suffix_start = bin_name.len().saturating_sub(suffix.len());
    match (bin_name.get(..suffix_start), bin_name.get(suffix_start..)) {
        (Some(name), Some(actual_suffix)) if actual_suffix.eq_ignore_ascii_case(suffix) => name,
        _ => bin_name,
    }
}

fn validate_wrapper_name(name: &str) -> Result<()> {
    if name.is_empty() || name == "." || name == ".." || name.contains('/') || name.contains('\\') {
        bail!("invalid command wrapper name: {name:?}");
    }
    if cfg!(windows) && name.contains('.') {
        bail!("command wrapper names cannot contain dots on Windows: {name:?}");
    }
    Ok(())
}

fn validate_wrapper_names<'a>(names: impl IntoIterator<Item = &'a String>) -> Result<()> {
    let mut normalized = HashSet::new();
    for name in names {
        validate_wrapper_name(name)?;
        if cfg!(macos) && !normalized.insert(name.to_lowercase()) {
            bail!("command wrapper names collide on macOS after case normalization: {name:?}");
        }
    }
    Ok(())
}

/// Resolve the mise executable that Unix symlink shims should target.
///
/// Snap exposes applications through `/snap/bin`, where each command is a symlink to the
/// `snap` dispatcher. That dispatcher identifies the application from argv[0], so invoking it
/// through a mise shim named `node`, `python`, etc. runs the snap CLI instead of mise. Point Snap
/// shims at the payload beneath its refresh-stable `current` symlink instead. For other package
/// managers, retain the PATH-visible executable so their stable launcher survives upgrades.
pub(crate) fn mise_bin_for_shims() -> PathBuf {
    env::var_path("SNAP")
        .as_deref()
        .and_then(|snap| snap_mise_bin(&env::MISE_BIN, snap))
        .unwrap_or_else(|| file::which_no_shims("mise").unwrap_or(env::MISE_BIN.clone()))
}

fn snap_mise_bin(mise_bin: &Path, snap: &Path) -> Option<PathBuf> {
    let relative = mise_bin
        .strip_prefix(snap)
        .map(Path::to_path_buf)
        .or_else(|_| {
            let mise_bin = file::canonicalize_or_self(mise_bin);
            let snap = file::canonicalize_or_self(snap);
            mise_bin.strip_prefix(snap).map(Path::to_path_buf)
        })
        .ok()?;
    let snap_mount = snap.parent()?;
    Some(snap_mount.join("current").join(relative))
}

/// Remove all shim files from a directory individually, skipping dotfiles like
/// `.mode`. Uses [`remove_shim_with_rename_fallback`] for each entry so locked
/// `.exe` files on Windows are renamed out of the way instead of causing a
/// hard error.
fn remove_shims_individually(shims_dir: &Path) -> Result<()> {
    let entries = match shims_dir.read_dir() {
        Ok(entries) => entries,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(e) => {
            return Err(e).wrap_err_with(|| {
                format!(
                    "failed to read shims directory: {}",
                    display_path(shims_dir)
                )
            });
        }
    };
    for entry in entries {
        let entry = entry?;
        let name = entry.file_name();
        // skip dotfiles (e.g. .mode) — these are metadata, not shims
        if is_hidden_shim_name(&name) {
            continue;
        }
        let path = entry.path();
        remove_shim_with_rename_fallback(&path)?;
    }
    Ok(())
}

/// Remove a single shim file. On Windows, if deletion fails (e.g. because the
/// `.exe` is locked by another process), rename it to `<name>.old` so the path
/// is freed for a new shim. The `.old` file will be cleaned up on the next
/// reshim or when the lock is released.
fn remove_shim_with_rename_fallback(path: &Path) -> Result<()> {
    // First, try to clean up any leftover .old files from a previous run.
    let old_path = old_shim_path(path);
    if old_path.exists() {
        let _ = fs::remove_file(&old_path); // best-effort
    }

    match fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(e) if cfg!(windows) && matches!(e.raw_os_error(), Some(5) | Some(32)) => {
            // ERROR_ACCESS_DENIED (5) or ERROR_SHARING_VIOLATION (32): file is
            // locked by another process, rename it instead.
            trace!(
                "cannot delete locked shim {}, renaming to .old",
                display_path(path)
            );
            fs::rename(path, &old_path).wrap_err_with(|| {
                format!(
                    "failed to rename locked shim {} to {}",
                    display_path(path),
                    display_path(&old_path)
                )
            })?;
            Ok(())
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(e) => Err(e).wrap_err_with(|| format!("failed to remove shim: {}", display_path(path))),
    }
}

fn old_shim_path(path: &Path) -> PathBuf {
    let mut name = path.file_name().unwrap_or_default().to_os_string();
    name.push(".old");
    path.with_file_name(name)
}

/// Find the `mise-shim.exe` that ships beside the real `mise_bin` — following
/// symlinks/junctions — if there is one.
///
/// Not `#[cfg(windows)]`, unlike the shim code around it: `mise generate task-stubs
/// --windows-launcher=exe` asks the same question, and on a host where the answer is always `None`
/// that is the honest answer to report rather than a compile error to route around.
pub(crate) fn find_mise_shim_bin(mise_bin: &Path) -> Option<PathBuf> {
    // mise-shim.exe ships beside the real mise.exe, which may sit behind a
    // symlink or junction (dunce avoids the `\\?\` verbatim prefix)
    let real_bin = dunce::canonicalize(mise_bin).unwrap_or_else(|_| mise_bin.to_path_buf());
    if let Some(parent) = real_bin.parent() {
        let candidate = parent.join("mise-shim.exe");
        if candidate.is_file() {
            return Some(candidate);
        }
    }
    // Fall back to searching PATH
    // Note: file::which on Windows checks extension only, not file existence,
    // so we must verify the file actually exists.
    file::which("mise-shim.exe").filter(|p| p.is_file())
}

/// Resolve the effective Windows shim mode, falling back to "file" if "exe" is
/// requested but mise-shim.exe is not available.
#[cfg(windows)]
fn effective_shim_mode(mise_bin: &Path) -> String {
    let mode = Settings::get().windows_shim_mode.clone();
    if mode == "exe" && find_mise_shim_bin(mise_bin).is_none() {
        // Once, not once per shim: this runs for every desired shim and for every shim written.
        warn_once!(
            "mise-shim.exe not found next to {} or on PATH, falling back to \"file\" shim mode",
            display_path(mise_bin)
        );
        return "file".to_string();
    }
    mode
}

/// Build the extension-less bash shim used on Windows in "file" mode (for Git
/// Bash/Cygwin). "exe" mode does not emit this — its native <tool>.exe is found
/// by those shells via `.exe` magic — so only "file" mode reaches this code.
///
/// The shim's directory can leak into WSL via the default Windows-PATH interop
/// (it is mounted under /mnt/c where every file is treated as executable), so WSL
/// runs this script natively. Calling the Windows `mise` from there either fails
/// with `exec: mise: not found` or, with a Linux mise present, recurses forever --
/// mise's loop guard only recognises its own shims dir, not the Windows one under
/// /mnt/c. So detect WSL, drop this shim's own directory from PATH, and exec a
/// native tool instead (or fail with a clear `<tool>: not found`). Outside WSL the
/// guard is inert, so Git Bash/Cygwin behaviour is unchanged. (#10299)
#[cfg(windows)]
fn bash_shim_script(tool: &str) -> String {
    formatdoc! {r#"
        #!/bin/bash
        # mise generated shim

        shim_dir=$(cd -- "$(dirname -- "$0")" && pwd -P)
        shim_path="$shim_dir/${{0##*/}}"
        if [ "${{__MISE_SHIM_PATH:-}}" = "$shim_path" ]; then
          echo "mise: recursive shim invocation detected for {tool}: $shim_path" >&2
          exit 1
        fi

        if [ -n "${{WSL_DISTRO_NAME:-}}" ] || [ -n "${{WSL_INTEROP:-}}" ] || [ -e /proc/sys/fs/binfmt_misc/WSLInterop ]; then
          new_path=
          # disable globbing so a PATH entry containing * ? [ is not expanded
          set -f
          IFS=:
          for p in $PATH; do
            [ "$p" = "$shim_dir" ] && continue
            new_path="${{new_path:+$new_path:}}$p"
          done
          unset IFS
          set +f
          export PATH="$new_path"
          exec {tool} "$@"
        fi

        export __MISE_SHIM_PATH="$shim_path"
        exec mise x -- {tool} "$@"
        "#}
}

/// The `.cmd` body for a `"file"`-mode shim: run `mise x -- <tool>` with the caller's arguments.
///
/// `%*` alone does not carry them. cmd.exe parses the whole command line before a batch file runs,
/// so calling one from PowerShell loses `& ^ | " < >` and expands `%VAR%` before `%*` ever expands.
/// Measured through a shim, every shape that arrives intact in `"exe"` mode arrives different here:
/// `c&d` runs `d` as a second command, `i^j` becomes `ij`, `a>b` writes a file called `b`, `e%OS%f`
/// becomes `eWindows_NTf`. The other three modes are native executables and are handed argv
/// directly, so this is the only mode that needs it.
///
/// The recovery is the one [`crate::cli::generate::windows_launcher_body`] documents in full: what
/// cmd destroys on the way in it also keeps, in its `CMDCMDLINE` pseudo-variable, so the body
/// copies that out — `!CMDCMDLINE!` rather than `%CMDCMDLINE%`, which is substituted before special
/// characters are parsed and truncates at the first `&` — and passes it through the environment,
/// where cmd gets no second chance to parse it. mise re-splits it with the rules a native program's
/// runtime uses and substitutes the result for everything after [`env::LAUNCHER_ARGS_SENTINEL`].
///
/// The guard decides whether cmd was spawned *for* this shim. When it was not — an interactive
/// prompt, a `call` from another batch file — the shell already split the arguments as it would for
/// a native program, so `%*` is correct and the script must not `exit`, which would close the
/// caller's shell. When it was, `exit` (rather than `exit /b`) also stops cmd running whatever it
/// queued from the same line, which would otherwise be reported as the tool's exit code.
///
/// Kept separate from the launcher body rather than shared: this one runs its recursion guard
/// first, and the launcher's line layout is pinned by `is_generated_launcher`, which has no
/// business tracking shim changes. The names come from the same constants, and
/// `the_shim_body_carries_the_same_recovery` fails if the shapes drift.
///
/// Compiled for tests on every platform, unlike the shim code around it, so the body itself is
/// unit-tested everywhere; `pub(crate)` so that comparison can live beside the launcher.
#[cfg(any(windows, test))]
pub(crate) fn windows_file_shim_body(shim: &str) -> String {
    let raw = env::LAUNCHER_RAW_CMDLINE_ENV;
    let path = env::LAUNCHER_PATH_ENV;
    let sentinel = env::LAUNCHER_ARGS_SENTINEL;
    let shim_env = env::MISE_SHIM_PATH_ENV;
    let run = format!("mise x -- {shim} {sentinel} %*");
    [
        "@echo off",
        "rem mise generated shim",
        // `%~f0` is captured before delayed expansion is on, so a `!` in the path survives.
        "setlocal DisableDelayedExpansion",
        "set \"shim_path=%~f0\"",
        &format!("if /I \"%{shim_env}%\"==\"%shim_path%\" ("),
        &format!("  echo mise: recursive shim invocation detected for {shim}: %shim_path% 1>&2"),
        "  exit /b 1",
        ")",
        &format!("set \"{shim_env}=%shim_path%\""),
        "setlocal EnableDelayedExpansion",
        &format!("set \"{path}=!shim_path!\""),
        &format!("set \"{raw}=!CMDCMDLINE!\""),
        &format!("if \"!{raw}!\"==\"!{raw}:%{path}%=!\" goto mise_shim_fallback"),
        &run,
        "exit !ERRORLEVEL!",
        ":mise_shim_fallback",
        // Cleared, or a value inherited from an outer launcher would be recovered as this one's.
        &format!("set \"{raw}=\""),
        &format!("set \"{path}=\""),
        &run,
    ]
    .join("\r\n")
        + "\r\n"
}

#[cfg(windows)]
fn add_shim(mise_bin: &Path, symlink_path: &Path, shim: &str) -> Result<()> {
    match effective_shim_mode(mise_bin).as_ref() {
        "exe" => {
            // In "exe" mode every desired shim is a native <tool>.exe copy of
            // mise-shim.exe (see get_desired_shims). No extension-less bash shim is
            // emitted: Git Bash / Cygwin resolve a bare name to the .exe via their
            // `.exe` magic, so emitting one is redundant and only pollutes WSL via
            // /mnt/c PATH interop (#10299).
            let mise_shim_bin =
                find_mise_shim_bin(mise_bin).ok_or_else(|| eyre!("mise-shim.exe not found"))?;
            // Copy mise-shim.exe as <tool>.exe
            fs::copy(&mise_shim_bin, symlink_path).wrap_err_with(|| {
                eyre!(
                    "Failed to copy {} to {}",
                    display_path(&mise_shim_bin),
                    display_path(symlink_path)
                )
            })?;
            Ok(())
        }
        "file" => {
            let shim = shim.trim_end_matches(".cmd");
            // write a shim file without extension for use in Git Bash/Cygwin
            file::write(symlink_path.with_extension(""), bash_shim_script(shim)).wrap_err_with(
                || {
                    eyre!(
                        "Failed to create symlink from {} to {}",
                        display_path(mise_bin),
                        display_path(symlink_path)
                    )
                },
            )?;
            file::write(
                symlink_path.with_extension("cmd"),
                windows_file_shim_body(shim),
            )
            .wrap_err_with(|| {
                eyre!(
                    "Failed to create symlink from {} to {}",
                    display_path(mise_bin),
                    display_path(symlink_path)
                )
            })
        }
        "hardlink" => fs::hard_link(mise_bin, symlink_path).wrap_err_with(|| {
            eyre!(
                "Failed to create hardlink from {} to {}",
                display_path(mise_bin),
                display_path(symlink_path)
            )
        }),
        "symlink" => {
            std::os::windows::fs::symlink_file(mise_bin, symlink_path).wrap_err_with(|| {
                eyre!(
                    "Failed to create symlink from {} to {}",
                    display_path(mise_bin),
                    display_path(symlink_path)
                )
            })
        }
        _ => panic!("Unknown shim mode"),
    }
}

#[cfg(unix)]
fn add_shim(mise_bin: &Path, symlink_path: &Path, _shim: &str) -> Result<()> {
    file::make_symlink(mise_bin, symlink_path).wrap_err_with(|| {
        eyre!(
            "Failed to create symlink from {} to {}",
            display_path(mise_bin),
            display_path(symlink_path)
        )
    })?;
    Ok(())
}

pub(crate) struct ShimDiffs {
    pub missing: BTreeSet<String>,
    pub extra: BTreeSet<String>,
    pub desired: HashSet<String>,
    owned: HashSet<String>,
}

struct ActualShims {
    current: HashSet<String>,
    dedicated_present: HashSet<String>,
    owned: HashSet<String>,
    occupied: HashSet<String>,
    repairable: HashSet<String>,
}

fn calculate_shim_diffs(
    actual: &ActualShims,
    desired: &HashSet<String>,
    dedicated: bool,
) -> (BTreeSet<String>, BTreeSet<String>) {
    // In a shared executable directory, a same-named unmanaged entry is an
    // intentional collision, not a missing shim that reshim can repair. Treat
    // it as occupied so doctor and automatic post-install reshims stay quiet.
    let (missing, extra) = if dedicated {
        (
            desired
                .difference(&actual.dedicated_present)
                .cloned()
                .collect(),
            actual
                .dedicated_present
                .difference(desired)
                .cloned()
                .collect(),
        )
    } else {
        (
            desired
                .iter()
                .filter(|name| {
                    !actual.current.contains(*name)
                        && (!actual.occupied.contains(*name)
                            || actual.owned.contains(*name)
                            || actual.repairable.contains(*name))
                })
                .cloned()
                .collect(),
            actual.owned.difference(desired).cloned().collect(),
        )
    };
    (missing, extra)
}

// get_shim_diffs contrasts the actual shims on disk
// with the desired shims specified by the Toolset
pub(crate) async fn get_shim_diffs(
    config: &Arc<Config>,
    mise_bin: impl AsRef<Path>,
    toolset: &Toolset,
    shims_dir: &Path,
    scope: ShimScope,
    strict_lazy_bins: bool,
) -> Result<ShimDiffs> {
    let mise_bin = mise_bin.as_ref();
    let (actual_shims, desired_shims) = tokio::join!(
        get_actual_shims(mise_bin, shims_dir),
        get_desired_shims(config, mise_bin, toolset, scope, strict_lazy_bins)
    );
    let (actual_shims, desired_shims) = (actual_shims?, desired_shims?);
    let (missing, extra) = calculate_shim_diffs(
        &actual_shims,
        &desired_shims,
        is_dedicated_shims_dir(shims_dir),
    );
    time!("get_shim_diffs sizes: ({},{})", missing.len(), extra.len());
    Ok(ShimDiffs {
        missing,
        extra,
        desired: desired_shims,
        owned: actual_shims.owned,
    })
}

async fn get_actual_shims(mise_bin: impl AsRef<Path>, shims_dir: &Path) -> Result<ActualShims> {
    let mise_bin = mise_bin.as_ref();
    let occupied = list_shims_in(shims_dir)?;
    let mut current = HashSet::new();
    let mut dedicated_present = HashSet::new();
    let mut owned = HashSet::new();
    let mut repairable = HashSet::new();
    for bin in &occupied {
        let path = shims_dir.join(bin);
        if is_mise_shim(&path, mise_bin).unwrap_or(false) {
            owned.insert(bin.clone());
            if is_current_owned_mise_shim(&path, mise_bin).unwrap_or(false) {
                current.insert(bin.clone());
            }
        } else if symlink_target_names_mise(&path).unwrap_or(false) {
            repairable.insert(bin.clone());
        }
        if !path.is_symlink() || current.contains(bin) {
            dedicated_present.insert(bin.clone());
        }
    }
    Ok(ActualShims {
        current,
        dedicated_present,
        owned,
        occupied,
        repairable,
    })
}

fn is_current_owned_mise_shim(path: &Path, mise_bin: &Path) -> Result<bool> {
    if !path.is_symlink() {
        return Ok(true);
    }
    let target = resolved_symlink_target(path)?.expect("symlink target");
    // Raw path equality is deliberate. A shim that points through to the same
    // binary but bypasses the stable package-manager launcher must be migrated
    // before the versioned path disappears during an upgrade.
    Ok(file::paths_eq(&target, mise_bin))
}

fn list_executables_in_dir(dir: &Path) -> Result<HashSet<String>> {
    Ok(dir
        .read_dir()?
        .map(|bin| {
            let bin = bin?;
            let name = bin.file_name();
            if is_hidden_shim_name(&name) {
                return Ok(None);
            }
            // files and symlinks which are executable
            if file::is_executable(&bin.path())
                && (bin.file_type()?.is_file() || bin.file_type()?.is_symlink())
            {
                Ok(name.into_string().ok())
            } else {
                Ok(None)
            }
        })
        .collect::<Result<Vec<_>>>()?
        .into_iter()
        .flatten()
        .collect())
}

fn list_shims_in(dir: &Path) -> Result<HashSet<String>> {
    Ok(dir
        .read_dir()?
        .map(|bin| {
            let bin = bin?;
            let name = bin.file_name();
            // skip dotfiles (e.g. .mode) — these are metadata, not shims
            if is_hidden_shim_name(&name) {
                return Ok(None);
            }
            // files and symlinks which are executable or extensionless files (Git Bash/Cygwin)
            if (file::is_executable(&bin.path()) || bin.path().extension().is_none())
                && (bin.file_type()?.is_file() || bin.file_type()?.is_symlink())
            {
                Ok(name.into_string().ok())
            } else {
                Ok(None)
            }
        })
        .collect::<Result<Vec<_>>>()?
        .into_iter()
        .flatten()
        .collect())
}

fn is_hidden_shim_name(name: &std::ffi::OsStr) -> bool {
    name.to_string_lossy().starts_with('.')
}

/// Whether existing shims were generated by a different mise version AND the
/// current shim mode produces version-dependent shim files. "exe"/"hardlink"
/// embed a literal copy of the mise/mise-shim binary; "file" writes a bash script
/// whose contents are baked into the mise binary as well (e.g. the WSL guard added
/// in #10299), so all three must rebuild on a version change to pick up script
/// changes — otherwise a normal reshim leaves the old script in place. "symlink"
/// only points at the mise binary (no embedded content), so it is never
/// version-stale. `prev == None` (no `.version` marker yet) heals installs that
/// predate the marker by forcing a one-time rebuild. See discussions #10022 and
/// #10299.
fn shim_version_stale(prev: Option<&str>, current: &str, shim_mode: &str) -> bool {
    if !matches!(shim_mode, "exe" | "hardlink" | "file") {
        return false;
    }
    prev.map(|p| p.trim() != current).unwrap_or(true)
}

fn shim_scope_contains_install(scope: ShimScope, install_path: &Path) -> bool {
    if scope == ShimScope::Both {
        return true;
    }
    let system_installs = Settings::get().system_installs_dir().to_path_buf();
    if file::storage_paths_eq(&system_installs, &dirs::INSTALLS) {
        return true;
    }
    let is_system = install_path.starts_with(system_installs);
    matches!(scope, ShimScope::System) == is_system
}

fn shim_scope_contains_request(scope: ShimScope, request: &crate::toolset::ToolRequest) -> bool {
    if scope == ShimScope::Both {
        return true;
    }
    let is_system = request.source().path().is_some_and(|path| {
        crate::config::provenance::ConfigProvenance::from_path(path).scope()
            == crate::config::provenance::ConfigFileScope::System
    });
    matches!(scope, ShimScope::System) == is_system
}

async fn get_desired_shims(
    config: &Arc<Config>,
    mise_bin: &Path,
    toolset: &Toolset,
    scope: ShimScope,
    strict_lazy_bins: bool,
) -> Result<HashSet<String>> {
    let _mise_bin = mise_bin; // used on Windows only
    let mut shims = HashSet::new();
    for (t, tv) in toolset.list_installed_versions(config).await? {
        if !shim_scope_contains_install(scope, &tv.install_path()) {
            continue;
        }
        let bins = list_tool_bins(config, t.clone(), &tv)
            .await
            .unwrap_or_else(|e| {
                warn!("Error listing bin paths for {}: {:#}", tv, e);
                Vec::new()
            });
        shims.extend(
            bins.into_iter()
                .flat_map(|b| platform_shim_names(_mise_bin, &b)),
        );
    }
    for request in toolset.list_current_requests() {
        if !shim_scope_contains_request(scope, request) {
            continue;
        }
        match request.lazy_bins() {
            Ok(Some(bins)) => shims.extend(
                bins.into_iter()
                    .flat_map(|bin| platform_shim_names(_mise_bin, &bin)),
            ),
            Ok(None) => {}
            Err(err) if strict_lazy_bins => return Err(err),
            Err(err) => warn!("Skipping invalid lazy shim declaration: {err:#}"),
        }
    }
    Ok(shims)
}

fn platform_shim_names(_mise_bin: &Path, bin: &str) -> Vec<String> {
    if cfg!(windows) {
        #[cfg(windows)]
        let shim_mode = effective_shim_mode(_mise_bin);
        #[cfg(not(windows))]
        let shim_mode = String::new();
        let p = PathBuf::from(bin);
        match shim_mode.as_ref() {
            "hardlink" | "symlink" | "exe" => {
                vec![p.with_extension("exe").to_string_lossy().to_string()]
            }
            "file" => vec![
                p.with_extension("").to_string_lossy().to_string(),
                p.with_extension("cmd").to_string_lossy().to_string(),
            ],
            _ => panic!("Unknown shim mode"),
        }
    } else if cfg!(macos) {
        vec![bin.to_lowercase()]
    } else {
        vec![bin.to_string()]
    }
}

// lists all the paths to bins in a tv that shims will be needed for
async fn list_tool_bins(
    config: &Arc<Config>,
    t: Arc<dyn Backend>,
    tv: &ToolVersion,
) -> Result<Vec<String>> {
    Ok(t.list_bin_paths(config, tv)
        .await?
        .into_iter()
        .filter(|p| p.parent().is_some())
        .filter(|path| path.exists())
        .map(|dir| list_executables_in_dir(&dir))
        .collect::<Result<Vec<_>>>()?
        .into_iter()
        .flatten()
        .collect())
}

async fn make_shim(target: &Path, shim: &Path) -> Result<()> {
    file::remove_file_async_if_exists(shim).await?;
    file::write_async(
        shim,
        format!(
            "{GENERATED_SHELL_SHIM_HEADER}export ASDF_DATA_DIR={data_dir}\nexport PATH=\"{fake_asdf_dir}:$PATH\"\nmise x -- {target} \"$@\"\n",
        data_dir = dirs::DATA.display(),
        fake_asdf_dir = fake_asdf::setup()?.display(),
        target = target.display()
        ),
    )
    .await?;
    file::make_executable_async(shim).await?;
    trace!(
        "shim created from {} to {}",
        target.display(),
        shim.display()
    );
    Ok(())
}

async fn err_no_version_set(
    config: &Arc<Config>,
    ts: Toolset,
    bin_name: &str,
    tvs: Vec<ToolVersion>,
) -> Result<PathBuf> {
    if tvs.is_empty() {
        bail!(
            "{bin_name} is not a valid shim. This likely means you uninstalled a tool and the shim does not point to anything. Run `mise use <TOOL>` to reinstall the tool."
        );
    }
    let missing_plugins = tvs.iter().map(|tv| tv.ba()).collect::<HashSet<_>>();
    let mut missing_tools = ts
        .list_missing_versions(config)
        .await
        .into_iter()
        .filter(|t| missing_plugins.contains(t.ba()))
        .collect_vec();
    if missing_tools.is_empty() {
        if let Some(msg) = unavailable_configured_tool_message(config, &ts, bin_name) {
            return Err(eyre!(msg));
        }
        let mut msg = format!("No version is set for shim: {bin_name}\n");
        msg.push_str("Set a global default version with one of the following:\n");
        for tv in tvs {
            msg.push_str(&format!("mise use -g {}@{}\n", tv.ba(), tv.version));
        }
        Err(eyre!(msg.trim().to_string()))
    } else {
        let mut msg = format!(
            "Tool{} not installed for shim: {}\n",
            if missing_tools.len() > 1 { "s" } else { "" },
            bin_name
        );
        for t in missing_tools.drain(..) {
            msg.push_str(&format!("Missing tool version: {t}\n"));
        }
        msg.push_str("Install all missing tools with: mise install\n");
        Err(eyre!(msg.trim().to_string()))
    }
}

pub(crate) fn unavailable_configured_tool_message(
    config: &Arc<Config>,
    ts: &Toolset,
    bin_name: &str,
) -> Option<String> {
    let versions = ts
        .list_current_versions()
        .into_iter()
        .filter(|(backend, tv)| {
            tv.ba().matches_bin_name(bin_name) && backend.is_version_installed(config, tv, true)
        })
        .map(|(_, tv)| tv)
        .collect_vec();
    if versions.is_empty() {
        return None;
    }

    let mut msg = format!("No executable found for configured tool: {bin_name}\n");
    msg.push_str(
        "The installed version does not provide this executable with its current backend metadata.\n",
    );
    msg.push_str("Reinstall it with:\n");
    for tv in versions {
        msg.push_str(&format!(
            "mise install --force {}@{}\n",
            tv.ba(),
            tv.version
        ));
    }
    Some(msg.trim().to_string())
}

/// `mise install <tool>` deliberately writes to no config file, so the tool's
/// bin dir never joins the PATH `mise exec` builds and resolution fails with an
/// opaque error (`cannot find binary path` on Windows, `couldn't exec process`
/// on unix). When an installed-but-unconfigured tool would have supplied the
/// bin, name it and say how to activate it. See discussion #4407.
///
/// Returns `None` when a configured tool already matches the bin: that failure
/// has a different cause, and `err_no_version_set` /
/// `unavailable_configured_tool_message` already explain it.
///
/// Matching is by tool name, so a bin that shares no name with the tool
/// providing it (`npm` from `node`) is not recognized. Those callers fall back
/// to the existing message rather than getting a wrong one.
pub(crate) fn inactive_installed_tool_message(
    ts: &Toolset,
    installed_shorts: &[String],
    bin_name: &str,
) -> Option<String> {
    // Every tool declared in config is a key here, even one that failed version
    // resolution, is unsupported on this OS, or whose backend could not be
    // built. `list_current_versions()` drops all three, which would let a
    // configured tool be reported as "not in any config file".
    if ts.versions.keys().any(|ba| ba.matches_bin_name(bin_name)) {
        return None;
    }
    let shorts = installed_shorts
        .iter()
        .filter(|short| BackendArg::from(short.as_str()).matches_bin_name(bin_name))
        .collect_vec();
    if shorts.is_empty() {
        return None;
    }
    let mut msg =
        format!("{bin_name} is installed but not activated — it is not in any config file.\n");
    msg.push_str("To activate it, run:\n");
    for short in &shorts {
        msg.push_str(&format!("  mise use {short}\n"));
    }
    msg.push_str("To run it without changing any config file, run:\n");
    for short in &shorts {
        msg.push_str(&format!("  mise exec {short} -- {bin_name}\n"));
    }
    Some(msg.trim().to_string())
}

/// Name the registry tool that provides `bin_name` on other platforms but not this one.
///
/// `registry/<tool>.toml` carries an `os` list, and a tool whose list omits the running OS is
/// dropped by `ToolRequestSetBuilder::is_disabled` before any version is resolved. That drop is
/// silent by design — the tool is not unknown, and the user has not disabled it — so nothing is
/// installed and the bin is simply absent. `mise install` and `mise use` reach the backend and
/// report the reason; `mise exec` only ever saw the missing bin. mise has the answer in its own
/// registry, so say it rather than leaving the user with `cannot find binary path`.
///
/// Matched on `bins` and on the tool's own name, so `mise x aws-cli -- aws` is recognised through
/// either. Returns `None` when the OS list is empty or includes this one, which is the normal case.
pub(crate) fn os_unsupported_tool_message(bin_name: &str) -> Option<String> {
    let shorts = crate::registry::REGISTRY
        .values()
        .unique_by(|rt| rt.short)
        .filter(|rt| !rt.is_supported_os())
        .filter(|rt| rt.short == bin_name || rt.bins.contains(&bin_name))
        .map(|rt| (rt.short, rt.os.join(", ")))
        .collect_vec();
    if shorts.is_empty() {
        return None;
    }
    let mut msg = String::new();
    for (short, oses) in &shorts {
        let provides = if *short == bin_name {
            String::new()
        } else {
            format!(", which provides {bin_name},")
        };
        msg.push_str(&format!(
            "{short}{provides} is not available on {}: mise's registry lists it for {oses} only.\n",
            std::env::consts::OS,
        ));
    }
    Some(msg.trim().to_string())
}

/// Gather what [`inactive_installed_tool_message`] needs. Only called once a
/// binary has definitively failed to resolve, so the config/toolset load lands
/// on a path that is about to abort anyway.
#[cfg(not(test))]
pub(crate) async fn exec_resolution_hint(bin_name: &str) -> Option<String> {
    // Windows invokes binaries as `<tool>.exe`; name `<tool>` in the message.
    let bin_stem = bin_name
        .strip_suffix(std::env::consts::EXE_SUFFIX)
        .unwrap_or(bin_name);
    let config = Config::get().await.ok()?;
    let ts = ToolsetBuilder::new().build(&config).await.ok()?;
    // A disabled tool is skipped by `Toolset::add_version`, so it never reaches
    // the configured-tool check above. Suggesting `mise use` for one would be
    // wrong twice over: it may well be in a config file, and mise has been told
    // not to manage it.
    let settings = Settings::get();
    let enable_tools = settings.enable_tools();
    let disable_tools = settings.disable_tools();
    let installed_shorts = crate::toolset::install_state::list_tools()
        .values()
        .filter(|t| !t.versions.is_empty())
        .map(|t| t.short.clone())
        .filter(|short| crate::registry::tool_enabled(enable_tools.as_ref(), &disable_tools, short))
        .collect_vec();
    inactive_installed_tool_message(&ts, &installed_shorts, bin_stem)
        // Checked second because the two cannot both apply: a tool this OS is excluded from is
        // never installed here, so there is nothing to be "installed but not activated".
        .or_else(|| os_unsupported_tool_message(bin_stem))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::args::BackendArg;
    use crate::toolset::{ToolRequest, ToolSource, ToolVersionList};

    #[test]
    fn locked_windows_shims_get_distinct_old_paths() {
        assert_eq!(old_shim_path(Path::new("foo")), PathBuf::from("foo.old"));
        assert_eq!(
            old_shim_path(Path::new("foo.cmd")),
            PathBuf::from("foo.cmd.old")
        );
    }

    #[cfg(windows)]
    #[test]
    fn command_wrappers_remove_stale_windows_shim_variants() {
        let dir = tempfile::tempdir().unwrap();
        for shim in ["cargo", "cargo.cmd", "cargo.exe", "cargo.exe.old"] {
            fs::write(dir.path().join(shim), "shim").unwrap();
        }

        let desired = BTreeSet::from(["cargo".to_string(), "cargo.cmd".to_string()]);
        remove_stale_windows_shim_variants(dir.path(), &desired).unwrap();

        assert!(dir.path().join("cargo").exists());
        assert!(dir.path().join("cargo.cmd").exists());
        assert!(!dir.path().join("cargo.exe").exists());
        assert!(!dir.path().join("cargo.exe.old").exists());
    }

    #[cfg(macos)]
    #[test]
    fn case_colliding_macos_wrapper_names_are_rejected() {
        let names = ["Foo".to_string(), "foo".to_string()];
        assert!(validate_wrapper_names(&names).is_err());
    }

    #[cfg(windows)]
    #[test]
    fn dotted_windows_wrapper_names_are_rejected() {
        let names = ["foo.bar".to_string()];
        assert!(validate_wrapper_names(&names).is_err());
    }

    #[cfg(windows)]
    #[test]
    fn windows_shim_command_names_drop_exe_suffix_case_insensitively() {
        assert_eq!(command_name_without_exe_suffix("dummy.exe"), "dummy");
        assert_eq!(command_name_without_exe_suffix("DUMMY.EXE"), "DUMMY");
        assert_eq!(
            command_name_without_exe_suffix("python3.12.exe"),
            "python3.12"
        );
        assert_eq!(command_name_without_exe_suffix("dummy.cmd"), "dummy.cmd");
    }

    #[test]
    fn snap_mise_bin_uses_refresh_stable_current_path() {
        assert_eq!(
            snap_mise_bin(
                Path::new("/snap/mise/189/bin/mise"),
                Path::new("/snap/mise/189")
            ),
            Some(PathBuf::from("/snap/mise/current/bin/mise"))
        );
    }

    #[test]
    fn snap_mise_bin_rejects_unrelated_executable() {
        assert_eq!(
            snap_mise_bin(
                Path::new("/home/user/.local/bin/mise"),
                Path::new("/snap/mise/189")
            ),
            None
        );
    }

    #[cfg(unix)]
    #[test]
    fn snap_mise_bin_handles_symlinked_snap_mount() {
        let temp = tempfile::tempdir().unwrap();
        let canonical_mount = temp.path().join("var/lib/snapd/snap");
        let canonical_snap = canonical_mount.join("mise/189");
        let mise_bin = canonical_snap.join("bin/mise");
        fs::create_dir_all(mise_bin.parent().unwrap()).unwrap();
        fs::write(&mise_bin, "").unwrap();

        let snap_mount = temp.path().join("snap");
        std::os::unix::fs::symlink(&canonical_mount, &snap_mount).unwrap();
        let snap = snap_mount.join("mise/189");

        assert_eq!(
            snap_mise_bin(&mise_bin, &snap),
            Some(snap_mount.join("mise/current/bin/mise"))
        );
    }

    #[tokio::test]
    async fn unavailable_tool_message_prefers_matching_configured_tool() {
        let config = Config::get().await.unwrap();
        let temp = tempfile::tempdir().unwrap();
        let mut ts = Toolset::new(ToolSource::Argument);

        for name in ["codex", "node"] {
            let ba = Arc::new(BackendArg::from(name));
            let request = ToolRequest::new(ba.clone(), "1.0.0", ToolSource::Argument).unwrap();
            let mut tv = ToolVersion::new(request.clone(), "1.0.0".into());
            let install_path = temp.path().join(name);
            file::create_dir_all(&install_path).unwrap();
            tv.install_path = Some(install_path);

            let mut tvl = ToolVersionList::new(ba.clone(), ToolSource::Argument);
            tvl.requests.push(request);
            tvl.versions.push(tv);
            ts.versions.insert(ba, tvl);
        }

        let msg = unavailable_configured_tool_message(&config, &ts, "codex").unwrap();
        assert!(msg.contains("mise install --force codex@1.0.0"));
        assert!(!msg.contains("node@1.0.0"));
    }

    #[tokio::test]
    async fn inactive_tool_message_names_the_installed_tool() {
        let _config = Config::get().await.unwrap();
        let ts = Toolset::new(ToolSource::Argument);

        let msg = inactive_installed_tool_message(&ts, &["gh".to_string()], "gh").unwrap();
        assert!(msg.contains("installed but not activated"));
        assert!(msg.contains("mise use gh"));
        assert!(msg.contains("mise exec gh -- gh"));
    }

    #[tokio::test]
    async fn inactive_tool_message_is_none_for_configured_tool() {
        let _config = Config::get().await.unwrap();
        let mut ts = Toolset::new(ToolSource::Argument);
        let ba = Arc::new(BackendArg::from("gh"));
        let request = ToolRequest::new(ba.clone(), "1.0.0", ToolSource::Argument).unwrap();
        let tv = ToolVersion::new(request.clone(), "1.0.0".into());
        let mut tvl = ToolVersionList::new(ba.clone(), ToolSource::Argument);
        tvl.requests.push(request);
        tvl.versions.push(tv);
        ts.versions.insert(ba, tvl);

        // A configured tool that still fails to resolve has a different cause;
        // err_no_version_set/unavailable_configured_tool_message own that case.
        assert!(inactive_installed_tool_message(&ts, &["gh".to_string()], "gh").is_none());
    }

    /// A tool can be declared in config and still be absent from
    /// `list_current_versions()` -- version resolution failed, the OS is not
    /// supported, or its backend could not be built. It must not then be
    /// reported as "not in any config file".
    #[tokio::test]
    async fn inactive_tool_message_is_none_for_a_configured_tool_with_no_resolved_versions() {
        let _config = Config::get().await.unwrap();
        let mut ts = Toolset::new(ToolSource::Argument);
        let ba = Arc::new(BackendArg::from("gh"));
        ts.versions.insert(
            ba.clone(),
            ToolVersionList::new(ba, ToolSource::Argument), // no versions resolved
        );

        assert!(inactive_installed_tool_message(&ts, &["gh".to_string()], "gh").is_none());
    }

    #[tokio::test]
    async fn inactive_tool_message_is_none_when_bin_does_not_name_the_tool() {
        let _config = Config::get().await.unwrap();
        let ts = Toolset::new(ToolSource::Argument);

        // Matching is by tool name, so a bin like npm (provided by node) is not
        // recognized and the caller keeps its existing message.
        assert!(inactive_installed_tool_message(&ts, &["node".to_string()], "npm").is_none());
    }

    #[test]
    fn os_unsupported_tool_message_names_the_tool_and_this_platform() {
        // Taken from the registry rather than hardcoded: which tools are excluded depends on the
        // platform the test runs on, and a hardcoded name would pass for the wrong reason
        // wherever it happens to be supported. Every platform has some — the registry carries
        // `["macos"]`, `["linux"]` and `["windows"]` entries.
        let rt = crate::registry::REGISTRY
            .values()
            .unique_by(|rt| rt.short)
            .find(|rt| !rt.is_supported_os() && !rt.bins.is_empty())
            .expect("the registry lists no tool this platform is excluded from");

        let msg = os_unsupported_tool_message(rt.bins[0])
            .expect("a bin only an excluded tool provides should be explained");
        assert!(msg.contains(rt.short), "{msg}");
        assert!(msg.contains(std::env::consts::OS), "{msg}");
        // and it must say where the tool *is* available, or the user learns nothing actionable
        assert!(msg.contains(rt.os[0]), "{msg}");
    }

    #[test]
    fn os_unsupported_tool_message_is_silent_when_nothing_is_excluded() {
        // The control. Without it a message that fired for every name would satisfy the test
        // above. `node` carries no `os` list, so it is supported everywhere.
        assert_eq!(os_unsupported_tool_message("node"), None);
        assert_eq!(os_unsupported_tool_message("not-a-registry-bin-9f3a"), None);
    }

    // `e2e-win/exec_os_unsupported_tool.Tests.ps1` observes this message by running
    // `mise x docker-slim -- mint` on a Windows runner, and it can only observe it while
    // `docker-slim` is still restricted away from Windows and still provides a bin under another
    // name. A registry edit that takes either away leaves that file green with nothing to assert,
    // and a full Windows e2e run to notice. The same strings are pinned here so `windows-unit`
    // fails first, saying which one went.
    //
    // Windows-only because everywhere else `docker-slim` is supported and `None` is the right
    // answer, so the assertions could not run at all.
    #[cfg(windows)]
    #[test]
    fn os_unsupported_tool_message_still_backs_the_windows_e2e_fixture() {
        let msg = os_unsupported_tool_message("mint")
            .expect("docker-slim provides mint and its os list omits windows");
        for expected in [
            "docker-slim",
            "not available on windows",
            "mint",
            "linux",
            "macos",
        ] {
            assert!(msg.contains(expected), "{expected:?} missing from {msg:?}");
        }
    }

    #[test]
    fn windows_file_shim_body_recovers_the_arguments_cmd_destroys() {
        let body = windows_file_shim_body("gh");
        let lines: Vec<&str> = body.lines().collect();
        let enable = lines
            .iter()
            .position(|l| l.contains("EnableDelayedExpansion"))
            .unwrap();

        // `%~f0` is captured before delayed expansion is on, or a `!` in the path would be eaten.
        let capture = lines.iter().position(|l| l.contains("%~f0")).unwrap();
        assert!(capture < enable, "{body}");

        // `!CMDCMDLINE!`, not `%CMDCMDLINE%`: the percent form is substituted before special
        // characters are parsed and truncates the line at the first `&`.
        assert!(
            body.contains(r#"set "__MISE_RAW_CMDLINE=!CMDCMDLINE!""#),
            "{body}"
        );
        assert!(!body.contains("%CMDCMDLINE%"), "{body}");

        // The recursion guard is unchanged, and still decides before anything else runs.
        let guard = lines
            .iter()
            .position(|l| l.contains("%__MISE_SHIM_PATH%"))
            .unwrap();
        assert!(guard < enable, "{body}");
        assert!(
            body.contains("recursive shim invocation detected for gh"),
            "{body}"
        );
        assert!(body.contains("exit /b 1"), "{body}");

        // Both arms hand mise the same command; the sentinel marks where `%*` begins, so a run
        // that cannot recover the raw line still gets what cmd managed to deliver.
        let run = format!("mise x -- gh {} %*", env::LAUNCHER_ARGS_SENTINEL);
        assert_eq!(lines.iter().filter(|l| **l == run).count(), 2, "{body}");

        // The recovering arm exits rather than `exit /b`, so cmd does not go on to run whatever it
        // queued from the same line -- given `gh c&d`, that is `d`.
        assert!(body.contains("goto mise_shim_fallback"), "{body}");
        assert!(body.contains("exit !ERRORLEVEL!"), "{body}");
    }

    #[test]
    fn windows_file_shim_body_clears_the_launcher_variables_when_it_declines() {
        let body = windows_file_shim_body("gh");
        let after: Vec<&str> = body
            .lines()
            .skip_while(|l| *l != ":mise_shim_fallback")
            .collect();
        assert!(!after.is_empty(), "{body}");
        // Or a value inherited from an outer launcher would be recovered as this shim's arguments.
        assert!(after.contains(&r#"set "__MISE_RAW_CMDLINE=""#), "{body}");
        assert!(after.contains(&r#"set "__MISE_LAUNCHER=""#), "{body}");
    }

    #[test]
    fn windows_file_shim_body_is_crlf_terminated() {
        // A label reached by `goto` is the classic thing a lone `\n` breaks in a batch file.
        let body = windows_file_shim_body("gh");
        assert!(body.ends_with("\r\n"));
        assert_eq!(body.matches('\n').count(), body.matches("\r\n").count());
    }

    #[test]
    fn windows_file_shim_detection_uses_stable_markers_and_legacy_body() {
        let dir = tempfile::tempdir().unwrap();
        let shim = dir.path().join("gh.cmd");
        fs::write(&shim, windows_file_shim_body("gh")).unwrap();
        assert!(is_generated_windows_file_shim(&shim));

        fs::write(
            &shim,
            format!("{GENERATED_WINDOWS_CMD_SHIM_HEADER}echo future body\r\n"),
        )
        .unwrap();
        assert!(is_generated_windows_file_shim(&shim));

        fs::write(&shim, "@echo off\r\nsetlocal\r\nmise x -- %*\r\n").unwrap();
        assert!(is_generated_windows_file_shim(&shim));

        fs::write(&shim, "@echo off\r\necho user script\r\n").unwrap();
        assert!(!is_generated_windows_file_shim(&shim));

        let bash_shim = dir.path().join("gh");
        fs::write(
            &bash_shim,
            format!("{GENERATED_WINDOWS_BASH_SHIM_HEADER}echo future body\n"),
        )
        .unwrap();
        assert!(is_generated_windows_file_shim(&bash_shim));
    }

    #[cfg(windows)]
    #[test]
    fn bash_shim_script_includes_wsl_guard() {
        let script = bash_shim_script("gh");
        assert!(script.starts_with("#!/bin/bash"));
        // WSL detection
        assert!(script.contains("WSL_DISTRO_NAME"));
        assert!(script.contains("WSL_INTEROP"));
        assert!(script.contains("/proc/sys/fs/binfmt_misc/WSLInterop"));
        assert!(script.contains(r#"shim_dir=$(cd -- "$(dirname -- "$0")" && pwd -P)"#));
        // globbing disabled while splitting PATH so wildcard entries are not expanded
        assert!(script.contains("set -f"));
        // The shim identifies its actual location even if MISE_DATA_DIR is absent.
        assert!(script.contains(r#"shim_path="$shim_dir/${0##*/}""#));
        assert!(script.contains(r#"export __MISE_SHIM_PATH="$shim_path""#));
        assert!(script.contains("recursive shim invocation detected"));
        // In WSL: drop the shim dir and run the native tool directly.
        assert!(script.contains(r#"exec gh "$@""#));
        // Outside WSL: defer to mise as before.
        assert!(script.contains(r#"exec mise x -- gh "$@""#));
    }

    #[test]
    fn list_executables_in_dir_skips_dotfiles() {
        let dir = tempfile::tempdir().unwrap();
        let visible_name = if cfg!(windows) {
            "ffmpeg.exe"
        } else {
            "ffmpeg"
        };
        let visible = dir.path().join(visible_name);
        let hidden = dir.path().join(".librsvg-post-link.exe");

        fs::write(&visible, "").unwrap();
        fs::write(&hidden, "").unwrap();
        file::make_executable(&visible).unwrap();
        file::make_executable(&hidden).unwrap();

        let bins = list_executables_in_dir(dir.path()).unwrap();

        assert!(bins.contains(visible_name));
        assert!(!bins.contains(".librsvg-post-link.exe"));
    }

    #[test]
    fn staged_shim_publication_preserves_unmanaged_and_modified_files() {
        let live = tempfile::tempdir().unwrap();
        let mise_bin = live.path().join("mise");
        fs::write(&mise_bin, "mise").unwrap();
        let unmanaged = live.path().join("unmanaged");
        fs::write(&unmanaged, "from another package manager").unwrap();
        file::make_executable(&unmanaged).unwrap();

        let staging = tempfile::Builder::new()
            .prefix(".mise-shims-stage-")
            .tempdir_in(live.path())
            .unwrap();
        fs::write(staging.path().join("unmanaged"), "mise shim").unwrap();
        fs::write(
            staging.path().join("owned"),
            "#!/bin/sh\n# mise generated shim\nmise x -- owned \"$@\"\n",
        )
        .unwrap();
        publish_staged_shim_farm(
            live.path(),
            &mise_bin,
            staging,
            HashSet::new(),
            HashSet::new(),
            HashSet::new(),
            false,
        )
        .unwrap();

        assert_eq!(
            fs::read_to_string(&unmanaged).unwrap(),
            "from another package manager"
        );
        assert_eq!(
            fs::read_to_string(live.path().join("owned")).unwrap(),
            "#!/bin/sh\n# mise generated shim\nmise x -- owned \"$@\"\n"
        );

        // Once an owned shim is changed outside mise, an otherwise empty
        // reshim cedes ownership instead of deleting the replacement.
        fs::write(live.path().join("owned"), "user replacement").unwrap();
        let empty_staging = tempfile::Builder::new()
            .prefix(".mise-shims-stage-")
            .tempdir_in(live.path())
            .unwrap();
        publish_staged_shim_farm(
            live.path(),
            &mise_bin,
            empty_staging,
            HashSet::new(),
            HashSet::new(),
            HashSet::new(),
            false,
        )
        .unwrap();

        assert_eq!(
            fs::read_to_string(live.path().join("owned")).unwrap(),
            "user replacement"
        );
    }

    #[test]
    fn staged_shim_publication_removes_unchanged_owned_shims() {
        let live = tempfile::tempdir().unwrap();
        let mise_bin = live.path().join("mise");
        fs::write(&mise_bin, "mise").unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".mise-shims-stage-")
            .tempdir_in(live.path())
            .unwrap();
        fs::write(
            staging.path().join("owned"),
            "#!/bin/sh\n# mise generated shim\nmise x -- owned \"$@\"\n",
        )
        .unwrap();
        publish_staged_shim_farm(
            live.path(),
            &mise_bin,
            staging,
            HashSet::new(),
            HashSet::new(),
            HashSet::new(),
            false,
        )
        .unwrap();

        let empty_staging = tempfile::Builder::new()
            .prefix(".mise-shims-stage-")
            .tempdir_in(live.path())
            .unwrap();
        publish_staged_shim_farm(
            live.path(),
            &mise_bin,
            empty_staging,
            HashSet::new(),
            HashSet::from(["owned".to_string()]),
            HashSet::from(["owned".to_string()]),
            false,
        )
        .unwrap();

        assert!(!live.path().join("owned").exists());
    }

    #[test]
    fn staged_shim_publication_prunes_unmanaged_files_in_dedicated_farm() {
        let live = tempfile::tempdir().unwrap();
        let mise_bin = live.path().join("mise");
        fs::write(&mise_bin, "mise").unwrap();
        let orphan = live.path().join("orphan");
        fs::write(&orphan, "not a mise shim").unwrap();
        file::make_executable(&orphan).unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".mise-shims-stage-")
            .tempdir_in(live.path())
            .unwrap();
        let collision = live.path().join("collision");
        fs::write(&collision, "unmanaged old file").unwrap();
        fs::write(staging.path().join("collision"), "replacement shim").unwrap();

        publish_staged_shim_farm(
            live.path(),
            &mise_bin,
            staging,
            HashSet::new(),
            HashSet::new(),
            HashSet::new(),
            true,
        )
        .unwrap();

        assert!(!orphan.exists());
        assert_eq!(fs::read_to_string(collision).unwrap(), "replacement shim");
    }

    #[test]
    fn staged_shim_publication_updates_metadata_in_shared_directory() {
        let live = tempfile::tempdir().unwrap();
        let mise_bin = live.path().join("mise");
        fs::write(&mise_bin, "mise").unwrap();
        fs::write(live.path().join(".version"), "old").unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".mise-shims-stage-")
            .tempdir_in(live.path())
            .unwrap();
        fs::write(staging.path().join(".version"), "new").unwrap();

        publish_staged_shim_farm(
            live.path(),
            &mise_bin,
            staging,
            HashSet::new(),
            HashSet::new(),
            HashSet::new(),
            false,
        )
        .unwrap();

        assert_eq!(
            fs::read_to_string(live.path().join(".version")).unwrap(),
            "new"
        );
    }

    #[test]
    fn shared_collision_is_not_missing_or_extra() {
        let actual = ActualShims {
            current: HashSet::new(),
            dedicated_present: HashSet::new(),
            owned: HashSet::new(),
            occupied: HashSet::from(["black".to_string()]),
            repairable: HashSet::new(),
        };
        let desired = HashSet::from(["black".to_string()]);

        let (missing, extra) = calculate_shim_diffs(&actual, &desired, false);

        assert!(missing.is_empty());
        assert!(extra.is_empty());
    }

    #[test]
    fn dedicated_diff_reports_the_same_entries_incremental_pruning_removes() {
        let actual = ActualShims {
            current: HashSet::from(["node".to_string()]),
            dedicated_present: HashSet::from(["node".to_string(), "orphan".to_string()]),
            owned: HashSet::from(["node".to_string()]),
            occupied: HashSet::from([
                "node".to_string(),
                "orphan".to_string(),
                "foreign-symlink".to_string(),
            ]),
            repairable: HashSet::new(),
        };
        let desired = HashSet::from(["node".to_string()]);

        let (missing, extra) = calculate_shim_diffs(&actual, &desired, true);

        assert!(missing.is_empty());
        assert_eq!(extra, BTreeSet::from(["orphan".to_string()]));
    }

    #[test]
    fn dangling_desired_mise_symlink_remains_missing_in_shared_directory() {
        let actual = ActualShims {
            current: HashSet::new(),
            dedicated_present: HashSet::new(),
            owned: HashSet::new(),
            occupied: HashSet::from(["node".to_string()]),
            repairable: HashSet::from(["node".to_string()]),
        };
        let desired = HashSet::from(["node".to_string()]);

        let (missing, extra) = calculate_shim_diffs(&actual, &desired, false);

        assert_eq!(missing, BTreeSet::from(["node".to_string()]));
        assert!(extra.is_empty());
    }

    #[test]
    fn stale_owned_shim_is_missing_in_shared_directory() {
        let actual = ActualShims {
            current: HashSet::new(),
            dedicated_present: HashSet::new(),
            owned: HashSet::from(["node".to_string()]),
            occupied: HashSet::from(["node".to_string()]),
            repairable: HashSet::new(),
        };
        let desired = HashSet::from(["node".to_string()]);

        let (missing, extra) = calculate_shim_diffs(&actual, &desired, false);

        assert_eq!(missing, BTreeSet::from(["node".to_string()]));
        assert!(extra.is_empty());
    }

    #[cfg(unix)]
    #[test]
    fn symlinked_dedicated_farm_is_treated_as_shared() {
        let root = tempfile::tempdir().unwrap();
        let shared = root.path().join("shared");
        let dedicated = root.path().join("shims");
        fs::create_dir(&shared).unwrap();
        std::os::unix::fs::symlink(&shared, &dedicated).unwrap();

        assert!(!matches_unredirected_dedicated_dir(&dedicated, &dedicated));
        assert!(matches_unredirected_dedicated_dir(&shared, &shared));
    }

    #[test]
    fn staged_shim_publication_preserves_unstaged_desired_shims() {
        let live = tempfile::tempdir().unwrap();
        let mise_bin = live.path().join("mise");
        fs::write(&mise_bin, "mise").unwrap();
        let existing = live.path().join("existing");
        fs::write(
            &existing,
            "#!/bin/sh\n# mise generated shim\nmise x -- existing \"$@\"\n",
        )
        .unwrap();
        file::make_executable(&existing).unwrap();
        let staging = tempfile::Builder::new()
            .prefix(".mise-shims-stage-")
            .tempdir_in(live.path())
            .unwrap();

        publish_staged_shim_farm(
            live.path(),
            &mise_bin,
            staging,
            HashSet::from(["existing".to_string()]),
            HashSet::from(["existing".to_string()]),
            HashSet::new(),
            false,
        )
        .unwrap();

        assert!(existing.exists());
    }

    #[cfg(unix)]
    #[test]
    fn mise_shim_detection_distinguishes_symlink_targets() {
        let dir = tempfile::tempdir().unwrap();
        let mise_bin = dir.path().join("mise");
        let other_bin = dir.path().join("other");
        fs::write(&mise_bin, "mise").unwrap();
        fs::write(&other_bin, "other").unwrap();
        let shim = dir.path().join("shim");

        std::os::unix::fs::symlink(&mise_bin, &shim).unwrap();
        assert!(is_mise_shim(&shim, &mise_bin).unwrap());

        fs::remove_file(&shim).unwrap();
        std::os::unix::fs::symlink(&other_bin, &shim).unwrap();
        assert!(!is_mise_shim(&shim, &mise_bin).unwrap());

        // A dangling target name alone is not proof of ownership: an unrelated
        // symlink in a shared directory may also point at a file named `mise`.
        // Publication uses this weaker signal only for a desired collision.
        fs::remove_file(&shim).unwrap();
        std::os::unix::fs::symlink(dir.path().join("old/bin/mise"), &shim).unwrap();
        assert!(!is_mise_shim(&shim, &mise_bin).unwrap());
        assert!(symlink_target_names_mise(&shim).unwrap());
        assert!(!is_current_owned_mise_shim(&shim, &mise_bin).unwrap());

        fs::remove_file(&shim).unwrap();
        std::os::unix::fs::symlink(dir.path().join("scripts/mise-wrapper.sh"), &shim).unwrap();
        assert!(!is_mise_shim(&shim, &mise_bin).unwrap());
        assert!(!symlink_target_names_mise(&shim).unwrap());

        // The real mise dispatcher may itself be installed as a symlink in a
        // shared bin directory; its own name keeps it out of shim pruning.
        let dispatcher = dir.path().join("mise");
        fs::remove_file(&dispatcher).unwrap();
        std::os::unix::fs::symlink(&other_bin, &dispatcher).unwrap();
        assert!(!is_mise_shim(&dispatcher, &mise_bin).unwrap());
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn current_shim_check_migrates_to_stable_launcher_path() {
        let dir = tempfile::tempdir().unwrap();
        let versioned = dir.path().join("Cellar/mise/1/bin/mise");
        fs::create_dir_all(versioned.parent().unwrap()).unwrap();
        fs::write(&versioned, "mise").unwrap();
        let launcher = dir.path().join("bin/mise");
        fs::create_dir_all(launcher.parent().unwrap()).unwrap();
        std::os::unix::fs::symlink(&versioned, &launcher).unwrap();
        let shim = dir.path().join("shims/node");
        fs::create_dir_all(shim.parent().unwrap()).unwrap();
        std::os::unix::fs::symlink(&versioned, &shim).unwrap();

        assert!(is_mise_shim(&shim, &launcher).unwrap());
        assert!(!is_current_owned_mise_shim(&shim, &launcher).unwrap());

        let actual = get_actual_shims(&launcher, shim.parent().unwrap())
            .await
            .unwrap();
        let desired = HashSet::from(["node".to_string()]);
        let (missing, extra) = calculate_shim_diffs(&actual, &desired, false);
        assert_eq!(missing, BTreeSet::from(["node".to_string()]));
        assert!(extra.is_empty());
    }

    #[test]
    fn mise_shim_detection_recognizes_current_and_legacy_plugin_scripts() {
        let dir = tempfile::tempdir().unwrap();
        let mise_bin = dir.path().join("mise");
        fs::write(&mise_bin, "mise").unwrap();
        let shim = dir.path().join("shim");

        fs::write(
            &shim,
            format!("{GENERATED_SHELL_SHIM_HEADER}mise x -- foo \"$@\"\n"),
        )
        .unwrap();
        assert!(is_mise_shim(&shim, &mise_bin).unwrap());

        fs::write(
            &shim,
            "#!/bin/sh\nexport ASDF_DATA_DIR=/tmp/mise\necho user-wrapper\nmise x -- foo \"$@\"\n",
        )
        .unwrap();
        assert!(!is_mise_shim(&shim, &mise_bin).unwrap());

        fs::write(
            &shim,
            "#!/bin/sh\nexport ASDF_DATA_DIR=/tmp/mise\nexport PATH=\"x:$PATH\"\nmise x -- foo \"$@\"\n",
        )
        .unwrap();
        assert!(is_mise_shim(&shim, &mise_bin).unwrap());

        fs::write(&shim, "#!/bin/sh\necho user-script\n").unwrap();
        assert!(!is_mise_shim(&shim, &mise_bin).unwrap());
    }

    #[test]
    fn native_shim_fingerprint_recognizes_current_and_transition_binaries() {
        assert!(has_mise_native_shim_fingerprint(
            b"PE\0mise generated native shim v1\n\0"
        ));
        assert!(has_mise_native_shim_fingerprint(
            b"mise-shim: failed to determine executable path\0mise-shim: failed to execute mise"
        ));
        assert!(has_mise_native_shim_fingerprint(
            b"__MISE_SHIM_PATH\0recursive shim invocation detected\0mise x --"
        ));
        assert!(!has_mise_native_shim_fingerprint(
            b"an unrelated executable mentioning mise x --"
        ));
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn list_executables_in_dir_skips_non_utf8_names() {
        use std::ffi::OsString;
        use std::os::unix::ffi::OsStringExt;

        let dir = tempfile::tempdir().unwrap();
        let non_utf8 = dir.path().join(OsString::from_vec(vec![0xff]));

        fs::write(&non_utf8, "").unwrap();
        file::make_executable(&non_utf8).unwrap();

        let bins = list_executables_in_dir(dir.path()).unwrap();

        assert!(bins.is_empty());
    }

    #[test]
    fn shim_version_stale_detects_version_changes() {
        // exe/hardlink copies embed the binary: a version change makes them stale
        assert!(shim_version_stale(Some("2026.5.13"), "2026.5.16", "exe"));
        assert!(shim_version_stale(
            Some("2026.5.13"),
            "2026.5.16",
            "hardlink"
        ));
        // file mode writes a versioned bash script (e.g. the WSL guard, #10299),
        // so a version change must rebuild it too
        assert!(shim_version_stale(Some("2026.5.13"), "2026.5.16", "file"));
        // matching version is not stale
        assert!(!shim_version_stale(Some("2026.5.16"), "2026.5.16", "exe"));
        assert!(!shim_version_stale(Some("2026.5.16"), "2026.5.16", "file"));
        // surrounding whitespace in the marker is ignored
        assert!(!shim_version_stale(Some("2026.5.16\n"), "2026.5.16", "exe"));
        // no marker yet: heal once (covers installs created before this marker)
        assert!(shim_version_stale(None, "2026.5.16", "exe"));
        assert!(shim_version_stale(None, "2026.5.16", "file"));
        // symlink shims only point at the mise binary, so never version-stale
        assert!(!shim_version_stale(
            Some("2026.5.13"),
            "2026.5.16",
            "symlink"
        ));
    }

    /// Create a file symlink, or return `false` when the platform refuses
    /// (e.g. Windows without the symlink privilege) so the caller can skip
    /// itself. Any other failure panics: swallowing it would silently turn the
    /// new coverage into a no-op.
    fn try_symlink_file(target: &Path, link: &Path) -> bool {
        let result = {
            #[cfg(unix)]
            {
                std::os::unix::fs::symlink(target, link)
            }
            #[cfg(windows)]
            {
                std::os::windows::fs::symlink_file(target, link)
            }
        };
        match result {
            Ok(()) => true,
            Err(err)
                if matches!(
                    err.kind(),
                    std::io::ErrorKind::PermissionDenied | std::io::ErrorKind::Unsupported
                ) =>
            {
                false
            }
            Err(err) => panic!("failed to create file symlink: {err}"),
        }
    }

    /// A single-link mise layout: the PATH-visible `mise.exe` is a link, and
    /// `mise-shim.exe` ships only beside the real binary. Returns the linked
    /// mise and the real shim, or `None` when symlinks cannot be created on
    /// this host.
    fn single_link_layout(temp: &Path) -> Option<(PathBuf, PathBuf)> {
        let real_dir = temp.join("real").join("bin");
        let links_dir = temp.join("links");
        fs::create_dir_all(&real_dir).unwrap();
        fs::create_dir_all(&links_dir).unwrap();
        let real_mise = real_dir.join("mise.exe");
        fs::write(&real_mise, "mise").unwrap();
        let real_shim = real_dir.join("mise-shim.exe");
        fs::write(&real_shim, "mise-shim").unwrap();
        let linked_mise = links_dir.join("mise.exe");
        if !try_symlink_file(&real_mise, &linked_mise) {
            return None;
        }
        Some((linked_mise, real_shim))
    }

    #[test]
    fn find_mise_shim_bin_finds_the_shim_beside_the_binary() {
        let temp = tempfile::tempdir().unwrap();
        let bin = temp.path().join("bin");
        fs::create_dir_all(&bin).unwrap();
        fs::write(bin.join("mise.exe"), "mise").unwrap();
        let shim = bin.join("mise-shim.exe");
        fs::write(&shim, "mise-shim").unwrap();

        let found = find_mise_shim_bin(&bin.join("mise.exe")).expect("shim beside the binary");
        assert_eq!(
            dunce::canonicalize(&found).unwrap(),
            dunce::canonicalize(&shim).unwrap()
        );
    }

    #[test]
    fn find_mise_shim_bin_follows_a_symlinked_mise_bin() {
        let temp = tempfile::tempdir().unwrap();
        let Some((linked_mise, real_shim)) = single_link_layout(temp.path()) else {
            return;
        };

        // Regression: the old lookup checked only beside the link and on PATH.
        let found = find_mise_shim_bin(&linked_mise).expect("shim beside the real binary");
        assert_eq!(
            dunce::canonicalize(&found).unwrap(),
            dunce::canonicalize(&real_shim).unwrap()
        );
    }

    #[test]
    fn find_mise_shim_bin_follows_chained_symlinks() {
        let temp = tempfile::tempdir().unwrap();
        let Some((linked_mise, real_shim)) = single_link_layout(temp.path()) else {
            return;
        };
        let redirect_dir = temp.path().join("redirect");
        fs::create_dir_all(&redirect_dir).unwrap();
        let redirected = redirect_dir.join("mise.exe");
        if !try_symlink_file(&linked_mise, &redirected) {
            return;
        }

        let found = find_mise_shim_bin(&redirected).expect("shim through two links");
        assert_eq!(
            dunce::canonicalize(&found).unwrap(),
            dunce::canonicalize(&real_shim).unwrap()
        );
    }

    #[test]
    fn find_mise_shim_bin_prefers_the_shim_beside_the_real_binary() {
        let temp = tempfile::tempdir().unwrap();
        let Some((linked_mise, real_shim)) = single_link_layout(temp.path()) else {
            return;
        };
        let beside_link = temp.path().join("links").join("mise-shim.exe");
        fs::write(&beside_link, "another shim").unwrap();

        let found = find_mise_shim_bin(&linked_mise).expect("a shim beside the real binary");
        assert_eq!(
            dunce::canonicalize(&found).unwrap(),
            dunce::canonicalize(&real_shim).unwrap()
        );
        assert_ne!(
            dunce::canonicalize(&found).unwrap(),
            dunce::canonicalize(&beside_link).unwrap()
        );
    }

    #[test]
    fn find_mise_shim_bin_returns_none_when_no_shim_exists() {
        let temp = tempfile::tempdir().unwrap();
        let bin = temp.path().join("bin");
        fs::create_dir_all(&bin).unwrap();
        let mise = bin.join("mise.exe");
        fs::write(&mise, "mise").unwrap();

        // A mise-shim.exe on PATH happens in dev environments that build it, so
        // the assertion is scoped to what this test controls. `file::which` on
        // Windows matches the extension only, so filter to existing files the
        // same way the resolver does before deciding to skip.
        let found = find_mise_shim_bin(&mise);
        if file::which("mise-shim.exe")
            .filter(|p| p.is_file())
            .is_none()
        {
            assert_eq!(found, None);
        }
    }
}