diff --git a/crates/nu-cli/src/config_files.rs b/crates/nu-cli/src/config_files.rs index 61a9562808..4865f937be 100644 --- a/crates/nu-cli/src/config_files.rs +++ b/crates/nu-cli/src/config_files.rs @@ -41,6 +41,7 @@ pub fn read_plugin_file( &contents, &plugin_filename, PipelineData::empty(), + false, ); } } @@ -93,6 +94,7 @@ pub fn eval_config_contents( &contents, &config_filename, PipelineData::empty(), + false, ); // Merge the environment in case env vars changed in the config diff --git a/crates/nu-cli/src/eval_file.rs b/crates/nu-cli/src/eval_file.rs index 528a41ed4a..043078a021 100644 --- a/crates/nu-cli/src/eval_file.rs +++ b/crates/nu-cli/src/eval_file.rs @@ -106,13 +106,21 @@ pub fn evaluate_file( &file, file_path_str, PipelineData::empty(), + true, ) { std::process::exit(1); } - if !eval_source(engine_state, stack, args.as_bytes(), "", input) { + if !eval_source( + engine_state, + stack, + args.as_bytes(), + "", + input, + true, + ) { std::process::exit(1); } - } else if !eval_source(engine_state, stack, &file, file_path_str, input) { + } else if !eval_source(engine_state, stack, &file, file_path_str, input, true) { std::process::exit(1); } diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index 5ee2d44de2..af6bbc7f62 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -145,6 +145,7 @@ pub fn evaluate_repl( s.item.as_bytes(), &format!("entry #{entry_num}"), PipelineData::empty(), + false, ); engine_state.merge_env(stack, get_guaranteed_cwd(engine_state, stack))?; } @@ -472,6 +473,7 @@ pub fn evaluate_repl( s.as_bytes(), &format!("entry #{entry_num}"), PipelineData::empty(), + false, ); } let cmd_duration = start_time.elapsed(); diff --git a/crates/nu-cli/src/util.rs b/crates/nu-cli/src/util.rs index 1f57eac31e..04fa0b23f2 100644 --- a/crates/nu-cli/src/util.rs +++ b/crates/nu-cli/src/util.rs @@ -1,5 +1,5 @@ use crate::repl::eval_hook; -use nu_engine::eval_block; +use nu_engine::{eval_block, eval_block_with_early_return}; use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents}; use nu_protocol::engine::StateWorkingSet; use nu_protocol::CliError; @@ -203,6 +203,7 @@ pub fn eval_source( source: &[u8], fname: &str, input: PipelineData, + allow_return: bool, ) -> bool { let start_time = std::time::Instant::now(); @@ -230,7 +231,13 @@ pub fn eval_source( return false; } - match eval_block(engine_state, stack, &block, input, false, false) { + let b = if allow_return { + eval_block_with_early_return(engine_state, stack, &block, input, false, false) + } else { + eval_block(engine_state, stack, &block, input, false, false) + }; + + match b { Ok(pipeline_data) => { let config = engine_state.get_config(); let result; diff --git a/crates/nu-command/src/deprecated/source.rs b/crates/nu-command/src/deprecated/source.rs index a5fb36d63d..5a29828dd5 100644 --- a/crates/nu-command/src/deprecated/source.rs +++ b/crates/nu-command/src/deprecated/source.rs @@ -1,4 +1,4 @@ -use nu_engine::{eval_block, CallExt}; +use nu_engine::{eval_block_with_early_return, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type}; @@ -48,7 +48,7 @@ impl Command for Source { let block_id: i64 = call.req_parser_info(engine_state, stack, 0)?; let block = engine_state.get_block(block_id as usize).clone(); - eval_block( + eval_block_with_early_return( engine_state, stack, &block, diff --git a/crates/nu-command/src/env/source_env.rs b/crates/nu-command/src/env/source_env.rs index a74fcdfd8c..3edabac17f 100644 --- a/crates/nu-command/src/env/source_env.rs +++ b/crates/nu-command/src/env/source_env.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use nu_engine::{eval_block, find_in_dirs_env, redirect_env, CallExt}; +use nu_engine::{eval_block_with_early_return, find_in_dirs_env, redirect_env, CallExt}; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ @@ -62,7 +62,7 @@ impl Command for SourceEnv { let block = engine_state.get_block(block_id as usize).clone(); let mut callee_stack = caller_stack.gather_captures(&block.captures); - let result = eval_block( + let result = eval_block_with_early_return( engine_state, &mut callee_stack, &block, diff --git a/crates/nu-command/tests/commands/return_.rs b/crates/nu-command/tests/commands/return_.rs index e5c5e2c9c1..bc8b3cc35f 100644 --- a/crates/nu-command/tests/commands/return_.rs +++ b/crates/nu-command/tests/commands/return_.rs @@ -23,3 +23,15 @@ fn early_return_if_false() { assert_eq!(actual.out, r#"100"#); } + +#[test] +fn return_works_in_script_without_def_main() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + nu early_return.nu + "# + )); + + assert!(actual.err.is_empty()); +} diff --git a/crates/nu-command/tests/commands/source_env.rs b/crates/nu-command/tests/commands/source_env.rs index 7036aee68a..bf09bf1b44 100644 --- a/crates/nu-command/tests/commands/source_env.rs +++ b/crates/nu-command/tests/commands/source_env.rs @@ -314,3 +314,15 @@ fn source_env_const_file() { assert_eq!(actual.out, "foo"); }) } + +#[test] +fn source_respects_early_return() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + source early_return.nu + "# + )); + + assert!(actual.err.is_empty()); +} diff --git a/src/config_files.rs b/src/config_files.rs index 6b1e2bbd05..1a62bbf9db 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -131,6 +131,7 @@ pub(crate) fn read_default_env_file(engine_state: &mut EngineState, stack: &mut config_file.as_bytes(), "default_env.nu", PipelineData::empty(), + false, ); info!("read_config_file {}:{}:{}", file!(), line!(), column!()); @@ -167,6 +168,7 @@ fn eval_default_config( "default_config.nu" }, PipelineData::empty(), + false, ); // Merge the environment in case env vars changed in the config diff --git a/tests/fixtures/formats/early_return.nu b/tests/fixtures/formats/early_return.nu new file mode 100644 index 0000000000..f79f872c73 --- /dev/null +++ b/tests/fixtures/formats/early_return.nu @@ -0,0 +1,3 @@ +echo "a" +return 1 +error make {msg: "this should not show"}