Skip to content

Commit afc40f0

Browse files
authored
fix: Don't require default Python version for pip hubs (#1344)
This fixes the issue where a sub-module was required to always have a pip.parse() call configured for the default Python version if it used any pip.parse() call. Such a requirement puts sub-modules in an impossible situation: * If they don't have the default version, they'll get an error. * If they register the default version, but also register a specific version, they'll potentially cause an error if a root module changes the default to match their specific version (because two pip.parse() calls for the same version are made, which is an error). The requirement to have the default version registered for a pip hub was only present to satisfy the `whl_library_alias` repository rule, which needed a Python version to map `//conditions:default` to. To fix, the `whl_library_alias` rule's `default_version` arg is made optional. When None is passed, the `//conditions:default` condition is replaced with a `no_match_error` setting. This prevents the pip hub from being used with the version-unaware rules, but that makes sense: no wheels were setup for that version, so it's not like there is something that can be used anyways. Fixes #1320
1 parent bb8c485 commit afc40f0

File tree

11 files changed

+145
-42
lines changed

11 files changed

+145
-42
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# This lets us glob() up all the files inside the examples to make them inputs to tests
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, run tools/bazel_integration_test/update_deleted_packages.sh
6-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/entry_point,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/whl_mods,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,tests/compile_pip_requirements,tests/compile_pip_requirements_test_from_external_workspace,tests/ignore_root_user_error,tests/pip_repository_entry_points
7-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod/entry_point,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/whl_mods,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,tests/compile_pip_requirements,tests/compile_pip_requirements_test_from_external_workspace,tests/ignore_root_user_error,tests/pip_repository_entry_points
6+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_point,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,tests/compile_pip_requirements,tests/compile_pip_requirements_test_from_external_workspace,tests/ignore_root_user_error,tests/pip_repository_entry_points
7+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_point,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_install,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,tests/compile_pip_requirements,tests/compile_pip_requirements_test_from_external_workspace,tests/ignore_root_user_error,tests/pip_repository_entry_points
88

99
test --test_output=errors
1010

docs/pip.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@python_versions//3.11:defs.bzl", compile_pip_requirements_311 = "compile_pip_requirements")
2+
3+
# NOTE: To update the requirements, you need to uncomment the rules_python
4+
# override in the MODULE.bazel.
5+
compile_pip_requirements_311(
6+
name = "requirements",
7+
requirements_in = "requirements.in",
8+
requirements_txt = "requirements_lock_3_11.txt",
9+
)

