fallow-graph 2.31.0

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

use std::path::{Path, PathBuf};

use rustc_hash::FxHashMap;

use fallow_types::discover::FileId;

use super::types::{OUTPUT_DIRS, ResolveContext, ResolveResult, SOURCE_EXTS};

/// Try resolving a specifier using plugin-provided path aliases.
///
/// Substitutes a matching alias prefix (e.g., `~/`) with a directory relative to the
/// project root (e.g., `app/`) and resolves the resulting path. This handles framework
/// aliases like Nuxt's `~/`, `~~/`, `#shared/` that aren't defined in tsconfig.json
/// but map to real filesystem paths.
pub(super) fn try_path_alias_fallback(
    ctx: &ResolveContext<'_>,
    specifier: &str,
) -> Option<ResolveResult> {
    for (prefix, replacement) in ctx.path_aliases {
        if !specifier.starts_with(prefix.as_str()) {
            continue;
        }

        let remainder = &specifier[prefix.len()..];
        // Build the substituted path relative to root.
        // If replacement is empty, remainder is relative to root directly.
        let substituted = if replacement.is_empty() {
            format!("./{remainder}")
        } else {
            format!("./{replacement}/{remainder}")
        };

        // Resolve from a synthetic file at the project root so relative paths work.
        // Use a dummy file path in the root directory.
        let root_file = ctx.root.join("__resolve_root__");
        if let Ok(resolved) = ctx.resolver.resolve_file(&root_file, &substituted) {
            let resolved_path = resolved.path();
            // Try raw path lookup first
            if let Some(&file_id) = ctx.raw_path_to_id.get(resolved_path) {
                return Some(ResolveResult::InternalModule(file_id));
            }
            // Fall back to canonical path lookup
            if let Ok(canonical) = dunce::canonicalize(resolved_path) {
                if let Some(&file_id) = ctx.path_to_id.get(canonical.as_path()) {
                    return Some(ResolveResult::InternalModule(file_id));
                }
                if let Some(file_id) = try_source_fallback(&canonical, ctx.path_to_id) {
                    return Some(ResolveResult::InternalModule(file_id));
                }
                if let Some(file_id) =
                    try_pnpm_workspace_fallback(&canonical, ctx.path_to_id, ctx.workspace_roots)
                {
                    return Some(ResolveResult::InternalModule(file_id));
                }
                if let Some(pkg_name) = extract_package_name_from_node_modules_path(&canonical) {
                    return Some(ResolveResult::NpmPackage(pkg_name));
                }
                return Some(ResolveResult::ExternalFile(canonical));
            }
        }
    }
    None
}

/// Try SCSS partial resolution: `_filename` and `_index` conventions.
///
/// SCSS resolves imports in this order:
/// 1. `@use 'variables'` → `_variables.scss` (partial convention)
/// 2. `@use 'components'` → `components/_index.scss` or `components/index.scss` (directory index)
///
/// Handles both relative (`../styles/variables`) and bare (`variables`) specifiers
/// that were normalized to `./variables` during extraction.
pub(super) fn try_scss_partial_fallback(
    ctx: &ResolveContext<'_>,
    from_file: &Path,
    specifier: &str,
) -> Option<ResolveResult> {
    // SCSS built-in modules (`sass:math`) should not be retried
    if specifier.contains(':') {
        return None;
    }

    let spec_path = Path::new(specifier);
    let filename = spec_path.file_name()?.to_str()?;

    // Already has underscore prefix
    if filename.starts_with('_') {
        return None;
    }

    // 1. Try partial convention: prepend _ to the filename
    let partial_filename = format!("_{filename}");
    let partial_specifier = if let Some(parent) = spec_path.parent()
        && !parent.as_os_str().is_empty()
    {
        format!("{}/{partial_filename}", parent.display())
    } else {
        partial_filename
    };

    if let Some(result) = try_resolve_scss(ctx, from_file, &partial_specifier) {
        return Some(result);
    }

    // 2. Try directory index convention: specifier/_index and specifier/index
    let index_partial = format!("{specifier}/_index");
    if let Some(result) = try_resolve_scss(ctx, from_file, &index_partial) {
        return Some(result);
    }

    let index_plain = format!("{specifier}/index");
    try_resolve_scss(ctx, from_file, &index_plain)
}

/// Attempt to resolve a single SCSS specifier and map to an internal module.
fn try_resolve_scss(
    ctx: &ResolveContext<'_>,
    from_file: &Path,
    specifier: &str,
) -> Option<ResolveResult> {
    let resolved = ctx.resolver.resolve_file(from_file, specifier).ok()?;
    let resolved_path = resolved.path();

    if let Some(&file_id) = ctx.raw_path_to_id.get(resolved_path) {
        return Some(ResolveResult::InternalModule(file_id));
    }
    if let Ok(canonical) = dunce::canonicalize(resolved_path)
        && let Some(&file_id) = ctx.path_to_id.get(canonical.as_path())
    {
        return Some(ResolveResult::InternalModule(file_id));
    }
    None
}

