oxc_resolver 11.24.2

ESM / CJS module resolution
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
use std::{
    env, fs,
    io::{self, Write},
    path::{Path, PathBuf},
};

use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use memory_fs::BenchMemoryFS;
use oxc_resolver::{FileSystem as FileSystemTrait, FileSystemOs, PackageJson};
use rayon::prelude::*;

fn data() -> Vec<(PathBuf, &'static str)> {
    let cwd = env::current_dir().unwrap();
    let f1 = cwd.join("fixtures/enhanced-resolve");
    let f2 = f1.join("test/fixtures");
    vec![
        // real packages
        (cwd.clone(), "@napi-rs/cli"),
        (cwd.clone(), "@napi-rs/wasm-runtime"),
        (cwd.clone(), "vite-plus"),
        (cwd.clone(), "emnapi"),
        (cwd, "typescript"),
        // relative path
        (f1.clone(), "./"),
        (f1.clone(), "./lib/index"),
        // absolute path
        (f1.clone(), "/absolute/path"),
        // query fragment
        (f2.clone(), "./main1.js#fragment?query"),
        (f2.clone(), "m1/a.js?query#fragment"),
        // browserField
        (f2.join("browser-module"), "./lib/replaced"),
        (f2.join("browser-module/lib"), "./replaced"),
        // exportsField
        (f2.join("exports-field"), "exports-field"),
        (f2.join("exports-field"), "exports-field/dist/main.js"),
        (f2.join("exports-field"), "exports-field/dist/main.js?foo"),
        (f2.join("exports-field"), "exports-field/dist/main.js#foo"),
        (f2.join("exports-field"), "@exports-field/core"),
        (f2.join("imports-exports-wildcard"), "m/features/f.js"),
        // extensionAlias
        (f2.join("extension-alias"), "./index.js"),
        (f2.join("extension-alias"), "./dir2/index.mjs"),
        // extensions
        (f2.join("extensions"), "./foo"),
        (f2.join("extensions"), "."),
        (f2.join("extensions"), "./dir"),
        (f2.join("extensions"), "module/"),
        // importsField
        (f2.join("imports-field"), "#imports-field"),
        (f2.join("imports-exports-wildcard/node_modules/m/"), "#internal/i.js"),
        // scoped
        (f2.join("scoped"), "@scope/pack1"),
        (f2.join("scoped"), "@scope/pack2/lib"),
        // dashed name
        (f2.clone(), "dash"),
        (f2.clone(), "dash-name"),
        (f2.join("node_modules/dash"), "dash"),
        (f2.join("node_modules/dash"), "dash-name"),
        (f2.join("node_modules/dash-name"), "dash"),
        (f2.join("node_modules/dash-name"), "dash-name"),
        // alias
        (f1.clone(), "aaa"),
        (f1.clone(), "ggg"),
        (f1.clone(), "rrr"),
        (f1.clone(), "@"),
        (f1, "@@@"),
    ]
}

fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
    #[cfg(target_family = "unix")]
    {
        std::os::unix::fs::symlink(original, link)
    }

    #[cfg(target_family = "windows")]
    {
        std::os::windows::fs::symlink_file(original, link)
    }
}

fn create_symlinks() -> io::Result<PathBuf> {
    let root = env::current_dir()?.join("fixtures/enhanced-resolve");
    let dirname = root.join("test");
    let temp_path = dirname.join("temp_symlinks");
    let create_symlink_fixtures = || -> io::Result<()> {
        fs::create_dir(&temp_path)?;
        let mut index = fs::File::create(temp_path.join("index.js"))?;
        index.write_all(b"console.log('Hello, World!')")?;
        // create 10000 symlink files pointing to the index.js
        for i in 0..10000 {
            symlink(temp_path.join("index.js"), temp_path.join(format!("file{i}.js")))?;
        }
        Ok(())
    };
    if !temp_path.exists()
        && let Err(err) = create_symlink_fixtures()
    {
        let _ = fs::remove_dir_all(&temp_path);
        return Err(err);
    }
    Ok(temp_path)
}

