Is your feature request related to a problem? Please describe.
The POSIX toolchain provided by rules_sh tracks and exposes the tools it bundles as strings, paths to binaries installed outside of Bazel. The common use-cases are either the Nix use-case, where these paths will point to a Nix store path, or the default use-case, where the toolchain setup looks the tools up on PATH and then stores the resolved absolute paths.
So long as we use specific tools by absolute paths this is not an issue, e.g. so long as we explicitly invoke $(POSIX_GREP). However, in some use-cases we need to provide the tools bundled by the toolchain in a PATH variable to another binary or script. In this case the PATH may cover more tools than are really meant to be captured by the toolchain. For example, if the default POSIX toolchain found the tools in /usr/local/bin, then PATH will also expose other tools installed under /usr/local/bin, e.g. /usr/local/bin/clang or /usr/local/bin/my_custom_binary_that_should_not_be_known_to_bazel. This breaks hermeticity.
Describe the solution you'd like
This can be solved by symlinking the discovered tools into the Bazel sandbox and only exposing them to build actions through these symlinks. I.e. build actions would no longer invoke /usr/local/bin/grep, but instead something like external/posix/grep, note the relative vs. absolute path. These symlinks would also need to be declared as explicit inputs into the build action.
Note, on Windows symlinks may not be available. In that case we can instead use shims. For example by using Scoop's Shim.
Notably this enables further use-cases that are currently not supported. For example, with this approach one could fetch a busybox release, or some other bundle of shell binaries, and generate a POSIX toolchain based on it.
This approach is already implemented here and would only need to be upstreamed into this repository. A Windows variant is implemented here, that one is specific to that particular use-case, but the concept can be generalized.
Additional context
The approach implemented here is more general. It is not restricted to tools for a POSIX toolchain, but applies to any bundle of binaries. Similarly, the same module contains an implementation for a cc_library bundle.
Tasks
Is your feature request related to a problem? Please describe.
The POSIX toolchain provided by rules_sh tracks and exposes the tools it bundles as strings, paths to binaries installed outside of Bazel. The common use-cases are either the Nix use-case, where these paths will point to a Nix store path, or the default use-case, where the toolchain setup looks the tools up on
PATHand then stores the resolved absolute paths.So long as we use specific tools by absolute paths this is not an issue, e.g. so long as we explicitly invoke
$(POSIX_GREP). However, in some use-cases we need to provide the tools bundled by the toolchain in aPATHvariable to another binary or script. In this case thePATHmay cover more tools than are really meant to be captured by the toolchain. For example, if the default POSIX toolchain found the tools in/usr/local/bin, thenPATHwill also expose other tools installed under/usr/local/bin, e.g./usr/local/bin/clangor/usr/local/bin/my_custom_binary_that_should_not_be_known_to_bazel. This breaks hermeticity.Describe the solution you'd like
This can be solved by symlinking the discovered tools into the Bazel sandbox and only exposing them to build actions through these symlinks. I.e. build actions would no longer invoke
/usr/local/bin/grep, but instead something likeexternal/posix/grep, note the relative vs. absolute path. These symlinks would also need to be declared as explicit inputs into the build action.Note, on Windows symlinks may not be available. In that case we can instead use shims. For example by using Scoop's Shim.
Notably this enables further use-cases that are currently not supported. For example, with this approach one could fetch a busybox release, or some other bundle of shell binaries, and generate a POSIX toolchain based on it.
This approach is already implemented here and would only need to be upstreamed into this repository. A Windows variant is implemented here, that one is specific to that particular use-case, but the concept can be generalized.
Additional context
The approach implemented here is more general. It is not restricted to tools for a POSIX toolchain, but applies to any bundle of binaries. Similarly, the same module contains an implementation for a cc_library bundle.
Tasks
Addressed in Add sh_binaries rule #23 by adding the
sh_binariesrule.Addressed in Implement create_shim to create a shim on Windows #26 by adding the
create_shimutility.PATH(e.g. usingrepository_ctx.which) and import them into an external Bazel workspace (e.g. usingrepository_ctx.symlinkorcreate_shimas appropriate). (Beware of false friends)Fileobjects.sh_binaries.PATHusing symlinks or Shims as appropriate.sh_binaries. Complete the new POSIX toolchain based onsh_binaries#46