我们经常需要每个技术堆栈的通用规则组合。目前这在 WORKSPACE 中浪费了大量空间——它们应该在多个存储库中保持同步。在 buildifier 之后有 50 多行,并且包含太多的 url、版本和哈希。
现在说我有一个“技术堆栈”回购并做类似的事情
load("@techstack_repo//mylang.bzl", "load_rules")
load_rules()
将在哪里load_rules
加载和初始化例如 rules_go、bazel-gazelle、rules_docker、rules_proto 的固定版本并以正确的顺序初始化它们,以便它们在 WORKSPACE 中可见?
我没有让它在我的测试中工作,因为load
显然不能在 bzl 文件中的函数中运行 - 它本身不是函数。
有没有办法做到这一点?
这是我为 Java 测试的示例:
load("@io_bazel_rules_docker//repositories:repositories.bzl", container_repositories = "repositories")
load("@io_bazel_rules_docker//repositories:deps.bzl", container_deps = "deps")
load("@io_bazel_rules_docker//container:container.bzl", "container_pull")
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
load(
"@io_grpc_grpc_java//:repositories.bzl",
"IO_GRPC_GRPC_JAVA_ARTIFACTS",
"IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS",
"grpc_java_repositories",
)
load("@rules_jvm_external//:defs.bzl", "maven_install")
def prepare_stack(maven_deps = []):
container_repositories()
container_deps()
container_pull(
name = "java_base",
# https://console.cloud.google.com/gcr/images/distroless/GLOBAL/java-debian10
# tag = "11", # OpenJDK 11 as of 2020-03-04
digest = "sha256:eda9e5ae2facccc9c7016f0c2d718d2ee352743bda81234783b64aaa402679b6",
registry = "gcr.io",
repository = "distroless/java-debian10",
)
rules_proto_dependencies()
rules_proto_toolchains()
maven_install(
artifacts = maven_deps + IO_GRPC_GRPC_JAVA_ARTIFACTS,
# for improved debugging in IDE
fetch_sources = True,
generate_compat_repositories = True,
override_targets = IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS,
repositories = [
"https://repo.maven.apache.org/maven2/",
"https://repo1.maven.org/maven2",
],
strict_visibility = True,
)
grpc_java_repositories()
...所有http_archive
对规则回购的要求都在WORKSPACE
,我想把它们移到这里,但这根本不起作用。照原样,我收到此错误:
ERROR: Failed to load Starlark extension '@rules_python//python:pip.bzl'.
Cycle in the workspace file detected. This indicates that a repository is used prior to being defined.
The following chain of repository dependencies lead to the missing definition.
- @rules_python
This could either mean you have to add the '@rules_python' repository with a statement like `http_archive` in your WORKSPACE file (note that transitive dependencies are not added automatically), or move an existing definition earlier in your WORKSPACE file.
也添加 rules_python 也无济于事。