/// Try SCSS `includePaths` fallback: resolve the specifier against each
/// framework-contributed include directory.
///
/// Angular's `stylePreprocessorOptions.includePaths` (and Nx's equivalent via
/// project.json) adds extra search paths that SCSS resolves against before
/// falling back to node_modules. Bare `@use 'variables'` statements that were
/// normalized to `./variables` at extraction time fail the usual file-local
/// resolution, so when the importing file is `.scss`/`.sass` and the spec
/// originated from such a bare specifier, we retry against each include path,
/// applying the SCSS partial (`_variables`) and directory-index conventions.
///
/// The specifier arrives with a `./` prefix because `normalize_css_import_path`
/// rewrites bare extensionless SCSS specifiers to relative ones. We strip that
/// prefix here to re-enter the include-path search from the root of each
/// directory. Relative specifiers that already escape the importing file
/// (e.g. `../shared/variables`) are left untouched — include paths only
/// disambiguate bare specifiers, not explicit relative paths.
pub(super) fn try_scss_include_path_fallback(
    ctx: &ResolveContext<'_>,
    from_file: &Path,
    specifier: &str,
) -> Option<ResolveResult> {
    if ctx.scss_include_paths.is_empty() {
        return None;
    }
    if !from_file
        .extension()
        .is_some_and(|e| e == "scss" || e == "sass")
    {
        return None;
    }
    // SCSS built-in modules (`sass:math`) should not be retried
    if specifier.contains(':') {
        return None;
    }
    // Only bare (normalized) specifiers benefit from include-path search.
    // Parent-relative specifiers like `../shared/vars` explicitly escape the
    // importing file's directory and should not be silently redirected.
    let bare = specifier.strip_prefix("./")?;
    if bare.starts_with("..") || bare.starts_with('/') {
        return None;
    }

    for include_dir in ctx.scss_include_paths {
        if let Some(file_id) = find_scss_in_dir(include_dir, bare, ctx) {
            return Some(ResolveResult::InternalModule(file_id));
        }
    }
    None
}

/// Probe an SCSS include directory for a bare specifier, applying the standard
/// SCSS resolution order: exact file, `_`-prefixed partial, `_index` / `index`
/// directory conventions. Supports `.scss` and `.sass` extensions.
fn find_scss_in_dir(include_dir: &Path, bare: &str, ctx: &ResolveContext<'_>) -> Option<FileId> {
    let bare_path = Path::new(bare);
    let has_scss_ext = matches!(
        bare_path.extension().and_then(|e| e.to_str()),
        Some(ext) if ext.eq_ignore_ascii_case("scss") || ext.eq_ignore_ascii_case("sass")
    );

    // Split bare spec so we can build the `_`-prefixed partial for the final
    // component while preserving any leading directory segments.
    let parent = bare_path.parent();
    let stem_with_ext = bare_path.file_name()?.to_str()?;
    let stem_without_ext = bare_path.file_stem().and_then(|s| s.to_str())?;

    let build = |rel: &Path| -> std::path::PathBuf { include_dir.join(rel) };
    let join_with_parent = |name: &str| -> std::path::PathBuf {
        parent.map_or_else(|| build(Path::new(name)), |p| build(&p.join(name)))
    };

    let exts: &[&str] = if has_scss_ext {
        &[""]
    } else {
        &["scss", "sass"]
    };

    for ext in exts {
        let suffix = if ext.is_empty() {
            String::new()
        } else {
            format!(".{ext}")
        };
        // 1. Direct file: include_dir/<bare><ext>
        let direct = if ext.is_empty() {
            build(bare_path)
        } else {
            join_with_parent(&format!("{stem_with_ext}{suffix}"))
        };
        if let Some(fid) = lookup_scss_path(&direct, ctx) {
            return Some(fid);
        }
        // 2. Partial: include_dir/<parent>/_<stem><ext>
        let partial_name = if ext.is_empty() {
            format!("_{stem_with_ext}")
        } else {
            format!("_{stem_without_ext}{suffix}")
        };
        let partial = join_with_parent(&partial_name);
        if let Some(fid) = lookup_scss_path(&partial, ctx) {
            return Some(fid);
        }
        if ext.is_empty() {
            // Already has extension; directory index candidates below don't apply.
            continue;
        }
        // 3. Directory index: include_dir/<bare>/_index.<ext>
        let idx_partial = build(bare_path).join(format!("_index{suffix}"));
        if let Some(fid) = lookup_scss_path(&idx_partial, ctx) {
            return Some(fid);
        }
        let idx_plain = build(bare_path).join(format!("index{suffix}"));
        if let Some(fid) = lookup_scss_path(&idx_plain, ctx) {
            return Some(fid);
        }
    }
    None
}

/// Look up an absolute candidate path in the file index, falling back to
/// canonical path lookup for intra-project symlinks.
fn lookup_scss_path(candidate: &Path, ctx: &ResolveContext<'_>) -> Option<FileId> {
    if let Some(&file_id) = ctx.raw_path_to_id.get(candidate) {
        return Some(file_id);
    }
    if let Ok(canonical) = dunce::canonicalize(candidate) {
        if let Some(&file_id) = ctx.path_to_id.get(canonical.as_path()) {
            return Some(file_id);
        }
        if let Some(fallback) = ctx.canonical_fallback
            && let Some(file_id) = fallback.get(&canonical)
        {
            return Some(file_id);
        }
    }
    None
}