fn resolve_options() -> oxc_resolver::ResolveOptions {
    use oxc_resolver::{AliasValue, ResolveOptions};
    let alias_value = AliasValue::from("./");
    ResolveOptions {
        extensions: vec![".ts".into(), ".js".into()],
        condition_names: vec!["webpack".into(), "require".into()],
        alias_fields: vec![vec!["browser".into()]],
        extension_alias: vec![
            (".js".into(), vec![".ts".into(), ".js".into()]),
            (".mjs".into(), vec![".mts".into()]),
        ],
        // Real projects LOVE setting these many aliases.
        // I saw them with my own eyes.
        alias: vec![
            ("/absolute/path".into(), vec![alias_value.clone()]),
            ("aaa".into(), vec![alias_value.clone()]),
            ("bbb".into(), vec![alias_value.clone()]),
            ("ccc".into(), vec![alias_value.clone()]),
            ("ddd".into(), vec![alias_value.clone()]),
            ("eee".into(), vec![alias_value.clone()]),
            ("fff".into(), vec![alias_value.clone()]),
            ("ggg".into(), vec![alias_value.clone()]),
            ("hhh".into(), vec![alias_value.clone()]),
            ("iii".into(), vec![alias_value.clone()]),
            ("jjj".into(), vec![alias_value.clone()]),
            ("kkk".into(), vec![alias_value.clone()]),
            ("lll".into(), vec![alias_value.clone()]),
            ("mmm".into(), vec![alias_value.clone()]),
            ("nnn".into(), vec![alias_value.clone()]),
            ("ooo".into(), vec![alias_value.clone()]),
            ("ppp".into(), vec![alias_value.clone()]),
            ("qqq".into(), vec![alias_value.clone()]),
            ("rrr".into(), vec![alias_value.clone()]),
            ("sss".into(), vec![alias_value.clone()]),
            ("@".into(), vec![alias_value.clone()]),
            ("@@".into(), vec![alias_value.clone()]),
            ("@@@".into(), vec![alias_value]),
        ],
        ..ResolveOptions::default()
    }
}

fn oxc_resolver_memory() -> oxc_resolver::ResolverGeneric<BenchMemoryFS> {
    use oxc_resolver::ResolverGeneric;
    let fs = BenchMemoryFS::new();
    ResolverGeneric::new_with_file_system(fs, resolve_options())
}

fn oxc_resolver_real() -> oxc_resolver::Resolver {
    use oxc_resolver::Resolver;
    Resolver::new(resolve_options())
}

/// Pin rayon's global pool to a fixed thread count so the `multi-thread` benches are deterministic.
/// Rayon otherwise sizes the pool to the host core count, which varies across CI runners and makes
/// CodSpeed's instrumented instruction count flaky. Idempotent: the first call wins, the rest are
/// no-ops, so it's safe to call from every parallel bench regardless of run order or filtering.
fn pin_rayon_threads() {
    let _ = rayon::ThreadPoolBuilder::new().num_threads(4).build_global();
}

