mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 04:48:52 -05:00
fix(node): package path not exported error - add if types resolution was occurring (#19963)
This commit is contained in:
parent
02d6bbff2c
commit
cfc0c80642
2 changed files with 45 additions and 3 deletions
|
@ -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'")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue