Skip to content

Commit 530fd85

Browse files
authored
fix(bzlmod pip): ensure that sub-modules do not have invalid repos (#1549)
This fixes the cases where the 'default_version' is passed to the 'render_pkg_aliases' utility but the 'default_version' is not present for the wheels. This usually happens when a sub-module is using the 'pip.parse' extension and the default_version can only be set by the root module. Previously, such a case would generate a `select()` expression that mapped the default condition to a non-existent target (because the sub-module didn't call `pip.parse()` with that version). This would either result in errors due the target not existing, or silently using a target intended for a different Python version (which may work, but isn't correct to so). Now, it results in a error via `select.no_match_error`. Fixes #1548.
1 parent d96214f commit 530fd85

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ Breaking changes:
8686
* (gazelle) Generate a single `py_test` target when `gazelle:python_generation_mode project`
8787
is used.
8888

89+
* (bzlmod) sub-modules now don't have the `//conditions:default` clause in the
90+
hub repos created by `pip.parse`. This should fix confusing error messages
91+
in case there is a misconfiguration of toolchains or a bug in `rules_python`.
92+
8993
### Added
9094

9195
* (bzlmod) Added `.whl` patching support via `patches` and `patch_strip`

python/private/render_pkg_aliases.bzl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ def _render_common_aliases(repo_name, name, versions = None, default_version = N
109109
if versions:
110110
versions = sorted(versions)
111111

112-
if versions and not default_version:
112+
if not versions:
113+
pass
114+
elif default_version in versions:
115+
pass
116+
else:
113117
error_msg = NO_MATCH_ERROR_MESSAGE_TEMPLATE.format(
114118
supported_versions = ", ".join(versions),
115119
rules_python = rules_python,
@@ -119,6 +123,10 @@ def _render_common_aliases(repo_name, name, versions = None, default_version = N
119123
error_msg = error_msg,
120124
))
121125

126+
# This is to simplify the code in _render_whl_library_alias and to ensure
127+
# that we don't pass a 'default_version' that is not in 'versions'.
128+
default_version = None
129+
122130
lines.append(
123131
render.alias(
124132
name = name,

tests/pip_hub_repository/render_pkg_aliases/render_pkg_aliases_test.bzl

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,93 @@ alias(
222222

223223
_tests.append(_test_bzlmod_aliases_with_no_default_version)
224224

225+
def _test_bzlmod_aliases_for_non_root_modules(env):
226+
actual = render_pkg_aliases(
227+
default_version = "3.2.4",
228+
repo_name = "pypi",
229+
rules_python = "rules_python",
230+
whl_map = {
231+
"bar-baz": ["3.2.3", "3.1.3"],
232+
},
233+
)
234+
235+
want_key = "bar_baz/BUILD.bazel"
236+
want_content = """\
237+
package(default_visibility = ["//visibility:public"])
238+
239+
_NO_MATCH_ERROR = \"\"\"\\
240+
No matching wheel for current configuration's Python version.
241+
242+
The current build configuration's Python version doesn't match any of the Python
243+
versions available for this wheel. This wheel supports the following Python versions:
244+
3.1.3, 3.2.3
245+
246+
As matched by the `@rules_python//python/config_settings:is_python_<version>`
247+
configuration settings.
248+
249+
To determine the current configuration's Python version, run:
250+
`bazel config <config id>` (shown further below)
251+
and look for
252+
rules_python//python/config_settings:python_version
253+
254+
If the value is missing, then the "default" Python version is being used,
255+
which has a "null" version value and will not match version constraints.
256+
\"\"\"
257+
258+
alias(
259+
name = "bar_baz",
260+
actual = ":pkg",
261+
)
262+
263+
alias(
264+
name = "pkg",
265+
actual = select(
266+
{
267+
"@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:pkg",
268+
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:pkg",
269+
},
270+
no_match_error = _NO_MATCH_ERROR,
271+
),
272+
)
273+
274+
alias(
275+
name = "whl",
276+
actual = select(
277+
{
278+
"@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:whl",
279+
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:whl",
280+
},
281+
no_match_error = _NO_MATCH_ERROR,
282+
),
283+
)
284+
285+
alias(
286+
name = "data",
287+
actual = select(
288+
{
289+
"@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:data",
290+
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:data",
291+
},
292+
no_match_error = _NO_MATCH_ERROR,
293+
),
294+
)
295+
296+
alias(
297+
name = "dist_info",
298+
actual = select(
299+
{
300+
"@@rules_python//python/config_settings:is_python_3.1.3": "@pypi_31_bar_baz//:dist_info",
301+
"@@rules_python//python/config_settings:is_python_3.2.3": "@pypi_32_bar_baz//:dist_info",
302+
},
303+
no_match_error = _NO_MATCH_ERROR,
304+
),
305+
)"""
306+
307+
env.expect.that_collection(actual.keys()).contains_exactly([want_key])
308+
env.expect.that_str(actual[want_key]).equals(want_content)
309+
310+
_tests.append(_test_bzlmod_aliases_for_non_root_modules)
311+
225312
def _test_bzlmod_aliases_are_created_for_all_wheels(env):
226313
actual = render_pkg_aliases(
227314
default_version = "3.2.3",

0 commit comments

Comments
 (0)