Skip to content

Commit

Permalink
Allow $env to be mutated by =
Browse files Browse the repository at this point in the history
  • Loading branch information
webbedspace committed Dec 2, 2022
1 parent 94c89eb commit 661d9d6
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions crates/nu-engine/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,30 +491,39 @@ pub fn eval_expression(
}
Expr::FullCellPath(cell_path) => match &cell_path.head.expr {
Expr::Var(var_id) | Expr::VarDecl(var_id) => {
if var_id == &ENV_VARIABLE_ID {
// let mut lhs =
// eval_expression(engine_state, stack, &cell_path.head)?;
//lhs.update_data_at_cell_path(&cell_path.tail, rhs)?;
match &cell_path.tail[0] {
PathMember::String { val, .. } => {
stack.add_env_var(val.to_string(), rhs);
// The $env variable is considered "mutable" in Nushell.
// As such, give it special treatment here.
let is_env = var_id == &ENV_VARIABLE_ID;
if is_env || engine_state.get_var(*var_id).mutable {
let mut lhs =
eval_expression(engine_state, stack, &cell_path.head)?;

if is_env {
// Unlike "real" mutable vars, though, $env can be upserted by =
// instead of just updated. This allows external env-vars like $env.PYTHON_IO_ENCODING
// to be added using this syntax.
lhs.upsert_data_at_cell_path(&cell_path.tail, rhs)?;
// The special $env treatment: for something like $env.config.history.max_size = 2000,
// get $env.config AFTER the above mutation, and set it as the "config" environment variable.
let vardata = lhs.follow_cell_path(
&[cell_path.tail[0].clone()],
false,
)?;
match &cell_path.tail[0] {
PathMember::String { val, .. } => {
stack.add_env_var(val.to_string(), vardata);
}
PathMember::Int { val, .. } => {
stack.add_env_var(val.to_string(), vardata);
}
}
PathMember::Int { val, .. } => {
stack.add_env_var(val.to_string(), rhs);
}
}
Ok(Value::nothing(cell_path.head.span))
} else {
let var_info = engine_state.get_var(*var_id);
if var_info.mutable {
let mut lhs =
eval_expression(engine_state, stack, &cell_path.head)?;
} else {
lhs.update_data_at_cell_path(&cell_path.tail, rhs)?;
stack.vars.insert(*var_id, lhs);
Ok(Value::nothing(cell_path.head.span))
} else {
Err(ShellError::AssignmentRequiresMutableVar(lhs.span))
}
Ok(Value::nothing(cell_path.head.span))
} else {
Err(ShellError::AssignmentRequiresMutableVar(lhs.span))
}
}
_ => Err(ShellError::AssignmentRequiresVar(lhs.span)),
Expand Down

0 comments on commit 661d9d6

Please sign in to comment.