From 66087b01e6f0dec9139242669760cf8f3acbeb3e Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Sat, 26 Mar 2022 10:43:46 +1300 Subject: [PATCH] Improve the 'use' and 'source' errors (#4966) * Improve the 'use' and 'source' errors * Add register --- crates/nu-parser/src/errors.rs | 24 +++++++++++++++++++++++- crates/nu-parser/src/parse_keywords.rs | 17 ++++++++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/crates/nu-parser/src/errors.rs b/crates/nu-parser/src/errors.rs index 6e31ce7995..05a743ff8c 100644 --- a/crates/nu-parser/src/errors.rs +++ b/crates/nu-parser/src/errors.rs @@ -95,7 +95,11 @@ pub enum ParseError { VariableNotValid(#[label = "variable name can't contain spaces or quotes"] Span), #[error("Module not found.")] - #[diagnostic(code(nu::parser::module_not_found), url(docsrs))] + #[diagnostic( + code(nu::parser::module_not_found), + url(docsrs), + help("module files need to be available before your script is run") + )] ModuleNotFound(#[label = "module not found"] Span), #[error("Not found.")] @@ -221,6 +225,22 @@ pub enum ParseError { #[diagnostic(code(nu::parser::export_not_found), url(docsrs))] ExportNotFound(#[label = "could not find imports"] Span), + #[error("File not found")] + #[diagnostic( + code(nu::parser::sourced_file_not_found), + url(docsrs), + help("sourced files need to be available before your script is run") + )] + SourcedFileNotFound(String, #[label("File not found: {0}")] Span), + + #[error("File not found")] + #[diagnostic( + code(nu::parser::registered_file_not_found), + url(docsrs), + help("registered files need to be available before your script is run") + )] + RegisteredFileNotFound(String, #[label("File not found: {0}")] Span), + #[error("File not found")] #[diagnostic(code(nu::parser::file_not_found), url(docsrs))] FileNotFound(String, #[label("File not found: {0}")] Span), @@ -274,6 +294,8 @@ impl ParseError { ParseError::MissingImportPattern(s) => *s, ParseError::WrongImportPattern(s) => *s, ParseError::ExportNotFound(s) => *s, + ParseError::SourcedFileNotFound(_, s) => *s, + ParseError::RegisteredFileNotFound(_, s) => *s, ParseError::FileNotFound(_, s) => *s, ParseError::LabeledError(_, _, s) => *s, } diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 24d7fc33b4..dc01bc14a2 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -1853,7 +1853,7 @@ pub fn parse_source( } } } else { - error = error.or(Some(ParseError::FileNotFound(filename, spans[1]))); + error = error.or(Some(ParseError::SourcedFileNotFound(filename, spans[1]))); } } else { return (garbage_pipeline(spans), Some(ParseError::NonUtf8(spans[1]))); @@ -1959,14 +1959,17 @@ pub fn parse_register( if let Some(p) = find_in_dirs(&name, working_set, &cwd, PLUGIN_DIRS_ENV) { Ok(p) } else { - Err(ParseError::FileNotFound(name, expr.span)) + Err(ParseError::RegisteredFileNotFound(name, expr.span)) } }) .and_then(|path| { if path.exists() & path.is_file() { Ok(path) } else { - Err(ParseError::FileNotFound(format!("{:?}", path), expr.span)) + Err(ParseError::RegisteredFileNotFound( + format!("{:?}", path), + expr.span, + )) } }) }) @@ -2007,13 +2010,17 @@ pub fn parse_register( String::from_utf8(shell_expr.to_vec()) .map_err(|_| ParseError::NonUtf8(expr.span)) .and_then(|name| { - canonicalize_with(&name, cwd).map_err(|_| ParseError::FileNotFound(name, expr.span)) + canonicalize_with(&name, cwd) + .map_err(|_| ParseError::RegisteredFileNotFound(name, expr.span)) }) .and_then(|path| { if path.exists() & path.is_file() { Ok(path) } else { - Err(ParseError::FileNotFound(format!("{:?}", path), expr.span)) + Err(ParseError::RegisteredFileNotFound( + format!("{:?}", path), + expr.span, + )) } }) });