/// Try to map a resolved output path (e.g., `packages/ui/dist/utils.js`) back to
/// the corresponding source file (e.g., `packages/ui/src/utils.ts`).
///
/// This handles cross-workspace imports that go through `exports` maps pointing to
/// built output directories. Since fallow ignores `dist/`, `build/`, etc. by default,
/// the resolved path won't be in the file set, but the source file will be.
///
/// Nested output subdirectories (e.g., `dist/esm/utils.mjs`, `build/cjs/index.cjs`)
/// are handled by finding the last output directory component (closest to the file,
/// avoiding false matches on parent directories) and then walking backwards to collect
/// all consecutive output directory components before it.
pub(super) fn try_source_fallback(
    resolved: &Path,
    path_to_id: &FxHashMap<&Path, FileId>,
) -> Option<FileId> {
    let components: Vec<_> = resolved.components().collect();

    let is_output_dir = |c: &std::path::Component| -> bool {
        if let std::path::Component::Normal(s) = c
            && let Some(name) = s.to_str()
        {
            return OUTPUT_DIRS.contains(&name);
        }
        false
    };

    // Find the LAST output directory component (closest to the file).
    // Using rposition avoids false matches on parent directories that happen to
    // be named "build", "dist", etc.
    let last_output_pos = components.iter().rposition(&is_output_dir)?;

    // Walk backwards to find the start of consecutive output directory components.
    // e.g., for `dist/esm/utils.mjs`, rposition finds `esm`, then we walk back to `dist`.
    let mut first_output_pos = last_output_pos;
    while first_output_pos > 0 && is_output_dir(&components[first_output_pos - 1]) {
        first_output_pos -= 1;
    }

    // Build the path prefix (everything before the first consecutive output dir)
    let prefix: PathBuf = components[..first_output_pos].iter().collect();

    // Build the relative path after the last consecutive output dir
    let suffix: PathBuf = components[last_output_pos + 1..].iter().collect();
    suffix.file_stem()?; // Ensure the suffix has a filename

    // Try replacing the output dirs with "src" and each source extension
    for ext in SOURCE_EXTS {
        let source_candidate = prefix.join("src").join(suffix.with_extension(ext));
        if let Some(&file_id) = path_to_id.get(source_candidate.as_path()) {
            return Some(file_id);
        }
    }

    None
}

/// Extract npm package name from a resolved path inside `node_modules`.
///
/// Given a path like `/project/node_modules/react/index.js`, returns `Some("react")`.
/// Given a path like `/project/node_modules/@scope/pkg/dist/index.js`, returns `Some("@scope/pkg")`.
/// Returns `None` if the path doesn't contain a `node_modules` segment.
pub(super) fn extract_package_name_from_node_modules_path(path: &Path) -> Option<String> {
    let components: Vec<&str> = path
        .components()
        .filter_map(|c| match c {
            std::path::Component::Normal(s) => s.to_str(),
            _ => None,
        })
        .collect();

    // Find the last "node_modules" component (handles nested node_modules)
    let nm_idx = components.iter().rposition(|&c| c == "node_modules")?;

    let after = &components[nm_idx + 1..];
    if after.is_empty() {
        return None;
    }

    if after[0].starts_with('@') {
        // Scoped package: @scope/pkg
        if after.len() >= 2 {
            Some(format!("{}/{}", after[0], after[1]))
        } else {
            Some(after[0].to_string())
        }
    } else {
        Some(after[0].to_string())
    }
}

/// Try to map a pnpm virtual store path back to a workspace source file.
///
/// When pnpm uses injected dependencies or certain linking strategies, canonical
/// paths go through `.pnpm`:
///   `/project/node_modules/.pnpm/@myorg+ui@1.0.0/node_modules/@myorg/ui/dist/index.js`
///
/// This function detects such paths, extracts the package name, checks if it
/// matches a workspace package, and tries to find the source file in that workspace.
pub(super) fn try_pnpm_workspace_fallback(
    path: &Path,
    path_to_id: &FxHashMap<&Path, FileId>,
    workspace_roots: &FxHashMap<&str, &Path>,
) -> Option<FileId> {
    // Only relevant for paths containing .pnpm
    let components: Vec<&str> = path
        .components()
        .filter_map(|c| match c {
            std::path::Component::Normal(s) => s.to_str(),
            _ => None,
        })
        .collect();

    // Find .pnpm component
    let pnpm_idx = components.iter().position(|&c| c == ".pnpm")?;

    // After .pnpm, find the inner node_modules (the actual package location)
    // Structure: .pnpm/<name>@<version>/node_modules/<package>/...
    let after_pnpm = &components[pnpm_idx + 1..];

    // Find "node_modules" inside the .pnpm directory
    let inner_nm_idx = after_pnpm.iter().position(|&c| c == "node_modules")?;
    let after_inner_nm = &after_pnpm[inner_nm_idx + 1..];

    if after_inner_nm.is_empty() {
        return None;
    }

    // Extract package name (handle scoped packages)
    let (pkg_name, pkg_name_components) = if after_inner_nm[0].starts_with('@') {
        if after_inner_nm.len() >= 2 {
            (format!("{}/{}", after_inner_nm[0], after_inner_nm[1]), 2)
        } else {
            return None;
        }
    } else {
        (after_inner_nm[0].to_string(), 1)
    };

    // Check if this package is a workspace package
    let ws_root = workspace_roots.get(pkg_name.as_str())?;

    // Get the relative path within the package (after the package name components)
    let relative_parts = &after_inner_nm[pkg_name_components..];
    if relative_parts.is_empty() {
        return None;
    }

    let relative_path: PathBuf = relative_parts.iter().collect();

    // Try direct file lookup in workspace root
    let direct = ws_root.join(&relative_path);
    if let Some(&file_id) = path_to_id.get(direct.as_path()) {
        return Some(file_id);
    }

    // Try source fallback (dist/ → src/ etc.) within the workspace
    try_source_fallback(&direct, path_to_id)
}

