oxc_resolver 11.21.1

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
#[cfg(target_os = "windows")]
use crate::PathUtil;
#[cfg(target_os = "windows")]
use crate::tests::windows::get_dos_device_path;
use std::path::PathBuf;
use std::{fs, io, path::Path};

use walkdir::WalkDir;

use super::fixture_root;
use crate::{ResolveOptions, Resolver};

#[derive(Debug, Clone, Copy)]
enum FileType {
    File,
    Dir,
}

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

    #[cfg(target_os = "windows")]
    match file_type {
        // NOTE: original path should use `\` instead of `/` for relative paths
        //       otherwise the symlink will be broken and the test will fail with InvalidFilename error
        FileType::File => std::os::windows::fs::symlink_file(original.as_ref().normalize(), link),
        FileType::Dir => std::os::windows::fs::symlink_dir(original.as_ref().normalize(), link),
    }
    #[cfg(target_family = "wasm")]
    {
        Err(io::Error::new(io::ErrorKind::Other, "not supported"))
    }
}

fn init(dirname: &Path, temp_path: &Path) -> io::Result<()> {
    if temp_path.exists() {
        _ = fs::remove_dir_all(temp_path);
    }
    fs::create_dir(temp_path)?;
    symlink(dirname.join("../lib/index.js"), temp_path.join("test"), FileType::File)?;
    symlink(dirname.join("../lib"), temp_path.join("test2"), FileType::Dir)?;
    fs::remove_dir_all(temp_path)
}

fn create_symlinks(dirname: &Path, temp_path: &Path) -> io::Result<()> {
    fs::create_dir(temp_path)?;
    symlink(
        dirname.join("../lib/index.js").canonicalize()?,
        temp_path.join("index.js"),
        FileType::File,
    )?;
    symlink(dirname.join("../lib").canonicalize().unwrap(), temp_path.join("lib"), FileType::Dir)?;
    symlink(dirname.join("..").canonicalize().unwrap(), temp_path.join("this"), FileType::Dir)?;
    symlink(temp_path.join("this"), temp_path.join("that"), FileType::Dir)?;
    symlink(Path::new("../../lib/index.js"), temp_path.join("node.relative.js"), FileType::File)?;
    symlink(
        Path::new("./node.relative.js"),
        temp_path.join("node.relative.sym.js"),
        FileType::File,
    )?;

    #[cfg(target_os = "windows")]
    {
        // Ideally we should point to a Volume that does not have a drive letter.
        // However, it's not trivial to create a Volume in CI environment.
        // Here we are just picking up any Volume, as resolver itself is not calling `fs::canonicalize`,
        // which potentially can resolve the Volume GUID into driver letter whenever possible.
        let dos_device_temp_path = get_dos_device_path(temp_path).unwrap();
        symlink(
            dos_device_temp_path.join(r"..\..\lib"),
            temp_path.join("device_path_lib"),
            FileType::Dir,
        )?;
        symlink(
            dos_device_temp_path.join(r"..\..\lib\index.js"),
            temp_path.join("device_path_index.js"),
            FileType::File,
        )?;
    }

    Ok(())
}

fn cleanup_symlinks(temp_path: &Path) {
    _ = fs::remove_dir_all(temp_path);
}

struct SymlinkFixturePaths {
    root: PathBuf,
    temp_path: PathBuf,
}

/// Prepares symlinks for the test.
/// Specify a different `temp_path_segment` for each test to avoid conflicts when tests are executed concurrently.
/// Returns `Ok(None)` if the symlink fixtures cannot be created at all (usually due to a lack of permission).
/// Returns `Ok(Some(_))` if the symlink fixtures are created successfully, or already exist.
/// Returns `Err(_)` if there is error creating the symlinks.
fn prepare_symlinks<P: AsRef<Path>>(
    temp_path_segment: P,
) -> io::Result<Option<SymlinkFixturePaths>> {
    let root = super::fixture_root().join("enhanced-resolve");
    let dirname = root.join("test");
    let temp_path = dirname.join(temp_path_segment.as_ref());
    if !temp_path.exists() {
        if let Err(err) = init(&dirname, &temp_path) {
            println!(
                "Skipped test: Failed to create symlinks. You may need administrator privileges. Error: {err}"
            );
            return Ok(None);
        }
        if let Err(err) = create_symlinks(&dirname, &temp_path) {
            cleanup_symlinks(&temp_path);
            return Err(err);
        }
    }

    Ok(Some(SymlinkFixturePaths { root, temp_path }))
}

#[test]
#[cfg_attr(target_family = "wasm", ignore)]
fn test() {
    let Some(SymlinkFixturePaths { root, temp_path }) = prepare_symlinks("temp").unwrap() else {
        return;
    };
    let resolver_without_symlinks =
        Resolver::new(ResolveOptions { symlinks: false, ..ResolveOptions::default() });
    let resolver_with_symlinks = Resolver::default();

    #[rustfmt::skip]
    let pass = [
        ("with a symlink to a file", temp_path.clone(), "./index.js"),
        ("with a relative symlink to a file", temp_path.clone(), "./node.relative.js"),
        ("with a relative symlink to a symlink to a file", temp_path.clone(), "./node.relative.sym.js"),
        ("with a symlink to a directory 1", temp_path.clone(), "./lib/index.js"),
        ("with a symlink to a directory 2", temp_path.clone(), "./this/lib/index.js"),
        ("with multiple symlinks in the path 1", temp_path.clone(), "./this/test/temp/index.js"),
        ("with multiple symlinks in the path 2", temp_path.clone(), "./this/test/temp/lib/index.js"),
        ("with multiple symlinks in the path 3", temp_path.clone(), "./this/test/temp/this/lib/index.js"),
        ("with a symlink to a directory 2 (chained)", temp_path.clone(), "./that/lib/index.js"),
        ("with multiple symlinks in the path 1 (chained)", temp_path.clone(), "./that/test/temp/index.js"),
        ("with multiple symlinks in the path 2 (chained)", temp_path.clone(), "./that/test/temp/lib/index.js"),
        ("with multiple symlinks in the path 3 (chained)", temp_path.clone(), "./that/test/temp/that/lib/index.js"),
        ("with symlinked directory as context 1", temp_path.join( "lib"), "./index.js"),
        ("with symlinked directory as context 2", temp_path.join( "this"), "./lib/index.js"),
        ("with symlinked directory as context and in path", temp_path.join( "this"), "./test/temp/lib/index.js"),
        ("with symlinked directory in context path", temp_path.join( "this/lib"), "./index.js"),
        ("with symlinked directory in context path and symlinked file", temp_path.join( "this/test"), "./temp/index.js"),
        ("with symlinked directory in context path and symlinked directory", temp_path.join( "this/test"), "./temp/lib/index.js"),
        ("with symlinked directory as context 2 (chained)", temp_path.join( "that"), "./lib/index.js"),
        ("with symlinked directory as context and in path (chained)", temp_path.join( "that"), "./test/temp/lib/index.js"),
        ("with symlinked directory in context path (chained)", temp_path.join( "that/lib"), "./index.js"),
        ("with symlinked directory in context path and symlinked file (chained)", temp_path.join( "that/test"), "./temp/index.js"),
        ("with symlinked directory in context path and symlinked directory (chained)", temp_path.join( "that/test"), "./temp/lib/index.js")
    ];

    for (comment, path, request) in pass {
        let filename = resolver_with_symlinks.resolve(&path, request).map(|r| r.full_path());
        assert_eq!(filename, Ok(root.join("lib/index.js")), "{comment:?}");

        let resolved_path =
            resolver_without_symlinks.resolve(&path, request).map(|r| r.full_path());
        assert_eq!(resolved_path, Ok(path.join(request)));
    }
}

#[cfg(target_os = "windows")]
#[test]
fn test_unsupported_targets() {
    use crate::ResolveError;

    let Some(SymlinkFixturePaths { root, temp_path }) =
        prepare_symlinks("temp.test_unsupported_targets").unwrap()
    else {
        return;
    };
    let resolver_with_symlinks = Resolver::default();

    // Symlinks pointing to unsupported DOS device paths are not followed, as if `symlinks = false`.
    // See doc of `ResolveOptions::symlinks` for details.
    // They are treated as if they are ordinary files and folders.
    // FIXME: these tests does no pass
    // assert_eq!(
    //     resolver_with_symlinks.resolve(&temp_path, "./device_path_lib").unwrap().full_path(),
    //     temp_path.join("device_path_lib/index.js"),
    // );
    // assert_eq!(
    //     resolver_with_symlinks.resolve(&temp_path, "./device_path_index.js").unwrap().full_path(),
    //     temp_path.join("device_path_index.js"),
    // );

    // UB if the resolution starts at a directory with unsupported DOS device path. Don't do this.
    // While we haven't set up any convention on this, de facto behavior for now is
    // * if there is `package.json` in the ancestor, a `ResolveError::PathNotSupported` will be returned
    //   from `FsCachedPath::find_package_json` when trying to canonicalize the full path of `package.json`.
    // * Otherwise, a `ResolveError::NotFound` will be returned.
    let dos_device_temp_path = get_dos_device_path(&temp_path).unwrap();
    let dos_device_root = get_dos_device_path(&root).unwrap();
    assert_eq!(
        resolver_with_symlinks.resolve(&dos_device_temp_path, "./index.js"),
        Err(ResolveError::PathNotSupported(dos_device_root))
    );
}

#[test]
fn test_circular_symlink() {
    let Some(SymlinkFixturePaths { root: _, temp_path }) =
        prepare_symlinks("temp.test_circular_symlink").unwrap()
    else {
        return;
    };

    // Create a circular symlink: link1 -> link2 -> link1
    let link1_path = temp_path.join("link1");
    let link2_path = temp_path.join("link2");

    if symlink(&link2_path, &link1_path, FileType::File).is_err() {
        // Skip test if we can't create symlinks
        return;
    }
    if symlink(&link1_path, &link2_path, FileType::File).is_err() {
        // Skip test if we can't create symlinks
        _ = fs::remove_file(&link1_path);
        return;
    }

    let resolver = Resolver::default();
    let result = resolver.resolve(&temp_path, "./link1");

    // Should error due to circular symlink
    assert!(result.is_err());

    // Cleanup
    _ = fs::remove_file(&link1_path);
    _ = fs::remove_file(&link2_path);
}

// ----------------------------------------------------------------------------
// `node_modules` canonicalization: assert the resolver's canonicalization equals
// `std::fs::canonicalize` across package-manager layouts and symlink shapes.
// ----------------------------------------------------------------------------

const COMBOS: &[&str] = &[
    "npm-flat",
    "pnpm-isolated",
    "pnpm-hoisted",
    "yarn-flat",
    "yarn-isolated",
    "yarn-pnp",
    "bun-flat",
    "bun-isolated",
];

/// Walk every entry under each installed `fixtures/bench-pm/installs/<combo>/node_modules` tree
/// (all npm/pnpm/yarn/bun × flat/isolated/hoisted/pnp layouts) and assert the resolver's
/// canonicalization equals `std::fs::canonicalize`, including the symlinks (and, on Windows,
/// junctions) into isolated virtual stores and the `.bin` shim directory. The fixtures are produced
/// by `just install-bench-fixtures` and are not committed, so combos that are not installed are
/// skipped.
#[test]
fn canonicalize_matches_os_for_all_node_modules() {
    let installs = fixture_root().join("bench-pm").join("installs");
    let mut combos_checked = 0u32;
    let mut paths_checked = 0u32;

    for combo in COMBOS {
        let node_modules = installs.join(combo).join("node_modules");
        if !node_modules.is_dir() {
            continue;
        }
        combos_checked += 1;
        let resolver = Resolver::new(ResolveOptions::default());

        for entry in WalkDir::new(&node_modules).follow_links(false) {
            let Ok(entry) = entry else { continue };
            let path = entry.path();
            // Ground truth from the OS. Skip broken symlinks / unreadable entries.
            let Ok(expected) = fs::canonicalize(path) else { continue };
            // `std::fs::canonicalize` returns a `\\?\` verbatim prefix on Windows; the resolver
            // strips it (`strip_windows_prefix`), so run the oracle through the same strip to
            // compare the two in the same representation.
            #[cfg(target_os = "windows")]
            let Ok(expected) = crate::windows::strip_windows_prefix(expected) else { continue };
            let cached = resolver.cache.value(path);
            let actual = resolver.cache.canonicalize(&cached).unwrap_or_else(|err| {
                panic!("{combo}: resolver canonicalize({}) failed: {err}", path.display())
            });
            assert_eq!(actual, expected, "{combo}: canonicalize mismatch for {}", path.display());
            paths_checked += 1;
        }
    }

    if combos_checked == 0 {
        eprintln!(
            "skip canonicalize_matches_os_for_all_node_modules: no bench-pm installs found \
             (run `just install-bench-fixtures`)"
        );
        return;
    }
    eprintln!("checked {paths_checked} paths across {combos_checked} bench-pm combos");
}

/// A symlinked workspace package anchor with a symlink in the suffix below it: canonicalization must
/// follow both the anchor link and the inner link.
#[test]
fn symlinked_package_anchor_walks_suffix_symlinks() {
    let root = fixture_root().join("node_modules-canonicalize/symlinked-workspace");
    let anchor = root.join("node_modules/pkg");
    let inner_link = anchor.join("src/link");

    if !fs::symlink_metadata(&anchor).is_ok_and(|m| m.is_symlink())
        || !fs::symlink_metadata(&inner_link).is_ok_and(|m| m.is_symlink())
    {
        eprintln!("skip symlinked_package_anchor_walks_suffix_symlinks: symlinks unavailable");
        return;
    }

    let path = inner_link.join("file.js");
    let expected = fs::canonicalize(&path).unwrap();
    #[cfg(target_os = "windows")]
    let expected = crate::windows::strip_windows_prefix(expected).unwrap();

    let resolver = Resolver::new(ResolveOptions::default());
    let cached = resolver.cache.value(&path);
    let actual = resolver.cache.canonicalize(&cached).unwrap();

    assert_eq!(actual, expected);
    assert_eq!(expected, root.join("packages/pkg/real/file.js"));
}

/// A package in a recognized (lockfile-marked) flat layout can still ship a symlink below its real
/// `node_modules/<pkg>` anchor — a directory link (`lib -> dist`) or a re-export file link.
/// Canonicalization must follow it, not append the suffix across it.
#[test]
fn real_package_anchor_walks_internal_symlinks() {
    let root = fixture_root().join("node_modules-canonicalize/internal-symlink");
    let dir_link = root.join("node_modules/pkg/lib");
    let file_link = root.join("node_modules/pkg/reexport.js");

    if !fs::symlink_metadata(&dir_link).is_ok_and(|m| m.is_symlink())
        || !fs::symlink_metadata(&file_link).is_ok_and(|m| m.is_symlink())
    {
        eprintln!("skip real_package_anchor_walks_internal_symlinks: symlinks unavailable");
        return;
    }

    let real = root.join("node_modules/pkg/real/file.js");
    let resolver = Resolver::new(ResolveOptions::default());
    for path in [dir_link.join("file.js"), file_link] {
        let expected = fs::canonicalize(&path).unwrap();
        #[cfg(target_os = "windows")]
        let expected = crate::windows::strip_windows_prefix(expected).unwrap();
        let cached = resolver.cache.value(&path);
        let actual = resolver.cache.canonicalize(&cached).unwrap();
        assert_eq!(actual, expected, "{}", path.display());
        assert_eq!(expected, real, "{}", path.display());
    }
}

/// A monorepo with a version conflict: the root resolves `dep@2` while a workspace package nests
/// its own `dep@1` (an isolated-store symlink). Canonicalization must pick each package's own
/// deepest `node_modules/<pkg>` anchor and follow it to the correct store version. Every path is
/// compared against `std::fs::canonicalize`, walking through symlinked directories too.
#[test]
fn nested_monorepo_canonicalize_matches_os() {
    let root = fixture_root().join("node_modules-canonicalize/nested-monorepo");
    let nested = root.join("packages/ui/node_modules/dep");
    if !fs::symlink_metadata(&nested).is_ok_and(|m| m.is_symlink()) {
        eprintln!("skip nested_monorepo_canonicalize_matches_os: symlinks unavailable");
        return;
    }

    let resolver = Resolver::new(ResolveOptions::default());
    let walk = WalkDir::new(&root)
        .follow_links(false)
        .into_iter()
        .chain(WalkDir::new(&root).follow_links(true).max_depth(12));
    for entry in walk {
        let Ok(entry) = entry else { continue };
        let path = entry.path();
        let Ok(expected) = fs::canonicalize(path) else { continue };
        #[cfg(target_os = "windows")]
        let Ok(expected) = crate::windows::strip_windows_prefix(expected) else { continue };
        let cached = resolver.cache.value(path);
        let actual = resolver.cache.canonicalize(&cached).unwrap();
        assert_eq!(actual, expected, "canonicalize mismatch for {}", path.display());
    }

    // The conflicting versions resolve to their respective stores.
    let resolve = |p: &Path| {
        let cached = resolver.cache.value(p);
        resolver.cache.canonicalize(&cached).unwrap()
    };
    assert_eq!(
        resolve(&nested.join("index.js")),
        root.join("node_modules/.pnpm/dep@1.0.0/node_modules/dep/index.js")
    );
    assert_eq!(
        resolve(&root.join("node_modules/dep/index.js")),
        root.join("node_modules/.pnpm/dep@2.0.0/node_modules/dep/index.js")
    );
}