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
6.97k stars 332 forks source link

expand: FuncEnviron provided environment variables will not copy cross pipe #1043

Closed zhaodiaoer closed 7 months ago

zhaodiaoer commented 7 months ago

When using expand.FuncEnviron to generate runner and using this runner to run the file type Node that contains any pipe operator symbol, it will cause environment variables to not transfer across pipes and be visible for all command statements.

That's because expand.FuncEnviron does implement the Environ interface, but the Each is no-op method, so when executing runner.Subshell() to get a new runner for one side command of pipe, environment variables will not be copied as expected.

It would be helpful to add comments to the expand.FuncEnviron function. This will prevent others from troubleshooting similar problems for a long time, like I did.

mvdan commented 7 months ago

Thanks for spotting this - I agree it's a significant pitfall. I'd prefer to fix this over documenting the limitation, as it seems rather severe.

mvdan commented 7 months ago

Just pushed a fix after some tweaking - let me know if that works.