fix(node): use JsErrorClass for code property on node resolver errors (#29294)
Some checks are pending
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / build wasm32 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions

Superseeds #26631

---------

Signed-off-by: Leo Kettmeir <crowlkats@toaxl.com>
Co-authored-by: David Sherret <dsherret@gmail.com>
This commit is contained in:
Leo Kettmeir 2025-05-16 20:54:47 +02:00 committed by GitHub
parent 744eaa956d
commit 97b84669a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 60 additions and 15 deletions

8
Cargo.lock generated
View file

@ -1879,9 +1879,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_error" name = "deno_error"
version = "0.5.6" version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19fae9fe305307b5ef3ee4e8244c79cffcca421ab0ce8634dea0c6b1342f220f" checksum = "e983933fb4958fbe1e0a63c1e89a2af72b12c409e86404e547955564e6e217b8"
dependencies = [ dependencies = [
"deno_error_macro", "deno_error_macro",
"libc", "libc",
@ -1893,9 +1893,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_error_macro" name = "deno_error_macro"
version = "0.5.6" version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5abb2556e91848b66f562451fcbcdee2a3b7c88281828908dcf7cca355f5d997" checksum = "a1ad5ae3ef15db33e917d6ed54b53d0a98d068c4d1217eb35a4997423203c3ef"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

@ -59,7 +59,7 @@ deno_core = { version = "0.347.0" }
deno_cache_dir = "=0.20.0" deno_cache_dir = "=0.20.0"
deno_config = { version = "=0.54.2", features = ["workspace"] } deno_config = { version = "=0.54.2", features = ["workspace"] }
deno_doc = "=0.173.0" deno_doc = "=0.173.0"
deno_error = "=0.5.6" deno_error = "=0.5.7"
deno_graph = { version = "=0.91.0", default-features = false } deno_graph = { version = "=0.91.0", default-features = false }
deno_lint = "=0.75.0" deno_lint = "=0.75.0"
deno_lockfile = "=0.28.0" deno_lockfile = "=0.28.0"

View file

@ -66,25 +66,31 @@ pub enum RequireErrorKind {
#[error(transparent)] #[error(transparent)]
Permission(#[inherit] JsErrorBox), Permission(#[inherit] JsErrorBox),
#[class(generic)] #[class(generic)]
#[properties(inherit)]
#[error(transparent)] #[error(transparent)]
PackageExportsResolve( PackageExportsResolve(
#[from] node_resolver::errors::PackageExportsResolveError, #[from] node_resolver::errors::PackageExportsResolveError,
), ),
#[class(generic)] #[class(generic)]
#[properties(inherit)]
#[error(transparent)] #[error(transparent)]
PackageJsonLoad(#[from] node_resolver::errors::PackageJsonLoadError), PackageJsonLoad(#[from] node_resolver::errors::PackageJsonLoadError),
#[class(generic)] #[class(generic)]
#[properties(inherit)]
#[error(transparent)] #[error(transparent)]
ClosestPkgJson(#[from] ClosestPkgJsonError), ClosestPkgJson(#[from] ClosestPkgJsonError),
#[class(generic)] #[class(generic)]
#[properties(inherit)]
#[error(transparent)] #[error(transparent)]
PackageImportsResolve( PackageImportsResolve(
#[from] node_resolver::errors::PackageImportsResolveError, #[from] node_resolver::errors::PackageImportsResolveError,
), ),
#[class(generic)] #[class(generic)]
#[properties(inherit)]
#[error(transparent)] #[error(transparent)]
FilePathConversion(#[from] deno_path_util::UrlToFilePathError), FilePathConversion(#[from] deno_path_util::UrlToFilePathError),
#[class(generic)] #[class(generic)]
#[properties(inherit)]
#[error(transparent)] #[error(transparent)]
UrlConversion(#[from] deno_path_util::PathToUrlError), UrlConversion(#[from] deno_path_util::PathToUrlError),
#[class(inherit)] #[class(inherit)]

View file

@ -71,6 +71,7 @@ pub trait NodeJsErrorCoded {
maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default() maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default()
)] )]
#[class(type)] #[class(type)]
#[property("code" = self.code())]
pub struct InvalidModuleSpecifierError { pub struct InvalidModuleSpecifierError {
pub request: String, pub request: String,
pub reason: Cow<'static, str>, pub reason: Cow<'static, str>,
@ -113,6 +114,7 @@ impl NodeJsErrorCoded for LegacyResolveError {
referrer_extra.as_ref().map(|r| format!(" ({})", r)).unwrap_or_default() referrer_extra.as_ref().map(|r| format!(" ({})", r)).unwrap_or_default()
)] )]
#[class(generic)] #[class(generic)]
#[property("code" = self.code())]
pub struct PackageNotFoundError { pub struct PackageNotFoundError {
pub package_name: String, pub package_name: String,
pub referrer: UrlOrPath, pub referrer: UrlOrPath,
@ -133,6 +135,7 @@ impl NodeJsErrorCoded for PackageNotFoundError {
referrer_extra.as_ref().map(|r| format!(" ({})", r)).unwrap_or_default() referrer_extra.as_ref().map(|r| format!(" ({})", r)).unwrap_or_default()
)] )]
#[class(generic)] #[class(generic)]
#[property("code" = self.code())]
pub struct ReferrerNotFoundError { pub struct ReferrerNotFoundError {
pub referrer: UrlOrPath, pub referrer: UrlOrPath,
/// Extra information about the referrer. /// Extra information about the referrer.
@ -148,6 +151,7 @@ impl NodeJsErrorCoded for ReferrerNotFoundError {
#[derive(Debug, Error, JsError)] #[derive(Debug, Error, JsError)]
#[class(inherit)] #[class(inherit)]
#[error("Failed resolving '{package_name}' from referrer '{referrer}'.")] #[error("Failed resolving '{package_name}' from referrer '{referrer}'.")]
#[property("code" = self.code())]
pub struct PackageFolderResolveIoError { pub struct PackageFolderResolveIoError {
pub package_name: String, pub package_name: String,
pub referrer: UrlOrPath, pub referrer: UrlOrPath,
@ -162,9 +166,9 @@ impl NodeJsErrorCoded for PackageFolderResolveIoError {
} }
} }
impl NodeJsErrorCoded for PackageFolderResolveError { impl NodeJsErrorCoded for PackageFolderResolveErrorKind {
fn code(&self) -> NodeJsErrorCode { fn code(&self) -> NodeJsErrorCode {
match self.as_kind() { match self {
PackageFolderResolveErrorKind::PackageNotFound(e) => e.code(), PackageFolderResolveErrorKind::PackageNotFound(e) => e.code(),
PackageFolderResolveErrorKind::ReferrerNotFound(e) => e.code(), PackageFolderResolveErrorKind::ReferrerNotFound(e) => e.code(),
PackageFolderResolveErrorKind::Io(e) => e.code(), PackageFolderResolveErrorKind::Io(e) => e.code(),
@ -191,6 +195,7 @@ pub enum PackageFolderResolveErrorKind {
Io(#[from] PackageFolderResolveIoError), Io(#[from] PackageFolderResolveIoError),
#[class(inherit)] #[class(inherit)]
#[error(transparent)] #[error(transparent)]
#[property("code" = self.code())]
PathToUrl(#[from] deno_path_util::PathToUrlError), PathToUrl(#[from] deno_path_util::PathToUrlError),
} }
@ -281,6 +286,7 @@ impl PackageSubpathResolveErrorKind {
NodeResolutionKind::Types => " for types", NodeResolutionKind::Types => " for types",
} }
)] )]
#[property("code" = self.code())]
pub struct PackageTargetNotFoundError { pub struct PackageTargetNotFoundError {
pub pkg_json_path: PathBuf, pub pkg_json_path: PathBuf,
pub target: String, pub target: String,
@ -295,9 +301,9 @@ impl NodeJsErrorCoded for PackageTargetNotFoundError {
} }
} }
impl NodeJsErrorCoded for PackageTargetResolveError { impl NodeJsErrorCoded for PackageTargetResolveErrorKind {
fn code(&self) -> NodeJsErrorCode { fn code(&self) -> NodeJsErrorCode {
match self.as_kind() { match self {
PackageTargetResolveErrorKind::NotFound(e) => e.code(), PackageTargetResolveErrorKind::NotFound(e) => e.code(),
PackageTargetResolveErrorKind::InvalidPackageTarget(e) => e.code(), PackageTargetResolveErrorKind::InvalidPackageTarget(e) => e.code(),
PackageTargetResolveErrorKind::InvalidModuleSpecifier(e) => e.code(), PackageTargetResolveErrorKind::InvalidModuleSpecifier(e) => e.code(),
@ -336,6 +342,7 @@ pub enum PackageTargetResolveErrorKind {
UnknownBuiltInNodeModule(#[from] UnknownBuiltInNodeModuleError), UnknownBuiltInNodeModule(#[from] UnknownBuiltInNodeModuleError),
#[class(inherit)] #[class(inherit)]
#[error(transparent)] #[error(transparent)]
#[property("code" = self.code())]
UrlToFilePath(#[from] deno_path_util::UrlToFilePathError), UrlToFilePath(#[from] deno_path_util::UrlToFilePathError),
} }
@ -348,9 +355,9 @@ impl PackageTargetResolveErrorKind {
} }
} }
impl NodeJsErrorCoded for PackageExportsResolveError { impl NodeJsErrorCoded for PackageExportsResolveErrorKind {
fn code(&self) -> NodeJsErrorCode { fn code(&self) -> NodeJsErrorCode {
match self.as_kind() { match self {
PackageExportsResolveErrorKind::PackagePathNotExported(e) => e.code(), PackageExportsResolveErrorKind::PackagePathNotExported(e) => e.code(),
PackageExportsResolveErrorKind::PackageTargetResolve(e) => e.code(), PackageExportsResolveErrorKind::PackageTargetResolve(e) => e.code(),
} }
@ -378,6 +385,7 @@ pub enum PackageExportsResolveErrorKind {
self.0.maybe_referrer.as_ref().map(|r| format!(" imported from '{}'", r)).unwrap_or_default(), self.0.maybe_referrer.as_ref().map(|r| format!(" imported from '{}'", r)).unwrap_or_default(),
)] )]
#[class(generic)] #[class(generic)]
#[property("code" = self.code())]
pub struct TypesNotFoundError(pub Box<TypesNotFoundErrorData>); pub struct TypesNotFoundError(pub Box<TypesNotFoundErrorData>);
#[derive(Debug)] #[derive(Debug)]
@ -398,6 +406,7 @@ impl NodeJsErrorCoded for TypesNotFoundError {
self.code(), self.code(),
self.0 self.0
)] )]
#[property("code" = self.code())]
pub struct PackageJsonLoadError( pub struct PackageJsonLoadError(
#[source] #[source]
#[from] #[from]
@ -437,6 +446,7 @@ pub enum ClosestPkgJsonErrorKind {
package_json_path.as_ref().map(|p| format!(" in package {}", p.display())).unwrap_or_default(), package_json_path.as_ref().map(|p| format!(" in package {}", p.display())).unwrap_or_default(),
maybe_referrer.as_ref().map(|r| format!(" imported from '{}'", r)).unwrap_or_default(), maybe_referrer.as_ref().map(|r| format!(" imported from '{}'", r)).unwrap_or_default(),
)] )]
#[property("code" = self.code())]
pub struct PackageImportNotDefinedError { pub struct PackageImportNotDefinedError {
pub name: String, pub name: String,
pub package_json_path: Option<PathBuf>, pub package_json_path: Option<PathBuf>,
@ -488,9 +498,9 @@ impl NodeJsErrorCoded for PackageImportsResolveErrorKind {
} }
} }
impl NodeJsErrorCoded for PackageResolveError { impl NodeJsErrorCoded for PackageResolveErrorKind {
fn code(&self) -> NodeJsErrorCode { fn code(&self) -> NodeJsErrorCode {
match self.as_kind() { match self {
PackageResolveErrorKind::ClosestPkgJson(e) => e.code(), PackageResolveErrorKind::ClosestPkgJson(e) => e.code(),
PackageResolveErrorKind::InvalidModuleSpecifier(e) => e.code(), PackageResolveErrorKind::InvalidModuleSpecifier(e) => e.code(),
PackageResolveErrorKind::PackageFolderResolve(e) => e.code(), PackageResolveErrorKind::PackageFolderResolve(e) => e.code(),
@ -525,6 +535,7 @@ pub enum PackageResolveErrorKind {
SubpathResolve(#[from] PackageSubpathResolveError), SubpathResolve(#[from] PackageSubpathResolveError),
#[class(inherit)] #[class(inherit)]
#[error(transparent)] #[error(transparent)]
#[property("code" = self.code())]
UrlToFilePath(#[from] UrlToFilePathError), UrlToFilePath(#[from] UrlToFilePathError),
} }
@ -633,12 +644,13 @@ pub enum FinalizeResolutionErrorKind {
UnsupportedDirImport(#[from] UnsupportedDirImportError), UnsupportedDirImport(#[from] UnsupportedDirImportError),
#[class(inherit)] #[class(inherit)]
#[error(transparent)] #[error(transparent)]
#[property("code" = self.code())]
UrlToFilePath(#[from] deno_path_util::UrlToFilePathError), UrlToFilePath(#[from] deno_path_util::UrlToFilePathError),
} }
impl NodeJsErrorCoded for FinalizeResolutionError { impl NodeJsErrorCoded for FinalizeResolutionErrorKind {
fn code(&self) -> NodeJsErrorCode { fn code(&self) -> NodeJsErrorCode {
match self.as_kind() { match self {
FinalizeResolutionErrorKind::InvalidModuleSpecifierError(e) => e.code(), FinalizeResolutionErrorKind::InvalidModuleSpecifierError(e) => e.code(),
FinalizeResolutionErrorKind::ModuleNotFound(e) => e.code(), FinalizeResolutionErrorKind::ModuleNotFound(e) => e.code(),
FinalizeResolutionErrorKind::UnsupportedDirImport(e) => e.code(), FinalizeResolutionErrorKind::UnsupportedDirImport(e) => e.code(),
@ -659,6 +671,7 @@ impl NodeJsErrorCoded for FinalizeResolutionError {
maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default(), maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default(),
suggested_ext.as_ref().map(|m| format!("\nDid you mean to import with the \".{}\" extension?", m)).unwrap_or_default() suggested_ext.as_ref().map(|m| format!("\nDid you mean to import with the \".{}\" extension?", m)).unwrap_or_default()
)] )]
#[property("code" = self.code())]
pub struct ModuleNotFoundError { pub struct ModuleNotFoundError {
pub specifier: UrlOrPath, pub specifier: UrlOrPath,
pub maybe_referrer: Option<UrlOrPath>, pub maybe_referrer: Option<UrlOrPath>,
@ -681,6 +694,7 @@ impl NodeJsErrorCoded for ModuleNotFoundError {
maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default(), maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default(),
suggested_file_name.map(|file_name| format!("\nDid you mean to import {file_name} within the directory?")).unwrap_or_default(), suggested_file_name.map(|file_name| format!("\nDid you mean to import {file_name} within the directory?")).unwrap_or_default(),
)] )]
#[property("code" = self.code())]
pub struct UnsupportedDirImportError { pub struct UnsupportedDirImportError {
pub dir_url: UrlOrPath, pub dir_url: UrlOrPath,
pub maybe_referrer: Option<UrlOrPath>, pub maybe_referrer: Option<UrlOrPath>,
@ -695,6 +709,7 @@ impl NodeJsErrorCoded for UnsupportedDirImportError {
#[derive(Debug, JsError)] #[derive(Debug, JsError)]
#[class(generic)] #[class(generic)]
#[property("code" = self.code())]
pub struct InvalidPackageTargetError { pub struct InvalidPackageTargetError {
pub pkg_json_path: PathBuf, pub pkg_json_path: PathBuf,
pub sub_path: String, pub sub_path: String,
@ -752,6 +767,7 @@ impl NodeJsErrorCoded for InvalidPackageTargetError {
#[derive(Debug, JsError)] #[derive(Debug, JsError)]
#[class(generic)] #[class(generic)]
#[property("code" = self.code())]
pub struct PackagePathNotExportedError { pub struct PackagePathNotExportedError {
pub pkg_json_path: PathBuf, pub pkg_json_path: PathBuf,
pub subpath: String, pub subpath: String,
@ -809,6 +825,7 @@ impl std::fmt::Display for PackagePathNotExportedError {
if cfg!(windows) && url_scheme.len() == 2 { " On Windows, absolute path must be valid file:// URLS."} else { "" }, if cfg!(windows) && url_scheme.len() == 2 { " On Windows, absolute path must be valid file:// URLS."} else { "" },
url_scheme url_scheme
)] )]
#[property("code" = self.code())]
pub struct UnsupportedEsmUrlSchemeError { pub struct UnsupportedEsmUrlSchemeError {
pub url_scheme: String, pub url_scheme: String,
} }
@ -845,6 +862,7 @@ pub enum ResolveBinaryCommandsError {
#[derive(Error, Debug, Clone, deno_error::JsError)] #[derive(Error, Debug, Clone, deno_error::JsError)]
#[class("NotFound")] #[class("NotFound")]
#[error("No such built-in module: node:{module_name}")] #[error("No such built-in module: node:{module_name}")]
#[property("code" = self.code())]
pub struct UnknownBuiltInNodeModuleError { pub struct UnknownBuiltInNodeModuleError {
/// Name of the invalid module. /// Name of the invalid module.
pub module_name: String, pub module_name: String,

View file

@ -0,0 +1,10 @@
{
"tempDir": true,
"steps": [{
"args": "add npm:chalk",
"output": "[WILDCARD]"
}, {
"args": "run -A main.cjs",
"output": "ERR_PACKAGE_PATH_NOT_EXPORTED\n"
}]
}

View file

@ -0,0 +1,6 @@
{
"nodeModulesDir": "auto",
"imports": {
"chalk": "npm:chalk@^5.4.1"
}
}

View file

@ -0,0 +1,5 @@
try {
require.resolve("chalk/package.json");
} catch (e) {
console.log(e.code);
}