refactor: do not parse the versions in the files autoloaded by Bazel 8 (#4569) Bazel 8 autoloads the Python rules and providers from `rules_python` and the files that it loads for that can only load `.bzl` files from a fixed list of repositories, otherwise the build fails with `Cycle caused by autoloads`. This means that `py_library.bzl` and `py_runtime_rule.bzl` cannot use a version parser from a different repository, e.g. `pypackaging.bzl`, and they are the reason why `version.bzl` cannot be replaced with it. Neither of them needs a full PEP 440 parser: - `py_runtime` only needs the release numbers from the value of the `python_version` flag. - `py_library` only compares the versions of the same distribution for equality, so it now uses the version as it is spelled in the name of the `.dist-info` directory, which the installers already normalize. Add a test that checks that the autoloaded files only load from the repositories that Bazel allows.
diff --git a/news/dist-info-version-not-normalized.changed.md b/news/dist-info-version-not-normalized.changed.md new file mode 100644 index 0000000..8fa23f2 --- /dev/null +++ b/news/dist-info-version-not-normalized.changed.md
@@ -0,0 +1,3 @@ +(rules) {obj}`VenvSymlinkEntry.version` is the version as it is spelled in the +name of the `.dist-info` directory and is no longer normalized by +`rules_python`.
diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index 78e4c48..8f43b08 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel
@@ -627,7 +627,6 @@ ":rule_builders", ":toolchain_types", ":venv_runfiles", - ":version", "@bazel_skylib//lib:dicts", "@bazel_skylib//lib:paths", "@bazel_skylib//rules:common_settings", @@ -700,7 +699,6 @@ ":py_internal", ":py_runtime_info", ":reexports", - ":version", "@bazel_skylib//lib:dicts", "@bazel_skylib//lib:paths", "@bazel_skylib//rules:common_settings",
diff --git a/python/private/py_info.bzl b/python/private/py_info.bzl index ead24ec..c04cb26 100644 --- a/python/private/py_info.bzl +++ b/python/private/py_info.bzl
@@ -146,8 +146,13 @@ "version": """ :type: str | None -Represents the PyPI package version that the code originates from. It is normalized according to the -PEP440 standard. +Represents the PyPI package version that the code originates from. It is the +version as it is spelled in the name of the `.dist-info` directory, which the +installers normalize according to the PEP440 standard. + +:::{versionchanged} VERSION_NEXT_PATCH +The version is no longer normalized by `rules_python`. +::: """, }, )
diff --git a/python/private/py_library.bzl b/python/private/py_library.bzl index 9b1e064..b92402b 100644 --- a/python/private/py_library.bzl +++ b/python/private/py_library.bzl
@@ -51,7 +51,6 @@ TOOLCHAIN_TYPE = "TARGET_TOOLCHAIN_TYPE", ) load(":venv_runfiles.bzl", "get_venv_symlinks") -load(":version.bzl", "version") LIBRARY_ATTRS = dicts.add( COMMON_ATTRS, @@ -236,8 +235,8 @@ Returns: {type}`tuple[str | None, str | None]` the distribution name, lowercased with runs of `-`, `_` and `.` turned into `_` (e.g. `google_cloud_storage`), - and the normalized version; `(None, None)` without a - `.dist-info/METADATA` file. + and the version as it is spelled in the name of the `.dist-info` + directory; `(None, None)` without a `.dist-info/METADATA` file. """ dist_info_metadata = None for d in data_files: @@ -260,7 +259,7 @@ package, _, version_str = package.rpartition("-") return ( normalize_name(package), # will have no dashes - version.normalize(version_str), # will have no dashes either + version_str, # will have no dashes either ) def _get_imports_and_venv_symlinks(ctx):
diff --git a/python/private/py_runtime_rule.bzl b/python/private/py_runtime_rule.bzl index 4f450c4..6099d99 100644 --- a/python/private/py_runtime_rule.bzl +++ b/python/private/py_runtime_rule.bzl
@@ -21,7 +21,6 @@ load(":py_internal.bzl", "py_internal") load(":py_runtime_info.bzl", "DEFAULT_STUB_SHEBANG", "PyRuntimeInfo") load(":reexports.bzl", "BuiltinPyRuntimeInfo") -load(":version.bzl", "version") _py_builtins = py_internal @@ -467,11 +466,15 @@ return _py_builtins.is_singleton_depset(files) def _interpreter_version_info_from_version_str(version_str): - v = version.parse(version_str) + # We only need the release numbers, e.g. `3.15.0` from `3.15.0rc1`. version_info = {} - parts = list(v.release) - for key in ("major", "minor", "micro"): - if not parts: - break - version_info[key] = parts.pop(0) + for key, part in zip(("major", "minor", "micro"), version_str.split(".")): + digits = "" + for c in part.elems(): + if not c.isdigit(): + break + digits += c + if not digits: + fail("Expected the Python version to be '<major>[.<minor>[.<micro>]]', got: '{}'".format(version_str)) + version_info[key] = int(digits) return version_info
diff --git a/tests/bazel_autoloads/BUILD.bazel b/tests/bazel_autoloads/BUILD.bazel new file mode 100644 index 0000000..a7f2a95 --- /dev/null +++ b/tests/bazel_autoloads/BUILD.bazel
@@ -0,0 +1,3 @@ +load(":bazel_autoloads_tests.bzl", "bazel_autoloads_test_suite") + +bazel_autoloads_test_suite(name = "bazel_autoloads_tests")
diff --git a/tests/bazel_autoloads/bazel_autoloads_tests.bzl b/tests/bazel_autoloads/bazel_autoloads_tests.bzl new file mode 100644 index 0000000..bcd5ac0 --- /dev/null +++ b/tests/bazel_autoloads/bazel_autoloads_tests.bzl
@@ -0,0 +1,95 @@ +"""Tests for the `.bzl` files that Bazel 8 autoloads from `rules_python`.""" + +load("@bazel_skylib//:bzl_library.bzl", "StarlarkLibraryInfo") +load("@rules_testing//lib:analysis_test.bzl", "analysis_test") +load("@rules_testing//lib:test_suite.bzl", "test_suite") + +# The `bzl_library` targets in `//python` of the files that Bazel 8 loads in +# order to autoload the Python rules and providers, see `AUTOLOAD_CONFIG` in +# https://github.com/bazelbuild/bazel/blob/release-8.9.0/src/main/java/com/google/devtools/build/lib/packages/AutoloadSymbols.java +_AUTOLOADED = [ + "py_binary", + "py_cc_link_params_info", + "py_info", + "py_library", + "py_runtime", + "py_runtime_info", + "py_test", +] + +# The repositories where Bazel 8 does not use the autoloads, see +# `PREDECLARED_REPOS_DISALLOWING_AUTOLOADS` in the file above. The repositories +# created by the module extensions of these modules are included, because +# only the part of the canonical name that is before the first `+` is used. +# +# A `.bzl` file from any other repository cannot be loaded by the autoloaded +# files, because Bazel would need the autoloaded symbols in order to load it, +# which is reported as `Cycle caused by autoloads`. The users can only work +# this around with the `--repositories_without_autoloads` flag. +_REPOS_WITHOUT_AUTOLOADS = { + # The main repository, i.e. `rules_python` itself + "": None, + "apple_support": None, + "bazel_features": None, + "bazel_features_globals": None, + "bazel_features_version": None, + "bazel_skylib": None, + "bazel_tools": None, + "build_bazel_apple_support": None, + "cc_compatibility_proxy": None, + "com_google_protobuf": None, + "compatibility_proxy": None, + "proto_bazel_features": None, + "protobuf": None, + "rules_android": None, + "rules_cc": None, + "rules_java": None, + "rules_java_builtin": None, + "rules_python": None, + "rules_python_internal": None, + "rules_shell": None, +} + +_tests = [] + +# NOTE: This is why the autoloaded files, e.g. `py_library.bzl` and +# `py_runtime_rule.bzl`, do not use `@pypackaging.bzl` to parse and normalize +# the versions. If we want to fully use it in those files, `pypackaging.bzl` +# first needs to be added to the list of repositories without autoloads in +# Bazel. +def _test_autoloaded_files_only_load_from_repos_without_autoloads(name): + analysis_test( + name = name, + impl = _test_autoloaded_files_only_load_from_repos_without_autoloads_impl, + targets = { + lib: "//python:" + lib + for lib in _AUTOLOADED + }, + ) + +def _test_autoloaded_files_only_load_from_repos_without_autoloads_impl(env, targets): + for lib in _AUTOLOADED: + target = getattr(targets, lib) + got = [] + for src in target[StarlarkLibraryInfo].transitive_srcs.to_list(): + repo = src.owner.workspace_name + + # Bzlmod uses `+` (`~` before Bazel 8) to separate the module name + # from the rest of the canonical repository name. + repo, _, _ = repo.partition("+") + repo, _, _ = repo.partition("~") + if repo not in _REPOS_WITHOUT_AUTOLOADS: + got.append(str(src.owner)) + + env.expect.that_collection( + got, + expr = "files loaded by {} from repos with autoloads".format(target.label), + ).contains_exactly([]) + +_tests.append(_test_autoloaded_files_only_load_from_repos_without_autoloads) + +def bazel_autoloads_test_suite(name): + test_suite( + name = name, + tests = _tests, + )
diff --git a/tests/py_runtime/py_runtime_tests.bzl b/tests/py_runtime/py_runtime_tests.bzl index 47cd97f..ca16fe3 100644 --- a/tests/py_runtime/py_runtime_tests.bzl +++ b/tests/py_runtime/py_runtime_tests.bzl
@@ -594,6 +594,31 @@ _tests.append(_test_version_info_from_flag) +def _test_version_info_from_flag_with_prerelease(name): + py_runtime( + name = name + "_subject", + interpreter_version_info = None, + interpreter_path = "/bogus", + ) + analysis_test( + name = name, + target = name + "_subject", + impl = _test_version_info_from_flag_with_prerelease_impl, + config_settings = { + labels.PYTHON_VERSION: "3.15.0rc1", + }, + ) + +def _test_version_info_from_flag_with_prerelease_impl(env, target): + version_info = env.expect.that_target(target).provider(PyRuntimeInfo, factory = py_runtime_info_subject).interpreter_version_info() + version_info.major().equals(3) + version_info.minor().equals(15) + version_info.micro().equals(0) + version_info.releaselevel().equals(None) + version_info.serial().equals(None) + +_tests.append(_test_version_info_from_flag_with_prerelease) + def py_runtime_test_suite(name): test_suite( name = name,