fn bench_resolver_memory(c: &mut Criterion) {
    pin_rayon_threads();
    let data = data();
    let cwd = env::current_dir().unwrap();
    let symlink_test_dir = cwd.join("fixtures/enhanced-resolve/test/temp_symlinks");

    // check validity
    for (path, request) in &data {
        assert!(
            oxc_resolver_memory().resolve(path, request).is_ok(),
            "{} {request}",
            path.display()
        );
    }

    let symlink_specifiers = (0u32..10000).map(|i| format!("./file{i}")).collect::<Vec<_>>();

    {
        let oxc_resolver = oxc_resolver_memory();
        for specifier in &symlink_specifiers {
            assert!(oxc_resolver.resolve(&symlink_test_dir, specifier).is_ok(), "{specifier}");
        }
    }

    let mut group = c.benchmark_group("resolver_memory");

    group.bench_with_input(BenchmarkId::from_parameter("single-thread"), &data, |b, data| {
        let oxc_resolver = oxc_resolver_memory();
        b.iter(|| {
            for (path, request) in data {
                _ = oxc_resolver.resolve(path, request);
            }
        });
    });

    group.bench_with_input(BenchmarkId::from_parameter("drop"), &data, |b, data| {
        b.iter(|| {
            let oxc_resolver = oxc_resolver_memory(); // Measure `Drop` performance.
            for (path, request) in data {
                _ = oxc_resolver.resolve(path, request);
            }
        });
    });

    group.bench_with_input(BenchmarkId::from_parameter("multi-thread"), &data, |b, data| {
        let oxc_resolver = oxc_resolver_memory();
        b.iter(|| {
            data.par_iter().for_each(|(path, request)| {
                _ = oxc_resolver.resolve(path, request);
            });
        });
    });

    group.bench_with_input(
        BenchmarkId::from_parameter("resolve from symlinks"),
        &symlink_specifiers,
        |b, specifiers| {
            let oxc_resolver = oxc_resolver_memory();
            b.iter(|| {
                for specifier in specifiers {
                    _ = oxc_resolver.resolve(&symlink_test_dir, specifier);
                }
            });
        },
    );

    group.bench_with_input(BenchmarkId::from_parameter("find tsconfig"), &data, |b, data| {
        let oxc_resolver = oxc_resolver_memory();
        let paths = data
            .iter()
            .map(|(path, request)| oxc_resolver.resolve(path, request).unwrap().into_path_buf())
            .collect::<Vec<_>>();
        b.iter(|| {
            for path in &paths {
                let _ = oxc_resolver.find_tsconfig(path);
            }
        });
    });
}

fn bench_resolver_real(c: &mut Criterion) {
    pin_rayon_threads();
    let data = data();
    let symlink_test_dir = create_symlinks().expect("Create symlink fixtures failed");

    // check validity
    for (path, request) in &data {
        assert!(oxc_resolver_real().resolve(path, request).is_ok(), "{} {request}", path.display());
    }

    let symlink_specifiers = (0u32..10000).map(|i| format!("./file{i}")).collect::<Vec<_>>();

    {
        let oxc_resolver = oxc_resolver_real();
        for specifier in &symlink_specifiers {
            assert!(oxc_resolver.resolve(&symlink_test_dir, specifier).is_ok(), "{specifier}");
        }
    }

    let mut group = c.benchmark_group("resolver_real");

    group.bench_with_input(BenchmarkId::from_parameter("single-thread"), &data, |b, data| {
        let oxc_resolver = oxc_resolver_real();
        b.iter(|| {
            for (path, request) in data {
                _ = oxc_resolver.resolve(path, request);
            }
        });
    });

    group.bench_with_input(BenchmarkId::from_parameter("multi-thread"), &data, |b, data| {
        let oxc_resolver = oxc_resolver_real();
        b.iter(|| {
            data.par_iter().for_each(|(path, request)| {
                _ = oxc_resolver.resolve(path, request);
            });
        });
    });

    group.bench_with_input(
        BenchmarkId::from_parameter("resolve from symlinks"),
        &symlink_specifiers,
        |b, specifiers| {
            let oxc_resolver = oxc_resolver_real();
            b.iter(|| {
                for specifier in specifiers {
                    _ = oxc_resolver.resolve(&symlink_test_dir, specifier);
                }
            });
        },
    );
}

