Skip to content

Commit

Permalink
fix(bzlmod): keep the lockfile platform independent when resolving py…
Browse files Browse the repository at this point in the history
…thon

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 bazelbuild#1105, bazelbuild#1868.
  • Loading branch information
aignas committed Aug 21, 2024
1 parent 65f6efa commit 43e0d16
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions examples/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions python/private/pypi/pypi_repo_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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", "")
Expand Down

0 comments on commit 43e0d16

Please sign in to comment.