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