fn bench_package_json_deserialization(c: &mut Criterion) {
    let mut group = c.benchmark_group("package_json_deserialization");

    // Prepare different sizes of package.json content
    let small_json = r#"{
        "name": "test-package",
        "version": "1.0.0"
    }"#;

    let medium_json = r##"{
        "name": "test-package",
        "version": "1.0.0",
        "main": "./lib/index.js",
        "type": "module",
        "exports": {
            ".": "./lib/index.js",
            "./feature": "./lib/feature.js"
        },
        "imports": {
            "#internal": "./src/internal.js"
        },
        "browser": {
            "./lib/node.js": "./lib/browser.js"
        },
        "sideEffects": false
    }"##;

    let large_json = r##"{
        "name": "test-package",
        "version": "1.0.0",
        "main": "./lib/index.js",
        "type": "module",
        "exports": {
            ".": {
                "import": "./lib/index.mjs",
                "require": "./lib/index.cjs",
                "browser": "./lib/browser.js"
            },
            "./feature": {
                "import": "./lib/feature.mjs",
                "require": "./lib/feature.cjs"
            },
            "./utils": "./lib/utils.js",
            "./internal/*": "./lib/internal/*.js"
        },
        "imports": {
            "#internal": "./src/internal.js",
            "#utils/*": "./src/utils/*.js"
        },
        "browser": {
            "./lib/node.js": "./lib/browser.js",
            "module-a": "./browser/module-a.js",
            "module-b": "module-c",
            "./lib/replaced.js": "./lib/browser"
        },
        "sideEffects": ["*.css", "*.scss"],
        "dependencies": {
            "lodash": "^4.17.21",
            "react": "^18.0.0",
            "express": "^4.18.0"
        },
        "devDependencies": {
            "typescript": "^5.0.0",
            "eslint": "^8.0.0",
            "jest": "^29.0.0"
        },
        "scripts": {
            "test": "jest",
            "build": "tsc",
            "lint": "eslint src"
        }
    }"##;

    // Load real complex package.json from fixtures
    let complex_json_path = env::current_dir()
        .unwrap()
        .join("fixtures/enhanced-resolve/test/fixtures/browser-module/package.json");
    let complex_json =
        fs::read_to_string(&complex_json_path).expect("Failed to read complex package.json");

    let test_path = PathBuf::from("/test/package.json");
    let test_realpath = test_path.clone();
    #[cfg(feature = "yarn_pnp")]
    let fs = FileSystemOs::new(false);
    #[cfg(not(feature = "yarn_pnp"))]
    let fs = FileSystemOs::new();

    let data = [
        ("small", small_json.to_string()),
        ("medium", medium_json.to_string()),
        ("large", large_json.to_string()),
        ("complex_real", complex_json),
    ];

    for (name, json) in data {
        group.bench_function(name, |b| {
            b.iter_with_setup_wrapper(|runner| {
                let json = json.clone().into_bytes();
                runner.run(|| {
                    PackageJson::parse(&fs, test_path.clone(), test_realpath.clone(), json)
                        .expect("Failed to parse JSON");
                });
            });
        });
    }

    group.finish();
}

fn bench_tsconfig_paths_aliases(c: &mut Criterion) {
    use oxc_resolver::{
        ResolveOptions, ResolverGeneric, TsconfigDiscovery, TsconfigOptions, TsconfigReferences,
    };

    let mut group = c.benchmark_group("tsconfig_paths_aliases_memory");
    let alias_count = 200usize;
    let (fs, importer, tsconfig_path, requests) =
        BenchMemoryFS::with_large_tsconfig_paths_fixture(alias_count);
    let resolver = ResolverGeneric::new_with_file_system(
        fs,
        ResolveOptions {
            tsconfig: Some(TsconfigDiscovery::Manual(TsconfigOptions {
                config_file: tsconfig_path,
                references: TsconfigReferences::Disabled,
            })),
            extensions: vec![".ts".into(), ".js".into()],
            ..ResolveOptions::default()
        },
    );

    // Check validity by resolving every alias once.
    for request in &requests {
        assert!(
            resolver.resolve_file(&importer, request).is_ok(),
            "alias_count={alias_count} request={request}"
        );
    }

    group.bench_with_input(
        BenchmarkId::from_parameter("query each alias"),
        &requests,
        |b, requests| {
            b.iter(|| {
                for request in requests {
                    _ = resolver.resolve_file(&importer, request);
                }
            });
        },
    );
}

