From 99ba365c4afe4506ca7f1201b2596db7e5afbbc9 Mon Sep 17 00:00:00 2001 From: Jack Wright <56345+ayax79@users.noreply.github.com> Date: Thu, 22 Feb 2024 14:25:55 -0800 Subject: [PATCH] Handle configuration panics (#11935) Use the default configuration on panic. Adding a line that panics to any configuration: ```nushell # Nushell Config File # # version = "0.86.0" "2031-13-31" | into datetime ``` An error message will be displayed and the shell will continue: Screenshot 2024-02-22 at 10 14 25 Co-authored-by: Jack Wright --- src/config_files.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/config_files.rs b/src/config_files.rs index 6bcd314612..b38f4a970c 100644 --- a/src/config_files.rs +++ b/src/config_files.rs @@ -4,11 +4,12 @@ use nu_cli::read_plugin_file; use nu_cli::{eval_config_contents, eval_source}; use nu_path::canonicalize_with; use nu_protocol::engine::{EngineState, Stack, StateWorkingSet}; -use nu_protocol::report_error; +use nu_protocol::{report_error, Config}; use nu_protocol::{ParseError, PipelineData, Spanned}; use nu_utils::{get_default_config, get_default_env}; use std::fs::File; use std::io::Write; +use std::panic::{catch_unwind, AssertUnwindSafe}; use std::path::Path; pub(crate) const NUSHELL_FOLDER: &str = "nushell"; @@ -194,14 +195,22 @@ pub(crate) fn setup_config( env_file: Option>, is_login_shell: bool, ) { - #[cfg(feature = "plugin")] - read_plugin_file(engine_state, stack, plugin_file, NUSHELL_FOLDER); + let result = catch_unwind(AssertUnwindSafe(|| { + #[cfg(feature = "plugin")] + read_plugin_file(engine_state, stack, plugin_file, NUSHELL_FOLDER); - read_config_file(engine_state, stack, env_file, true); - read_config_file(engine_state, stack, config_file, false); + read_config_file(engine_state, stack, env_file, true); + read_config_file(engine_state, stack, config_file, false); - if is_login_shell { - read_loginshell_file(engine_state, stack); + if is_login_shell { + read_loginshell_file(engine_state, stack); + } + })); + if result.is_err() { + eprintln!( + "A panic occurred while reading configuration files, using default configuration." + ); + engine_state.config = Config::default() } }