Permit toggling the toolchain type migrations with --repo_env (#168) The value for the toolchain migration features now comes from the `BAZEL_INCOMPATIBLE_TOOLCHAIN_TYPES_IN_RULES` environment variable, which can be set with `--repo_env` and defaults to `0` for now. The default will be flipped based on the Bazel version, with the variable remaining available as an escape hatch, e.g. for testing development builds of Bazel or if the migration turns out to be more breaking than expected. The value is recorded in the existing `bazel_features_version` repo, which now also reruns when the variable changes.
diff --git a/features.bzl b/features.bzl index e8a613d..4c4eb65 100644 --- a/features.bzl +++ b/features.bzl
@@ -1,7 +1,12 @@ """Defines all the features this module supports detecting.""" load("@bazel_features_globals//:globals.bzl", "globals") -load("//private:util.bzl", "ge", "ge_same_major", "gt", "lt") +load("//private:util.bzl", "ge", "ge_same_major", "gt", "incompatible", "lt") + +# Whether the toolchain types in @bazel_tools are aliases of the types in rules_cc, rules_java and +# rules_python. The default will be flipped based on the Bazel version, with +# --repo_env=BAZEL_INCOMPATIBLE_TOOLCHAIN_TYPES_IN_RULES=0|1 as an escape hatch. +_TOOLCHAIN_TYPES_IN_RULES = incompatible("BAZEL_INCOMPATIBLE_TOOLCHAIN_TYPES_IN_RULES", default = False) _apple = struct( # From Bazel 8.0.0 on, the `xcode_version_flag` is available and the `XcodeVersionConfig` @@ -67,8 +72,8 @@ # Whether map_directory() can handle additional_params needed for create_cc_compile_action # https://github.com/bazelbuild/bazel/commit/0d277213563ce36408f105f01b8f08b043c5fbaf _map_directory_supports_cc_compile = ge("10.0.0rc1") or ge("10.0.0-pre.20261001.2"), - # Placeholder for the migration. - _toolchain_type_in_rules_cc = False, + # See _TOOLCHAIN_TYPES_IN_RULES. + _toolchain_type_in_rules_cc = _TOOLCHAIN_TYPES_IN_RULES, ) _docs = struct( @@ -141,8 +146,8 @@ # https://github.com/bazelbuild/bazel/commit/e2249f91ff84541565d8ba841592a0a8a43fcb66 (7.0.0) java_info_constructor_module_flags = ge_same_major("7.0.0") or ge("8.0.0-pre.20240101.1"), # Internal only, don't use outside rules_java. - # Placeholder for the migration. - _toolchain_type_in_rules_java = False, + # See _TOOLCHAIN_TYPES_IN_RULES. + _toolchain_type_in_rules_java = _TOOLCHAIN_TYPES_IN_RULES, ) _proto = struct( @@ -218,8 +223,8 @@ # TODO: Use a larger version range after cherry-picking # https://github.com/bazelbuild/bazel/commit/e81949554f3ecab5e2c4afd79031f498f36427fe _has_launcher_maker_toolchain = ge_same_major("8.3.0") or ge("9.0.0-pre.20250516.1"), - # Placeholder for the migration. - _toolchain_type_in_rules_python = False, + # See _TOOLCHAIN_TYPES_IN_RULES. + _toolchain_type_in_rules_python = _TOOLCHAIN_TYPES_IN_RULES, ) _toolchains = struct(
diff --git a/private/util.bzl b/private/util.bzl index afdcbfa..6234339 100644 --- a/private/util.bzl +++ b/private/util.bzl
@@ -1,10 +1,42 @@ """Internal-only utility functions.""" -load("@bazel_features_version//:version.bzl", "version") +load("@bazel_features_version//:version.bzl", "incompatible_env", "version") load(":parse.bzl", "parse_version") BAZEL_VERSION = parse_version(version) +def parse_incompatible_env(name, value, default): + """Parses the value of an environment variable toggling an incompatible change. + + Args: + name: the name of the environment variable, used in error messages. + value: the value of the environment variable, or None if it isn't set. + default: the value to return if the environment variable isn't set or is empty. + + Returns: + True if the incompatible change is enabled, False otherwise. + """ + if not value: + return default + if value in ("1", "true", "True"): + return True + if value in ("0", "false", "False"): + return False + fail("Invalid value for --repo_env={}: '{}', expected 0 or 1".format(name, value)) + +def incompatible(name, default): + """Returns whether the incompatible change toggled by an environment variable is enabled. + + Args: + name: the name of the environment variable, which must be listed in + INCOMPATIBLE_ENV_VARS in version_repo.bzl. + default: whether the change is enabled if the variable isn't set. + + Returns: + True if the incompatible change is enabled, False otherwise. + """ + return parse_incompatible_env(name, incompatible_env.get(name), default) + def le(v): return BAZEL_VERSION <= parse_version(v)
diff --git a/private/version_repo.bzl b/private/version_repo.bzl index 323d760..ae7b75a 100644 --- a/private/version_repo.bzl +++ b/private/version_repo.bzl
@@ -1,5 +1,12 @@ """Contains the internal repository rule version_repo.""" +# Environment variables that toggle incompatible changes tracked as features. +INCOMPATIBLE_ENV_VARS = [ + # If enabled, the toolchain types in @bazel_tools are aliases of the types + # in rules_cc, rules_java and rules_python. + "BAZEL_INCOMPATIBLE_TOOLCHAIN_TYPES_IN_RULES", +] + def _version_repo_impl(rctx): rctx.file( "BUILD.bazel", @@ -15,13 +22,23 @@ ) """, ) - rctx.file("version.bzl", "version = '" + native.bazel_version + "'") + incompatible_env = { + name: rctx.os.environ[name] + for name in INCOMPATIBLE_ENV_VARS + if name in rctx.os.environ + } + rctx.file("version.bzl", "\n".join([ + "version = '" + native.bazel_version + "'", + "incompatible_env = " + repr(incompatible_env), + ])) if hasattr(rctx, "repo_metadata"): return rctx.repo_metadata(reproducible = False) return None version_repo = repository_rule( _version_repo_impl, + # Rerun when an incompatible change is toggled with --repo_env. + environ = INCOMPATIBLE_ENV_VARS, # Force reruns on server restarts to keep native.bazel_version up-to-date. local = True, )
diff --git a/test/test.bzl b/test/test.bzl index b5f85c4..49f3a24 100644 --- a/test/test.bzl +++ b/test/test.bzl
@@ -1,9 +1,9 @@ """Provides a macro to do some loading-time test assertions.""" +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("//:features.bzl", "bazel_features") load("//private:parse.bzl", "parse_version") -load("//private:util.bzl", "BAZEL_VERSION", "ge", "lt") -load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("//private:util.bzl", "BAZEL_VERSION", "ge", "lt", "parse_incompatible_env") def _assert_lt(a, b): if parse_version(a) >= parse_version(b): @@ -13,6 +13,11 @@ if parse_version(a) != parse_version(b): fail("expected '{}' to be equal to '{}', but was not the case".format(a, b)) +def _assert_incompatible_env(value, default, expected): + actual = parse_incompatible_env("TEST_VAR", value, default) + if actual != expected: + fail("expected parse_incompatible_env('TEST_VAR', {}, {}) to be {}, got {}".format(repr(value), default, expected, actual)) + def run_test(name): """Performs some loading-time assertions about bazel_features' APIs, and creates a fake test target. @@ -37,6 +42,24 @@ _assert_lt("1.0.0-rc.1", "1.0.0") _assert_lt("10.0.0-pre.20260524.1", "10.0.0-pre-db077161e43484e4cb0f2270374ee33d05f3690d") + # parsing of environment variables toggling incompatible changes + for default in (False, True): + _assert_incompatible_env(None, default, default) + _assert_incompatible_env("", default, default) + for value in ("1", "true", "True"): + _assert_incompatible_env(value, default, True) + for value in ("0", "false", "False"): + _assert_incompatible_env(value, default, False) + + # the toolchain type migrations are toggled together + toolchain_types_in_rules = [ + bazel_features.cc._toolchain_type_in_rules_cc, + bazel_features.java._toolchain_type_in_rules_java, + bazel_features.rules._toolchain_type_in_rules_python, + ] + if toolchain_types_in_rules not in ([False, False, False], [True, True, True]): + fail("expected the _toolchain_type_in_rules_* features to agree, got {}".format(toolchain_types_in_rules)) + # a smoke test on the actual current Bazel version if not ge("0.0.1"): fail("somehow the current Bazel version (parsed: '{}') is not >= 0.0.1".format(BAZEL_VERSION))