Skip to content

Commit 52f3a25

Browse files
authored
feat: show tried extension aliases in ResolveError::ExtensionAlias (#251)
It is now displayed as `Cannot resolve 'index.mjs' with extension aliases 'index.mts' in ...`
1 parent 88caae4 commit 52f3a25

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

src/error.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ pub enum ResolveError {
4747
Builtin(String),
4848

4949
/// All of the aliased extension are not found
50-
#[error("All of the aliased extensions are not found for {0}")]
51-
ExtensionAlias(PathBuf),
50+
///
51+
/// Displays `Cannot resolve 'index.mjs' with extension aliases 'index.mts' in ...`
52+
#[error("Cannot resolve '{0}' for extension aliases '{1}' in '{2}'")]
53+
ExtensionAlias(
54+
/* File name */ String,
55+
/* Tried file names */ String,
56+
/* Path to dir */ PathBuf,
57+
),
5258

5359
/// The provided path specifier cannot be parsed
5460
#[error("{0}")]

src/lib.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,11 +1047,12 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
10471047
else {
10481048
return Ok(None);
10491049
};
1050-
let path = cached_path.path().with_extension("");
1051-
let path = path.as_os_str();
1050+
let path = cached_path.path();
1051+
let Some(filename) = path.file_name() else { return Ok(None) };
1052+
let path_without_extension = path.with_extension("");
10521053
ctx.with_fully_specified(true);
10531054
for extension in extensions {
1054-
let mut path_with_extension = path.to_os_string();
1055+
let mut path_with_extension = path_without_extension.clone().into_os_string();
10551056
path_with_extension.reserve_exact(extension.len());
10561057
path_with_extension.push(extension);
10571058
let cached_path = self.cache.value(Path::new(&path_with_extension));
@@ -1065,7 +1066,16 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
10651066
return Ok(Some(path));
10661067
}
10671068
}
1068-
Err(ResolveError::ExtensionAlias(cached_path.to_path_buf()))
1069+
// Create a meaningful error message.
1070+
let dir = path.parent().unwrap().to_path_buf();
1071+
let filename_without_extension = Path::new(filename).with_extension("");
1072+
let filename_without_extension = filename_without_extension.to_string_lossy();
1073+
let files = extensions
1074+
.iter()
1075+
.map(|ext| format!("{filename_without_extension}{ext}"))
1076+
.collect::<Vec<_>>()
1077+
.join(",");
1078+
Err(ResolveError::ExtensionAlias(filename.to_string_lossy().to_string(), files, dir))
10691079
}
10701080

10711081
/// enhanced-resolve: RootsPlugin

src/tests/exports_field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn extension_alias_throw_error() {
269269
let fail = [
270270
// enhanced-resolve has two test cases that are exactly the same here
271271
// https://github.com/webpack/enhanced-resolve/blob/a998c7d218b7a9ec2461fc4fddd1ad5dd7687485/test/exportsField.test.js#L2976-L3024
272-
("should throw error with the `extensionAlias` option", f.clone(), "pkg/string.js", ResolveError::ExtensionAlias(f.join("node_modules/pkg/dist/string.js"))),
272+
("should throw error with the `extensionAlias` option", f.clone(), "pkg/string.js", ResolveError::ExtensionAlias("string.js".into(), "string.ts".into(), f.join("node_modules/pkg/dist"))),
273273
// TODO: The error is PackagePathNotExported in enhanced-resolve
274274
// ("should throw error with the `extensionAlias` option", f.clone(), "pkg/string.js", ResolveError::PackagePathNotExported("node_modules/pkg/dist/string.ts".to_string())),
275275
];

src/tests/extension_alias.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,10 @@ fn extension_alias() {
2929
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
3030
}
3131

32-
#[rustfmt::skip]
33-
let fail = [
34-
("should not allow to fallback to the original extension or add extensions", f.clone(), "./index.mjs"),
35-
];
36-
37-
for (comment, path, request) in fail {
38-
let resolution = resolver.resolve(&path, request);
39-
assert_eq!(
40-
resolution,
41-
Err(ResolveError::ExtensionAlias(f.join(request))),
42-
"{comment} {path:?} {request}"
43-
);
44-
}
32+
// should not allow to fallback to the original extension or add extensions
33+
let resolution = resolver.resolve(&f, "./index.mjs").unwrap_err();
34+
let expected = ResolveError::ExtensionAlias("index.mjs".into(), "index.mts".into(), f);
35+
assert_eq!(resolution, expected);
4536
}
4637

4738
// should not apply extension alias to extensions or mainFiles field

0 commit comments

Comments
 (0)