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:
WindSoilder 2023-09-29 00:49:42 +08:00 committed by GitHub
parent 80220b722b
commit 9c52b93975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 3 deletions

View File

@ -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);

View File

@ -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());
}

View File

@ -0,0 +1,7 @@
let y = 'not hiya'
return
def main [] {
error make {msg: "this should not show"}
}