Skip to content

Fix bash array expansion on macOS - #797

Merged
ViktorHofer merged 1 commit into
dotnet:mainfrom
omajid:build-bash-empty-array-macos-compat
May 26, 2025
Merged

ViktorHofer merged 1 commit into
dotnet:mainfrom
omajid:build-bash-empty-array-macos-compat

Conversation

@omajid

@omajid omajid commented May 26, 2025

Copy link
Copy Markdown
Member

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.

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.

@ViktorHofer ViktorHofer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, thanks for fixing. I don't fully understand the syntax. Would you mind explaining that?

@ViktorHofer
ViktorHofer requested a review from Copilot May 26, 2025 17:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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[@]}"}

Comment thread src/runtime/eng/build.sh Resolved
Comment thread src/roslyn/eng/build.sh Resolved
Comment thread src/fsharp/eng/build.sh Resolved
Comment thread src/arcade/eng/common/build.sh Resolved
@omajid

omajid commented May 26, 2025

Copy link
Copy Markdown
Member Author

Looks good, thanks for fixing. I don't fully understand the syntax. Would you mind explaining that?

(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 "{array[@]}" syntax: it tries to expand it to the individual elements of the array, then it errors out because there are no elements in the array, finally falling back to an even more legacy approach of treating array[@] as the actual variable name, and the variable with that name doesn't exist.

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.

  • "${array[@]}" (with double quotes) expands to all elements of the array, each individual element quoted correctly.
  • ${variable} or ${array[@]} (without double quoted) expands to the full value of the variable/array, without any quoting.
  • ${variable+alternate} expands to alternate if variable is set. If variable is unset or empty, it expands to nothing.

If you put it together, you get: ${array[@]+"${array[@]}"}. Which says: expand array, and see if it is non-empty, if is not-empty, use the alternate value ("${array[@]}") which is the elements of the array properly quoted.

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

@ViktorHofer

Copy link
Copy Markdown
Member

Thanks a lot for providing all that context. Today I learned something :)

@ViktorHofer
ViktorHofer merged commit b53264c into dotnet:main May 26, 2025
akoeplinger added a commit that referenced this pull request Jul 16, 2026
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 90a6dd11-87c9-47ed-8812-f4ed28ba0ee7
ViktorHofer pushed a commit to dotnet/msbuild that referenced this pull request Jul 28, 2026
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]>
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.

3 participants