Skip to content

Commit 972a424

Browse files
committed
Merge branch 'main' of https://github.com/bazel-contrib/rules_python into pr-3404
2 parents cd86638 + 411b937 commit 972a424

13 files changed

Lines changed: 128 additions & 121 deletions

File tree

.github/workflows/mypy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ubuntu-latest
1919
steps:
2020
# Checkout the code
21-
- uses: actions/checkout@v5
21+
- uses: actions/checkout@v6
2222
- uses: jpetrucciani/mypy-check@master
2323
with:
2424
requirements: 1.6.0

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
runs-on: ubuntu-latest
4141
steps:
4242
- name: Checkout
43-
uses: actions/checkout@v5
43+
uses: actions/checkout@v6
4444
with:
4545
ref: ${{ github.ref_name }}
4646
- name: Create release archive and notes
@@ -71,7 +71,7 @@ jobs:
7171
runs-on: ubuntu-latest
7272
steps:
7373
- name: Checkout
74-
uses: actions/checkout@v5
74+
uses: actions/checkout@v6
7575
with:
7676
ref: ${{ github.tag_name || github.ref_name }}
7777
- if: github.event_name == 'push' || github.event.inputs.publish_to_pypi

BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ filegroup(
4545
"version.bzl",
4646
"//python:distribution",
4747
"//tools:distribution",
48-
"@rules_python_gazelle_plugin//:distribution",
4948
],
5049
visibility = [
5150
"//:__subpackages__",

MODULE.bazel

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,6 @@ bazel_dep(name = "another_module", version = "0", dev_dependency = True)
228228
# We use `WORKSPACE.bzlmod` because it is impossible to have dev-only local overrides.
229229
bazel_dep(name = "rules_go", version = "0.41.0", dev_dependency = True, repo_name = "io_bazel_rules_go")
230230

231-
##bazel_dep(name = "rules_go", version = "0.59.0", dev_dependency = True, repo_name = "io_bazel_rules_go")
232-
bazel_dep(name = "rules_python_gazelle_plugin", version = "0", dev_dependency = True)
233-
bazel_dep(name = "gazelle", version = "0.40.0", dev_dependency = True, repo_name = "bazel_gazelle")
234-
##bazel_dep(name = "gazelle", version = "0.47.0", dev_dependency = True, repo_name = "bazel_gazelle")
235-
236231
internal_dev_deps = use_extension(
237232
"//python/private:internal_dev_deps.bzl",
238233
"internal_dev_deps",
@@ -261,13 +256,6 @@ dev_rules_python_config.add_transition_setting(
261256
setting = "//tests/multi_pypi:external_deps_name",
262257
)
263258

264-
# Add gazelle plugin so that we can run the gazelle example as an e2e integration
265-
# test and include the distribution files.
266-
local_path_override(
267-
module_name = "rules_python_gazelle_plugin",
268-
path = "gazelle",
269-
)
270-
271259
local_path_override(
272260
module_name = "other",
273261
path = "tests/modules/other",

python/private/internal_config_repo.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ config = struct(
3232
enable_pystar = True,
3333
enable_pipstar = {enable_pipstar},
3434
enable_deprecation_warnings = {enable_deprecation_warnings},
35+
bazel_8_or_later = {bazel_8_or_later},
3536
bazel_9_or_later = {bazel_9_or_later},
3637
BuiltinPyInfo = getattr(getattr(native, "legacy_globals", None), "PyInfo", {builtin_py_info_symbol}),
3738
BuiltinPyRuntimeInfo = getattr(getattr(native, "legacy_globals", None), "PyRuntimeInfo", {builtin_py_runtime_info_symbol}),
@@ -107,6 +108,7 @@ def _internal_config_repo_impl(rctx):
107108
builtin_py_info_symbol = builtin_py_info_symbol,
108109
builtin_py_runtime_info_symbol = builtin_py_runtime_info_symbol,
109110
builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider,
111+
bazel_8_or_later = str(bazel_major_version >= 8),
110112
bazel_9_or_later = str(bazel_major_version >= 9),
111113
))
112114

python/private/pypi/hub_builder.bzl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ def _create_whl_repos(
440440
pip_attr = pip_attr,
441441
enable_pipstar = enable_pipstar,
442442
)
443+
444+
interpreter = _detect_interpreter(self, pip_attr)
445+
443446
for whl in requirements_by_platform:
444447
whl_library_args = common_args | _whl_library_args(
445448
self,
@@ -456,6 +459,7 @@ def _create_whl_repos(
456459
auth_patterns = self._config.auth_patterns or pip_attr.auth_patterns,
457460
python_version = _major_minor_version(pip_attr.python_version),
458461
is_multiple_versions = whl.is_multiple_versions,
462+
interpreter = interpreter,
459463
enable_pipstar = enable_pipstar,
460464
)
461465
_add_whl_library(
@@ -467,8 +471,6 @@ def _create_whl_repos(
467471
)
468472

469473
def _common_args(self, module_ctx, *, pip_attr, enable_pipstar):
470-
interpreter = _detect_interpreter(self, pip_attr)
471-
472474
# Construct args separately so that the lock file can be smaller and does not include unused
473475
# attrs.
474476
whl_library_args = dict(
@@ -483,8 +485,6 @@ def _common_args(self, module_ctx, *, pip_attr, enable_pipstar):
483485
environment = pip_attr.environment,
484486
envsubst = pip_attr.envsubst,
485487
pip_data_exclude = pip_attr.pip_data_exclude,
486-
python_interpreter = interpreter.path,
487-
python_interpreter_target = interpreter.target,
488488
)
489489
if not enable_pipstar:
490490
maybe_args["experimental_target_platforms"] = pip_attr.experimental_target_platforms
@@ -536,6 +536,7 @@ def _whl_repo(
536536
auth_patterns,
537537
python_version,
538538
use_downloader,
539+
interpreter,
539540
enable_pipstar = False):
540541
args = dict(whl_library_args)
541542
args["requirement"] = src.requirement_line
@@ -548,6 +549,12 @@ def _whl_repo(
548549
# need to pass the extra args there, so only pop this for whls
549550
args["extra_pip_args"] = src.extra_pip_args
550551

552+
if "whl_patches" in args or not (enable_pipstar and is_whl):
553+
if interpreter.path:
554+
args["python_interpreter"] = interpreter.path
555+
if interpreter.target:
556+
args["python_interpreter_target"] = interpreter.target
557+
551558
if not src.url or (not is_whl and download_only):
552559
if download_only and use_downloader:
553560
# If the user did not allow using sdists and we are using the downloader

python/private/pypi/parse_requirements.bzl

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,35 @@ def parse_requirements(
188188
for p in r.target_platforms:
189189
requirement_target_platforms[p] = None
190190

191+
package_srcs = _package_srcs(
192+
name = name,
193+
reqs = reqs,
194+
index_urls = index_urls,
195+
platforms = platforms,
196+
extract_url_srcs = extract_url_srcs,
197+
logger = logger,
198+
)
199+
200+
# FIXME @aignas 2025-11-24: we can get the list of target platforms here
201+
#
202+
# However it is likely that we may stop exposing packages like torch in here
203+
# which do not have wheels for all osx platforms.
204+
#
205+
# If users specify the target platforms accurately, then it is a different
206+
# (better) story, but we may not be able to guarantee this
207+
#
208+
# target_platforms = [
209+
# p
210+
# for dist in package_srcs
211+
# for p in dist.target_platforms
212+
# ]
213+
191214
item = struct(
192215
# Return normalized names
193216
name = normalize_name(name),
194217
is_exposed = len(requirement_target_platforms) == len(requirements),
195218
is_multiple_versions = len(reqs.values()) > 1,
196-
srcs = _package_srcs(
197-
name = name,
198-
reqs = reqs,
199-
index_urls = index_urls,
200-
platforms = platforms,
201-
extract_url_srcs = extract_url_srcs,
202-
logger = logger,
203-
),
219+
srcs = package_srcs,
204220
)
205221
ret.append(item)
206222
if not item.is_exposed and logger:
@@ -234,7 +250,7 @@ def _package_srcs(
234250
platforms.keys(),
235251
))
236252

237-
dist = _add_dists(
253+
dist, can_fallback = _add_dists(
238254
requirement = r,
239255
target_platform = platforms.get(target_platform),
240256
index_urls = index_urls.get(name),
@@ -244,14 +260,16 @@ def _package_srcs(
244260

245261
if extract_url_srcs and dist:
246262
req_line = r.srcs.requirement
247-
else:
263+
elif can_fallback:
248264
dist = struct(
249265
url = "",
250266
filename = "",
251267
sha256 = "",
252268
yanked = False,
253269
)
254270
req_line = r.srcs.requirement_line
271+
else:
272+
continue
255273

256274
key = (
257275
dist.filename,
@@ -337,14 +355,22 @@ def _add_dists(*, requirement, index_urls, target_platform, logger = None):
337355
index_urls: The result of simpleapi_download.
338356
target_platform: The target_platform information.
339357
logger: A logger for printing diagnostic info.
358+
359+
Returns:
360+
(dist, can_fallback_to_pip): a struct with distribution details and how to fetch
361+
it and a boolean flag to tell the other layers if we should add an entry to
362+
fallback for pip if there are no supported whls found - if there is an sdist, we
363+
can attempt the fallback, otherwise better to not, because the pip command will
364+
fail and the error message will be confusing. What is more that would lead to
365+
breakage of the bazel query.
340366
"""
341367

342368
if requirement.srcs.url:
343369
if not requirement.srcs.filename:
344370
logger.debug(lambda: "Could not detect the filename from the URL, falling back to pip: {}".format(
345371
requirement.srcs.url,
346372
))
347-
return None
373+
return None, True
348374

349375
# Handle direct URLs in requirements
350376
dist = struct(
@@ -354,13 +380,10 @@ def _add_dists(*, requirement, index_urls, target_platform, logger = None):
354380
yanked = False,
355381
)
356382

357-
if dist.filename.endswith(".whl"):
358-
return dist
359-
else:
360-
return dist
383+
return dist, False
361384

362385
if not index_urls:
363-
return None
386+
return None, True
364387

365388
whls = []
366389
sdist = None
@@ -403,7 +426,14 @@ def _add_dists(*, requirement, index_urls, target_platform, logger = None):
403426

404427
if not target_platform:
405428
# The pipstar platforms are undefined here, so we cannot do any matching
406-
return sdist
429+
return sdist, True
430+
431+
if not whls and not sdist:
432+
# If there are no suitable wheels to handle for now allow fallback to pip, it
433+
# may be a little bit more helpful when debugging? Most likely something is
434+
# going a bit wrong here, should we raise an error because the sha256 have most
435+
# likely mismatched? We are already printing a warning above.
436+
return None, True
407437

408438
# Select a single wheel that can work on the target_platform
409439
return select_whl(
@@ -413,4 +443,4 @@ def _add_dists(*, requirement, index_urls, target_platform, logger = None):
413443
whl_abi_tags = target_platform.whl_abi_tags,
414444
whl_platform_tags = target_platform.whl_platform_tags,
415445
logger = logger,
416-
) or sdist
446+
) or sdist, sdist != None

python/private/pypi/patch_whl.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ def patch_whl(rctx, *, python_interpreter, whl_path, patches, **kwargs):
8787
# symlink to a zip file to use bazel's extract so that we can use bazel's
8888
# repository_ctx patch implementation. The whl file may be in a different
8989
# external repository.
90+
#
91+
# TODO @aignas 2025-11-24: remove this symlinking workaround when we drop support for bazel 7
9092
whl_file_zip = whl_input.basename + ".zip"
9193
rctx.symlink(whl_input, whl_file_zip)
9294
rctx.extract(whl_file_zip)

python/private/pypi/whl_library.bzl

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -377,21 +377,17 @@ def _whl_library_impl(rctx):
377377
#
378378
# Remove non-pipstar and config_load check when we release rules_python 2.
379379
if enable_pipstar:
380-
pypi_repo_utils.execute_checked(
381-
rctx,
382-
op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path),
383-
python = python_interpreter,
384-
arguments = args + [
385-
"--whl-file",
386-
whl_path,
387-
"--enable-pipstar",
388-
],
389-
srcs = rctx.attr._python_srcs,
390-
environment = environment,
391-
quiet = rctx.attr.quiet,
392-
timeout = rctx.attr.timeout,
393-
logger = logger,
380+
if rp_config.bazel_8_or_later:
381+
extract_path = whl_path
382+
else:
383+
extract_path = rctx.path(whl_path.basename + ".zip")
384+
rctx.symlink(whl_path, extract_path)
385+
rctx.extract(
386+
archive = extract_path,
387+
output = "site-packages",
394388
)
389+
if not rp_config.bazel_8_or_later:
390+
rctx.delete(extract_path)
395391

396392
metadata = whl_metadata(
397393
install_dir = whl_path.dirname.get_child("site-packages"),

tests/bootstrap_impls/BUILD.bazel

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ py_reconfig_test(
133133
env = {"BOOTSTRAP": "system_python"},
134134
imports = ["./site-packages"],
135135
main = "sys_path_order_test.py",
136-
deps = [
137-
"@bazel_tools//tools/python/runfiles",
138-
],
139136
)
140137

141138
py_reconfig_test(

0 commit comments

Comments
 (0)