如何设置一个基本的repository_rule?
How to setup a basic repository_rule?
我在尝试创建自定义存储库规则时迷路了...
pypi.bzl
:
def _impl(repository_ctx):
repository_ctx.execute(['echo', '"testing testing 123"'])
repository_ctx.execute(['pip', 'download', repository_ctx.attr.package])
repository_ctx.file("BUILD",
"""
py_library(
name = "lib",
srcs = glob(["*.py"]),
)
""")
pypi_package = repository_rule(
implementation=_impl,
attrs={"package": attr.string(mandatory=True)},
)
WORKSPACE
:
load("//:pypi.bzl", "pypi_package")
pypi_package(
name = "dateutil",
package = "python-dateutil",
)
BUILD
:
py_binary(
name = "app",
srcs = ["app.py"],
deps = ["@dateutil//lib"],
)
$ bazel build app
ERROR: /Users/alec/code/bazel-pypi/BUILD:1:1: no such package '@dateutil//lib': BUILD file not found on package path and referenced by '//:app'.
ERROR: Analysis of target '//:app' failed; build aborted.
我什至不确定如何开始调试这个问题,因为我的存储库规则实施中的命令似乎根本就不是 运行。 (例如,echo
没有输出)。如果 repository_ctx.file
调用没有发生,显然不会有 library
目标...
错误显示 no such package '@dateutil//lib'
,因此很自然地会使用 bazel 查询来确定确实存在哪些包:
$ bazel query @dateutil//...
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:2:2: indentation error.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:5:3: Trailing comma is allowed only in parenthesized tuples.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:6:6: syntax error at 'outdent': expected expression.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:2:2: assignment length mismatch: left-hand side has length 2, but right-hand side evaluates to value of length 1.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:6:6: contains syntax error(s).
ERROR: package contains errors: .
ERROR: error loading package '@dateutil//': Package '' contains errors.
看来 BUILD 文件格式不正确。特别是,您需要去掉 "py_library" 之前的缩进。解决这个问题并重试,我们得到
$ bazel query @dateutil//...
@dateutil//:lib
所以错误是您的 BUILD 文件包含“@dateutil//lib”而不是“@dateutil//:lib”。
那么现在的问题是,为什么“@dateutil//:lib”是正确的?要理解这一点,您需要了解 Bazel 的标签语法。 “@dateutil//lib”是“@dateutil//lib:lib”的shorthand,意思是"the lib target in the lib package in the dateutil repository"。 "lib package in the dateutil repository" 对应于 "lib/BUILD"。但是您的存储库规则写入 "BUILD",而不是 "lib/BUILD"。所以你的目标是在“”包中创建的。因此,您创建的 py_library 的语法是“@dateutil//:lib”。
我在尝试创建自定义存储库规则时迷路了...
pypi.bzl
:
def _impl(repository_ctx):
repository_ctx.execute(['echo', '"testing testing 123"'])
repository_ctx.execute(['pip', 'download', repository_ctx.attr.package])
repository_ctx.file("BUILD",
"""
py_library(
name = "lib",
srcs = glob(["*.py"]),
)
""")
pypi_package = repository_rule(
implementation=_impl,
attrs={"package": attr.string(mandatory=True)},
)
WORKSPACE
:
load("//:pypi.bzl", "pypi_package")
pypi_package(
name = "dateutil",
package = "python-dateutil",
)
BUILD
:
py_binary(
name = "app",
srcs = ["app.py"],
deps = ["@dateutil//lib"],
)
$ bazel build app
ERROR: /Users/alec/code/bazel-pypi/BUILD:1:1: no such package '@dateutil//lib': BUILD file not found on package path and referenced by '//:app'.
ERROR: Analysis of target '//:app' failed; build aborted.
我什至不确定如何开始调试这个问题,因为我的存储库规则实施中的命令似乎根本就不是 运行。 (例如,echo
没有输出)。如果 repository_ctx.file
调用没有发生,显然不会有 library
目标...
错误显示 no such package '@dateutil//lib'
,因此很自然地会使用 bazel 查询来确定确实存在哪些包:
$ bazel query @dateutil//...
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:2:2: indentation error.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:5:3: Trailing comma is allowed only in parenthesized tuples.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:6:6: syntax error at 'outdent': expected expression.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:2:2: assignment length mismatch: left-hand side has length 2, but right-hand side evaluates to value of length 1.
ERROR: /usr/local/google/home/ajmichael/.cache/bazel/_bazel_ajmichael/473b6943104a48bd7642dd8ab62af629/external/dateutil/BUILD:6:6: contains syntax error(s).
ERROR: package contains errors: .
ERROR: error loading package '@dateutil//': Package '' contains errors.
看来 BUILD 文件格式不正确。特别是,您需要去掉 "py_library" 之前的缩进。解决这个问题并重试,我们得到
$ bazel query @dateutil//...
@dateutil//:lib
所以错误是您的 BUILD 文件包含“@dateutil//lib”而不是“@dateutil//:lib”。
那么现在的问题是,为什么“@dateutil//:lib”是正确的?要理解这一点,您需要了解 Bazel 的标签语法。 “@dateutil//lib”是“@dateutil//lib:lib”的shorthand,意思是"the lib target in the lib package in the dateutil repository"。 "lib package in the dateutil repository" 对应于 "lib/BUILD"。但是您的存储库规则写入 "BUILD",而不是 "lib/BUILD"。所以你的目标是在“”包中创建的。因此,您创建的 py_library 的语法是“@dateutil//:lib”。