nushell/src/run.rs
Douglas 72d50cf8b7
Convert Path to list in main and preserve case (#14764)
# Description

Fixes multiple issues related to `ENV_CONVERSION` and
path-conversion-to-list.

* #14681 removed some calls to `convert_env_values()`, but we found that
this caused `nu -n` to no longer convert the path properly.
* `ENV_CONVERSIONS` have apparently never preserved case, meaning a
conversion with a key of `foo` would not update `$env.FOO` but rather
create a new environment variable with a different case.
* There was a partial code-path that attempted to solve this for `PATH`,
but it only worked for `PATH` and `Path`.
* `convert_env_values()`, which handled `ENV_CONVERSIONS` was called in
multiple places in the startup depending on flags.

This PR:

* Refactors the startup to handle the conversion in `main()` rather than
in each potential startup path
* Updates `get_env_var_insensitive()` functions added in #14390 to
return the name of the environment variable with its original case. This
allows code that updates environment variables to preserve the case.
* Makes use of the updated function in `ENV_CONVERSIONS` to preserve the
case of any updated environment variables. The `ENV_CONVERSION` key
itself is still case **insensitive**.
* Makes use of the updated function to preserve the case of the `PATH`
environment variable (normally handled separately, regardless of whether
or not there was an `ENV_CONVERSION` for it).

## Before

`env_convert_values` was run:

* Before the user `env.nu` ran, which included `nu -c <commandstring>`
and `nu <script.nu>`
* Before the REPL loaded, which included `nu -n`

## After

`env_convert_values` always runs once in `main()` before any config file
is processed or the REPL is started

# User-Facing Changes

Bug fixes

# Tests + Formatting

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

Added additional tests to prevent future regression.

# After Submitting

There is additional cleanup that should probably be done in
`convert_env_values()`. This function previously handled
`ENV_CONVERSIONS`, but there is no longer any need for this since
`convert_env_vars()` runs whenever `$env.ENV_CONVERSIONS` changes now.

This means that the only relevant task in the old `convert_env_values()`
is to convert the `PATH` to a list, and ensure that it is a list of
strings. It's still calling the `from_string` conversion on every
variable (just once) even though there are no `ENV_CONVERSIONS` at this
point.

Leaving that to another PR though, while we get the core issue fixed
with this one.
2025-01-10 10:18:44 -06:00

217 lines
7.2 KiB
Rust

use crate::{
command,
config_files::{self, setup_config},
};
use log::trace;
#[cfg(feature = "plugin")]
use nu_cli::read_plugin_file;
use nu_cli::{evaluate_commands, evaluate_file, evaluate_repl, EvaluateCommandsOpts};
use nu_protocol::{
engine::{EngineState, Stack},
report_shell_error, PipelineData, Spanned,
};
use nu_utils::perf;
pub(crate) fn run_commands(
engine_state: &mut EngineState,
mut stack: Stack,
parsed_nu_cli_args: command::NushellCliArgs,
use_color: bool,
commands: &Spanned<String>,
input: PipelineData,
entire_start_time: std::time::Instant,
) {
trace!("run_commands");
let start_time = std::time::Instant::now();
let create_scaffold = nu_path::nu_config_dir().map_or(false, |p| !p.exists());
// 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),
// and maybe a custom config file (depending on parsed_nu_cli_args.config_file)
//
// 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() {
#[cfg(feature = "plugin")]
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
perf!("read plugins", start_time, use_color);
let start_time = std::time::Instant::now();
// If we have a env file parameter *OR* we have a login shell parameter, read the env file
if parsed_nu_cli_args.env_file.is_some() || parsed_nu_cli_args.login_shell.is_some() {
config_files::read_config_file(
engine_state,
&mut stack,
parsed_nu_cli_args.env_file,
true,
create_scaffold,
);
} else {
config_files::read_default_env_file(engine_state, &mut stack)
}
perf!("read env.nu", start_time, use_color);
let start_time = std::time::Instant::now();
let create_scaffold = nu_path::nu_config_dir().map_or(false, |p| !p.exists());
// 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() {
config_files::read_config_file(
engine_state,
&mut stack,
parsed_nu_cli_args.config_file,
false,
create_scaffold,
);
}
perf!("read config.nu", start_time, use_color);
// If we have a login shell parameter, read the login file
let start_time = std::time::Instant::now();
if parsed_nu_cli_args.login_shell.is_some() {
config_files::read_loginshell_file(engine_state, &mut stack);
}
perf!("read login.nu", start_time, use_color);
}
// Before running commands, set up the startup time
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
engine_state.generate_nu_constant();
let start_time = std::time::Instant::now();
let result = evaluate_commands(
commands,
engine_state,
&mut stack,
input,
EvaluateCommandsOpts {
table_mode: parsed_nu_cli_args.table_mode,
error_style: parsed_nu_cli_args.error_style,
no_newline: parsed_nu_cli_args.no_newline.is_some(),
},
);
perf!("evaluate_commands", start_time, use_color);
if let Err(err) = result {
report_shell_error(engine_state, &err);
std::process::exit(err.exit_code().unwrap_or(0));
}
}
pub(crate) fn run_file(
engine_state: &mut EngineState,
mut stack: Stack,
parsed_nu_cli_args: command::NushellCliArgs,
use_color: bool,
script_name: String,
args_to_script: Vec<String>,
input: PipelineData,
) {
trace!("run_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),
// and maybe a custom config file (depending on parsed_nu_cli_args.config_file)
//
// 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() {
let start_time = std::time::Instant::now();
let create_scaffold = nu_path::nu_config_dir().map_or(false, |p| !p.exists());
#[cfg(feature = "plugin")]
read_plugin_file(engine_state, parsed_nu_cli_args.plugin_file);
perf!("read plugins", start_time, use_color);
let start_time = std::time::Instant::now();
// only want to load config and env if relative argument is provided.
if parsed_nu_cli_args.env_file.is_some() {
config_files::read_config_file(
engine_state,
&mut stack,
parsed_nu_cli_args.env_file,
true,
create_scaffold,
);
} else {
config_files::read_default_env_file(engine_state, &mut stack)
}
perf!("read env.nu", start_time, use_color);
let start_time = std::time::Instant::now();
if parsed_nu_cli_args.config_file.is_some() {
config_files::read_config_file(
engine_state,
&mut stack,
parsed_nu_cli_args.config_file,
false,
create_scaffold,
);
}
perf!("read config.nu", start_time, use_color);
}
// Regenerate the $nu constant to contain the startup time and any other potential updates
engine_state.generate_nu_constant();
let start_time = std::time::Instant::now();
let result = evaluate_file(
script_name,
&args_to_script,
engine_state,
&mut stack,
input,
);
perf!("evaluate_file", start_time, use_color);
if let Err(err) = result {
report_shell_error(engine_state, &err);
std::process::exit(err.exit_code().unwrap_or(0));
}
}
pub(crate) fn run_repl(
engine_state: &mut EngineState,
mut stack: Stack,
parsed_nu_cli_args: command::NushellCliArgs,
entire_start_time: std::time::Instant,
) -> Result<(), miette::ErrReport> {
trace!("run_repl");
let start_time = std::time::Instant::now();
if parsed_nu_cli_args.no_config_file.is_none() {
setup_config(
engine_state,
&mut stack,
#[cfg(feature = "plugin")]
parsed_nu_cli_args.plugin_file,
parsed_nu_cli_args.config_file,
parsed_nu_cli_args.env_file,
parsed_nu_cli_args.login_shell.is_some(),
);
}
// Reload use_color from config in case it's different from the default value
let use_color = engine_state
.get_config()
.use_ansi_coloring
.get(engine_state);
perf!("setup_config", start_time, use_color);
let start_time = std::time::Instant::now();
let ret_val = evaluate_repl(
engine_state,
stack,
parsed_nu_cli_args.execute,
parsed_nu_cli_args.no_std_lib,
entire_start_time,
);
perf!("evaluate_repl", start_time, use_color);
ret_val
}