Only ask to create config files the first time nu is started (#13857)

# Description

Implements #13669

When nu is started for the first time, the directory represented by
`$nu.default-config-dir` typically will not exist. In this case, Nushell
will create the directory. It will then detect that either or both
`config.nu`/`env.nu` don't exist and offer to create them.

(Existing behavior) If the user declines, the directory will still be
created (since the history file lives there as well). The
`default_config.nu` and `default_env.nu` will be loaded.

On subsequent launches, as long as the config directory continues to
exist, the user will not be prompted to recreate the config files.
Nushell will behave as if the user answered "N" to the prompt in that
`default_config.nu` and `default_env.nu` will be used.

The user can still create a `config.nu` or `env.nu` at any point, and
that will be used. In that case, `default_config.nu` and/or
`default_env.nu` will no longer be loaded (unless and until #13671 is
implemented).

# User-Facing Changes

User will no longer be prompted to create config files if they are
missing so long as the config directory exists.

## Before this change:

1. Nushell starts for the first time
2. The directory where config files are stored does not exist
3. The config files do not exist
   * User is asked whether they want to create `env.nu`
- User says, "Y", `default_env.nu` is copied to the directory as
`env.nu` (and directory is created if needed)
- User says, "n", `default_env.nu` is loaded, but no file on the
filesystem is created.
 
   * User is asked whether they want to create `config.nu`
- User says, "Y", `default_config.nu` is copied to the directory as
`config.nu` (and directory is created if needed)
- User says, "n", `default_config.nu` is loaded, but no file on the
filesystem is created.

4. The next time `nu` is run, if either file is missing, the user will
be prompted again for that file.

## After this change:

Steps 1 - 3 remains the same.

4. The next time `nu` is run, we check if the directory exists.  If so:
5. Do not prompt user to create any missing files **(New Behavior)**
6. `$nu.default-config-dir/env.nu` exists?
   * Yes? Use it. (Normal behavior)
   * No?  Evaluate `default_env.nu`.  (Normal behavior)
   * No file is created on the filesystem

7. `$nu.default-config-dir/config.nu` exists?
   * Yes? Use it. (Normal behavior)
   * No?  Evaluate `default_config.nu` (Normal behavior)
   * No file is created on the filesystem

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

This behavior isn't currently mentioned in the configuration doc. I'll
probably hold off on changing anything in the doc until #13671 is
implemented. Regardless, given the timing, this won't make it into a
release for at least 4 weeks.
This commit is contained in:
Douglas 2024-09-26 14:54:42 -04:00 committed by GitHub
parent 0c72f881a6
commit d68c3ec89a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 63 additions and 19 deletions

View file

@ -27,6 +27,7 @@ pub(crate) fn read_config_file(
stack: &mut Stack, stack: &mut Stack,
config_file: Option<Spanned<String>>, config_file: Option<Spanned<String>>,
is_env_config: bool, is_env_config: bool,
ask_to_create: bool,
) { ) {
warn!( warn!(
"read_config_file() config_file_specified: {:?}, is_env_config: {is_env_config}", "read_config_file() config_file_specified: {:?}, is_env_config: {is_env_config}",
@ -66,6 +67,9 @@ pub(crate) fn read_config_file(
} else { } else {
"config" "config"
}; };
let will_create_file = match ask_to_create {
true => {
println!( println!(
"No {} file found at {}", "No {} file found at {}",
file_msg, file_msg,
@ -78,14 +82,19 @@ pub(crate) fn read_config_file(
.read_line(&mut answer) .read_line(&mut answer)
.expect("Failed to read user input"); .expect("Failed to read user input");
matches!(answer.trim(), "y" | "Y" | "")
}
_ => false,
};
let config_file = if is_env_config { let config_file = if is_env_config {
get_default_env() get_default_env()
} else { } else {
get_default_config() get_default_config()
}; };
match answer.trim() { match will_create_file {
"y" | "Y" | "" => { true => {
if let Ok(mut output) = File::create(&config_path) { if let Ok(mut output) = File::create(&config_path) {
if write!(output, "{config_file}").is_ok() { if write!(output, "{config_file}").is_ok() {
let config_type = if is_env_config { let config_type = if is_env_config {
@ -226,7 +235,6 @@ fn eval_default_config(
"eval_default_config() config_file_specified: {:?}, is_env_config: {}", "eval_default_config() config_file_specified: {:?}, is_env_config: {}",
&config_file, is_env_config &config_file, is_env_config
); );
println!("Continuing without config file");
// Just use the contents of "default_config.nu" or "default_env.nu" // Just use the contents of "default_config.nu" or "default_env.nu"
eval_source( eval_source(
engine_state, engine_state,
@ -259,12 +267,26 @@ pub(crate) fn setup_config(
"setup_config() config_file_specified: {:?}, env_file_specified: {:?}, login: {}", "setup_config() config_file_specified: {:?}, env_file_specified: {:?}, login: {}",
&config_file, &env_file, is_login_shell &config_file, &env_file, is_login_shell
); );
let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(NUSHELL_FOLDER);
!config_path.exists()
} else {
false
};
let result = catch_unwind(AssertUnwindSafe(|| { let result = catch_unwind(AssertUnwindSafe(|| {
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
read_plugin_file(engine_state, plugin_file, NUSHELL_FOLDER); read_plugin_file(engine_state, plugin_file, NUSHELL_FOLDER);
read_config_file(engine_state, stack, env_file, true); read_config_file(engine_state, stack, env_file, true, ask_to_create_config);
read_config_file(engine_state, stack, config_file, false); read_config_file(
engine_state,
stack,
config_file,
false,
ask_to_create_config,
);
if is_login_shell { if is_login_shell {
read_loginshell_file(engine_state, stack); read_loginshell_file(engine_state, stack);

View file

@ -1,8 +1,6 @@
#[cfg(feature = "plugin")]
use crate::config_files::NUSHELL_FOLDER;
use crate::{ use crate::{
command, command,
config_files::{self, setup_config}, config_files::{self, setup_config, NUSHELL_FOLDER},
}; };
use log::trace; use log::trace;
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
@ -26,6 +24,13 @@ pub(crate) fn run_commands(
let mut stack = Stack::new(); let mut stack = Stack::new();
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(NUSHELL_FOLDER);
!config_path.exists()
} else {
false
};
if stack.has_env_var(engine_state, "NU_DISABLE_IR") { if stack.has_env_var(engine_state, "NU_DISABLE_IR") {
stack.use_ir = false; stack.use_ir = false;
} }
@ -49,6 +54,7 @@ pub(crate) fn run_commands(
&mut stack, &mut stack,
parsed_nu_cli_args.env_file, parsed_nu_cli_args.env_file,
true, true,
ask_to_create_config,
); );
} else { } else {
config_files::read_default_env_file(engine_state, &mut stack) config_files::read_default_env_file(engine_state, &mut stack)
@ -57,6 +63,13 @@ pub(crate) fn run_commands(
perf!("read env.nu", start_time, use_color); perf!("read env.nu", start_time, use_color);
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(config_files::NUSHELL_FOLDER);
!config_path.exists()
} else {
false
};
// If we have a config file parameter *OR* we have a login shell parameter, read the config file // If we have a config file parameter *OR* we have a login shell parameter, read the config file
if parsed_nu_cli_args.config_file.is_some() || parsed_nu_cli_args.login_shell.is_some() { if parsed_nu_cli_args.config_file.is_some() || parsed_nu_cli_args.login_shell.is_some() {
config_files::read_config_file( config_files::read_config_file(
@ -64,6 +77,7 @@ pub(crate) fn run_commands(
&mut stack, &mut stack,
parsed_nu_cli_args.config_file, parsed_nu_cli_args.config_file,
false, false,
ask_to_create_config,
); );
} }
@ -126,6 +140,12 @@ pub(crate) fn run_file(
// if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files // if the --no-config-file(-n) flag is passed, do not load plugin, env, or config files
if parsed_nu_cli_args.no_config_file.is_none() { if parsed_nu_cli_args.no_config_file.is_none() {
let start_time = std::time::Instant::now(); let start_time = std::time::Instant::now();
let ask_to_create_config = if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(config_files::NUSHELL_FOLDER);
!config_path.exists()
} else {
false
};
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER); read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file, NUSHELL_FOLDER);
perf!("read plugins", start_time, use_color); perf!("read plugins", start_time, use_color);
@ -138,6 +158,7 @@ pub(crate) fn run_file(
&mut stack, &mut stack,
parsed_nu_cli_args.env_file, parsed_nu_cli_args.env_file,
true, true,
ask_to_create_config,
); );
} else { } else {
config_files::read_default_env_file(engine_state, &mut stack) config_files::read_default_env_file(engine_state, &mut stack)
@ -151,6 +172,7 @@ pub(crate) fn run_file(
&mut stack, &mut stack,
parsed_nu_cli_args.config_file, parsed_nu_cli_args.config_file,
false, false,
ask_to_create_config,
); );
} }
perf!("read config.nu", start_time, use_color); perf!("read config.nu", start_time, use_color);
@ -208,7 +230,7 @@ pub(crate) fn run_repl(
let ret_val = evaluate_repl( let ret_val = evaluate_repl(
engine_state, engine_state,
stack, stack,
config_files::NUSHELL_FOLDER, NUSHELL_FOLDER,
parsed_nu_cli_args.execute, parsed_nu_cli_args.execute,
parsed_nu_cli_args.no_std_lib, parsed_nu_cli_args.no_std_lib,
entire_start_time, entire_start_time,