Fix panic when assigning value to $env (#7894)

This commit is contained in:
Jakub Žádník 2023-01-28 21:17:32 +02:00 committed by GitHub
parent 3c6b10c6b2
commit 2a39332d51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -467,6 +467,12 @@ pub fn eval_expression(
lhs.upsert_data_at_cell_path(&cell_path.tail, rhs)?;
if is_env {
if cell_path.tail.is_empty() {
return Err(ShellError::CannotReplaceEnv(
cell_path.head.span,
));
}
// The special $env treatment: for something like $env.config.history.max_size = 2000,
// get $env.config (or whichever one it is) AFTER the above mutation, and set it
// as the "config" environment variable.

View File

@ -365,6 +365,20 @@ Either make sure {0} is a string, or add a 'to_string' entry for it in ENV_CONVE
)]
AutomaticEnvVarSetManually(String, #[label("cannot set '{0}' manually")] Span),
/// It is not possible to replace the entire environment at once
///
/// ## Resolution
///
/// Setting the entire environment is not allowed. Change environment variables individually
/// instead.
#[error("Cannot replace environment.")]
#[diagnostic(
code(nu::shell::cannot_replace_env),
url(docsrs),
help(r#"Assigning a value to $env is not allowed."#)
)]
CannotReplaceEnv(#[label("setting $env not allowed")] Span),
/// Division by zero is not a thing.
///
/// ## Resolution

View File

@ -371,3 +371,8 @@ fn range_right_exclusive() -> TestResult {
fn assignment_to_in_var_no_panic() -> TestResult {
fail_test(r#"$in = 3"#, "needs to be a mutable variable")
}
#[test]
fn assignment_to_env_no_panic() -> TestResult {
fail_test(r#"$env = 3"#, "cannot_replace_env")
}