From 1eebc2ab2c3da834682b54ec989a459f5e5a5461 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Tue, 22 Oct 2024 10:58:57 -0700 Subject: [PATCH 1/3] fix(rules): remove rules_python --incompatible_python_disallow_native_rules checking (#2327) When --incompatible_python_disallow_native_rules is enabled, all the core rules fail with an error that rules_python should be used. This is incorrect, since the rules_python rules are being used. What's happening is https://github.com/bazelbuild/rules_python/pull/2257 removed the magic migration tag when pystar is enabled, but the code to check the tag was present wasn't removed. This went unnoticed because our CI doesn't set the migration flag. To fix, remove the validation logic entirely. If we're in the rules_python implementation, then there is not need to perform this validation. It was just something copy/pasted from the original code from Bazel itself. Also update the bazelrc to always set --incompatible_python_disallow_native_rules. Fixes https://github.com/bazelbuild/rules_python/issues/2326 Fixes https://github.com/bazelbuild/rules_python/issues/1645 --- .bazelci/presubmit.yml | 2 + .bazelrc | 2 + CHANGELOG.md | 4 +- examples/build_file_generation/.bazelrc | 2 + examples/bzlmod/.bazelrc | 1 + .../bzlmod_build_file_generation/.bazelrc | 1 + examples/multi_python_versions/.bazelrc | 1 + examples/pip_parse/.bazelrc | 1 + examples/pip_parse_vendored/.bazelrc | 1 + examples/pip_repository_annotations/.bazelrc | 1 + examples/py_proto_library/.bazelrc | 1 + gazelle/.bazelrc | 8 +-- python/private/common.bzl | 71 ------------------- python/private/py_executable.bzl | 2 - python/private/py_library.bzl | 2 - .../compile_pip_requirements/.bazelrc | 1 + .../.bazelrc | 1 + .../ignore_root_user_error/.bazelrc | 1 + tests/integration/local_toolchains/.bazelrc | 1 + tests/integration/pip_parse/.bazelrc | 1 + .../py_cc_toolchain_registered/.bazelrc | 1 + 21 files changed, 23 insertions(+), 83 deletions(-) diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 631ad864f7..db2878a3e7 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -94,6 +94,8 @@ buildifier: bazel: "7.x" environment: RULES_PYTHON_ENABLE_PYSTAR: "1" + build_flags: + - "--config=bazel7.x" test_flags: # The doc check tests fail because the Starlark implementation makes the # PyInfo and PyRuntimeInfo symbols become documented. diff --git a/.bazelrc b/.bazelrc index 1ca469cd75..c44124d961 100644 --- a/.bazelrc +++ b/.bazelrc @@ -33,4 +33,6 @@ build:rtd --stamp # Some bzl files contain repos only available under bzlmod build:rtd --enable_bzlmod +common:bazel7.x --incompatible_python_disallow_native_rules + build --lockfile_mode=update diff --git a/CHANGELOG.md b/CHANGELOG.md index e428fa3aa8..eb18fc5ea8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,9 @@ A brief description of the categories of changes: - Nothing yet ### Fixed -- Nothing yet +* (rules) Setting `--incompatible_python_disallow_native_rules` no longer + causes rules_python rules to fail. + ([#2326](https://github.com/bazelbuild/rules_python/issues/2326). ### Added - Nothing yet diff --git a/examples/build_file_generation/.bazelrc b/examples/build_file_generation/.bazelrc index e0b1984e4e..fd0f731d2f 100644 --- a/examples/build_file_generation/.bazelrc +++ b/examples/build_file_generation/.bazelrc @@ -6,3 +6,5 @@ build --enable_runfiles # The bzlmod version of this example is in examples/bzlmod_build_file_generation # Once WORKSPACE support is dropped, this example can be entirely deleted. build --experimental_enable_bzlmod=false + +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/examples/bzlmod/.bazelrc b/examples/bzlmod/.bazelrc index fd16095857..ca83047ccc 100644 --- a/examples/bzlmod/.bazelrc +++ b/examples/bzlmod/.bazelrc @@ -7,3 +7,4 @@ test --test_output=errors --enable_runfiles # Windows requires these for multi-python support: build --enable_runfiles +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/examples/bzlmod_build_file_generation/.bazelrc b/examples/bzlmod_build_file_generation/.bazelrc index acc7102a17..0289886d4d 100644 --- a/examples/bzlmod_build_file_generation/.bazelrc +++ b/examples/bzlmod_build_file_generation/.bazelrc @@ -6,3 +6,4 @@ build --enable_runfiles common --experimental_enable_bzlmod coverage --java_runtime_version=remotejdk_11 +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/examples/multi_python_versions/.bazelrc b/examples/multi_python_versions/.bazelrc index 58080ab51b..97a973bd85 100644 --- a/examples/multi_python_versions/.bazelrc +++ b/examples/multi_python_versions/.bazelrc @@ -4,3 +4,4 @@ test --test_output=errors build --enable_runfiles coverage --java_runtime_version=remotejdk_11 +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/examples/pip_parse/.bazelrc b/examples/pip_parse/.bazelrc index 9e7ef37327..a56904803c 100644 --- a/examples/pip_parse/.bazelrc +++ b/examples/pip_parse/.bazelrc @@ -1,2 +1,3 @@ # https://docs.bazel.build/versions/main/best-practices.html#using-the-bazelrc-file try-import %workspace%/user.bazelrc +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/examples/pip_parse_vendored/.bazelrc b/examples/pip_parse_vendored/.bazelrc index 3818a03808..be3555d1eb 100644 --- a/examples/pip_parse_vendored/.bazelrc +++ b/examples/pip_parse_vendored/.bazelrc @@ -6,3 +6,4 @@ build --enable_runfiles # Vendoring requirements.bzl files isn't necessary under bzlmod # When workspace support is dropped, this example can be removed. build --noexperimental_enable_bzlmod +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/examples/pip_repository_annotations/.bazelrc b/examples/pip_repository_annotations/.bazelrc index 9ce0b72b48..4f62c6e76f 100644 --- a/examples/pip_repository_annotations/.bazelrc +++ b/examples/pip_repository_annotations/.bazelrc @@ -4,3 +4,4 @@ try-import %workspace%/user.bazelrc # This example is WORKSPACE specific. The equivalent functionality # is in examples/bzlmod as the `whl_mods` feature. build --experimental_enable_bzlmod=false +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/examples/py_proto_library/.bazelrc b/examples/py_proto_library/.bazelrc index ef0e530774..65d8a0a2f6 100644 --- a/examples/py_proto_library/.bazelrc +++ b/examples/py_proto_library/.bazelrc @@ -1,2 +1,3 @@ # The equivalent bzlmod behavior is covered by examples/bzlmod/py_proto_library common --noenable_bzlmod +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/gazelle/.bazelrc b/gazelle/.bazelrc index e10cd78a26..97040903a6 100644 --- a/gazelle/.bazelrc +++ b/gazelle/.bazelrc @@ -11,10 +11,4 @@ build --incompatible_default_to_explicit_init_py # Windows makes use of runfiles for some rules build --enable_runfiles -# Do NOT implicitly create empty __init__.py files in the runfiles tree. -# By default, these are created in every directory containing Python source code -# or shared libraries, and every parent directory of those directories, -# excluding the repo root directory. With this flag set, we are responsible for -# creating (possibly empty) __init__.py files and adding them to the srcs of -# Python targets as required. -build --incompatible_default_to_explicit_init_py +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/python/private/common.bzl b/python/private/common.bzl index 2dcc9482ca..837cd19097 100644 --- a/python/private/common.bzl +++ b/python/private/common.bzl @@ -17,24 +17,15 @@ load(":cc_helper.bzl", "cc_helper") load(":py_info.bzl", "PyInfo", "PyInfoBuilder") load(":py_internal.bzl", "py_internal") load(":reexports.bzl", "BuiltinPyInfo") -load( - ":semantics.bzl", - "NATIVE_RULES_MIGRATION_FIX_CMD", - "NATIVE_RULES_MIGRATION_HELP_URL", -) _testing = testing _platform_common = platform_common _coverage_common = coverage_common -_py_builtins = py_internal PackageSpecificationInfo = getattr(py_internal, "PackageSpecificationInfo", None) # Extensions without the dot _PYTHON_SOURCE_EXTENSIONS = ["py"] -# NOTE: Must stay in sync with the value used in rules_python -_MIGRATION_TAG = "__PYTHON_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__" - def create_binary_semantics_struct( *, create_executable, @@ -477,65 +468,3 @@ def target_platform_has_any_constraint(ctx, constraints): if ctx.target_platform_has_constraint(constraint_value): return True return False - -def check_native_allowed(ctx): - """Check if the usage of the native rule is allowed. - - Args: - ctx: rule context to check - """ - if not ctx.fragments.py.disallow_native_rules: - return - - if _MIGRATION_TAG in ctx.attr.tags: - return - - # NOTE: The main repo name is empty in *labels*, but not in - # ctx.workspace_name - is_main_repo = not bool(ctx.label.workspace_name) - if is_main_repo: - check_label = ctx.label - else: - # package_group doesn't allow @repo syntax, so we work around that - # by prefixing external repos with a fake package path. This also - # makes it easy to enable or disable all external repos. - check_label = Label("@//__EXTERNAL_REPOS__/{workspace}/{package}".format( - workspace = ctx.label.workspace_name, - package = ctx.label.package, - )) - allowlist = ctx.attr._native_rules_allowlist - if allowlist: - allowed = ctx.attr._native_rules_allowlist[PackageSpecificationInfo].contains(check_label) - allowlist_help = str(allowlist.label).replace("@//", "//") - else: - allowed = False - allowlist_help = ("no allowlist specified; all disallowed; specify one " + - "with --python_native_rules_allowlist") - if not allowed: - if ctx.attr.generator_function: - generator = "{generator_function}(name={generator_name}) in {generator_location}".format( - generator_function = ctx.attr.generator_function, - generator_name = ctx.attr.generator_name, - generator_location = ctx.attr.generator_location, - ) - else: - generator = "No generator (called directly in BUILD file)" - - msg = ( - "{target} not allowed to use native.{rule}\n" + - "Generated by: {generator}\n" + - "Allowlist: {allowlist}\n" + - "Migrate to using @rules_python, see {help_url}\n" + - "FIXCMD: {fix_cmd} --target={target} --rule={rule} " + - "--generator_name={generator_name} --location={generator_location}" - ) - fail(msg.format( - target = str(ctx.label).replace("@//", "//"), - rule = _py_builtins.get_rule_name(ctx), - generator = generator, - allowlist = allowlist_help, - generator_name = ctx.attr.generator_name, - generator_location = ctx.attr.generator_location, - help_url = NATIVE_RULES_MIGRATION_HELP_URL, - fix_cmd = NATIVE_RULES_MIGRATION_FIX_CMD, - )) diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index ce1288cc29..b81f07e01a 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -33,7 +33,6 @@ load(":builders.bzl", "builders") load(":cc_helper.bzl", "cc_helper") load( ":common.bzl", - "check_native_allowed", "collect_imports", "collect_runfiles", "create_instrumented_files_info", @@ -269,7 +268,6 @@ def _get_build_info(ctx, cc_toolchain): def _validate_executable(ctx): if ctx.attr.python_version != "PY3": fail("It is not allowed to use Python 2") - check_native_allowed(ctx) def _declare_executable_file(ctx): if target_platform_has_any_constraint(ctx, ctx.attr._windows_constraints): diff --git a/python/private/py_library.bzl b/python/private/py_library.bzl index 4f43116947..1bc96b5e4b 100644 --- a/python/private/py_library.bzl +++ b/python/private/py_library.bzl @@ -28,7 +28,6 @@ load( load(":builders.bzl", "builders") load( ":common.bzl", - "check_native_allowed", "collect_imports", "collect_runfiles", "create_instrumented_files_info", @@ -70,7 +69,6 @@ def py_library_impl(ctx, *, semantics): Returns: A list of modern providers to propagate. """ - check_native_allowed(ctx) direct_sources = filter_to_py_srcs(ctx.files.srcs) precompile_result = semantics.maybe_precompile(ctx, direct_sources) diff --git a/tests/integration/compile_pip_requirements/.bazelrc b/tests/integration/compile_pip_requirements/.bazelrc index 8a42e6405b..b85f03bcb6 100644 --- a/tests/integration/compile_pip_requirements/.bazelrc +++ b/tests/integration/compile_pip_requirements/.bazelrc @@ -2,3 +2,4 @@ test --test_output=errors # Windows requires these for multi-python support: build --enable_runfiles +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/tests/integration/compile_pip_requirements_test_from_external_repo/.bazelrc b/tests/integration/compile_pip_requirements_test_from_external_repo/.bazelrc index b98fc09774..ab10c8caf7 100644 --- a/tests/integration/compile_pip_requirements_test_from_external_repo/.bazelrc +++ b/tests/integration/compile_pip_requirements_test_from_external_repo/.bazelrc @@ -1 +1,2 @@ test --test_output=errors +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/tests/integration/ignore_root_user_error/.bazelrc b/tests/integration/ignore_root_user_error/.bazelrc index 27d7d137cd..bb7b5742cd 100644 --- a/tests/integration/ignore_root_user_error/.bazelrc +++ b/tests/integration/ignore_root_user_error/.bazelrc @@ -4,3 +4,4 @@ test --test_output=errors # Windows requires these for multi-python support: build --enable_runfiles +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/tests/integration/local_toolchains/.bazelrc b/tests/integration/local_toolchains/.bazelrc index 551df401b3..39df41d9f4 100644 --- a/tests/integration/local_toolchains/.bazelrc +++ b/tests/integration/local_toolchains/.bazelrc @@ -3,3 +3,4 @@ common --lockfile_mode=off test --test_output=errors # Windows requires these for multi-python support: build --enable_runfiles +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/tests/integration/pip_parse/.bazelrc b/tests/integration/pip_parse/.bazelrc index efeccbe919..a74909297d 100644 --- a/tests/integration/pip_parse/.bazelrc +++ b/tests/integration/pip_parse/.bazelrc @@ -5,3 +5,4 @@ build --enable_runfiles # https://docs.bazel.build/versions/main/best-practices.html#using-the-bazelrc-file try-import %workspace%/user.bazelrc +common:bazel7.x --incompatible_python_disallow_native_rules diff --git a/tests/integration/py_cc_toolchain_registered/.bazelrc b/tests/integration/py_cc_toolchain_registered/.bazelrc index 741d758a4f..fb31561892 100644 --- a/tests/integration/py_cc_toolchain_registered/.bazelrc +++ b/tests/integration/py_cc_toolchain_registered/.bazelrc @@ -1,2 +1,3 @@ # This aids debugging on failure build --toolchain_resolution_debug=python +common:bazel7.x --incompatible_python_disallow_native_rules From 8c3acea5e4192db05e9a71c5bd4fbec7c0853e4d Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Tue, 22 Oct 2024 21:36:56 -0700 Subject: [PATCH 2/3] docs: update changelog for 0.37.1 (#2330) Update the headers and sections for the 0.37.1 release. --- CHANGELOG.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb18fc5ea8..a94ff8763f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,9 +26,7 @@ A brief description of the categories of changes: - Nothing yet ### Fixed -* (rules) Setting `--incompatible_python_disallow_native_rules` no longer - causes rules_python rules to fail. - ([#2326](https://github.com/bazelbuild/rules_python/issues/2326). +- Nothing yet ### Added - Nothing yet @@ -36,6 +34,18 @@ A brief description of the categories of changes: ### Removed - Nothing yet +{#v0-37-1} +## [0.37.1] - 2024-10-22 + +[0.37.1]: https://github.com/bazelbuild/rules_python/releases/tag/0.37.1 + +{#v0-37-1-fixed} +### Fixed +* (rules) Setting `--incompatible_python_disallow_native_rules` no longer + causes rules_python rules to fail + ([#2326](https://github.com/bazelbuild/rules_python/issues/2326)). + +{#v0-37-0} ## [0.37.0] - 2024-10-18 [x.x.x]: https://github.com/bazelbuild/rules_python/releases/tag/0.37.0 From 2ee80c3e4675c1fc84976cf4c2d036ce69fd4eb1 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 27 Oct 2024 03:36:14 +0900 Subject: [PATCH 3/3] fix(bzlmod): generate config_setting values for all python toolchains (#2350) PR #2253 broke how the config settings are generated and only generated the config setting values for the python version values that we would have the registered toolchains for. This PR restores the previous behaviour. However, if the root module uses `python.override` to remove the allowed toolchains, then `config_settings` will be also affected. (cherry picked from commit 9340a8199cac0912305dad427a5cdc72ee4fc7ff) Conflicts: - CHANGELOG.md --- CHANGELOG.md | 14 +++++++ examples/multi_python_versions/MODULE.bazel | 1 + .../multi_python_versions/tests/BUILD.bazel | 39 +++++++++++++++++++ python/private/python.bzl | 1 + 4 files changed, 55 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a94ff8763f..49128cff3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,8 +20,11 @@ A brief description of the categories of changes: * Particular sub-systems are identified using parentheses, e.g. `(bzlmod)` or `(docs)`. +{#v0-0-0} ## Unreleased +[0.0.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.0.0 + ### Changed - Nothing yet @@ -34,6 +37,17 @@ A brief description of the categories of changes: ### Removed - Nothing yet +{#v0-37-2} +## [0.37.2] - 2024-10-27 + +[0.37.2]: https://github.com/bazelbuild/rules_python/releases/tag/0.37.2 + +{#v0-37-2-fixed} +### Fixed +* (bzlmod) Generate `config_setting` values for all available toolchains instead + of only the registered toolchains, which restores the previous behaviour that + `bzlmod` users would have observed. + {#v0-37-1} ## [0.37.1] - 2024-10-22 diff --git a/examples/multi_python_versions/MODULE.bazel b/examples/multi_python_versions/MODULE.bazel index 4223916d22..536940ba49 100644 --- a/examples/multi_python_versions/MODULE.bazel +++ b/examples/multi_python_versions/MODULE.bazel @@ -30,6 +30,7 @@ python.toolchain( ) use_repo( python, + "pythons_hub", python = "python_versions", ) diff --git a/examples/multi_python_versions/tests/BUILD.bazel b/examples/multi_python_versions/tests/BUILD.bazel index cf14bf0a3b..d5c66e026f 100644 --- a/examples/multi_python_versions/tests/BUILD.bazel +++ b/examples/multi_python_versions/tests/BUILD.bazel @@ -1,9 +1,14 @@ load("@bazel_skylib//rules:copy_file.bzl", "copy_file") +load("@bazel_skylib//rules:diff_test.bzl", "diff_test") +load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@python//3.10:defs.bzl", py_binary_3_10 = "py_binary", py_test_3_10 = "py_test") load("@python//3.11:defs.bzl", py_binary_3_11 = "py_binary", py_test_3_11 = "py_test") load("@python//3.8:defs.bzl", py_binary_3_8 = "py_binary", py_test_3_8 = "py_test") load("@python//3.9:defs.bzl", py_binary_3_9 = "py_binary", py_test_3_9 = "py_test") +load("@pythons_hub//:versions.bzl", "MINOR_MAPPING", "PYTHON_VERSIONS") load("@rules_python//python:defs.bzl", "py_binary", "py_test") +load("@rules_python//python:versions.bzl", DEFAULT_MINOR_MAPPING = "MINOR_MAPPING", DEFAULT_TOOL_VERSIONS = "TOOL_VERSIONS") +load("@rules_python//python/private:text_util.bzl", "render") # buildifier: disable=bzl-visibility load("@rules_shell//shell:sh_test.bzl", "sh_test") copy_file( @@ -183,3 +188,37 @@ sh_test( "VERSION_PY_BINARY": "$(rootpath :version_3_10)", }, ) + +# The following test ensures that default toolchain versions are the same as in +# the TOOL_VERSIONS array. + +# NOTE @aignas 2024-10-26: This test here is to do a sanity check and not +# include extra dependencies - if rules_testing is included here, we can +# potentially uses `rules_testing` for a more lightweight test. +write_file( + name = "default_python_versions", + out = "default_python_versions.txt", + content = [ + "MINOR_MAPPING:", + render.dict(dict(sorted(DEFAULT_MINOR_MAPPING.items()))), + "PYTHON_VERSIONS:", + render.list(sorted(DEFAULT_TOOL_VERSIONS)), + ], +) + +write_file( + name = "pythons_hub_versions", + out = "pythons_hub_versions.txt", + content = [ + "MINOR_MAPPING:", + render.dict(dict(sorted(MINOR_MAPPING.items()))), + "PYTHON_VERSIONS:", + render.list(sorted(PYTHON_VERSIONS)), + ], +) + +diff_test( + name = "test_versions", + file1 = "default_python_versions", + file2 = "pythons_hub_versions", +) diff --git a/python/private/python.bzl b/python/private/python.bzl index 12ab4bb48d..d2b1007231 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -241,6 +241,7 @@ def _python_impl(module_ctx): # Last toolchain is default default_python_version = py.default_python_version, minor_mapping = py.config.minor_mapping, + python_versions = list(py.config.default["tool_versions"].keys()), toolchain_prefixes = [ render.toolchain_prefix(index, toolchain.name, _TOOLCHAIN_INDEX_PAD_LENGTH) for index, toolchain in enumerate(py.toolchains) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy