Skip to content

Commit 4e60d07

Browse files
authored
fix(render_pkg_aliases): correctly render when we have target_platforms set (bazel-contrib#2447)
It seems that during bazel-contrib#2424 I broke the rendering of aliases for the cases when the target platform is set. This means that the feature for multiplatform whls when `experimental_index_url` has never worked even though it was advertised. This ensures that the rendering is happening correctly and adds extra missing tests. Whilst at it: - add an extra test for `pip.parse` handling of env markers that I added to ensure that the error is not in the module extension. - Cleanup unused code - error message constant and the repo arg in `whl_config_setting`. Fixes bazel-contrib#2446
1 parent 077eec6 commit 4e60d07

File tree

5 files changed

+113
-26
lines changed

5 files changed

+113
-26
lines changed

python/private/pypi/extension.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def _create_whl_repos(
6868
pip_attr,
6969
whl_overrides,
7070
simpleapi_cache,
71+
evaluate_markers = evaluate_markers,
7172
available_interpreters = INTERPRETER_LABELS,
7273
simpleapi_download = simpleapi_download):
7374
"""create all of the whl repositories
@@ -78,6 +79,7 @@ def _create_whl_repos(
7879
whl_overrides: {type}`dict[str, struct]` - per-wheel overrides.
7980
simpleapi_cache: {type}`dict` - an opaque dictionary used for caching the results from calling
8081
SimpleAPI evaluating all of the tag class invocations {bzl:obj}`pip.parse`.
82+
evaluate_markers: the function to use to evaluate markers.
8183
simpleapi_download: Used for testing overrides
8284
available_interpreters: {type}`dict[str, Label]` The dictionary of available
8385
interpreters that have been registered using the `python` bzlmod extension.

python/private/pypi/render_pkg_aliases.bzl

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,17 @@ If the value is missing, then the "default" Python version is being used,
4444
which has a "null" version value and will not match version constraints.
4545
"""
4646

47-
NO_MATCH_ERROR_MESSAGE_TEMPLATE_V2 = """\
48-
No matching wheel for current configuration's Python version.
49-
50-
The current build configuration's Python version doesn't match any of the Python
51-
wheels available for this wheel. This wheel supports the following Python
52-
configuration settings:
53-
{config_settings}
54-
55-
To determine the current configuration's Python version, run:
56-
`bazel config <config id>` (shown further below)
57-
and look for
58-
{rules_python}//python/config_settings:python_version
59-
60-
If the value is missing, then the "default" Python version is being used,
61-
which has a "null" version value and will not match version constraints.
62-
"""
63-
6447
def _repr_dict(*, value_repr = repr, **kwargs):
6548
return {k: value_repr(v) for k, v in kwargs.items() if v}
6649

6750
def _repr_config_setting(alias):
68-
if alias.filename:
51+
if alias.filename or alias.target_platforms:
6952
return render.call(
7053
"whl_config_setting",
7154
**_repr_dict(
7255
filename = alias.filename,
7356
target_platforms = alias.target_platforms,
57+
config_setting = alias.config_setting,
7458
version = alias.version,
7559
)
7660
)

python/private/pypi/whl_config_setting.bzl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
"A small function to create an alias for a whl distribution"
1616

17-
def whl_config_setting(*, repo = None, version = None, config_setting = None, filename = None, target_platforms = None):
17+
def whl_config_setting(*, version = None, config_setting = None, filename = None, target_platforms = None):
1818
"""The bzl_packages value used by by the render_pkg_aliases function.
1919
2020
This contains the minimum amount of information required to generate correct
2121
aliases in a hub repository.
2222
2323
Args:
24-
repo: str, the repo of where to find the things to be aliased.
2524
version: optional(str), the version of the python toolchain that this
2625
whl alias is for. If not set, then non-version aware aliases will be
2726
constructed. This is mainly used for better error messages when there
@@ -43,7 +42,6 @@ def whl_config_setting(*, repo = None, version = None, config_setting = None, fi
4342
return struct(
4443
config_setting = config_setting,
4544
filename = filename,
46-
repo = repo,
4745
# Make the struct hashable
4846
target_platforms = tuple(target_platforms) if target_platforms else None,
4947
version = version,

tests/pypi/extension/extension_tests.bzl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,87 @@ def _test_simple_multiple_requirements(env):
245245

246246
_tests.append(_test_simple_multiple_requirements)
247247

248+
def _test_simple_with_markers(env):
249+
pypi = _parse_modules(
250+
env,
251+
module_ctx = _mock_mctx(
252+
_mod(
253+
name = "rules_python",
254+
parse = [
255+
_parse(
256+
hub_name = "pypi",
257+
python_version = "3.15",
258+
requirements_lock = "universal.txt",
259+
),
260+
],
261+
),
262+
read = lambda x: {
263+
"universal.txt": """\
264+
torch==2.4.1+cpu ; platform_machine == 'x86_64'
265+
torch==2.4.1 ; platform_machine != 'x86_64'
266+
""",
267+
}[x],
268+
),
269+
available_interpreters = {
270+
"python_3_15_host": "unit_test_interpreter_target",
271+
},
272+
evaluate_markers = lambda _, requirements, **__: {
273+
key: [
274+
platform
275+
for platform in platforms
276+
if ("x86_64" in platform and "platform_machine ==" in key) or ("x86_64" not in platform and "platform_machine !=" in key)
277+
]
278+
for key, platforms in requirements.items()
279+
},
280+
)
281+
282+
pypi.is_reproducible().equals(True)
283+
pypi.exposed_packages().contains_exactly({"pypi": ["torch"]})
284+
pypi.hub_group_map().contains_exactly({"pypi": {}})
285+
pypi.hub_whl_map().contains_exactly({"pypi": {
286+
"torch": {
287+
"pypi_315_torch_linux_aarch64_linux_arm_linux_ppc_linux_s390x_osx_aarch64": [
288+
whl_config_setting(
289+
target_platforms = [
290+
"cp315_linux_aarch64",
291+
"cp315_linux_arm",
292+
"cp315_linux_ppc",
293+
"cp315_linux_s390x",
294+
"cp315_osx_aarch64",
295+
],
296+
version = "3.15",
297+
),
298+
],
299+
"pypi_315_torch_linux_x86_64_osx_x86_64_windows_x86_64": [
300+
whl_config_setting(
301+
target_platforms = [
302+
"cp315_linux_x86_64",
303+
"cp315_osx_x86_64",
304+
"cp315_windows_x86_64",
305+
],
306+
version = "3.15",
307+
),
308+
],
309+
},
310+
}})
311+
pypi.whl_libraries().contains_exactly({
312+
"pypi_315_torch_linux_aarch64_linux_arm_linux_ppc_linux_s390x_osx_aarch64": {
313+
"dep_template": "@pypi//{name}:{target}",
314+
"python_interpreter_target": "unit_test_interpreter_target",
315+
"repo": "pypi_315",
316+
"requirement": "torch==2.4.1 ; platform_machine != 'x86_64'",
317+
},
318+
"pypi_315_torch_linux_x86_64_osx_x86_64_windows_x86_64": {
319+
"dep_template": "@pypi//{name}:{target}",
320+
"python_interpreter_target": "unit_test_interpreter_target",
321+
"repo": "pypi_315",
322+
"requirement": "torch==2.4.1+cpu ; platform_machine == 'x86_64'",
323+
},
324+
})
325+
pypi.whl_mods().contains_exactly({})
326+
327+
_tests.append(_test_simple_with_markers)
328+
248329
def _test_download_only_multiple(env):
249330
pypi = _parse_modules(
250331
env,

tests/pypi/render_pkg_aliases/render_pkg_aliases_test.bzl

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,24 @@ def _test_bzlmod_aliases(env):
7171
version = "3.2",
7272
config_setting = "//:my_config_setting",
7373
): "pypi_32_bar_baz",
74+
whl_config_setting(
75+
version = "3.2",
76+
config_setting = "//:my_config_setting",
77+
target_platforms = [
78+
"cp32_linux_x86_64",
79+
],
80+
): "pypi_32_bar_baz_linux_x86_64",
7481
whl_config_setting(
7582
version = "3.2",
7683
filename = "foo-0.0.0-py3-none-any.whl",
7784
): "filename_repo",
85+
whl_config_setting(
86+
version = "3.2",
87+
filename = "foo-0.0.0-py3-none-any.whl",
88+
target_platforms = [
89+
"cp32_linux_x86_64",
90+
],
91+
): "filename_repo_linux_x86_64",
7892
},
7993
},
8094
extra_hub_aliases = {"bar_baz": ["foo"]},
@@ -91,10 +105,20 @@ pkg_aliases(
91105
name = "bar_baz",
92106
actual = {
93107
"//:my_config_setting": "pypi_32_bar_baz",
108+
whl_config_setting(
109+
target_platforms = ("cp32_linux_x86_64",),
110+
config_setting = "//:my_config_setting",
111+
version = "3.2",
112+
): "pypi_32_bar_baz_linux_x86_64",
94113
whl_config_setting(
95114
filename = "foo-0.0.0-py3-none-any.whl",
96115
version = "3.2",
97116
): "filename_repo",
117+
whl_config_setting(
118+
filename = "foo-0.0.0-py3-none-any.whl",
119+
target_platforms = ("cp32_linux_x86_64",),
120+
version = "3.2",
121+
): "filename_repo_linux_x86_64",
98122
},
99123
extra_aliases = ["foo"],
100124
)"""
@@ -106,6 +130,7 @@ load("@rules_python//python/private/pypi:config_settings.bzl", "config_settings"
106130
config_settings(
107131
name = "config_settings",
108132
python_versions = ["3.2"],
133+
target_platforms = ["linux_x86_64"],
109134
visibility = ["//:__subpackages__"],
110135
)""",
111136
)
@@ -204,8 +229,8 @@ _tests.append(_test_get_python_versions)
204229
def _test_get_python_versions_with_target_platforms(env):
205230
got = get_whl_flag_versions(
206231
settings = [
207-
whl_config_setting(repo = "foo", version = "3.3", target_platforms = ["cp33_linux_x86_64"]),
208-
whl_config_setting(repo = "foo", version = "3.2", target_platforms = ["cp32_linux_x86_64", "cp32_osx_aarch64"]),
232+
whl_config_setting(version = "3.3", target_platforms = ["cp33_linux_x86_64"]),
233+
whl_config_setting(version = "3.2", target_platforms = ["cp32_linux_x86_64", "cp32_osx_aarch64"]),
209234
],
210235
)
211236
want = {
@@ -223,7 +248,6 @@ def _test_get_python_versions_from_filenames(env):
223248
got = get_whl_flag_versions(
224249
settings = [
225250
whl_config_setting(
226-
repo = "foo",
227251
version = "3.3",
228252
filename = "foo-0.0.0-py3-none-" + plat + ".whl",
229253
)
@@ -261,7 +285,6 @@ def _test_get_flag_versions_from_alias_target_platforms(env):
261285
got = get_whl_flag_versions(
262286
settings = [
263287
whl_config_setting(
264-
repo = "foo",
265288
version = "3.3",
266289
filename = "foo-0.0.0-py3-none-" + plat + ".whl",
267290
)
@@ -270,7 +293,6 @@ def _test_get_flag_versions_from_alias_target_platforms(env):
270293
]
271294
] + [
272295
whl_config_setting(
273-
repo = "foo",
274296
version = "3.3",
275297
filename = "foo-0.0.0-py3-none-any.whl",
276298
target_platforms = [

0 commit comments

Comments
 (0)