Refactor the CLI code a bit (#12782)

# Description
Refactors the code in `nu-cli`, `main.rs`, `run.rs`, and few others.
Namely, I added `EngineState::generate_nu_constant` function to
eliminate some duplicate code. Otherwise, I changed a bunch of areas to
return errors instead of calling `std::process::exit`.

# User-Facing Changes
Should be none.
This commit is contained in:
Ian Manske 2024-05-09 23:29:27 +00:00 committed by GitHub
parent 1b2e680059
commit 72d3860d05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 146 additions and 305 deletions

View file

@ -4,8 +4,7 @@ use nu_plugin_protocol::{PluginCallResponse, PluginOutput};
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack}, engine::{EngineState, Stack},
eval_const::create_nu_constant, PipelineData, Span, Spanned, Value,
PipelineData, Span, Spanned, Value, NU_VARIABLE_ID,
}; };
use nu_std::load_standard_library; use nu_std::load_standard_library;
use nu_utils::{get_default_config, get_default_env}; use nu_utils::{get_default_config, get_default_env};
@ -30,9 +29,7 @@ fn setup_engine() -> EngineState {
// parsing config.nu breaks without PWD set, so set a valid path // parsing config.nu breaks without PWD set, so set a valid path
engine_state.add_env_var("PWD".into(), Value::string(cwd, Span::test_data())); engine_state.add_env_var("PWD".into(), Value::string(cwd, Span::test_data()));
let nu_const = create_nu_constant(&engine_state, Span::unknown()) engine_state.generate_nu_constant();
.expect("Failed to create nushell constant.");
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
engine_state engine_state
} }
@ -86,6 +83,7 @@ fn bench_command(
b.iter(move || { b.iter(move || {
let mut stack = stack.clone(); let mut stack = stack.clone();
let mut engine = engine.clone(); let mut engine = engine.clone();
#[allow(clippy::unit_arg)]
black_box( black_box(
evaluate_commands( evaluate_commands(
&commands, &commands,

View file

@ -1,12 +1,12 @@
use crate::util::eval_source; use crate::util::eval_source;
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
report_error, HistoryFileFormat, PipelineData,
};
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
use nu_protocol::{ParseError, PluginRegistryFile, Spanned}; use nu_protocol::{engine::StateWorkingSet, report_error, ParseError, PluginRegistryFile, Spanned};
use nu_protocol::{
engine::{EngineState, Stack},
report_error_new, HistoryFileFormat, PipelineData,
};
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
use nu_utils::utils::perf; use nu_utils::utils::perf;
use std::path::PathBuf; use std::path::PathBuf;
@ -25,10 +25,9 @@ pub fn read_plugin_file(
plugin_file: Option<Spanned<String>>, plugin_file: Option<Spanned<String>>,
storage_path: &str, storage_path: &str,
) { ) {
use nu_protocol::ShellError;
use std::path::Path; use std::path::Path;
use nu_protocol::{report_error_new, ShellError};
let span = plugin_file.as_ref().map(|s| s.span); let span = plugin_file.as_ref().map(|s| s.span);
// Check and warn + abort if this is a .nu plugin file // Check and warn + abort if this is a .nu plugin file
@ -239,13 +238,11 @@ pub fn eval_config_contents(
match engine_state.cwd(Some(stack)) { match engine_state.cwd(Some(stack)) {
Ok(cwd) => { Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) { if let Err(e) = engine_state.merge_env(stack, cwd) {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &e);
report_error(&working_set, &e);
} }
} }
Err(e) => { Err(e) => {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &e);
report_error(&working_set, &e);
} }
} }
} }
@ -266,8 +263,8 @@ pub(crate) fn get_history_path(storage_path: &str, mode: HistoryFileFormat) -> O
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -> bool { pub fn migrate_old_plugin_file(engine_state: &EngineState, storage_path: &str) -> bool {
use nu_protocol::{ use nu_protocol::{
report_error_new, PluginExample, PluginIdentity, PluginRegistryItem, PluginExample, PluginIdentity, PluginRegistryItem, PluginRegistryItemData, PluginSignature,
PluginRegistryItemData, PluginSignature, ShellError, ShellError,
}; };
use std::collections::BTreeMap; use std::collections::BTreeMap;

View file

@ -5,8 +5,9 @@ use nu_parser::parse;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
report_error, PipelineData, Spanned, Value, report_error, PipelineData, ShellError, Spanned, Value,
}; };
use std::sync::Arc;
/// Run a command (or commands) given to us by the user /// Run a command (or commands) given to us by the user
pub fn evaluate_commands( pub fn evaluate_commands(
@ -16,13 +17,9 @@ pub fn evaluate_commands(
input: PipelineData, input: PipelineData,
table_mode: Option<Value>, table_mode: Option<Value>,
no_newline: bool, no_newline: bool,
) -> Result<Option<i64>> { ) -> Result<(), ShellError> {
// Translate environment variables from Strings to Values // Translate environment variables from Strings to Values
if let Some(e) = convert_env_values(engine_state, stack) { convert_env_values(engine_state, stack)?;
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
std::process::exit(1);
}
// Parse the source code // Parse the source code
let (block, delta) = { let (block, delta) = {
@ -41,7 +38,6 @@ pub fn evaluate_commands(
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);
} }
@ -49,35 +45,26 @@ pub fn evaluate_commands(
}; };
// Update permanent state // Update permanent state
if let Err(err) = engine_state.merge_delta(delta) { engine_state.merge_delta(delta)?;
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
}
// Run the block // Run the block
let exit_code = match eval_block::<WithoutDebug>(engine_state, stack, &block, input) { let pipeline = eval_block::<WithoutDebug>(engine_state, stack, &block, input)?;
Ok(pipeline_data) => {
let mut config = engine_state.get_config().clone();
if let Some(t_mode) = table_mode {
config.table_mode = t_mode.coerce_str()?.parse().unwrap_or_default();
}
crate::eval_file::print_table_or_error(
engine_state,
stack,
pipeline_data,
&mut config,
no_newline,
)
}
Err(err) => {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err); if let PipelineData::Value(Value::Error { error, .. }, ..) = pipeline {
std::process::exit(1); return Err(*error);
}
if let Some(t_mode) = table_mode {
Arc::make_mut(&mut engine_state.config).table_mode =
t_mode.coerce_str()?.parse().unwrap_or_default();
}
let exit_code = pipeline.print(engine_state, stack, no_newline, false)?;
if exit_code != 0 {
std::process::exit(exit_code as i32);
} }
};
info!("evaluate {}:{}:{}", file!(), line!(), column!()); info!("evaluate {}:{}:{}", file!(), line!(), column!());
Ok(exit_code) Ok(())
} }

View file

@ -1,15 +1,14 @@
use crate::util::eval_source; use crate::util::eval_source;
use log::{info, trace}; use log::{info, trace};
use miette::{IntoDiagnostic, Result};
use nu_engine::{convert_env_values, eval_block}; use nu_engine::{convert_env_values, eval_block};
use nu_parser::parse; use nu_parser::parse;
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
report_error, Config, PipelineData, ShellError, Span, Value, report_error, PipelineData, ShellError, Span, Value,
}; };
use std::{io::Write, sync::Arc}; use std::sync::Arc;
/// Entry point for evaluating a file. /// Entry point for evaluating a file.
/// ///
@ -21,73 +20,40 @@ pub fn evaluate_file(
engine_state: &mut EngineState, engine_state: &mut EngineState,
stack: &mut Stack, stack: &mut Stack,
input: PipelineData, input: PipelineData,
) -> Result<()> { ) -> Result<(), ShellError> {
// Convert environment variables from Strings to Values and store them in the engine state. // Convert environment variables from Strings to Values and store them in the engine state.
if let Some(e) = convert_env_values(engine_state, stack) { convert_env_values(engine_state, stack)?;
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
std::process::exit(1);
}
let cwd = engine_state.cwd_as_string(Some(stack))?; let cwd = engine_state.cwd_as_string(Some(stack))?;
let file_path = canonicalize_with(&path, cwd).unwrap_or_else(|e| { let file_path =
let working_set = StateWorkingSet::new(engine_state); canonicalize_with(&path, cwd).map_err(|err| ShellError::FileNotFoundCustom {
report_error( msg: format!("Could not access file '{path}': {err}"),
&working_set,
&ShellError::FileNotFoundCustom {
msg: format!("Could not access file '{}': {:?}", path, e.to_string()),
span: Span::unknown(), span: Span::unknown(),
}, })?;
);
std::process::exit(1);
});
let file_path_str = file_path.to_str().unwrap_or_else(|| { let file_path_str = file_path
let working_set = StateWorkingSet::new(engine_state); .to_str()
report_error( .ok_or_else(|| ShellError::NonUtf8Custom {
&working_set,
&ShellError::NonUtf8Custom {
msg: format!( msg: format!(
"Input file name '{}' is not valid UTF8", "Input file name '{}' is not valid UTF8",
file_path.to_string_lossy() file_path.to_string_lossy()
), ),
span: Span::unknown(), span: Span::unknown(),
}, })?;
);
std::process::exit(1);
});
let file = std::fs::read(&file_path) let file = std::fs::read(&file_path).map_err(|err| ShellError::FileNotFoundCustom {
.into_diagnostic() msg: format!("Could not read file '{file_path_str}': {err}"),
.unwrap_or_else(|e| {
let working_set = StateWorkingSet::new(engine_state);
report_error(
&working_set,
&ShellError::FileNotFoundCustom {
msg: format!(
"Could not read file '{}': {:?}",
file_path_str,
e.to_string()
),
span: Span::unknown(), span: Span::unknown(),
}, })?;
);
std::process::exit(1);
});
engine_state.file = Some(file_path.clone()); engine_state.file = Some(file_path.clone());
let parent = file_path.parent().unwrap_or_else(|| { let parent = file_path
let working_set = StateWorkingSet::new(engine_state); .parent()
report_error( .ok_or_else(|| ShellError::FileNotFoundCustom {
&working_set,
&ShellError::FileNotFoundCustom {
msg: format!("The file path '{file_path_str}' does not have a parent"), msg: format!("The file path '{file_path_str}' does not have a parent"),
span: Span::unknown(), span: Span::unknown(),
}, })?;
);
std::process::exit(1);
});
stack.add_env_var( stack.add_env_var(
"FILE_PWD".to_string(), "FILE_PWD".to_string(),
@ -127,43 +93,26 @@ pub fn evaluate_file(
} }
// Merge the changes into the engine state. // Merge the changes into the engine state.
engine_state engine_state.merge_delta(working_set.delta)?;
.merge_delta(working_set.delta)
.expect("merging delta into engine_state should succeed");
// Check if the file contains a main command. // Check if the file contains a main command.
if engine_state.find_decl(b"main", &[]).is_some() { if engine_state.find_decl(b"main", &[]).is_some() {
// Evaluate the file, but don't run main yet. // Evaluate the file, but don't run main yet.
let pipeline_data = let pipeline =
eval_block::<WithoutDebug>(engine_state, stack, &block, PipelineData::empty()); match eval_block::<WithoutDebug>(engine_state, stack, &block, PipelineData::empty()) {
let pipeline_data = match pipeline_data { Ok(data) => data,
Err(ShellError::Return { .. }) => { Err(ShellError::Return { .. }) => {
// Allow early return before main is run. // Allow early return before main is run.
return Ok(()); return Ok(());
} }
x => x, Err(err) => return Err(err),
} };
.unwrap_or_else(|e| {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &e);
std::process::exit(1);
});
// Print the pipeline output of the file. // Print the pipeline output of the last command of the file.
// The pipeline output of a file is the pipeline output of its last command. let exit_code = pipeline.print(engine_state, stack, true, false)?;
let result = pipeline_data.print(engine_state, stack, true, false);
match result {
Err(err) => {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
std::process::exit(1);
}
Ok(exit_code) => {
if exit_code != 0 { if exit_code != 0 {
std::process::exit(exit_code as i32); std::process::exit(exit_code as i32);
} }
}
}
// 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.
@ -186,60 +135,3 @@ pub fn evaluate_file(
Ok(()) Ok(())
} }
pub(crate) fn print_table_or_error(
engine_state: &mut EngineState,
stack: &mut Stack,
mut pipeline_data: PipelineData,
config: &mut Config,
no_newline: bool,
) -> Option<i64> {
let exit_code = match &mut pipeline_data {
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
_ => None,
};
// Change the engine_state config to use the passed in configuration
engine_state.set_config(config.clone());
if let PipelineData::Value(Value::Error { error, .. }, ..) = &pipeline_data {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &**error);
std::process::exit(1);
}
// We don't need to do anything special to print a table because print() handles it
print_or_exit(pipeline_data, engine_state, stack, no_newline);
// Make sure everything has finished
if let Some(exit_code) = exit_code {
let mut exit_code: Vec<_> = exit_code.into_iter().collect();
exit_code
.pop()
.and_then(|last_exit_code| match last_exit_code {
Value::Int { val: code, .. } => Some(code),
_ => None,
})
} else {
None
}
}
fn print_or_exit(
pipeline_data: PipelineData,
engine_state: &EngineState,
stack: &mut Stack,
no_newline: bool,
) {
let result = pipeline_data.print(engine_state, stack, no_newline, false);
let _ = std::io::stdout().flush();
let _ = std::io::stderr().flush();
if let Err(error) = result {
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &error);
let _ = std::io::stderr().flush();
std::process::exit(1);
}
}

View file

@ -2,8 +2,8 @@ use crate::NushellPrompt;
use log::trace; use log::trace;
use nu_engine::ClosureEvalOnce; use nu_engine::ClosureEvalOnce;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack},
report_error, Config, PipelineData, Value, report_error_new, Config, PipelineData, Value,
}; };
use reedline::Prompt; use reedline::Prompt;
@ -77,8 +77,7 @@ fn get_prompt_string(
result result
.map_err(|err| { .map_err(|err| {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &err);
report_error(&working_set, &err);
}) })
.ok() .ok()
} }

