Skip to content
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

Closed
brandjon opened this issue Mar 29, 2019 · 38 comments
Assignees
Labels
incompatible-change Incompatible/breaking change team-Rules-Python Native rules for Python

Comments

@brandjon
Copy link
Member

brandjon commented Mar 29, 2019

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 with bytes vs str (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 is PY3).

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 is PY3).

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:

Error: The default python toolchain (@bazel_tools//tools/python:autodetecting_toolchain) was unable to locate a suitable Python interpreter on the target platform at execution time. Please register an appropriate Python toolchain. [...]
Failure reason: Cannot locate 'python3' or 'python' on the target platform's PATH, which is: [...]

Determine whether you have python2, python3, and/or python on your shell PATH. For py_test targets, and for py_binary targets used as tools (in genrules, etc.), also check whether your PATH 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 in PATH by default, which is where python3 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 that python 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

build --extra_toolchains=@bazel_tools//tools/python:autodetecting_toolchain_nonstrict

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 the py3_runtime attribute point to a py_runtime that declares itself as PY3 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 and py_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.

  1. Executable Python targets will retrieve their runtime from the new Python toolchain.

  2. 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.

  3. The python_version attribute of the py_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 the py_runtime you were pointing it to, you just need to follow the steps below to define a py_runtime_pair and toolchain, 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 new py_runtime with the interpreter_path attribute set to that path.

Otherwise, if you were only relying on the default behavior that resolved python from PATH, just enjoy the new default behavior, which is:

  1. First try python2 or python3 (depending on the target's version)
  2. Then fall back on python if not found
  3. Fail-fast if the interpreter that is found doesn't match the target's major Python version (PY2 or PY3), as per the python -V flag.

On Windows the default behavior is currently unchanged (#7844).

Example toolchain definition

# In your BUILD file...
load("@bazel_tools//tools/python/toolchain.bzl", "py_runtime_pair")
py_runtime(
    name = "my_py2_runtime",
    interpreter_path = "/system/python2",
    python_version = "PY2",
)
py_runtime(
    name = "my_py3_runtime",
    interpreter_path = "/system/python3",
    python_version = "PY3",
)
py_runtime_pair(
    name = "my_py_runtime_pair",
    py2_runtime = ":my_py2_runtime",
    py3_runtime = ":my_py3_runtime",
)
toolchain(
    name = "my_toolchain",
    target_compatible_with = [...],  # optional platform constraints
    toolchain = ":my_py_runtime_pair",
    toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)
# In your WORKSPACE...
register_toolchains("//my_pkg:my_toolchain")

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.

@brandjon brandjon added incompatible-change Incompatible/breaking change team-Rules-Python Native rules for Python labels Mar 29, 2019
@brandjon brandjon self-assigned this Mar 29, 2019
bazel-io pushed a commit that referenced this issue Mar 30, 2019
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
@nlopezgi
Copy link
Contributor

nlopezgi commented Apr 5, 2019

I'm trying this flag for rules_k8s in the context of bazelbuild/rules_k8s#305.
If I run a build with an explicit --python_top my build / test succeeds (bazelbuild/rules_k8s#306)
However if I use the new --incompatible_use_python_toolchains I get the following error:

ERROR: .../src/rules_k8s/fork/k8s/BUILD:58:1: Building par file //k8s:resolver.par failed (Exit 1) compiler.par failed: error executing command bazel-out/host/bin/external/subpar/compiler/compiler.par --manifest_file bazel-out/k8-fastbuild/bin/k8s/resolver.par_SOURCES --outputpar bazel-out/k8-fastbuild/bin/k8s/resolver.par --stub_file ... (remaining 4 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
src/main/tools/linux-sandbox-pid1.cc:427: "execvp(bazel-out/host/bin/external/subpar/compiler/compiler.par, 0x1830290)": No such file or directory

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

bazel-io pushed a commit that referenced this issue May 1, 2019
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, ??.
@brandjon
Copy link
Member Author

brandjon commented May 2, 2019

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.

@brandjon brandjon reopened this May 2, 2019
@brandjon brandjon reopened this May 2, 2019
bazel-io pushed a commit that referenced this issue May 2, 2019
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
@tmc
Copy link
Contributor

tmc commented May 5, 2019

I'm trying this flag for rules_k8s in the context of bazelbuild/rules_k8s#305.
If I run a build with an explicit --python_top my build / test succeeds (bazelbuild/rules_k8s#306)
However if I use the new --incompatible_use_python_toolchains I get the following error:

ERROR: .../src/rules_k8s/fork/k8s/BUILD:58:1: Building par file //k8s:resolver.par failed (Exit 1) compiler.par failed: error executing command bazel-out/host/bin/external/subpar/compiler/compiler.par --manifest_file bazel-out/k8-fastbuild/bin/k8s/resolver.par_SOURCES --outputpar bazel-out/k8-fastbuild/bin/k8s/resolver.par --stub_file ... (remaining 4 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
src/main/tools/linux-sandbox-pid1.cc:427: "execvp(bazel-out/host/bin/external/subpar/compiler/compiler.par, 0x1830290)": No such file or directory

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

FWIW I think this is google/subpar#98

dkelmer pushed a commit that referenced this issue May 6, 2019
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, ??.
@nlopezgi
Copy link
Contributor

nlopezgi commented May 8, 2019

Documentation here has a small typo. Currently says:
load("@bazel_tools//tools/python/toolchain.bzl", "py_runtime_pair")
when it should be
load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
Note /toolchain.bzl vs :toolchain.bzl

@brandjon
Copy link
Member Author

brandjon commented May 8, 2019

Fixed in 331c84b. :)

@brandjon
Copy link
Member Author

Downstream buildkite run here. Cataloging the failures:

  • Tests that run on mac workers fail because python3 is not on the PATH propagated to actions.

  • Android Testing failures are tracked here

  • Bazel's own failures are android-related so they're probably the same cause as the above.

  • The Bazel Toolchains failure is due to a PY2 host tool running as PY3.

  • The Cloud Robotics Core failure is also a host config issue.

  • Remote execution and Rules jvm external have some android issues.

  • rules_k8s has the same issue as the above host-config stuff with Bazel Toolchains

@brandjon
Copy link
Member Author

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 --host_force_python=PY2.

krlvi pushed a commit to krlvi/intellij that referenced this issue Sep 20, 2019
…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
maraino pushed a commit to mikemaxey/cert-manager that referenced this issue Sep 25, 2019
See following issues for details
* bazelbuild/rules_docker#842
* bazelbuild/rules_docker#580
* bazelbuild/bazel#7899

Signed-off-by: stuart.warren <stuart.warren@ocado.com>
gvisor-bot pushed a commit to google/gvisor that referenced this issue Sep 26, 2019
bazelbuild/bazel#7899 was fixed
and we don't need this hack anymore.

PiperOrigin-RevId: 271434565
aradchykov pushed a commit to aradchykov/intellij that referenced this issue Oct 6, 2019
…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
mihaimaruseac pushed a commit to tensorflow/estimator that referenced this issue Oct 15, 2019
mihaimaruseac pushed a commit to tensorflow/estimator that referenced this issue Feb 27, 2020
mihaimaruseac pushed a commit to tensorflow/estimator that referenced this issue Feb 27, 2020
nkoroste added a commit to nkoroste/rules_ios that referenced this issue Dec 23, 2021
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.
```
@bjacklyn
Copy link

bjacklyn commented Mar 1, 2022

@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:

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 https://github.com/bazelbuild/bazel/issues/7899 for more information.

Also python2 was EOL in 2020

luca-digrazia pushed a commit to luca-digrazia/DatasetCommitsDiffSearch that referenced this issue Sep 4, 2022
    (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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
incompatible-change Incompatible/breaking change team-Rules-Python Native rules for Python
Projects
None yet
Development

No branches or pull requests