diff --git a/crates/nu-cli/src/commands/classified/internal.rs b/crates/nu-cli/src/commands/classified/internal.rs index 3e721dd22..3a7ddeb8b 100644 --- a/crates/nu-cli/src/commands/classified/internal.rs +++ b/crates/nu-cli/src/commands/classified/internal.rs @@ -189,26 +189,31 @@ pub(crate) async fn run_internal_command( InputStream::from_stream(futures::stream::iter(vec![])) } CommandAction::SourceScript(filename) => { - let contents = std::fs::read_to_string(&filename); - if let Ok(contents) = contents { - let result = crate::script::run_script_standalone( - contents, true, &context, false, - ) - .await; + let contents = std::fs::read_to_string(&filename.item); + match contents { + Ok(contents) => { + let result = crate::script::run_script_standalone( + contents, true, &context, false, + ) + .await; - if let Err(err) = result { - return InputStream::one( - UntaggedValue::Error(err.into()).into_untagged_value(), - ); + if let Err(err) = result { + return InputStream::one( + UntaggedValue::Error(err.into()) + .into_untagged_value(), + ); + } + InputStream::from_stream(futures::stream::iter(vec![])) + } + Err(_) => { + context.error(ShellError::labeled_error( + "Can't load file to source", + "can't load file", + filename.span(), + )); + + InputStream::from_stream(futures::stream::iter(vec![])) } - InputStream::from_stream(futures::stream::iter(vec![])) - } else { - InputStream::one( - UntaggedValue::Error(ShellError::untagged_runtime_error( - format!("could not source '{}'", filename), - )) - .into_untagged_value(), - ) } } CommandAction::AddPlugins(path) => { diff --git a/crates/nu-cli/src/commands/source.rs b/crates/nu-cli/src/commands/source.rs index cdc59a954..776382d30 100644 --- a/crates/nu-cli/src/commands/source.rs +++ b/crates/nu-cli/src/commands/source.rs @@ -43,6 +43,6 @@ pub async fn source(args: CommandArgs) -> Result { let (SourceArgs { filename }, _) = args.process().await?; Ok(OutputStream::one(ReturnSuccess::action( - CommandAction::SourceScript(filename.item), + CommandAction::SourceScript(filename), ))) } diff --git a/crates/nu-protocol/src/return_value.rs b/crates/nu-protocol/src/return_value.rs index dbcb388af..a57d23ce0 100644 --- a/crates/nu-protocol/src/return_value.rs +++ b/crates/nu-protocol/src/return_value.rs @@ -1,6 +1,6 @@ use crate::value::Value; use nu_errors::ShellError; -use nu_source::{b, DebugDocBuilder, PrettyDebug}; +use nu_source::{b, DebugDocBuilder, PrettyDebug, Tagged}; use serde::{Deserialize, Serialize}; /// The inner set of actions for the command processor. Each denotes a way to change state in the processor without changing it directly from the command itself. @@ -27,7 +27,7 @@ pub enum CommandAction { /// Add plugins from path given AddPlugins(String), /// Run the given script in the current context (given filename) - SourceScript(String), + SourceScript(Tagged), /// Go to the previous shell in the shell ring buffer PreviousShell, /// Go to the next shell in the shell ring buffer