Skip to content

Commit 1d721ac

Browse files
ecalubaquibGoogle-ML-Automation
authored andcommitted
Update rules_python to latest 1.8.4 for tensorflow and xla. Doing so would enable us to use latest feature which includes a venv layout. This latest version also introduce stricter hermeticity which was first introduce in 1.7.0 as seen in logs:
- https://rules-python.readthedocs.io/en/latest/changelog.html#v1-7.0 (see sys.path order change and bootstraping) - pipstar flag default has been flipped to be on by default. It can be disabled through RULES_PYTHON_ENABLE_PIPSTAR=0 environment variable 1. Replace native bazel py_binary/py_library rules with rules_python's py_binary and py_library to selected BUILD files affected with updated to avoid %interpreter% error 2. Export PYTHONPATH build_pip_package.py 3. Add new commit sha and version for rules_python 1.8.4. Rules_python 1.8.4 added "scope" params to bool_flag and string_flag which is incompatible with bazel 7.7.0 used by TF, for now we can patch it 5. For all non bzlmod commands use RULES_PYTHON_ENABLE_PIPSTAR=0 6. Update patches for rules_python PiperOrigin-RevId: 868750024
1 parent 06f2977 commit 1d721ac

File tree

12 files changed

+83
-190
lines changed

12 files changed

+83
-190
lines changed

MODULE.bazel

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module(name = "xla")
77
bazel_dep(name = "abseil-cpp", version = "20250814.1", repo_name = "com_google_absl")
88
bazel_dep(name = "abseil-py", version = "2.1.0", repo_name = "absl_py")
99
bazel_dep(name = "bazel_features", version = "1.36.0")
10-
bazel_dep(name = "bazel_skylib", version = "1.8.1")
10+
bazel_dep(name = "bazel_skylib", version = "1.8.2")
1111
bazel_dep(name = "boringssl", version = "0.20250818.0")
1212
bazel_dep(name = "curl", version = "8.11.0")
1313
bazel_dep(name = "google_benchmark", version = "1.8.5", repo_name = "com_google_benchmark")
@@ -25,7 +25,7 @@ bazel_dep(name = "re2", version = "2025-11-05", repo_name = "com_googlesource_co
2525
bazel_dep(name = "rules_cc", version = "0.2.0")
2626
bazel_dep(name = "rules_java", version = "8.16.1")
2727
bazel_dep(name = "rules_license", version = "1.0.0")
28-
bazel_dep(name = "rules_python", version = "1.6.3")
28+
bazel_dep(name = "rules_python", version = "1.8.4")
2929
bazel_dep(name = "rules_shell", version = "0.6.1")
3030
bazel_dep(name = "snappy", version = "1.2.1")
3131
bazel_dep(name = "xxd", version = "9.1.0917")
@@ -128,8 +128,9 @@ single_version_override(
128128
"//third_party/py:rules_python_pip_version.patch",
129129
"//third_party/py:rules_python_freethreaded.patch",
130130
"//third_party/py:rules_python_versions.patch",
131+
"//third_party/py:rules_python_scope.patch",
131132
],
132-
version = "1.6.3",
133+
version = "1.8.4",
133134
)
134135

