Fix warning on declared config variable that originates from commands (#8891)

# Description

This PR fixes an issue described in #8890 where config variables
declared in command parameters cause the warning `use `let-env config =
...` instead of `let config = ...` to be printed.

# User-Facing Changes
The user is only warned when they define a config variable with a
warning with the type `record`.

# Tests + Formatting

I think this can only be tested manually by first trying to reproduce
#8890.
To test if the warning is still printed when it's supposed to be one can
add `let config = $env.config` to the end of the `config.nu` file.
This commit is contained in:
Julius Riegel 2023-04-26 16:21:51 +02:00 committed by GitHub
parent c422c6cc3d
commit 07c9f681c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,7 +4,7 @@ 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, Span};
use nu_protocol::{ParseError, PipelineData, Spanned};
use nu_utils::{get_default_config, get_default_env};
use std::fs::File;
@ -207,8 +207,13 @@ pub(crate) fn setup_config(
// Give a warning if we see `$config` for a few releases
{
let working_set = StateWorkingSet::new(engine_state);
if working_set.find_variable(b"$config").is_some() {
println!("warning: use `let-env config = ...` instead of `let config = ...`");
if let Some(var) = working_set
.find_variable(b"$config")
.and_then(|id| stack.get_var(id, Span::unknown()).ok())
{
if var.as_record().is_ok() {
println!("warning: use `let-env config = ...` instead of `let config = ...`");
}
}
}
}