-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
incompatible_use_python_toolchains: The Python runtime is obtained from a toolchain rather than a flag #7899
Comments
This renames --experimental_use_python_toolchains to --incompatible. It also adds the behavior to the flag that 1) py_runtime's python_version attribute becomes manadatory, and 2) the --python_top flag is disallowed (it is superseded by toolchains), as well as --python2_path and --python3_path (they were already no-ops). --python_path is strongly discouraged but not yet banned due to an existing use on Windows (#7901). Feature issue is #7375, incompatible change migration issue is #7899. RELNOTES[INC]: Introduced --incompatible_use_python_toolchains, which supersedes --python_top/--python_path. See #7899 and #7375 for more information. PiperOrigin-RevId: 241134532
I'm trying this flag for rules_k8s in the context of bazelbuild/rules_k8s#305.
Note I did not experience this type of error when trying this flag out with rules_docker (bazelbuild/rules_docker#787) See also bazelbuild/rules_k8s#305 for context |
Baseline: 0366246 Cherry picks: + 3f7f255: Windows: fix native test wrapper's arg. escaping + afeb8d0: Flip --incompatible_windows_escape_jvm_flags + 4299b65: Sort DirectoryNode children to ensure validity. + 231270c: Conditionally use deprecated signature for initWithContentsOfURL + 75a3a53: Add http_archive entries for testing with various JDK versions. + 4a6354a: Now that ubuntu1804 uses JDK 11, remove explicit ubuntu1804_java11 tests. + ae102fb: Fix wrong name of ubuntu1804_javabase9 task. + 0020a97: Remove @executable_path/Frameworks from rpaths + 130f86d: Download stderr/stdout to a temporary FileOutErr Incompatible changes: - (Starlark rules) The legacy "py" provider can no longer be passed to or produced by native Python rules; use [PyInfo](https://docs.bazel.build/versions/master/skylark/lib/PyIn fo.html) instead. See [#7298](#7298) for more information. - (Python rules) The `default_python_version` attribute of the `py_binary` and `py_test` rules has been renamed to `python_version`. Also, the `--force_python` flag has been renamed to `--python_version`. See [#7308](#7308) for more information. - (Python rules) The python version now changes to whatever version is specified in a `py_binary` or `py_test`'s `python_version` attribute, instead of being forced to the value set by a command line flag. You can temporarily revert this change with `--incompatible_allow_python_version_transitions=false`. See [#7307](#7307) for more information. - --incompatible_disable_third_party_license_checking` is enabled by default - Introduced --incompatible_use_python_toolchains, which supersedes --python_top/--python_path. See #7899 and #7375 for more information. - Python 3 is now the default Python version (for `py_binary` and `py_test` targets that don't specify the `python_version` attribute). Targets that are built for Python 3 will no longer have their output put in a separate `-py3` directory; instead there is now a separate `-py2` directory for Python 2 targets. See #7359 and #7593 for more information. - objc_library resource attributes are now disabled by default. Please migrate them to data instead. See #7594 for more info. - Flip --incompatible_windows_escape_jvm_flags to true. See #7486 New features: - genrules now support a $(RULEDIR) variable that resolves to the directory where the outputs of the rule are put. - Added --incompatible_windows_native_test_wrapper flag: enables using the Bash-less test wrapper on Windows. (No-op on other platforms.) Important changes: - incompatible_use_jdk11_as_host_javabase: makes JDK 11 the default --host_javabase for remote jdk (#7219) - Makes genquery somepath output deterministic. - Tristate attributes of native rules now reject True/False (use 1/0) - Rollback of "Tristate attributes of native rules now reject True/False (use 1/0)" - Tristate attributes of native rules now reject True/False (use 1/0) - Added -incompatible_do_not_split_linking_cmdline flag. See #7670 - Tristate attributes of native rules now temporarily accept True/False again - `--incompatible_disable_legacy_crosstool_fields` has been flipped (#6861) `--incompatible_disable_expand_if_all_available_in_flag_set` has been flipped (#7008) - `--incompatible_disable_legacy_crosstool_fields` has been flipped (#6861) `--incompatible_disable_expand_if_all_available_in_flag_set... RELNOTES: None. - --incompatible_no_transitive_loads is enabled by default. - Makes TreeArtifact deterministic. - --incompatible_no_transitive_loads is enabled by default. - Android NDK C++ toolchain is now configured in Starlark. This should be a backwards compatible change, but in case of bugs blame unknown commit. - `--incompatible_disable_legacy_crosstool_fields` has been flipped (#6861) `--incompatible_disable_expand_if_all_available_in_flag_set` has been flipped (#7008) - --incompatible_no_transitive_loads is enabled by default. - --incompatible_bzl_disallow_load_after_statement is enabled - Added `--incompatible_require_ctx_in_configure_features`, see #7793 for details. - Flag --incompatible_merge_genfiles_directory is flipped. This removes the directory `bazel-genfiles` in favor of `bazel-bin`. - previously deprecated flag --experimental_remote_spawn_cache was removed - `--incompatible_disallow_load_labels_to_cross_package_boundaries` is enabled by default - Fix an issue where the Android resource processor did not surface errors from aapt2 compile and link actions. - --incompatible_no_attr_license is enabled by default - `--incompatible_disable_crosstool_file` has been flipped (#7320) - A new flag `--incompatible_string_join_requires_strings` is introduced. The sequence argument of `string.join` must contain only string elements. - --incompatible_symlinked_sandbox_expands_tree_artifacts_in_runfile s_tree has been flipped - Incompatible flag `--incompatible_disable_legacy_cc_provider` has been flipped (see #7036 for details). - Don't drop the analysis cache when the same --define flag is set multiple times and the last value is the same (e.g. if the current invocation was run with "--define foo=bar" and the previous one was run with "--define foo=baz --define foo=bar"). - The --incompatible_disable_genrule_cc_toolchain_dependency flag has been flipped (see #6867 for details). - Incompatible change `--incompatible_remove_cpu_and_compiler_attributes_from_cc_toolcha in` has been flipped (see #7075 for details). - --noexperimental_java_coverage is a no-op flag. - --experimental_java_coverage/--incompatible_java_coverage flag was removed. See #7425. - incompatible_use_toolchain_providers_in_java_common: pass JavaToolchainInfo and JavaRuntimeInfo providers to java_common APIs instead of configured targets (#7186.) - --incompatible_remote_symlinks has been flipped. The remote caching and execution protocol will now represent symlinks in outputs as such. See #7917 for more details. - Bazel is now ~20MiB smaller, from unbundling the Android rules' runtime dependencies. This release contains contributions from many people at Google, as well as Andreas Herrmann, Andrew Suffield, Andy Scott, Benjamin Peterson, Ed Baunton, George Gensure, Ian McGinnis, Ity Kaul, Jingwen Chen, John Millikin, Keith Smiley, Marwan Tammam, Mike Fourie, Oscar Bonilla, perwestling, petros, Robert Sayre, Ryan Beasley, silvergasp, Stanimir Mladenov, Travis Cline, Vladimir Chebotarev, ??.
Unfortunately this broke a large number of downstream projects, mostly because it actually enforces that the Python interpreter has the requested version. Looks like switching the default Python version in Bazel 0.25 from PY2 to PY3 was too easy precisely because it wasn't being enforced at execution time. :( We'll need to do some downstream fixing before we can flip again. |
This rolls-forward the parts of bf66dc7 that made our tests compatible with --incompatible_use_python_toolchains, without actually flipping the flag. The flag is set to default true within our test setup. Work toward #7899. TESTED=Confirmed that it doesn't break mac postsubmit: https://buildkite.com/bazel/bazel-bazel/builds/8065 PiperOrigin-RevId: 246404708
FWIW I think this is google/subpar#98 |
Baseline: 0366246 Cherry picks: + 3f7f255: Windows: fix native test wrapper's arg. escaping + afeb8d0: Flip --incompatible_windows_escape_jvm_flags + 4299b65: Sort DirectoryNode children to ensure validity. + 231270c: Conditionally use deprecated signature for initWithContentsOfURL + 75a3a53: Add http_archive entries for testing with various JDK versions. + 4a6354a: Now that ubuntu1804 uses JDK 11, remove explicit ubuntu1804_java11 tests. + ae102fb: Fix wrong name of ubuntu1804_javabase9 task. + 0020a97: Remove @executable_path/Frameworks from rpaths + 130f86d: Download stderr/stdout to a temporary FileOutErr Incompatible changes: - (Starlark rules) The legacy "py" provider can no longer be passed to or produced by native Python rules; use [PyInfo](https://docs.bazel.build/versions/master/skylark/lib/PyIn fo.html) instead. See [#7298](#7298) for more information. - (Python rules) The `default_python_version` attribute of the `py_binary` and `py_test` rules has been renamed to `python_version`. Also, the `--force_python` flag has been renamed to `--python_version`. See [#7308](#7308) for more information. - (Python rules) The python version now changes to whatever version is specified in a `py_binary` or `py_test`'s `python_version` attribute, instead of being forced to the value set by a command line flag. You can temporarily revert this change with `--incompatible_allow_python_version_transitions=false`. See [#7307](#7307) for more information. - --incompatible_disable_third_party_license_checking` is enabled by default - Introduced --incompatible_use_python_toolchains, which supersedes --python_top/--python_path. See #7899 and #7375 for more information. - Python 3 is now the default Python version (for `py_binary` and `py_test` targets that don't specify the `python_version` attribute). Targets that are built for Python 3 will no longer have their output put in a separate `-py3` directory; instead there is now a separate `-py2` directory for Python 2 targets. See #7359 and #7593 for more information. - objc_library resource attributes are now disabled by default. Please migrate them to data instead. See #7594 for more info. - Flip --incompatible_windows_escape_jvm_flags to true. See #7486 New features: - genrules now support a $(RULEDIR) variable that resolves to the directory where the outputs of the rule are put. - Added --incompatible_windows_native_test_wrapper flag: enables using the Bash-less test wrapper on Windows. (No-op on other platforms.) Important changes: - incompatible_use_jdk11_as_host_javabase: makes JDK 11 the default --host_javabase for remote jdk (#7219) - Makes genquery somepath output deterministic. - Tristate attributes of native rules now reject True/False (use 1/0) - Rollback of "Tristate attributes of native rules now reject True/False (use 1/0)" - Tristate attributes of native rules now reject True/False (use 1/0) - Added -incompatible_do_not_split_linking_cmdline flag. See #7670 - Tristate attributes of native rules now temporarily accept True/False again - `--incompatible_disable_legacy_crosstool_fields` has been flipped (#6861) `--incompatible_disable_expand_if_all_available_in_flag_set` has been flipped (#7008) - `--incompatible_disable_legacy_crosstool_fields` has been flipped (#6861) `--incompatible_disable_expand_if_all_available_in_flag_set... RELNOTES: None. - --incompatible_no_transitive_loads is enabled by default. - Makes TreeArtifact deterministic. - --incompatible_no_transitive_loads is enabled by default. - Android NDK C++ toolchain is now configured in Starlark. This should be a backwards compatible change, but in case of bugs blame unknown commit. - `--incompatible_disable_legacy_crosstool_fields` has been flipped (#6861) `--incompatible_disable_expand_if_all_available_in_flag_set` has been flipped (#7008) - --incompatible_no_transitive_loads is enabled by default. - --incompatible_bzl_disallow_load_after_statement is enabled - Added `--incompatible_require_ctx_in_configure_features`, see #7793 for details. - Flag --incompatible_merge_genfiles_directory is flipped. This removes the directory `bazel-genfiles` in favor of `bazel-bin`. - previously deprecated flag --experimental_remote_spawn_cache was removed - `--incompatible_disallow_load_labels_to_cross_package_boundaries` is enabled by default - Fix an issue where the Android resource processor did not surface errors from aapt2 compile and link actions. - --incompatible_no_attr_license is enabled by default - `--incompatible_disable_crosstool_file` has been flipped (#7320) - A new flag `--incompatible_string_join_requires_strings` is introduced. The sequence argument of `string.join` must contain only string elements. - --incompatible_symlinked_sandbox_expands_tree_artifacts_in_runfile s_tree has been flipped - Incompatible flag `--incompatible_disable_legacy_cc_provider` has been flipped (see #7036 for details). - Don't drop the analysis cache when the same --define flag is set multiple times and the last value is the same (e.g. if the current invocation was run with "--define foo=bar" and the previous one was run with "--define foo=baz --define foo=bar"). - The --incompatible_disable_genrule_cc_toolchain_dependency flag has been flipped (see #6867 for details). - Incompatible change `--incompatible_remove_cpu_and_compiler_attributes_from_cc_toolcha in` has been flipped (see #7075 for details). - --noexperimental_java_coverage is a no-op flag. - --experimental_java_coverage/--incompatible_java_coverage flag was removed. See #7425. - incompatible_use_toolchain_providers_in_java_common: pass JavaToolchainInfo and JavaRuntimeInfo providers to java_common APIs instead of configured targets (#7186.) - --incompatible_remote_symlinks has been flipped. The remote caching and execution protocol will now represent symlinks in outputs as such. See #7917 for more details. - Bazel is now ~20MiB smaller, from unbundling the Android rules' runtime dependencies. This release contains contributions from many people at Google, as well as Andreas Herrmann, Andrew Suffield, Andy Scott, Benjamin Peterson, Ed Baunton, George Gensure, Ian McGinnis, Ity Kaul, Jingwen Chen, John Millikin, Keith Smiley, Marwan Tammam, Mike Fourie, Oscar Bonilla, perwestling, petros, Robert Sayre, Ryan Beasley, silvergasp, Stanimir Mladenov, Travis Cline, Vladimir Chebotarev, ??.
Documentation here has a small typo. Currently says: |
Fixed in 331c84b. :) |
Downstream buildkite run here. Cataloging the failures:
|
We're now targeting 0.27 for flipping this flag. In the absence of the execution transition feature, all projects that require PY2 host tools should set |
…import bazelbuild#978) Bazel 0.27.0 introduced an incompatible change flag that forces the host python to be python3. This changes the way encoding/decoding is done, causing failures as seen in bazelbuild#976 (comment): ``` $ bazel build //aswb:aswb_bazel_zip --define=ij_product=android-studio-latest --experimental_google_legacy_api INFO: Analyzed target //aswb:aswb_bazel_zip (62 packages loaded, 2243 targets configured). INFO: Found 1 target... ERROR: /Users/jingwen/code/intellij/aswb/BUILD:234:1: Creating final plugin zip archive failed (Exit 1) zip_plugin_files failed: error executing command bazel-out/host/bin/build_defs/zip_plugin_files --output bazel-out/darwin-fastbuild/bin/aswb/aswb_bazel.zip aspect/WORKSPACE aswb/aspect/WORKSPACE aspect/artifacts.bzl aswb/aspect/artifacts.bzl ... (remaining 20 argument(s) skipped) Use --sandbox_debug to see verbose messages from the sandbox Traceback (most recent call last): File "/private/var/tmp/_bazel_jingwen/a7f9cfb321ff8f053a43633ca4d90cc9/sandbox/darwin-sandbox/379/execroot/intellij_with_bazel/bazel-out/host/bin/build_defs/zip_plugin_files.runfiles/intellij_with_bazel/build_defs/zip_plugin_files.py", line 38, in <module> main() File "/private/var/tmp/_bazel_jingwen/a7f9cfb321ff8f053a43633ca4d90cc9/sandbox/darwin-sandbox/379/execroot/intellij_with_bazel/bazel-out/host/bin/build_defs/zip_plugin_files.runfiles/intellij_with_bazel/build_defs/zip_plugin_files.py", line 35, in main outfile.writestr(zipinfo, input_file.read(), zipfile.ZIP_DEFLATED) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 39: ordinal not in range(128) ---------------- Note: The failure of target //build_defs:zip_plugin_files (with exit code 1) may have been caused by the fact that it is a Python 2 program that was built in the host configuration, which uses Python 3. You can change the host configuration (for the entire build) to instead use Python 2 by setting --host_force_python=PY2. If this error started occurring in Bazel 0.27 and later, it may be because the Python toolchain now enforces that targets analyzed as PY2 and PY3 run under a Python 2 and Python 3 interpreter, respectively. See bazelbuild/bazel#7899 for more information. ---------------- Target //aswb:aswb_bazel_zip failed to build Use --verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 347.553s, Critical Path: 91.50s INFO: 370 processes: 348 darwin-sandbox, 22 worker. FAILED: Build did NOT complete successfully ``` This PR fixes that breakage by extending the decoder to from using ascii to direct bytes, allowing zip_plugin_files.py to be used with Python 3 directly. Closes bazelbuild#978 PiperOrigin-RevId: 257788170
See following issues for details * bazelbuild/rules_docker#842 * bazelbuild/rules_docker#580 * bazelbuild/bazel#7899 Signed-off-by: stuart.warren <[email protected]>
bazelbuild/bazel#7899 was fixed and we don't need this hack anymore. PiperOrigin-RevId: 271434565
…import bazelbuild#978) Bazel 0.27.0 introduced an incompatible change flag that forces the host python to be python3. This changes the way encoding/decoding is done, causing failures as seen in bazelbuild#976 (comment): ``` $ bazel build //aswb:aswb_bazel_zip --define=ij_product=android-studio-latest --experimental_google_legacy_api INFO: Analyzed target //aswb:aswb_bazel_zip (62 packages loaded, 2243 targets configured). INFO: Found 1 target... ERROR: /Users/jingwen/code/intellij/aswb/BUILD:234:1: Creating final plugin zip archive failed (Exit 1) zip_plugin_files failed: error executing command bazel-out/host/bin/build_defs/zip_plugin_files --output bazel-out/darwin-fastbuild/bin/aswb/aswb_bazel.zip aspect/WORKSPACE aswb/aspect/WORKSPACE aspect/artifacts.bzl aswb/aspect/artifacts.bzl ... (remaining 20 argument(s) skipped) Use --sandbox_debug to see verbose messages from the sandbox Traceback (most recent call last): File "/private/var/tmp/_bazel_jingwen/a7f9cfb321ff8f053a43633ca4d90cc9/sandbox/darwin-sandbox/379/execroot/intellij_with_bazel/bazel-out/host/bin/build_defs/zip_plugin_files.runfiles/intellij_with_bazel/build_defs/zip_plugin_files.py", line 38, in <module> main() File "/private/var/tmp/_bazel_jingwen/a7f9cfb321ff8f053a43633ca4d90cc9/sandbox/darwin-sandbox/379/execroot/intellij_with_bazel/bazel-out/host/bin/build_defs/zip_plugin_files.runfiles/intellij_with_bazel/build_defs/zip_plugin_files.py", line 35, in main outfile.writestr(zipinfo, input_file.read(), zipfile.ZIP_DEFLATED) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 39: ordinal not in range(128) ---------------- Note: The failure of target //build_defs:zip_plugin_files (with exit code 1) may have been caused by the fact that it is a Python 2 program that was built in the host configuration, which uses Python 3. You can change the host configuration (for the entire build) to instead use Python 2 by setting --host_force_python=PY2. If this error started occurring in Bazel 0.27 and later, it may be because the Python toolchain now enforces that targets analyzed as PY2 and PY3 run under a Python 2 and Python 3 interpreter, respectively. See bazelbuild/bazel#7899 for more information. ---------------- Target //aswb:aswb_bazel_zip failed to build Use --verbose_failures to see the command lines of failed build steps. INFO: Elapsed time: 347.553s, Critical Path: 91.50s INFO: 370 processes: 348 darwin-sandbox, 22 worker. FAILED: Build did NOT complete successfully ``` This PR fixes that breakage by extending the decoder to from using ascii to direct bytes, allowing zip_plugin_files.py to be used with Python 3 directly. Closes bazelbuild#978 PiperOrigin-RevId: 257788170
Migration for bazelbuild/bazel#7899 PiperOrigin-RevId: 274810453
Migration for bazelbuild/bazel#7899 PiperOrigin-RevId: 274810453
Migration for bazelbuild/bazel#7899 PiperOrigin-RevId: 274810453
Enforce the correct version potential issues and warnings like this: ``` Note: The failure of target @build_bazel_rules_ios//rules/framework:framework_packaging (with exit code 1) may have been caused by the fact that it is running under Python 3 instead of Python 2. Examine the error to determine if that appears to be the problem. Since this target is built in the host configuration, the only way to change its version is to set --host_force_python=PY2, which affects the entire build. If this error started occurring in Bazel 0.27 and later, it may be because the Python toolchain now enforces that targets analyzed as PY2 and PY3 run under a Python 2 and Python 3 interpreter, respectively. See bazelbuild/bazel#7899 for more information. ```
@brandjon can we remove this diagnostic, or add a way to silence it? We're on bazel 5.0.0 now, I think it has been enough time since 0.27 that this is probably never the reason that a python rule fails (and it has never been the case in my org, confusing devs), but this is still always printed:
Also python2 was EOL in 2020 |
(This is a roll-forward of bazelbuild/bazel@bf66dc7.) This flips --incompatible_use_python_toolchains, which deprecates --python_top (and for the most part, --python_path). See #7899 for more on the change and migration procedure. Known downstream breakages are summarized [here](bazelbuild/bazel#7899 (comment)). Fixes #7899, fixes #7375, significant progress on #4815. RELNOTES[INC]: Python rules now determine the Python runtime using toolchains rather than `--python_top` and `--python_path`, which are deprecated. See [#7899](bazelbuild/bazel#7899) for information on declaring Python toolchains and migrating your code. As a side-benefit, this addresses [#4815](bazelbuild/bazel#4815) (incorrect interpreter version used) on non-Windows platforms. Note however that some builds break due to getting the version they asked for -- consider setting `python_version = "PY2"` on Python 2 targets and `--host_force_python=PY2` if any Python 2 targets are used in the host configuration. You can temporarily opt out of this change with `--incompatible_use_python_toolchains=false`. PiperOrigin-RevId: 250918912
Flag:
--incompatible_use_python_toolchains
Available since: 0.25
Will be flipped in: 0.27
Feature tracking issue: #7375
FAQ (common problems)
I'm getting Python 2 vs 3 errors
This flag fixes #4815 on non-Windows platforms, so your code might now be running under a different version of Python than it was in previous Bazel versions. You may notice this as a Python stack trace complaining about bad
print
syntax, problems withbytes
vsstr
(encode
/decode
), unknown imports, etc.In order for your code to run under the proper version of Python, make sure Python 2 binaries and tests have the attribute
python_version = "PY2"
(the default isPY3
).For targets that are built in the host configuration (for example, genrule tools in particular),
python_version
has no effect. It is currently impossible for PY2 and PY3 host-configured targets to co-exist in the same build; they will always be overridden to one or the other, depending on the value of--host_force_python
. This incompatible change does not affect how the host config works, it just makes it so targets actually run with the version the host config specifies. If you (or your dependencies) have host-configured tools that require Python 2, and which are now failing because they're running under Python 3, add--host_force_python=PY2
to your bazelrc (the default value isPY3
).Bazel 0.27 introduces a diagnostic message when a host-configured tool fails at run time (non-zero exit code), alerting you when it may be necessary to set this flag.
The default Python toolchain can't find the interpreter
If you get an error like this:
Determine whether you have
python2
,python3
, and/orpython
on your shellPATH
. Forpy_test
targets, and forpy_binary
targets used as tools (in genrules, etc.), also check whether yourPATH
is being manipulated by the flags--incompatible_strict_action_env
and/or--action_env=PATH=[...]
. For instance, the strict action environment does not include/usr/local/bin
inPATH
by default, which is wherepython3
is typically located on Mac, if it is installed at all. See also #8536.If modifying your
PATH
is not feasible, try defining and registering your own Python toolchain as described at the bottom of this post.I don't have Python 3 installed (e.g. default Mac environment)
Previously, if you didn't have a Python 3 interpreter but all your code was compatible with Python 2, Bazel would happily analyze it as PY3 and execute it using a Python 2
python
command. Now this breaks because the autodetecting toolchain validates thatpython
is actually Python 3.The ideal solution is to not depend on Python 3 code, or else install a Python 3 environment on the target system. The practical workaround is to opt out of version checking by using the non-strict autodetecting toolchain. The error message tells you how: Add to your bazelrc
Note that you will not benefit from the fix to #4815 as long as you are using this toolchain.
If you're using a custom Python toolchain (using
py_runtime_pair
, as described at the bottom of this post), you can have thepy3_runtime
attribute point to apy_runtime
that declares itself asPY3
but in actuality references a Python 2 interpreter. This abuse of version information achieves the same result: PY3-analyzed targets get run with a Python 2 interpreter.Neither of these approaches is recommended for anyone but end-users, since they affect how Python targets get run globally throughout the build.
I'm a rule author and I want my target to run regardless of whether the downstream user has Python 2 or 3
See this comment.
Did the behavior of toolchains change between 0.26 and 0.27
This incompatible change was available since 0.25 and flipped to true by default in 0.27. Bazel 0.27 introduces some bug fixes in the behavior of the autodetecting toolchain, better diagnostic messages, and the non-strict toolchain.
Motivation
For background on toolchains, see here.
Previously, the Python runtime (i.e., the interpreter used to execute
py_binary
andpy_test
targets) could only be controlled globally, and required passing flags like--python_top
to the bazel invocation. This is out-of-step with our ambitions for flagless builds and remote-execution-friendly toolchains. Using the toolchain mechanism means that each Python target can automatically select an appropriate runtime based on what target platform it is being built for.Change
Enabling this flag triggers the following changes.
Executable Python targets will retrieve their runtime from the new Python toolchain.
It is forbidden to set any of the legacy flags
--python_top
,--python2_path
, or--python3_path
. Note that the last two of those are already no-ops. It is also strongly discouraged to set--python_path
, but this flag will be removed in a later cleanup due to Remove--python_path
flag #7901.The
python_version
attribute of thepy_runtime
rule becomes mandatory. It must be either"PY2"
or"PY3"
, indicating which kind of runtime it is describing.For builds that rely on a Python interpreter installed on the system, it is recommended that users (or platform rule authors) ensure that each platform has an appropriate Python toolchain definition.
If no Python toolchain is explicitly registered, on non-Windows platforms there is a new default toolchain that automatically detects and executes an interpreter (of the appropriate version) from
PATH
. This resolves longstanding issue #4815. A Windows version of this toolchain will come later (#7844).Migration
See the above FAQ for common issues with the autodetecting toolchain.
If you were relying on
--python_top
, and you want your whole build to continue to use thepy_runtime
you were pointing it to, you just need to follow the steps below to define apy_runtime_pair
andtoolchain
, and register this toolchain in your workspace. So long as you don't add any platform constraints that would prevent your toolchain from matching, it will take precedence over the default toolchain described above.If you were relying on
--python_path
, and you want your whole build to use the interpreter located at the absolute path you were passing in this flag, the steps are the same, except you also have to define a newpy_runtime
with theinterpreter_path
attribute set to that path.Otherwise, if you were only relying on the default behavior that resolved
python
fromPATH
, just enjoy the new default behavior, which is:python2
orpython3
(depending on the target's version)python
if not foundPY2
orPY3
), as per thepython -V
flag.On Windows the default behavior is currently unchanged (#7844).
Example toolchain definition
Of course, you can define and register many different toolchains and use platform constraints to restrict them to appropriate target platforms. It is recommended to use the constraint settings
@bazel_tools//tools/python:py2_interpreter_path
and[...]:py3_interpreter_path
as the namespaces for constraints about where a platform's Python interpreters are located.The new toolchain-related rules and default toolchain are implemented in Starlark under
@bazel_tools
. Their source code and documentation strings can be read here.The text was updated successfully, but these errors were encountered: