Skip to content

Commit 4f38119

Browse files
authored
fix(bzlmod): correctly wire the extra_pip_args (#2258)
Before this PR we were just dropping the `extra_pip_args` passed to `pip.parse` and were just using the args passed through the requirements file. Thanks to @swarren12 for pointing this out. This PR also passes `extra_pip_args` to `sdist` `whl_library` instances so that users can build the `sdists` correctly when using `experimental_index_url` feature. Summary: - pass `extra_pip_args` when building sdists in experimental mode - join `extra_pip_args` from the file and the pip.parse attr - test: add a test to ensure that the extra args are joined Fixes #2239 Closes #2254
1 parent 413690f commit 4f38119

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ A brief description of the categories of changes:
2828
* Nothing yet
2929

3030
### Fixed
31+
* (bzlmod) correctly wire the {attr}`pip.parse.extra_pip_args` all the
32+
way to {obj}`whl_library`. What is more we will pass the `extra_pip_args` to
33+
{obj}`whl_library` for `sdist` distributions when using
34+
{attr}`pip.parse.experimental_index_url`. See
35+
[#2239](https://github.com/bazelbuild/rules_python/issues/2239).
3136
* (whl_filegroup): Provide per default also the `RECORD` file
3237

3338
### Added

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/private/pypi/extension.bzl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides, group_map, s
182182
python_version = major_minor,
183183
logger = logger,
184184
),
185+
extra_pip_args = pip_attr.extra_pip_args,
185186
get_index_urls = get_index_urls,
186187
# NOTE @aignas 2024-08-02: , we will execute any interpreter that we find either
187188
# in the PATH or if specified as a label. We will configure the env
@@ -275,8 +276,13 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides, group_map, s
275276
if pip_attr.auth_patterns:
276277
whl_library_args["auth_patterns"] = pip_attr.auth_patterns
277278

278-
# pip is not used to download wheels and the python `whl_library` helpers are only extracting things
279-
whl_library_args.pop("extra_pip_args", None)
279+
if distribution.filename.endswith(".whl"):
280+
# pip is not used to download wheels and the python `whl_library` helpers are only extracting things
281+
whl_library_args.pop("extra_pip_args", None)
282+
else:
283+
# For sdists, they will be built by `pip`, so we still
284+
# need to pass the extra args there.
285+
pass
280286

281287
# This is no-op because pip is not used to download the wheel.
282288
whl_library_args.pop("download_only", None)

python/private/pypi/parse_requirements.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def parse_requirements(
4848
different package versions (or different packages) for different
4949
os, arch combinations.
5050
extra_pip_args (string list): Extra pip arguments to perform extra validations and to
51-
be joined with args fined in files.
51+
be joined with args found in files.
5252
get_index_urls: Callable[[ctx, list[str]], dict], a callable to get all
5353
of the distribution URLs from a PyPI index. Accepts ctx and
5454
distribution names to query.

tests/pypi/parse_requirements/parse_requirements_tests.bzl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def _mock_ctx():
2121
testdata = {
2222
"requirements_direct": """\
2323
foo[extra] @ https://some-url
24+
""",
25+
"requirements_extra_args": """\
26+
--index-url=example.org
27+
28+
foo[extra]==0.0.1 --hash=sha256:deadbeef
2429
""",
2530
"requirements_linux": """\
2631
foo==0.0.3 --hash=sha256:deadbaaf
@@ -93,6 +98,43 @@ def _test_simple(env):
9398

9499
_tests.append(_test_simple)
95100

101+
def _test_extra_pip_args(env):
102+
got = parse_requirements(
103+
ctx = _mock_ctx(),
104+
requirements_by_platform = {
105+
"requirements_extra_args": ["linux_x86_64"],
106+
},
107+
extra_pip_args = ["--trusted-host=example.org"],
108+
)
109+
env.expect.that_dict(got).contains_exactly({
110+
"foo": [
111+
struct(
112+
distribution = "foo",
113+
extra_pip_args = ["--index-url=example.org", "--trusted-host=example.org"],
114+
requirement_line = "foo[extra]==0.0.1 --hash=sha256:deadbeef",
115+
srcs = struct(
116+
requirement = "foo[extra]==0.0.1",
117+
shas = ["deadbeef"],
118+
version = "0.0.1",
119+
),
120+
target_platforms = [
121+
"linux_x86_64",
122+
],
123+
whls = [],
124+
sdist = None,
125+
is_exposed = True,
126+
),
127+
],
128+
})
129+
env.expect.that_str(
130+
select_requirement(
131+
got["foo"],
132+
platform = "linux_x86_64",
133+
).srcs.version,
134+
).equals("0.0.1")
135+
136+
_tests.append(_test_extra_pip_args)
137+
96138
def _test_dupe_requirements(env):
97139
got = parse_requirements(
98140
ctx = _mock_ctx(),

0 commit comments

Comments
 (0)