examples/bzlmod/other_module/MODULE.bazel

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ module(
66
# that the parent module uses.
77
bazel_dep(name = "rules_python", version = "")
88

9-
# It is not best practice to use a python.toolchian in
10-
# a submodule. This code only exists to test that
11-
# we support doing this. This code is only for rules_python
12-
# testing purposes.
9+
# The story behind this commented out override:
10+
# This override is necessary to generate/update the requirements file
11+
# for this module. This is because running it via the outer
12+
# module doesn't work -- the `requirements.update` target can't find
13+
# the correct file to update.
14+
# Running in the submodule itself works, but submodules using overrides
15+
# is considered an error until Bazel 6.3, which prevents the outer module
16+
# from depending on this module.
17+
# So until 6.3 and higher is the minimum, we leave this commented out.
18+
# local_path_override(
19+
# module_name = "rules_python",
20+
# path = "../../..",
21+
# )
22+
1323
PYTHON_NAME_39 = "python_3_9"
1424

1525
PYTHON_NAME_311 = "python_3_11"
@@ -29,6 +39,20 @@ python.toolchain(
2939
# created by the above python.toolchain calls.
3040
use_repo(
3141
python,
42+
"python_versions",
3243
PYTHON_NAME_39,
3344
PYTHON_NAME_311,
3445
)
46+
47+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
48+
pip.parse(
49+
hub_name = "other_module_pip",
50+
# NOTE: This version must be different than the root module's
51+
# default python version.
52+
# This is testing that a sub-module can use pip.parse() and only specify
53+
# Python versions that DON'T include whatever the root-module's default
54+
# Python version is.
55+
python_version = "3.11",
56+
requirements_lock = ":requirements_lock_3_11.txt",
57+
)
58+
use_repo(pip, "other_module_pip")

examples/bzlmod/other_module/other_module/pkg/BUILD.bazel

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
load("@python_3_11//:defs.bzl", py_binary_311 = "py_binary")
1+
load(
2+
"@python_3_11//:defs.bzl",
3+
py_binary_311 = "py_binary",
4+
)
25
load("@rules_python//python:defs.bzl", "py_library")
36

47
py_library(
@@ -13,11 +16,16 @@ py_library(
1316
# used only when you need to support multiple versions of Python
1417
# in the same project.
1518
py_binary_311(
16-
name = "lib_311",
17-
srcs = ["lib.py"],
19+
name = "bin",
20+
srcs = ["bin.py"],
1821
data = ["data/data.txt"],
22+
main = "bin.py",
1923
visibility = ["//visibility:public"],
20-
deps = ["@rules_python//python/runfiles"],
24+
deps = [
25+
":lib",
26+
"@other_module_pip//absl_py",
27+
"@rules_python//python/runfiles",
28+
],
2129
)
2230

2331
exports_files(["data/data.txt"])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
3+
import absl
4+
5+
print("Python version:", sys.version)
6+
print("Module 'absl':", absl)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
absl-py
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.11
3+
# by the following command:
4+
#
5+
# bazel run //other_module/pkg:requirements.update
6+
#
7+
absl-py==1.4.0 \
8+
--hash=sha256:0d3fe606adfa4f7db64792dd4c7aee4ee0c38ab75dfd353b7a83ed3e957fcb47 \
9+
--hash=sha256:d2c244d01048ba476e7c080bd2c6df5e141d211de80223460d5b3b8a2a58433d
10+
# via -r other_module/pkg/requirements.in
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Tests to verify the root module can interact with the "other_module"
2+
# submodule.
3+
#
4+
# Note that other_module is seen as "our_other_module" due to repo-remapping
5+
# in the root module.
6+
7+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
8+
9+
build_test(
10+
name = "other_module_bin_build_test",
11+
targets = [
12+
"@our_other_module//other_module/pkg:bin",
13+
],
14+
)

python/extensions/pip.bzl

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -296,23 +296,18 @@ def _pip_impl(module_ctx):
296296

297297
for hub_name, whl_map in hub_whl_map.items():
298298
for whl_name, version_map in whl_map.items():
299-
if DEFAULT_PYTHON_VERSION not in version_map:
300-
fail((
301-
"Default python version '{version}' missing in pip " +
302-
"hub '{hub}': update your pip.parse() calls so that " +
303-
'includes `python_version = "{version}"`'
304-
).format(
305-
version = DEFAULT_PYTHON_VERSION,
306-
hub = hub_name,
307-
))
299+
if DEFAULT_PYTHON_VERSION in version_map:
300+
whl_default_version = DEFAULT_PYTHON_VERSION
301+
else:
302+
whl_default_version = None
308303

309304
# Create the alias repositories which contains different select
310305
# statements These select statements point to the different pip
311306
# whls that are based on a specific version of Python.
312307
whl_library_alias(
313308
name = hub_name + "_" + whl_name,
314309
wheel_name = whl_name,
315-
default_version = DEFAULT_PYTHON_VERSION,
310+
default_version = whl_default_version,
316311
version_map = version_map,
317312
)
318313

@@ -362,7 +357,7 @@ configured.
362357
mandatory = False,
363358
doc = """\
364359
A dict of labels to wheel names that is typically generated by the whl_modifications.
365-
The labels are JSON config files describing the modifications.
360+
The labels are JSON config files describing the modifications.
366361
""",
367362
),
368363
}, **pip_repository_attrs)
@@ -395,7 +390,7 @@ executable.""",
395390
),
396391
"copy_files": attr.string_dict(
397392
doc = """\
398-
(dict, optional): A mapping of `src` and `out` files for
393+
(dict, optional): A mapping of `src` and `out` files for
399394
[@bazel_skylib//rules:copy_file.bzl][cf]""",
400395
),
401396
"data": attr.string_list(
@@ -456,10 +451,10 @@ the BUILD files for wheels.
456451
attrs = _pip_parse_ext_attrs(),
457452
doc = """\
458453
This tag class is used to create a pip hub and all of the spokes that are part of that hub.
459-
This tag class reuses most of the pip attributes that are found in
454+
This tag class reuses most of the pip attributes that are found in
460455
@rules_python//python/pip_install:pip_repository.bzl.
461-
The exceptions are it does not use the args 'repo_prefix',
462-
and 'incompatible_generate_aliases'. We set the repository prefix
456+
The exceptions are it does not use the args 'repo_prefix',
457+
and 'incompatible_generate_aliases'. We set the repository prefix
463458
for the user and the alias arg is always True in bzlmod.
464459
""",
465460
),
@@ -483,7 +478,7 @@ def _whl_mods_repo_impl(rctx):
483478

484479
_whl_mods_repo = repository_rule(
485480
doc = """\
486-
This rule creates json files based on the whl_mods attribute.
481+
This rule creates json files based on the whl_mods attribute.
487482
""",
488483
implementation = _whl_mods_repo_impl,
489484
attrs = {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy