forked from extern/nushell
allow early return outside of main (#10514)
# Description Fixes: #9792 When evaluating file, we need to allow early return if we evaluate script file first.
This commit is contained in:
parent
80220b722b
commit
9c52b93975
@ -2,7 +2,7 @@ use crate::util::eval_source;
|
||||
use log::info;
|
||||
use log::trace;
|
||||
use miette::{IntoDiagnostic, Result};
|
||||
use nu_engine::eval_block_with_early_return;
|
||||
use nu_engine::eval_block;
|
||||
use nu_engine::{convert_env_values, current_dir};
|
||||
use nu_parser::parse;
|
||||
use nu_path::canonicalize_with;
|
||||
@ -126,14 +126,22 @@ pub fn evaluate_file(
|
||||
if engine_state.find_decl(b"main", &[]).is_some() {
|
||||
let args = format!("main {}", args.join(" "));
|
||||
|
||||
let pipeline_data = eval_block_with_early_return(
|
||||
let pipeline_data = eval_block(
|
||||
engine_state,
|
||||
stack,
|
||||
&block,
|
||||
PipelineData::empty(),
|
||||
false,
|
||||
false,
|
||||
)
|
||||
);
|
||||
let pipeline_data = match pipeline_data {
|
||||
Err(ShellError::Return(_, _)) => {
|
||||
// allows early exists before `main` is run.
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
x => x,
|
||||
}
|
||||
.unwrap_or_else(|e| {
|
||||
let working_set = StateWorkingSet::new(engine_state);
|
||||
report_error(&working_set, &e);
|
||||
|
@ -23,3 +23,12 @@ fn return_works_in_script_without_def_main() {
|
||||
|
||||
assert!(actual.err.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn return_works_in_script_with_def_main() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats",
|
||||
pipeline("nu early_return_outside_main.nu")
|
||||
);
|
||||
assert!(actual.err.is_empty());
|
||||
}
|
||||
|
7
tests/fixtures/formats/early_return_outside_main.nu
vendored
Normal file
7
tests/fixtures/formats/early_return_outside_main.nu
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
let y = 'not hiya'
|
||||
|
||||
return
|
||||
|
||||
def main [] {
|
||||
error make {msg: "this should not show"}
|
||||
}
|
Loading…
Reference in New Issue
Block a user