Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): show directory import and missing extension suggestions #27905

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions resolvers/node/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl NodeJsErrorCoded for PackageSubpathResolveError {
PackageSubpathResolveErrorKind::PkgJsonLoad(e) => e.code(),
PackageSubpathResolveErrorKind::Exports(e) => e.code(),
PackageSubpathResolveErrorKind::LegacyResolve(e) => e.code(),
PackageSubpathResolveErrorKind::FinalizeResolution(e) => e.code(),
}
}
}
Expand All @@ -216,6 +217,9 @@ pub enum PackageSubpathResolveErrorKind {
#[class(inherit)]
#[error(transparent)]
LegacyResolve(LegacyResolveError),
#[class(inherit)]
#[error(transparent)]
FinalizeResolution(#[from] FinalizeResolutionError),
}

#[derive(Debug, Error, JsError)]
Expand Down Expand Up @@ -551,16 +555,18 @@ impl NodeJsErrorCoded for FinalizeResolutionError {
#[derive(Debug, Error, JsError)]
#[class(generic)]
#[error(
"[{}] Cannot find {} '{}'{}",
"[{}] Cannot find {} '{}'{}{}",
self.code(),
typ,
specifier,
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()
)]
pub struct ModuleNotFoundError {
pub specifier: UrlOrPath,
pub maybe_referrer: Option<UrlOrPath>,
pub typ: &'static str,
pub suggested_ext: Option<&'static str>,
}

impl NodeJsErrorCoded for ModuleNotFoundError {
Expand All @@ -572,14 +578,16 @@ impl NodeJsErrorCoded for ModuleNotFoundError {
#[derive(Debug, Error, JsError)]
#[class(generic)]
#[error(
"[{}] Directory import '{}' is not supported resolving ES modules{}",
"[{}] Directory import '{}' is not supported resolving ES modules{}{}",
self.code(),
dir_url,
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(),
)]
pub struct UnsupportedDirImportError {
pub dir_url: UrlOrPath,
pub maybe_referrer: Option<UrlOrPath>,
pub suggested_file_name: Option<&'static str>,
}

impl NodeJsErrorCoded for UnsupportedDirImportError {
Expand Down
Loading
Loading