rules_python icon indicating copy to clipboard operation
rules_python copied to clipboard

no such target '@rules_python//python:current_py_toolchain'

Open matt-sm opened this issue 2 years ago • 4 comments

🐞 bug report

Affected Rule

The issue is caused by the rule: current_py_toolchain

Description

Unable to use current_py_toolchain with hermetic toolchains

🔬 Minimal Reproduction

WORKSPACE.bazel

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_python",
    sha256 = "a644da969b6824cc87f8fe7b18101a8a6c57da5db39caa6566ec6109f37d2141",
    strip_prefix = "rules_python-0.20.0",
    url = "https://github.com/bazelbuild/rules_python/releases/download/0.20.0/rules_python-0.20.0.tar.gz",
)

register_toolchains("//python:python_toolchain")

python/BUILD.bazel

load("@rules_python//python:defs.bzl", "py_runtime", "py_runtime_pair")

package(default_visibility = ["//visibility:public"])

py_runtime(
    name = "py3_runtime",
    files = ["//foreign_cc:Python"], # custom Python3 build
    interpreter = "//foreign_cc:python3_bin",
    python_version = "PY3",
)

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

toolchain(
    name = "python_toolchain",
    toolchain = ":py_runtime_pair",
    toolchain_type = "@rules_python//python:toolchain_type",
)

genrule(
    name = "python_version",
    srcs = [],
    outs = ["out.txt"],
    cmd = "$(PYTHON3) --version > $(location out.txt)",
    toolchains=["@rules_python//python:current_py_toolchain"],
)

🔥 Exception or Error

no such target '@rules_python//python:current_py_toolchain': target 'current_py_toolchain' not declared in package 'python' defined by /root/.cache/bazel/_bazel_root/2c1115910326a22ebb790fbe5829dd0f/external/rules_python/python/BUILD

query output:

bazel query "@rules_python//python:*"
@rules_python//python:BUILD
@rules_python//python:PY2
@rules_python//python:PY3
@rules_python//python:autodetecting_toolchain
@rules_python//python:autodetecting_toolchain_nonstrict
@rules_python//python:bzl
@rules_python//python:defs.bzl
@rules_python//python:distribution
@rules_python//python:packaging.bzl
@rules_python//python:pip.bzl
@rules_python//python:python.bzl
@rules_python//python:python_version
@rules_python//python:repositories.bzl
@rules_python//python:toolchain_type
@rules_python//python:versions.bzl

🌍 Your Environment

Operating System: Python:3.10

Output of bazel version: bazel 6.1.1

Rules_python version: 0.20.0

Anything else relevant?

matt-sm avatar Mar 29 '23 05:03 matt-sm

I am no longer trying to use a genrule and have modified this to a more "traditional" setup using pip_parse and requirements.txt (BUILD.bazel remains the same as above, minus the genrule):

WORKSPACE.bazel:

http_archive(
    name = "rules_python",
    sha256 = "a644da969b6824cc87f8fe7b18101a8a6c57da5db39caa6566ec6109f37d2141",
    strip_prefix = "rules_python-0.20.0",
    url = "https://github.com/bazelbuild/rules_python/releases/download/0.20.0/rules_python-0.20.0.tar.gz",
)

load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

register_toolchains("//python:python_toolchain")

load("@rules_python//python:pip.bzl", "pip_parse")

pip_parse(
    name = "pip",
    # python_interpreter_target = <???>
    requirements_lock = "//python:requirements.txt",
)

load("@pip//:requirements.bzl", "install_deps")

install_deps()

This all works, but I am curious when using a custom python interpreter built by another repository rule, what value should go in python_interpreter_target ?

matt-sm avatar Apr 03 '23 06:04 matt-sm

So the only example I can see is like this where python is built in the http_archive:

https://github.com/kku1993/bazel-hermetic-python/blob/master/WORKSPACE#L42

I'm building python in a build rule that then needs to be run before the pip_parse in the workspace. This is the part that confuses me - how do I create the dependency between this build rule and the workspace pip_parse?

matt-sm avatar Apr 17 '23 02:04 matt-sm

This all works, but I am curious when using a custom python interpreter built by another repository rule, what value should go in python_interpreter_target ?

The label for the Python binary.

Are you building your own version of Python?

You say you are building it, so the way the WORKSPACE rule resolves the label may not work. You can write a rule that uses pip_parse outside the WORKSPACE file. Also, you could have an external project that builds Python and provides a label accessible to pip_parse.

chrislovecnm avatar Aug 07 '23 18:08 chrislovecnm

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

github-actions[bot] avatar Feb 03 '24 22:02 github-actions[bot]

This issue was automatically closed because it went 30 days without a reply since it was labeled "Can Close?"

github-actions[bot] avatar Mar 04 '24 22:03 github-actions[bot]