Skip to content

Commit f56fe42

Browse files
authored
fix(whl_library): correctly handle arch-specific deps in wheels (#2007)
It seems that there was a single-line bug that did not allow us to handle arch-specific deps and since the percentage of such wheels is extremely low, it went under the radar for quite some time. I am going to not implement any explicit passing of the default python version to `whl_library` as it is a much bigger change. Just doing this could be sufficient for the time being. Fixes #1996
1 parent 5dfc1c7 commit f56fe42

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ A brief description of the categories of changes:
4141
`interpreter_version_info` arg.
4242
* (bzlmod) Correctly pass `isolated`, `quiet` and `timeout` values to `whl_library`
4343
and drop the defaults from the lock file.
44+
* (whl_library) Correctly handle arch-specific dependencies when we encounter a
45+
platform specific wheel and use `experimental_target_platforms`.
46+
Fixes [#1996](https://github.com/bazelbuild/rules_python/issues/1996).
4447
* (rules) The first element of the default outputs is now the executable again.
4548

4649
### Removed
@@ -55,7 +58,7 @@ A brief description of the categories of changes:
5558
To enable it, set {obj}`--//python/config_settings:exec_tools_toolchain=enabled`.
5659
This toolchain must be enabled for precompilation to work. This toolchain will
5760
be enabled by default in a future release.
58-
Fixes [1967](https://github.com/bazelbuild/rules_python/issues/1967).
61+
Fixes [#1967](https://github.com/bazelbuild/rules_python/issues/1967).
5962

6063
## [0.33.1] - 2024-06-13
6164

python/private/pypi/whl_installer/wheel.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,12 @@ def __init__(
342342
self.name: str = Deps._normalize(name)
343343
self._platforms: Set[Platform] = platforms or set()
344344
self._target_versions = {p.minor_version for p in platforms or {}}
345-
self._add_version_select = platforms and len(self._target_versions) > 2
345+
self._default_minor_version = None
346+
if platforms and len(self._target_versions) > 2:
347+
# TODO @aignas 2024-06-23: enable this to be set via a CLI arg
348+
# for being more explicit.
349+
self._default_minor_version = host_interpreter_minor_version()
350+
346351
if None in self._target_versions and len(self._target_versions) > 2:
347352
raise ValueError(
348353
f"all python versions need to be specified explicitly, got: {platforms}"
@@ -540,21 +545,21 @@ def _add_req(self, req: Requirement, extras: Set[str]) -> None:
540545
):
541546
continue
542547

543-
if match_arch and self._add_version_select:
548+
if match_arch and self._default_minor_version:
544549
self._add(req.name, plat)
545-
if plat.minor_version == host_interpreter_minor_version():
550+
if plat.minor_version == self._default_minor_version:
546551
self._add(req.name, Platform(plat.os, plat.arch))
547552
elif match_arch:
548-
self._add(req.name, plat)
549-
elif match_os and self._add_version_select:
553+
self._add(req.name, Platform(plat.os, plat.arch))
554+
elif match_os and self._default_minor_version:
550555
self._add(req.name, Platform(plat.os, minor_version=plat.minor_version))
551-
if plat.minor_version == host_interpreter_minor_version():
556+
if plat.minor_version == self._default_minor_version:
552557
self._add(req.name, Platform(plat.os))
553558
elif match_os:
554559
self._add(req.name, Platform(plat.os))
555-
elif match_version and self._add_version_select:
560+
elif match_version and self._default_minor_version:
556561
self._add(req.name, Platform(minor_version=plat.minor_version))
557-
if plat.minor_version == host_interpreter_minor_version():
562+
if plat.minor_version == self._default_minor_version:
558563
self._add(req.name, Platform())
559564
elif match_version:
560565
self._add(req.name, None)

tests/pypi/whl_installer/wheel_test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,42 @@ def test_can_get_deps_based_on_specific_python_version(self):
217217
self.assertEqual(["bar"], py38_deps.deps)
218218
self.assertEqual({"@platforms//os:linux": ["posix_dep"]}, py38_deps.deps_select)
219219

220+
@mock.patch(
221+
"python.private.pypi.whl_installer.wheel.host_interpreter_minor_version"
222+
)
223+
def test_no_version_select_when_single_version(self, mock_host_interpreter_version):
224+
requires_dist = [
225+
"bar",
226+
"baz; python_version >= '3.8'",
227+
"posix_dep; os_name=='posix'",
228+
"posix_dep_with_version; os_name=='posix' and python_version >= '3.8'",
229+
"arch_dep; platform_machine=='x86_64' and python_version >= '3.8'",
230+
]
231+
mock_host_interpreter_version.return_value = 7
232+
233+
self.maxDiff = None
234+
235+
deps = wheel.Deps(
236+
"foo",
237+
requires_dist=requires_dist,
238+
platforms=[
239+
wheel.Platform(os=os, arch=wheel.Arch.x86_64, minor_version=minor)
240+
for minor in [8]
241+
for os in [wheel.OS.linux, wheel.OS.windows]
242+
],
243+
)
244+
got = deps.build()
245+
246+
self.assertEqual(["bar", "baz"], got.deps)
247+
self.assertEqual(
248+
{
249+
"@platforms//os:linux": ["posix_dep", "posix_dep_with_version"],
250+
"linux_x86_64": ["arch_dep", "posix_dep", "posix_dep_with_version"],
251+
"windows_x86_64": ["arch_dep"],
252+
},
253+
got.deps_select,
254+
)
255+
220256
@mock.patch(
221257
"python.private.pypi.whl_installer.wheel.host_interpreter_minor_version"
222258
)
@@ -227,6 +263,7 @@ def test_can_get_version_select(self, mock_host_interpreter_version):
227263
"baz_new; python_version >= '3.8'",
228264
"posix_dep; os_name=='posix'",
229265
"posix_dep_with_version; os_name=='posix' and python_version >= '3.8'",
266+
"arch_dep; platform_machine=='x86_64' and python_version < '3.8'",
230267
]
231268
mock_host_interpreter_version.return_value = 7
232269

@@ -251,6 +288,8 @@ def test_can_get_version_select(self, mock_host_interpreter_version):
251288
"@//python/config_settings:is_python_3.8": ["baz_new"],
252289
"@//python/config_settings:is_python_3.9": ["baz_new"],
253290
"@platforms//os:linux": ["baz", "posix_dep"],
291+
"cp37_linux_x86_64": ["arch_dep", "baz", "posix_dep"],
292+
"cp37_windows_x86_64": ["arch_dep", "baz"],
254293
"cp37_linux_anyarch": ["baz", "posix_dep"],
255294
"cp38_linux_anyarch": [
256295
"baz_new",
@@ -262,6 +301,8 @@ def test_can_get_version_select(self, mock_host_interpreter_version):
262301
"posix_dep",
263302
"posix_dep_with_version",
264303
],
304+
"linux_x86_64": ["arch_dep", "baz", "posix_dep"],
305+
"windows_x86_64": ["arch_dep", "baz"],
265306
},
266307
got.deps_select,
267308
)

0 commit comments

Comments
 (0)