Skip to content

Commit 0a3b8a8

Browse files
committed
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 bazel-contrib#1105, bazel-contrib#1868.
1 parent 777d7ed commit 0a3b8a8

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ A brief description of the categories of changes:
3333
### Removed
3434
* Nothing yet
3535

36+
### Fixed
37+
* (bzlmod) get the path to the host python interpreter without calling
38+
`mctx.path` on Labels that have differing contents on different OSes.
39+
* (bzlmod) correctly watch sources when using `pypi_repo_utils`.
40+
3641
## [0.35.0] - 2024-08-15
3742

3843
[0.35.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.35.0

examples/bzlmod/MODULE.bazel.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/pypi/pypi_repo_utils.bzl

+15-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,20 @@ def _resolve_python_interpreter(mrctx, *, python_interpreter = None, python_inte
5151
python_interpreter = _get_python_interpreter_attr(mrctx, python_interpreter = python_interpreter)
5252

5353
if python_interpreter_target != None:
54-
python_interpreter = mrctx.path(python_interpreter_target)
54+
# The following line would make the MODULE.bazel.lock platform
55+
# independent, because the lock file will then contain a hash of the
56+
# file so that the lock file can be recalculated, hence the best way is
57+
# to add this directory to PATH.
58+
#
59+
# hence we add the root BUILD.bazel file and get the directory of that
60+
# and construct the path differently. At the end of the day we don't
61+
# want the hash of the interpreter to end up in the lock file.
62+
if hasattr(python_interpreter_target, "same_package_label"):
63+
root_build_bazel = python_interpreter_target.same_package_label("BUILD.bazel")
64+
else:
65+
root_build_bazel = python_interpreter_target.relative(":BUILD.bazel")
66+
67+
python_interpreter = mrctx.path(root_build_bazel).dirname.get_child(python_interpreter_target.name)
5568

5669
os = repo_utils.get_platforms_os_name(mrctx)
5770

@@ -110,7 +123,7 @@ def _execute_checked(mrctx, *, srcs, **kwargs):
110123
# This will ensure that we will re-evaluate the bzlmod extension or
111124
# refetch the repository_rule when the srcs change. This should work on
112125
# Bazel versions without `mrctx.watch` as well.
113-
repo_utils.watch(mrctx.path(src))
126+
repo_utils.watch(mrctx, mrctx.path(src))
114127

115128
env = kwargs.pop("environment", {})
116129
pythonpath = env.get("PYTHONPATH", "")

0 commit comments

Comments
 (0)