Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow $env and mutable records to be mutated by = (closes #7110) #7318

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Allow $env to be mutated by =
  • Loading branch information
webbedspace committed Dec 4, 2022
commit 0bd120f2c35ed4ff54a6ac923e72fdf8134d8a49
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