From 19698d625684ef5c6efc013da2c613ed266f8b22 Mon Sep 17 00:00:00 2001 From: aignas <240938+aignas@users.noreply.github.com> Date: Tue, 20 Aug 2024 22:55:22 +0300 Subject: [PATCH] fix(bzlmod): keep the lockfile platform independent when resolving python Before this PR the lockfile would become platform dependent when the `requirements` file would have env markers. This was not caught because we do not have MODULE.bazel.lock checked into the `rules_python` repository because the CI is running against many versions and the lock file is different, therefor we would not be able to run with `bazel build --lockfile_mode=error`. With this change we use the label to `BUILD.bazel` which is living next to the `python` symlink and since the `BUILD.bazel` is the same on all platforms, the lockfile will remain the same. Work towards #1105, #1868. --- CHANGELOG.md | 5 +++++ examples/bzlmod/MODULE.bazel.lock | 6 +++--- python/private/pypi/pypi_repo_utils.bzl | 17 +++++++++++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b457c411d..61b7f89044 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,11 @@ A brief description of the categories of changes: ### Removed * Nothing yet +### Fixed +* (bzlmod) get the path to the host python interpreter without calling + `mctx.path` on Labels that have differing contents on different OSes. +* (bzlmod) correctly watch sources when using `pypi_repo_utils`. + ## [0.35.0] - 2024-08-15 [0.35.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.35.0 diff --git a/examples/bzlmod/MODULE.bazel.lock b/examples/bzlmod/MODULE.bazel.lock index 9252d4f077..cbd0489b28 100644 --- a/examples/bzlmod/MODULE.bazel.lock +++ b/examples/bzlmod/MODULE.bazel.lock @@ -1231,7 +1231,7 @@ }, "@@rules_python~//python/extensions:pip.bzl%pip": { "general": { - "bzlTransitiveDigest": "9GnLEprSx4ta69xVzCkTB+6C2HjuV7YVLcAiwY9TomQ=", + "bzlTransitiveDigest": "HapONJ1RAsxO/oKgr5ozho+SMJBUXnmCwv/Z2+irLYI=", "usagesDigest": "MChlcSw99EuW3K7OOoMcXQIdcJnEh6YmfyjJm+9mxIg=", "recordedFileInputs": { "@@other_module~//requirements_lock_3_11.txt": "a7d0061366569043d5efcf80e34a32c732679367cb3c831c4cdc606adc36d314", @@ -1239,11 +1239,11 @@ "@@//requirements_lock_3_10.txt": "5e7083982a7e60f34998579a0ae83b520d46ab8f2552cc51337217f024e6def5", "@@rules_python~~internal_deps~pypi__packaging//BUILD.bazel": "8d36246aeefaab4b26fb9c1175cfaf13df5b6f1587e6753f1e78b132bad74795", "@@//whl_mods/appended_build_content.BUILD": "87745b00382c66e5efbd7cb44a08fc3edbf7fd5099cf593f87599188f1557a9e", - "@@rules_python~~python~python_3_9_host//python": "3f2abf225fa30c9e1c16e1d07d8db0b8ef0d368c260d365c1366027b76d7e1b0", "@@//requirements_lock_3_9.txt": "ee8cf067b458c95ca9a417036b4b7c25332412738447dbd783cdc631344fca26", "@@rules_python~~internal_deps~pypi__packaging//packaging-24.0.dist-info/RECORD": "be1aea790359b4c2c9ea83d153c1a57c407742a35b95ee36d00723509f5ed5dd", "@@//requirements_windows_3_10.txt": "c79f04bfaca147b8330275911a3328b81fc80828b9050a6bebdb15477627dabc", "@@rules_python~//BUILD.bazel": "c421a2c2f3f428d2685a16eb9cc3fb8662605aba4ef151a87a356678bb7e866d", + "@@rules_python~~python~python_3_9_host//BUILD.bazel": "cf97d5763b728ce5ba8fdc3243350b967658ba4e3879734504aee002cec0d2b3", "@@rules_python~//python/private/pypi/requirements_parser/resolve_target_platforms.py": "42bf51980528302373529bcdfddb8014e485182d6bc9d2f7d3bbe1f11d8d923d" }, "recordedDirentsInputs": {}, @@ -5910,7 +5910,7 @@ }, "@@rules_python~//python/private/pypi:pip.bzl%pip_internal": { "general": { - "bzlTransitiveDigest": "ObsRmNHKy0jYhsgsjPB1pDqpngC5yuUUTAWiOuHhvKk=", + "bzlTransitiveDigest": "AvN8NGX1Ou1eOlRF8GpQkRFf2GKGoTVHSYc9SLrWUbA=", "usagesDigest": "Y8ihY+R57BAFhalrVLVdJFrpwlbsiKz9JPJ99ljF7HA=", "recordedFileInputs": { "@@rules_python~//tools/publish/requirements.txt": "031e35d03dde03ae6305fe4b3d1f58ad7bdad857379752deede0f93649991b8a", diff --git a/python/private/pypi/pypi_repo_utils.bzl b/python/private/pypi/pypi_repo_utils.bzl index da449b4b50..196431636f 100644 --- a/python/private/pypi/pypi_repo_utils.bzl +++ b/python/private/pypi/pypi_repo_utils.bzl @@ -51,7 +51,20 @@ def _resolve_python_interpreter(mrctx, *, python_interpreter = None, python_inte python_interpreter = _get_python_interpreter_attr(mrctx, python_interpreter = python_interpreter) if python_interpreter_target != None: - python_interpreter = mrctx.path(python_interpreter_target) + # The following line would make the MODULE.bazel.lock platform + # independent, because the lock file will then contain a hash of the + # file so that the lock file can be recalculated, hence the best way is + # to add this directory to PATH. + # + # hence we add the root BUILD.bazel file and get the directory of that + # and construct the path differently. At the end of the day we don't + # want the hash of the interpreter to end up in the lock file. + if hasattr(python_interpreter_target, "same_package_label"): + root_build_bazel = python_interpreter_target.same_package_label("BUILD.bazel") + else: + root_build_bazel = python_interpreter_target.relative(":BUILD.bazel") + + python_interpreter = mrctx.path(root_build_bazel).dirname.get_child(python_interpreter_target.name) os = repo_utils.get_platforms_os_name(mrctx) @@ -110,7 +123,7 @@ def _execute_checked(mrctx, *, srcs, **kwargs): # 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.path(src)) + repo_utils.watch(mrctx, mrctx.path(src)) env = kwargs.pop("environment", {}) pythonpath = env.get("PYTHONPATH", "")