View file

@ -26,9 +26,8 @@ use nu_parser::{lex, parse, trim_quotes_str};
use nu_protocol::{ use nu_protocol::{
config::NuCursorShape, config::NuCursorShape,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
eval_const::create_nu_constant,
report_error_new, HistoryConfig, HistoryFileFormat, PipelineData, ShellError, Span, Spanned, report_error_new, HistoryConfig, HistoryFileFormat, PipelineData, ShellError, Span, Spanned,
Value, NU_VARIABLE_ID, Value,
}; };
use nu_utils::{ use nu_utils::{
filesystem::{have_permission, PermissionResult}, filesystem::{have_permission, PermissionResult},
@ -87,7 +86,7 @@ pub fn evaluate_repl(
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
// Translate environment variables from Strings to Values // Translate environment variables from Strings to Values
if let Some(e) = convert_env_values(engine_state, &unique_stack) { if let Err(e) = convert_env_values(engine_state, &unique_stack) {
report_error_new(engine_state, &e); report_error_new(engine_state, &e);
} }
perf( perf(
@ -145,8 +144,7 @@ pub fn evaluate_repl(
engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64); engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64);
// Regenerate the $nu constant to contain the startup time and any other potential updates // Regenerate the $nu constant to contain the startup time and any other potential updates
let nu_const = create_nu_constant(engine_state, Span::unknown())?; engine_state.generate_nu_constant();
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
if load_std_lib.is_none() && engine_state.get_config().show_banner { if load_std_lib.is_none() && engine_state.get_config().show_banner {
eval_source( eval_source(

View file

@ -39,9 +39,8 @@ fn gather_env_vars(
init_cwd: &Path, init_cwd: &Path,
) { ) {
fn report_capture_error(engine_state: &EngineState, env_str: &str, msg: &str) { fn report_capture_error(engine_state: &EngineState, env_str: &str, msg: &str) {
let working_set = StateWorkingSet::new(engine_state); report_error_new(
report_error( engine_state,
&working_set,
&ShellError::GenericError { &ShellError::GenericError {
error: format!("Environment variable was not captured: {env_str}"), error: format!("Environment variable was not captured: {env_str}"),
msg: "".into(), msg: "".into(),
@ -71,9 +70,8 @@ fn gather_env_vars(
} }
None => { None => {
// Could not capture current working directory // Could not capture current working directory
let working_set = StateWorkingSet::new(engine_state); report_error_new(
report_error( engine_state,
&working_set,
&ShellError::GenericError { &ShellError::GenericError {
error: "Current directory is not a valid utf-8 path".into(), error: "Current directory is not a valid utf-8 path".into(),
msg: "".into(), msg: "".into(),
@ -278,10 +276,7 @@ pub fn eval_source(
match result { match result {
Err(err) => { Err(err) => {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &err);
report_error(&working_set, &err);
return false; return false;
} }
Ok(exit_code) => { Ok(exit_code) => {
@ -297,11 +292,7 @@ pub fn eval_source(
} }
Err(err) => { Err(err) => {
set_last_exit_code(stack, 1); set_last_exit_code(stack, 1);
report_error_new(engine_state, &err);
let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, &err);
return false; return false;
} }
} }

View file

@ -3,8 +3,7 @@ use nu_parser::parse;
use nu_protocol::{ use nu_protocol::{
debugger::WithoutDebug, debugger::WithoutDebug,
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
eval_const::create_nu_constant, PipelineData, ShellError, Span, Value,
PipelineData, ShellError, Span, Value, NU_VARIABLE_ID,
}; };
use nu_test_support::fs; use nu_test_support::fs;
use reedline::Suggestion; use reedline::Suggestion;
@ -28,9 +27,7 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
let mut engine_state = create_default_context(); let mut engine_state = create_default_context();
// Add $nu // Add $nu
let nu_const = engine_state.generate_nu_constant();
create_nu_constant(&engine_state, Span::test_data()).expect("Failed creating $nu");
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
// New stack // New stack
let mut stack = Stack::new(); let mut stack = Stack::new();

View file

@ -1,6 +1,6 @@
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack},
report_error, Range, ShellError, Span, Value, report_error_new, Range, ShellError, Span, Value,
}; };
use std::{ops::Bound, path::PathBuf}; use std::{ops::Bound, path::PathBuf};
@ -14,8 +14,7 @@ pub fn get_init_cwd() -> PathBuf {
pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf { pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf {
engine_state.cwd(Some(stack)).unwrap_or_else(|e| { engine_state.cwd(Some(stack)).unwrap_or_else(|e| {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &e);
report_error(&working_set, &e);
crate::util::get_init_cwd() crate::util::get_init_cwd()
}) })
} }

View file

@ -32,7 +32,7 @@ enum ConversionResult {
/// It returns Option instead of Result since we do want to translate all the values we can and /// It returns Option instead of Result since we do want to translate all the values we can and
/// skip errors. This function is called in the main() so we want to keep running, we cannot just /// skip errors. This function is called in the main() so we want to keep running, we cannot just
/// exit. /// exit.
pub fn convert_env_values(engine_state: &mut EngineState, stack: &Stack) -> Option<ShellError> { pub fn convert_env_values(engine_state: &mut EngineState, stack: &Stack) -> Result<(), ShellError> {
let mut error = None; let mut error = None;
let mut new_scope = HashMap::new(); let mut new_scope = HashMap::new();
@ -85,7 +85,11 @@ pub fn convert_env_values(engine_state: &mut EngineState, stack: &Stack) -> Opti
}); });
} }
error if let Some(err) = error {
Err(err)
} else {
Ok(())
}
} }
/// Translate one environment variable from Value to String /// Translate one environment variable from Value to String

View file

@ -7,8 +7,7 @@ use miette::{IntoDiagnostic, Result};
use nu_parser::parse; use nu_parser::parse;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, StateWorkingSet}, engine::{EngineState, StateWorkingSet},
eval_const::create_nu_constant, Value,
Span, Value, NU_VARIABLE_ID,
}; };
impl LanguageServer { impl LanguageServer {
@ -19,11 +18,7 @@ impl LanguageServer {
) -> Result<()> { ) -> Result<()> {
let cwd = std::env::current_dir().expect("Could not get current working directory."); let cwd = std::env::current_dir().expect("Could not get current working directory.");
engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy())); engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy()));
engine_state.generate_nu_constant();
let Ok(nu_const) = create_nu_constant(engine_state, Span::unknown()) else {
return Ok(());
};
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);

View file

@ -6,6 +6,7 @@ use crate::{
CachedFile, Command, CommandType, EnvVars, OverlayFrame, ScopeFrame, Stack, StateDelta, CachedFile, Command, CommandType, EnvVars, OverlayFrame, ScopeFrame, Stack, StateDelta,
Variable, Visibility, DEFAULT_OVERLAY_NAME, Variable, Visibility, DEFAULT_OVERLAY_NAME,
}, },
eval_const::create_nu_constant,
BlockId, Category, Config, DeclId, Example, FileId, HistoryConfig, Module, ModuleId, OverlayId, BlockId, Category, Config, DeclId, Example, FileId, HistoryConfig, Module, ModuleId, OverlayId,
ShellError, Signature, Span, Type, Value, VarId, VirtualPathId, ShellError, Signature, Span, Type, Value, VarId, VirtualPathId,
}; };
@ -753,8 +754,8 @@ impl EngineState {
var.const_val.as_ref() var.const_val.as_ref()
} }
pub fn set_variable_const_val(&mut self, var_id: VarId, val: Value) { pub fn generate_nu_constant(&mut self) {
self.vars[var_id].const_val = Some(val); self.vars[NU_VARIABLE_ID].const_val = Some(create_nu_constant(self, Span::unknown()));
} }
pub fn get_decl(&self, decl_id: DeclId) -> &dyn Command { pub fn get_decl(&self, decl_id: DeclId) -> &dyn Command {

View file

@ -41,7 +41,6 @@ pub fn report_error_new(
error: &(dyn miette::Diagnostic + Send + Sync + 'static), error: &(dyn miette::Diagnostic + Send + Sync + 'static),
) { ) {
let working_set = StateWorkingSet::new(engine_state); let working_set = StateWorkingSet::new(engine_state);
report_error(&working_set, error); report_error(&working_set, error);
} }

View file

@ -12,7 +12,7 @@ use std::{
}; };
/// Create a Value for `$nu`. /// Create a Value for `$nu`.
pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Value, ShellError> { pub(crate) fn create_nu_constant(engine_state: &EngineState, span: Span) -> Value {
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf { fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
#[allow(deprecated)] #[allow(deprecated)]
let cwd = engine_state.current_work_dir(); let cwd = engine_state.current_work_dir();
@ -200,7 +200,7 @@ pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Valu
}, },
); );
Ok(Value::record(record, span)) Value::record(record, span)
} }
fn eval_const_call( fn eval_const_call(

View file

@ -67,7 +67,6 @@ pub(crate) fn parse_commandline_args(
let output = parse(&mut working_set, None, commandline_args.as_bytes(), false); let output = parse(&mut working_set, None, commandline_args.as_bytes(), false);
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);
} }

