From ad00c104d4b52d08284383e8ab2fe8d9b8ddff31 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Tue, 12 Nov 2024 10:18:57 -0800 Subject: [PATCH] fix: skip precompiling if using Bazel-builtin PyRuntimeInfo --- CHANGELOG.md | 4 +++- python/private/common_bazel.bzl | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e91eadf5a1..9ecf3442ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,9 @@ A brief description of the categories of changes: {#v0-0-0-fixed} ### Fixed -* Nothing yet +* (precompiling) Skip precompiling (instead of erroring) if the legacy + `@bazel_tools//tools/python:autodetecting_toolchain` is being used + ([#2364](https://github.com/bazelbuild/rules_python/issues/2364)). {#v0-0-0-added} ### Added diff --git a/python/private/common_bazel.bzl b/python/private/common_bazel.bzl index 9e1f3a8578..efbebd0252 100644 --- a/python/private/common_bazel.bzl +++ b/python/private/common_bazel.bzl @@ -166,10 +166,13 @@ def _precompile(ctx, src, *, use_pycache): stem = src.basename[:-(len(src.extension) + 1)] if use_pycache: - if not target_toolchain.pyc_tag: - # This is most likely because of a "runtime toolchain", i.e. the - # autodetecting toolchain, or some equivalent toolchain that can't - # assume to know the runtime Python version at build time. + if not hasattr(target_toolchain, "pyc_tag") or not target_toolchain.pyc_tag: + # This is likely one of two situations: + # 1. The pyc_tag attribute is missing because it's the Bazel-builtin + # PyRuntimeInfo object. + # 2. It's a "runtime toolchain", i.e. the autodetecting toolchain, + # or some equivalent toolchain that can't assume to know the + # runtime Python version at build time. # Instead of failing, just don't generate any pyc. return None pyc_path = "__pycache__/{stem}.{tag}.pyc".format(