bazelbuild / bazelbuild/bazel

Support sh shell or throw an error when using it from runfiles.bash snippet

Open
#21,436 2 comments 0 reactions 0 assignees View on GitHub
help wanted P3 team-ExternalDeps type: feature request
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### 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.

[Slack Thread](https://bazelbuild.slack.com/archives/C014RARENH0/p1708450308417619)

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_

Contributor guide

Open the contributing guide

Research direction

Start with tools/bash/runfiles/runfiles.bash and inspect the subprocess expressions that require Bash. Determine whether the change should provide a descriptive error for non-Bash shells or produce a V4 snippet compatible with sh. Done means the selected behavior is implemented and the silent failure is replaced by clear behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
bash, shell
Domain
build-system
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.