Skip to content

Use launcher wrapper to initialize runfiles#38

Merged
fmeum merged 2 commits intobazel-contrib:mainfrom
mering:runfiles-launcher
Aug 20, 2025
Merged

Use launcher wrapper to initialize runfiles#38
fmeum merged 2 commits intobazel-contrib:mainfrom
mering:runfiles-launcher

Conversation

@mering
Copy link
Copy Markdown
Contributor

@mering mering commented Aug 12, 2025

This avoid the need to copy&paste the verbose initialization snippet into every script.

This should resolve the issues raised in #24.

Copy link
Copy Markdown
Member

@fmeum fmeum left a comment

Choose a reason for hiding this comment

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

I really like the ergonomics of this approach. It also cleanly avoids the issues #24 had.

Since sh_binary is such a foundational rule and has always had a simple shell script with no "deps" as its executable, I'm worried that we could be breaking users by introducing a launcher of sorts. In particular since it probably requires bash.

What do you think of gating the new behavior behind a boolean attribute for now? We could also consider making this an attribute of sh_toolchain instead, which would only require opt-in in a single location.

Could you also add an example to the BCR test module to exercise this new functionality?

Comment thread shell/private/sh_executable.bzl Outdated
@fmeum fmeum requested a review from meteorcloudy August 13, 2025 05:36
@mering
Copy link
Copy Markdown
Contributor Author

mering commented Aug 13, 2025

Since sh_binary is such a foundational rule and has always had a simple shell script with no "deps" as its executable, I'm worried that we could be breaking users by introducing a launcher of sorts. In particular since it probably requires bash.

IIUC the runfiles library itself requires bash, so if we want to provide runfiles functionality, then I think there is no way around it. Instead of hardcoding /bin/bash, can I get the absolute path for the interpreter from the toolchain somehow?

What do you think of gating the new behavior behind a boolean attribute for now? We could also consider making this an attribute of sh_toolchain instead, which would only require opt-in in a single location.

Sure, sounds like a good approach to have a knob for people in case there are incompatibilities. Do you have a suggestion on how to name this attribute?

Could you also add an example to the BCR test module to exercise this new functionality?

I tried it with the existing examples and it works like a charm for Bazel 8 but IIUC with Bazel 6 and 7 it uses the native rules instead of the ones in this repo. Is there a way to disable a test for Bazel 6 and 7?

@fmeum
Copy link
Copy Markdown
Member

fmeum commented Aug 13, 2025

IIUC the runfiles library itself requires bash, so if we want to provide runfiles functionality, then I think there is no way around it. Instead of hardcoding /bin/bash, can I get the absolute path for the interpreter from the toolchain somehow?

Yes, but you always had the option to not use bash in your sh_binary, even if that meant you couldn't use the runfiles library.

You can get the path from https://github.com/bazelbuild/rules_shell/blob/e6d845428779b1e5ebb9971451ed3c43c865fc0b/shell/toolchains/sh_toolchain.bzl#L31. The Windows launcher part already uses this.

Sure, sounds like a good approach to have a knob for people in case there are incompatibilities. Do you have a suggestion on how to name this attribute?

How about use_bash_launcher? It should probably contain bash since that's what we force onto the user. It should also be more generic than, say, inejct_rlocation since I could see this being an avenue towards a more hermetic interpreter/tool setup.

I tried it with the existing examples and it works like a charm for Bazel 8 but IIUC with Bazel 6 and 7 it uses the native rules instead of the ones in this repo. Is there a way to disable a test for Bazel 6 and 7?

You could export a constant from a .bzl file that checks for hasattr(native, "sh_binary") and tag the test as manual depending on that constant. Feature detection is better than version detection.

@mering
Copy link
Copy Markdown
Contributor Author

mering commented Aug 14, 2025

@fmeum I addressed all review comments.

Comment thread shell/runfiles/runfiles.bash
Comment thread shell/private/sh_executable.bzl Outdated
Comment thread shell/private/sh_executable.bzl Outdated
Comment thread tests/bcr/bash_launcher/targets.bzl Outdated
Comment thread tests/bcr/bash_launcher/targets.bzl Outdated
Comment thread tests/bcr/bash_launcher/test.sh Outdated
Comment thread shell/private/sh_executable.bzl Outdated
Comment thread shell/private/sh_executable.bzl Outdated
Comment thread shell/private/sh_executable.bzl Outdated
Comment thread shell/private/sh_executable.bzl Outdated
mering and others added 2 commits August 19, 2025 09:35
This avoid the need to copy&paste the initialization snippet into every script.
@fmeum fmeum merged commit 94c1e66 into bazel-contrib:main Aug 20, 2025
2 checks passed
@mering mering deleted the runfiles-launcher branch August 21, 2025 07:13
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