Skip to content

Commit f016977

Browse files
committed
fix(pip.parse): pass requirements without env markers to the whl_library
With this change the environment markers from the requirements.txt files no longer end up in the whl_library definitions. The alternative would be to fix this in the whl_library itself and do the string manipulation then. However, this means that we will be doing refetching of the repository when the markers change and the overall behaviour may be more complex. This solution also makes the MODULE.bazel.lock files simpler. That said, I am 50/50 on putting it in this way, so I can be easily convinced to put it in the whl_library if there is preference.
1 parent fac693f commit f016977

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ Unreleased changes template.
5858
### Fixed
5959
* (py_wheel) Use the default shell environment when building wheels to allow
6060
toolchains that search PATH to be used for the wheel builder tool.
61+
* (pip.parse) The requirement argument parsed to `whl_library` will now not have
62+
env marker information allowing `bazel query` to work in cases where the `whl`
63+
is available for all of the platforms and the sdist can be built.
64+
Work towards [#2450](https://github.com/bazelbuild/rules_python/issues/2450).
6165

6266
{#v0-0-0-added}
6367
### Added

python/private/pypi/extension.bzl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def _create_whl_repos(
292292
args.pop("download_only", None)
293293

294294
repo_name = whl_repo_name(pip_name, distribution.filename, distribution.sha256)
295-
args["requirement"] = requirement.srcs.requirement
295+
args["requirement"] = _requirement_without_marker(requirement.srcs.requirement)
296296
args["urls"] = [distribution.url]
297297
args["sha256"] = distribution.sha256
298298
args["filename"] = distribution.filename
@@ -324,7 +324,7 @@ def _create_whl_repos(
324324
logger.warn(lambda: "falling back to pip for installing the right file for {}".format(requirement.requirement_line))
325325

326326
args = dict(whl_library_args) # make a copy
327-
args["requirement"] = requirement.requirement_line
327+
args["requirement"] = _requirement_without_marker(requirement.requirement_line)
328328
if requirement.extra_pip_args:
329329
args["extra_pip_args"] = requirement.extra_pip_args
330330

@@ -535,6 +535,25 @@ You cannot use both the additive_build_content and additive_build_content_file a
535535
is_reproducible = is_reproducible,
536536
)
537537

538+
def _requirement_without_marker(requirement):
539+
requirement, _, maybe_hashes = requirement.partition(";")
540+
ret = requirement.strip(" ")
541+
_, marker, maybe_hashes = maybe_hashes.partition("--hash=")
542+
if not maybe_hashes:
543+
ret, marker, maybe_hashes = ret.partition("--hash=")
544+
545+
if maybe_hashes:
546+
return "{} {}{}".format(
547+
ret.strip(" "),
548+
marker.strip(" "),
549+
" --hash=".join([
550+
h.strip(" ")
551+
for h in maybe_hashes.split("--hash=")
552+
]),
553+
)
554+
else:
555+
return ret
556+
538557
def _pip_impl(module_ctx):
539558
"""Implementation of a class tag that creates the pip hub and corresponding pip spoke whl repositories.
540559

tests/pypi/extension/extension_tests.bzl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ def _mock_mctx(*modules, environ = {}, read = None):
2828
name = "unittest",
2929
arch = "exotic",
3030
),
31-
read = read or (lambda _: "simple==0.0.1 --hash=sha256:deadbeef --hash=sha256:deadbaaf"),
31+
read = read or (lambda _: """\
32+
simple==0.0.1 \
33+
--hash=sha256:deadbeef \
34+
--hash=sha256:deadbaaf"""),
3235
modules = [
3336
struct(
3437
name = modules[0].name,
@@ -262,7 +265,8 @@ def _test_simple_with_markers(env):
262265
read = lambda x: {
263266
"universal.txt": """\
264267
torch==2.4.1+cpu ; platform_machine == 'x86_64'
265-
torch==2.4.1 ; platform_machine != 'x86_64'
268+
torch==2.4.1 ; platform_machine != 'x86_64' \
269+
--hash=sha256:deadbeef
266270
""",
267271
}[x],
268272
),
@@ -313,13 +317,13 @@ torch==2.4.1 ; platform_machine != 'x86_64'
313317
"dep_template": "@pypi//{name}:{target}",
314318
"python_interpreter_target": "unit_test_interpreter_target",
315319
"repo": "pypi_315",
316-
"requirement": "torch==2.4.1 ; platform_machine != 'x86_64'",
320+
"requirement": "torch==2.4.1 --hash=sha256:deadbeef",
317321
},
318322
"pypi_315_torch_linux_x86_64_osx_x86_64_windows_x86_64": {
319323
"dep_template": "@pypi//{name}:{target}",
320324
"python_interpreter_target": "unit_test_interpreter_target",
321325
"repo": "pypi_315",
322-
"requirement": "torch==2.4.1+cpu ; platform_machine == 'x86_64'",
326+
"requirement": "torch==2.4.1+cpu",
323327
},
324328
})
325329
pypi.whl_mods().contains_exactly({})
@@ -351,16 +355,19 @@ def _test_download_only_multiple(env):
351355
--implementation=cp
352356
--abi=cp315
353357
354-
simple==0.0.1 --hash=sha256:deadbeef
355-
extra==0.0.1 --hash=sha256:deadb00f
358+
simple==0.0.1 \
359+
--hash=sha256:deadbeef
360+
extra==0.0.1 \
361+
--hash=sha256:deadb00f
356362
""",
357363
"requirements.osx_aarch64.txt": """\
358364
--platform=macosx_10_9_arm64
359365
--python-version=315
360366
--implementation=cp
361367
--abi=cp315
362368
363-
simple==0.0.3 --hash=sha256:deadbaaf
369+
simple==0.0.3 \
370+
--hash=sha256:deadbaaf
364371
""",
365372
}[x],
366373
),
@@ -473,7 +480,9 @@ def _test_simple_get_index(env):
473480
),
474481
read = lambda x: {
475482
"requirements.txt": """
476-
simple==0.0.1 --hash=sha256:deadbeef --hash=sha256:deadb00f
483+
simple==0.0.1 \
484+
--hash=sha256:deadbeef \
485+
--hash=sha256:deadb00f
477486
some_pkg==0.0.1
478487
""",
479488
}[x],

0 commit comments

Comments
 (0)