Skip to content

Commit 9cbcb84

Browse files
dsherretHajime-san
andauthored
fix(node): show directory import and missing extension suggestions (denoland#27905)
Shows directory import and missing extension suggestions in error messages similar but not exactly to node. Closes denoland#26802 Co-authored-by: Hajime-san <[email protected]>
1 parent 7d19668 commit 9cbcb84

File tree

21 files changed

+418
-149
lines changed

21 files changed

+418
-149
lines changed

resolvers/node/errors.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ impl NodeJsErrorCoded for PackageSubpathResolveError {
198198
PackageSubpathResolveErrorKind::PkgJsonLoad(e) => e.code(),
199199
PackageSubpathResolveErrorKind::Exports(e) => e.code(),
200200
PackageSubpathResolveErrorKind::LegacyResolve(e) => e.code(),
201+
PackageSubpathResolveErrorKind::FinalizeResolution(e) => e.code(),
201202
}
202203
}
203204
}
@@ -216,6 +217,9 @@ pub enum PackageSubpathResolveErrorKind {
216217
#[class(inherit)]
217218
#[error(transparent)]
218219
LegacyResolve(LegacyResolveError),
220+
#[class(inherit)]
221+
#[error(transparent)]
222+
FinalizeResolution(#[from] FinalizeResolutionError),
219223
}
220224

221225
#[derive(Debug, Error, JsError)]
@@ -551,16 +555,18 @@ impl NodeJsErrorCoded for FinalizeResolutionError {
551555
#[derive(Debug, Error, JsError)]
552556
#[class(generic)]
553557
#[error(
554-
"[{}] Cannot find {} '{}'{}",
558+
"[{}] Cannot find {} '{}'{}{}",
555559
self.code(),
556560
typ,
557561
specifier,
558-
maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default()
562+
maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default(),
563+
suggested_ext.as_ref().map(|m| format!("\nDid you mean to import with the \".{}\" extension?", m)).unwrap_or_default()
559564
)]
560565
pub struct ModuleNotFoundError {
561566
pub specifier: UrlOrPath,
562567
pub maybe_referrer: Option<UrlOrPath>,
563568
pub typ: &'static str,
569+
pub suggested_ext: Option<&'static str>,
564570
}
565571

566572
impl NodeJsErrorCoded for ModuleNotFoundError {
@@ -572,14 +578,16 @@ impl NodeJsErrorCoded for ModuleNotFoundError {
572578
#[derive(Debug, Error, JsError)]
573579
#[class(generic)]
574580
#[error(
575-
"[{}] Directory import '{}' is not supported resolving ES modules{}",
581+
"[{}] Directory import '{}' is not supported resolving ES modules{}{}",
576582
self.code(),
577583
dir_url,
578584
maybe_referrer.as_ref().map(|referrer| format!(" imported from '{}'", referrer)).unwrap_or_default(),
585+
suggested_file_name.map(|file_name| format!("\nDid you mean to import {file_name} within the directory?")).unwrap_or_default(),
579586
)]
580587
pub struct UnsupportedDirImportError {
581588
pub dir_url: UrlOrPath,
582589
pub maybe_referrer: Option<UrlOrPath>,
590+
pub suggested_file_name: Option<&'static str>,
583591
}
584592

585593
impl NodeJsErrorCoded for UnsupportedDirImportError {

0 commit comments

Comments
 (0)