Skip to content

Commit 5cd59eb

Browse files
aignasrickeylev
andcommitted
fix(toolchain): disable exec toolchain by default (#1968)
This makes the exec tools toolchain disabled by default to prevent toolchain resolution from matching it and inadvertently pulling in a dependency on the hermetic runtimes. While the hermetic runtime wouldn't actually be used (precompiling is disabled by default), the dependency triggered downloading of the runtimes, which breaks environments which forbid remote downloads they haven't vetted (such a case is Bazel's own build process). To fix this, a flag is added to control if the exec tools toolchain is enabled or not. When disabled (the default), the toolchain won't match, and the remote dependency isn't triggered. Fixes #1967. Cherry-pick of cf1f36d. --------- Co-authored-by: Richard Levasseur <rlevasseur@google.com>
1 parent b07525c commit 5cd59eb

File tree

10 files changed

+89
-5
lines changed

10 files changed

+89
-5
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ A brief description of the categories of changes:
3333
### Removed
3434
* Nothing yet
3535

36+
## [0.33.2] - 2024-06-13
37+
38+
[0.33.2]: https://github.com/bazelbuild/rules_python/releases/tag/0.33.2
39+
40+
### Fixed
41+
* (toolchains) The {obj}`exec_tools_toolchain_type` is disabled by default.
42+
To enable it, set {obj}`--//python/config_settings:exec_tools_toolchain=enabled`.
43+
This toolchain must be enabled for precompilation to work. This toolchain will
44+
be enabled by default in a future release.
45+
Fixes [1967](https://github.com/bazelbuild/rules_python/issues/1967).
46+
3647
## [0.33.1] - 2024-06-13
3748

3849
[0.33.1]: https://github.com/bazelbuild/rules_python/releases/tag/0.33.1

docs/sphinx/api/python/config_settings/index.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ Determines the default hermetic Python toolchain version. This can be set to
1010
one of the values that `rules_python` maintains.
1111
:::
1212

13+
::::{bzl:flag} exec_tools_toolchain
14+
Determines if the {obj}`exec_tools_toolchain_type` toolchain is enabled.
15+
16+
:::{note}
17+
* Note that this only affects the rules_python generated toolchains.
18+
:::
19+
20+
Values:
21+
22+
* `enabled`: Allow matching of the registered toolchains at build time.
23+
* `disabled`: Prevent the toolchain from being matched at build time.
24+
25+
:::{versionadded} 0.33.2
26+
:::
27+
::::
28+
1329
::::{bzl:flag} precompile
1430
Determines if Python source files should be compiled at build time.
1531

@@ -34,6 +50,8 @@ Values:
3450
* `force_disabled`: Like `disabled`, except overrides target-level setting. This
3551
is useful useful for development, testing enabling precompilation more
3652
broadly, or as an escape hatch if build-time compiling is not available.
53+
:::{versionadded} 0.33.0
54+
:::
3755
::::
3856

3957
::::{bzl:flag} precompile_source_retention
@@ -51,9 +69,11 @@ Values:
5169
* `omit_source`: Don't include the orignal py source.
5270
* `omit_if_generated_source`: Keep the original source if it's a regular source
5371
file, but omit it if it's a generated file.
72+
:::{versionadded} 0.33.0
73+
:::
5474
::::
5575

56-
:::{bzl:flag} precompile_add_to_runfiles
76+
::::{bzl:flag} precompile_add_to_runfiles
5777
Determines if a target adds its compiled files to its runfiles.
5878

5979
When a target compiles its files, but doesn't add them to its own runfiles, it
@@ -66,7 +86,9 @@ Values:
6686
runfiles; they are still added to {bzl:obj}`PyInfo.transitive_pyc_files`. See
6787
also: {bzl:obj}`py_binary.pyc_collection` attribute. This is useful for allowing
6888
incrementally enabling precompilation on a per-binary basis.
89+
:::{versionadded} 0.33.0
6990
:::
91+
::::
7092

7193
::::{bzl:flag} pyc_collection
7294
Determine if `py_binary` collects transitive pyc files.
@@ -78,6 +100,8 @@ This flag is overridden by the target level `pyc_collection` attribute.
78100
Values:
79101
* `include_pyc`: Include `PyInfo.transitive_pyc_files` as part of the binary.
80102
* `disabled`: Don't include `PyInfo.transitive_pyc_files` as part of the binary.
103+
:::{versionadded} 0.33.0
104+
:::
81105
::::
82106

83107
::::{bzl:flag} py_linux_libc

docs/sphinx/toolchains.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ use `python3` from the environment a binary runs within. This provides extremely
233233
limited functionality to the rules (at build time, nothing is knowable about
234234
the Python runtime).
235235

236-
Bazel itself automatically registers `@bazel_tools//python:autodetecting_toolchain`
236+
Bazel itself automatically registers `@bazel_tools//tools/python:autodetecting_toolchain`
237237
as the lowest priority toolchain. For WORKSPACE builds, if no other toolchain
238238
is registered, that toolchain will be used. For bzlmod builds, rules_python
239239
automatically registers a higher-priority toolchain; it won't be used unless

python/config_settings/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
22
load(
33
"//python/private:flags.bzl",
44
"BootstrapImplFlag",
5+
"ExecToolsToolchainFlag",
56
"PrecompileAddToRunfilesFlag",
67
"PrecompileFlag",
78
"PrecompileSourceRetentionFlag",
@@ -29,6 +30,25 @@ construct_config_settings(
2930
name = "construct_config_settings",
3031
)
3132

33+
string_flag(
34+
name = "exec_tools_toolchain",
35+
build_setting_default = ExecToolsToolchainFlag.DISABLED,
36+
values = sorted(ExecToolsToolchainFlag.__members__.values()),
37+
# NOTE: Only public because it is used in py_toolchain_suite from toolchain
38+
# repositories
39+
visibility = ["//visibility:private"],
40+
)
41+
42+
config_setting(
43+
name = "is_exec_tools_toolchain_enabled",
44+
flag_values = {
45+
"exec_tools_toolchain": ExecToolsToolchainFlag.ENABLED,
46+
},
47+
# NOTE: Only public because it is used in py_toolchain_suite from toolchain
48+
# repositories
49+
visibility = ["//visibility:public"],
50+
)
51+
3252
string_flag(
3353
name = "precompile",
3454
build_setting_default = PrecompileFlag.AUTO,

python/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ bzl_library(
5959
name = "autodetecting_toolchain_bzl",
6060
srcs = ["autodetecting_toolchain.bzl"],
6161
deps = [
62+
":toolchain_types_bzl",
6263
"//python:py_runtime_bzl",
6364
"//python:py_runtime_pair_bzl",
6465
],

python/private/autodetecting_toolchain.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load("//python:py_runtime.bzl", "py_runtime")
1818
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
19+
load(":toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
1920

2021
def define_autodetecting_toolchain(name):
2122
"""Defines the autodetecting Python toolchain.
@@ -65,6 +66,6 @@ def define_autodetecting_toolchain(name):
6566
native.toolchain(
6667
name = name,
6768
toolchain = ":_autodetecting_py_runtime_pair",
68-
toolchain_type = ":toolchain_type",
69+
toolchain_type = TARGET_TOOLCHAIN_TYPE,
6970
visibility = ["//visibility:public"],
7071
)

python/private/flags.bzl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ def _precompile_flag_get_effective_value(ctx):
3737
value = PrecompileFlag.DISABLED
3838
return value
3939

40+
# Determines if the Python exec tools toolchain should be registered.
41+
# buildifier: disable=name-conventions
42+
ExecToolsToolchainFlag = enum(
43+
# Enable registering the exec tools toolchain using the hermetic toolchain.
44+
ENABLED = "enabled",
45+
# Disable registering the exec tools toolchain using the hermetic toolchain.
46+
DISABLED = "disabled",
47+
)
48+
4049
# Determines if Python source files should be compiled at build time.
4150
#
4251
# NOTE: The flag value is overridden by the target-level attribute, except

python/private/py_toolchain_suite.bzl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ load(
2222
"TARGET_TOOLCHAIN_TYPE",
2323
)
2424

25+
_IS_EXEC_TOOLCHAIN_ENABLED = Label("//python/config_settings:is_exec_tools_toolchain_enabled")
26+
2527
def py_toolchain_suite(*, prefix, user_repository_name, python_version, set_python_version_constraint, flag_values, **kwargs):
2628
"""For internal use only.
2729
@@ -106,8 +108,16 @@ def py_toolchain_suite(*, prefix, user_repository_name, python_version, set_pyth
106108
user_repository_name = user_repository_name,
107109
),
108110
toolchain_type = EXEC_TOOLS_TOOLCHAIN_TYPE,
109-
# The target settings capture the Python version
110-
target_settings = target_settings,
111+
target_settings = select({
112+
_IS_EXEC_TOOLCHAIN_ENABLED: target_settings,
113+
# Whatever the default is, it has to map to a `config_setting`
114+
# that will never match. Since the default branch is only taken if
115+
# _IS_EXEC_TOOLCHAIN_ENABLED is false, then it will never match
116+
# when later evaluated during toolchain resolution.
117+
# Note that @platforms//:incompatible can't be used here because
118+
# the RHS must be a `config_setting`.
119+
"//conditions:default": [_IS_EXEC_TOOLCHAIN_ENABLED],
120+
}),
111121
exec_compatible_with = kwargs.get("target_compatible_with"),
112122
)
113123

tests/base_rules/precompile/precompile_tests.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ load("//tests/base_rules:py_info_subject.bzl", "py_info_subject")
2727
load(
2828
"//tests/support:support.bzl",
2929
"CC_TOOLCHAIN",
30+
"EXEC_TOOLS_TOOLCHAIN",
3031
"PLATFORM_TOOLCHAIN",
3132
"PRECOMPILE",
3233
"PRECOMPILE_ADD_TO_RUNFILES",
@@ -61,6 +62,7 @@ def _test_precompile_enabled_setup(name, py_rule, **kwargs):
6162
target = name + "_subject",
6263
config_settings = {
6364
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
65+
EXEC_TOOLS_TOOLCHAIN: "enabled",
6466
},
6567
)
6668

@@ -119,6 +121,7 @@ def _test_pyc_only(name):
119121
config_settings = {
120122
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
121123
##PRECOMPILE_SOURCE_RETENTION: "omit_source",
124+
EXEC_TOOLS_TOOLCHAIN: "enabled",
122125
},
123126
target = name + "_subject",
124127
)
@@ -161,6 +164,7 @@ def _test_precompile_if_generated(name):
161164
target = name + "_subject",
162165
config_settings = {
163166
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
167+
EXEC_TOOLS_TOOLCHAIN: "enabled",
164168
},
165169
)
166170

@@ -203,6 +207,7 @@ def _test_omit_source_if_generated_source(name):
203207
config_settings = {
204208
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
205209
PRECOMPILE_SOURCE_RETENTION: "omit_if_generated_source",
210+
EXEC_TOOLS_TOOLCHAIN: "enabled",
206211
},
207212
)
208213

@@ -252,6 +257,7 @@ def _test_precompile_add_to_runfiles_decided_elsewhere(name):
252257
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
253258
PRECOMPILE_ADD_TO_RUNFILES: "decided_elsewhere",
254259
PRECOMPILE: "enabled",
260+
EXEC_TOOLS_TOOLCHAIN: "enabled",
255261
},
256262
)
257263

@@ -288,6 +294,7 @@ def _test_precompiler_action(name):
288294
target = name + "_subject",
289295
config_settings = {
290296
"//command_line_option:extra_toolchains": _TEST_TOOLCHAINS,
297+
EXEC_TOOLS_TOOLCHAIN: "enabled",
291298
},
292299
)
293300

tests/support/support.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ CC_TOOLCHAIN = str(Label("//tests/cc:all"))
3131

3232
# str() around Label() is necessary because rules_testing's config_settings
3333
# doesn't accept yet Label objects.
34+
EXEC_TOOLS_TOOLCHAIN = str(Label("//python/config_settings:exec_tools_toolchain"))
3435
PRECOMPILE = str(Label("//python/config_settings:precompile"))
3536
PYC_COLLECTION = str(Label("//python/config_settings:pyc_collection"))
3637
PRECOMPILE_SOURCE_RETENTION = str(Label("//python/config_settings:precompile_source_retention"))

0 commit comments

Comments
 (0)