mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.16k stars 338 forks source link

interp: fix variables scope in functions #759

Closed cclerget closed 2 years ago

cclerget commented 2 years ago

Variables declared as exported or readonly in functions without assignment are not correctly propagated to the global scope, this snippet should return a bar: readonly variable error:

_foo() {
    bar=foo
    readonly bar
}

_foo
bar=bar

This snippet should also export bar variable correctly:

_foo() {
    export bar
}

_foo
bar=foo
env | grep ^bar=
mvdan commented 2 years ago

FYI, it is likely I'll cherry-pick some of the important fixes into a v3.4.1 in a week or so. I assume that pulling master is fine in the meantime; there haven't been any large or breaking changes since then.

mvdan commented 2 years ago

I've started the cherry-picking with what we've got so far at https://github.com/mvdan/sh/tree/v3.4, if you want to use the HEAD of that branch rather than master, which is now the start of v3.5.

cclerget commented 2 years ago

I've started the cherry-picking with what we've got so far at https://github.com/mvdan/sh/tree/v3.4, if you want to use the HEAD of that branch rather than master, which is now the start of v3.5.

Great thanks :+1: :)