From 8142b93b52c03d306078e5feda1e2e61bb340e27 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 +++++ python/private/pypi/pypi_repo_utils.bzl | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 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/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", "")