From fc4b246cafaae998494130ea48e35522720361a8 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 21 Sep 2025 22:01:18 +0900 Subject: [PATCH 01/19] chore: remove bazel 5 and bazel 6 support code --- docs/BUILD.bazel | 11 +-- examples/bzlmod/other_module/MODULE.bazel | 18 ++--- python/BUILD.bazel | 6 +- python/private/BUILD.bazel | 4 -- python/private/envsubst.bzl | 2 +- python/private/internal_config_repo.bzl | 35 ++-------- python/private/interpreter.bzl | 2 +- python/private/local_runtime_repo.bzl | 6 +- python/private/py_cc_link_params_info.bzl | 3 +- python/private/py_info.bzl | 3 +- python/private/py_package.bzl | 10 +-- python/private/py_runtime_info.bzl | 4 +- python/private/py_runtime_pair_rule.bzl | 3 +- python/private/py_runtime_rule.bzl | 10 +-- python/private/pypi/pypi_repo_utils.bzl | 5 +- python/private/pypi/whl_library_targets.bzl | 8 +-- python/private/python.bzl | 11 +-- python/private/python_register_toolchains.bzl | 15 ---- python/private/repo_utils.bzl | 22 +----- python/private/util.bzl | 32 --------- python/py_runtime.bzl | 7 +- python/py_runtime_pair.bzl | 6 +- .../tests/proto_to_markdown/BUILD.bazel | 2 - sphinxdocs/tests/sphinx_docs/BUILD.bazel | 3 +- sphinxdocs/tests/sphinx_stardoc/BUILD.bazel | 3 +- tests/base_rules/py_executable_base_tests.bzl | 38 +++------- tests/base_rules/py_info/py_info_tests.bzl | 2 +- tests/base_rules/py_test/py_test_tests.bzl | 15 ---- tests/base_rules/util.bzl | 5 -- tests/bootstrap_impls/BUILD.bazel | 4 +- tests/bootstrap_impls/a/b/c/BUILD.bazel | 3 +- .../transition/multi_version_tests.bzl | 50 +++++--------- .../exec_toolchain_matching_tests.bzl | 8 +-- tests/integration/BUILD.bazel | 11 --- tests/integration/integration_test.bzl | 6 -- tests/integration/runner.py | 3 - tests/py_runtime/py_runtime_tests.bzl | 69 +++++-------------- .../py_runtime_info/py_runtime_info_tests.bzl | 7 +- .../whl_library_targets_tests.bzl | 26 +++---- .../runtime_env_toolchain_tests.bzl | 18 +---- tests/support/sh_py_run_test.bzl | 2 +- tests/support/support.bzl | 3 +- .../whl_from_dir/whl_from_dir_repo.bzl | 2 +- 43 files changed, 99 insertions(+), 404 deletions(-) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index b6c48b0539..1c6f903a1c 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -15,7 +15,6 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@dev_pip//:requirements.bzl", "requirement") load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//python/uv:lock.bzl", "lock") # buildifier: disable=bzl-visibility load("//sphinxdocs:readthedocs.bzl", "readthedocs_install") load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") @@ -107,6 +106,8 @@ sphinx_stardocs( "//python/cc:py_cc_toolchain_bzl", "//python/cc:py_cc_toolchain_info_bzl", "//python/entry_points:py_console_script_binary_bzl", + "//python/extensions:pip_bzl", + "//python/extensions:python_bzl", "//python/local_toolchains:repos_bzl", "//python/private:attr_builders_bzl", "//python/private:builders_util_bzl", @@ -127,13 +128,7 @@ sphinx_stardocs( "//python/uv:uv_bzl", "//python/uv:uv_toolchain_bzl", "//python/uv:uv_toolchain_info_bzl", - ] + ([ - # Bazel 6 + Stardoc isn't able to parse something about the python bzlmod extension - "//python/extensions:python_bzl", - ] if IS_BAZEL_7_OR_HIGHER else []) + ([ - # This depends on @pythons_hub, which is only created under bzlmod, - "//python/extensions:pip_bzl", - ] if IS_BAZEL_7_OR_HIGHER and BZLMOD_ENABLED else []), + ], prefix = "api/rules_python/", tags = ["docs"], target_compatible_with = _TARGET_COMPATIBLE_WITH, diff --git a/examples/bzlmod/other_module/MODULE.bazel b/examples/bzlmod/other_module/MODULE.bazel index f9d6706120..7b88bd73ff 100644 --- a/examples/bzlmod/other_module/MODULE.bazel +++ b/examples/bzlmod/other_module/MODULE.bazel @@ -5,20 +5,10 @@ module( # This module is using the same version of rules_python # that the parent module uses. bazel_dep(name = "rules_python", version = "") - -# The story behind this commented out override: -# This override is necessary to generate/update the requirements file -# for this module. This is because running it via the outer -# module doesn't work -- the `requirements.update` target can't find -# the correct file to update. -# Running in the submodule itself works, but submodules using overrides -# is considered an error until Bazel 6.3, which prevents the outer module -# from depending on this module. -# So until 6.3 and higher is the minimum, we leave this commented out. -# local_path_override( -# module_name = "rules_python", -# path = "../../..", -# ) +local_path_override( + module_name = "rules_python", + path = "../../..", +) PYTHON_NAME_39 = "python_3_9" diff --git a/python/BUILD.bazel b/python/BUILD.bazel index 76fa5dde6e..ef7bf990dc 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -201,11 +201,7 @@ bzl_library( bzl_library( name = "py_runtime_pair_bzl", srcs = ["py_runtime_pair.bzl"], - deps = [ - "//python/private:bazel_tools_bzl", - "//python/private:py_runtime_pair_macro_bzl", - "//python/private:util_bzl", - ], + deps = ["//python/private:py_runtime_pair_macro_bzl"], ) bzl_library( diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index 0c8ccdea99..a6a91373d8 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -71,7 +71,6 @@ bzl_library( ":enum_bzl", ":flags_bzl", ":py_info_bzl", - ":py_internal_bzl", ":reexports_bzl", ":rules_cc_srcs_bzl", "@bazel_skylib//rules:common_settings", @@ -342,7 +341,6 @@ bzl_library( srcs = ["py_cc_link_params_info.bzl"], deps = [ ":rules_cc_srcs_bzl", - ":util_bzl", ], ) @@ -440,7 +438,6 @@ bzl_library( deps = [ ":builders_bzl", ":reexports_bzl", - ":util_bzl", "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -506,7 +503,6 @@ bzl_library( bzl_library( name = "py_runtime_info_bzl", srcs = ["py_runtime_info.bzl"], - deps = [":util_bzl"], ) bzl_library( diff --git a/python/private/envsubst.bzl b/python/private/envsubst.bzl index b2fdb99e1e..b3f8192cb6 100644 --- a/python/private/envsubst.bzl +++ b/python/private/envsubst.bzl @@ -22,7 +22,7 @@ def envsubst(template_string, varnames, getenv): listed in the `varnames` list in the environment defined by the `getenv` function. Typically called with `getenv = rctx.getenv` (if it is available) or `getenv = rctx.os.environ.get` (on e.g. - Bazel 6 or Bazel 7, which don't have `rctx.getenv` yet). + Bazel 7, which don't have `rctx.getenv` yet). Limitations: Unlike the shell, we don't support `${VARNAME}` and `${VARNAME:-default}` in the default expression for a different diff --git a/python/private/internal_config_repo.bzl b/python/private/internal_config_repo.bzl index b57275b672..d8a113789a 100644 --- a/python/private/internal_config_repo.bzl +++ b/python/private/internal_config_repo.bzl @@ -23,8 +23,6 @@ load(":repo_utils.bzl", "repo_utils") _ENABLE_PIPSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PIPSTAR" _ENABLE_PIPSTAR_DEFAULT = "0" -_ENABLE_PYSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PYSTAR" -_ENABLE_PYSTAR_DEFAULT = "1" _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS" _ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0" @@ -88,15 +86,6 @@ _TRANSITION_SETTINGS_DEBUG_TEMPLATE = """ """ def _internal_config_repo_impl(rctx): - pystar_requested = _bool_from_environ(rctx, _ENABLE_PYSTAR_ENVVAR_NAME, _ENABLE_PYSTAR_DEFAULT) - - # Bazel 7+ (dev and later) has native.starlark_doc_extract, and thus the - # py_internal global, which are necessary for the pystar implementation. - if pystar_requested and hasattr(native, "starlark_doc_extract"): - enable_pystar = pystar_requested - else: - enable_pystar = False - if not native.bazel_version or int(native.bazel_version.split(".")[0]) >= 8: builtin_py_info_symbol = "None" builtin_py_runtime_info_symbol = "None" @@ -107,7 +96,7 @@ def _internal_config_repo_impl(rctx): builtin_py_cc_link_params_provider = "PyCcLinkParamsProvider" rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format( - enable_pystar = enable_pystar, + enable_pystar = True, enable_pipstar = _bool_from_environ(rctx, _ENABLE_PIPSTAR_ENVVAR_NAME, _ENABLE_PIPSTAR_DEFAULT), enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT), builtin_py_info_symbol = builtin_py_info_symbol, @@ -115,25 +104,11 @@ def _internal_config_repo_impl(rctx): builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider, )) - if enable_pystar: - shim_content = _PY_INTERNAL_SHIM - py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"' - else: - shim_content = "py_internal_impl = None\n" - py_internal_dep = "" - - # Bazel 5 doesn't support repository visibility, so just use public - # as a stand-in - if native.bazel_version.startswith("5."): - visibility = "//visibility:public" - else: - visibility = "@rules_python//:__subpackages__" - rctx.file("BUILD", ROOT_BUILD_TEMPLATE.format( - py_internal_dep = py_internal_dep, - visibility = visibility, + py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"', + visibility = "@rules_python//:__subpackages__", )) - rctx.file("py_internal.bzl", shim_content) + rctx.file("py_internal.bzl", _PY_INTERNAL_SHIM) rctx.file( "extra_transition_settings.bzl", @@ -155,7 +130,7 @@ def _internal_config_repo_impl(rctx): internal_config_repo = repository_rule( implementation = _internal_config_repo_impl, configure = True, - environ = [_ENABLE_PYSTAR_ENVVAR_NAME], + environ = [_ENABLE_PIPSTAR_ENVVAR_NAME], attrs = { "transition_setting_generators": attr.string_list_dict(), "transition_settings": attr.string_list(), diff --git a/python/private/interpreter.bzl b/python/private/interpreter.bzl index c66d3dc21e..ca65262bfc 100644 --- a/python/private/interpreter.bzl +++ b/python/private/interpreter.bzl @@ -72,7 +72,7 @@ interpreter_binary = rule( mandatory = True, ), "_bash_runfiles": attr.label( - default = "@bazel_tools//tools/bash/runfiles", + default = "@rules_shell//shell/runfiles", ), "_template": attr.label( default = "//python/private:interpreter_tmpl.sh", diff --git a/python/private/local_runtime_repo.bzl b/python/private/local_runtime_repo.bzl index c053a03508..27c90b1bc9 100644 --- a/python/private/local_runtime_repo.bzl +++ b/python/private/local_runtime_repo.bzl @@ -71,7 +71,7 @@ def _symlink_first_library(rctx, logger, libraries): else: linked = "lib/{}".format(origin.basename) logger.debug("Symlinking {} to {}".format(origin, linked)) - repo_utils.watch(rctx, origin) + rctx.watch(origin) rctx.symlink(origin, linked) break @@ -142,7 +142,7 @@ def _local_runtime_repo_impl(rctx): # path is an error. Silently skip, since includes are only necessary # if C extensions are built. if include_path.exists and include_path.is_dir: - repo_utils.watch_tree(rctx, include_path) + rctx.watch_tree(include_path) else: pass @@ -268,7 +268,7 @@ def _resolve_interpreter_path(rctx): resolved_path = result.binary describe_failure = result.describe_failure else: - repo_utils.watch(rctx, rctx.attr.interpreter_path) + rctx.watch(rctx.attr.interpreter_path) resolved_path = rctx.path(rctx.attr.interpreter_path) if not resolved_path.exists: describe_failure = lambda: "Path not found: {}".format(repr(rctx.attr.interpreter_path)) diff --git a/python/private/py_cc_link_params_info.bzl b/python/private/py_cc_link_params_info.bzl index 35919a04e2..2fbd255eed 100644 --- a/python/private/py_cc_link_params_info.bzl +++ b/python/private/py_cc_link_params_info.bzl @@ -14,7 +14,6 @@ """Providers for Python rules.""" load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") -load(":util.bzl", "define_bazel_6_provider") def _PyCcLinkParamsInfo_init(cc_info): return { @@ -22,7 +21,7 @@ def _PyCcLinkParamsInfo_init(cc_info): } # buildifier: disable=name-conventions -PyCcLinkParamsInfo, _unused_raw_py_cc_link_params_provider_ctor = define_bazel_6_provider( +PyCcLinkParamsInfo, _unused_raw_py_cc_link_params_provider_ctor = provider( doc = ("Python-wrapper to forward {obj}`CcInfo.linking_context`. This is to " + "allow Python targets to propagate C++ linking information, but " + "without the Python target appearing to be a valid C++ rule dependency"), diff --git a/python/private/py_info.bzl b/python/private/py_info.bzl index f96dec554b..18a1015761 100644 --- a/python/private/py_info.bzl +++ b/python/private/py_info.bzl @@ -16,7 +16,6 @@ load("@rules_python_internal//:rules_python_config.bzl", "config") load(":builders.bzl", "builders") load(":reexports.bzl", "BuiltinPyInfo") -load(":util.bzl", "define_bazel_6_provider") def _VenvSymlinkKind_typedef(): """An enum of types of venv directories. @@ -160,7 +159,7 @@ def _PyInfo_init( "venv_symlinks": venv_symlinks, } -PyInfo, _unused_raw_py_info_ctor = define_bazel_6_provider( +PyInfo, _unused_raw_py_info_ctor = provider( doc = """Encapsulates information provided by the Python rules. Instead of creating this object directly, use {obj}`PyInfoBuilder` and diff --git a/python/private/py_package.bzl b/python/private/py_package.bzl index adf2b6deef..2d19ca7437 100644 --- a/python/private/py_package.bzl +++ b/python/private/py_package.bzl @@ -41,14 +41,8 @@ def _py_package_impl(ctx): py_info.merge_target(dep) py_info = py_info.build() inputs.add(py_info.transitive_sources) - - # Remove conditional once Bazel 6 support dropped. - if hasattr(py_info, "transitive_pyc_files"): - inputs.add(py_info.transitive_pyc_files) - - if hasattr(py_info, "transitive_pyi_files"): - inputs.add(py_info.transitive_pyi_files) - + inputs.add(py_info.transitive_pyc_files) + inputs.add(py_info.transitive_pyi_files) inputs = inputs.build() # TODO: '/' is wrong on windows, but the path separator is not available in starlark. diff --git a/python/private/py_runtime_info.bzl b/python/private/py_runtime_info.bzl index efe14b2c06..af4e7f0596 100644 --- a/python/private/py_runtime_info.bzl +++ b/python/private/py_runtime_info.bzl @@ -13,8 +13,6 @@ # limitations under the License. """Providers for Python rules.""" -load(":util.bzl", "define_bazel_6_provider") - DEFAULT_STUB_SHEBANG = "#!/usr/bin/env python3" _PYTHON_VERSION_VALUES = ["PY2", "PY3"] @@ -124,7 +122,7 @@ def _PyRuntimeInfo_init( "zip_main_template": zip_main_template, } -PyRuntimeInfo, _unused_raw_py_runtime_info_ctor = define_bazel_6_provider( +PyRuntimeInfo, _unused_raw_py_runtime_info_ctor = provider( doc = """Contains information about a Python runtime, as returned by the `py_runtime` rule. diff --git a/python/private/py_runtime_pair_rule.bzl b/python/private/py_runtime_pair_rule.bzl index 61cbdcd6f4..203e5d4df7 100644 --- a/python/private/py_runtime_pair_rule.bzl +++ b/python/private/py_runtime_pair_rule.bzl @@ -19,7 +19,6 @@ load("//python:py_runtime_info.bzl", "PyRuntimeInfo") load(":common_labels.bzl", "labels") load(":flags.bzl", "read_possibly_native_flag") load(":reexports.bzl", "BuiltinPyRuntimeInfo") -load(":util.bzl", "IS_BAZEL_7_OR_HIGHER") def _py_runtime_pair_impl(ctx): if ctx.attr.py2_runtime != None: @@ -58,7 +57,7 @@ def _get_py_runtime_info(target): # 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) or BuiltinPyRuntimeInfo == None: + if (PyRuntimeInfo in target) or BuiltinPyRuntimeInfo == None: return target[PyRuntimeInfo] else: return target[BuiltinPyRuntimeInfo] diff --git a/python/private/py_runtime_rule.bzl b/python/private/py_runtime_rule.bzl index ba1a390ef3..5020d7ad9b 100644 --- a/python/private/py_runtime_rule.bzl +++ b/python/private/py_runtime_rule.bzl @@ -21,7 +21,6 @@ load(":flags.bzl", "FreeThreadedFlag") load(":py_internal.bzl", "py_internal") load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo") load(":reexports.bzl", "BuiltinPyRuntimeInfo") -load(":util.bzl", "IS_BAZEL_7_OR_HIGHER") _py_builtins = py_internal @@ -133,9 +132,6 @@ def _py_runtime_impl(ctx): supports_build_time_venv = ctx.attr.supports_build_time_venv, )) - if not IS_BAZEL_7_OR_HIGHER: - builtin_py_runtime_info_kwargs.pop("bootstrap_template") - providers = [ PyRuntimeInfo(**py_runtime_info_kwargs), DefaultInfo( @@ -388,11 +384,7 @@ The {obj}`PyRuntimeInfo.zip_main_template` field. ) def _is_singleton_depset(files): - # Bazel 6 doesn't have this helper to optimize detecting singleton depsets. - if _py_builtins: - return _py_builtins.is_singleton_depset(files) - else: - return len(files.to_list()) == 1 + return _py_builtins.is_singleton_depset(files) def _interpreter_version_info_from_version_str(version_str): parts = version_str.split(".") diff --git a/python/private/pypi/pypi_repo_utils.bzl b/python/private/pypi/pypi_repo_utils.bzl index bb2acc850a..04c9b5d685 100644 --- a/python/private/pypi/pypi_repo_utils.bzl +++ b/python/private/pypi/pypi_repo_utils.bzl @@ -107,9 +107,8 @@ def _construct_pypath(mrctx, *, entries): def _execute_prep(mrctx, *, python, srcs, **kwargs): for src in srcs: # This will ensure that we will re-evaluate the bzlmod extension or - # refetch the repository_rule when the srcs change. This should work on - # Bazel versions without `mrctx.watch` as well. - repo_utils.watch(mrctx, mrctx.path(src)) + # refetch the repository_rule when the srcs change. + mrctx.watch(mrctx.path(src)) environment = kwargs.pop("environment", {}) pythonpath = environment.get("PYTHONPATH", "") diff --git a/python/private/pypi/whl_library_targets.bzl b/python/private/pypi/whl_library_targets.bzl index aed5bc74f5..b19be5d465 100644 --- a/python/private/pypi/whl_library_targets.bzl +++ b/python/private/pypi/whl_library_targets.bzl @@ -315,13 +315,7 @@ def whl_library_targets( deps_by_platform = dependencies_by_platform, deps_conditional = deps_conditional, tmpl = dep_template.format(name = "{}", target = WHEEL_FILE_PUBLIC_LABEL), - # NOTE @aignas 2024-10-28: Actually, `select` is not part of - # `native`, but in order to support bazel 6.4 in unit tests, I - # have to somehow pass the `select` implementation in the unit - # tests and I chose this to be routed through the `native` - # struct. So, tests` will be successful in `getattr` and the - # real code will use the fallback provided here. - select = getattr(native, "select", select), + select = select, ), visibility = impl_vis, ) diff --git a/python/private/python.bzl b/python/private/python.bzl index 6eb8a3742e..faad53fab4 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -29,7 +29,6 @@ load( "sorted_host_platform_names", "sorted_host_platforms", ) -load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER") load(":version.bzl", "version") def parse_modules(*, module_ctx, logger, _fail = fail): @@ -932,14 +931,6 @@ def _create_toolchain_attrs_struct(*, tag = None, python_version = None, toolcha ignore_root_user_error = getattr(tag, "ignore_root_user_error", True), ) -def _get_bazel_version_specific_kwargs(): - kwargs = {} - - if IS_BAZEL_6_4_OR_HIGHER: - kwargs["environ"] = ["RULES_PYTHON_BZLMOD_DEBUG"] - - return kwargs - _defaults = tag_class( doc = """Tag class to specify the default Python version.""", attrs = { @@ -1356,7 +1347,7 @@ python = module_extension( "single_version_platform_override": _single_version_platform_override, "toolchain": _toolchain, }, - **_get_bazel_version_specific_kwargs() + environ = ["RULES_PYTHON_BZLMOD_DEBUG"], ) _DEBUG_BUILD_CONTENT = """ diff --git a/python/private/python_register_toolchains.bzl b/python/private/python_register_toolchains.bzl index 2e0748deb0..9e75c41978 100644 --- a/python/private/python_register_toolchains.bzl +++ b/python/private/python_register_toolchains.bzl @@ -97,21 +97,6 @@ def python_register_toolchains( toolchain_repo_name = "{name}_toolchains".format(name = name) - # When using unreleased Bazel versions, the version is an empty string - if native.bazel_version: - bazel_major = int(native.bazel_version.split(".")[0]) - if bazel_major < 6: - if register_coverage_tool: - # buildifier: disable=print - print(( - "WARNING: ignoring register_coverage_tool=True when " + - "registering @{name}: Bazel 6+ required, got {version}" - ).format( - name = name, - version = native.bazel_version, - )) - register_coverage_tool = False - # list[str] of the platform names that were used loaded_platforms = [] diff --git a/python/private/repo_utils.bzl b/python/private/repo_utils.bzl index 32a5b70e15..77eac55c16 100644 --- a/python/private/repo_utils.bzl +++ b/python/private/repo_utils.bzl @@ -291,7 +291,7 @@ def _which_unchecked(mrctx, binary_name): """ binary = mrctx.which(binary_name) if binary: - _watch(mrctx, binary) + mrctx.watch(binary) describe_failure = None else: path = _getenv(mrctx, "PATH", "") @@ -429,24 +429,6 @@ def _get_platforms_cpu_name(mrctx): return "riscv64" return arch -# TODO: Remove after Bazel 6 support dropped -def _watch(mrctx, *args, **kwargs): - """Calls mrctx.watch, if available.""" - if not args and not kwargs: - fail("'watch' needs at least a single argument.") - - if hasattr(mrctx, "watch"): - mrctx.watch(*args, **kwargs) - -# TODO: Remove after Bazel 6 support dropped -def _watch_tree(mrctx, *args, **kwargs): - """Calls mrctx.watch_tree, if available.""" - if not args and not kwargs: - fail("'watch_tree' needs at least a single argument.") - - if hasattr(mrctx, "watch_tree"): - mrctx.watch_tree(*args, **kwargs) - repo_utils = struct( # keep sorted execute_checked = _execute_checked, @@ -457,8 +439,6 @@ repo_utils = struct( getenv = _getenv, is_repo_debug_enabled = _is_repo_debug_enabled, logger = _logger, - watch = _watch, - watch_tree = _watch_tree, which_checked = _which_checked, which_unchecked = _which_unchecked, ) diff --git a/python/private/util.bzl b/python/private/util.bzl index 4d2da57760..5a180e5711 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -86,36 +86,4 @@ def add_tag(attrs, tag): else: attrs["tags"] = [tag] -# Helper to make the provider definitions not crash under Bazel 5.4: -# Bazel 5.4 doesn't support the `init` arg of `provider()`, so we have to -# not pass that when using Bazel 5.4. But, not passing the `init` arg -# changes the return value from a two-tuple to a single value, which then -# breaks Bazel 6+ code. -# This isn't actually used under Bazel 5.4, so just stub out the values -# to get past the loading phase. -def define_bazel_6_provider(doc, fields, **kwargs): - """Define a provider, or a stub for pre-Bazel 7.""" - if not IS_BAZEL_6_OR_HIGHER: - return provider("Stub, not used", fields = []), None - return provider(doc = doc, fields = fields, **kwargs) - IS_BAZEL_7_4_OR_HIGHER = hasattr(native, "legacy_globals") - -IS_BAZEL_7_OR_HIGHER = hasattr(native, "starlark_doc_extract") - -# Bazel 5.4 has a bug where every access of testing.ExecutionInfo is a -# different object that isn't equal to any other. This is fixed in bazel 6+. -IS_BAZEL_6_OR_HIGHER = testing.ExecutionInfo == testing.ExecutionInfo - -_marker_rule_to_detect_bazel_6_4_or_higher = rule(implementation = lambda ctx: None) - -# Bazel 6.4 and higher have a bug fix where rule names show up in the str() -# of a rule. See -# https://github.com/bazelbuild/bazel/commit/002490b9a2376f0b2ea4a37102c5e94fc50a65ba -# https://github.com/bazelbuild/bazel/commit/443cbcb641e17f7337ccfdecdfa5e69bc16cae55 -# This technique is done instead of using native.bazel_version because, -# under stardoc, the native.bazel_version attribute is entirely missing, which -# prevents doc generation from being able to correctly generate docs. -IS_BAZEL_6_4_OR_HIGHER = "_marker_rule_to_detect_bazel_6_4_or_higher" in str( - _marker_rule_to_detect_bazel_6_4_or_higher, -) diff --git a/python/py_runtime.bzl b/python/py_runtime.bzl index dad2965cf5..3cd9a8efac 100644 --- a/python/py_runtime.bzl +++ b/python/py_runtime.bzl @@ -14,11 +14,8 @@ """Public entry point for py_runtime.""" -load("//python/private:py_runtime_macro.bzl", _starlark_py_runtime = "py_runtime") -load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER", "add_migration_tag") - -# buildifier: disable=native-python -_py_runtime_impl = _starlark_py_runtime if IS_BAZEL_6_OR_HIGHER else native.py_runtime +load("//python/private:py_runtime_macro.bzl", _py_runtime_impl = "py_runtime") +load("//python/private:util.bzl", "add_migration_tag") def py_runtime(**attrs): """Creates an executable Python program. diff --git a/python/py_runtime_pair.bzl b/python/py_runtime_pair.bzl index 26d378fce2..f44c722d54 100644 --- a/python/py_runtime_pair.bzl +++ b/python/py_runtime_pair.bzl @@ -14,11 +14,7 @@ """Public entry point for py_runtime_pair.""" -load("@bazel_tools//tools/python:toolchain.bzl", _bazel_tools_impl = "py_runtime_pair") -load("//python/private:py_runtime_pair_macro.bzl", _starlark_impl = "py_runtime_pair") -load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") - -_py_runtime_pair = _starlark_impl if IS_BAZEL_6_OR_HIGHER else _bazel_tools_impl +load("//python/private:py_runtime_pair_macro.bzl", _py_runtime_pair = "py_runtime_pair") # NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our # doc generator gives useful API docs. diff --git a/sphinxdocs/tests/proto_to_markdown/BUILD.bazel b/sphinxdocs/tests/proto_to_markdown/BUILD.bazel index 09f537472c..2964785eed 100644 --- a/sphinxdocs/tests/proto_to_markdown/BUILD.bazel +++ b/sphinxdocs/tests/proto_to_markdown/BUILD.bazel @@ -13,12 +13,10 @@ # limitations under the License. load("//python:py_test.bzl", "py_test") -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility py_test( name = "proto_to_markdown_test", srcs = ["proto_to_markdown_test.py"], - target_compatible_with = [] if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"], deps = [ "//sphinxdocs/private:proto_to_markdown_lib", "@dev_pip//absl_py", diff --git a/sphinxdocs/tests/sphinx_docs/BUILD.bazel b/sphinxdocs/tests/sphinx_docs/BUILD.bazel index f9c82967c1..33b98ec585 100644 --- a/sphinxdocs/tests/sphinx_docs/BUILD.bazel +++ b/sphinxdocs/tests/sphinx_docs/BUILD.bazel @@ -1,5 +1,4 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") load(":defs.bzl", "gen_directory") @@ -13,7 +12,7 @@ _TARGET_COMPATIBLE_WITH = select({ "@platforms//os:linux": [], "@platforms//os:macos": [], "//conditions:default": ["@platforms//:incompatible"], -}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] +}) sphinx_docs( name = "docs", diff --git a/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel b/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel index e3a68ea225..af9af30886 100644 --- a/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel +++ b/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel @@ -1,7 +1,6 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//python:py_test.bzl", "py_test") -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") load("//sphinxdocs:sphinx_stardoc.bzl", "sphinx_stardoc", "sphinx_stardocs") @@ -15,7 +14,7 @@ _TARGET_COMPATIBLE_WITH = select({ "@platforms//os:linux": [], "@platforms//os:macos": [], "//conditions:default": ["@platforms//:incompatible"], -}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] +}) sphinx_docs( name = "docs", diff --git a/tests/base_rules/py_executable_base_tests.bzl b/tests/base_rules/py_executable_base_tests.bzl index 4e451289dc..a3e4ca1240 100644 --- a/tests/base_rules/py_executable_base_tests.bzl +++ b/tests/base_rules/py_executable_base_tests.bzl @@ -21,7 +21,6 @@ load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:py_executable_info.bzl", "PyExecutableInfo") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/base_rules:base_tests.bzl", "create_base_tests") load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util") load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject") @@ -188,11 +187,6 @@ def _test_default_main_can_have_multiple_path_segments_impl(env, target): ) def _test_default_main_must_be_in_srcs(name, config): - # Bazel 5 will crash with a Java stacktrace when the native Python - # rules have an error. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject", @@ -213,11 +207,6 @@ def _test_default_main_must_be_in_srcs_impl(env, target): ) def _test_default_main_cannot_be_ambiguous(name, config): - # Bazel 5 will crash with a Java stacktrace when the native Python - # rules have an error. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject", @@ -260,11 +249,6 @@ def _test_explicit_main_impl(env, target): ) def _test_explicit_main_cannot_be_ambiguous(name, config): - # Bazel 5 will crash with a Java stacktrace when the native Python - # rules have an error. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject", @@ -310,22 +294,16 @@ def _test_files_to_build_impl(env, target): "{package}/{test_name}_subject.py", ]) - if IS_BAZEL_7_OR_HIGHER: - # As of Bazel 7, the first default output is the executable, so - # verify that is the case. rules_testing - # DepsetFileSubject.contains_exactly doesn't provide an in_order() - # call, nor access to the underlying depset, so we have to do things - # manually. - first_default_output = target[DefaultInfo].files.to_list()[0] - executable = target[DefaultInfo].files_to_run.executable - env.expect.that_file(first_default_output).equals(executable) + # As of Bazel 7, the first default output is the executable, so + # verify that is the case. rules_testing + # DepsetFileSubject.contains_exactly doesn't provide an in_order() + # call, nor access to the underlying depset, so we have to do things + # manually. + first_default_output = target[DefaultInfo].files.to_list()[0] + executable = target[DefaultInfo].files_to_run.executable + env.expect.that_file(first_default_output).equals(executable) def _test_name_cannot_end_in_py(name, config): - # Bazel 5 will crash with a Java stacktrace when the native Python - # rules have an error. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject.py", diff --git a/tests/base_rules/py_info/py_info_tests.bzl b/tests/base_rules/py_info/py_info_tests.bzl index aa252a2937..52c483c417 100644 --- a/tests/base_rules/py_info/py_info_tests.bzl +++ b/tests/base_rules/py_info/py_info_tests.bzl @@ -42,7 +42,7 @@ def _provide_py_info_impl(ctx): if config.enable_pystar: providers.append(PyInfo(**kwargs)) - # Handle Bazel 6 or if Bazel autoloading is enabled + # Handle if Bazel autoloading is enabled if not config.enable_pystar or (BuiltinPyInfo and PyInfo != BuiltinPyInfo): providers.append(BuiltinPyInfo(**{ k: kwargs[k] diff --git a/tests/base_rules/py_test/py_test_tests.bzl b/tests/base_rules/py_test/py_test_tests.bzl index 1ec1dc428f..c28eec4346 100644 --- a/tests/base_rules/py_test/py_test_tests.bzl +++ b/tests/base_rules/py_test/py_test_tests.bzl @@ -39,14 +39,6 @@ _SKIP_WINDOWS = { _tests = [] def _test_mac_requires_darwin_for_execution(name, config): - # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is - # a different object that isn't equal to any other, which prevents - # rules_testing from detecting it properly and fails with an error. - # This is fixed in Bazel 6+. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return - rt_util.helper_target( config.rule, name = name + "_subject", @@ -74,13 +66,6 @@ def _test_mac_requires_darwin_for_execution_impl(env, target): _tests.append(_test_mac_requires_darwin_for_execution) def _test_non_mac_doesnt_require_darwin_for_execution(name, config): - # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is - # a different object that isn't equal to any other, which prevents - # rules_testing from detecting it properly and fails with an error. - # This is fixed in Bazel 6+. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject", diff --git a/tests/base_rules/util.bzl b/tests/base_rules/util.bzl index a02cafa992..9fb66d7eb3 100644 --- a/tests/base_rules/util.bzl +++ b/tests/base_rules/util.bzl @@ -14,7 +14,6 @@ """Helpers and utilities multiple tests re-use.""" load("@bazel_skylib//lib:structs.bzl", "structs") -load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") # buildifier: disable=bzl-visibility # Use this with is_windows() WINDOWS_ATTR = {"windows": attr.label(default = "@platforms//os:windows")} @@ -53,9 +52,6 @@ def _struct_with(s, **kwargs): struct_dict.update(kwargs) return struct(**struct_dict) -def _is_bazel_6_or_higher(): - return IS_BAZEL_6_OR_HIGHER - def _is_windows(env): """Tell if the target platform is windows. @@ -72,6 +68,5 @@ def _is_windows(env): util = struct( create_tests = _create_tests, struct_with = _struct_with, - is_bazel_6_or_higher = _is_bazel_6_or_higher, is_windows = _is_windows, ) diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel index c3d44df240..245bf0cc4a 100644 --- a/tests/bootstrap_impls/BUILD.bazel +++ b/tests/bootstrap_impls/BUILD.bazel @@ -43,9 +43,7 @@ sh_test( "ZIP_RLOCATION": "$(rlocationpaths :bootstrap_script_zipapp_zip)".format(), }, target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT, - deps = [ - "@bazel_tools//tools/bash/runfiles", - ], + deps = ["@rules_shell//shell/runfiles"], ) sh_py_run_test( diff --git a/tests/bootstrap_impls/a/b/c/BUILD.bazel b/tests/bootstrap_impls/a/b/c/BUILD.bazel index 1659ef25bc..1c4b1e7b6b 100644 --- a/tests/bootstrap_impls/a/b/c/BUILD.bazel +++ b/tests/bootstrap_impls/a/b/c/BUILD.bazel @@ -1,10 +1,9 @@ -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:py_reconfig.bzl", "py_reconfig_test") _SUPPORTS_BOOTSTRAP_SCRIPT = select({ "@platforms//os:windows": ["@platforms//:incompatible"], "//conditions:default": [], -}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] +}) py_reconfig_test( name = "nested_dir_test", diff --git a/tests/config_settings/transition/multi_version_tests.bzl b/tests/config_settings/transition/multi_version_tests.bzl index 93f6efd728..2f9e28a0b3 100644 --- a/tests/config_settings/transition/multi_version_tests.bzl +++ b/tests/config_settings/transition/multi_version_tests.bzl @@ -16,12 +16,11 @@ load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION") load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") -load("@rules_testing//lib:util.bzl", "TestingAspectInfo", rt_util = "util") +load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:py_binary.bzl", "py_binary") load("//python:py_info.bzl", "PyInfo") load("//python:py_test.bzl", "py_test") load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "CC_TOOLCHAIN") # NOTE @aignas 2024-06-04: we are using here something that is registered in the MODULE.Bazel @@ -105,21 +104,14 @@ def _test_py_binary_windows_build_python_zip_false(name): ) def _test_py_binary_windows_build_python_zip_false_impl(env, target): - default_outputs = env.expect.that_target(target).default_outputs() - if IS_BAZEL_7_OR_HIGHER: - # TODO: These outputs aren't correct. The outputs shouldn't - # have the "_" prefix on them (those are coming from the underlying - # wrapped binary). - env.expect.that_target(target).default_outputs().contains_exactly([ - "{package}/{test_name}_subject.exe", - "{package}/{test_name}_subject", - "{package}/{test_name}_subject.py", - ]) - else: - inner_exe = target[TestingAspectInfo].attrs.target[DefaultInfo].files_to_run.executable - default_outputs.contains_at_least([ - inner_exe.short_path, - ]) + # TODO: These outputs aren't correct. The outputs shouldn't + # have the "_" prefix on them (those are coming from the underlying + # wrapped binary). + env.expect.that_target(target).default_outputs().contains_exactly([ + "{package}/{test_name}_subject.exe", + "{package}/{test_name}_subject", + "{package}/{test_name}_subject.py", + ]) _tests.append(_test_py_binary_windows_build_python_zip_false) @@ -132,21 +124,15 @@ def _test_py_binary_windows_build_python_zip_true(name): def _test_py_binary_windows_build_python_zip_true_impl(env, target): default_outputs = env.expect.that_target(target).default_outputs() - if IS_BAZEL_7_OR_HIGHER: - # TODO: These outputs aren't correct. The outputs shouldn't - # have the "_" prefix on them (those are coming from the underlying - # wrapped binary). - default_outputs.contains_exactly([ - "{package}/{test_name}_subject.exe", - "{package}/{test_name}_subject.py", - "{package}/{test_name}_subject.zip", - ]) - else: - inner_exe = target[TestingAspectInfo].attrs.target[DefaultInfo].files_to_run.executable - default_outputs.contains_at_least([ - "{package}/{test_name}_subject.zip", - inner_exe.short_path, - ]) + + # TODO: These outputs aren't correct. The outputs shouldn't + # have the "_" prefix on them (those are coming from the underlying + # wrapped binary). + default_outputs.contains_exactly([ + "{package}/{test_name}_subject.exe", + "{package}/{test_name}_subject.py", + "{package}/{test_name}_subject.zip", + ]) _tests.append(_test_py_binary_windows_build_python_zip_true) diff --git a/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl b/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl index 43a9717314..b3ff294b6f 100644 --- a/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl +++ b/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl @@ -20,7 +20,6 @@ load("//python:py_runtime.bzl", "py_runtime") load("//python:py_runtime_pair.bzl", "py_runtime_pair") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "LINUX", "MAC") _LookupInfo = provider() # buildifier: disable=provider-params @@ -143,11 +142,10 @@ def _test_exec_matches_target_python_version_impl(env, target): env.expect.that_str(target_runtime.interpreter_path).equals("/linux/python3.12") env.expect.that_str(exec_runtime.interpreter_path).equals("/mac/python3.12") - if IS_BAZEL_7_OR_HIGHER: - target_version = target_runtime.interpreter_version_info - exec_version = exec_runtime.interpreter_version_info + target_version = target_runtime.interpreter_version_info + exec_version = exec_runtime.interpreter_version_info - env.expect.that_bool(target_version == exec_version) + env.expect.that_bool(target_version == exec_version) def exec_toolchain_matching_test_suite(name): test_suite(name = name, tests = _tests) diff --git a/tests/integration/BUILD.bazel b/tests/integration/BUILD.bazel index df7fe15444..fa8f96b61d 100644 --- a/tests/integration/BUILD.bazel +++ b/tests/integration/BUILD.bazel @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("@bazel_binaries//:defs.bzl", "bazel_binaries") load("@rules_bazel_integration_test//bazel_integration_test:defs.bzl", "default_test_runner") load("//python:py_library.bzl", "py_library") load(":integration_test.bzl", "rules_python_integration_test") @@ -88,20 +87,10 @@ rules_python_integration_test( rules_python_integration_test( name = "local_toolchains_test", - bazel_versions = [ - version - for version in bazel_binaries.versions.all - if not version.startswith("6.") - ], ) rules_python_integration_test( name = "local_toolchains_workspace_test", - bazel_versions = [ - version - for version in bazel_binaries.versions.all - if not version.startswith("6.") - ], bzlmod = False, workspace_path = "local_toolchains", ) diff --git a/tests/integration/integration_test.bzl b/tests/integration/integration_test.bzl index c437953319..90cc4a3fb7 100644 --- a/tests/integration/integration_test.bzl +++ b/tests/integration/integration_test.bzl @@ -35,12 +35,6 @@ def _test_runner(*, name, bazel_version, py_main, bzlmod, gazelle_plugin): ) return test_runner - if bazel_version.startswith("6") and not bzlmod: - if gazelle_plugin: - return "//tests/integration:bazel_6_4_workspace_test_runner_gazelle_plugin" - else: - return "//tests/integration:bazel_6_4_workspace_test_runner" - if bzlmod and gazelle_plugin: return "//tests/integration:test_runner_gazelle_plugin" elif bzlmod: diff --git a/tests/integration/runner.py b/tests/integration/runner.py index 2534ab2d90..caefd10f12 100644 --- a/tests/integration/runner.py +++ b/tests/integration/runner.py @@ -84,9 +84,6 @@ def setUp(self): "PATH": os.environ["PATH"], "TEST_TMPDIR": str(self.test_tmp_dir), "TMP": str(self.tmp_dir), - # For some reason, this is necessary for Bazel 6.4 to work. - # If not present, it can't find some bash helpers in @bazel_tools - "RUNFILES_DIR": os.environ["TEST_SRCDIR"], } def run_bazel(self, *args: str, check: bool = True) -> ExecuteResult: diff --git a/tests/py_runtime/py_runtime_tests.bzl b/tests/py_runtime/py_runtime_tests.bzl index 4ec7590ab2..051b4495a2 100644 --- a/tests/py_runtime/py_runtime_tests.bzl +++ b/tests/py_runtime/py_runtime_tests.bzl @@ -21,7 +21,6 @@ load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:py_runtime.bzl", "py_runtime") load("//python:py_runtime_info.bzl", "PyRuntimeInfo") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility -load("//tests/base_rules:util.bzl", br_util = "util") load("//tests/support:py_runtime_info_subject.bzl", "py_runtime_info_subject") _tests = [] @@ -81,17 +80,11 @@ def _test_bootstrap_template_impl(env, target): _tests.append(_test_bootstrap_template) def _test_cannot_have_both_inbuild_and_system_interpreter(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = { - "interpreter": "fake_interpreter", - "interpreter_path": "/some/path", - } - attr_values = {} - else: - py_runtime_kwargs = { - "interpreter_path": "/some/path", - } - attr_values = _SKIP_TEST + py_runtime_kwargs = { + "interpreter": "fake_interpreter", + "interpreter_path": "/some/path", + } + attr_values = {} rt_util.helper_target( py_runtime, name = name + "_subject", @@ -114,12 +107,8 @@ def _test_cannot_have_both_inbuild_and_system_interpreter_impl(env, target): _tests.append(_test_cannot_have_both_inbuild_and_system_interpreter) def _test_cannot_specify_files_for_system_interpreter(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = {"files": ["foo.txt"]} - attr_values = {} - else: - py_runtime_kwargs = {} - attr_values = _SKIP_TEST + py_runtime_kwargs = {"files": ["foo.txt"]} + attr_values = {} rt_util.helper_target( py_runtime, name = name + "_subject", @@ -143,14 +132,10 @@ def _test_cannot_specify_files_for_system_interpreter_impl(env, target): _tests.append(_test_cannot_specify_files_for_system_interpreter) def _test_coverage_tool_executable(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = { - "coverage_tool": name + "_coverage_tool", - } - attr_values = {} - else: - py_runtime_kwargs = {} - attr_values = _SKIP_TEST + py_runtime_kwargs = { + "coverage_tool": name + "_coverage_tool", + } + attr_values = {} rt_util.helper_target( py_runtime, @@ -183,14 +168,10 @@ def _test_coverage_tool_executable_impl(env, target): _tests.append(_test_coverage_tool_executable) def _test_coverage_tool_plain_files(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = { - "coverage_tool": name + "_coverage_tool", - } - attr_values = {} - else: - py_runtime_kwargs = {} - attr_values = _SKIP_TEST + py_runtime_kwargs = { + "coverage_tool": name + "_coverage_tool", + } + attr_values = {} rt_util.helper_target( py_runtime, name = name + "_subject", @@ -334,14 +315,8 @@ def _test_interpreter_binary_with_single_output_and_runfiles_impl(env, target): _tests.append(_test_interpreter_binary_with_single_output_and_runfiles) def _test_must_have_either_inbuild_or_system_interpreter(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = {} - attr_values = {} - else: - py_runtime_kwargs = { - "interpreter_path": "/some/path", - } - attr_values = _SKIP_TEST + py_runtime_kwargs = {} + attr_values = {} rt_util.helper_target( py_runtime, name = name + "_subject", @@ -385,14 +360,8 @@ def _test_system_interpreter_impl(env, target): _tests.append(_test_system_interpreter) def _test_system_interpreter_must_be_absolute(name): - # Bazel 5.4 will entirely crash when an invalid interpreter_path - # is given. - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = {"interpreter_path": "relative/path"} - attr_values = {} - else: - py_runtime_kwargs = {"interpreter_path": "/junk/value/for/bazel5.4"} - attr_values = _SKIP_TEST + py_runtime_kwargs = {"interpreter_path": "relative/path"} + attr_values = {} rt_util.helper_target( py_runtime, name = name + "_subject", diff --git a/tests/py_runtime_info/py_runtime_info_tests.bzl b/tests/py_runtime_info/py_runtime_info_tests.bzl index 9acf541683..a44fb60c2f 100644 --- a/tests/py_runtime_info/py_runtime_info_tests.bzl +++ b/tests/py_runtime_info/py_runtime_info_tests.bzl @@ -16,18 +16,13 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") load("//python:py_runtime_info.bzl", "PyRuntimeInfo") -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility def _create_py_runtime_info_without_interpreter_version_info_impl(ctx): - kwargs = {} - if IS_BAZEL_7_OR_HIGHER: - kwargs["bootstrap_template"] = ctx.attr.bootstrap_template - return [PyRuntimeInfo( interpreter = ctx.file.interpreter, files = depset(ctx.files.files), python_version = "PY3", - **kwargs + bootstrap_template = ctx.attr.bootstrap_template, )] _create_py_runtime_info_without_interpreter_version_info = rule( diff --git a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl index ec7ca63832..6af25a4e75 100644 --- a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl +++ b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl @@ -217,7 +217,6 @@ def _test_whl_and_library_deps_from_requires(env): filegroup = lambda **kwargs: filegroup_calls.append(kwargs), config_setting = lambda **_: None, glob = mock_glob.glob, - select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -230,7 +229,7 @@ def _test_whl_and_library_deps_from_requires(env): { "name": "whl", "srcs": ["foo-0-py3-none-any.whl"], - "data": ["@pypi//bar:whl"] + _select({ + "data": ["@pypi//bar:whl"] + select({ ":is_include_bar_baz_true": ["@pypi//bar_baz:whl"], "//conditions:default": [], }), @@ -245,14 +244,14 @@ def _test_whl_and_library_deps_from_requires(env): env.expect.that_dict(py_library_call).contains_exactly({ "name": "pkg", - "srcs": ["site-packages/foo/SRCS.py"] + _select({ + "srcs": ["site-packages/foo/SRCS.py"] + select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), "pyi_srcs": ["site-packages/foo/PYI.pyi"], "data": ["site-packages/foo/DATA.txt"], "imports": ["site-packages"], - "deps": ["@pypi//bar:pkg"] + _select({ + "deps": ["@pypi//bar:pkg"] + select({ ":is_include_bar_baz_true": ["@pypi//bar_baz:pkg"], "//conditions:default": [], }), @@ -321,7 +320,6 @@ def _test_whl_and_library_deps(env): filegroup = lambda **kwargs: filegroup_calls.append(kwargs), config_setting = lambda **_: None, glob = mock_glob.glob, - select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -336,7 +334,7 @@ def _test_whl_and_library_deps(env): "data": [ "@pypi_bar_baz//:whl", "@pypi_foo//:whl", - ] + _select( + ] + select( { Label("//python/config_settings:is_python_3.9"): ["@pypi_py39_dep//:whl"], "@platforms//cpu:aarch64": ["@pypi_arm_dep//:whl"], @@ -357,7 +355,7 @@ def _test_whl_and_library_deps(env): return env.expect.that_dict(py_library_calls[0]).contains_exactly({ "name": "pkg", - "srcs": ["site-packages/foo/SRCS.py"] + _select({ + "srcs": ["site-packages/foo/SRCS.py"] + select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), @@ -367,7 +365,7 @@ def _test_whl_and_library_deps(env): "deps": [ "@pypi_bar_baz//:pkg", "@pypi_foo//:pkg", - ] + _select( + ] + select( { Label("//python/config_settings:is_python_3.9"): ["@pypi_py39_dep//:pkg"], "@platforms//cpu:aarch64": ["@pypi_arm_dep//:pkg"], @@ -415,7 +413,6 @@ def _test_group(env): config_setting = lambda **_: None, glob = mock_glob.glob, alias = lambda **kwargs: alias_calls.append(kwargs), - select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -437,14 +434,14 @@ def _test_group(env): py_library_call, ).contains_exactly({ "name": "_pkg", - "srcs": ["site-packages/foo/srcs.py"] + _select({ + "srcs": ["site-packages/foo/srcs.py"] + select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), "pyi_srcs": ["site-packages/foo/pyi.pyi"], "data": ["site-packages/foo/data.txt"], "imports": ["site-packages"], - "deps": ["@pypi_bar_baz//:pkg"] + _select({ + "deps": ["@pypi_bar_baz//:pkg"] + select({ "@platforms//os:linux": ["@pypi_box//:pkg"], ":is_linux_x86_64": ["@pypi_box//:pkg", "@pypi_box_amd64//:pkg"], "//conditions:default": [], @@ -491,13 +488,6 @@ def _mock_glob(): ) return mock -def _select(*args, **kwargs): - """We need to have this mock select because we still need to support bazel 6.""" - return [struct( - select = args, - kwargs = kwargs, - )] - def whl_library_targets_test_suite(name): """create the test suite. diff --git a/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl b/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl index aa4d1c793b..add741e300 100644 --- a/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl +++ b/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl @@ -24,7 +24,6 @@ load( "PY_CC_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE", ) # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "CC_TOOLCHAIN") _LookupInfo = provider() # buildifier: disable=provider-params @@ -55,25 +54,10 @@ def _test_runtime_env_toolchain_matches(name): name = name + "_subject", ) extra_toolchains = [ + CC_TOOLCHAIN, str(Label("//python/runtime_env_toolchains:all")), ] - # We have to add a cc toolchain because py_cc toolchain depends on it. - # However, that package also defines a different fake py_cc toolchain we - # don't want to use, so we need to ensure the runtime_env toolchain has - # higher precendence. - # However, Bazel 6 and Bazel 7 process --extra_toolchains in different - # orders: - # * Bazel 6 goes left to right - # * Bazel 7 goes right to left - # We could just put our preferred toolchain before *and* after - # the undesired toolchain... - # However, Bazel 7 has a bug where *duplicate* entries are ignored, - # and only the *first* entry is respected. - if IS_BAZEL_7_OR_HIGHER: - extra_toolchains.insert(0, CC_TOOLCHAIN) - else: - extra_toolchains.append(CC_TOOLCHAIN) analysis_test( name = name, impl = _test_runtime_env_toolchain_matches_impl, diff --git a/tests/support/sh_py_run_test.bzl b/tests/support/sh_py_run_test.bzl index 7dff6673ce..2d26e890a2 100644 --- a/tests/support/sh_py_run_test.bzl +++ b/tests/support/sh_py_run_test.bzl @@ -37,7 +37,7 @@ def sh_py_run_test(*, name, sh_src, py_src, **kwargs): srcs = [sh_src], data = [bin_name], deps = [ - "@bazel_tools//tools/bash/runfiles", + "@rules_shell//shell/runfiles", ], env = { "BIN_RLOCATION": "$(rlocationpaths {})".format(bin_name), diff --git a/tests/support/support.bzl b/tests/support/support.bzl index 37d3488316..96c6ad902a 100644 --- a/tests/support/support.bzl +++ b/tests/support/support.bzl @@ -20,7 +20,6 @@ # places. load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility MAC = Label("//tests/support:mac") MAC_X86_64 = Label("//tests/support:mac_x86_64") @@ -40,7 +39,7 @@ CUSTOM_RUNTIME = str(Label("//tests/support:custom_runtime")) SUPPORTS_BOOTSTRAP_SCRIPT = select({ "@platforms//os:windows": ["@platforms//:incompatible"], "//conditions:default": [], -}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] +}) SUPPORTS_BZLMOD_UNIXY = select({ "@platforms//os:windows": ["@platforms//:incompatible"], diff --git a/tests/support/whl_from_dir/whl_from_dir_repo.bzl b/tests/support/whl_from_dir/whl_from_dir_repo.bzl index 176525636c..4e16e8ee4a 100644 --- a/tests/support/whl_from_dir/whl_from_dir_repo.bzl +++ b/tests/support/whl_from_dir/whl_from_dir_repo.bzl @@ -8,7 +8,7 @@ load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl def _whl_from_dir_repo(rctx): root = rctx.path(rctx.attr.root).dirname - repo_utils.watch_tree(rctx, root) + rctx.watch_tree(root) output = rctx.path(rctx.attr.output) repo_utils.execute_checked( From ee42fbdaaa674a0f810c0efae73e5b1507afbfc9 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:00:12 +0900 Subject: [PATCH 02/19] revert the cleanup to debug the integration tests --- docs/BUILD.bazel | 11 ++- examples/bzlmod/other_module/MODULE.bazel | 18 +++-- python/BUILD.bazel | 6 +- python/private/BUILD.bazel | 4 ++ python/private/envsubst.bzl | 2 +- python/private/internal_config_repo.bzl | 35 ++++++++-- python/private/interpreter.bzl | 2 +- python/private/local_runtime_repo.bzl | 6 +- python/private/py_cc_link_params_info.bzl | 3 +- python/private/py_info.bzl | 3 +- python/private/py_package.bzl | 10 ++- python/private/py_runtime_info.bzl | 4 +- python/private/py_runtime_pair_rule.bzl | 3 +- python/private/py_runtime_rule.bzl | 10 ++- python/private/pypi/pypi_repo_utils.bzl | 5 +- python/private/pypi/whl_library_targets.bzl | 8 ++- python/private/python.bzl | 11 ++- python/private/python_register_toolchains.bzl | 15 ++++ python/private/repo_utils.bzl | 22 +++++- python/private/util.bzl | 32 +++++++++ python/py_runtime.bzl | 7 +- python/py_runtime_pair.bzl | 6 +- .../tests/proto_to_markdown/BUILD.bazel | 2 + sphinxdocs/tests/sphinx_docs/BUILD.bazel | 3 +- sphinxdocs/tests/sphinx_stardoc/BUILD.bazel | 3 +- tests/base_rules/py_executable_base_tests.bzl | 38 +++++++--- tests/base_rules/py_info/py_info_tests.bzl | 2 +- tests/base_rules/py_test/py_test_tests.bzl | 15 ++++ tests/base_rules/util.bzl | 5 ++ tests/bootstrap_impls/BUILD.bazel | 4 +- tests/bootstrap_impls/a/b/c/BUILD.bazel | 3 +- .../transition/multi_version_tests.bzl | 50 +++++++++----- .../exec_toolchain_matching_tests.bzl | 8 ++- tests/integration/BUILD.bazel | 11 +++ tests/integration/integration_test.bzl | 6 ++ tests/integration/runner.py | 3 + tests/py_runtime/py_runtime_tests.bzl | 69 ++++++++++++++----- .../py_runtime_info/py_runtime_info_tests.bzl | 7 +- .../whl_library_targets_tests.bzl | 26 ++++--- .../runtime_env_toolchain_tests.bzl | 18 ++++- tests/support/sh_py_run_test.bzl | 2 +- tests/support/support.bzl | 3 +- .../whl_from_dir/whl_from_dir_repo.bzl | 2 +- 43 files changed, 404 insertions(+), 99 deletions(-) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 1c6f903a1c..b6c48b0539 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -15,6 +15,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@dev_pip//:requirements.bzl", "requirement") load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//python/uv:lock.bzl", "lock") # buildifier: disable=bzl-visibility load("//sphinxdocs:readthedocs.bzl", "readthedocs_install") load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") @@ -106,8 +107,6 @@ sphinx_stardocs( "//python/cc:py_cc_toolchain_bzl", "//python/cc:py_cc_toolchain_info_bzl", "//python/entry_points:py_console_script_binary_bzl", - "//python/extensions:pip_bzl", - "//python/extensions:python_bzl", "//python/local_toolchains:repos_bzl", "//python/private:attr_builders_bzl", "//python/private:builders_util_bzl", @@ -128,7 +127,13 @@ sphinx_stardocs( "//python/uv:uv_bzl", "//python/uv:uv_toolchain_bzl", "//python/uv:uv_toolchain_info_bzl", - ], + ] + ([ + # Bazel 6 + Stardoc isn't able to parse something about the python bzlmod extension + "//python/extensions:python_bzl", + ] if IS_BAZEL_7_OR_HIGHER else []) + ([ + # This depends on @pythons_hub, which is only created under bzlmod, + "//python/extensions:pip_bzl", + ] if IS_BAZEL_7_OR_HIGHER and BZLMOD_ENABLED else []), prefix = "api/rules_python/", tags = ["docs"], target_compatible_with = _TARGET_COMPATIBLE_WITH, diff --git a/examples/bzlmod/other_module/MODULE.bazel b/examples/bzlmod/other_module/MODULE.bazel index 7b88bd73ff..f9d6706120 100644 --- a/examples/bzlmod/other_module/MODULE.bazel +++ b/examples/bzlmod/other_module/MODULE.bazel @@ -5,10 +5,20 @@ module( # This module is using the same version of rules_python # that the parent module uses. bazel_dep(name = "rules_python", version = "") -local_path_override( - module_name = "rules_python", - path = "../../..", -) + +# The story behind this commented out override: +# This override is necessary to generate/update the requirements file +# for this module. This is because running it via the outer +# module doesn't work -- the `requirements.update` target can't find +# the correct file to update. +# Running in the submodule itself works, but submodules using overrides +# is considered an error until Bazel 6.3, which prevents the outer module +# from depending on this module. +# So until 6.3 and higher is the minimum, we leave this commented out. +# local_path_override( +# module_name = "rules_python", +# path = "../../..", +# ) PYTHON_NAME_39 = "python_3_9" diff --git a/python/BUILD.bazel b/python/BUILD.bazel index ef7bf990dc..76fa5dde6e 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -201,7 +201,11 @@ bzl_library( bzl_library( name = "py_runtime_pair_bzl", srcs = ["py_runtime_pair.bzl"], - deps = ["//python/private:py_runtime_pair_macro_bzl"], + deps = [ + "//python/private:bazel_tools_bzl", + "//python/private:py_runtime_pair_macro_bzl", + "//python/private:util_bzl", + ], ) bzl_library( diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index a6a91373d8..0c8ccdea99 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -71,6 +71,7 @@ bzl_library( ":enum_bzl", ":flags_bzl", ":py_info_bzl", + ":py_internal_bzl", ":reexports_bzl", ":rules_cc_srcs_bzl", "@bazel_skylib//rules:common_settings", @@ -341,6 +342,7 @@ bzl_library( srcs = ["py_cc_link_params_info.bzl"], deps = [ ":rules_cc_srcs_bzl", + ":util_bzl", ], ) @@ -438,6 +440,7 @@ bzl_library( deps = [ ":builders_bzl", ":reexports_bzl", + ":util_bzl", "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -503,6 +506,7 @@ bzl_library( bzl_library( name = "py_runtime_info_bzl", srcs = ["py_runtime_info.bzl"], + deps = [":util_bzl"], ) bzl_library( diff --git a/python/private/envsubst.bzl b/python/private/envsubst.bzl index b3f8192cb6..b2fdb99e1e 100644 --- a/python/private/envsubst.bzl +++ b/python/private/envsubst.bzl @@ -22,7 +22,7 @@ def envsubst(template_string, varnames, getenv): listed in the `varnames` list in the environment defined by the `getenv` function. Typically called with `getenv = rctx.getenv` (if it is available) or `getenv = rctx.os.environ.get` (on e.g. - Bazel 7, which don't have `rctx.getenv` yet). + Bazel 6 or Bazel 7, which don't have `rctx.getenv` yet). Limitations: Unlike the shell, we don't support `${VARNAME}` and `${VARNAME:-default}` in the default expression for a different diff --git a/python/private/internal_config_repo.bzl b/python/private/internal_config_repo.bzl index d8a113789a..b57275b672 100644 --- a/python/private/internal_config_repo.bzl +++ b/python/private/internal_config_repo.bzl @@ -23,6 +23,8 @@ load(":repo_utils.bzl", "repo_utils") _ENABLE_PIPSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PIPSTAR" _ENABLE_PIPSTAR_DEFAULT = "0" +_ENABLE_PYSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PYSTAR" +_ENABLE_PYSTAR_DEFAULT = "1" _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS" _ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0" @@ -86,6 +88,15 @@ _TRANSITION_SETTINGS_DEBUG_TEMPLATE = """ """ def _internal_config_repo_impl(rctx): + pystar_requested = _bool_from_environ(rctx, _ENABLE_PYSTAR_ENVVAR_NAME, _ENABLE_PYSTAR_DEFAULT) + + # Bazel 7+ (dev and later) has native.starlark_doc_extract, and thus the + # py_internal global, which are necessary for the pystar implementation. + if pystar_requested and hasattr(native, "starlark_doc_extract"): + enable_pystar = pystar_requested + else: + enable_pystar = False + if not native.bazel_version or int(native.bazel_version.split(".")[0]) >= 8: builtin_py_info_symbol = "None" builtin_py_runtime_info_symbol = "None" @@ -96,7 +107,7 @@ def _internal_config_repo_impl(rctx): builtin_py_cc_link_params_provider = "PyCcLinkParamsProvider" rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format( - enable_pystar = True, + enable_pystar = enable_pystar, enable_pipstar = _bool_from_environ(rctx, _ENABLE_PIPSTAR_ENVVAR_NAME, _ENABLE_PIPSTAR_DEFAULT), enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT), builtin_py_info_symbol = builtin_py_info_symbol, @@ -104,11 +115,25 @@ def _internal_config_repo_impl(rctx): builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider, )) + if enable_pystar: + shim_content = _PY_INTERNAL_SHIM + py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"' + else: + shim_content = "py_internal_impl = None\n" + py_internal_dep = "" + + # Bazel 5 doesn't support repository visibility, so just use public + # as a stand-in + if native.bazel_version.startswith("5."): + visibility = "//visibility:public" + else: + visibility = "@rules_python//:__subpackages__" + rctx.file("BUILD", ROOT_BUILD_TEMPLATE.format( - py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"', - visibility = "@rules_python//:__subpackages__", + py_internal_dep = py_internal_dep, + visibility = visibility, )) - rctx.file("py_internal.bzl", _PY_INTERNAL_SHIM) + rctx.file("py_internal.bzl", shim_content) rctx.file( "extra_transition_settings.bzl", @@ -130,7 +155,7 @@ def _internal_config_repo_impl(rctx): internal_config_repo = repository_rule( implementation = _internal_config_repo_impl, configure = True, - environ = [_ENABLE_PIPSTAR_ENVVAR_NAME], + environ = [_ENABLE_PYSTAR_ENVVAR_NAME], attrs = { "transition_setting_generators": attr.string_list_dict(), "transition_settings": attr.string_list(), diff --git a/python/private/interpreter.bzl b/python/private/interpreter.bzl index ca65262bfc..c66d3dc21e 100644 --- a/python/private/interpreter.bzl +++ b/python/private/interpreter.bzl @@ -72,7 +72,7 @@ interpreter_binary = rule( mandatory = True, ), "_bash_runfiles": attr.label( - default = "@rules_shell//shell/runfiles", + default = "@bazel_tools//tools/bash/runfiles", ), "_template": attr.label( default = "//python/private:interpreter_tmpl.sh", diff --git a/python/private/local_runtime_repo.bzl b/python/private/local_runtime_repo.bzl index 27c90b1bc9..c053a03508 100644 --- a/python/private/local_runtime_repo.bzl +++ b/python/private/local_runtime_repo.bzl @@ -71,7 +71,7 @@ def _symlink_first_library(rctx, logger, libraries): else: linked = "lib/{}".format(origin.basename) logger.debug("Symlinking {} to {}".format(origin, linked)) - rctx.watch(origin) + repo_utils.watch(rctx, origin) rctx.symlink(origin, linked) break @@ -142,7 +142,7 @@ def _local_runtime_repo_impl(rctx): # path is an error. Silently skip, since includes are only necessary # if C extensions are built. if include_path.exists and include_path.is_dir: - rctx.watch_tree(include_path) + repo_utils.watch_tree(rctx, include_path) else: pass @@ -268,7 +268,7 @@ def _resolve_interpreter_path(rctx): resolved_path = result.binary describe_failure = result.describe_failure else: - rctx.watch(rctx.attr.interpreter_path) + repo_utils.watch(rctx, rctx.attr.interpreter_path) resolved_path = rctx.path(rctx.attr.interpreter_path) if not resolved_path.exists: describe_failure = lambda: "Path not found: {}".format(repr(rctx.attr.interpreter_path)) diff --git a/python/private/py_cc_link_params_info.bzl b/python/private/py_cc_link_params_info.bzl index 2fbd255eed..35919a04e2 100644 --- a/python/private/py_cc_link_params_info.bzl +++ b/python/private/py_cc_link_params_info.bzl @@ -14,6 +14,7 @@ """Providers for Python rules.""" load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") +load(":util.bzl", "define_bazel_6_provider") def _PyCcLinkParamsInfo_init(cc_info): return { @@ -21,7 +22,7 @@ def _PyCcLinkParamsInfo_init(cc_info): } # buildifier: disable=name-conventions -PyCcLinkParamsInfo, _unused_raw_py_cc_link_params_provider_ctor = provider( +PyCcLinkParamsInfo, _unused_raw_py_cc_link_params_provider_ctor = define_bazel_6_provider( doc = ("Python-wrapper to forward {obj}`CcInfo.linking_context`. This is to " + "allow Python targets to propagate C++ linking information, but " + "without the Python target appearing to be a valid C++ rule dependency"), diff --git a/python/private/py_info.bzl b/python/private/py_info.bzl index 18a1015761..f96dec554b 100644 --- a/python/private/py_info.bzl +++ b/python/private/py_info.bzl @@ -16,6 +16,7 @@ load("@rules_python_internal//:rules_python_config.bzl", "config") load(":builders.bzl", "builders") load(":reexports.bzl", "BuiltinPyInfo") +load(":util.bzl", "define_bazel_6_provider") def _VenvSymlinkKind_typedef(): """An enum of types of venv directories. @@ -159,7 +160,7 @@ def _PyInfo_init( "venv_symlinks": venv_symlinks, } -PyInfo, _unused_raw_py_info_ctor = provider( +PyInfo, _unused_raw_py_info_ctor = define_bazel_6_provider( doc = """Encapsulates information provided by the Python rules. Instead of creating this object directly, use {obj}`PyInfoBuilder` and diff --git a/python/private/py_package.bzl b/python/private/py_package.bzl index 2d19ca7437..adf2b6deef 100644 --- a/python/private/py_package.bzl +++ b/python/private/py_package.bzl @@ -41,8 +41,14 @@ def _py_package_impl(ctx): py_info.merge_target(dep) py_info = py_info.build() inputs.add(py_info.transitive_sources) - inputs.add(py_info.transitive_pyc_files) - inputs.add(py_info.transitive_pyi_files) + + # Remove conditional once Bazel 6 support dropped. + if hasattr(py_info, "transitive_pyc_files"): + inputs.add(py_info.transitive_pyc_files) + + if hasattr(py_info, "transitive_pyi_files"): + inputs.add(py_info.transitive_pyi_files) + inputs = inputs.build() # TODO: '/' is wrong on windows, but the path separator is not available in starlark. diff --git a/python/private/py_runtime_info.bzl b/python/private/py_runtime_info.bzl index af4e7f0596..efe14b2c06 100644 --- a/python/private/py_runtime_info.bzl +++ b/python/private/py_runtime_info.bzl @@ -13,6 +13,8 @@ # limitations under the License. """Providers for Python rules.""" +load(":util.bzl", "define_bazel_6_provider") + DEFAULT_STUB_SHEBANG = "#!/usr/bin/env python3" _PYTHON_VERSION_VALUES = ["PY2", "PY3"] @@ -122,7 +124,7 @@ def _PyRuntimeInfo_init( "zip_main_template": zip_main_template, } -PyRuntimeInfo, _unused_raw_py_runtime_info_ctor = provider( +PyRuntimeInfo, _unused_raw_py_runtime_info_ctor = define_bazel_6_provider( doc = """Contains information about a Python runtime, as returned by the `py_runtime` rule. diff --git a/python/private/py_runtime_pair_rule.bzl b/python/private/py_runtime_pair_rule.bzl index 203e5d4df7..61cbdcd6f4 100644 --- a/python/private/py_runtime_pair_rule.bzl +++ b/python/private/py_runtime_pair_rule.bzl @@ -19,6 +19,7 @@ load("//python:py_runtime_info.bzl", "PyRuntimeInfo") load(":common_labels.bzl", "labels") load(":flags.bzl", "read_possibly_native_flag") load(":reexports.bzl", "BuiltinPyRuntimeInfo") +load(":util.bzl", "IS_BAZEL_7_OR_HIGHER") def _py_runtime_pair_impl(ctx): if ctx.attr.py2_runtime != None: @@ -57,7 +58,7 @@ def _get_py_runtime_info(target): # 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 (PyRuntimeInfo in target) or BuiltinPyRuntimeInfo == None: + if (IS_BAZEL_7_OR_HIGHER and PyRuntimeInfo in target) or BuiltinPyRuntimeInfo == None: return target[PyRuntimeInfo] else: return target[BuiltinPyRuntimeInfo] diff --git a/python/private/py_runtime_rule.bzl b/python/private/py_runtime_rule.bzl index 5020d7ad9b..ba1a390ef3 100644 --- a/python/private/py_runtime_rule.bzl +++ b/python/private/py_runtime_rule.bzl @@ -21,6 +21,7 @@ load(":flags.bzl", "FreeThreadedFlag") load(":py_internal.bzl", "py_internal") load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo") load(":reexports.bzl", "BuiltinPyRuntimeInfo") +load(":util.bzl", "IS_BAZEL_7_OR_HIGHER") _py_builtins = py_internal @@ -132,6 +133,9 @@ def _py_runtime_impl(ctx): supports_build_time_venv = ctx.attr.supports_build_time_venv, )) + if not IS_BAZEL_7_OR_HIGHER: + builtin_py_runtime_info_kwargs.pop("bootstrap_template") + providers = [ PyRuntimeInfo(**py_runtime_info_kwargs), DefaultInfo( @@ -384,7 +388,11 @@ The {obj}`PyRuntimeInfo.zip_main_template` field. ) def _is_singleton_depset(files): - return _py_builtins.is_singleton_depset(files) + # Bazel 6 doesn't have this helper to optimize detecting singleton depsets. + if _py_builtins: + return _py_builtins.is_singleton_depset(files) + else: + return len(files.to_list()) == 1 def _interpreter_version_info_from_version_str(version_str): parts = version_str.split(".") diff --git a/python/private/pypi/pypi_repo_utils.bzl b/python/private/pypi/pypi_repo_utils.bzl index 04c9b5d685..bb2acc850a 100644 --- a/python/private/pypi/pypi_repo_utils.bzl +++ b/python/private/pypi/pypi_repo_utils.bzl @@ -107,8 +107,9 @@ def _construct_pypath(mrctx, *, entries): def _execute_prep(mrctx, *, python, srcs, **kwargs): for src in srcs: # This will ensure that we will re-evaluate the bzlmod extension or - # refetch the repository_rule when the srcs change. - mrctx.watch(mrctx.path(src)) + # refetch the repository_rule when the srcs change. This should work on + # Bazel versions without `mrctx.watch` as well. + repo_utils.watch(mrctx, mrctx.path(src)) environment = kwargs.pop("environment", {}) pythonpath = environment.get("PYTHONPATH", "") diff --git a/python/private/pypi/whl_library_targets.bzl b/python/private/pypi/whl_library_targets.bzl index b19be5d465..aed5bc74f5 100644 --- a/python/private/pypi/whl_library_targets.bzl +++ b/python/private/pypi/whl_library_targets.bzl @@ -315,7 +315,13 @@ def whl_library_targets( deps_by_platform = dependencies_by_platform, deps_conditional = deps_conditional, tmpl = dep_template.format(name = "{}", target = WHEEL_FILE_PUBLIC_LABEL), - select = select, + # NOTE @aignas 2024-10-28: Actually, `select` is not part of + # `native`, but in order to support bazel 6.4 in unit tests, I + # have to somehow pass the `select` implementation in the unit + # tests and I chose this to be routed through the `native` + # struct. So, tests` will be successful in `getattr` and the + # real code will use the fallback provided here. + select = getattr(native, "select", select), ), visibility = impl_vis, ) diff --git a/python/private/python.bzl b/python/private/python.bzl index faad53fab4..6eb8a3742e 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -29,6 +29,7 @@ load( "sorted_host_platform_names", "sorted_host_platforms", ) +load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER") load(":version.bzl", "version") def parse_modules(*, module_ctx, logger, _fail = fail): @@ -931,6 +932,14 @@ def _create_toolchain_attrs_struct(*, tag = None, python_version = None, toolcha ignore_root_user_error = getattr(tag, "ignore_root_user_error", True), ) +def _get_bazel_version_specific_kwargs(): + kwargs = {} + + if IS_BAZEL_6_4_OR_HIGHER: + kwargs["environ"] = ["RULES_PYTHON_BZLMOD_DEBUG"] + + return kwargs + _defaults = tag_class( doc = """Tag class to specify the default Python version.""", attrs = { @@ -1347,7 +1356,7 @@ python = module_extension( "single_version_platform_override": _single_version_platform_override, "toolchain": _toolchain, }, - environ = ["RULES_PYTHON_BZLMOD_DEBUG"], + **_get_bazel_version_specific_kwargs() ) _DEBUG_BUILD_CONTENT = """ diff --git a/python/private/python_register_toolchains.bzl b/python/private/python_register_toolchains.bzl index 9e75c41978..2e0748deb0 100644 --- a/python/private/python_register_toolchains.bzl +++ b/python/private/python_register_toolchains.bzl @@ -97,6 +97,21 @@ def python_register_toolchains( toolchain_repo_name = "{name}_toolchains".format(name = name) + # When using unreleased Bazel versions, the version is an empty string + if native.bazel_version: + bazel_major = int(native.bazel_version.split(".")[0]) + if bazel_major < 6: + if register_coverage_tool: + # buildifier: disable=print + print(( + "WARNING: ignoring register_coverage_tool=True when " + + "registering @{name}: Bazel 6+ required, got {version}" + ).format( + name = name, + version = native.bazel_version, + )) + register_coverage_tool = False + # list[str] of the platform names that were used loaded_platforms = [] diff --git a/python/private/repo_utils.bzl b/python/private/repo_utils.bzl index 77eac55c16..32a5b70e15 100644 --- a/python/private/repo_utils.bzl +++ b/python/private/repo_utils.bzl @@ -291,7 +291,7 @@ def _which_unchecked(mrctx, binary_name): """ binary = mrctx.which(binary_name) if binary: - mrctx.watch(binary) + _watch(mrctx, binary) describe_failure = None else: path = _getenv(mrctx, "PATH", "") @@ -429,6 +429,24 @@ def _get_platforms_cpu_name(mrctx): return "riscv64" return arch +# TODO: Remove after Bazel 6 support dropped +def _watch(mrctx, *args, **kwargs): + """Calls mrctx.watch, if available.""" + if not args and not kwargs: + fail("'watch' needs at least a single argument.") + + if hasattr(mrctx, "watch"): + mrctx.watch(*args, **kwargs) + +# TODO: Remove after Bazel 6 support dropped +def _watch_tree(mrctx, *args, **kwargs): + """Calls mrctx.watch_tree, if available.""" + if not args and not kwargs: + fail("'watch_tree' needs at least a single argument.") + + if hasattr(mrctx, "watch_tree"): + mrctx.watch_tree(*args, **kwargs) + repo_utils = struct( # keep sorted execute_checked = _execute_checked, @@ -439,6 +457,8 @@ repo_utils = struct( getenv = _getenv, is_repo_debug_enabled = _is_repo_debug_enabled, logger = _logger, + watch = _watch, + watch_tree = _watch_tree, which_checked = _which_checked, which_unchecked = _which_unchecked, ) diff --git a/python/private/util.bzl b/python/private/util.bzl index 5a180e5711..4d2da57760 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -86,4 +86,36 @@ def add_tag(attrs, tag): else: attrs["tags"] = [tag] +# Helper to make the provider definitions not crash under Bazel 5.4: +# Bazel 5.4 doesn't support the `init` arg of `provider()`, so we have to +# not pass that when using Bazel 5.4. But, not passing the `init` arg +# changes the return value from a two-tuple to a single value, which then +# breaks Bazel 6+ code. +# This isn't actually used under Bazel 5.4, so just stub out the values +# to get past the loading phase. +def define_bazel_6_provider(doc, fields, **kwargs): + """Define a provider, or a stub for pre-Bazel 7.""" + if not IS_BAZEL_6_OR_HIGHER: + return provider("Stub, not used", fields = []), None + return provider(doc = doc, fields = fields, **kwargs) + IS_BAZEL_7_4_OR_HIGHER = hasattr(native, "legacy_globals") + +IS_BAZEL_7_OR_HIGHER = hasattr(native, "starlark_doc_extract") + +# Bazel 5.4 has a bug where every access of testing.ExecutionInfo is a +# different object that isn't equal to any other. This is fixed in bazel 6+. +IS_BAZEL_6_OR_HIGHER = testing.ExecutionInfo == testing.ExecutionInfo + +_marker_rule_to_detect_bazel_6_4_or_higher = rule(implementation = lambda ctx: None) + +# Bazel 6.4 and higher have a bug fix where rule names show up in the str() +# of a rule. See +# https://github.com/bazelbuild/bazel/commit/002490b9a2376f0b2ea4a37102c5e94fc50a65ba +# https://github.com/bazelbuild/bazel/commit/443cbcb641e17f7337ccfdecdfa5e69bc16cae55 +# This technique is done instead of using native.bazel_version because, +# under stardoc, the native.bazel_version attribute is entirely missing, which +# prevents doc generation from being able to correctly generate docs. +IS_BAZEL_6_4_OR_HIGHER = "_marker_rule_to_detect_bazel_6_4_or_higher" in str( + _marker_rule_to_detect_bazel_6_4_or_higher, +) diff --git a/python/py_runtime.bzl b/python/py_runtime.bzl index 3cd9a8efac..dad2965cf5 100644 --- a/python/py_runtime.bzl +++ b/python/py_runtime.bzl @@ -14,8 +14,11 @@ """Public entry point for py_runtime.""" -load("//python/private:py_runtime_macro.bzl", _py_runtime_impl = "py_runtime") -load("//python/private:util.bzl", "add_migration_tag") +load("//python/private:py_runtime_macro.bzl", _starlark_py_runtime = "py_runtime") +load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER", "add_migration_tag") + +# buildifier: disable=native-python +_py_runtime_impl = _starlark_py_runtime if IS_BAZEL_6_OR_HIGHER else native.py_runtime def py_runtime(**attrs): """Creates an executable Python program. diff --git a/python/py_runtime_pair.bzl b/python/py_runtime_pair.bzl index f44c722d54..26d378fce2 100644 --- a/python/py_runtime_pair.bzl +++ b/python/py_runtime_pair.bzl @@ -14,7 +14,11 @@ """Public entry point for py_runtime_pair.""" -load("//python/private:py_runtime_pair_macro.bzl", _py_runtime_pair = "py_runtime_pair") +load("@bazel_tools//tools/python:toolchain.bzl", _bazel_tools_impl = "py_runtime_pair") +load("//python/private:py_runtime_pair_macro.bzl", _starlark_impl = "py_runtime_pair") +load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") + +_py_runtime_pair = _starlark_impl if IS_BAZEL_6_OR_HIGHER else _bazel_tools_impl # NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our # doc generator gives useful API docs. diff --git a/sphinxdocs/tests/proto_to_markdown/BUILD.bazel b/sphinxdocs/tests/proto_to_markdown/BUILD.bazel index 2964785eed..09f537472c 100644 --- a/sphinxdocs/tests/proto_to_markdown/BUILD.bazel +++ b/sphinxdocs/tests/proto_to_markdown/BUILD.bazel @@ -13,10 +13,12 @@ # limitations under the License. load("//python:py_test.bzl", "py_test") +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility py_test( name = "proto_to_markdown_test", srcs = ["proto_to_markdown_test.py"], + target_compatible_with = [] if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"], deps = [ "//sphinxdocs/private:proto_to_markdown_lib", "@dev_pip//absl_py", diff --git a/sphinxdocs/tests/sphinx_docs/BUILD.bazel b/sphinxdocs/tests/sphinx_docs/BUILD.bazel index 33b98ec585..f9c82967c1 100644 --- a/sphinxdocs/tests/sphinx_docs/BUILD.bazel +++ b/sphinxdocs/tests/sphinx_docs/BUILD.bazel @@ -1,4 +1,5 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") load(":defs.bzl", "gen_directory") @@ -12,7 +13,7 @@ _TARGET_COMPATIBLE_WITH = select({ "@platforms//os:linux": [], "@platforms//os:macos": [], "//conditions:default": ["@platforms//:incompatible"], -}) +}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] sphinx_docs( name = "docs", diff --git a/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel b/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel index af9af30886..e3a68ea225 100644 --- a/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel +++ b/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel @@ -1,6 +1,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//python:py_test.bzl", "py_test") +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") load("//sphinxdocs:sphinx_stardoc.bzl", "sphinx_stardoc", "sphinx_stardocs") @@ -14,7 +15,7 @@ _TARGET_COMPATIBLE_WITH = select({ "@platforms//os:linux": [], "@platforms//os:macos": [], "//conditions:default": ["@platforms//:incompatible"], -}) +}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] sphinx_docs( name = "docs", diff --git a/tests/base_rules/py_executable_base_tests.bzl b/tests/base_rules/py_executable_base_tests.bzl index a3e4ca1240..4e451289dc 100644 --- a/tests/base_rules/py_executable_base_tests.bzl +++ b/tests/base_rules/py_executable_base_tests.bzl @@ -21,6 +21,7 @@ load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:py_executable_info.bzl", "PyExecutableInfo") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") # buildifier: disable=bzl-visibility +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/base_rules:base_tests.bzl", "create_base_tests") load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util") load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject") @@ -187,6 +188,11 @@ def _test_default_main_can_have_multiple_path_segments_impl(env, target): ) def _test_default_main_must_be_in_srcs(name, config): + # Bazel 5 will crash with a Java stacktrace when the native Python + # rules have an error. + if not pt_util.is_bazel_6_or_higher(): + rt_util.skip_test(name = name) + return rt_util.helper_target( config.rule, name = name + "_subject", @@ -207,6 +213,11 @@ def _test_default_main_must_be_in_srcs_impl(env, target): ) def _test_default_main_cannot_be_ambiguous(name, config): + # Bazel 5 will crash with a Java stacktrace when the native Python + # rules have an error. + if not pt_util.is_bazel_6_or_higher(): + rt_util.skip_test(name = name) + return rt_util.helper_target( config.rule, name = name + "_subject", @@ -249,6 +260,11 @@ def _test_explicit_main_impl(env, target): ) def _test_explicit_main_cannot_be_ambiguous(name, config): + # Bazel 5 will crash with a Java stacktrace when the native Python + # rules have an error. + if not pt_util.is_bazel_6_or_higher(): + rt_util.skip_test(name = name) + return rt_util.helper_target( config.rule, name = name + "_subject", @@ -294,16 +310,22 @@ def _test_files_to_build_impl(env, target): "{package}/{test_name}_subject.py", ]) - # As of Bazel 7, the first default output is the executable, so - # verify that is the case. rules_testing - # DepsetFileSubject.contains_exactly doesn't provide an in_order() - # call, nor access to the underlying depset, so we have to do things - # manually. - first_default_output = target[DefaultInfo].files.to_list()[0] - executable = target[DefaultInfo].files_to_run.executable - env.expect.that_file(first_default_output).equals(executable) + if IS_BAZEL_7_OR_HIGHER: + # As of Bazel 7, the first default output is the executable, so + # verify that is the case. rules_testing + # DepsetFileSubject.contains_exactly doesn't provide an in_order() + # call, nor access to the underlying depset, so we have to do things + # manually. + first_default_output = target[DefaultInfo].files.to_list()[0] + executable = target[DefaultInfo].files_to_run.executable + env.expect.that_file(first_default_output).equals(executable) def _test_name_cannot_end_in_py(name, config): + # Bazel 5 will crash with a Java stacktrace when the native Python + # rules have an error. + if not pt_util.is_bazel_6_or_higher(): + rt_util.skip_test(name = name) + return rt_util.helper_target( config.rule, name = name + "_subject.py", diff --git a/tests/base_rules/py_info/py_info_tests.bzl b/tests/base_rules/py_info/py_info_tests.bzl index 52c483c417..aa252a2937 100644 --- a/tests/base_rules/py_info/py_info_tests.bzl +++ b/tests/base_rules/py_info/py_info_tests.bzl @@ -42,7 +42,7 @@ def _provide_py_info_impl(ctx): if config.enable_pystar: providers.append(PyInfo(**kwargs)) - # Handle if Bazel autoloading is enabled + # Handle Bazel 6 or if Bazel autoloading is enabled if not config.enable_pystar or (BuiltinPyInfo and PyInfo != BuiltinPyInfo): providers.append(BuiltinPyInfo(**{ k: kwargs[k] diff --git a/tests/base_rules/py_test/py_test_tests.bzl b/tests/base_rules/py_test/py_test_tests.bzl index c28eec4346..1ec1dc428f 100644 --- a/tests/base_rules/py_test/py_test_tests.bzl +++ b/tests/base_rules/py_test/py_test_tests.bzl @@ -39,6 +39,14 @@ _SKIP_WINDOWS = { _tests = [] def _test_mac_requires_darwin_for_execution(name, config): + # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is + # a different object that isn't equal to any other, which prevents + # rules_testing from detecting it properly and fails with an error. + # This is fixed in Bazel 6+. + if not pt_util.is_bazel_6_or_higher(): + rt_util.skip_test(name = name) + return + rt_util.helper_target( config.rule, name = name + "_subject", @@ -66,6 +74,13 @@ def _test_mac_requires_darwin_for_execution_impl(env, target): _tests.append(_test_mac_requires_darwin_for_execution) def _test_non_mac_doesnt_require_darwin_for_execution(name, config): + # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is + # a different object that isn't equal to any other, which prevents + # rules_testing from detecting it properly and fails with an error. + # This is fixed in Bazel 6+. + if not pt_util.is_bazel_6_or_higher(): + rt_util.skip_test(name = name) + return rt_util.helper_target( config.rule, name = name + "_subject", diff --git a/tests/base_rules/util.bzl b/tests/base_rules/util.bzl index 9fb66d7eb3..a02cafa992 100644 --- a/tests/base_rules/util.bzl +++ b/tests/base_rules/util.bzl @@ -14,6 +14,7 @@ """Helpers and utilities multiple tests re-use.""" load("@bazel_skylib//lib:structs.bzl", "structs") +load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") # buildifier: disable=bzl-visibility # Use this with is_windows() WINDOWS_ATTR = {"windows": attr.label(default = "@platforms//os:windows")} @@ -52,6 +53,9 @@ def _struct_with(s, **kwargs): struct_dict.update(kwargs) return struct(**struct_dict) +def _is_bazel_6_or_higher(): + return IS_BAZEL_6_OR_HIGHER + def _is_windows(env): """Tell if the target platform is windows. @@ -68,5 +72,6 @@ def _is_windows(env): util = struct( create_tests = _create_tests, struct_with = _struct_with, + is_bazel_6_or_higher = _is_bazel_6_or_higher, is_windows = _is_windows, ) diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel index 245bf0cc4a..c3d44df240 100644 --- a/tests/bootstrap_impls/BUILD.bazel +++ b/tests/bootstrap_impls/BUILD.bazel @@ -43,7 +43,9 @@ sh_test( "ZIP_RLOCATION": "$(rlocationpaths :bootstrap_script_zipapp_zip)".format(), }, target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT, - deps = ["@rules_shell//shell/runfiles"], + deps = [ + "@bazel_tools//tools/bash/runfiles", + ], ) sh_py_run_test( diff --git a/tests/bootstrap_impls/a/b/c/BUILD.bazel b/tests/bootstrap_impls/a/b/c/BUILD.bazel index 1c4b1e7b6b..1659ef25bc 100644 --- a/tests/bootstrap_impls/a/b/c/BUILD.bazel +++ b/tests/bootstrap_impls/a/b/c/BUILD.bazel @@ -1,9 +1,10 @@ +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:py_reconfig.bzl", "py_reconfig_test") _SUPPORTS_BOOTSTRAP_SCRIPT = select({ "@platforms//os:windows": ["@platforms//:incompatible"], "//conditions:default": [], -}) +}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] py_reconfig_test( name = "nested_dir_test", diff --git a/tests/config_settings/transition/multi_version_tests.bzl b/tests/config_settings/transition/multi_version_tests.bzl index 2f9e28a0b3..93f6efd728 100644 --- a/tests/config_settings/transition/multi_version_tests.bzl +++ b/tests/config_settings/transition/multi_version_tests.bzl @@ -16,11 +16,12 @@ load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION") load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") -load("@rules_testing//lib:util.bzl", rt_util = "util") +load("@rules_testing//lib:util.bzl", "TestingAspectInfo", rt_util = "util") load("//python:py_binary.bzl", "py_binary") load("//python:py_info.bzl", "PyInfo") load("//python:py_test.bzl", "py_test") load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "CC_TOOLCHAIN") # NOTE @aignas 2024-06-04: we are using here something that is registered in the MODULE.Bazel @@ -104,14 +105,21 @@ def _test_py_binary_windows_build_python_zip_false(name): ) def _test_py_binary_windows_build_python_zip_false_impl(env, target): - # TODO: These outputs aren't correct. The outputs shouldn't - # have the "_" prefix on them (those are coming from the underlying - # wrapped binary). - env.expect.that_target(target).default_outputs().contains_exactly([ - "{package}/{test_name}_subject.exe", - "{package}/{test_name}_subject", - "{package}/{test_name}_subject.py", - ]) + default_outputs = env.expect.that_target(target).default_outputs() + if IS_BAZEL_7_OR_HIGHER: + # TODO: These outputs aren't correct. The outputs shouldn't + # have the "_" prefix on them (those are coming from the underlying + # wrapped binary). + env.expect.that_target(target).default_outputs().contains_exactly([ + "{package}/{test_name}_subject.exe", + "{package}/{test_name}_subject", + "{package}/{test_name}_subject.py", + ]) + else: + inner_exe = target[TestingAspectInfo].attrs.target[DefaultInfo].files_to_run.executable + default_outputs.contains_at_least([ + inner_exe.short_path, + ]) _tests.append(_test_py_binary_windows_build_python_zip_false) @@ -124,15 +132,21 @@ def _test_py_binary_windows_build_python_zip_true(name): def _test_py_binary_windows_build_python_zip_true_impl(env, target): default_outputs = env.expect.that_target(target).default_outputs() - - # TODO: These outputs aren't correct. The outputs shouldn't - # have the "_" prefix on them (those are coming from the underlying - # wrapped binary). - default_outputs.contains_exactly([ - "{package}/{test_name}_subject.exe", - "{package}/{test_name}_subject.py", - "{package}/{test_name}_subject.zip", - ]) + if IS_BAZEL_7_OR_HIGHER: + # TODO: These outputs aren't correct. The outputs shouldn't + # have the "_" prefix on them (those are coming from the underlying + # wrapped binary). + default_outputs.contains_exactly([ + "{package}/{test_name}_subject.exe", + "{package}/{test_name}_subject.py", + "{package}/{test_name}_subject.zip", + ]) + else: + inner_exe = target[TestingAspectInfo].attrs.target[DefaultInfo].files_to_run.executable + default_outputs.contains_at_least([ + "{package}/{test_name}_subject.zip", + inner_exe.short_path, + ]) _tests.append(_test_py_binary_windows_build_python_zip_true) diff --git a/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl b/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl index b3ff294b6f..43a9717314 100644 --- a/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl +++ b/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl @@ -20,6 +20,7 @@ load("//python:py_runtime.bzl", "py_runtime") load("//python:py_runtime_pair.bzl", "py_runtime_pair") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "LINUX", "MAC") _LookupInfo = provider() # buildifier: disable=provider-params @@ -142,10 +143,11 @@ def _test_exec_matches_target_python_version_impl(env, target): env.expect.that_str(target_runtime.interpreter_path).equals("/linux/python3.12") env.expect.that_str(exec_runtime.interpreter_path).equals("/mac/python3.12") - target_version = target_runtime.interpreter_version_info - exec_version = exec_runtime.interpreter_version_info + if IS_BAZEL_7_OR_HIGHER: + target_version = target_runtime.interpreter_version_info + exec_version = exec_runtime.interpreter_version_info - env.expect.that_bool(target_version == exec_version) + env.expect.that_bool(target_version == exec_version) def exec_toolchain_matching_test_suite(name): test_suite(name = name, tests = _tests) diff --git a/tests/integration/BUILD.bazel b/tests/integration/BUILD.bazel index fa8f96b61d..df7fe15444 100644 --- a/tests/integration/BUILD.bazel +++ b/tests/integration/BUILD.bazel @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("@bazel_binaries//:defs.bzl", "bazel_binaries") load("@rules_bazel_integration_test//bazel_integration_test:defs.bzl", "default_test_runner") load("//python:py_library.bzl", "py_library") load(":integration_test.bzl", "rules_python_integration_test") @@ -87,10 +88,20 @@ rules_python_integration_test( rules_python_integration_test( name = "local_toolchains_test", + bazel_versions = [ + version + for version in bazel_binaries.versions.all + if not version.startswith("6.") + ], ) rules_python_integration_test( name = "local_toolchains_workspace_test", + bazel_versions = [ + version + for version in bazel_binaries.versions.all + if not version.startswith("6.") + ], bzlmod = False, workspace_path = "local_toolchains", ) diff --git a/tests/integration/integration_test.bzl b/tests/integration/integration_test.bzl index 90cc4a3fb7..c437953319 100644 --- a/tests/integration/integration_test.bzl +++ b/tests/integration/integration_test.bzl @@ -35,6 +35,12 @@ def _test_runner(*, name, bazel_version, py_main, bzlmod, gazelle_plugin): ) return test_runner + if bazel_version.startswith("6") and not bzlmod: + if gazelle_plugin: + return "//tests/integration:bazel_6_4_workspace_test_runner_gazelle_plugin" + else: + return "//tests/integration:bazel_6_4_workspace_test_runner" + if bzlmod and gazelle_plugin: return "//tests/integration:test_runner_gazelle_plugin" elif bzlmod: diff --git a/tests/integration/runner.py b/tests/integration/runner.py index caefd10f12..2534ab2d90 100644 --- a/tests/integration/runner.py +++ b/tests/integration/runner.py @@ -84,6 +84,9 @@ def setUp(self): "PATH": os.environ["PATH"], "TEST_TMPDIR": str(self.test_tmp_dir), "TMP": str(self.tmp_dir), + # For some reason, this is necessary for Bazel 6.4 to work. + # If not present, it can't find some bash helpers in @bazel_tools + "RUNFILES_DIR": os.environ["TEST_SRCDIR"], } def run_bazel(self, *args: str, check: bool = True) -> ExecuteResult: diff --git a/tests/py_runtime/py_runtime_tests.bzl b/tests/py_runtime/py_runtime_tests.bzl index 051b4495a2..4ec7590ab2 100644 --- a/tests/py_runtime/py_runtime_tests.bzl +++ b/tests/py_runtime/py_runtime_tests.bzl @@ -21,6 +21,7 @@ load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:py_runtime.bzl", "py_runtime") load("//python:py_runtime_info.bzl", "PyRuntimeInfo") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility +load("//tests/base_rules:util.bzl", br_util = "util") load("//tests/support:py_runtime_info_subject.bzl", "py_runtime_info_subject") _tests = [] @@ -80,11 +81,17 @@ def _test_bootstrap_template_impl(env, target): _tests.append(_test_bootstrap_template) def _test_cannot_have_both_inbuild_and_system_interpreter(name): - py_runtime_kwargs = { - "interpreter": "fake_interpreter", - "interpreter_path": "/some/path", - } - attr_values = {} + if br_util.is_bazel_6_or_higher(): + py_runtime_kwargs = { + "interpreter": "fake_interpreter", + "interpreter_path": "/some/path", + } + attr_values = {} + else: + py_runtime_kwargs = { + "interpreter_path": "/some/path", + } + attr_values = _SKIP_TEST rt_util.helper_target( py_runtime, name = name + "_subject", @@ -107,8 +114,12 @@ def _test_cannot_have_both_inbuild_and_system_interpreter_impl(env, target): _tests.append(_test_cannot_have_both_inbuild_and_system_interpreter) def _test_cannot_specify_files_for_system_interpreter(name): - py_runtime_kwargs = {"files": ["foo.txt"]} - attr_values = {} + if br_util.is_bazel_6_or_higher(): + py_runtime_kwargs = {"files": ["foo.txt"]} + attr_values = {} + else: + py_runtime_kwargs = {} + attr_values = _SKIP_TEST rt_util.helper_target( py_runtime, name = name + "_subject", @@ -132,10 +143,14 @@ def _test_cannot_specify_files_for_system_interpreter_impl(env, target): _tests.append(_test_cannot_specify_files_for_system_interpreter) def _test_coverage_tool_executable(name): - py_runtime_kwargs = { - "coverage_tool": name + "_coverage_tool", - } - attr_values = {} + if br_util.is_bazel_6_or_higher(): + py_runtime_kwargs = { + "coverage_tool": name + "_coverage_tool", + } + attr_values = {} + else: + py_runtime_kwargs = {} + attr_values = _SKIP_TEST rt_util.helper_target( py_runtime, @@ -168,10 +183,14 @@ def _test_coverage_tool_executable_impl(env, target): _tests.append(_test_coverage_tool_executable) def _test_coverage_tool_plain_files(name): - py_runtime_kwargs = { - "coverage_tool": name + "_coverage_tool", - } - attr_values = {} + if br_util.is_bazel_6_or_higher(): + py_runtime_kwargs = { + "coverage_tool": name + "_coverage_tool", + } + attr_values = {} + else: + py_runtime_kwargs = {} + attr_values = _SKIP_TEST rt_util.helper_target( py_runtime, name = name + "_subject", @@ -315,8 +334,14 @@ def _test_interpreter_binary_with_single_output_and_runfiles_impl(env, target): _tests.append(_test_interpreter_binary_with_single_output_and_runfiles) def _test_must_have_either_inbuild_or_system_interpreter(name): - py_runtime_kwargs = {} - attr_values = {} + if br_util.is_bazel_6_or_higher(): + py_runtime_kwargs = {} + attr_values = {} + else: + py_runtime_kwargs = { + "interpreter_path": "/some/path", + } + attr_values = _SKIP_TEST rt_util.helper_target( py_runtime, name = name + "_subject", @@ -360,8 +385,14 @@ def _test_system_interpreter_impl(env, target): _tests.append(_test_system_interpreter) def _test_system_interpreter_must_be_absolute(name): - py_runtime_kwargs = {"interpreter_path": "relative/path"} - attr_values = {} + # Bazel 5.4 will entirely crash when an invalid interpreter_path + # is given. + if br_util.is_bazel_6_or_higher(): + py_runtime_kwargs = {"interpreter_path": "relative/path"} + attr_values = {} + else: + py_runtime_kwargs = {"interpreter_path": "/junk/value/for/bazel5.4"} + attr_values = _SKIP_TEST rt_util.helper_target( py_runtime, name = name + "_subject", diff --git a/tests/py_runtime_info/py_runtime_info_tests.bzl b/tests/py_runtime_info/py_runtime_info_tests.bzl index a44fb60c2f..9acf541683 100644 --- a/tests/py_runtime_info/py_runtime_info_tests.bzl +++ b/tests/py_runtime_info/py_runtime_info_tests.bzl @@ -16,13 +16,18 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") load("//python:py_runtime_info.bzl", "PyRuntimeInfo") +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility def _create_py_runtime_info_without_interpreter_version_info_impl(ctx): + kwargs = {} + if IS_BAZEL_7_OR_HIGHER: + kwargs["bootstrap_template"] = ctx.attr.bootstrap_template + return [PyRuntimeInfo( interpreter = ctx.file.interpreter, files = depset(ctx.files.files), python_version = "PY3", - bootstrap_template = ctx.attr.bootstrap_template, + **kwargs )] _create_py_runtime_info_without_interpreter_version_info = rule( diff --git a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl index 6af25a4e75..ec7ca63832 100644 --- a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl +++ b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl @@ -217,6 +217,7 @@ def _test_whl_and_library_deps_from_requires(env): filegroup = lambda **kwargs: filegroup_calls.append(kwargs), config_setting = lambda **_: None, glob = mock_glob.glob, + select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -229,7 +230,7 @@ def _test_whl_and_library_deps_from_requires(env): { "name": "whl", "srcs": ["foo-0-py3-none-any.whl"], - "data": ["@pypi//bar:whl"] + select({ + "data": ["@pypi//bar:whl"] + _select({ ":is_include_bar_baz_true": ["@pypi//bar_baz:whl"], "//conditions:default": [], }), @@ -244,14 +245,14 @@ def _test_whl_and_library_deps_from_requires(env): env.expect.that_dict(py_library_call).contains_exactly({ "name": "pkg", - "srcs": ["site-packages/foo/SRCS.py"] + select({ + "srcs": ["site-packages/foo/SRCS.py"] + _select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), "pyi_srcs": ["site-packages/foo/PYI.pyi"], "data": ["site-packages/foo/DATA.txt"], "imports": ["site-packages"], - "deps": ["@pypi//bar:pkg"] + select({ + "deps": ["@pypi//bar:pkg"] + _select({ ":is_include_bar_baz_true": ["@pypi//bar_baz:pkg"], "//conditions:default": [], }), @@ -320,6 +321,7 @@ def _test_whl_and_library_deps(env): filegroup = lambda **kwargs: filegroup_calls.append(kwargs), config_setting = lambda **_: None, glob = mock_glob.glob, + select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -334,7 +336,7 @@ def _test_whl_and_library_deps(env): "data": [ "@pypi_bar_baz//:whl", "@pypi_foo//:whl", - ] + select( + ] + _select( { Label("//python/config_settings:is_python_3.9"): ["@pypi_py39_dep//:whl"], "@platforms//cpu:aarch64": ["@pypi_arm_dep//:whl"], @@ -355,7 +357,7 @@ def _test_whl_and_library_deps(env): return env.expect.that_dict(py_library_calls[0]).contains_exactly({ "name": "pkg", - "srcs": ["site-packages/foo/SRCS.py"] + select({ + "srcs": ["site-packages/foo/SRCS.py"] + _select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), @@ -365,7 +367,7 @@ def _test_whl_and_library_deps(env): "deps": [ "@pypi_bar_baz//:pkg", "@pypi_foo//:pkg", - ] + select( + ] + _select( { Label("//python/config_settings:is_python_3.9"): ["@pypi_py39_dep//:pkg"], "@platforms//cpu:aarch64": ["@pypi_arm_dep//:pkg"], @@ -413,6 +415,7 @@ def _test_group(env): config_setting = lambda **_: None, glob = mock_glob.glob, alias = lambda **kwargs: alias_calls.append(kwargs), + select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -434,14 +437,14 @@ def _test_group(env): py_library_call, ).contains_exactly({ "name": "_pkg", - "srcs": ["site-packages/foo/srcs.py"] + select({ + "srcs": ["site-packages/foo/srcs.py"] + _select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), "pyi_srcs": ["site-packages/foo/pyi.pyi"], "data": ["site-packages/foo/data.txt"], "imports": ["site-packages"], - "deps": ["@pypi_bar_baz//:pkg"] + select({ + "deps": ["@pypi_bar_baz//:pkg"] + _select({ "@platforms//os:linux": ["@pypi_box//:pkg"], ":is_linux_x86_64": ["@pypi_box//:pkg", "@pypi_box_amd64//:pkg"], "//conditions:default": [], @@ -488,6 +491,13 @@ def _mock_glob(): ) return mock +def _select(*args, **kwargs): + """We need to have this mock select because we still need to support bazel 6.""" + return [struct( + select = args, + kwargs = kwargs, + )] + def whl_library_targets_test_suite(name): """create the test suite. diff --git a/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl b/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl index add741e300..aa4d1c793b 100644 --- a/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl +++ b/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl @@ -24,6 +24,7 @@ load( "PY_CC_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE", ) # buildifier: disable=bzl-visibility +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "CC_TOOLCHAIN") _LookupInfo = provider() # buildifier: disable=provider-params @@ -54,10 +55,25 @@ def _test_runtime_env_toolchain_matches(name): name = name + "_subject", ) extra_toolchains = [ - CC_TOOLCHAIN, str(Label("//python/runtime_env_toolchains:all")), ] + # We have to add a cc toolchain because py_cc toolchain depends on it. + # However, that package also defines a different fake py_cc toolchain we + # don't want to use, so we need to ensure the runtime_env toolchain has + # higher precendence. + # However, Bazel 6 and Bazel 7 process --extra_toolchains in different + # orders: + # * Bazel 6 goes left to right + # * Bazel 7 goes right to left + # We could just put our preferred toolchain before *and* after + # the undesired toolchain... + # However, Bazel 7 has a bug where *duplicate* entries are ignored, + # and only the *first* entry is respected. + if IS_BAZEL_7_OR_HIGHER: + extra_toolchains.insert(0, CC_TOOLCHAIN) + else: + extra_toolchains.append(CC_TOOLCHAIN) analysis_test( name = name, impl = _test_runtime_env_toolchain_matches_impl, diff --git a/tests/support/sh_py_run_test.bzl b/tests/support/sh_py_run_test.bzl index 2d26e890a2..7dff6673ce 100644 --- a/tests/support/sh_py_run_test.bzl +++ b/tests/support/sh_py_run_test.bzl @@ -37,7 +37,7 @@ def sh_py_run_test(*, name, sh_src, py_src, **kwargs): srcs = [sh_src], data = [bin_name], deps = [ - "@rules_shell//shell/runfiles", + "@bazel_tools//tools/bash/runfiles", ], env = { "BIN_RLOCATION": "$(rlocationpaths {})".format(bin_name), diff --git a/tests/support/support.bzl b/tests/support/support.bzl index 96c6ad902a..37d3488316 100644 --- a/tests/support/support.bzl +++ b/tests/support/support.bzl @@ -20,6 +20,7 @@ # places. load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility +load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility MAC = Label("//tests/support:mac") MAC_X86_64 = Label("//tests/support:mac_x86_64") @@ -39,7 +40,7 @@ CUSTOM_RUNTIME = str(Label("//tests/support:custom_runtime")) SUPPORTS_BOOTSTRAP_SCRIPT = select({ "@platforms//os:windows": ["@platforms//:incompatible"], "//conditions:default": [], -}) +}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] SUPPORTS_BZLMOD_UNIXY = select({ "@platforms//os:windows": ["@platforms//:incompatible"], diff --git a/tests/support/whl_from_dir/whl_from_dir_repo.bzl b/tests/support/whl_from_dir/whl_from_dir_repo.bzl index 4e16e8ee4a..176525636c 100644 --- a/tests/support/whl_from_dir/whl_from_dir_repo.bzl +++ b/tests/support/whl_from_dir/whl_from_dir_repo.bzl @@ -8,7 +8,7 @@ load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl def _whl_from_dir_repo(rctx): root = rctx.path(rctx.attr.root).dirname - rctx.watch_tree(root) + repo_utils.watch_tree(rctx, root) output = rctx.path(rctx.attr.output) repo_utils.execute_checked( From 1c564f685d966c0c43b860a239ef21eb365b7ded Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:03:49 +0900 Subject: [PATCH 03/19] refactor: use rules_shell runfiles lib --- python/private/interpreter.bzl | 2 +- tests/bootstrap_impls/BUILD.bazel | 2 +- tests/support/sh_py_run_test.bzl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/private/interpreter.bzl b/python/private/interpreter.bzl index c66d3dc21e..ca65262bfc 100644 --- a/python/private/interpreter.bzl +++ b/python/private/interpreter.bzl @@ -72,7 +72,7 @@ interpreter_binary = rule( mandatory = True, ), "_bash_runfiles": attr.label( - default = "@bazel_tools//tools/bash/runfiles", + default = "@rules_shell//shell/runfiles", ), "_template": attr.label( default = "//python/private:interpreter_tmpl.sh", diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel index c3d44df240..d8967f3000 100644 --- a/tests/bootstrap_impls/BUILD.bazel +++ b/tests/bootstrap_impls/BUILD.bazel @@ -44,7 +44,7 @@ sh_test( }, target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT, deps = [ - "@bazel_tools//tools/bash/runfiles", + "@rules_shell//shell/runfiles", ], ) diff --git a/tests/support/sh_py_run_test.bzl b/tests/support/sh_py_run_test.bzl index 7dff6673ce..2d26e890a2 100644 --- a/tests/support/sh_py_run_test.bzl +++ b/tests/support/sh_py_run_test.bzl @@ -37,7 +37,7 @@ def sh_py_run_test(*, name, sh_src, py_src, **kwargs): srcs = [sh_src], data = [bin_name], deps = [ - "@bazel_tools//tools/bash/runfiles", + "@rules_shell//shell/runfiles", ], env = { "BIN_RLOCATION": "$(rlocationpaths {})".format(bin_name), From 4818ffa8c8004e6f33ad3692a879a5c5e222bbda Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:06:42 +0900 Subject: [PATCH 04/19] refactor: remove watch helpers --- python/private/local_runtime_repo.bzl | 6 ++--- python/private/pypi/pypi_repo_utils.bzl | 5 ++--- python/private/repo_utils.bzl | 22 +------------------ .../whl_from_dir/whl_from_dir_repo.bzl | 2 +- 4 files changed, 7 insertions(+), 28 deletions(-) diff --git a/python/private/local_runtime_repo.bzl b/python/private/local_runtime_repo.bzl index c053a03508..27c90b1bc9 100644 --- a/python/private/local_runtime_repo.bzl +++ b/python/private/local_runtime_repo.bzl @@ -71,7 +71,7 @@ def _symlink_first_library(rctx, logger, libraries): else: linked = "lib/{}".format(origin.basename) logger.debug("Symlinking {} to {}".format(origin, linked)) - repo_utils.watch(rctx, origin) + rctx.watch(origin) rctx.symlink(origin, linked) break @@ -142,7 +142,7 @@ def _local_runtime_repo_impl(rctx): # path is an error. Silently skip, since includes are only necessary # if C extensions are built. if include_path.exists and include_path.is_dir: - repo_utils.watch_tree(rctx, include_path) + rctx.watch_tree(include_path) else: pass @@ -268,7 +268,7 @@ def _resolve_interpreter_path(rctx): resolved_path = result.binary describe_failure = result.describe_failure else: - repo_utils.watch(rctx, rctx.attr.interpreter_path) + rctx.watch(rctx.attr.interpreter_path) resolved_path = rctx.path(rctx.attr.interpreter_path) if not resolved_path.exists: describe_failure = lambda: "Path not found: {}".format(repr(rctx.attr.interpreter_path)) diff --git a/python/private/pypi/pypi_repo_utils.bzl b/python/private/pypi/pypi_repo_utils.bzl index bb2acc850a..04c9b5d685 100644 --- a/python/private/pypi/pypi_repo_utils.bzl +++ b/python/private/pypi/pypi_repo_utils.bzl @@ -107,9 +107,8 @@ def _construct_pypath(mrctx, *, entries): def _execute_prep(mrctx, *, python, srcs, **kwargs): for src in srcs: # This will ensure that we will re-evaluate the bzlmod extension or - # refetch the repository_rule when the srcs change. This should work on - # Bazel versions without `mrctx.watch` as well. - repo_utils.watch(mrctx, mrctx.path(src)) + # refetch the repository_rule when the srcs change. + mrctx.watch(mrctx.path(src)) environment = kwargs.pop("environment", {}) pythonpath = environment.get("PYTHONPATH", "") diff --git a/python/private/repo_utils.bzl b/python/private/repo_utils.bzl index 32a5b70e15..77eac55c16 100644 --- a/python/private/repo_utils.bzl +++ b/python/private/repo_utils.bzl @@ -291,7 +291,7 @@ def _which_unchecked(mrctx, binary_name): """ binary = mrctx.which(binary_name) if binary: - _watch(mrctx, binary) + mrctx.watch(binary) describe_failure = None else: path = _getenv(mrctx, "PATH", "") @@ -429,24 +429,6 @@ def _get_platforms_cpu_name(mrctx): return "riscv64" return arch -# TODO: Remove after Bazel 6 support dropped -def _watch(mrctx, *args, **kwargs): - """Calls mrctx.watch, if available.""" - if not args and not kwargs: - fail("'watch' needs at least a single argument.") - - if hasattr(mrctx, "watch"): - mrctx.watch(*args, **kwargs) - -# TODO: Remove after Bazel 6 support dropped -def _watch_tree(mrctx, *args, **kwargs): - """Calls mrctx.watch_tree, if available.""" - if not args and not kwargs: - fail("'watch_tree' needs at least a single argument.") - - if hasattr(mrctx, "watch_tree"): - mrctx.watch_tree(*args, **kwargs) - repo_utils = struct( # keep sorted execute_checked = _execute_checked, @@ -457,8 +439,6 @@ repo_utils = struct( getenv = _getenv, is_repo_debug_enabled = _is_repo_debug_enabled, logger = _logger, - watch = _watch, - watch_tree = _watch_tree, which_checked = _which_checked, which_unchecked = _which_unchecked, ) diff --git a/tests/support/whl_from_dir/whl_from_dir_repo.bzl b/tests/support/whl_from_dir/whl_from_dir_repo.bzl index 176525636c..4e16e8ee4a 100644 --- a/tests/support/whl_from_dir/whl_from_dir_repo.bzl +++ b/tests/support/whl_from_dir/whl_from_dir_repo.bzl @@ -8,7 +8,7 @@ load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl def _whl_from_dir_repo(rctx): root = rctx.path(rctx.attr.root).dirname - repo_utils.watch_tree(rctx, root) + rctx.watch_tree(root) output = rctx.path(rctx.attr.output) repo_utils.execute_checked( From 735a72cd448da17a2d0d30a5df7c79046aa79258 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:09:17 +0900 Subject: [PATCH 05/19] refactor: remove usage of select helper --- python/private/pypi/whl_library_targets.bzl | 12 ++------- .../whl_library_targets_tests.bzl | 26 ++++++------------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/python/private/pypi/whl_library_targets.bzl b/python/private/pypi/whl_library_targets.bzl index aed5bc74f5..a443d7858d 100644 --- a/python/private/pypi/whl_library_targets.bzl +++ b/python/private/pypi/whl_library_targets.bzl @@ -315,13 +315,6 @@ def whl_library_targets( deps_by_platform = dependencies_by_platform, deps_conditional = deps_conditional, tmpl = dep_template.format(name = "{}", target = WHEEL_FILE_PUBLIC_LABEL), - # NOTE @aignas 2024-10-28: Actually, `select` is not part of - # `native`, but in order to support bazel 6.4 in unit tests, I - # have to somehow pass the `select` implementation in the unit - # tests and I chose this to be routed through the `native` - # struct. So, tests` will be successful in `getattr` and the - # real code will use the fallback provided here. - select = getattr(native, "select", select), ), visibility = impl_vis, ) @@ -362,7 +355,7 @@ def whl_library_targets( ) if not enable_implicit_namespace_pkgs: - srcs = srcs + getattr(native, "select", select)({ + srcs = srcs + select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": rules.create_inits( srcs = srcs + data + pyi_srcs, @@ -384,7 +377,6 @@ def whl_library_targets( deps_by_platform = dependencies_by_platform, deps_conditional = deps_conditional, tmpl = dep_template.format(name = "{}", target = PY_LIBRARY_PUBLIC_LABEL), - select = getattr(native, "select", select), ), tags = tags, visibility = impl_vis, @@ -455,7 +447,7 @@ def _plat_label(plat): else: return ":is_" + plat.replace("cp3", "python_3.") -def _deps(deps, deps_by_platform, deps_conditional, tmpl, select = select): +def _deps(deps, deps_by_platform, deps_conditional, tmpl): deps = [tmpl.format(d) for d in sorted(deps)] for dep, setting in deps_conditional.items(): diff --git a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl index ec7ca63832..6af25a4e75 100644 --- a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl +++ b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl @@ -217,7 +217,6 @@ def _test_whl_and_library_deps_from_requires(env): filegroup = lambda **kwargs: filegroup_calls.append(kwargs), config_setting = lambda **_: None, glob = mock_glob.glob, - select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -230,7 +229,7 @@ def _test_whl_and_library_deps_from_requires(env): { "name": "whl", "srcs": ["foo-0-py3-none-any.whl"], - "data": ["@pypi//bar:whl"] + _select({ + "data": ["@pypi//bar:whl"] + select({ ":is_include_bar_baz_true": ["@pypi//bar_baz:whl"], "//conditions:default": [], }), @@ -245,14 +244,14 @@ def _test_whl_and_library_deps_from_requires(env): env.expect.that_dict(py_library_call).contains_exactly({ "name": "pkg", - "srcs": ["site-packages/foo/SRCS.py"] + _select({ + "srcs": ["site-packages/foo/SRCS.py"] + select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), "pyi_srcs": ["site-packages/foo/PYI.pyi"], "data": ["site-packages/foo/DATA.txt"], "imports": ["site-packages"], - "deps": ["@pypi//bar:pkg"] + _select({ + "deps": ["@pypi//bar:pkg"] + select({ ":is_include_bar_baz_true": ["@pypi//bar_baz:pkg"], "//conditions:default": [], }), @@ -321,7 +320,6 @@ def _test_whl_and_library_deps(env): filegroup = lambda **kwargs: filegroup_calls.append(kwargs), config_setting = lambda **_: None, glob = mock_glob.glob, - select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -336,7 +334,7 @@ def _test_whl_and_library_deps(env): "data": [ "@pypi_bar_baz//:whl", "@pypi_foo//:whl", - ] + _select( + ] + select( { Label("//python/config_settings:is_python_3.9"): ["@pypi_py39_dep//:whl"], "@platforms//cpu:aarch64": ["@pypi_arm_dep//:whl"], @@ -357,7 +355,7 @@ def _test_whl_and_library_deps(env): return env.expect.that_dict(py_library_calls[0]).contains_exactly({ "name": "pkg", - "srcs": ["site-packages/foo/SRCS.py"] + _select({ + "srcs": ["site-packages/foo/SRCS.py"] + select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), @@ -367,7 +365,7 @@ def _test_whl_and_library_deps(env): "deps": [ "@pypi_bar_baz//:pkg", "@pypi_foo//:pkg", - ] + _select( + ] + select( { Label("//python/config_settings:is_python_3.9"): ["@pypi_py39_dep//:pkg"], "@platforms//cpu:aarch64": ["@pypi_arm_dep//:pkg"], @@ -415,7 +413,6 @@ def _test_group(env): config_setting = lambda **_: None, glob = mock_glob.glob, alias = lambda **kwargs: alias_calls.append(kwargs), - select = _select, ), rules = struct( py_library = lambda **kwargs: py_library_calls.append(kwargs), @@ -437,14 +434,14 @@ def _test_group(env): py_library_call, ).contains_exactly({ "name": "_pkg", - "srcs": ["site-packages/foo/srcs.py"] + _select({ + "srcs": ["site-packages/foo/srcs.py"] + select({ Label("//python/config_settings:is_venvs_site_packages"): [], "//conditions:default": ["_create_inits_target"], }), "pyi_srcs": ["site-packages/foo/pyi.pyi"], "data": ["site-packages/foo/data.txt"], "imports": ["site-packages"], - "deps": ["@pypi_bar_baz//:pkg"] + _select({ + "deps": ["@pypi_bar_baz//:pkg"] + select({ "@platforms//os:linux": ["@pypi_box//:pkg"], ":is_linux_x86_64": ["@pypi_box//:pkg", "@pypi_box_amd64//:pkg"], "//conditions:default": [], @@ -491,13 +488,6 @@ def _mock_glob(): ) return mock -def _select(*args, **kwargs): - """We need to have this mock select because we still need to support bazel 6.""" - return [struct( - select = args, - kwargs = kwargs, - )] - def whl_library_targets_test_suite(name): """create the test suite. From a9bd63136c28c8aae24a52777bd58db88aad2486 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:28:36 +0900 Subject: [PATCH 06/19] refactor: make enable_pystar fixed and cleanup code --- python/BUILD.bazel | 11 ------ python/features.bzl | 4 +-- python/private/BUILD.bazel | 2 -- python/private/internal_config_repo.bzl | 25 +++---------- python/private/py_info.bzl | 28 ++++++--------- python/private/util.bzl | 6 +--- python/py_binary.bzl | 3 +- python/py_cc_link_params_info.bzl | 7 +--- python/py_info.bzl | 4 +-- python/py_library.bzl | 3 +- python/py_runtime_info.bzl | 4 +-- python/py_test.bzl | 3 +- tests/api/py_common/py_common_tests.bzl | 10 ++---- .../precompile/precompile_tests.bzl | 34 ------------------ tests/base_rules/py_executable_base_tests.bzl | 35 +++++++------------ tests/base_rules/py_info/py_info_tests.bzl | 17 ++++----- tests/py_runtime/py_runtime_tests.bzl | 30 +++------------- 17 files changed, 49 insertions(+), 177 deletions(-) diff --git a/python/BUILD.bazel b/python/BUILD.bazel index 76fa5dde6e..416ba8fa33 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -81,9 +81,6 @@ bzl_library( bzl_library( name = "features_bzl", srcs = ["features.bzl"], - deps = [ - "@rules_python_internal//:rules_python_config_bzl", - ], ) bzl_library( @@ -131,7 +128,6 @@ bzl_library( "//python/private:py_binary_macro_bzl", "//python/private:register_extension_info_bzl", "//python/private:util_bzl", - "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -140,7 +136,6 @@ bzl_library( srcs = ["py_cc_link_params_info.bzl"], deps = [ "//python/private:py_cc_link_params_info_bzl", - "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -173,8 +168,6 @@ bzl_library( srcs = ["py_info.bzl"], deps = [ "//python/private:py_info_bzl", - "//python/private:reexports_bzl", - "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -185,7 +178,6 @@ bzl_library( "//python/private:py_library_macro_bzl", "//python/private:register_extension_info_bzl", "//python/private:util_bzl", - "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -213,8 +205,6 @@ bzl_library( srcs = ["py_runtime_info.bzl"], deps = [ "//python/private:py_runtime_info_bzl", - "//python/private:reexports_bzl", - "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -225,7 +215,6 @@ bzl_library( "//python/private:py_test_macro_bzl", "//python/private:register_extension_info_bzl", "//python/private:util_bzl", - "@rules_python_internal//:rules_python_config_bzl", ], ) diff --git a/python/features.bzl b/python/features.bzl index 21ff588dca..00bc1a7817 100644 --- a/python/features.bzl +++ b/python/features.bzl @@ -13,8 +13,6 @@ # limitations under the License. """Allows detecting of rules_python features that aren't easily detected.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") - # This is a magic string expanded by `git archive`, as set by `.gitattributes` # See https://git-scm.com/docs/git-archive/2.29.0#Documentation/git-archive.txt-export-subst _VERSION_PRIVATE = "$Format:%(describe:tags=true)$" @@ -73,6 +71,6 @@ features = struct( headers_abi3 = True, precompile = True, py_info_venv_symlinks = True, - uses_builtin_rules = not config.enable_pystar, + uses_builtin_rules = False, version = _VERSION_PRIVATE if "$Format" not in _VERSION_PRIVATE else "", ) diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index 0c8ccdea99..dfeecf2bd5 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -441,7 +441,6 @@ bzl_library( ":builders_bzl", ":reexports_bzl", ":util_bzl", - "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -688,7 +687,6 @@ bzl_library( ], deps = [ "@bazel_skylib//lib:types", - "@rules_python_internal//:rules_python_config_bzl", ], ) diff --git a/python/private/internal_config_repo.bzl b/python/private/internal_config_repo.bzl index b57275b672..174c6ec543 100644 --- a/python/private/internal_config_repo.bzl +++ b/python/private/internal_config_repo.bzl @@ -23,14 +23,12 @@ load(":repo_utils.bzl", "repo_utils") _ENABLE_PIPSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PIPSTAR" _ENABLE_PIPSTAR_DEFAULT = "0" -_ENABLE_PYSTAR_ENVVAR_NAME = "RULES_PYTHON_ENABLE_PYSTAR" -_ENABLE_PYSTAR_DEFAULT = "1" _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME = "RULES_PYTHON_DEPRECATION_WARNINGS" _ENABLE_DEPRECATION_WARNINGS_DEFAULT = "0" _CONFIG_TEMPLATE = """ config = struct( - enable_pystar = {enable_pystar}, + enable_pystar = True, enable_pipstar = {enable_pipstar}, enable_deprecation_warnings = {enable_deprecation_warnings}, BuiltinPyInfo = getattr(getattr(native, "legacy_globals", None), "PyInfo", {builtin_py_info_symbol}), @@ -88,15 +86,6 @@ _TRANSITION_SETTINGS_DEBUG_TEMPLATE = """ """ def _internal_config_repo_impl(rctx): - pystar_requested = _bool_from_environ(rctx, _ENABLE_PYSTAR_ENVVAR_NAME, _ENABLE_PYSTAR_DEFAULT) - - # Bazel 7+ (dev and later) has native.starlark_doc_extract, and thus the - # py_internal global, which are necessary for the pystar implementation. - if pystar_requested and hasattr(native, "starlark_doc_extract"): - enable_pystar = pystar_requested - else: - enable_pystar = False - if not native.bazel_version or int(native.bazel_version.split(".")[0]) >= 8: builtin_py_info_symbol = "None" builtin_py_runtime_info_symbol = "None" @@ -107,7 +96,6 @@ def _internal_config_repo_impl(rctx): builtin_py_cc_link_params_provider = "PyCcLinkParamsProvider" rctx.file("rules_python_config.bzl", _CONFIG_TEMPLATE.format( - enable_pystar = enable_pystar, enable_pipstar = _bool_from_environ(rctx, _ENABLE_PIPSTAR_ENVVAR_NAME, _ENABLE_PIPSTAR_DEFAULT), enable_deprecation_warnings = _bool_from_environ(rctx, _ENABLE_DEPRECATION_WARNINGS_ENVVAR_NAME, _ENABLE_DEPRECATION_WARNINGS_DEFAULT), builtin_py_info_symbol = builtin_py_info_symbol, @@ -115,12 +103,8 @@ def _internal_config_repo_impl(rctx): builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider, )) - if enable_pystar: - shim_content = _PY_INTERNAL_SHIM - py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"' - else: - shim_content = "py_internal_impl = None\n" - py_internal_dep = "" + shim_content = _PY_INTERNAL_SHIM + py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"' # Bazel 5 doesn't support repository visibility, so just use public # as a stand-in @@ -155,7 +139,8 @@ def _internal_config_repo_impl(rctx): internal_config_repo = repository_rule( implementation = _internal_config_repo_impl, configure = True, - environ = [_ENABLE_PYSTAR_ENVVAR_NAME], + # TODO @aignas 2025-09-27: PIPSTAR env var bugfix + environ = [], attrs = { "transition_setting_generators": attr.string_list_dict(), "transition_settings": attr.string_list(), diff --git a/python/private/py_info.bzl b/python/private/py_info.bzl index f96dec554b..e532cc92a2 100644 --- a/python/private/py_info.bzl +++ b/python/private/py_info.bzl @@ -13,7 +13,6 @@ # limitations under the License. """Implementation of PyInfo provider and PyInfo-specific utilities.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load(":builders.bzl", "builders") load(":reexports.bzl", "BuiltinPyInfo") load(":util.bzl", "define_bazel_6_provider") @@ -327,7 +326,7 @@ This field is currently unused in Bazel and may go away in the future. ) # The "effective" PyInfo is what the canonical //python:py_info.bzl%PyInfo symbol refers to -_EffectivePyInfo = PyInfo if (config.enable_pystar or BuiltinPyInfo == None) else BuiltinPyInfo +_EffectivePyInfo = PyInfo def _PyInfoBuilder_typedef(): """Builder for PyInfo. @@ -630,28 +629,21 @@ def _PyInfoBuilder_build(self): Returns: {type}`PyInfo` """ - if config.enable_pystar: - kwargs = dict( - direct_original_sources = self.direct_original_sources.build(), - direct_pyc_files = self.direct_pyc_files.build(), - direct_pyi_files = self.direct_pyi_files.build(), - transitive_implicit_pyc_files = self.transitive_implicit_pyc_files.build(), - transitive_implicit_pyc_source_files = self.transitive_implicit_pyc_source_files.build(), - transitive_original_sources = self.transitive_original_sources.build(), - transitive_pyc_files = self.transitive_pyc_files.build(), - transitive_pyi_files = self.transitive_pyi_files.build(), - venv_symlinks = self.venv_symlinks.build(), - ) - else: - kwargs = {} - return _EffectivePyInfo( has_py2_only_sources = self._has_py2_only_sources[0], has_py3_only_sources = self._has_py3_only_sources[0], imports = self.imports.build(), transitive_sources = self.transitive_sources.build(), uses_shared_libraries = self._uses_shared_libraries[0], - **kwargs + direct_original_sources = self.direct_original_sources.build(), + direct_pyc_files = self.direct_pyc_files.build(), + direct_pyi_files = self.direct_pyi_files.build(), + transitive_implicit_pyc_files = self.transitive_implicit_pyc_files.build(), + transitive_implicit_pyc_source_files = self.transitive_implicit_pyc_source_files.build(), + transitive_original_sources = self.transitive_original_sources.build(), + transitive_pyc_files = self.transitive_pyc_files.build(), + transitive_pyi_files = self.transitive_pyi_files.build(), + venv_symlinks = self.venv_symlinks.build(), ) def _PyInfoBuilder_build_builtin_py_info(self): diff --git a/python/private/util.bzl b/python/private/util.bzl index 4d2da57760..f31e8affe9 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -15,7 +15,6 @@ """Functionality shared by multiple pieces of code.""" load("@bazel_skylib//lib:types.bzl", "types") -load("@rules_python_internal//:rules_python_config.bzl", "config") def copy_propagating_kwargs(from_kwargs, into_kwargs = None): """Copies args that must be compatible between two targets with a dependency relationship. @@ -50,8 +49,7 @@ def copy_propagating_kwargs(from_kwargs, into_kwargs = None): # The implementation of the macros and tagging mechanism follows the example # set by rules_cc and rules_java. -_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__" - +# TODO @aignas 2025-09-27: remove this function def add_migration_tag(attrs): """Add a special tag to `attrs` to aid migration off native rles. @@ -61,8 +59,6 @@ def add_migration_tag(attrs): Returns: The same `attrs` object, but modified. """ - if not config.enable_pystar: - add_tag(attrs, _MIGRATION_TAG) return attrs def add_tag(attrs, tag): diff --git a/python/py_binary.bzl b/python/py_binary.bzl index 48ea768948..54bb8640c6 100644 --- a/python/py_binary.bzl +++ b/python/py_binary.bzl @@ -14,13 +14,12 @@ """Public entry point for py_binary.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load("//python/private:py_binary_macro.bzl", _starlark_py_binary = "py_binary") load("//python/private:register_extension_info.bzl", "register_extension_info") load("//python/private:util.bzl", "add_migration_tag") # buildifier: disable=native-python -_py_binary_impl = _starlark_py_binary if config.enable_pystar else native.py_binary +_py_binary_impl = _starlark_py_binary def py_binary(**attrs): """Creates an executable Python program. diff --git a/python/py_cc_link_params_info.bzl b/python/py_cc_link_params_info.bzl index 02eff71c4d..6c510d6c8e 100644 --- a/python/py_cc_link_params_info.bzl +++ b/python/py_cc_link_params_info.bzl @@ -1,10 +1,5 @@ """Public entry point for PyCcLinkParamsInfo.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load("//python/private:py_cc_link_params_info.bzl", _starlark_PyCcLinkParamsInfo = "PyCcLinkParamsInfo") -PyCcLinkParamsInfo = ( - _starlark_PyCcLinkParamsInfo if ( - config.enable_pystar or config.BuiltinPyCcLinkParamsProvider == None - ) else config.BuiltinPyCcLinkParamsProvider -) +PyCcLinkParamsInfo = _starlark_PyCcLinkParamsInfo diff --git a/python/py_info.bzl b/python/py_info.bzl index 5697f58419..5582d3b491 100644 --- a/python/py_info.bzl +++ b/python/py_info.bzl @@ -14,8 +14,6 @@ """Public entry point for PyInfo.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load("//python/private:py_info.bzl", _starlark_PyInfo = "PyInfo") -load("//python/private:reexports.bzl", "BuiltinPyInfo") -PyInfo = _starlark_PyInfo if config.enable_pystar or BuiltinPyInfo == None else BuiltinPyInfo +PyInfo = _starlark_PyInfo diff --git a/python/py_library.bzl b/python/py_library.bzl index 8b8d46870b..b8e6e25952 100644 --- a/python/py_library.bzl +++ b/python/py_library.bzl @@ -14,13 +14,12 @@ """Public entry point for py_library.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load("//python/private:py_library_macro.bzl", _starlark_py_library = "py_library") load("//python/private:register_extension_info.bzl", "register_extension_info") load("//python/private:util.bzl", "add_migration_tag") # buildifier: disable=native-python -_py_library_impl = _starlark_py_library if config.enable_pystar else native.py_library +_py_library_impl = _starlark_py_library def py_library(**attrs): """Creates an executable Python program. diff --git a/python/py_runtime_info.bzl b/python/py_runtime_info.bzl index 3a31c0f2f4..082a9b0f19 100644 --- a/python/py_runtime_info.bzl +++ b/python/py_runtime_info.bzl @@ -14,8 +14,6 @@ """Public entry point for PyRuntimeInfo.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load("//python/private:py_runtime_info.bzl", _starlark_PyRuntimeInfo = "PyRuntimeInfo") -load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") -PyRuntimeInfo = _starlark_PyRuntimeInfo if config.enable_pystar else BuiltinPyRuntimeInfo +PyRuntimeInfo = _starlark_PyRuntimeInfo diff --git a/python/py_test.bzl b/python/py_test.bzl index b5657730b7..1c3f019ad0 100644 --- a/python/py_test.bzl +++ b/python/py_test.bzl @@ -14,13 +14,12 @@ """Public entry point for py_test.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load("//python/private:py_test_macro.bzl", _starlark_py_test = "py_test") load("//python/private:register_extension_info.bzl", "register_extension_info") load("//python/private:util.bzl", "add_migration_tag") # buildifier: disable=native-python -_py_test_impl = _starlark_py_test if config.enable_pystar else native.py_test +_py_test_impl = _starlark_py_test def py_test(**attrs): """Creates an executable Python program. diff --git a/tests/api/py_common/py_common_tests.bzl b/tests/api/py_common/py_common_tests.bzl index 572028b2a6..028da6cc37 100644 --- a/tests/api/py_common/py_common_tests.bzl +++ b/tests/api/py_common/py_common_tests.bzl @@ -13,7 +13,6 @@ # limitations under the License. """py_common tests.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") load("@rules_testing//lib:util.bzl", rt_util = "util") @@ -41,13 +40,11 @@ def _test_merge_py_infos_impl(env, target): py_common = _py_common.get(env.ctx) py1 = py_common.PyInfoBuilder() - if config.enable_pystar: - py1.direct_pyc_files.add(f1_pyc) + py1.direct_pyc_files.add(f1_pyc) py1.transitive_sources.add(f1_py) py2 = py_common.PyInfoBuilder() - if config.enable_pystar: - py1.direct_pyc_files.add(f2_pyc) + py1.direct_pyc_files.add(f2_pyc) py2.transitive_sources.add(f2_py) actual = py_info_subject( @@ -56,8 +53,7 @@ def _test_merge_py_infos_impl(env, target): ) actual.transitive_sources().contains_exactly([f1_py.path, f2_py.path]) - if config.enable_pystar: - actual.direct_pyc_files().contains_exactly([f1_pyc.path, f2_pyc.path]) + actual.direct_pyc_files().contains_exactly([f1_pyc.path, f2_pyc.path]) _tests.append(_test_merge_py_infos) diff --git a/tests/base_rules/precompile/precompile_tests.bzl b/tests/base_rules/precompile/precompile_tests.bzl index fe5c165648..bff994aa1a 100644 --- a/tests/base_rules/precompile/precompile_tests.bzl +++ b/tests/base_rules/precompile/precompile_tests.bzl @@ -14,7 +14,6 @@ """Tests for precompiling behavior.""" -load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") 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") @@ -42,9 +41,6 @@ _COMMON_CONFIG_SETTINGS = { _tests = [] def _test_executable_precompile_attr_enabled_setup(name, py_rule, **kwargs): - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return rt_util.helper_target( py_rule, name = name + "_subject", @@ -112,9 +108,6 @@ def _test_precompile_enabled_py_test(name): _tests.append(_test_precompile_enabled_py_test) def _test_precompile_enabled_py_library_setup(name, impl, config_settings): - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return rt_util.helper_target( py_library, name = name + "_subject", @@ -178,9 +171,6 @@ def _test_precompile_enabled_py_library_add_to_runfiles_enabled_impl(env, target _tests.append(_test_precompile_enabled_py_library_add_to_runfiles_enabled) def _test_pyc_only(name): - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return rt_util.helper_target( py_binary, name = name + "_subject", @@ -231,9 +221,6 @@ def _test_pyc_only_impl(env, target): ) def _test_precompiler_action(name): - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return rt_util.helper_target( py_binary, name = name + "_subject", @@ -325,9 +312,6 @@ def _verify_runfiles(contains_patterns, not_contains_patterns): return _verify_runfiles_impl def _test_precompile_flag_enabled_pyc_collection_attr_include_pyc(name): - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return _setup_precompile_flag_pyc_collection_attr_interaction( name = name, precompile_flag = "enabled", @@ -351,9 +335,6 @@ def _test_precompile_flag_enabled_pyc_collection_attr_disabled(name): """Verify that a binary can opt-out of using implicit pycs even when precompiling is enabled by default. """ - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return _setup_precompile_flag_pyc_collection_attr_interaction( name = name, precompile_flag = "enabled", @@ -376,9 +357,6 @@ _tests.append(_test_precompile_flag_enabled_pyc_collection_attr_disabled) def _test_precompile_flag_disabled_pyc_collection_attr_include_pyc(name): """Verify that a binary can opt-in to using pycs even when precompiling is disabled by default.""" - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return _setup_precompile_flag_pyc_collection_attr_interaction( name = name, precompile_flag = "disabled", @@ -398,9 +376,6 @@ def _test_precompile_flag_disabled_pyc_collection_attr_include_pyc(name): _tests.append(_test_precompile_flag_disabled_pyc_collection_attr_include_pyc) def _test_precompile_flag_disabled_pyc_collection_attr_disabled(name): - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return _setup_precompile_flag_pyc_collection_attr_interaction( name = name, precompile_flag = "disabled", @@ -424,9 +399,6 @@ def _test_pyc_collection_disabled_library_omit_source(name): """Verify that, when a binary doesn't include implicit pyc files, libraries that set omit_source still have the py source file included. """ - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return rt_util.helper_target( py_binary, name = name + "_subject", @@ -469,9 +441,6 @@ def _test_pyc_collection_disabled_library_omit_source_impl(env, target): _tests.append(_test_pyc_collection_disabled_library_omit_source) def _test_pyc_collection_include_dep_omit_source(name): - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return rt_util.helper_target( py_binary, name = name + "_subject", @@ -513,9 +482,6 @@ def _test_pyc_collection_include_dep_omit_source_impl(env, target): _tests.append(_test_pyc_collection_include_dep_omit_source) def _test_precompile_attr_inherit_pyc_collection_disabled_precompile_flag_enabled(name): - if not rp_config.enable_pystar: - rt_util.skip_test(name = name) - return rt_util.helper_target( py_binary, name = name + "_subject", diff --git a/tests/base_rules/py_executable_base_tests.bzl b/tests/base_rules/py_executable_base_tests.bzl index 4e451289dc..6abfb914da 100644 --- a/tests/base_rules/py_executable_base_tests.bzl +++ b/tests/base_rules/py_executable_base_tests.bzl @@ -14,7 +14,6 @@ """Tests common to py_binary and py_test (executable rules).""" load("@rules_python//python:py_runtime_info.bzl", RulesPythonPyRuntimeInfo = "PyRuntimeInfo") -load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:truth.bzl", "matching") load("@rules_testing//lib:util.bzl", rt_util = "util") @@ -30,10 +29,6 @@ load("//tests/support:support.bzl", "CC_TOOLCHAIN", "CROSSTOOL_TOP", "LINUX_X86_ _tests = [] def _test_basic_windows(name, config): - if rp_config.enable_pystar: - target_compatible_with = [] - else: - target_compatible_with = ["@platforms//:incompatible"] rt_util.helper_target( config.rule, name = name + "_subject", @@ -56,7 +51,7 @@ def _test_basic_windows(name, config): "//command_line_option:extra_toolchains": [CC_TOOLCHAIN], "//command_line_option:platforms": [WINDOWS_X86_64], }, - attr_values = {"target_compatible_with": target_compatible_with}, + attr_values = {}, ) def _test_basic_windows_impl(env, target): @@ -72,14 +67,11 @@ def _test_basic_windows_impl(env, target): _tests.append(_test_basic_windows) def _test_basic_zip(name, config): - if rp_config.enable_pystar: - target_compatible_with = select({ - # Disable the new test on windows because we have _test_basic_windows. - "@platforms//os:windows": ["@platforms//:incompatible"], - "//conditions:default": [], - }) - else: - target_compatible_with = ["@platforms//:incompatible"] + target_compatible_with = select({ + # Disable the new test on windows because we have _test_basic_windows. + "@platforms//os:windows": ["@platforms//:incompatible"], + "//conditions:default": [], + }) rt_util.helper_target( config.rule, name = name + "_subject", @@ -140,14 +132,13 @@ def _test_executable_in_runfiles_impl(env, target): "{workspace}/{package}/{test_name}_subject" + exe, ]) - if rp_config.enable_pystar: - py_exec_info = env.expect.that_target(target).provider(PyExecutableInfo, factory = PyExecutableInfoSubject.new) - py_exec_info.main().path().contains("_subject.py") - py_exec_info.interpreter_path().contains("python") - py_exec_info.runfiles_without_exe().contains_none_of([ - "{workspace}/{package}/{test_name}_subject" + exe, - "{workspace}/{package}/{test_name}_subject", - ]) + py_exec_info = env.expect.that_target(target).provider(PyExecutableInfo, factory = PyExecutableInfoSubject.new) + py_exec_info.main().path().contains("_subject.py") + py_exec_info.interpreter_path().contains("python") + py_exec_info.runfiles_without_exe().contains_none_of([ + "{workspace}/{package}/{test_name}_subject" + exe, + "{workspace}/{package}/{test_name}_subject", + ]) def _test_default_main_can_be_generated(name, config): rt_util.helper_target( diff --git a/tests/base_rules/py_info/py_info_tests.bzl b/tests/base_rules/py_info/py_info_tests.bzl index aa252a2937..623594807a 100644 --- a/tests/base_rules/py_info/py_info_tests.bzl +++ b/tests/base_rules/py_info/py_info_tests.bzl @@ -13,7 +13,6 @@ # limitations under the License. """Tests for py_info.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") load("@rules_testing//lib:util.bzl", rt_util = "util") @@ -39,11 +38,10 @@ def _provide_py_info_impl(ctx): kwargs["has_py2_only_sources"] = bool(ctx.attr.has_py2_only_sources) providers = [] - if config.enable_pystar: - providers.append(PyInfo(**kwargs)) + providers.append(PyInfo(**kwargs)) # Handle Bazel 6 or if Bazel autoloading is enabled - if not config.enable_pystar or (BuiltinPyInfo and PyInfo != BuiltinPyInfo): + if BuiltinPyInfo and PyInfo != BuiltinPyInfo: providers.append(BuiltinPyInfo(**{ k: kwargs[k] for k in ( @@ -95,10 +93,8 @@ def _test_py_info_create_impl(env, target): imports = depset(["import-path"]), transitive_sources = depset([trans_py]), uses_shared_libraries = True, - **(dict( - direct_pyc_files = depset([direct_pyc]), - transitive_pyc_files = depset([trans_pyc]), - ) if config.enable_pystar else {}) + direct_pyc_files = depset([direct_pyc]), + transitive_pyc_files = depset([trans_pyc]), ) subject = py_info_subject(actual, meta = env.expect.meta) @@ -107,9 +103,8 @@ def _test_py_info_create_impl(env, target): subject.has_py3_only_sources().equals(True) subject.transitive_sources().contains_exactly(["tests/base_rules/py_info/trans.py"]) subject.imports().contains_exactly(["import-path"]) - if config.enable_pystar: - subject.direct_pyc_files().contains_exactly(["tests/base_rules/py_info/direct.pyc"]) - subject.transitive_pyc_files().contains_exactly(["tests/base_rules/py_info/trans.pyc"]) + subject.direct_pyc_files().contains_exactly(["tests/base_rules/py_info/direct.pyc"]) + subject.transitive_pyc_files().contains_exactly(["tests/base_rules/py_info/trans.pyc"]) _tests.append(_test_py_info_create) diff --git a/tests/py_runtime/py_runtime_tests.bzl b/tests/py_runtime/py_runtime_tests.bzl index 4ec7590ab2..3f8fae9254 100644 --- a/tests/py_runtime/py_runtime_tests.bzl +++ b/tests/py_runtime/py_runtime_tests.bzl @@ -13,7 +13,6 @@ # limitations under the License. """Starlark tests for py_runtime rule.""" -load("@rules_python_internal//:rules_python_config.bzl", "config") 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") @@ -49,27 +48,18 @@ _simple_binary = rule( ) def _test_bootstrap_template(name): - # The bootstrap_template arg isn't present in older Bazel versions, so - # we have to conditionally pass the arg and mark the test incompatible. - if config.enable_pystar: - py_runtime_kwargs = {"bootstrap_template": "bootstrap.txt"} - attr_values = {} - else: - py_runtime_kwargs = {} - attr_values = _SKIP_TEST - rt_util.helper_target( py_runtime, name = name + "_subject", interpreter_path = "/py", python_version = "PY3", - **py_runtime_kwargs + bootstrap_template = "bootstrap.txt", ) analysis_test( name = name, target = name + "_subject", impl = _test_bootstrap_template_impl, - attr_values = attr_values, + attr_values = {}, ) def _test_bootstrap_template_impl(env, target): @@ -415,28 +405,19 @@ def _test_system_interpreter_must_be_absolute_impl(env, target): _tests.append(_test_system_interpreter_must_be_absolute) def _interpreter_version_info_test(name, interpreter_version_info, impl, expect_failure = True): - if config.enable_pystar: - py_runtime_kwargs = { - "interpreter_version_info": interpreter_version_info, - } - attr_values = {} - else: - py_runtime_kwargs = {} - attr_values = _SKIP_TEST - rt_util.helper_target( py_runtime, name = name + "_subject", python_version = "PY3", interpreter_path = "/py", - **py_runtime_kwargs + interpreter_version_info = interpreter_version_info, ) analysis_test( name = name, target = name + "_subject", impl = impl, expect_failure = expect_failure, - attr_values = attr_values, + attr_values = {}, ) def _test_interpreter_version_info_must_define_major_and_minor_only_major(name): @@ -530,9 +511,6 @@ def _test_interpreter_version_info_parses_values_to_struct_impl(env, target): _tests.append(_test_interpreter_version_info_parses_values_to_struct) def _test_version_info_from_flag(name): - if not config.enable_pystar: - rt_util.skip_test(name) - return py_runtime( name = name + "_subject", interpreter_version_info = None, From 3f580d1df6d7e77264d34edd5dd0fc513343267b Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:29:07 +0900 Subject: [PATCH 07/19] refactor: remove migration tag helper --- python/BUILD.bazel | 3 --- python/private/util.bzl | 12 ------------ python/py_binary.bzl | 3 +-- python/py_library.bzl | 3 +-- python/py_runtime.bzl | 4 ++-- python/py_test.bzl | 3 +-- 6 files changed, 5 insertions(+), 23 deletions(-) diff --git a/python/BUILD.bazel b/python/BUILD.bazel index 416ba8fa33..2d74de0c4a 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -127,7 +127,6 @@ bzl_library( deps = [ "//python/private:py_binary_macro_bzl", "//python/private:register_extension_info_bzl", - "//python/private:util_bzl", ], ) @@ -177,7 +176,6 @@ bzl_library( deps = [ "//python/private:py_library_macro_bzl", "//python/private:register_extension_info_bzl", - "//python/private:util_bzl", ], ) @@ -214,7 +212,6 @@ bzl_library( deps = [ "//python/private:py_test_macro_bzl", "//python/private:register_extension_info_bzl", - "//python/private:util_bzl", ], ) diff --git a/python/private/util.bzl b/python/private/util.bzl index f31e8affe9..0e9d024b50 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -49,18 +49,6 @@ def copy_propagating_kwargs(from_kwargs, into_kwargs = None): # The implementation of the macros and tagging mechanism follows the example # set by rules_cc and rules_java. -# TODO @aignas 2025-09-27: remove this function -def add_migration_tag(attrs): - """Add a special tag to `attrs` to aid migration off native rles. - - Args: - attrs: dict of keyword args. The `tags` key will be modified in-place. - - Returns: - The same `attrs` object, but modified. - """ - return attrs - def add_tag(attrs, tag): """Adds `tag` to `attrs["tags"]`. diff --git a/python/py_binary.bzl b/python/py_binary.bzl index 54bb8640c6..4e26a29af2 100644 --- a/python/py_binary.bzl +++ b/python/py_binary.bzl @@ -16,7 +16,6 @@ load("//python/private:py_binary_macro.bzl", _starlark_py_binary = "py_binary") load("//python/private:register_extension_info.bzl", "register_extension_info") -load("//python/private:util.bzl", "add_migration_tag") # buildifier: disable=native-python _py_binary_impl = _starlark_py_binary @@ -41,7 +40,7 @@ def py_binary(**attrs): if attrs.get("srcs_version") in ("PY2", "PY2ONLY"): fail("Python 2 is no longer supported: https://github.com/bazel-contrib/rules_python/issues/886") - _py_binary_impl(**add_migration_tag(attrs)) + _py_binary_impl(**attrs) register_extension_info( extension = py_binary, diff --git a/python/py_library.bzl b/python/py_library.bzl index b8e6e25952..4b79d8f0eb 100644 --- a/python/py_library.bzl +++ b/python/py_library.bzl @@ -16,7 +16,6 @@ load("//python/private:py_library_macro.bzl", _starlark_py_library = "py_library") load("//python/private:register_extension_info.bzl", "register_extension_info") -load("//python/private:util.bzl", "add_migration_tag") # buildifier: disable=native-python _py_library_impl = _starlark_py_library @@ -38,7 +37,7 @@ def py_library(**attrs): if attrs.get("srcs_version") in ("PY2", "PY2ONLY"): fail("Python 2 is no longer supported: https://github.com/bazel-contrib/rules_python/issues/886") - _py_library_impl(**add_migration_tag(attrs)) + _py_library_impl(**attrs) register_extension_info( extension = py_library, diff --git a/python/py_runtime.bzl b/python/py_runtime.bzl index dad2965cf5..05ae32aa3b 100644 --- a/python/py_runtime.bzl +++ b/python/py_runtime.bzl @@ -15,7 +15,7 @@ """Public entry point for py_runtime.""" load("//python/private:py_runtime_macro.bzl", _starlark_py_runtime = "py_runtime") -load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER", "add_migration_tag") +load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") # buildifier: disable=native-python _py_runtime_impl = _starlark_py_runtime if IS_BAZEL_6_OR_HIGHER else native.py_runtime @@ -39,4 +39,4 @@ def py_runtime(**attrs): if attrs.get("python_version") == "PY2": fail("Python 2 is no longer supported: see https://github.com/bazel-contrib/rules_python/issues/886") - _py_runtime_impl(**add_migration_tag(attrs)) + _py_runtime_impl(**attrs) diff --git a/python/py_test.bzl b/python/py_test.bzl index 1c3f019ad0..5b8ad31725 100644 --- a/python/py_test.bzl +++ b/python/py_test.bzl @@ -16,7 +16,6 @@ load("//python/private:py_test_macro.bzl", _starlark_py_test = "py_test") load("//python/private:register_extension_info.bzl", "register_extension_info") -load("//python/private:util.bzl", "add_migration_tag") # buildifier: disable=native-python _py_test_impl = _starlark_py_test @@ -42,7 +41,7 @@ def py_test(**attrs): fail("Python 2 is no longer supported: https://github.com/bazel-contrib/rules_python/issues/886") # buildifier: disable=native-python - _py_test_impl(**add_migration_tag(attrs)) + _py_test_impl(**attrs) register_extension_info( extension = py_test, From aca0f8036976350201ec3e9f80938b0aa698e606 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:38:26 +0900 Subject: [PATCH 08/19] refactor: remove is_bazel_6_or_higher --- python/BUILD.bazel | 3 - python/private/BUILD.bazel | 2 - python/private/py_cc_link_params_info.bzl | 3 +- python/private/py_info.bzl | 3 +- python/private/py_runtime_info.bzl | 4 +- python/private/util.bzl | 17 ----- python/py_runtime.bzl | 3 +- python/py_runtime_pair.bzl | 4 +- tests/base_rules/py_executable_base_tests.bzl | 20 ----- tests/base_rules/py_test/py_test_tests.bzl | 15 ---- tests/base_rules/util.bzl | 5 -- tests/py_runtime/py_runtime_tests.bzl | 76 ++++--------------- 12 files changed, 20 insertions(+), 135 deletions(-) diff --git a/python/BUILD.bazel b/python/BUILD.bazel index 2d74de0c4a..5fc35f8357 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel @@ -184,7 +184,6 @@ bzl_library( srcs = ["py_runtime.bzl"], deps = [ "//python/private:py_runtime_macro_bzl", - "//python/private:util_bzl", ], ) @@ -192,9 +191,7 @@ bzl_library( name = "py_runtime_pair_bzl", srcs = ["py_runtime_pair.bzl"], deps = [ - "//python/private:bazel_tools_bzl", "//python/private:py_runtime_pair_macro_bzl", - "//python/private:util_bzl", ], ) diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index dfeecf2bd5..ae818e2020 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -440,7 +440,6 @@ bzl_library( deps = [ ":builders_bzl", ":reexports_bzl", - ":util_bzl", ], ) @@ -505,7 +504,6 @@ bzl_library( bzl_library( name = "py_runtime_info_bzl", srcs = ["py_runtime_info.bzl"], - deps = [":util_bzl"], ) bzl_library( diff --git a/python/private/py_cc_link_params_info.bzl b/python/private/py_cc_link_params_info.bzl index 35919a04e2..2fbd255eed 100644 --- a/python/private/py_cc_link_params_info.bzl +++ b/python/private/py_cc_link_params_info.bzl @@ -14,7 +14,6 @@ """Providers for Python rules.""" load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") -load(":util.bzl", "define_bazel_6_provider") def _PyCcLinkParamsInfo_init(cc_info): return { @@ -22,7 +21,7 @@ def _PyCcLinkParamsInfo_init(cc_info): } # buildifier: disable=name-conventions -PyCcLinkParamsInfo, _unused_raw_py_cc_link_params_provider_ctor = define_bazel_6_provider( +PyCcLinkParamsInfo, _unused_raw_py_cc_link_params_provider_ctor = provider( doc = ("Python-wrapper to forward {obj}`CcInfo.linking_context`. This is to " + "allow Python targets to propagate C++ linking information, but " + "without the Python target appearing to be a valid C++ rule dependency"), diff --git a/python/private/py_info.bzl b/python/private/py_info.bzl index e532cc92a2..4059b30c63 100644 --- a/python/private/py_info.bzl +++ b/python/private/py_info.bzl @@ -15,7 +15,6 @@ load(":builders.bzl", "builders") load(":reexports.bzl", "BuiltinPyInfo") -load(":util.bzl", "define_bazel_6_provider") def _VenvSymlinkKind_typedef(): """An enum of types of venv directories. @@ -159,7 +158,7 @@ def _PyInfo_init( "venv_symlinks": venv_symlinks, } -PyInfo, _unused_raw_py_info_ctor = define_bazel_6_provider( +PyInfo, _unused_raw_py_info_ctor = provider( doc = """Encapsulates information provided by the Python rules. Instead of creating this object directly, use {obj}`PyInfoBuilder` and diff --git a/python/private/py_runtime_info.bzl b/python/private/py_runtime_info.bzl index efe14b2c06..af4e7f0596 100644 --- a/python/private/py_runtime_info.bzl +++ b/python/private/py_runtime_info.bzl @@ -13,8 +13,6 @@ # limitations under the License. """Providers for Python rules.""" -load(":util.bzl", "define_bazel_6_provider") - DEFAULT_STUB_SHEBANG = "#!/usr/bin/env python3" _PYTHON_VERSION_VALUES = ["PY2", "PY3"] @@ -124,7 +122,7 @@ def _PyRuntimeInfo_init( "zip_main_template": zip_main_template, } -PyRuntimeInfo, _unused_raw_py_runtime_info_ctor = define_bazel_6_provider( +PyRuntimeInfo, _unused_raw_py_runtime_info_ctor = provider( doc = """Contains information about a Python runtime, as returned by the `py_runtime` rule. diff --git a/python/private/util.bzl b/python/private/util.bzl index 0e9d024b50..baba172baf 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -70,27 +70,10 @@ def add_tag(attrs, tag): else: attrs["tags"] = [tag] -# Helper to make the provider definitions not crash under Bazel 5.4: -# Bazel 5.4 doesn't support the `init` arg of `provider()`, so we have to -# not pass that when using Bazel 5.4. But, not passing the `init` arg -# changes the return value from a two-tuple to a single value, which then -# breaks Bazel 6+ code. -# This isn't actually used under Bazel 5.4, so just stub out the values -# to get past the loading phase. -def define_bazel_6_provider(doc, fields, **kwargs): - """Define a provider, or a stub for pre-Bazel 7.""" - if not IS_BAZEL_6_OR_HIGHER: - return provider("Stub, not used", fields = []), None - return provider(doc = doc, fields = fields, **kwargs) - IS_BAZEL_7_4_OR_HIGHER = hasattr(native, "legacy_globals") IS_BAZEL_7_OR_HIGHER = hasattr(native, "starlark_doc_extract") -# Bazel 5.4 has a bug where every access of testing.ExecutionInfo is a -# different object that isn't equal to any other. This is fixed in bazel 6+. -IS_BAZEL_6_OR_HIGHER = testing.ExecutionInfo == testing.ExecutionInfo - _marker_rule_to_detect_bazel_6_4_or_higher = rule(implementation = lambda ctx: None) # Bazel 6.4 and higher have a bug fix where rule names show up in the str() diff --git a/python/py_runtime.bzl b/python/py_runtime.bzl index 05ae32aa3b..8c3cee2eb7 100644 --- a/python/py_runtime.bzl +++ b/python/py_runtime.bzl @@ -15,10 +15,9 @@ """Public entry point for py_runtime.""" load("//python/private:py_runtime_macro.bzl", _starlark_py_runtime = "py_runtime") -load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") # buildifier: disable=native-python -_py_runtime_impl = _starlark_py_runtime if IS_BAZEL_6_OR_HIGHER else native.py_runtime +_py_runtime_impl = _starlark_py_runtime def py_runtime(**attrs): """Creates an executable Python program. diff --git a/python/py_runtime_pair.bzl b/python/py_runtime_pair.bzl index 26d378fce2..97cc4f5f18 100644 --- a/python/py_runtime_pair.bzl +++ b/python/py_runtime_pair.bzl @@ -14,11 +14,9 @@ """Public entry point for py_runtime_pair.""" -load("@bazel_tools//tools/python:toolchain.bzl", _bazel_tools_impl = "py_runtime_pair") load("//python/private:py_runtime_pair_macro.bzl", _starlark_impl = "py_runtime_pair") -load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") -_py_runtime_pair = _starlark_impl if IS_BAZEL_6_OR_HIGHER else _bazel_tools_impl +_py_runtime_pair = _starlark_impl # NOTE: This doc is copy/pasted from the builtin py_runtime_pair rule so our # doc generator gives useful API docs. diff --git a/tests/base_rules/py_executable_base_tests.bzl b/tests/base_rules/py_executable_base_tests.bzl index 6abfb914da..dfe1e01ddd 100644 --- a/tests/base_rules/py_executable_base_tests.bzl +++ b/tests/base_rules/py_executable_base_tests.bzl @@ -179,11 +179,6 @@ def _test_default_main_can_have_multiple_path_segments_impl(env, target): ) def _test_default_main_must_be_in_srcs(name, config): - # Bazel 5 will crash with a Java stacktrace when the native Python - # rules have an error. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject", @@ -204,11 +199,6 @@ def _test_default_main_must_be_in_srcs_impl(env, target): ) def _test_default_main_cannot_be_ambiguous(name, config): - # Bazel 5 will crash with a Java stacktrace when the native Python - # rules have an error. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject", @@ -251,11 +241,6 @@ def _test_explicit_main_impl(env, target): ) def _test_explicit_main_cannot_be_ambiguous(name, config): - # Bazel 5 will crash with a Java stacktrace when the native Python - # rules have an error. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject", @@ -312,11 +297,6 @@ def _test_files_to_build_impl(env, target): env.expect.that_file(first_default_output).equals(executable) def _test_name_cannot_end_in_py(name, config): - # Bazel 5 will crash with a Java stacktrace when the native Python - # rules have an error. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject.py", diff --git a/tests/base_rules/py_test/py_test_tests.bzl b/tests/base_rules/py_test/py_test_tests.bzl index 1ec1dc428f..c28eec4346 100644 --- a/tests/base_rules/py_test/py_test_tests.bzl +++ b/tests/base_rules/py_test/py_test_tests.bzl @@ -39,14 +39,6 @@ _SKIP_WINDOWS = { _tests = [] def _test_mac_requires_darwin_for_execution(name, config): - # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is - # a different object that isn't equal to any other, which prevents - # rules_testing from detecting it properly and fails with an error. - # This is fixed in Bazel 6+. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return - rt_util.helper_target( config.rule, name = name + "_subject", @@ -74,13 +66,6 @@ def _test_mac_requires_darwin_for_execution_impl(env, target): _tests.append(_test_mac_requires_darwin_for_execution) def _test_non_mac_doesnt_require_darwin_for_execution(name, config): - # Bazel 5.4 has a bug where every access of testing.ExecutionInfo is - # a different object that isn't equal to any other, which prevents - # rules_testing from detecting it properly and fails with an error. - # This is fixed in Bazel 6+. - if not pt_util.is_bazel_6_or_higher(): - rt_util.skip_test(name = name) - return rt_util.helper_target( config.rule, name = name + "_subject", diff --git a/tests/base_rules/util.bzl b/tests/base_rules/util.bzl index a02cafa992..9fb66d7eb3 100644 --- a/tests/base_rules/util.bzl +++ b/tests/base_rules/util.bzl @@ -14,7 +14,6 @@ """Helpers and utilities multiple tests re-use.""" load("@bazel_skylib//lib:structs.bzl", "structs") -load("//python/private:util.bzl", "IS_BAZEL_6_OR_HIGHER") # buildifier: disable=bzl-visibility # Use this with is_windows() WINDOWS_ATTR = {"windows": attr.label(default = "@platforms//os:windows")} @@ -53,9 +52,6 @@ def _struct_with(s, **kwargs): struct_dict.update(kwargs) return struct(**struct_dict) -def _is_bazel_6_or_higher(): - return IS_BAZEL_6_OR_HIGHER - def _is_windows(env): """Tell if the target platform is windows. @@ -72,6 +68,5 @@ def _is_windows(env): util = struct( create_tests = _create_tests, struct_with = _struct_with, - is_bazel_6_or_higher = _is_bazel_6_or_higher, is_windows = _is_windows, ) diff --git a/tests/py_runtime/py_runtime_tests.bzl b/tests/py_runtime/py_runtime_tests.bzl index 3f8fae9254..b8aa1f3fa6 100644 --- a/tests/py_runtime/py_runtime_tests.bzl +++ b/tests/py_runtime/py_runtime_tests.bzl @@ -20,15 +20,10 @@ load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:py_runtime.bzl", "py_runtime") load("//python:py_runtime_info.bzl", "PyRuntimeInfo") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility -load("//tests/base_rules:util.bzl", br_util = "util") load("//tests/support:py_runtime_info_subject.bzl", "py_runtime_info_subject") _tests = [] -_SKIP_TEST = { - "target_compatible_with": ["@platforms//:incompatible"], -} - def _simple_binary_impl(ctx): executable = ctx.actions.declare_file(ctx.label.name) ctx.actions.write(executable, "", is_executable = True) @@ -71,29 +66,19 @@ def _test_bootstrap_template_impl(env, target): _tests.append(_test_bootstrap_template) def _test_cannot_have_both_inbuild_and_system_interpreter(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = { - "interpreter": "fake_interpreter", - "interpreter_path": "/some/path", - } - attr_values = {} - else: - py_runtime_kwargs = { - "interpreter_path": "/some/path", - } - attr_values = _SKIP_TEST rt_util.helper_target( py_runtime, name = name + "_subject", python_version = "PY3", - **py_runtime_kwargs + interpreter = "fake_interpreter", + interpreter_path = "/some/path", ) analysis_test( name = name, target = name + "_subject", impl = _test_cannot_have_both_inbuild_and_system_interpreter_impl, expect_failure = True, - attr_values = attr_values, + attr_values = {}, ) def _test_cannot_have_both_inbuild_and_system_interpreter_impl(env, target): @@ -104,25 +89,19 @@ def _test_cannot_have_both_inbuild_and_system_interpreter_impl(env, target): _tests.append(_test_cannot_have_both_inbuild_and_system_interpreter) def _test_cannot_specify_files_for_system_interpreter(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = {"files": ["foo.txt"]} - attr_values = {} - else: - py_runtime_kwargs = {} - attr_values = _SKIP_TEST rt_util.helper_target( py_runtime, name = name + "_subject", interpreter_path = "/foo", python_version = "PY3", - **py_runtime_kwargs + files = ["foo.txt"], ) analysis_test( name = name, target = name + "_subject", impl = _test_cannot_specify_files_for_system_interpreter_impl, expect_failure = True, - attr_values = attr_values, + attr_values = {}, ) def _test_cannot_specify_files_for_system_interpreter_impl(env, target): @@ -133,21 +112,12 @@ def _test_cannot_specify_files_for_system_interpreter_impl(env, target): _tests.append(_test_cannot_specify_files_for_system_interpreter) def _test_coverage_tool_executable(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = { - "coverage_tool": name + "_coverage_tool", - } - attr_values = {} - else: - py_runtime_kwargs = {} - attr_values = _SKIP_TEST - rt_util.helper_target( py_runtime, name = name + "_subject", python_version = "PY3", interpreter_path = "/bogus", - **py_runtime_kwargs + coverage_tool = name + "_coverage_tool", ) rt_util.helper_target( _simple_binary, @@ -158,7 +128,7 @@ def _test_coverage_tool_executable(name): name = name, target = name + "_subject", impl = _test_coverage_tool_executable_impl, - attr_values = attr_values, + attr_values = {}, ) def _test_coverage_tool_executable_impl(env, target): @@ -173,14 +143,10 @@ def _test_coverage_tool_executable_impl(env, target): _tests.append(_test_coverage_tool_executable) def _test_coverage_tool_plain_files(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = { - "coverage_tool": name + "_coverage_tool", - } - attr_values = {} - else: - py_runtime_kwargs = {} - attr_values = _SKIP_TEST + py_runtime_kwargs = { + "coverage_tool": name + "_coverage_tool", + } + attr_values = {} rt_util.helper_target( py_runtime, name = name + "_subject", @@ -324,14 +290,8 @@ def _test_interpreter_binary_with_single_output_and_runfiles_impl(env, target): _tests.append(_test_interpreter_binary_with_single_output_and_runfiles) def _test_must_have_either_inbuild_or_system_interpreter(name): - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = {} - attr_values = {} - else: - py_runtime_kwargs = { - "interpreter_path": "/some/path", - } - attr_values = _SKIP_TEST + py_runtime_kwargs = {} + attr_values = {} rt_util.helper_target( py_runtime, name = name + "_subject", @@ -375,14 +335,8 @@ def _test_system_interpreter_impl(env, target): _tests.append(_test_system_interpreter) def _test_system_interpreter_must_be_absolute(name): - # Bazel 5.4 will entirely crash when an invalid interpreter_path - # is given. - if br_util.is_bazel_6_or_higher(): - py_runtime_kwargs = {"interpreter_path": "relative/path"} - attr_values = {} - else: - py_runtime_kwargs = {"interpreter_path": "/junk/value/for/bazel5.4"} - attr_values = _SKIP_TEST + py_runtime_kwargs = {"interpreter_path": "relative/path"} + attr_values = {} rt_util.helper_target( py_runtime, name = name + "_subject", From f03cd70f3d86f8160ee634e69403233b9ff79c0e Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:41:18 +0900 Subject: [PATCH 09/19] refactor: remove is_bazel_6_4_or_higher --- python/private/BUILD.bazel | 1 - python/private/python.bzl | 11 +---------- python/private/util.bzl | 13 ------------- 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index ae818e2020..eed50ba57a 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -264,7 +264,6 @@ bzl_library( ":pythons_hub_bzl", ":repo_utils_bzl", ":toolchains_repo_bzl", - ":util_bzl", ":version_bzl", "@bazel_features//:features", ], diff --git a/python/private/python.bzl b/python/private/python.bzl index 6eb8a3742e..faad53fab4 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -29,7 +29,6 @@ load( "sorted_host_platform_names", "sorted_host_platforms", ) -load(":util.bzl", "IS_BAZEL_6_4_OR_HIGHER") load(":version.bzl", "version") def parse_modules(*, module_ctx, logger, _fail = fail): @@ -932,14 +931,6 @@ def _create_toolchain_attrs_struct(*, tag = None, python_version = None, toolcha ignore_root_user_error = getattr(tag, "ignore_root_user_error", True), ) -def _get_bazel_version_specific_kwargs(): - kwargs = {} - - if IS_BAZEL_6_4_OR_HIGHER: - kwargs["environ"] = ["RULES_PYTHON_BZLMOD_DEBUG"] - - return kwargs - _defaults = tag_class( doc = """Tag class to specify the default Python version.""", attrs = { @@ -1356,7 +1347,7 @@ python = module_extension( "single_version_platform_override": _single_version_platform_override, "toolchain": _toolchain, }, - **_get_bazel_version_specific_kwargs() + environ = ["RULES_PYTHON_BZLMOD_DEBUG"], ) _DEBUG_BUILD_CONTENT = """ diff --git a/python/private/util.bzl b/python/private/util.bzl index baba172baf..70fa1a2851 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -73,16 +73,3 @@ def add_tag(attrs, tag): IS_BAZEL_7_4_OR_HIGHER = hasattr(native, "legacy_globals") IS_BAZEL_7_OR_HIGHER = hasattr(native, "starlark_doc_extract") - -_marker_rule_to_detect_bazel_6_4_or_higher = rule(implementation = lambda ctx: None) - -# Bazel 6.4 and higher have a bug fix where rule names show up in the str() -# of a rule. See -# https://github.com/bazelbuild/bazel/commit/002490b9a2376f0b2ea4a37102c5e94fc50a65ba -# https://github.com/bazelbuild/bazel/commit/443cbcb641e17f7337ccfdecdfa5e69bc16cae55 -# This technique is done instead of using native.bazel_version because, -# under stardoc, the native.bazel_version attribute is entirely missing, which -# prevents doc generation from being able to correctly generate docs. -IS_BAZEL_6_4_OR_HIGHER = "_marker_rule_to_detect_bazel_6_4_or_higher" in str( - _marker_rule_to_detect_bazel_6_4_or_higher, -) From 290484d0aaebe8c450e6a9efa7e60e2c460974fc Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:49:28 +0900 Subject: [PATCH 10/19] refactor: remove is_bazel_7_or_greater --- docs/BUILD.bazel | 7 +-- python/private/BUILD.bazel | 1 - python/private/py_runtime_pair_rule.bzl | 3 +- python/private/py_runtime_rule.bzl | 4 -- python/private/util.bzl | 2 - .../tests/proto_to_markdown/BUILD.bazel | 2 - sphinxdocs/tests/sphinx_docs/BUILD.bazel | 3 +- sphinxdocs/tests/sphinx_stardoc/BUILD.bazel | 3 +- tests/base_rules/py_executable_base_tests.bzl | 18 +++---- tests/bootstrap_impls/a/b/c/BUILD.bazel | 3 +- .../transition/multi_version_tests.bzl | 50 +++++++------------ .../exec_toolchain_matching_tests.bzl | 8 ++- .../py_runtime_info/py_runtime_info_tests.bzl | 7 +-- .../runtime_env_toolchain_tests.bzl | 22 ++------ tests/support/support.bzl | 3 +- 15 files changed, 43 insertions(+), 93 deletions(-) diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index b6c48b0539..c36ed5722a 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -15,7 +15,6 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@dev_pip//:requirements.bzl", "requirement") load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//python/uv:lock.bzl", "lock") # buildifier: disable=bzl-visibility load("//sphinxdocs:readthedocs.bzl", "readthedocs_install") load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") @@ -107,6 +106,7 @@ sphinx_stardocs( "//python/cc:py_cc_toolchain_bzl", "//python/cc:py_cc_toolchain_info_bzl", "//python/entry_points:py_console_script_binary_bzl", + "//python/extensions:python_bzl", "//python/local_toolchains:repos_bzl", "//python/private:attr_builders_bzl", "//python/private:builders_util_bzl", @@ -128,12 +128,9 @@ sphinx_stardocs( "//python/uv:uv_toolchain_bzl", "//python/uv:uv_toolchain_info_bzl", ] + ([ - # Bazel 6 + Stardoc isn't able to parse something about the python bzlmod extension - "//python/extensions:python_bzl", - ] if IS_BAZEL_7_OR_HIGHER else []) + ([ # This depends on @pythons_hub, which is only created under bzlmod, "//python/extensions:pip_bzl", - ] if IS_BAZEL_7_OR_HIGHER and BZLMOD_ENABLED else []), + ] if BZLMOD_ENABLED else []), prefix = "api/rules_python/", tags = ["docs"], target_compatible_with = _TARGET_COMPATIBLE_WITH, diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index eed50ba57a..a0ee2d8e47 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -534,7 +534,6 @@ bzl_library( ":py_runtime_info_bzl", ":reexports_bzl", ":rule_builders_bzl", - ":util_bzl", "@bazel_skylib//lib:dicts", "@bazel_skylib//lib:paths", "@bazel_skylib//rules:common_settings", diff --git a/python/private/py_runtime_pair_rule.bzl b/python/private/py_runtime_pair_rule.bzl index 61cbdcd6f4..203e5d4df7 100644 --- a/python/private/py_runtime_pair_rule.bzl +++ b/python/private/py_runtime_pair_rule.bzl @@ -19,7 +19,6 @@ load("//python:py_runtime_info.bzl", "PyRuntimeInfo") load(":common_labels.bzl", "labels") load(":flags.bzl", "read_possibly_native_flag") load(":reexports.bzl", "BuiltinPyRuntimeInfo") -load(":util.bzl", "IS_BAZEL_7_OR_HIGHER") def _py_runtime_pair_impl(ctx): if ctx.attr.py2_runtime != None: @@ -58,7 +57,7 @@ def _get_py_runtime_info(target): # 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) or BuiltinPyRuntimeInfo == None: + if (PyRuntimeInfo in target) or BuiltinPyRuntimeInfo == None: return target[PyRuntimeInfo] else: return target[BuiltinPyRuntimeInfo] diff --git a/python/private/py_runtime_rule.bzl b/python/private/py_runtime_rule.bzl index ba1a390ef3..185d0a6d24 100644 --- a/python/private/py_runtime_rule.bzl +++ b/python/private/py_runtime_rule.bzl @@ -21,7 +21,6 @@ load(":flags.bzl", "FreeThreadedFlag") load(":py_internal.bzl", "py_internal") load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo") load(":reexports.bzl", "BuiltinPyRuntimeInfo") -load(":util.bzl", "IS_BAZEL_7_OR_HIGHER") _py_builtins = py_internal @@ -133,9 +132,6 @@ def _py_runtime_impl(ctx): supports_build_time_venv = ctx.attr.supports_build_time_venv, )) - if not IS_BAZEL_7_OR_HIGHER: - builtin_py_runtime_info_kwargs.pop("bootstrap_template") - providers = [ PyRuntimeInfo(**py_runtime_info_kwargs), DefaultInfo( diff --git a/python/private/util.bzl b/python/private/util.bzl index 70fa1a2851..f37af80320 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -71,5 +71,3 @@ def add_tag(attrs, tag): attrs["tags"] = [tag] IS_BAZEL_7_4_OR_HIGHER = hasattr(native, "legacy_globals") - -IS_BAZEL_7_OR_HIGHER = hasattr(native, "starlark_doc_extract") diff --git a/sphinxdocs/tests/proto_to_markdown/BUILD.bazel b/sphinxdocs/tests/proto_to_markdown/BUILD.bazel index 09f537472c..2964785eed 100644 --- a/sphinxdocs/tests/proto_to_markdown/BUILD.bazel +++ b/sphinxdocs/tests/proto_to_markdown/BUILD.bazel @@ -13,12 +13,10 @@ # limitations under the License. load("//python:py_test.bzl", "py_test") -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility py_test( name = "proto_to_markdown_test", srcs = ["proto_to_markdown_test.py"], - target_compatible_with = [] if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"], deps = [ "//sphinxdocs/private:proto_to_markdown_lib", "@dev_pip//absl_py", diff --git a/sphinxdocs/tests/sphinx_docs/BUILD.bazel b/sphinxdocs/tests/sphinx_docs/BUILD.bazel index f9c82967c1..33b98ec585 100644 --- a/sphinxdocs/tests/sphinx_docs/BUILD.bazel +++ b/sphinxdocs/tests/sphinx_docs/BUILD.bazel @@ -1,5 +1,4 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") load(":defs.bzl", "gen_directory") @@ -13,7 +12,7 @@ _TARGET_COMPATIBLE_WITH = select({ "@platforms//os:linux": [], "@platforms//os:macos": [], "//conditions:default": ["@platforms//:incompatible"], -}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] +}) sphinx_docs( name = "docs", diff --git a/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel b/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel index e3a68ea225..af9af30886 100644 --- a/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel +++ b/sphinxdocs/tests/sphinx_stardoc/BUILD.bazel @@ -1,7 +1,6 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//python:py_test.bzl", "py_test") -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs") load("//sphinxdocs:sphinx_stardoc.bzl", "sphinx_stardoc", "sphinx_stardocs") @@ -15,7 +14,7 @@ _TARGET_COMPATIBLE_WITH = select({ "@platforms//os:linux": [], "@platforms//os:macos": [], "//conditions:default": ["@platforms//:incompatible"], -}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] +}) sphinx_docs( name = "docs", diff --git a/tests/base_rules/py_executable_base_tests.bzl b/tests/base_rules/py_executable_base_tests.bzl index dfe1e01ddd..c7723be54a 100644 --- a/tests/base_rules/py_executable_base_tests.bzl +++ b/tests/base_rules/py_executable_base_tests.bzl @@ -20,7 +20,6 @@ load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:py_executable_info.bzl", "PyExecutableInfo") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility load("//python/private:reexports.bzl", "BuiltinPyRuntimeInfo") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/base_rules:base_tests.bzl", "create_base_tests") load("//tests/base_rules:util.bzl", "WINDOWS_ATTR", pt_util = "util") load("//tests/support:py_executable_info_subject.bzl", "PyExecutableInfoSubject") @@ -286,15 +285,14 @@ def _test_files_to_build_impl(env, target): "{package}/{test_name}_subject.py", ]) - if IS_BAZEL_7_OR_HIGHER: - # As of Bazel 7, the first default output is the executable, so - # verify that is the case. rules_testing - # DepsetFileSubject.contains_exactly doesn't provide an in_order() - # call, nor access to the underlying depset, so we have to do things - # manually. - first_default_output = target[DefaultInfo].files.to_list()[0] - executable = target[DefaultInfo].files_to_run.executable - env.expect.that_file(first_default_output).equals(executable) + # As of Bazel 7, the first default output is the executable, so + # verify that is the case. rules_testing + # DepsetFileSubject.contains_exactly doesn't provide an in_order() + # call, nor access to the underlying depset, so we have to do things + # manually. + first_default_output = target[DefaultInfo].files.to_list()[0] + executable = target[DefaultInfo].files_to_run.executable + env.expect.that_file(first_default_output).equals(executable) def _test_name_cannot_end_in_py(name, config): rt_util.helper_target( diff --git a/tests/bootstrap_impls/a/b/c/BUILD.bazel b/tests/bootstrap_impls/a/b/c/BUILD.bazel index 1659ef25bc..1c4b1e7b6b 100644 --- a/tests/bootstrap_impls/a/b/c/BUILD.bazel +++ b/tests/bootstrap_impls/a/b/c/BUILD.bazel @@ -1,10 +1,9 @@ -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:py_reconfig.bzl", "py_reconfig_test") _SUPPORTS_BOOTSTRAP_SCRIPT = select({ "@platforms//os:windows": ["@platforms//:incompatible"], "//conditions:default": [], -}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] +}) py_reconfig_test( name = "nested_dir_test", diff --git a/tests/config_settings/transition/multi_version_tests.bzl b/tests/config_settings/transition/multi_version_tests.bzl index 93f6efd728..b2564a3fb3 100644 --- a/tests/config_settings/transition/multi_version_tests.bzl +++ b/tests/config_settings/transition/multi_version_tests.bzl @@ -16,12 +16,11 @@ load("@pythons_hub//:versions.bzl", "DEFAULT_PYTHON_VERSION") load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") -load("@rules_testing//lib:util.bzl", "TestingAspectInfo", rt_util = "util") +load("@rules_testing//lib:util.bzl", rt_util = "util") load("//python:py_binary.bzl", "py_binary") load("//python:py_info.bzl", "PyInfo") load("//python:py_test.bzl", "py_test") load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "CC_TOOLCHAIN") # NOTE @aignas 2024-06-04: we are using here something that is registered in the MODULE.Bazel @@ -106,20 +105,15 @@ def _test_py_binary_windows_build_python_zip_false(name): def _test_py_binary_windows_build_python_zip_false_impl(env, target): default_outputs = env.expect.that_target(target).default_outputs() - if IS_BAZEL_7_OR_HIGHER: - # TODO: These outputs aren't correct. The outputs shouldn't - # have the "_" prefix on them (those are coming from the underlying - # wrapped binary). - env.expect.that_target(target).default_outputs().contains_exactly([ - "{package}/{test_name}_subject.exe", - "{package}/{test_name}_subject", - "{package}/{test_name}_subject.py", - ]) - else: - inner_exe = target[TestingAspectInfo].attrs.target[DefaultInfo].files_to_run.executable - default_outputs.contains_at_least([ - inner_exe.short_path, - ]) + + # TODO: These outputs aren't correct. The outputs shouldn't + # have the "_" prefix on them (those are coming from the underlying + # wrapped binary). + default_outputs.contains_exactly([ + "{package}/{test_name}_subject.exe", + "{package}/{test_name}_subject", + "{package}/{test_name}_subject.py", + ]) _tests.append(_test_py_binary_windows_build_python_zip_false) @@ -132,21 +126,15 @@ def _test_py_binary_windows_build_python_zip_true(name): def _test_py_binary_windows_build_python_zip_true_impl(env, target): default_outputs = env.expect.that_target(target).default_outputs() - if IS_BAZEL_7_OR_HIGHER: - # TODO: These outputs aren't correct. The outputs shouldn't - # have the "_" prefix on them (those are coming from the underlying - # wrapped binary). - default_outputs.contains_exactly([ - "{package}/{test_name}_subject.exe", - "{package}/{test_name}_subject.py", - "{package}/{test_name}_subject.zip", - ]) - else: - inner_exe = target[TestingAspectInfo].attrs.target[DefaultInfo].files_to_run.executable - default_outputs.contains_at_least([ - "{package}/{test_name}_subject.zip", - inner_exe.short_path, - ]) + + # TODO: These outputs aren't correct. The outputs shouldn't + # have the "_" prefix on them (those are coming from the underlying + # wrapped binary). + default_outputs.contains_exactly([ + "{package}/{test_name}_subject.exe", + "{package}/{test_name}_subject.py", + "{package}/{test_name}_subject.zip", + ]) _tests.append(_test_py_binary_windows_build_python_zip_true) diff --git a/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl b/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl index 43a9717314..b3ff294b6f 100644 --- a/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl +++ b/tests/exec_toolchain_matching/exec_toolchain_matching_tests.bzl @@ -20,7 +20,6 @@ load("//python:py_runtime.bzl", "py_runtime") load("//python:py_runtime_pair.bzl", "py_runtime_pair") load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "LINUX", "MAC") _LookupInfo = provider() # buildifier: disable=provider-params @@ -143,11 +142,10 @@ def _test_exec_matches_target_python_version_impl(env, target): env.expect.that_str(target_runtime.interpreter_path).equals("/linux/python3.12") env.expect.that_str(exec_runtime.interpreter_path).equals("/mac/python3.12") - if IS_BAZEL_7_OR_HIGHER: - target_version = target_runtime.interpreter_version_info - exec_version = exec_runtime.interpreter_version_info + target_version = target_runtime.interpreter_version_info + exec_version = exec_runtime.interpreter_version_info - env.expect.that_bool(target_version == exec_version) + env.expect.that_bool(target_version == exec_version) def exec_toolchain_matching_test_suite(name): test_suite(name = name, tests = _tests) diff --git a/tests/py_runtime_info/py_runtime_info_tests.bzl b/tests/py_runtime_info/py_runtime_info_tests.bzl index 9acf541683..a44fb60c2f 100644 --- a/tests/py_runtime_info/py_runtime_info_tests.bzl +++ b/tests/py_runtime_info/py_runtime_info_tests.bzl @@ -16,18 +16,13 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test") load("@rules_testing//lib:test_suite.bzl", "test_suite") load("//python:py_runtime_info.bzl", "PyRuntimeInfo") -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility def _create_py_runtime_info_without_interpreter_version_info_impl(ctx): - kwargs = {} - if IS_BAZEL_7_OR_HIGHER: - kwargs["bootstrap_template"] = ctx.attr.bootstrap_template - return [PyRuntimeInfo( interpreter = ctx.file.interpreter, files = depset(ctx.files.files), python_version = "PY3", - **kwargs + bootstrap_template = ctx.attr.bootstrap_template, )] _create_py_runtime_info_without_interpreter_version_info = rule( diff --git a/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl b/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl index aa4d1c793b..527448ebbc 100644 --- a/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl +++ b/tests/runtime_env_toolchain/runtime_env_toolchain_tests.bzl @@ -24,7 +24,6 @@ load( "PY_CC_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE", ) # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility load("//tests/support:support.bzl", "CC_TOOLCHAIN") _LookupInfo = provider() # buildifier: disable=provider-params @@ -55,25 +54,14 @@ def _test_runtime_env_toolchain_matches(name): name = name + "_subject", ) extra_toolchains = [ + # We have to add a cc toolchain because py_cc toolchain depends on it. + # However, that package also defines a different fake py_cc toolchain we + # don't want to use, so we need to ensure the runtime_env toolchain has + # higher precendence. + CC_TOOLCHAIN, str(Label("//python/runtime_env_toolchains:all")), ] - # We have to add a cc toolchain because py_cc toolchain depends on it. - # However, that package also defines a different fake py_cc toolchain we - # don't want to use, so we need to ensure the runtime_env toolchain has - # higher precendence. - # However, Bazel 6 and Bazel 7 process --extra_toolchains in different - # orders: - # * Bazel 6 goes left to right - # * Bazel 7 goes right to left - # We could just put our preferred toolchain before *and* after - # the undesired toolchain... - # However, Bazel 7 has a bug where *duplicate* entries are ignored, - # and only the *first* entry is respected. - if IS_BAZEL_7_OR_HIGHER: - extra_toolchains.insert(0, CC_TOOLCHAIN) - else: - extra_toolchains.append(CC_TOOLCHAIN) analysis_test( name = name, impl = _test_runtime_env_toolchain_matches_impl, diff --git a/tests/support/support.bzl b/tests/support/support.bzl index 37d3488316..96c6ad902a 100644 --- a/tests/support/support.bzl +++ b/tests/support/support.bzl @@ -20,7 +20,6 @@ # places. load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility -load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility MAC = Label("//tests/support:mac") MAC_X86_64 = Label("//tests/support:mac_x86_64") @@ -40,7 +39,7 @@ CUSTOM_RUNTIME = str(Label("//tests/support:custom_runtime")) SUPPORTS_BOOTSTRAP_SCRIPT = select({ "@platforms//os:windows": ["@platforms//:incompatible"], "//conditions:default": [], -}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"] +}) SUPPORTS_BZLMOD_UNIXY = select({ "@platforms//os:windows": ["@platforms//:incompatible"], From b6f9c600359cca5fc31a0a4339dfa9c128a5ab1e Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:52:34 +0900 Subject: [PATCH 11/19] remove: is_bazel_7_4_or_greater --- python/private/BUILD.bazel | 6 ---- python/private/glob_excludes.bzl | 32 ------------------- .../private/hermetic_runtime_repo_setup.bzl | 3 +- python/private/pypi/BUILD.bazel | 1 - python/private/pypi/deps.bzl | 2 +- python/private/pypi/whl_library_targets.bzl | 3 +- python/private/util.bzl | 2 -- .../whl_library_targets_tests.bzl | 3 +- 8 files changed, 4 insertions(+), 48 deletions(-) delete mode 100644 python/private/glob_excludes.bzl diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index a0ee2d8e47..1bcd0f678f 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel @@ -194,12 +194,6 @@ bzl_library( srcs = ["full_version.bzl"], ) -bzl_library( - name = "glob_excludes_bzl", - srcs = ["glob_excludes.bzl"], - deps = [":util_bzl"], -) - bzl_library( name = "internal_config_repo_bzl", srcs = ["internal_config_repo.bzl"], diff --git a/python/private/glob_excludes.bzl b/python/private/glob_excludes.bzl deleted file mode 100644 index c98afe0ae2..0000000000 --- a/python/private/glob_excludes.bzl +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2024 The Bazel Authors. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"Utilities for glob exclusions." - -load(":util.bzl", "IS_BAZEL_7_4_OR_HIGHER") - -def _version_dependent_exclusions(): - """Returns glob exclusions that are sensitive to Bazel version. - - Returns: - a list of glob exclusion patterns - """ - if IS_BAZEL_7_4_OR_HIGHER: - return [] - else: - return ["**/* *"] - -glob_excludes = struct( - version_dependent_exclusions = _version_dependent_exclusions, -) diff --git a/python/private/hermetic_runtime_repo_setup.bzl b/python/private/hermetic_runtime_repo_setup.bzl index a35ce8ae7d..46495e49c0 100644 --- a/python/private/hermetic_runtime_repo_setup.bzl +++ b/python/private/hermetic_runtime_repo_setup.bzl @@ -18,7 +18,6 @@ load("@rules_cc//cc:cc_library.bzl", "cc_library") load("//python:py_runtime.bzl", "py_runtime") load("//python:py_runtime_pair.bzl", "py_runtime_pair") load("//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain") -load(":glob_excludes.bzl", "glob_excludes") load(":py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain") load(":version.bzl", "version") @@ -82,7 +81,7 @@ def define_hermetic_runtime_toolchain_impl( "lib/python{major}.{minor}*/**/tests/**".format(**version_dict), # During pyc creation, temp files named *.pyc.NNN are created "**/__pycache__/*.pyc.*", - ] + glob_excludes.version_dependent_exclusions() + extra_files_glob_exclude, + ] + extra_files_glob_exclude, ), ) cc_import( diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel index 0d2f73fb0b..095f85c8aa 100644 --- a/python/private/pypi/BUILD.bazel +++ b/python/private/pypi/BUILD.bazel @@ -74,7 +74,6 @@ bzl_library( srcs = ["deps.bzl"], deps = [ "//python/private:bazel_tools_bzl", - "//python/private:glob_excludes_bzl", ], ) diff --git a/python/private/pypi/deps.bzl b/python/private/pypi/deps.bzl index 73b30c69ee..039c927016 100644 --- a/python/private/pypi/deps.bzl +++ b/python/private/pypi/deps.bzl @@ -115,7 +115,7 @@ py_library( "**/*.dist-info/RECORD", "BUILD", "WORKSPACE", - ] + glob_excludes.version_dependent_exclusions()), + ]), # This makes this directory a top-level in the python import # search path for anything that depends on this. imports = ["."], diff --git a/python/private/pypi/whl_library_targets.bzl b/python/private/pypi/whl_library_targets.bzl index a443d7858d..89c1d348b3 100644 --- a/python/private/pypi/whl_library_targets.bzl +++ b/python/private/pypi/whl_library_targets.bzl @@ -17,7 +17,6 @@ load("@bazel_skylib//rules:copy_file.bzl", "copy_file") load("//python:py_binary.bzl", "py_binary") load("//python:py_library.bzl", "py_library") -load("//python/private:glob_excludes.bzl", "glob_excludes") load("//python/private:normalize_name.bzl", "normalize_name") load(":env_marker_setting.bzl", "env_marker_setting") load( @@ -339,7 +338,7 @@ def whl_library_targets( # of generated files produced when wheels are installed. The file is ignored to avoid # Bazel caching issues. "**/*.dist-info/RECORD", - ] + glob_excludes.version_dependent_exclusions() + ] for item in data_exclude: if item not in _data_exclude: _data_exclude.append(item) diff --git a/python/private/util.bzl b/python/private/util.bzl index f37af80320..d3053fe626 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -69,5 +69,3 @@ def add_tag(attrs, tag): attrs["tags"] = tags + [tag] else: attrs["tags"] = [tag] - -IS_BAZEL_7_4_OR_HIGHER = hasattr(native, "legacy_globals") diff --git a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl index 6af25a4e75..615358f35d 100644 --- a/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl +++ b/tests/pypi/whl_library_targets/whl_library_targets_tests.bzl @@ -15,7 +15,6 @@ "" load("@rules_testing//lib:test_suite.bzl", "test_suite") -load("//python/private:glob_excludes.bzl", "glob_excludes") # buildifier: disable=bzl-visibility load( "//python/private/pypi:whl_library_targets.bzl", "whl_library_targets", @@ -275,7 +274,7 @@ def _test_whl_and_library_deps_from_requires(env): "**/*.pyc", "**/*.pyc.*", "**/*.dist-info/RECORD", - ] + glob_excludes.version_dependent_exclusions(), + ], ), # pyi call _glob_call(["site-packages/**/*.pyi"], allow_empty = True), From 2014ca43b6093569ee4a3545d7267013162c3759 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:53:19 +0900 Subject: [PATCH 12/19] remove bazel 6 test runner --- tests/integration/integration_test.bzl | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/integration/integration_test.bzl b/tests/integration/integration_test.bzl index c437953319..90cc4a3fb7 100644 --- a/tests/integration/integration_test.bzl +++ b/tests/integration/integration_test.bzl @@ -35,12 +35,6 @@ def _test_runner(*, name, bazel_version, py_main, bzlmod, gazelle_plugin): ) return test_runner - if bazel_version.startswith("6") and not bzlmod: - if gazelle_plugin: - return "//tests/integration:bazel_6_4_workspace_test_runner_gazelle_plugin" - else: - return "//tests/integration:bazel_6_4_workspace_test_runner" - if bzlmod and gazelle_plugin: return "//tests/integration:test_runner_gazelle_plugin" elif bzlmod: From bb9eef141d965020968b3ad1eb31c32d4438011e Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:55:13 +0900 Subject: [PATCH 13/19] fix: pipstar env var is now respected --- python/private/internal_config_repo.bzl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/private/internal_config_repo.bzl b/python/private/internal_config_repo.bzl index 174c6ec543..5788810fcd 100644 --- a/python/private/internal_config_repo.bzl +++ b/python/private/internal_config_repo.bzl @@ -139,8 +139,7 @@ def _internal_config_repo_impl(rctx): internal_config_repo = repository_rule( implementation = _internal_config_repo_impl, configure = True, - # TODO @aignas 2025-09-27: PIPSTAR env var bugfix - environ = [], + environ = [_ENABLE_PIPSTAR_ENVVAR_NAME], attrs = { "transition_setting_generators": attr.string_list_dict(), "transition_settings": attr.string_list(), From 0db4c99859b4f9bb3c6d758df69fc15a16cab4a3 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:55:27 +0900 Subject: [PATCH 14/19] chore: drop bazel 5 --- python/private/internal_config_repo.bzl | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/python/private/internal_config_repo.bzl b/python/private/internal_config_repo.bzl index 5788810fcd..109e68a8a1 100644 --- a/python/private/internal_config_repo.bzl +++ b/python/private/internal_config_repo.bzl @@ -106,16 +106,9 @@ def _internal_config_repo_impl(rctx): shim_content = _PY_INTERNAL_SHIM py_internal_dep = '"@rules_python//tools/build_defs/python/private:py_internal_renamed_bzl"' - # Bazel 5 doesn't support repository visibility, so just use public - # as a stand-in - if native.bazel_version.startswith("5."): - visibility = "//visibility:public" - else: - visibility = "@rules_python//:__subpackages__" - rctx.file("BUILD", ROOT_BUILD_TEMPLATE.format( py_internal_dep = py_internal_dep, - visibility = visibility, + visibility = "@rules_python//:__subpackages__", )) rctx.file("py_internal.bzl", shim_content) From da0f30206f750d01defff9a35808e642e1428f74 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 21:57:45 +0900 Subject: [PATCH 15/19] chore: add an override for bzlmod example --- examples/bzlmod/other_module/MODULE.bazel | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/examples/bzlmod/other_module/MODULE.bazel b/examples/bzlmod/other_module/MODULE.bazel index f9d6706120..7b88bd73ff 100644 --- a/examples/bzlmod/other_module/MODULE.bazel +++ b/examples/bzlmod/other_module/MODULE.bazel @@ -5,20 +5,10 @@ module( # This module is using the same version of rules_python # that the parent module uses. bazel_dep(name = "rules_python", version = "") - -# The story behind this commented out override: -# This override is necessary to generate/update the requirements file -# for this module. This is because running it via the outer -# module doesn't work -- the `requirements.update` target can't find -# the correct file to update. -# Running in the submodule itself works, but submodules using overrides -# is considered an error until Bazel 6.3, which prevents the outer module -# from depending on this module. -# So until 6.3 and higher is the minimum, we leave this commented out. -# local_path_override( -# module_name = "rules_python", -# path = "../../..", -# ) +local_path_override( + module_name = "rules_python", + path = "../../..", +) PYTHON_NAME_39 = "python_3_9" From 33f7c9104a3212bc4b59bb4cdefd17167aafd839 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 22:01:44 +0900 Subject: [PATCH 16/19] remove extra bazel 6 code --- python/private/py_package.bzl | 9 ++------- python/private/py_runtime_rule.bzl | 6 +----- python/private/python_register_toolchains.bzl | 15 --------------- 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/python/private/py_package.bzl b/python/private/py_package.bzl index adf2b6deef..d23276a53b 100644 --- a/python/private/py_package.bzl +++ b/python/private/py_package.bzl @@ -41,13 +41,8 @@ def _py_package_impl(ctx): py_info.merge_target(dep) py_info = py_info.build() inputs.add(py_info.transitive_sources) - - # Remove conditional once Bazel 6 support dropped. - if hasattr(py_info, "transitive_pyc_files"): - inputs.add(py_info.transitive_pyc_files) - - if hasattr(py_info, "transitive_pyi_files"): - inputs.add(py_info.transitive_pyi_files) + inputs.add(py_info.transitive_pyc_files) + inputs.add(py_info.transitive_pyi_files) inputs = inputs.build() diff --git a/python/private/py_runtime_rule.bzl b/python/private/py_runtime_rule.bzl index 185d0a6d24..5020d7ad9b 100644 --- a/python/private/py_runtime_rule.bzl +++ b/python/private/py_runtime_rule.bzl @@ -384,11 +384,7 @@ The {obj}`PyRuntimeInfo.zip_main_template` field. ) def _is_singleton_depset(files): - # Bazel 6 doesn't have this helper to optimize detecting singleton depsets. - if _py_builtins: - return _py_builtins.is_singleton_depset(files) - else: - return len(files.to_list()) == 1 + return _py_builtins.is_singleton_depset(files) def _interpreter_version_info_from_version_str(version_str): parts = version_str.split(".") diff --git a/python/private/python_register_toolchains.bzl b/python/private/python_register_toolchains.bzl index 2e0748deb0..9e75c41978 100644 --- a/python/private/python_register_toolchains.bzl +++ b/python/private/python_register_toolchains.bzl @@ -97,21 +97,6 @@ def python_register_toolchains( toolchain_repo_name = "{name}_toolchains".format(name = name) - # When using unreleased Bazel versions, the version is an empty string - if native.bazel_version: - bazel_major = int(native.bazel_version.split(".")[0]) - if bazel_major < 6: - if register_coverage_tool: - # buildifier: disable=print - print(( - "WARNING: ignoring register_coverage_tool=True when " + - "registering @{name}: Bazel 6+ required, got {version}" - ).format( - name = name, - version = native.bazel_version, - )) - register_coverage_tool = False - # list[str] of the platform names that were used loaded_platforms = [] From 170fe3b89e8e1ae338def47a3f14b7b99a37654e Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 22:07:16 +0900 Subject: [PATCH 17/19] Revert "refactor: use rules_shell runfiles lib" This reverts commit 1c564f685d966c0c43b860a239ef21eb365b7ded. --- python/private/interpreter.bzl | 2 +- tests/bootstrap_impls/BUILD.bazel | 2 +- tests/support/sh_py_run_test.bzl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/python/private/interpreter.bzl b/python/private/interpreter.bzl index ca65262bfc..c66d3dc21e 100644 --- a/python/private/interpreter.bzl +++ b/python/private/interpreter.bzl @@ -72,7 +72,7 @@ interpreter_binary = rule( mandatory = True, ), "_bash_runfiles": attr.label( - default = "@rules_shell//shell/runfiles", + default = "@bazel_tools//tools/bash/runfiles", ), "_template": attr.label( default = "//python/private:interpreter_tmpl.sh", diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel index d8967f3000..c3d44df240 100644 --- a/tests/bootstrap_impls/BUILD.bazel +++ b/tests/bootstrap_impls/BUILD.bazel @@ -44,7 +44,7 @@ sh_test( }, target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT, deps = [ - "@rules_shell//shell/runfiles", + "@bazel_tools//tools/bash/runfiles", ], ) diff --git a/tests/support/sh_py_run_test.bzl b/tests/support/sh_py_run_test.bzl index 2d26e890a2..7dff6673ce 100644 --- a/tests/support/sh_py_run_test.bzl +++ b/tests/support/sh_py_run_test.bzl @@ -37,7 +37,7 @@ def sh_py_run_test(*, name, sh_src, py_src, **kwargs): srcs = [sh_src], data = [bin_name], deps = [ - "@rules_shell//shell/runfiles", + "@bazel_tools//tools/bash/runfiles", ], env = { "BIN_RLOCATION": "$(rlocationpaths {})".format(bin_name), From 13f2197fbba3c08876f474fcdf14e88df70bf216 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 22:15:03 +0900 Subject: [PATCH 18/19] add missing bzl_library deps --- python/private/pypi/BUILD.bazel | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel index 095f85c8aa..b9650c8152 100644 --- a/python/private/pypi/BUILD.bazel +++ b/python/private/pypi/BUILD.bazel @@ -125,6 +125,7 @@ bzl_library( "@bazel_features//:features", "@pythons_hub//:interpreters_bzl", "@pythons_hub//:versions_bzl", + "@rules_python_internal//:rules_python_config_bzl", ], ) @@ -438,6 +439,7 @@ bzl_library( "//python/private:envsubst_bzl", "//python/private:is_standalone_interpreter_bzl", "//python/private:repo_utils_bzl", + "@rules_python_internal//:rules_python_config_bzl", ], ) From d3fce3b5e92b4c6f64dc2527af3a9e80d96c3bb9 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sat, 27 Sep 2025 22:18:15 +0900 Subject: [PATCH 19/19] remove glob_excludes.bzl --- python/private/pypi/deps.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/python/private/pypi/deps.bzl b/python/private/pypi/deps.bzl index 039c927016..5379343d62 100644 --- a/python/private/pypi/deps.bzl +++ b/python/private/pypi/deps.bzl @@ -101,7 +101,6 @@ _GENERIC_WHEEL = """\ package(default_visibility = ["//visibility:public"]) load("@rules_python//python:py_library.bzl", "py_library") -load("@rules_python//python/private:glob_excludes.bzl", "glob_excludes") py_library( name = "lib",