criterion_group!(
    resolver,
    bench_resolver_memory,
    bench_resolver_real,
    bench_package_json_deserialization,
    bench_tsconfig_paths_aliases
);
criterion_main!(resolver);

mod memory_fs {
    //! Memory-based file system implementation for benchmarks.
    //!
    //! This module provides an in-memory file system that loads all fixture data
    //! and node_modules packages at initialization time, eliminating filesystem I/O
    //! variance during benchmark execution. This ensures stable, reproducible benchmark results.

    use std::{
        fs, io,
        path::{Path, PathBuf},
        sync::LazyLock,
    };

    use oxc_resolver::{FileMetadata, FileSystem, ResolveError};
    use rustc_hash::{FxHashMap, FxHashSet};
    use walkdir::WalkDir;

    /// Memory-based file system for benchmarks to eliminate I/O variance
    #[derive(Clone)]
    pub struct BenchMemoryFS {
        files: FxHashMap<PathBuf, Vec<u8>>,
        directories: FxHashSet<PathBuf>,
        symlinks: FxHashMap<PathBuf, PathBuf>,
    }

    static BENCH_FS: LazyLock<BenchMemoryFS> = LazyLock::new(|| {
        let mut fs = BenchMemoryFS {
            files: FxHashMap::default(),
            directories: FxHashSet::default(),
            symlinks: FxHashMap::default(),
        };
        fs.load_fixtures();
        fs
    });

    impl BenchMemoryFS {
        /// Create a new memory file system and load all fixtures
        pub fn new() -> Self {
            // Return a clone of the pre-loaded static FS
            BENCH_FS.clone()
        }

        pub fn with_large_tsconfig_paths_fixture(
            alias_count: usize,
        ) -> (Self, PathBuf, PathBuf, Vec<String>) {
            let fs = Self::new();
            let cwd = std::env::current_dir().unwrap();
            let root = cwd.join(format!("fixtures/tsconfig/bench-tsconfig-paths/{alias_count}"));
            let importer = root.join("app/main.ts");
            let tsconfig_path = root.join("tsconfig.json");
            assert!(
                fs.files.contains_key(&importer),
                "missing benchmark fixture: {}",
                importer.display()
            );
            assert!(
                fs.files.contains_key(&tsconfig_path),
                "missing benchmark fixture: {}",
                tsconfig_path.display()
            );
            let requests = (0..alias_count).map(|i| format!("@pkg{i:04}/file")).collect();
            (fs, importer, tsconfig_path, requests)
        }

        fn add_parent_directories(&mut self, path: &Path) {
            // Add all parent directories of a path
            for ancestor in path.ancestors().skip(1) {
                self.directories.insert(ancestor.to_path_buf());
            }
        }

        fn load_fixtures(&mut self) {
            let cwd = std::env::current_dir().unwrap();

            // Add all parent directories for the cwd
            self.add_parent_directories(&cwd);

            // Load fixtures from enhanced-resolve
            self.load_directory_tree(&cwd.join("fixtures/enhanced-resolve"));
            self.load_directory_tree(&cwd.join("fixtures/tsconfig/bench-tsconfig-paths"));

            // Load specific node_modules packages for benchmarks
            self.load_node_modules_packages(&cwd);

            // Create symlink fixtures for benchmark (10000 symlinks)
            self.create_symlink_fixtures(&cwd);
        }

        fn load_directory_tree(&mut self, root: &Path) {
            if !root.exists() {
                return;
            }
            for entry in WalkDir::new(root).follow_links(false).into_iter().filter_map(Result::ok) {
                let path = entry.path();
                let Ok(metadata) = fs::symlink_metadata(path) else { continue };

                // Store with absolute paths
                let abs_path = path.to_path_buf();

                if metadata.is_symlink() {
                    if let Ok(target) = fs::read_link(path) {
                        self.symlinks.insert(abs_path.clone(), target);
                        self.add_parent_directories(&abs_path);
                    }
                } else if metadata.is_dir() {
                    self.directories.insert(abs_path.clone());
                    self.add_parent_directories(&abs_path);
                } else if metadata.is_file()
                    && let Ok(content) = fs::read(path)
                {
                    self.files.insert(abs_path.clone(), content);
                    self.add_parent_directories(&abs_path);
                }
            }
        }