View file

@ -5,7 +5,7 @@ use nu_cli::{eval_config_contents, eval_source};
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
report_error, Config, ParseError, PipelineData, Spanned, report_error, report_error_new, Config, ParseError, PipelineData, Spanned,
}; };
use nu_utils::{get_default_config, get_default_env}; use nu_utils::{get_default_config, get_default_env};
use std::{ use std::{
@ -152,13 +152,11 @@ pub(crate) fn read_default_env_file(engine_state: &mut EngineState, stack: &mut
match engine_state.cwd(Some(stack)) { match engine_state.cwd(Some(stack)) {
Ok(cwd) => { Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) { if let Err(e) = engine_state.merge_env(stack, cwd) {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &e);
report_error(&working_set, &e);
} }
} }
Err(e) => { Err(e) => {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &e);
report_error(&working_set, &e);
} }
} }
} }
@ -193,13 +191,11 @@ fn eval_default_config(
match engine_state.cwd(Some(stack)) { match engine_state.cwd(Some(stack)) {
Ok(cwd) => { Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) { if let Err(e) = engine_state.merge_env(stack, cwd) {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &e);
report_error(&working_set, &e);
} }
} }
Err(e) => { Err(e) => {
let working_set = StateWorkingSet::new(engine_state); report_error_new(engine_state, &e);
report_error(&working_set, &e);
} }
} }
} }

