Skip to content

fix(builtins): read builtin respects local variable scoping#901

Merged
chaliy merged 1 commit intomainfrom
fix/read-local-scoping
Mar 30, 2026
Merged

fix(builtins): read builtin respects local variable scoping#901
chaliy merged 1 commit intomainfrom
fix/read-local-scoping

Conversation

@chaliy
Copy link
Copy Markdown
Contributor

@chaliy chaliy commented Mar 30, 2026

Summary

  • The read builtin wrote directly to the global variables map (ctx.variables), bypassing function-local scoping. When local k was declared, read -r k wrote to global scope while the local shadowed it, resulting in empty values.
  • Fix: read now returns SetVariable side effects (like read -a already returns SetArray), applied by the interpreter through set_variable() which checks call_stack.locals first.
  • Includes regression test and updated unit tests.

Test plan

  • cargo test --all-features -p bashkit --lib — all 2123 tests pass
  • cargo clippy --all-targets --all-features -- -D warnings — clean
  • cargo fmt --check — clean
  • Regression test test_read_respects_local_scope covers the exact bug

The read builtin wrote directly to the global variables map
(ctx.variables), bypassing function-local scoping. When local k was
declared, read -r k wrote to global scope while local shadowed it.

Fix: read now returns SetVariable side effects applied through
set_variable() which checks call_stack.locals first.
@chaliy chaliy merged commit 9ba8460 into main Mar 30, 2026
27 checks passed
@chaliy chaliy deleted the fix/read-local-scoping branch March 30, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant