Skip to content

Commit

Permalink
feat: enhance err msg when import empty local path (#1329)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored May 17, 2024
1 parent a6a21e7 commit f5903e7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
68 changes: 42 additions & 26 deletions kclvm/sema/src/resolver/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/grammar/import/empty_import_fail/stderr.golden.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit f5903e7

Please sign in to comment.