/// Try to resolve a bare specifier as a workspace package reference.
///
/// When the specifier's package name matches a workspace package, resolve the
/// subpath against that package's root directory directly instead of going
/// through `node_modules`. Covers two cases:
///
/// 1. **Self-referencing package imports**: Node.js v12+ lets a package import
///    itself via its own name (`import { X } from '@org/pkg/subentry'` from
///    inside `@org/pkg`). Angular libraries built with `ng-packagr` rely on
///    this to declare secondary entry points.
/// 2. **Cross-workspace imports without `node_modules` symlinks**: monorepos
///    that have not been installed yet, or bundlers that bypass `node_modules`
///    entirely, still need to resolve `@org/other-pkg/sub` to the sibling
///    workspace's source file.
///
/// Strategy: strip the package name prefix and resolve the remainder as a
/// relative path from inside the workspace root, so `oxc_resolver` applies
/// directory indices, source extensions, and any workspace-local `tsconfig.json`
/// path aliases. The `exports` field is intentionally bypassed — it points at
/// compiled output (`dist/esm/button/index.js`) that does not exist in a
/// source-only workspace.
///
/// See issue #106.
pub(super) fn try_workspace_package_fallback(
    ctx: &ResolveContext<'_>,
    specifier: &str,
) -> Option<ResolveResult> {
    // Must look like a bare package specifier to avoid matching `./button`, etc.
    if !super::path_info::is_bare_specifier(specifier) {
        return None;
    }
    let pkg_name = super::path_info::extract_package_name(specifier);
    let ws_root = *ctx.workspace_roots.get(pkg_name.as_str())?;

    // Remainder after the package name. Empty for `@org/pkg`, `"button"` for
    // `@org/pkg/button`, `"internal/base"` for `@org/pkg/internal/base`.
    let subpath = specifier
        .strip_prefix(pkg_name.as_str())
        .and_then(|s| s.strip_prefix('/'))
        .unwrap_or("");

    // Synthetic importer inside the workspace root so tsconfig discovery walks
    // up from the correct directory and relative specifiers anchor there.
    let root_file = ws_root.join("__fallow_ws_self_resolve__");
    let rel_spec = if subpath.is_empty() {
        "./".to_string()
    } else {
        format!("./{subpath}")
    };

    let resolved = ctx.resolver.resolve_file(&root_file, &rel_spec).ok()?;
    let resolved_path = resolved.path();

    if let Some(&file_id) = ctx.raw_path_to_id.get(resolved_path) {
        return Some(ResolveResult::InternalModule(file_id));
    }
    if let Ok(canonical) = dunce::canonicalize(resolved_path) {
        if let Some(&file_id) = ctx.path_to_id.get(canonical.as_path()) {
            return Some(ResolveResult::InternalModule(file_id));
        }
        if let Some(fallback) = ctx.canonical_fallback
            && let Some(file_id) = fallback.get(&canonical)
        {
            return Some(ResolveResult::InternalModule(file_id));
        }
        if let Some(file_id) = try_source_fallback(&canonical, ctx.path_to_id) {
            return Some(ResolveResult::InternalModule(file_id));
        }
    }
    None
}