135136
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel
2+
index 06ef17beb2..d38639967f 100644
3+
--- a/scripts/BUILD.bazel
4+
+++ b/scripts/BUILD.bazel
5+
@@ -19,6 +19,7 @@
6+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
7+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
8+
# SOFTWARE.
9+
+load("@rules_python//python:defs.bzl", "py_binary")
10+
11+
py_binary(
12+
name = "print_version_file",

third_party/cython.BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Modified version of @cython//:BUILD.bazel
2+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
23

34
py_library(
45
name = "cython_lib",

third_party/py/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
22
load("@python_version_repo//:py_version.bzl", "REQUIREMENTS")
3+
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
34
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
45

6+
exports_files(["rules_python_scope.patch"])
7+
58
compile_pip_requirements(
69
name = "requirements",
710
extra_args = [

third_party/py/python_init_pip.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@ cc_library(
4242
extra_hub_aliases = {
4343
"numpy": ["numpy_headers"],
4444
},
45+
# NOTE: (Required for rules_python >= 1.7.0)
46+
# pipstar flag default has been flipped to be on by default.
47+
# It can be disabled through RULES_PYTHON_ENABLE_PIPSTAR=0
48+
# environment variable.
49+
envsubst = ["RULES_PYTHON_ENABLE_PIPSTAR"],
4550
requirements_lock = REQUIREMENTS_WITH_LOCAL_WHEELS,
4651
)

third_party/py/python_init_rules.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ def python_init_rules(extra_patches = []):
3434

3535
tf_http_archive(
3636
name = "rules_python",
37-
sha256 = "2f5c284fbb4e86045c2632d3573fc006facbca5d1fa02976e89dc0cd5488b590",
38-
strip_prefix = "rules_python-1.6.3",
39-
urls = tf_mirror_urls("https://github.com/bazelbuild/rules_python/releases/download/1.6.3/rules_python-1.6.3.tar.gz"),
37+
sha256 = "c85d5db38d3eac06167a13b10c9dba54b003a986cd4f1ebc00806b74e7c12f06",
38+
strip_prefix = "rules_python-1.8.4",
39+
urls = tf_mirror_urls("https://github.com/bazelbuild/rules_python/releases/download/1.8.4/rules_python-1.8.4.tar.gz"),
4040
patch_file = [
41-
"@xla//third_party/py:rules_python_pip_version.patch",
41+
"@xla//third_party/py:rules_python_scope.patch",
4242
"@xla//third_party/py:rules_python_freethreaded.patch",
4343
"@xla//third_party/py:rules_python_versions.patch",
4444
] + extra_patches,

third_party/py/python_init_toolchains.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def python_init_toolchains(name = "python", python_version = None, **kwargs):
5353
base_url = "",
5454
ignore_root_user_error = True,
5555
python_version = tool_version,
56+
# NOTE: @rules_python//python/config_settings:py_freethreaded=yes/no
57+
# isnt sufficient to configure freethreaded python toolchain.
58+
# Because this happens in bazel's phase 2 (build/test) and can fail
59+
# if init_toolchains runs pip parse during phase1 (fetch).
60+
python_version_kind = "ft" if "ft" in HERMETIC_PYTHON_VERSION else HERMETIC_PYTHON_VERSION_KIND,
5661
tool_versions = {
5762
tool_version: {
5863
"url": HERMETIC_PYTHON_URL,

third_party/py/rules_python_freethreaded.patch

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/python/private/python_register_toolchains.bzl b/python/private/python_register_toolchains.bzl
2-
index 2e0748de..42223d51 100644
2+
index 9e75c419..ba6d8f7a 100644
33
--- a/python/private/python_register_toolchains.bzl
44
+++ b/python/private/python_register_toolchains.bzl
55
@@ -94,6 +94,7 @@ def python_register_toolchains(
@@ -10,7 +10,7 @@ index 2e0748de..42223d51 100644
1010

1111
toolchain_repo_name = "{name}_toolchains".format(name = name)
1212

13-
@@ -189,6 +190,7 @@ def python_register_toolchains(
13+
@@ -174,6 +175,7 @@ def python_register_toolchains(
1414
name = name + "_host",
1515
platforms = loaded_platforms,
1616
python_version = python_version,
@@ -19,18 +19,9 @@ index 2e0748de..42223d51 100644
1919

2020
toolchains_repo(
2121
diff --git a/python/private/toolchains_repo.bzl b/python/private/toolchains_repo.bzl
22-
index 93bbb521..ad9cd7fd 100644
22+
index f7ff19c3..458f2885 100644
2323
--- a/python/private/toolchains_repo.bzl
2424
+++ b/python/private/toolchains_repo.bzl
25-
@@ -214,7 +214,7 @@ def python_toolchain_build_file_content(
26-
user_repository_name = "{}_{}".format(user_repository_name, platform),
27-
python_version = python_version,
28-
set_python_version_constraint = set_python_version_constraint,
29-
- target_settings = [],
30-
+ target_settings = meta.target_settings,
31-
))
32-
return "\n\n".join(entries)
33-
3425
@@ -445,6 +445,10 @@ Full python version, Major.Minor.Micro.
3526
Only set in workspace calls.
3627
""",
@@ -42,7 +33,7 @@ index 93bbb521..ad9cd7fd 100644
4233
"python_versions": attr.string_dict(
4334
doc = """
4435
If set, the Python version for the corresponding selected platform. Values in
45-
@@ -603,6 +609,9 @@ def _get_host_impl_repo_name(*, rctx, logger, python_version, os_name, cpu_name,
36+
@@ -603,6 +607,9 @@ def _get_host_impl_repo_name(*, rctx, logger, python_version, os_name, cpu_name,
4637
else:
4738
candidates = [preference]
4839

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
diff --git a/python/config_settings/BUILD.bazel b/python/config_settings/BUILD.bazel
2+
index 369989eb..b20a0da6 100644
3+
--- a/python/config_settings/BUILD.bazel
4+
+++ b/python/config_settings/BUILD.bazel
5+
@@ -245,7 +245,6 @@ label_flag(
6+
bool_flag(
7+
name = "experimental_python_import_all_repositories",
8+
build_setting_default = True,
9+
- scope = "universal",
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
@@ -253,20 +252,17 @@ bool_flag(
14+
name = "build_python_zip",
15+
build_setting_default = config.build_python_zip_default,
16+
help = "Build python executable zip. Defaults to on on Windows, off on other platforms",
17+
- scope = "universal",
18+
visibility = ["//visibility:public"],
19+
)
20+
21+
bool_flag(
22+
name = "incompatible_default_to_explicit_init_py",
23+
build_setting_default = False,
24+
- scope = "universal",
25+
visibility = ["//visibility:public"],
26+
)
27+
28+
string_flag(
29+
name = "python_path",
30+
build_setting_default = "python",
31+
- scope = "universal",
32+
visibility = ["//visibility:public"],
33+
)

0 commit comments

Comments
 (0)