Skip to content

Commit

Permalink
Handle configuration panics (#11935)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ayax79 and ayax79 committed Feb 22, 2024
1 parent 28f5805 commit 99ba365
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/config_files.rs
Expand Up @@ -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";
Expand Down Expand Up @@ -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()
}
}

Expand Down

0 comments on commit 99ba365

Please sign in to comment.