/// Convert a `DynamicImportPattern` to a glob string for file matching.
pub(super) fn make_glob_from_pattern(
    pattern: &fallow_types::extract::DynamicImportPattern,
) -> String {
    // If the prefix already contains glob characters (from import.meta.glob), use as-is
    if pattern.prefix.contains('*') || pattern.prefix.contains('{') {
        return pattern.prefix.clone();
    }
    pattern.suffix.as_ref().map_or_else(
        || format!("{}*", pattern.prefix),
        |suffix| format!("{}*{}", pattern.prefix, suffix),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_package_name_from_node_modules_path_regular() {
        let path = PathBuf::from("/project/node_modules/react/index.js");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("react".to_string())
        );
    }

    #[test]
    fn test_extract_package_name_from_node_modules_path_scoped() {
        let path = PathBuf::from("/project/node_modules/@babel/core/lib/index.js");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("@babel/core".to_string())
        );
    }

    #[test]
    fn test_extract_package_name_from_node_modules_path_nested() {
        // Nested node_modules: should use the last (innermost) one
        let path = PathBuf::from("/project/node_modules/pkg-a/node_modules/pkg-b/dist/index.js");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("pkg-b".to_string())
        );
    }

    #[test]
    fn test_extract_package_name_from_node_modules_path_deep_subpath() {
        let path = PathBuf::from("/project/node_modules/react-dom/cjs/react-dom.production.min.js");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("react-dom".to_string())
        );
    }

    #[test]
    fn test_extract_package_name_from_node_modules_path_no_node_modules() {
        let path = PathBuf::from("/project/src/components/Button.tsx");
        assert_eq!(extract_package_name_from_node_modules_path(&path), None);
    }

    #[test]
    fn test_extract_package_name_from_node_modules_path_just_node_modules() {
        let path = PathBuf::from("/project/node_modules");
        assert_eq!(extract_package_name_from_node_modules_path(&path), None);
    }

    #[test]
    fn test_extract_package_name_from_node_modules_path_scoped_only_scope() {
        // Edge case: path ends at scope without package name
        let path = PathBuf::from("/project/node_modules/@scope");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("@scope".to_string())
        );
    }

    #[test]
    fn test_resolve_specifier_node_modules_returns_npm_package() {
        // When oxc_resolver resolves to a node_modules path that is NOT in path_to_id,
        // it should return NpmPackage instead of ExternalFile.
        // We can't easily test resolve_specifier directly without a real resolver,
        // but the extract_package_name_from_node_modules_path function covers the
        // core logic that was missing.
        let path =
            PathBuf::from("/project/node_modules/styled-components/dist/styled-components.esm.js");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("styled-components".to_string())
        );

        let path = PathBuf::from("/project/node_modules/next/dist/server/next.js");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("next".to_string())
        );
    }

    #[test]
    fn test_try_source_fallback_dist_to_src() {
        let src_path = PathBuf::from("/project/packages/ui/src/utils.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(0));

        let dist_path = PathBuf::from("/project/packages/ui/dist/utils.js");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(0)),
            "dist/utils.js should fall back to src/utils.ts"
        );
    }

    #[test]
    fn test_try_source_fallback_build_to_src() {
        let src_path = PathBuf::from("/project/packages/core/src/index.tsx");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(1));

        let build_path = PathBuf::from("/project/packages/core/build/index.js");
        assert_eq!(
            try_source_fallback(&build_path, &path_to_id),
            Some(FileId(1)),
            "build/index.js should fall back to src/index.tsx"
        );
    }

    #[test]
    fn test_try_source_fallback_no_match() {
        let path_to_id: FxHashMap<&Path, FileId> = FxHashMap::default();

        let dist_path = PathBuf::from("/project/packages/ui/dist/utils.js");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            None,
            "should return None when no source file exists"
        );
    }

    #[test]
    fn test_try_source_fallback_non_output_dir() {
        let src_path = PathBuf::from("/project/packages/ui/src/utils.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(0));

        // A path that's not in an output directory should not trigger fallback
        let normal_path = PathBuf::from("/project/packages/ui/scripts/utils.js");
        assert_eq!(
            try_source_fallback(&normal_path, &path_to_id),
            None,
            "non-output directory path should not trigger fallback"
        );
    }

    #[test]
    fn test_try_source_fallback_nested_path() {
        let src_path = PathBuf::from("/project/packages/ui/src/components/Button.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(2));

        let dist_path = PathBuf::from("/project/packages/ui/dist/components/Button.js");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(2)),
            "nested dist path should fall back to nested src path"
        );
    }

    #[test]
    fn test_try_source_fallback_nested_dist_esm() {
        let src_path = PathBuf::from("/project/packages/ui/src/utils.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(0));

        let dist_path = PathBuf::from("/project/packages/ui/dist/esm/utils.mjs");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(0)),
            "dist/esm/utils.mjs should fall back to src/utils.ts"
        );
    }

    #[test]
    fn test_try_source_fallback_nested_build_cjs() {
        let src_path = PathBuf::from("/project/packages/core/src/index.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(1));

        let build_path = PathBuf::from("/project/packages/core/build/cjs/index.cjs");
        assert_eq!(
            try_source_fallback(&build_path, &path_to_id),
            Some(FileId(1)),
            "build/cjs/index.cjs should fall back to src/index.ts"
        );
    }

    #[test]
    fn test_try_source_fallback_nested_dist_esm_deep_path() {
        let src_path = PathBuf::from("/project/packages/ui/src/components/Button.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(2));

        let dist_path = PathBuf::from("/project/packages/ui/dist/esm/components/Button.mjs");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(2)),
            "dist/esm/components/Button.mjs should fall back to src/components/Button.ts"
        );
    }

    #[test]
    fn test_try_source_fallback_triple_nested_output_dirs() {
        let src_path = PathBuf::from("/project/packages/ui/src/utils.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(0));

        let dist_path = PathBuf::from("/project/packages/ui/out/dist/esm/utils.mjs");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(0)),
            "out/dist/esm/utils.mjs should fall back to src/utils.ts"
        );
    }

    #[test]
    fn test_try_source_fallback_parent_dir_named_build() {
        let src_path = PathBuf::from("/home/user/build/my-project/src/utils.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(0));

        let dist_path = PathBuf::from("/home/user/build/my-project/dist/utils.js");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(0)),
            "should resolve dist/ within project, not match parent 'build' dir"
        );
    }

    #[test]
    fn test_pnpm_store_path_extract_package_name() {
        // pnpm virtual store paths should correctly extract package name
        let path =
            PathBuf::from("/project/node_modules/.pnpm/react@18.2.0/node_modules/react/index.js");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("react".to_string())
        );
    }

    #[test]
    fn test_pnpm_store_path_scoped_package() {
        let path = PathBuf::from(
            "/project/node_modules/.pnpm/@babel+core@7.24.0/node_modules/@babel/core/lib/index.js",
        );
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("@babel/core".to_string())
        );
    }

    #[test]
    fn test_pnpm_store_path_with_peer_deps() {
        let path = PathBuf::from(
            "/project/node_modules/.pnpm/webpack@5.0.0_esbuild@0.19.0/node_modules/webpack/lib/index.js",
        );
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("webpack".to_string())
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_dist_to_src() {
        let src_path = PathBuf::from("/project/packages/ui/src/utils.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(0));

        let mut workspace_roots = FxHashMap::default();
        let ws_root = PathBuf::from("/project/packages/ui");
        workspace_roots.insert("@myorg/ui", ws_root.as_path());

        // pnpm virtual store path with dist/ output
        let pnpm_path = PathBuf::from(
            "/project/node_modules/.pnpm/@myorg+ui@1.0.0/node_modules/@myorg/ui/dist/utils.js",
        );
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            Some(FileId(0)),
            ".pnpm workspace path should fall back to src/utils.ts"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_direct_source() {
        let src_path = PathBuf::from("/project/packages/core/src/index.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(1));

        let mut workspace_roots = FxHashMap::default();
        let ws_root = PathBuf::from("/project/packages/core");
        workspace_roots.insert("@myorg/core", ws_root.as_path());

        // pnpm path pointing directly to src/
        let pnpm_path = PathBuf::from(
            "/project/node_modules/.pnpm/@myorg+core@workspace/node_modules/@myorg/core/src/index.ts",
        );
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            Some(FileId(1)),
            ".pnpm workspace path with src/ should resolve directly"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_non_workspace_package() {
        let path_to_id: FxHashMap<&Path, FileId> = FxHashMap::default();

        let mut workspace_roots = FxHashMap::default();
        let ws_root = PathBuf::from("/project/packages/ui");
        workspace_roots.insert("@myorg/ui", ws_root.as_path());

        // External package (not a workspace) — should return None
        let pnpm_path =
            PathBuf::from("/project/node_modules/.pnpm/react@18.2.0/node_modules/react/index.js");
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            None,
            "non-workspace package in .pnpm should return None"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_unscoped_package() {
        let src_path = PathBuf::from("/project/packages/utils/src/index.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(2));

        let mut workspace_roots = FxHashMap::default();
        let ws_root = PathBuf::from("/project/packages/utils");
        workspace_roots.insert("my-utils", ws_root.as_path());

        // Unscoped workspace package in pnpm store
        let pnpm_path = PathBuf::from(
            "/project/node_modules/.pnpm/my-utils@1.0.0/node_modules/my-utils/dist/index.js",
        );
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            Some(FileId(2)),
            "unscoped workspace package in .pnpm should resolve"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_nested_path() {
        let src_path = PathBuf::from("/project/packages/ui/src/components/Button.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(3));

        let mut workspace_roots = FxHashMap::default();
        let ws_root = PathBuf::from("/project/packages/ui");
        workspace_roots.insert("@myorg/ui", ws_root.as_path());

        // Nested path within the package
        let pnpm_path = PathBuf::from(
            "/project/node_modules/.pnpm/@myorg+ui@1.0.0/node_modules/@myorg/ui/dist/components/Button.js",
        );
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            Some(FileId(3)),
            "nested .pnpm workspace path should resolve through source fallback"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_no_pnpm() {
        let path_to_id: FxHashMap<&Path, FileId> = FxHashMap::default();
        let workspace_roots: FxHashMap<&str, &Path> = FxHashMap::default();

        // Regular path without .pnpm — should return None immediately
        let regular_path = PathBuf::from("/project/node_modules/react/index.js");
        assert_eq!(
            try_pnpm_workspace_fallback(&regular_path, &path_to_id, &workspace_roots),
            None,
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_with_peer_deps() {
        let src_path = PathBuf::from("/project/packages/ui/src/index.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(4));

        let mut workspace_roots = FxHashMap::default();
        let ws_root = PathBuf::from("/project/packages/ui");
        workspace_roots.insert("@myorg/ui", ws_root.as_path());

        // pnpm path with peer dependency suffix
        let pnpm_path = PathBuf::from(
            "/project/node_modules/.pnpm/@myorg+ui@1.0.0_react@18.2.0/node_modules/@myorg/ui/dist/index.js",
        );
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            Some(FileId(4)),
            ".pnpm path with peer dep suffix should still resolve"
        );
    }

    // ── make_glob_from_pattern ───────────────────────────────────────

    #[test]
    fn make_glob_prefix_only_no_suffix() {
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./locales/".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./locales/*");
    }

    #[test]
    fn make_glob_prefix_with_suffix() {
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./locales/".to_string(),
            suffix: Some(".json".to_string()),
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./locales/*.json");
    }

    #[test]
    fn make_glob_passthrough_star() {
        // Prefix already contains glob characters — use as-is
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./pages/**/*.tsx".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./pages/**/*.tsx");
    }

    #[test]
    fn make_glob_passthrough_brace() {
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./i18n/{en,de,fr}.json".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./i18n/{en,de,fr}.json");
    }

    #[test]
    fn make_glob_empty_prefix_no_suffix() {
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: String::new(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "*");
    }

    #[test]
    fn make_glob_empty_prefix_with_suffix() {
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: String::new(),
            suffix: Some(".ts".to_string()),
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "*.ts");
    }

    // ── make_glob_from_pattern: template literal patterns ──────────

    #[test]
    fn make_glob_template_literal_prefix_only() {
        // `./pages/${page}` extracts prefix="./pages/", suffix=None
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./pages/".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./pages/*");
    }

    #[test]
    fn make_glob_template_literal_with_extension_suffix() {
        // `./locales/${lang}.json` extracts prefix="./locales/", suffix=".json"
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./locales/".to_string(),
            suffix: Some(".json".to_string()),
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./locales/*.json");
    }

    #[test]
    fn make_glob_template_literal_deep_prefix() {
        // `./modules/${area}/components/${name}.tsx`
        // Extractor captures prefix="./modules/", suffix=None (only first dynamic part)
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./modules/".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./modules/*");
    }

    #[test]
    fn make_glob_string_concat_prefix() {
        // `'./pages/' + name` extracts prefix="./pages/", suffix=None
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./pages/".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./pages/*");
    }

    #[test]
    fn make_glob_string_concat_with_extension() {
        // `'./views/' + name + '.vue'` extracts prefix="./views/", suffix=".vue"
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./views/".to_string(),
            suffix: Some(".vue".to_string()),
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./views/*.vue");
    }

    // ── make_glob_from_pattern: import.meta.glob ──────────────────

    #[test]
    fn make_glob_import_meta_glob_recursive() {
        // import.meta.glob('./components/**/*.vue')
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./components/**/*.vue".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(
            make_glob_from_pattern(&pattern),
            "./components/**/*.vue",
            "import.meta.glob patterns with * should pass through as-is"
        );
    }

    #[test]
    fn make_glob_import_meta_glob_brace_expansion() {
        // import.meta.glob('./plugins/{auth,analytics}.ts')
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./plugins/{auth,analytics}.ts".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(
            make_glob_from_pattern(&pattern),
            "./plugins/{auth,analytics}.ts",
            "import.meta.glob patterns with braces should pass through as-is"
        );
    }

    #[test]
    fn make_glob_import_meta_glob_star_with_brace() {
        // import.meta.glob('./routes/**/*.{ts,tsx}')
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./routes/**/*.{ts,tsx}".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(
            make_glob_from_pattern(&pattern),
            "./routes/**/*.{ts,tsx}",
            "combined * and brace patterns should pass through"
        );
    }

    #[test]
    fn make_glob_import_meta_glob_ignores_suffix_when_star_present() {
        // Edge case: prefix contains *, suffix is provided (unlikely but defensive)
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./*.ts".to_string(),
            suffix: Some(".extra".to_string()),
            span: oxc_span::Span::default(),
        };
        assert_eq!(
            make_glob_from_pattern(&pattern),
            "./*.ts",
            "when prefix has glob chars, suffix is ignored (prefix used as-is)"
        );
    }

    // ── make_glob_from_pattern: edge cases ────────────────────────

    #[test]
    fn make_glob_single_dot_prefix() {
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./*");
    }

    #[test]
    fn make_glob_prefix_without_trailing_slash() {
        // `'./config' + ext` -> prefix="./config", suffix might be extension
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "./config".to_string(),
            suffix: None,
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "./config*");
    }

    #[test]
    fn make_glob_prefix_with_dotdot() {
        let pattern = fallow_types::extract::DynamicImportPattern {
            prefix: "../shared/".to_string(),
            suffix: Some(".ts".to_string()),
            span: oxc_span::Span::default(),
        };
        assert_eq!(make_glob_from_pattern(&pattern), "../shared/*.ts");
    }

    // ── extract_package_name: additional edge cases ───────────────

    #[test]
    fn test_extract_package_name_with_pnpm_plus_encoded_scope() {
        // pnpm encodes @scope/pkg as @scope+pkg in store path
        // but the inner node_modules still uses the real scope
        let path = PathBuf::from(
            "/project/node_modules/.pnpm/@mui+material@5.15.0/node_modules/@mui/material/index.js",
        );
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("@mui/material".to_string())
        );
    }

    #[test]
    fn test_extract_package_name_windows_style_path() {
        // Windows-style paths should still work since we filter for Normal components
        let path = PathBuf::from("/project/node_modules/typescript/lib/tsc.js");
        assert_eq!(
            extract_package_name_from_node_modules_path(&path),
            Some("typescript".to_string())
        );
    }

    // ── try_source_fallback: additional output dir patterns ───────

    #[test]
    fn test_try_source_fallback_out_dir() {
        let src_path = PathBuf::from("/project/packages/api/src/handler.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(5));

        let out_path = PathBuf::from("/project/packages/api/out/handler.js");
        assert_eq!(
            try_source_fallback(&out_path, &path_to_id),
            Some(FileId(5)),
            "out/handler.js should fall back to src/handler.ts"
        );
    }

    #[test]
    fn test_try_source_fallback_mts_extension() {
        let src_path = PathBuf::from("/project/packages/lib/src/utils.mts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(6));

        let dist_path = PathBuf::from("/project/packages/lib/dist/utils.mjs");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(6)),
            "dist/utils.mjs should fall back to src/utils.mts"
        );
    }

    #[test]
    fn test_try_source_fallback_cts_extension() {
        let src_path = PathBuf::from("/project/packages/lib/src/config.cts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(7));

        let dist_path = PathBuf::from("/project/packages/lib/dist/config.cjs");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(7)),
            "dist/config.cjs should fall back to src/config.cts"
        );
    }

    #[test]
    fn test_try_source_fallback_jsx_extension() {
        let src_path = PathBuf::from("/project/packages/ui/src/App.jsx");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(8));

        let build_path = PathBuf::from("/project/packages/ui/build/App.js");
        assert_eq!(
            try_source_fallback(&build_path, &path_to_id),
            Some(FileId(8)),
            "build/App.js should fall back to src/App.jsx"
        );
    }

    #[test]
    fn test_try_source_fallback_no_file_stem() {
        // Path with no filename at all should return None gracefully
        let path_to_id: FxHashMap<&Path, FileId> = FxHashMap::default();
        let dist_path = PathBuf::from("/project/packages/ui/dist/");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            None,
            "directory path with no file should return None"
        );
    }

    #[test]
    fn test_try_source_fallback_esm_subdir() {
        // esm is an output directory, so dist/esm -> src
        let src_path = PathBuf::from("/project/lib/src/index.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(10));

        let dist_path = PathBuf::from("/project/lib/esm/index.mjs");
        assert_eq!(
            try_source_fallback(&dist_path, &path_to_id),
            Some(FileId(10)),
            "standalone esm/ directory should fall back to src/"
        );
    }

    #[test]
    fn test_try_source_fallback_cjs_subdir() {
        let src_path = PathBuf::from("/project/lib/src/index.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(11));

        let cjs_path = PathBuf::from("/project/lib/cjs/index.cjs");
        assert_eq!(
            try_source_fallback(&cjs_path, &path_to_id),
            Some(FileId(11)),
            "standalone cjs/ directory should fall back to src/"
        );
    }

    // ── try_pnpm_workspace_fallback: edge cases ──────────────────

    #[test]
    fn test_try_pnpm_workspace_fallback_empty_after_pnpm() {
        // Path that has .pnpm but nothing after the inner node_modules
        let path_to_id: FxHashMap<&Path, FileId> = FxHashMap::default();
        let workspace_roots: FxHashMap<&str, &Path> = FxHashMap::default();

        let pnpm_path = PathBuf::from("/project/node_modules/.pnpm/pkg@1.0.0/node_modules");
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            None,
            "path ending at node_modules with nothing after should return None"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_scoped_package_only_scope() {
        // Path has .pnpm/inner-node_modules/@scope but no package name after scope
        let path_to_id: FxHashMap<&Path, FileId> = FxHashMap::default();
        let workspace_roots: FxHashMap<&str, &Path> = FxHashMap::default();

        let pnpm_path =
            PathBuf::from("/project/node_modules/.pnpm/@scope+pkg@1.0.0/node_modules/@scope");
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            None,
            "scoped package without full name and no matching workspace should return None"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_no_inner_node_modules() {
        // Path has .pnpm but no inner node_modules
        let path_to_id: FxHashMap<&Path, FileId> = FxHashMap::default();
        let workspace_roots: FxHashMap<&str, &Path> = FxHashMap::default();

        let pnpm_path = PathBuf::from("/project/node_modules/.pnpm/pkg@1.0.0/dist/index.js");
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            None,
            "path without inner node_modules after .pnpm should return None"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_package_without_relative_path() {
        // Path ends right at the package name, no file path after it
        let path_to_id: FxHashMap<&Path, FileId> = FxHashMap::default();
        let mut workspace_roots = FxHashMap::default();
        let ws_root = PathBuf::from("/project/packages/ui");
        workspace_roots.insert("@myorg/ui", ws_root.as_path());

        let pnpm_path =
            PathBuf::from("/project/node_modules/.pnpm/@myorg+ui@1.0.0/node_modules/@myorg/ui");
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            None,
            "path ending at package name with no relative file should return None"
        );
    }

    #[test]
    fn test_try_pnpm_workspace_fallback_nested_dist_esm() {
        let src_path = PathBuf::from("/project/packages/ui/src/Button.ts");
        let mut path_to_id = FxHashMap::default();
        path_to_id.insert(src_path.as_path(), FileId(10));

        let mut workspace_roots = FxHashMap::default();
        let ws_root = PathBuf::from("/project/packages/ui");
        workspace_roots.insert("@myorg/ui", ws_root.as_path());

        // Nested output dirs within pnpm workspace path
        let pnpm_path = PathBuf::from(
            "/project/node_modules/.pnpm/@myorg+ui@1.0.0/node_modules/@myorg/ui/dist/esm/Button.mjs",
        );
        assert_eq!(
            try_pnpm_workspace_fallback(&pnpm_path, &path_to_id, &workspace_roots),
            Some(FileId(10)),
            "pnpm path with nested dist/esm should resolve through source fallback"
        );
    }
}