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:
<img width="1016" alt="Screenshot 2024-02-22 at 10 14 25"
src="https://github.com/nushell/nushell/assets/56345/8ccff001-300a-4caf-b131-bf7b114a06e3">

Co-authored-by: Jack Wright <jack.wright@disqo.com>
This commit is contained in:
Jack Wright 2024-02-22 14:25:55 -08:00 committed by GitHub
parent 28f58057b6
commit 99ba365c4a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<Spanned<String>>,
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()
}
}