small fixes

- take a working set instead of engine state in `make_main_call()`
 - improve panic message in `make_main_call()`
This commit is contained in:
Gwendolyn 2024-07-31 10:35:26 +02:00
parent 3e5ec558bf
commit 23846ecd84
2 changed files with 12 additions and 13 deletions

View File

@ -126,12 +126,14 @@ pub fn evaluate_file(
// Invoke the main command with arguments. // Invoke the main command with arguments.
// Arguments with whitespaces are quoted, thus can be safely concatenated by whitespace. // Arguments with whitespaces are quoted, thus can be safely concatenated by whitespace.
let mut working_set = StateWorkingSet::new(engine_state);
let main_call_block = make_main_call( let main_call_block = make_main_call(
engine_state, &mut working_set,
source_filename.to_string_lossy().to_string(), source_filename.to_string_lossy().to_string(),
args, args,
true, true,
)?; )?;
engine_state.merge_delta(working_set.delta)?;
evaluate_block_with_exit_code( evaluate_block_with_exit_code(
engine_state, engine_state,

View File

@ -205,12 +205,11 @@ fn gather_env_vars(
} }
pub fn make_main_call( pub fn make_main_call(
engine_state: &mut EngineState, working_set: &mut StateWorkingSet,
fname: String, fname: String,
args: &[String], args: &[String],
redirect_env: bool, redirect_env: bool,
) -> Result<Arc<Block>, ShellError> { ) -> Result<Arc<Block>, ShellError> {
let mut working_set = StateWorkingSet::new(engine_state);
let source = format!("{} {}", fname, args.join(" ")); let source = format!("{} {}", fname, args.join(" "));
let source = source.as_bytes(); let source = source.as_bytes();
let file_id = working_set.add_file("<commandline>".to_string(), source); let file_id = working_set.add_file("<commandline>".to_string(), source);
@ -226,35 +225,35 @@ pub fn make_main_call(
let lite_command = &lite_block.block[0].commands[0]; let lite_command = &lite_block.block[0].commands[0];
let (decl_id, command_len) = let (decl_id, command_len) =
find_longest_command(&working_set, b"main", &lite_command.parts[1..]) find_longest_command(working_set, b"main", &lite_command.parts[1..])
.expect("already checked that a main def exists"); .expect("make_main_call() called, but 'main' definition not found in state");
let parsed_call = parse_internal_call( let parsed_call = parse_internal_call(
&mut working_set, working_set,
Span::concat(&lite_command.parts[..command_len + 1]), Span::concat(&lite_command.parts[..command_len + 1]),
&lite_command.parts[(command_len + 1)..], &lite_command.parts[(command_len + 1)..],
decl_id, decl_id,
); );
let expression = Expression::new( let expression = Expression::new(
&mut working_set, working_set,
Expr::Call(parsed_call.call), Expr::Call(parsed_call.call),
Span::concat(lite_command.parts.as_slice()), Span::concat(lite_command.parts.as_slice()),
parsed_call.output, parsed_call.output,
); );
if let Some(warning) = working_set.parse_warnings.first() { if let Some(warning) = working_set.parse_warnings.first() {
report_error(&working_set, warning); report_error(working_set, warning);
} }
// If any parse errors were found, report the first error and exit. // If any parse errors were found, report the first error and exit.
if let Some(err) = working_set.parse_errors.first() { if let Some(err) = working_set.parse_errors.first() {
report_error(&working_set, err); report_error(working_set, err);
std::process::exit(1); std::process::exit(1);
} }
if let Some(err) = working_set.compile_errors.first() { if let Some(err) = working_set.compile_errors.first() {
report_error(&working_set, err); report_error(working_set, err);
// Not a fatal error, for now // Not a fatal error, for now
} }
@ -267,15 +266,13 @@ pub fn make_main_call(
span: Some(file_span), span: Some(file_span),
}; };
match nu_engine::compile(&working_set, &block) { match nu_engine::compile(working_set, &block) {
Ok(ir_block) => { Ok(ir_block) => {
block.ir_block = Some(ir_block); block.ir_block = Some(ir_block);
} }
Err(err) => working_set.compile_errors.push(err), Err(err) => working_set.compile_errors.push(err),
} }
engine_state.merge_delta(working_set.delta)?;
Ok(Arc::new(block)) Ok(Arc::new(block))
} }