diff --git a/python/repositories.bzl b/python/repositories.bzl index 687388c62e..5abc8f6f6d 100644 --- a/python/repositories.bzl +++ b/python/repositories.bzl @@ -35,6 +35,13 @@ def py_repositories(): # Remaining content of the file is only used to support toolchains. ######## +# Parse the bazel version string from `native.bazel_version`. +# e.g. +# "0.10.0rc1 abc123d" => (0, 10, 0) +# "0.3.0" => (0, 3, 0) +def _parse_native_bazel_version(bazel_version): + return tuple([int(n) for n in bazel_version.split(".")]) + def _python_repository_impl(rctx): if rctx.attr.distutils and rctx.attr.distutils_content: fail("Only one of (distutils, distutils_content) should be set.") @@ -104,6 +111,12 @@ def _python_repository_impl(rctx): fail(exec_result.stderr) python_bin = "python.exe" if ("windows" in platform) else "bin/python3" + print(native.bazel_version) + if _parse_native_bazel_version(native.bazel_version) >= (5,0,0): + abs_python_bin_path = rctx.path(python_bin) + stub_shebang_assignment = "stub_shebang = \"#!{}\"".format(abs_python_bin_path) + else: + stub_shebang_assignment = "" build_content = """\ # Generated by python/repositories.bzl @@ -155,6 +168,7 @@ py_runtime( files = [":files"], interpreter = "{python_path}", python_version = "PY3", + {stub_shebang_assignment} ) py_runtime_pair( @@ -165,6 +179,7 @@ py_runtime_pair( """.format( python_path = python_bin, python_version = python_short_version, + stub_shebang_assignment = stub_shebang_assignment, ) rctx.file("BUILD.bazel", build_content)