diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-07-27 12:27:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-27 16:27:01 +0000 |
commit | cfc0c806420cd44ea4422f6e6b83771a9597748a (patch) | |
tree | 9dbdcbae07e83e1632e595d2d711e0eead42b819 /ext/node/errors.rs | |
parent | 02d6bbff2c72817c9a9d6346b4bfd8ed25379ea3 (diff) |
fix(node): package path not exported error - add if types resolution was occurring (#19963)
Diffstat (limited to 'ext/node/errors.rs')
-rw-r--r-- | ext/node/errors.rs | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/ext/node/errors.rs b/ext/node/errors.rs index bad6696ba..e0f02725f 100644 --- a/ext/node/errors.rs +++ b/ext/node/errors.rs @@ -7,6 +7,8 @@ use deno_core::error::type_error; use deno_core::error::AnyError; use deno_core::url::Url; +use crate::NodeResolutionMode; + pub fn err_invalid_module_specifier( request: &str, reason: &str, @@ -95,6 +97,7 @@ pub fn err_package_path_not_exported( mut pkg_path: String, subpath: String, maybe_referrer: Option<String>, + mode: NodeResolutionMode, ) -> AnyError { let mut msg = "[ERR_PACKAGE_PATH_NOT_EXPORTED]".to_string(); @@ -111,11 +114,15 @@ pub fn err_package_path_not_exported( } } + let types_msg = match mode { + NodeResolutionMode::Execution => String::new(), + NodeResolutionMode::Types => " for types".to_string(), + }; if subpath == "." { msg = - format!("{msg} No \"exports\" main defined in '{pkg_path}package.json'"); + format!("{msg} No \"exports\" main defined{types_msg} in '{pkg_path}package.json'"); } else { - msg = format!("{msg} Package subpath '{subpath}' is not defined by \"exports\" in '{pkg_path}package.json'"); + msg = format!("{msg} Package subpath '{subpath}' is not defined{types_msg} by \"exports\" in '{pkg_path}package.json'"); }; if let Some(referrer) = maybe_referrer { @@ -160,3 +167,33 @@ pub fn err_unsupported_esm_url_scheme(url: &Url) -> AnyError { msg = format!("{}. Received protocol '{}'", msg, url.scheme()); generic_error(msg) } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn types_resolution_package_path_not_exported() { + let separator_char = if cfg!(windows) { '\\' } else { '/' }; + assert_eq!( + err_package_path_not_exported( + "test_path".to_string(), + "./jsx-runtime".to_string(), + None, + NodeResolutionMode::Types, + ) + .to_string(), + format!("[ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './jsx-runtime' is not defined for types by \"exports\" in 'test_path{separator_char}package.json'") + ); + assert_eq!( + err_package_path_not_exported( + "test_path".to_string(), + ".".to_string(), + None, + NodeResolutionMode::Types, + ) + .to_string(), + format!("[ERR_PACKAGE_PATH_NOT_EXPORTED] No \"exports\" main defined for types in 'test_path{separator_char}package.json'") + ); + } +} |