diff --git a/kclvm/sema/src/resolver/import.rs b/kclvm/sema/src/resolver/import.rs index 0bf2d3717..96a0d0df4 100644 --- a/kclvm/sema/src/resolver/import.rs +++ b/kclvm/sema/src/resolver/import.rs @@ -37,34 +37,50 @@ impl<'ctx> Resolver<'ctx> { let real_path = Path::new(&self.program.root).join(pkgpath.replace('.', "/")); if !self.program.pkgs.contains_key(pkgpath) { - self.handler.add_error( - ErrorKind::CannotFindModule, - &[Message { - range: stmt.get_span_pos(), - style: Style::Line, - message: format!( - "Cannot find the module {} from {}", - import_stmt.rawpath, - real_path.to_str().unwrap() - ), - note: None, - suggested_replacement: None, - }], - ); - - let mut suggestions = - vec![format!("find more package on 'https://artifacthub.io'")]; - - if let Ok(pkg_name) = parse_external_pkg_name(pkgpath) { - suggestions.insert( - 0, - format!( - "try 'kcl mod add {}' to download the package not found", - pkg_name - ), + if real_path.exists() { + self.handler.add_error( + ErrorKind::CannotFindModule, + &[Message { + range: stmt.get_span_pos(), + style: Style::Line, + message: format!( + "Cannot import the module {} from {}, attempted import folder with no kcl files", + import_stmt.rawpath, + real_path.to_str().unwrap() + ), + note: None, + suggested_replacement: None, + }], ); + } else { + self.handler.add_error( + ErrorKind::CannotFindModule, + &[Message { + range: stmt.get_span_pos(), + style: Style::Line, + message: format!( + "Cannot find the module {} from {}", + import_stmt.rawpath, + real_path.to_str().unwrap() + ), + note: None, + suggested_replacement: None, + }], + ); + let mut suggestions = + vec![format!("find more package on 'https://artifacthub.io'")]; + + if let Ok(pkg_name) = parse_external_pkg_name(pkgpath) { + suggestions.insert( + 0, + format!( + "try 'kcl mod add {}' to download the package not found", + pkg_name + ), + ); + } + self.handler.add_suggestions(suggestions); } - self.handler.add_suggestions(suggestions); } else { let file = real_path.to_str().unwrap().to_string(); if real_path.is_file() && main_files.contains(&file) { diff --git a/test/grammar/import/empty_import_fail/stderr.golden.py b/test/grammar/import/empty_import_fail/stderr.golden.py index 4bfd2c927..49bb33cf7 100644 --- a/test/grammar/import/empty_import_fail/stderr.golden.py +++ b/test/grammar/import/empty_import_fail/stderr.golden.py @@ -19,7 +19,7 @@ end_col_no=17 ) ], - arg_msg=kcl_error.CANNOT_FIND_MODULE_MSG.format(modulename, '{}'.format(location)) + arg_msg=kcl_error.CANNOT_FIND_MODULE_MSG.format(modulename, '{}'.format(location)) + ", attempted import folder with no kcl files" ), file=sys.stdout )