Skip to content
Merged
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
15 changes: 9 additions & 6 deletions crates/bashkit/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7714,15 +7714,18 @@ impl Interpreter {
s.to_string()
}

/// Expand an associative array key with full word expansion.
/// Unlike `expand_variable_or_literal`, this handles command substitutions
/// (`$(...)`, backticks) and all other expansion types. (Issue #872)
/// Fully expand an associative array key, including command substitutions.
/// Falls back to `expand_variable_or_literal` for keys without `$(` or backtick.
async fn expand_assoc_key(&mut self, s: &str) -> Result<String> {
if s.contains('$') || s.contains('`') {
let word = crate::parser::Parser::parse_word_string(s);
if s.contains("$(") || s.contains('`') {
Comment on lines +7718 to +7720
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In expand_assoc_key, the s.contains('')check looks redundant/dead for assignment subscripts because the lexer rewrites backtick substitutions to$(` during tokenization. Consider dropping the backtick condition (and adjusting the docstring accordingly) to avoid suggesting that the word parser here understands raw backticks.

Suggested change
/// Falls back to `expand_variable_or_literal` for keys without `$(` or backtick.
async fn expand_assoc_key(&mut self, s: &str) -> Result<String> {
if s.contains("$(") || s.contains('`') {
/// Falls back to `expand_variable_or_literal` for keys without `$(`.
async fn expand_assoc_key(&mut self, s: &str) -> Result<String> {
if s.contains("$(") {

Copilot uses AI. Check for mistakes.
let word = Parser::parse_word_string_with_limits(
s,
self.limits.max_ast_depth,
self.limits.max_parser_operations,
);
self.expand_word(&word).await
} else {
Ok(s.to_string())
Ok(self.expand_variable_or_literal(s))
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/bashkit/tests/spec_cases/bash/assoc-arrays.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ result: [a
b]
### end

### assoc_key_command_substitution
declare -A m=()
m["$(echo hello)"]="world"
echo "count: ${#m[@]}"
for k in "${!m[@]}"; do echo "key=[$k] val=[${m[$k]}]"; done
### expect
count: 1
key=[hello] val=[world]
### end

### assoc_iteration
declare -A m
m[a]="1"
Expand Down
Loading