From f70e0d2f2face19336c699a4e2b1b9f8b3a0d5ca Mon Sep 17 00:00:00 2001 From: Michael Krasnyk Date: Thu, 22 Dec 2022 15:06:41 +0100 Subject: [PATCH] Fix Python interpreter target labels with @@ prefixes. ERROR: An error occurred during the fetch of repository 'rules_python~0.16.1~pip~pip': Traceback (most recent call last): File "/home/miha/.cache/bazel/_bazel_miha/bd93af10788cff1331d75ed739998a3c/external/rules_python~0.16.1/python/pip_install/pip_repository.bzl", line 335, column 63, in _pip_repository_impl environment = _create_repository_execution_environment(rctx), File "/home/miha/.cache/bazel/_bazel_miha/bd93af10788cff1331d75ed739998a3c/external/rules_python~0.16.1/python/pip_install/pip_repository.bzl", line 210, column 47, in _create_repository_execution_environment cppflags.extend(_get_toolchain_unix_cflags(rctx)) File "/home/miha/.cache/bazel/_bazel_miha/bd93af10788cff1331d75ed739998a3c/external/rules_python~0.16.1/python/pip_install/pip_repository.bzl", line 115, column 37, in _get_toolchain_unix_cflags if not is_standalone_interpreter(rctx, rctx.attr.python_interpreter_target): File "/home/miha/.cache/bazel/_bazel_miha/bd93af10788cff1331d75ed739998a3c/external/rules_python~0.16.1/python/repositories.bzl", line 64, column 22, in is_standalone_interpreter rctx.path(Label("@{}//:WORKSPACE".format(rctx.attr.python_interpreter_target.workspace_name))).dirname, Error in path: Unable to load package for @[unknown repo 'rules_python~0.16.1~python~python3_10_x86_64-unknown-linux-gnu' requested from @rules_python~0.16.1]//:WORKSPACE: The repository '@[unknown repo 'rules_python~0.16.1~python~python3_10_x86_64-unknown-linux-gnu' requested from @rules_python~0.16.1]' could not be resolved: No repository visible as '@rules_python~0.16.1~python~python3_10_x86_64-unknown-linux-gnu' from repository '@rules_python~0.16.1' --- python/pip_install/pip_repository.bzl | 4 ++-- python/repositories.bzl | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/python/pip_install/pip_repository.bzl b/python/pip_install/pip_repository.bzl index afd9c1875c..9ba3f71a10 100644 --- a/python/pip_install/pip_repository.bzl +++ b/python/pip_install/pip_repository.bzl @@ -1,6 +1,6 @@ "" -load("//python:repositories.bzl", "is_standalone_interpreter") +load("//python:repositories.bzl", "get_interpreter_dirname", "is_standalone_interpreter") load("//python/pip_install:repositories.bzl", "all_requirements") load("//python/pip_install:requirements_parser.bzl", parse_requirements = "parse") load("//python/pip_install/private:srcs.bzl", "PIP_INSTALL_PY_SRCS") @@ -124,7 +124,7 @@ def _get_toolchain_unix_cflags(rctx): fail("could not get python version from interpreter (status {}): {}".format(er.return_code, er.stderr)) _python_version = er.stdout include_path = "{}/include/python{}".format( - rctx.path(Label("@{}//:WORKSPACE".format(rctx.attr.python_interpreter_target.workspace_name))).dirname.realpath, + get_interpreter_dirname(rctx, rctx.attr.python_interpreter_target), _python_version, ) diff --git a/python/repositories.bzl b/python/repositories.bzl index e0c9b0626d..714ffedd6c 100644 --- a/python/repositories.bzl +++ b/python/repositories.bzl @@ -42,6 +42,19 @@ def py_repositories(): STANDALONE_INTERPRETER_FILENAME = "STANDALONE_INTERPRETER" +def get_interpreter_dirname(rctx, python_interpreter_target): + """Get a python interpreter target dirname. + + Args: + rctx (repository_ctx): The repository rule's context object. + python_interpreter_target (Target): A target representing a python interpreter. + + Returns: + str: The Python interpreter directory. + """ + + return rctx.path(Label("{}//:WORKSPACE".format(str(python_interpreter_target).split("//")[0]))).dirname + def is_standalone_interpreter(rctx, python_interpreter_target): """Query a python interpreter target for whether or not it's a rules_rust provided toolchain @@ -61,7 +74,7 @@ def is_standalone_interpreter(rctx, python_interpreter_target): return rctx.execute([ "ls", "{}/{}".format( - rctx.path(Label("@{}//:WORKSPACE".format(rctx.attr.python_interpreter_target.workspace_name))).dirname, + get_interpreter_dirname(rctx, python_interpreter_target), STANDALONE_INTERPRETER_FILENAME, ), ]).return_code == 0