Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 7 additions & 12 deletions brush-builtins/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{io::Write, sync::LazyLock};

use brush_core::{
ErrorKind, ExecutionResult, builtins,
env::{self, EnvironmentLookup, EnvironmentScope},
env::{self, EnvironmentLookup, EnvironmentScope, VarNameExt},
error,
parser::ast,
variables::{
Expand Down Expand Up @@ -214,10 +214,9 @@ impl DeclareCommand {
} else if let Some((_, variable)) = context
.shell
.env()
.lookup(name.as_str())
.bypassing_nameref()
.lookup(name.as_str().direct())
.in_scope(lookup)
.get()
.get_direct()
{
let mut cs = variable.attribute_flags(context.shell);
if cs.is_empty() {
Expand Down Expand Up @@ -344,7 +343,7 @@ impl DeclareCommand {
.env_mut()
.lookup_mut(&resolved_name)
.in_scope(lookup)
.get()
.get_direct()
{
if self.make_associative_array.is_some() {
var.convert_to_associative_array()?;
Expand Down Expand Up @@ -605,18 +604,14 @@ impl DeclareCommand {
&& context
.shell
.env()
.lookup(name.as_str())
.bypassing_nameref()
.lookup(name.as_str().direct())
.in_scope(lookup)
.get()
.get_direct()
.is_none();

let should_resolve = !explicitly_modifying_nameref_attr && !creating_new_local;
if should_resolve {
let resolved = context
.shell
.env()
.resolve_nameref(name.as_str())?;
let resolved = context.shell.env().resolve_nameref(name.as_str())?;
if resolved.name() != name.as_str() {
// For subscripted targets (e.g., ref→arr[2]), resolve to the
// base variable name. `declare -x ref` applies the export to
Expand Down
11 changes: 2 additions & 9 deletions brush-builtins/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,9 @@ impl ExportCommand {
// Check for circular namerefs upfront so we can emit a warning
// (env_mut().get_mut() silently swallows the resolution error).
if let Err(err) = context.shell.env().resolve_nameref(s)
&& matches!(
err.kind(),
error::ErrorKind::CircularNameReference(_)
)
&& matches!(err.kind(), error::ErrorKind::CircularNameReference(_))
{
writeln!(
context.stderr(),
"{}: warning: {err}",
context.command_name
)?;
writeln!(context.stderr(), "{}: warning: {err}", context.command_name)?;
} else if let Some(mut resolved) = context.shell.env_mut().get_mut(s) {
if resolved.has_subscript() {
// Resolve the nameref to get the full target string for the error.
Expand Down
18 changes: 8 additions & 10 deletions brush-builtins/src/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::borrow::Cow;

use clap::Parser;

use brush_core::{ExecutionResult, Shell, ShellValue, builtins, variables::ShellValueUnsetType};
use brush_core::{
ExecutionResult, Shell, ShellValue, builtins, env::VarNameExt, variables::ShellValueUnsetType,
};

/// Unset a variable.
#[derive(Parser)]
Expand Down Expand Up @@ -54,15 +56,11 @@ impl builtins::Command for UnsetCommand {
let is_nameref = context
.shell
.env()
.lookup(name.as_str())
.bypassing_nameref()
.get()
.lookup(name.direct())
.get_direct()
.is_some_and(|(_, v)| v.is_treated_as_nameref());
if is_nameref {
context
.shell
.env_mut()
.unset_bypassing_nameref(name.as_str())?;
context.shell.env_mut().unset(name.direct())?;
}
// `unset -n` never touches functions or array elements — it
// operates only on nameref variables. Skip the rest of the loop.
Expand Down Expand Up @@ -120,7 +118,7 @@ fn unset_array_index(

// Check if the resolved target is an associative array (use lookup with the
// already-resolved name to avoid redundant nameref resolution).
let is_assoc_array = if let Some((_, var)) = shell.env().lookup(&resolved).get() {
let is_assoc_array = if let Some((_, var)) = shell.env().lookup(&resolved).get_direct() {
matches!(
var.value(),
ShellValue::AssociativeArray(_)
Expand All @@ -142,7 +140,7 @@ fn unset_array_index(
};

// Use lookup_mut with the already-resolved name to avoid redundant nameref resolution.
if let Some((_, var)) = shell.env_mut().lookup_mut(&resolved).get() {
if let Some((_, var)) = shell.env_mut().lookup_mut(&resolved).get_direct() {
var.unset_index(index_to_use.as_ref())
} else {
Ok(false)
Expand Down
Loading
Loading