diff --git a/crates/nu-command/src/core_commands/overlay/use_.rs b/crates/nu-command/src/core_commands/overlay/use_.rs index 20eee56795..61df0f9fc2 100644 --- a/crates/nu-command/src/core_commands/overlay/use_.rs +++ b/crates/nu-command/src/core_commands/overlay/use_.rs @@ -130,6 +130,9 @@ impl Command for OverlayUse { if let Some(block_id) = module.env_block { let maybe_path = find_in_dirs_env(&name_arg.item, engine_state, caller_stack)?; + let block = engine_state.get_block(block_id); + let mut callee_stack = caller_stack.gather_captures(&block.captures); + if let Some(path) = &maybe_path { // Set the currently evaluated directory, if the argument is a valid path let mut parent = path.clone(); @@ -137,12 +140,9 @@ impl Command for OverlayUse { let file_pwd = Value::string(parent.to_string_lossy(), call.head); - caller_stack.add_env_var("FILE_PWD".to_string(), file_pwd); + callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd); } - let block = engine_state.get_block(block_id); - let mut callee_stack = caller_stack.gather_captures(&block.captures); - let _ = eval_block( engine_state, &mut callee_stack, @@ -157,11 +157,6 @@ impl Command for OverlayUse { // Merge the block's environment to the current stack redirect_env(engine_state, caller_stack, &callee_stack); - - if maybe_path.is_some() { - // Remove the file-relative PWD, if the argument is a valid path - caller_stack.remove_env_var(engine_state, "FILE_PWD"); - } } else { caller_stack.add_overlay(overlay_name); }