        fn load_node_modules_packages(&mut self, cwd: &Path) {
            let node_modules = cwd.join("node_modules");
            if !node_modules.exists() {
                return;
            }

            // Only load these specific packages needed for benchmarks
            let packages =
                ["@napi-rs/cli", "@napi-rs/wasm-runtime", "vite-plus", "emnapi", "typescript"];

            for package_name in packages {
                let package_path = node_modules.join(package_name);
                if !package_path.exists() {
                    continue;
                }

                // For scoped packages, also register the parent scope directory
                if package_name.starts_with('@')
                    && let Some(parent) = package_path.parent()
                    && parent != node_modules
                {
                    self.directories.insert(parent.to_path_buf());
                    self.add_parent_directories(parent);
                }

                // Check if it's a symlink and resolve it
                if let Ok(metadata) = fs::symlink_metadata(&package_path) {
                    if metadata.is_symlink() {
                        // Add the symlink itself
                        if let Ok(target) = fs::read_link(&package_path) {
                            self.symlinks.insert(package_path.clone(), target.clone());
                            self.add_parent_directories(&package_path);

                            // Resolve the symlink target (relative to node_modules)
                            let resolved_target = if target.is_relative() {
                                package_path.parent().unwrap().join(&target)
                            } else {
                                target
                            };

                            // Load the actual package directory
                            if resolved_target.exists() {
                                self.load_package_files(&resolved_target);
                            }

                            // ALSO load via the symlink path itself, because the resolver
                            // might query using the symlink path
                            self.load_package_files(&package_path);
                        }
                    } else {
                        // Regular directory, load it directly
                        self.load_package_files(&package_path);
                    }
                }
            }
        }

        fn load_package_files(&mut self, package_root: &Path) {
            // Load package files with limited depth to avoid loading entire dependency trees
            for entry in WalkDir::new(package_root)
                .follow_links(true) // Follow symlinks within the package
                .max_depth(5) // Load a bit deeper to get dist/ and lib/ directories
                .into_iter()
                .filter_map(Result::ok)
            {
                let path = entry.path();
                let Ok(metadata) = fs::metadata(path) else { continue };
                let abs_path = path.to_path_buf();

                if metadata.is_dir() {
                    self.directories.insert(abs_path.clone());
                    self.add_parent_directories(&abs_path);
                } else if metadata.is_file() {
                    // Only load essential file types
                    if let Some(ext) = path.extension() {
                        let ext_str = ext.to_str();
                        if matches!(
                            ext_str,
                            Some("json" | "js" | "mjs" | "cjs" | "ts" | "mts" | "cts" | "d.ts")
                        ) && let Ok(content) = fs::read(path)
                        {
                            self.files.insert(abs_path.clone(), content);
                            self.add_parent_directories(&abs_path);
                        }
                    } else if path.file_name() == Some(std::ffi::OsStr::new("package.json")) {
                        // Also load package.json even if extension check fails
                        if let Ok(content) = fs::read(path) {
                            self.files.insert(abs_path.clone(), content);
                            self.add_parent_directories(&abs_path);
                        }
                    }
                }
            }
        }

        fn create_symlink_fixtures(&mut self, cwd: &Path) {
            // Create temp_symlinks directory
            let temp_path = cwd.join("fixtures/enhanced-resolve/test/temp_symlinks");
            self.directories.insert(temp_path.clone());
            self.add_parent_directories(&temp_path);

            // Create index.js
            let index_path = temp_path.join("index.js");
            self.files.insert(index_path, b"console.log('Hello, World!')".to_vec());

            // Create 10000 symlinks pointing to index.js
            // These are created in memory during initialization, not during benchmark execution
            for i in 0..10000 {
                let symlink_path = temp_path.join(format!("file{i}.js"));
                self.symlinks.insert(symlink_path, PathBuf::from("index.js"));
            }
        }
    }

