Skip to content

Commit

Permalink
fix(py_runtime): make py_runtime work with Bazel 6
Browse files Browse the repository at this point in the history
With Bazel 6, the `py_binary` rule performs a type check of the
PyRuntimeInfo value it gets from the toolchain to verify it is an
instance of the Java-implemented PyRuntimeInfo class. This type check
fails when the provider is implemented in rules_python in Starlark.

To fix, make the `py_runtime_info` prefer the builtin PyRuntimeInfo
provider when running under Bazel 6. The two providers are (currently)
the same, so are mostly interchangable. This satisfies the type check
that `py_binary` performs.

Fixes #1610
  • Loading branch information
rickeylev committed Dec 14, 2023
1 parent 27450f9 commit ff9121e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion python/private/py_runtime_pair_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo")
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER")

def _py_runtime_pair_impl(ctx):
if ctx.attr.py2_runtime != None:
Expand Down Expand Up @@ -45,7 +46,11 @@ def _py_runtime_pair_impl(ctx):
)]

def _get_py_runtime_info(target):
if PyRuntimeInfo in target:
# Prior to Bazel 7, the builtin PyRuntimeInfo object must be used because
# py_binary (implemented in Java) performs a type check on the provider
# value to verify it is an instance of the Java-implemented PyRuntimeInfo
# class.
if IS_BAZEL_7_OR_HIGHER and PyRuntimeInfo in target:
return target[PyRuntimeInfo]
else:
return target[BuiltinPyRuntimeInfo]
Expand Down
40 changes: 40 additions & 0 deletions tests/py_runtime_pair/py_runtime_pair_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("@rules_testing//lib:truth.bzl", "matching", "subjects")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:py_binary.bzl", "py_binary")
load("//python:py_runtime.bzl", "py_runtime")
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") # buildifier: disable=bzl-visibility
Expand Down Expand Up @@ -99,6 +100,45 @@ def _test_builtin_py_info_accepted_impl(env, target):

_tests.append(_test_builtin_py_info_accepted)

def _test_py_runtime_pair_and_binary(name):
rt_util.helper_target(
py_runtime,
name = name + "_runtime",
interpreter = "fake_interpreter",
python_version = "PY3",
files = ["file1.txt"],
)
rt_util.helper_target(
py_runtime_pair,
name = name + "_pair",
py3_runtime = name + "_runtime",
)
native.toolchain(
name = name + "_toolchain",
toolchain = name + "_pair",
toolchain_type = "//python:toolchain_type",
)
rt_util.helper_target(
py_binary,
name = name + "_subject",
srcs = [name + "_subject.py"],
)
analysis_test(
name = name,
target = name + "_subject",
impl = _test_py_runtime_pair_and_binary_impl,
config_settings = {
"//command_line_option:extra_toolchains": [
"//tests/py_runtime_pair:test_py_runtime_pair_and_binary_toolchain",
],
},
)

def _test_py_runtime_pair_and_binary_impl(env, target):
pass

_tests.append(_test_py_runtime_pair_and_binary)

def py_runtime_pair_test_suite(name):
test_suite(
name = name,
Expand Down

0 comments on commit ff9121e

Please sign in to comment.