From cfc0c806420cd44ea4422f6e6b83771a9597748a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 27 Jul 2023 12:27:01 -0400 Subject: [PATCH] fix(node): package path not exported error - add if types resolution was occurring (#19963) --- ext/node/errors.rs | 41 +++++++++++++++++++++++++++++++++++++++-- ext/node/resolution.rs | 7 ++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ext/node/errors.rs b/ext/node/errors.rs index bad6696bae..e0f02725fc 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, + 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'") + ); + } +} diff --git a/ext/node/resolution.rs b/ext/node/resolution.rs index 143edb6fe1..ce02f31f27 100644 --- a/ext/node/resolution.rs +++ b/ext/node/resolution.rs @@ -367,7 +367,7 @@ impl NodeResolver { permissions, ) .with_context(|| { - format!("Error resolving package config for '{reference}'") + format!("Failed resolving package config for '{reference}'") })?; let resolved_path = match maybe_resolved_path { Some(resolved_path) => resolved_path, @@ -933,6 +933,7 @@ impl NodeResolver { package_subpath, package_json_path, referrer, + mode, )); } return Ok(resolved.unwrap()); @@ -994,6 +995,7 @@ impl NodeResolver { package_subpath, package_json_path, referrer, + mode, )); } } @@ -1002,6 +1004,7 @@ impl NodeResolver { package_subpath, package_json_path, referrer, + mode, )) } @@ -1447,11 +1450,13 @@ fn throw_exports_not_found( subpath: String, package_json_path: &Path, referrer: &ModuleSpecifier, + mode: NodeResolutionMode, ) -> AnyError { errors::err_package_path_not_exported( package_json_path.parent().unwrap().display().to_string(), subpath, Some(to_specifier_display_string(referrer)), + mode, ) }