fix: Avoid C++ toolchain requirement if possible by fmeum · Pull Request #2919 · bazel-contrib/rules_python

@fmeum

By making use of the new launcher_maker_toolchain in Bazel 9, rules_python can avoid the requirement for a C++ toolchain targeting the target platform if that platform isn't Windows.

For example, this makes it possible to cross-compile pure Python targets from one Unix to another. Since Java targets have a dependency on Python targets through the proguard_allowlister, this also allows Java targets to be built without any C++ toolchain.

@rickeylev

fwiw/fyi: we have a CI job that uses bazel-rolling, in case that's helpful.

Also, a fun trick is using FeatureFlagInfo to detect if a toolchain is present. Something like:

def impl(ctx):
  return FeatureFlagInfo(value=str(ctx.toolchains["foo"] == None))
has_tc = rule(
  implementation = impl,
  toolchains = [toolchain_type("foo", mandatory=False)]

has_tc(name="has_tc")
config_setting("is_has_tc_true", flag_values = {":has_tc": "True"})

alias("launcher_maker", actual = select({
  "is_has_tc_true": ...,
  })
)

@fmeum

Adding a dep on bazel_features isn't easy with rules_python's WORKSPACE setup since it doesn't use two levels of macros. Can we add a call to bazel_features_deps to the relnotes?

@fmeum fmeum changed the title Use the launcher_maker toolchain if available fix: Avoid C++ toolchain requirement if possible

Nov 20, 2025

@fmeum

@fmeum

@rickeylev @aignas Friendly ping on #2919 (comment). I rebased this PR and updated the PR description to explain the motivation - this is needed to get rid of a transitive dependency on C++ of all Java rules.

@aignas

If we wanted to avoid the bazel_features introduction we can use the rp_config repo and just check if bazel is version 9 or above.

That said, maybe most of our users on WORKSPACE should either migrate to bzlmod or add bazel_features by this time because it is not sustainable to keep backwards compatibility for too long here.

I'd be +1 for asking them to add bazel_features or to introduce a secondary deps macro. Right now we have py_repository_deps and we could add py_repository_deps_setup. What do you think?

@fmeum

fmeum

@rickeylev

Yeah, lets just add a second deps macro. Workspace makes this painful no matter how we cut it. Might as well provide a utility function everyone can use

@fmeum

Is a deps macro better than asking users to instantiate bazel_features directly? In WORKSPACE days, I would have actually preferred the latter since the module is released frequently and a dep macro bringing in an outdated version is a net negative.

@fmeum

@fmeum

@fmeum fmeum marked this pull request as ready for review

November 21, 2025 19:54

@fmeum

I went with rp_config for now to get CI green without further considerations. Happy to migrate this back to bazel_features if you prefer that.

rickeylev