aspect-build / rules_py

More compatible Bazel rules for running Python tools and building Python projects
Apache License 2.0
84 stars 23 forks source link

[Bug]: interpreter version error when defining a Python toolchain #365

Open laurentlb opened 3 months ago

laurentlb commented 3 months ago

What happened?

When trying to define a Python toolchain, I get the error "_MUST_SET_INTERPRETER_VERSION_FLAG" which incorrectly complains about my Bazel version.

Version

Development (host) and target OS/architectures:

Output of bazel --version: 7.2.1

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: 0.7.3

Language(s) and/or frameworks involved:

How to reproduce

$ cat MODULE.bazel
bazel_dep(name = "aspect_rules_py", version = "0.7.3")

register_toolchains("//:my_toolchain")
$ cat BUILD 

load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
load("@aspect_rules_py//py:defs.bzl", "py_binary")

py_binary(
  name = "hello",
  srcs = ["hello.py"],
)

py_runtime(
    name = "python_runtime",
    interpreter_path = "/usr/bin/python3",
    python_version = "PY3",
)

py_runtime_pair(
    name = "my_py_runtime_pair",
    py3_runtime = ":python_runtime",
)

toolchain(
    name = "my_toolchain",
    toolchain = ":my_py_runtime_pair",
    toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)
$ bazel run :hello
ERROR: /Users/laurent/code/repro-python/BUILD:4:10: in py_binary rule //:hello: 
Traceback (most recent call last):
    File "/Users/laurent/output_base/external/aspect_rules_py~/py/private/py_binary.bzl", line 18, column 51, in _py_binary_rule_impl
        py_toolchain = _py_semantics.resolve_toolchain(ctx)
    File "/Users/laurent/output_base/external/aspect_rules_py~/py/private/py_semantics.bzl", line 103, column 13, in _resolve_toolchain
        fail(_MUST_SET_INTERPRETER_VERSION_FLAG)
Error in fail: ERROR: Prior to Bazel 7.x, the python interpreter version must be explicitly provided.

For example in `.bazelrc` with Bazel 6.4, add

    common --@aspect_rules_py//py:interpreter_version=3.9.18

Bazel 6.3 and earlier didn't handle the `common` verb for custom flags.
Repeat the flag to avoid discarding the analysis cache:

    build --@aspect_rules_py//py:interpreter_version=3.9.18
    fetch --@aspect_rules_py//py:interpreter_version=3.9.18
    query --@aspect_rules_py//py:interpreter_version=3.9.18
ERROR: /Users/laurent/code/repro-python/BUILD:4:10: Analysis of target '//:hello' failed
ERROR: Analysis of target '//:hello' failed; build aborted
INFO: Elapsed time: 0.152s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

$ bazel --version 
bazel 7.2.1


### Any other information?

_No response_
njlr commented 1 month ago

Does your .bazelrc have an interpreter version? e.g.

common --@aspect_rules_py//py:interpreter_version=3.9.18
alexeagle commented 2 weeks ago

@laurentlb I think the error message was trying to tell you exactly what to do: upgrade to Bazel 7 or use one of the workaround flags. LMK if that's still not working?

laurentlb commented 2 weeks ago

From the original message :

Output of bazel --version: 7.2.1

So I think the error message was wrong. (I don't fully remember the context of the bug and I haven't tried njlr's suggestion)

alexeagle commented 2 weeks ago

Oh I see now. I wonder if it's related to "pystar" https://github.com/bazelbuild/rules_python/issues/1069