Skip to content

Commit 2f6ca17

Browse files
authored
fix: skip precompiling if using Bazel-builtin PyRuntimeInfo (bazel-contrib#2394)
When the `@bazel_tools//python/tools:python_autodetecting` toolchain is used, it returns the Bazel-builtin PyRuntimeInfo provider. Because this provider doesn't support the additional fields needed for precompiling (`pyc_tag`, among others), it would result in an attribute error. To fix, detect the absence of the `pyc_tag` attribute and return early, as is done when the pyc_tag field is empty. Fixes bazel-contrib#2364
1 parent 1b2714e commit 2f6ca17

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ A brief description of the categories of changes:
4949

5050
{#v0-0-0-fixed}
5151
### Fixed
52-
* Nothing yet
52+
* (precompiling) Skip precompiling (instead of erroring) if the legacy
53+
`@bazel_tools//tools/python:autodetecting_toolchain` is being used
54+
([#2364](https://github.com/bazelbuild/rules_python/issues/2364)).
5355

5456
{#v0-0-0-added}
5557
### Added

python/private/common_bazel.bzl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,13 @@ def _precompile(ctx, src, *, use_pycache):
166166

167167
stem = src.basename[:-(len(src.extension) + 1)]
168168
if use_pycache:
169-
if not target_toolchain.pyc_tag:
170-
# This is most likely because of a "runtime toolchain", i.e. the
171-
# autodetecting toolchain, or some equivalent toolchain that can't
172-
# assume to know the runtime Python version at build time.
169+
if not hasattr(target_toolchain, "pyc_tag") or not target_toolchain.pyc_tag:
170+
# This is likely one of two situations:
171+
# 1. The pyc_tag attribute is missing because it's the Bazel-builtin
172+
# PyRuntimeInfo object.
173+
# 2. It's a "runtime toolchain", i.e. the autodetecting toolchain,
174+
# or some equivalent toolchain that can't assume to know the
175+
# runtime Python version at build time.
173176
# Instead of failing, just don't generate any pyc.
174177
return None
175178
pyc_path = "__pycache__/{stem}.{tag}.pyc".format(

0 commit comments

Comments
 (0)