    impl Default for BenchMemoryFS {
        fn default() -> Self {
            Self::new()
        }
    }

    impl FileSystem for BenchMemoryFS {
        #[cfg(not(feature = "yarn_pnp"))]
        fn new() -> Self {
            Self::default()
        }

        #[cfg(feature = "yarn_pnp")]
        fn new(_yarn_pnp: bool) -> Self {
            Self::default()
        }

        fn read(&self, path: &Path) -> io::Result<Vec<u8>> {
            // Try direct lookup first
            if let Some(bytes) = self.files.get(path) {
                return Ok(bytes.clone());
            }

            // Try following symlinks
            let mut current = path.to_path_buf();
            let mut visited = FxHashSet::default();

            while let Some(target) = self.symlinks.get(&current) {
                if !visited.insert(current.clone()) {
                    return Err(io::Error::other("Circular symlink"));
                }

                current = if target.is_relative() {
                    current.parent().unwrap().join(target)
                } else {
                    target.clone()
                };

                if let Some(bytes) = self.files.get(&current) {
                    return Ok(bytes.clone());
                }
            }

            Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!("File not found: {}", path.display()),
            ))
        }

        fn read_to_string(&self, path: &Path) -> io::Result<String> {
            let bytes = self.read(path)?;
            String::from_utf8(bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
        }

        fn metadata(&self, path: &Path) -> io::Result<FileMetadata> {
            // Check if it's a file (direct)
            if self.files.contains_key(path) {
                return Ok(FileMetadata::new(true, false, false));
            }

            // Check if it's a directory (direct)
            if self.directories.contains(path) {
                return Ok(FileMetadata::new(false, true, false));
            }

            // Follow symlinks to find the target
            let mut current = path.to_path_buf();
            let mut visited = FxHashSet::default();

            while let Some(target) = self.symlinks.get(&current) {
                if !visited.insert(current.clone()) {
                    return Err(io::Error::other("Circular symlink"));
                }

                current = if target.is_relative() {
                    current.parent().unwrap().join(target)
                } else {
                    target.clone()
                };

                if self.files.contains_key(&current) {
                    return Ok(FileMetadata::new(true, false, false));
                } else if self.directories.contains(&current) {
                    return Ok(FileMetadata::new(false, true, false));
                }
            }

            Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!("Path not found: {}", path.display()),
            ))
        }

        fn symlink_metadata(&self, path: &Path) -> io::Result<FileMetadata> {
            // Check if it's a symlink first (before resolving)
            if self.symlinks.contains_key(path) {
                return Ok(FileMetadata::new(false, false, true));
            }

            // Otherwise, fall back to regular metadata
            self.metadata(path)
        }

        fn read_link(&self, path: &Path) -> Result<PathBuf, ResolveError> {
            self.symlinks.get(path).cloned().ok_or_else(|| {
                ResolveError::from(io::Error::new(
                    io::ErrorKind::NotFound,
                    format!("Not a symlink: {}", path.display()),
                ))
            })
        }

        fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
            // Follow symlinks to resolve the canonical path
            let mut current = path.to_path_buf();
            let mut visited = FxHashSet::default();

            while let Some(target) = self.symlinks.get(&current) {
                if !visited.insert(current.clone()) {
                    return Err(io::Error::other("Circular symlink"));
                }

                current = if target.is_relative() {
                    current.parent().unwrap().join(target)
                } else {
                    target.clone()
                };
            }

            // Verify the final path exists
            if self.files.contains_key(&current) || self.directories.contains(&current) {
                Ok(current)
            } else {
                Err(io::Error::new(
                    io::ErrorKind::NotFound,
                    format!("Path not found: {}", path.display()),
                ))
            }
        }
    }
}