-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bazel_6, bazel_5, bazel_4: add cpp darwin bash set -e workaround #267670
Conversation
Submitting upstream here bazelbuild/bazel#20212 Looks like with some bash versions and under some conditions sub-shell would propagate errors to parent shell under `set -e` even though it shouldn't. https://hydra.nixos.org/build/240805256/nixlog/1 https://hydra.nixos.org/build/240805170/nixlog/2 Was able to reproduce the Hydra issue on `x86_64-darwin` with `bazel_6`, but not with `aarch64-darwin` having same numeric bash version. ZHF: NixOS#265948
Both did compile locally. bazel-watcher tests also fail for unrelated reasons and it may make sense to remove bazel-watcher from passthru.tests like in https://github.com/NixOS/nixpkgs/pull/231839/files because it will still be using bazel_5, not the bazel_4 or bazel_6 and also bazel-watcher doesn't have the highest level of support |
Is this a difference between gnu readlink and bsd readlink? |
Also does it matter? |
I can't repro the failure locally ( |
Saw it on hydra, and by chance it was reproducible on my x86-64 machine (and fixed with the change) % sw_vers
ProductName: macOS
ProductVersion: 12.7.1
BuildVersion: 21G920
% bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.
% man readlink | tail
file(1), ls(1), lstat(2), readlink(2), stat(2), printf(3), strftime(3)
HISTORY
The stat utility appeared in NetBSD 1.6 and FreeBSD 4.10.
AUTHORS
The stat utility was written by Andrew Brown <atatat@NetBSD.org>. This man
page was written by Jan Schaumann <jschauma@NetBSD.org>.
macOS 12.7 June 22, 2017 macOS 12.7 It also started to appear on hydra without any obvious related changes to bazel build expressions, but may potentially be related to recent upgrades like
Though agree that it would be even better with more detailed investigation. It should still be reproducible for me, so can run extra tests if you have any suggestions for those |
The top-level script doesn't fail here when issue is triggered. |
Converting to draft after more debugging seemingly invalidating the hypothesis on why the patch helps bazelbuild/bazel#20212 (comment) |
Looks like #266847 might be the triggering change. |
Next clue is that bash 5.2 runs in non-posix mode but bash 3.2 runs in posix mode and also propagates errexit to subshells. That's the difference. But not yet clear why bash 3.2 decides to run in posix mode (it'd be the case if it is invoked via |
Does get invoked via
|
Looks like somehow on MacOS So it does seem like #266847 is the trigger. But maybe it is MacOS or bazel to blame on breaking/relying that shebang is respected or maybe that Looks like this behavior doesn't happen with NixOS /bin/sh&env, but didn't test on other distros yet. |
MacOS behavior for following shells % /bin/zsh --version
zsh 5.8.1 (x86_64-apple-darwin21.0)
% /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.
% /bin/sh --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.
% cat test.sh
#!/missing
echo "I run"
% for x in sh bash zsh; do /bin/${x} -c './test.sh'; done
/bin/sh: ./test.sh: /missing: bad interpreter: No such file or directory
/bin/bash: ./test.sh: /missing: bad interpreter: No such file or directory
zsh:1: ./test.sh: bad interpreter: /missing: no such file or directory
% touch /tmp/unexec
% cat test.sh
#!/tmp/unexec
echo "I run"
% for x in sh bash zsh; do /bin/${x} -c './test.sh'; done
/bin/sh: ./test.sh: /tmp/unexec: bad interpreter: Permission denied
/bin/bash: ./test.sh: /tmp/unexec: bad interpreter: Permission denied
zsh:1: permission denied: ./test.sh
% cat test.sh
#!/bin/echo echo
echo "I run"
% for x in sh bash zsh; do /bin/${x} -c './test.sh'; done
echo ./test.sh
echo ./test.sh
echo ./test.sh
% touch /tmp/somefile
% chmod +x /tmp/somefile
% for x in sh bash zsh; do /bin/${x} -c './test.sh'; done
I run
I run
zsh:1: exec format error: ./test.sh
% cat test.sh
#!/tmp/somefile
/bin/echo "set:"
set | /usr/bin/grep "SH[^_]*="
/bin/echo "env:"
/usr/bin/env | /usr/bin/grep "SH[^_]*="
echo "I run"
% for x in sh bash zsh; do env - /bin/${x} -c 'env A=B ./test.sh'; done
set:
BASH=/bin/sh
SHELL=/bin/zsh
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=2
env:
SHLVL=2
I run
set:
BASH=/bin/sh
SHELL=/bin/zsh
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=2
env:
SHLVL=2
I run
set:
BASH=/bin/sh
SHELL=/bin/zsh
SHELLOPTS=braceexpand:hashall:interactive-comments:posix
SHLVL=1
env:
SHLVL=1
I run |
bazelbuild/bazel#20212 (comment)
Whoops, I even was aware of the fact, we have some binaries in nixpkgs for that express reason, but I forgot about it when reviewing #266847. |
See #23018. |
Yep, already preparing a PR with fix via makeBinaryWrapper |
https://hydra.nixos.org/build/240805256/nixlog/1 https://hydra.nixos.org/build/240805170/nixlog/2 Failure is a bit obscured but long story short, a script within bazel gets custom nixpkgs shebang which in turn makes shell run in POSIX-compatible mode. Bazel expects bash in non-POSIX mode and osx-specific script starts to fail due to `set -e` and subshell interaction differences in those modes (sub-shells and functions suddently start inheriting `set -e` and fail to produce desired output). More debug info is available in NixOS#267670 Shell scripts aren't guaranteed to work as interpreters in shebang. In particular thin shell wrappers aren't shebang-ready on MacOS. It may work sometimes depending on what exactly would try to execute a script with such shebang, but generally it's not guaranteed to work. See NixOS#124556 Bash wrapper was introduced in NixOS#266847 and so far seems like the issue only affects darwin builds: hydra failure is in osx-specific script, also shebang issue is usually darwin-specific. Let's wrap it as a native binary to make it shebang-compatible. The wrapper is only currently added to `bazel_6` so no need for changes in other versions. ZHF: NixOS#265948
https://hydra.nixos.org/build/240805256/nixlog/1 https://hydra.nixos.org/build/240805170/nixlog/2 Failure is a bit obscured but long story short, a script within bazel gets custom nixpkgs shebang which in turn makes shell run in POSIX-compatible mode. Bazel expects bash in non-POSIX mode and osx-specific script starts to fail due to `set -e` and subshell interaction differences in those modes (sub-shells and functions suddently start inheriting `set -e` and fail to produce desired output). More debug info is available in #267670 Shell scripts aren't guaranteed to work as interpreters in shebang. In particular thin shell wrappers aren't shebang-ready on MacOS. It may work sometimes depending on what exactly would try to execute a script with such shebang, but generally it's not guaranteed to work. See #124556 Bash wrapper was introduced in #266847 and so far seems like the issue only affects darwin builds: hydra failure is in osx-specific script, also shebang issue is usually darwin-specific. Let's wrap it as a native binary to make it shebang-compatible. The wrapper is only currently added to `bazel_6` so no need for changes in other versions. ZHF: #265948 (cherry picked from commit 7377bba)
https://hydra.nixos.org/build/240805256/nixlog/1 https://hydra.nixos.org/build/240805170/nixlog/2 Failure is a bit obscured but long story short, a script within bazel gets custom nixpkgs shebang which in turn makes shell run in POSIX-compatible mode. Bazel expects bash in non-POSIX mode and osx-specific script starts to fail due to `set -e` and subshell interaction differences in those modes (sub-shells and functions suddently start inheriting `set -e` and fail to produce desired output). More debug info is available in #267670 Shell scripts aren't guaranteed to work as interpreters in shebang. In particular thin shell wrappers aren't shebang-ready on MacOS. It may work sometimes depending on what exactly would try to execute a script with such shebang, but generally it's not guaranteed to work. See #124556 Bash wrapper was introduced in #266847 and so far seems like the issue only affects darwin builds: hydra failure is in osx-specific script, also shebang issue is usually darwin-specific. Let's wrap it as a native binary to make it shebang-compatible. The wrapper is only currently added to `bazel_6` so no need for changes in other versions. ZHF: #265948 (cherry picked from commit 7377bba)
Description of changes
Submitting upstream here
bazelbuild/bazel#20212
Looks like with some bash versions and under some conditions sub-shell would propagate errors to parent shell under
set -e
even though it shouldn't.https://hydra.nixos.org/build/240805256/nixlog/1
https://hydra.nixos.org/build/240805170/nixlog/2
Was able to reproduce the Hydra issue on
x86_64-darwin
withbazel_6
, but not withaarch64-darwin
having same numeric bash version.ZHF: #265948
Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)