Support sh shell or throw an error when using it from runfiles.bash snippet
#21.436 aperta il 20 feb 2024
Metriche repository
- Star
- (25.384 star)
- Metriche merge PR
- (Merge medio 22g 20h) (77 PR mergiate in 30 g)
Descrizione
Description of the feature request:
The snippet in tools/bash/runfiles/runfiles.bash uses subprocess expressions which are supported in bash but not sh shells. This can lead to the snippet failing silently without a helpful error message if users attempt to use it in non-bash shells. Earlier versions of this snippet worked in sh shells (but were more verbose). We should either throw a descriptive error if the shell is not bash, or update the snippet to a V4 which supports non-bash shells.
Which category does this issue belong to?
No response
What underlying problem are you trying to solve with this feature?
I spent a while debugging an issue with the runfiles.bash snippet trying to resolve a runfile for an sh_binary.
We finally found the root cause which was that my shell script was using the sh shell via its sh-bang:
#!/bin/sh
Not:
#!/bin/bash
And this was silently failing because the sh shell does not support subprocess expressions.
I thought sh was just an alias of bash and didn't even realize they were separate shells with different capabilities, especially given that the rule is called sh_binary, not bash_binary.
The expression was just failing silently which was very confusing for myself and another engineer who ran into this issue. Also earlier versions of this snippet did not use subprocess expressions so they were compatible with other shells, so there are examples on Stack Overflow and other sites with the sh sh-bang (or they clip this part out so users may not know which shell they were using).
IMHO at the very least if we require this capability, the snippet should throw an error if the shell is not bash. Alternately, a V4 of the snippet could not use these expressions for wider shell compatibility.
Which operating system are you running Bazel on?
macOS Sonoma 14.3.1
What is the output of bazel info release?
release 7.0.2
If bazel info release returns development version or (@non-git), tell us how you built Bazel.
No response
What's the output of git remote get-url origin; git rev-parse HEAD ?
No response
Have you found anything relevant by searching the web?
No response
Any other information, logs, or outputs that you want to share?
No response