Update config directly at assignment (#13332)

# Description

Allows `Stack` to have a modified local `Config`, which is updated
immediately when `$env.config` is assigned to. This means that even
within a script, commands that come after `$env.config` changes will
always see those changes in `Stack::get_config()`.

Also fixed a lot of cases where `engine_state.get_config()` was used
even when `Stack` was available.

Closes #13324.

# User-Facing Changes
- Config changes apply immediately after the assignment is executed,
rather than whenever config is read by a command that needs it.
- Potentially slower performance when executing a lot of lines that
change `$env.config` one after another. Recommended to get `$env.config`
into a `mut` variable first and do modifications, then assign it back.
- Much faster performance when executing a script that made
modifications to `$env.config`, as the changes are only parsed once.

# Tests + Formatting
All passing.

# After Submitting
- [ ] release notes
This commit is contained in:
Devyn Cairns
2024-07-11 06:09:33 -07:00
committed by GitHub
parent deaa711ca6
commit f65bc97a54
46 changed files with 327 additions and 222 deletions

View File

@ -1,4 +1,4 @@
use nu_engine::{command_prelude::*, env::get_config, find_in_dirs_env, get_dirs_var_from_call};
use nu_engine::{command_prelude::*, find_in_dirs_env, get_dirs_var_from_call};
use nu_parser::{parse, parse_module_block, parse_module_file_or_dir, unescape_unquote_string};
use nu_protocol::engine::{FileStack, StateWorkingSet};
use std::path::Path;
@ -59,7 +59,7 @@ impl Command for NuCheck {
}
}
PipelineData::ListStream(stream, ..) => {
let config = get_config(engine_state, stack);
let config = stack.get_config(engine_state);
let list_stream = stream.into_string("\n", &config);
let contents = Vec::from(list_stream);

View File

@ -366,7 +366,7 @@ pub fn command_not_found(
stack: &mut Stack,
) -> ShellError {
// Run the `command_not_found` hook if there is one.
if let Some(hook) = &engine_state.config.hooks.command_not_found {
if let Some(hook) = &stack.get_config(engine_state).hooks.command_not_found {
let mut stack = stack.start_capture();
// Set a special environment variable to avoid infinite loops when the
// `command_not_found` hook triggers itself.