refactor: make whl_library loadable before py_repositories in WORKSPACE (#4458) In WORKSPACE, `python/repositories.bzl` is loaded before `py_repositories()` defines `@rules_python_internal` and `@bazel_skylib`, so nothing loaded from it can depend on those repositories. The WORKSPACE toolchains in #4457 need to use `whl_library` for the coverage wheels, which means the `whl_library` load chain must only load `@bazel_tools`. The `whl_extract.bzl` and `patch_whl.bzl` files loaded `@rules_python_internal` only to read `supports_whl_extraction` and `extract_needs_chmod`, which depend on nothing but the Bazel version. They now compute these from `native.bazel_version` themselves, and the values are no longer written to the `@rules_python_internal` config. The `pypi_repo_utils.bzl` and `util.bzl` files loaded `@bazel_skylib` only for `types.is_string` and `types.is_tuple`, so those are replaced with plain `type()` checks. Work towards #3902
diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index b111df6..f3ebdfb 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel
@@ -913,10 +913,7 @@ bzl_library( name = "util", srcs = ["util.bzl"], - deps = [ - ":py_internal", - "@bazel_skylib//lib:types", - ], + deps = [":py_internal"], ) bzl_library(
diff --git a/python/private/internal_config_repo.bzl b/python/private/internal_config_repo.bzl index fef5caa..51a09e1 100644 --- a/python/private/internal_config_repo.bzl +++ b/python/private/internal_config_repo.bzl
@@ -27,10 +27,8 @@ _CONFIG_TEMPLATE = """ config = struct( build_python_zip_default = {build_python_zip_default}, - supports_whl_extraction = {supports_whl_extraction}, enable_pystar = True, enable_deprecation_warnings = {enable_deprecation_warnings}, - extract_needs_chmod = {extract_needs_chmod}, bazel_8_or_later = {bazel_8_or_later}, bazel_9_or_later = {bazel_9_or_later}, bazel_10_or_later = {bazel_10_or_later}, @@ -82,20 +80,11 @@ # An empty version signifies a development build, which is treated as # the latest version. if native.bazel_version: - version_parts = native.bazel_version.split(".") - bazel_major_version = int(version_parts[0]) - bazel_minor_version = int(version_parts[1]) + bazel_major_version = int(native.bazel_version.split(".")[0]) else: bazel_major_version = 99999 - bazel_minor_version = 99999 - supports_whl_extraction = False - extract_needs_chmod = bazel_major_version < 8 or (bazel_major_version == 8 and bazel_minor_version < 6) if bazel_major_version >= 8: - # Extracting .whl files requires Bazel 8.3.0 or later. - if bazel_major_version > 8 or bazel_minor_version >= 3: - supports_whl_extraction = True - builtin_py_info_symbol = "None" builtin_py_runtime_info_symbol = "None" builtin_py_cc_link_params_provider = "None" @@ -110,8 +99,6 @@ enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT), builtin_py_info_symbol = builtin_py_info_symbol, builtin_py_runtime_info_symbol = builtin_py_runtime_info_symbol, - supports_whl_extraction = str(supports_whl_extraction), - extract_needs_chmod = str(extract_needs_chmod), builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider, modules_using_explicit_initpy = str(explicit_init_py_modules), tool_deps_dev_only = str(rctx.attr.tool_deps_dev_only),
diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel index f94d245..03a9e11 100644 --- a/python/private/pypi/BUILD.bazel +++ b/python/private/pypi/BUILD.bazel
@@ -266,7 +266,6 @@ deps = [ ":parse_whl_name", "//python/private:repo_utils", - "@rules_python_internal//:rules_python_config", ], ) @@ -371,7 +370,6 @@ deps = [ "//python/private:repo_utils", "//python/private:util", - "@bazel_skylib//lib:types", ], ) @@ -476,7 +474,6 @@ deps = [ ":whl_metadata", "//python/private:repo_utils", - "@rules_python_internal//:rules_python_config", ], )
diff --git a/python/private/pypi/patch_whl.bzl b/python/private/pypi/patch_whl.bzl index b257f2b..9007a31 100644 --- a/python/private/pypi/patch_whl.bzl +++ b/python/private/pypi/patch_whl.bzl
@@ -19,7 +19,6 @@ the RECORD file natively in Starlark and repack using system zip. """ -load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") load("//python/private:repo_utils.bzl", "repo_utils") load(":parse_whl_name.bzl", "parse_whl_name") @@ -93,11 +92,19 @@ """ whl_input = rctx.path(whl_path) + # An empty version is a development build, which is treated as the latest. + bazel_version = (99999, 99999) + if native.bazel_version: + major, minor = native.bazel_version.split(".")[:2] + bazel_version = (int(major), int(minor)) + repo_utils.extract( rctx, archive = whl_input, - supports_whl_extraction = rp_config.supports_whl_extraction, - extract_needs_chmod = rp_config.extract_needs_chmod, + # Extracting `.whl` files requires Bazel 8.3.0 and the extracted files + # need their permissions fixed before Bazel 8.6.0. + supports_whl_extraction = bazel_version >= (8, 3), + extract_needs_chmod = bazel_version < (8, 6), ) if not patches:
diff --git a/python/private/pypi/pypi_repo_utils.bzl b/python/private/pypi/pypi_repo_utils.bzl index 8ec7bd1..d8ad340 100644 --- a/python/private/pypi/pypi_repo_utils.bzl +++ b/python/private/pypi/pypi_repo_utils.bzl
@@ -14,7 +14,6 @@ "" -load("@bazel_skylib//lib:types.bzl", "types") load("//python/private:repo_utils.bzl", "repo_utils") load("//python/private:util.bzl", "is_importable_name") @@ -113,7 +112,11 @@ environment = kwargs.pop("environment", {}) pythonpath = environment.get("PYTHONPATH", "") - if pythonpath and not types.is_string(pythonpath): + + # NOTE: `types.is_string` from `@bazel_skylib` is not used, so that this + # file can be loaded before the `WORKSPACE` defines `@bazel_skylib`, e.g. + # via `whl_library` from `python/repositories.bzl`. + if pythonpath and type(pythonpath) != "string": environment["PYTHONPATH"] = _construct_pypath(mrctx, entries = pythonpath) kwargs["environment"] = environment
diff --git a/python/private/pypi/whl_extract.bzl b/python/private/pypi/whl_extract.bzl index d7195fd..dc403e7 100644 --- a/python/private/pypi/whl_extract.bzl +++ b/python/private/pypi/whl_extract.bzl
@@ -1,6 +1,5 @@ """A simple whl extractor.""" -load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") load("//python/private:repo_utils.bzl", "repo_utils") load(":whl_metadata.bzl", "find_whl_metadata") @@ -28,12 +27,21 @@ logger: The logger to use """ install_dir_path = rctx.path("site-packages") + + # An empty version is a development build, which is treated as the latest. + bazel_version = (99999, 99999) + if native.bazel_version: + major, minor = native.bazel_version.split(".")[:2] + bazel_version = (int(major), int(minor)) + repo_utils.extract( rctx, archive = whl_path, output = install_dir_path, - supports_whl_extraction = rp_config.supports_whl_extraction, - extract_needs_chmod = rp_config.extract_needs_chmod, + # Extracting `.whl` files requires Bazel 8.3.0 and the extracted files + # need their permissions fixed before Bazel 8.6.0. + supports_whl_extraction = bazel_version >= (8, 3), + extract_needs_chmod = bazel_version < (8, 6), ) metadata_file = find_whl_metadata(
diff --git a/python/private/util.bzl b/python/private/util.bzl index 31f317f..e96bd56 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl
@@ -14,7 +14,6 @@ """Functionality shared by multiple pieces of code.""" -load("@bazel_skylib//lib:types.bzl", "types") load("//python/private:py_internal.bzl", "py_internal") def copy_propagating_kwargs(from_kwargs, into_kwargs = None): @@ -62,7 +61,11 @@ # Preserve the input type: this allows a test verifying the underlying # rule can accept the tuple for the tags argument. - if types.is_tuple(tags): + # NOTE: `types.is_tuple` from `@bazel_skylib` is not used, so that + # this file can be loaded before the `WORKSPACE` defines + # `@bazel_skylib`, e.g. via `whl_library` from + # `python/repositories.bzl`. + if type(tags) == "tuple": attrs["tags"] = tags + (tag,) else: # List concatenation is necessary because the original value