繁体   English   中英

尝试使用 pip_install 会导致:“repository_rule 中的错误:'repository rule http_archive' 只能在工作空间加载期间调用”

[英]Attempt to use pip_install results in: "Error in repository_rule: 'repository rule http_archive' can only be called during workspace loading"

考虑以下示例,由三个文件组成:

建造

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

pip_install(
    requirements = ":requirements.txt",
)

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

工作空间

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_python",
    sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
        "https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
    ],
)

二进制文件

print('hello, world')

我收到以下错误:

> bazel run bin
ERROR: Traceback (most recent call last):
        File "/data/d33tah/workspace/tmp/experiments/bazel/3-dependencies-without-docker/repro/BUILD", line 3, column 12, in <toplevel>
                pip_install(
        File "/home/d33tah/.cache/bazel/_bazel_d33tah/055ed32c3fa80a842a34f0252f6032c8/external/rules_python/python/pip.bzl", line 82, column 29, in pip_install
                pip_install_dependencies()
        File "/home/d33tah/.cache/bazel/_bazel_d33tah/055ed32c3fa80a842a34f0252f6032c8/external/rules_python/python/pip_install/repositories.bzl", line 67, column 14, in pip_install_dependencies
                maybe(
        File "/home/d33tah/.cache/bazel/_bazel_d33tah/055ed32c3fa80a842a34f0252f6032c8/external/bazel_tools/tools/build_defs/repo/utils.bzl", line 201, column 18, in maybe
                repo_rule(name = name, **kwargs)
Error in repository_rule: 'repository rule http_archive' can only be called during workspace loading
ERROR: Skipping 'bin': no such target '//:bin': target 'bin' not declared in package '' defined by /data/d33tah/workspace/tmp/experiments/bazel/3-dependencies-without-docker/repro/BUILD
WARNING: Target pattern parsing failed.
ERROR: no such target '//:bin': target 'bin' not declared in package '' defined by /data/d33tah/workspace/tmp/experiments/bazel/3-dependencies-without-docker/repro/BUILD
INFO: Elapsed time: 0.091s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
FAILED: Build did NOT complete successfully (0 packages loaded)

创建空的requirements.txt没有帮助。 此外,如果它有任何相关性:

> bazel version
Bazelisk version: v1.10.1
Build label: 4.2.2
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Thu Dec 2 18:15:58 2021 (1638468958)
Build timestamp: 1638468958
Build timestamp as int: 1638468958

我做错了什么,我该如何解决?

Bazel Slack 上的 Matt Mackay 立即找到了解决方案:

pip_install 是一个存储库规则,所以它应该在 WORKSPACE 文件中。 https://github.com/bazelbuild/rules_python/blob/main/docs/pip.md#pip_install

James "jsharpe" Sharpe 也指出:

快速浏览一下,当它应该在 WORKSPACE 文件中时,您正在 BUILD 文件中调用 pip_install。

这是更正后的代码:

建造


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

工作空间

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "rules_python",
    sha256 = "954aa89b491be4a083304a2cb838019c8b8c3720a7abb9c4cb81ac7a24230cea",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
        "https://github.com/bazelbuild/rules_python/releases/download/0.4.0/rules_python-0.4.0.tar.gz",
    ],
)

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

pip_install(
    requirements = ":requirements.txt",
)


二进制文件

print('hello, world')

要求.txt

# intentionally left empty

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM