Repository: oxc-project/oxc-resolver
Pull request #168 (2024-05-27 09:41:11)

fix: alias not found should return error
Not found matched alias should return error instead of continue resolving

https://github.com/webpack/enhanced-resolve/blob/570337b969eee46120a18b62b72809a3246147da/lib/AliasPlugin.js#L134-L135

codspeed-hq[bot]: ## [CodSpeed Performance Report](https://codspeed.io/oxc-project/oxc-resolver/branches/ahabhgk:fix-alias-not-found)

### Merging #168 will **not alter performance**

<sub>Comparing <code>ahabhgk:fix-alias-not-found</code> (9a88d87) with <code>main</code> (b766a06)</sub>



### Summary

`✅ 2` untouched benchmarks






Patch:
diff --git a/src/lib.rs b/src/lib.rs
index 8cc2831..f4203c7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -871,6 +871,10 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
                 }
                 alias_key_raw
             };
+            // It should stop resolving when all of the tried alias values
+            // failed to resolve.
+            // <https://github.com/webpack/enhanced-resolve/blob/570337b969eee46120a18b62b72809a3246147da/lib/AliasPlugin.js#L65>
+            let mut should_stop = false;
             for r in specifiers {
                 match r {
                     AliasValue::Path(alias_value) => {
@@ -885,6 +889,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
                                 alias_value, // pass in original alias value, not parsed
                                 specifier,
                                 ctx,
+                                &mut should_stop,
                             )? {
                                 return Ok(Some(path));
                             }
@@ -900,6 +905,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
                             new_specifier.path(), // pass in parsed alias value
                             specifier,
                             ctx,
+                            &mut should_stop,
                         )? {
                             return Ok(Some(path));
                         }
@@ -911,6 +917,9 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
                     }
                 }
             }
+            if should_stop {
+                return Err(ResolveError::NotFound(specifier.to_string()));
+            }
         }
         Ok(None)
     }
@@ -922,6 +931,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
         alias_value: &str,
         request: &str,
         ctx: &mut Ctx,
+        should_stop: &mut bool,
     ) -> ResolveResult {
         if request != alias_value
             && !request.strip_prefix(alias_value).is_some_and(|prefix| prefix.starts_with('/'))
@@ -944,6 +954,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
                 Cow::Owned(normalized.to_string_lossy().to_string())
             };
 
+            *should_stop = true;
             ctx.with_fully_specified(false);
             return match self.require(cached_path, new_specifier.as_ref(), ctx) {
                 Err(ResolveError::NotFound(_)) => Ok(None),


Test patch:
diff --git a/src/tests/alias.rs b/src/tests/alias.rs
index c7f7129..84e5c73 100644
--- a/src/tests/alias.rs
+++ b/src/tests/alias.rs
@@ -218,3 +218,18 @@ fn alias_is_full_path() {
         }
     }
 }
+
+// For the `should_stop` variable in `load_alias`
+#[test]
+fn all_alias_values_are_not_found() {
+    let f = super::fixture();
+    let resolver = Resolver::new(ResolveOptions {
+        alias: vec![(
+            "m1".to_string(),
+            vec![AliasValue::Path(f.join("node_modules").join("m2").to_string_lossy().to_string())],
+        )],
+        ..ResolveOptions::default()
+    });
+    let resolution = resolver.resolve(&f, "m1/a.js");
+    assert_eq!(resolution, Err(ResolveError::NotFound("m1/a.js".to_string())));
+}