Repository navigation
Fix bash array expansion on macOS - #797
Conversation
Fix broken builds on macOS. macOS ships with an older version of bash, which doesn't expand empty arrays correctly. So check that arrays are not empty before expanding them.
0ac4503 to
3267631
Compare
ViktorHofer
left a comment
There was a problem hiding this comment.
Looks good, thanks for fixing. I don't fully understand the syntax. Would you mind explaining that?
There was a problem hiding this comment.
Pull Request Overview
This PR fixes broken builds on macOS by introducing conditional array expansions that prevent empty arrays from being expanded incorrectly on older versions of bash.
- Update bash array expansion patterns across multiple build scripts.
- Ensure compatibility with older macOS bash by conditionally expanding arrays.
- Adjust similar patterns in files for consistent behavior.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/runtime/eng/build.sh | Applies conditional array expansion for extra arguments and bootstrap arguments. |
| src/roslyn/eng/build.sh | Updates properties array expansion to use a conditional syntax. |
| src/nuget-client/eng/dotnet-build/build.sh | Modifies dotnet msbuild argument expansion using a conditional array expansion pattern. |
| src/fsharp/eng/build.sh | Uses conditional array expansion for the properties array in build commands. |
| src/arcade/eng/common/build.sh | Implements conditional array expansion to address potential empty array issues. |
Comments suppressed due to low confidence (1)
src/nuget-client/eng/dotnet-build/build.sh:141
- Add an inline comment noting that the conditional array expansion is necessary for correct behavior on older macOS bash versions.
"$DOTNET" msbuild ... ${dotnetArguments[@]+"${dotnetArguments[@]}"} ${args[@]+"${args[@]}"}
(Did you mean documenting it in the code itself, like Copilot's feedback?) The version of bash on macOS is so old that it follows a very strict interpretation of There are many solutions possible. They all boil down to "only expand the elements of the array if the array is not empty" to not break different versions of bash. https://stackoverflow.com/questions/7577052/unbound-variable-error-in-bash-when-expanding-empty-array/7577209 has a set of them. I picked the one that seems to be the most broadly compatible with most/all versions of bash based on the research there. The fix uses a few pieces of syntax together.
If you put it together, you get: There are other possible ways of expressing the same thing using different bash syntax, but as the StackOverflow post says, the majority of other ways run into issues with some version of bash. For more details on the array and variable substitution syntax see https://web.archive.org/web/20230403055152/https://wiki.bash-hackers.org/syntax/arrays and https://web.archive.org/web/20230404084543/https://wiki.bash-hackers.org/syntax/pe#use_an_alternate_value |
|
Thanks a lot for providing all that context. Today I learned something :) |
Co-authored-by: Copilot <[email protected]> Copilot-Session: 90a6dd11-87c9-47ed-8812-f4ed28ba0ee7
I use a particular set of flags to build .NET VMR, which flows something
like this to the msbuild build scripts:
$ ./build.sh /p:OfficialBuilder="Red Hat Inc."
This regressed recently (probably
#14361). This change fixes the
issue.
For arrays which can be empty, use the ${var[@]+"${var[@]}"} syntax, as
discussed in dotnet/dotnet#797, to avoid issues
on macOS.
Assisted-By: Claude Opus 4.6 <[email protected]>
---------
Co-authored-by: Claude Opus 4.6 <[email protected]>
Fix broken builds on macOS. macOS ships with an older version of bash, which doesn't expand empty arrays correctly. So check that arrays are not empty before expanding them.