View file

@ -3,8 +3,7 @@ use nu_cli::NuCompleter;
use nu_parser::{flatten_block, parse, FlatShape}; use nu_parser::{flatten_block, parse, FlatShape};
use nu_protocol::{ use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet}, engine::{EngineState, Stack, StateWorkingSet},
eval_const::create_nu_constant, report_error_new, DeclId, ShellError, Span, Value, VarId,
report_error, DeclId, ShellError, Span, Value, VarId, NU_VARIABLE_ID,
}; };
use reedline::Completer; use reedline::Completer;
use serde_json::{json, Value as JsonValue}; use serde_json::{json, Value as JsonValue};
@ -56,9 +55,8 @@ fn read_in_file<'a>(
let file = std::fs::read(file_path) let file = std::fs::read(file_path)
.into_diagnostic() .into_diagnostic()
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
let working_set = StateWorkingSet::new(engine_state); report_error_new(
report_error( engine_state,
&working_set,
&ShellError::FileNotFoundCustom { &ShellError::FileNotFoundCustom {
msg: format!("Could not read file '{}': {:?}", file_path, e.to_string()), msg: format!("Could not read file '{}': {:?}", file_path, e.to_string()),
span: Span::unknown(), span: Span::unknown(),
@ -77,16 +75,7 @@ fn read_in_file<'a>(
pub fn check(engine_state: &mut EngineState, file_path: &str, max_errors: &Value) { pub fn check(engine_state: &mut EngineState, file_path: &str, max_errors: &Value) {
let cwd = std::env::current_dir().expect("Could not get current working directory."); let cwd = std::env::current_dir().expect("Could not get current working directory.");
engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy())); engine_state.add_env_var("PWD".into(), Value::test_string(cwd.to_string_lossy()));
let working_set = StateWorkingSet::new(engine_state); engine_state.generate_nu_constant();
let nu_const = match create_nu_constant(engine_state, Span::unknown()) {
Ok(nu_const) => nu_const,
Err(err) => {
report_error(&working_set, &err);
std::process::exit(1);
}
};
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
let mut working_set = StateWorkingSet::new(engine_state); let mut working_set = StateWorkingSet::new(engine_state);
let file = std::fs::read(file_path); let file = std::fs::read(file_path);

View file

@ -27,8 +27,8 @@ use nu_cmd_base::util::get_init_cwd;
use nu_lsp::LanguageServer; use nu_lsp::LanguageServer;
use nu_path::canonicalize_with; use nu_path::canonicalize_with;
use nu_protocol::{ use nu_protocol::{
engine::EngineState, eval_const::create_nu_constant, report_error_new, util::BufferedReader, engine::EngineState, report_error_new, util::BufferedReader, PipelineData, RawStream,
PipelineData, RawStream, ShellError, Span, Value, NU_VARIABLE_ID, ShellError, Span, Value,
}; };
use nu_std::load_standard_library; use nu_std::load_standard_library;
use nu_utils::utils::perf; use nu_utils::utils::perf;
@ -378,8 +378,7 @@ fn main() -> Result<()> {
start_time = std::time::Instant::now(); start_time = std::time::Instant::now();
// Set up the $nu constant before evaluating config files (need to have $nu available in them) // Set up the $nu constant before evaluating config files (need to have $nu available in them)
let nu_const = create_nu_constant(&engine_state, input.span().unwrap_or_else(Span::unknown))?; engine_state.generate_nu_constant();
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
perf( perf(
"create_nu_constant", "create_nu_constant",
start_time, start_time,
@ -462,7 +461,8 @@ fn main() -> Result<()> {
&commands, &commands,
input, input,
entire_start_time, entire_start_time,
) );
Ok(())
} else if !script_name.is_empty() { } else if !script_name.is_empty() {
run_file( run_file(
&mut engine_state, &mut engine_state,
@ -471,7 +471,8 @@ fn main() -> Result<()> {
script_name, script_name,
args_to_script, args_to_script,
input, input,
) );
Ok(())
} else { } else {
run_repl(&mut engine_state, parsed_nu_cli_args, entire_start_time) run_repl(&mut engine_state, parsed_nu_cli_args, entire_start_time)
} }

View file

@ -8,19 +8,22 @@ use log::trace;
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
use nu_cli::read_plugin_file; use nu_cli::read_plugin_file;
use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl}; use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl};
use nu_protocol::{eval_const::create_nu_constant, PipelineData, Span, NU_VARIABLE_ID}; use nu_protocol::{
engine::{EngineState, Stack},
report_error_new, PipelineData, Spanned,
};
use nu_utils::utils::perf; use nu_utils::utils::perf;
pub(crate) fn run_commands( pub(crate) fn run_commands(
engine_state: &mut nu_protocol::engine::EngineState, engine_state: &mut EngineState,
parsed_nu_cli_args: command::NushellCliArgs, parsed_nu_cli_args: command::NushellCliArgs,
use_color: bool, use_color: bool,
commands: &nu_protocol::Spanned<String>, commands: &Spanned<String>,
input: PipelineData, input: PipelineData,
entire_start_time: std::time::Instant, entire_start_time: std::time::Instant,
) -> Result<(), miette::ErrReport> { ) {
trace!("run_commands"); trace!("run_commands");
let mut stack = nu_protocol::engine::Stack::new(); let mut stack = Stack::new();
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
// if the --no-config-file(-n) option is NOT passed, load the plugin file, // if the --no-config-file(-n) option is NOT passed, load the plugin file,
@ -103,18 +106,20 @@ pub(crate) fn run_commands(
engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64); engine_state.set_startup_time(entire_start_time.elapsed().as_nanos() as i64);
// Regenerate the $nu constant to contain the startup time and any other potential updates // Regenerate the $nu constant to contain the startup time and any other potential updates
let nu_const = create_nu_constant(engine_state, commands.span)?; engine_state.generate_nu_constant();
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let ret_val = evaluate_commands( if let Err(err) = evaluate_commands(
commands, commands,
engine_state, engine_state,
&mut stack, &mut stack,
input, input,
parsed_nu_cli_args.table_mode, parsed_nu_cli_args.table_mode,
parsed_nu_cli_args.no_newline.is_some(), parsed_nu_cli_args.no_newline.is_some(),
); ) {
report_error_new(engine_state, &err);
std::process::exit(1);
}
perf( perf(
"evaluate_commands", "evaluate_commands",
start_time, start_time,
@ -123,24 +128,18 @@ pub(crate) fn run_commands(
column!(), column!(),
use_color, use_color,
); );
match ret_val {
Ok(Some(exit_code)) => std::process::exit(exit_code as i32),
Ok(None) => Ok(()),
Err(e) => Err(e),
}
} }
pub(crate) fn run_file( pub(crate) fn run_file(
engine_state: &mut nu_protocol::engine::EngineState, engine_state: &mut EngineState,
parsed_nu_cli_args: command::NushellCliArgs, parsed_nu_cli_args: command::NushellCliArgs,
use_color: bool, use_color: bool,
script_name: String, script_name: String,
args_to_script: Vec<String>, args_to_script: Vec<String>,
input: PipelineData, input: PipelineData,
) -> Result<(), miette::ErrReport> { ) {
trace!("run_file"); trace!("run_file");
let mut stack = nu_protocol::engine::Stack::new(); let mut stack = Stack::new();
// if the --no-config-file(-n) option is NOT passed, load the plugin file, // if the --no-config-file(-n) option is NOT passed, load the plugin file,
// load the default env file or custom (depending on parsed_nu_cli_args.env_file), // load the default env file or custom (depending on parsed_nu_cli_args.env_file),
@ -201,17 +200,19 @@ pub(crate) fn run_file(
} }
// Regenerate the $nu constant to contain the startup time and any other potential updates // Regenerate the $nu constant to contain the startup time and any other potential updates
let nu_const = create_nu_constant(engine_state, input.span().unwrap_or_else(Span::unknown))?; engine_state.generate_nu_constant();
engine_state.set_variable_const_val(NU_VARIABLE_ID, nu_const);
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let ret_val = evaluate_file( if let Err(err) = evaluate_file(
script_name, script_name,
&args_to_script, &args_to_script,
engine_state, engine_state,
&mut stack, &mut stack,
input, input,
); ) {
report_error_new(engine_state, &err);
std::process::exit(1);
}
perf( perf(
"evaluate_file", "evaluate_file",
start_time, start_time,
@ -239,17 +240,15 @@ pub(crate) fn run_file(
column!(), column!(),
use_color, use_color,
); );
ret_val
} }
pub(crate) fn run_repl( pub(crate) fn run_repl(
engine_state: &mut nu_protocol::engine::EngineState, engine_state: &mut EngineState,
parsed_nu_cli_args: command::NushellCliArgs, parsed_nu_cli_args: command::NushellCliArgs,
entire_start_time: std::time::Instant, entire_start_time: std::time::Instant,
) -> Result<(), miette::ErrReport> { ) -> Result<(), miette::ErrReport> {
trace!("run_repl"); trace!("run_repl");
let mut stack = nu_protocol::engine::Stack::new(); let mut stack = Stack::new();
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
if parsed_nu_cli_args.no_config_file.is_none() { if parsed_nu_cli_args.no_config_file.is_none() {