2

我们经常需要每个技术堆栈的通用规则组合。目前这在 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 也无济于事。

4

1 回答 1

3

我找到了一个解决方案:

将其拆分为两个文件。一个像这样的进口:

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

def declare():
    maybe(
        git_repository,
        name = "rules_cc",
        commit = "34ca16f4aa4bf2a5d3e4747229202d6cb630ebab",
        remote = "https://github.com/bazelbuild/rules_cc.git",
        shallow_since = "1584036492 -0700",
    )
    # ... for me requires at least rules_cc, rules_python, bazel_skylib
    # for later proto, docker, go, java support

另一个使用声明的外部源:

# go
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

# protobuf
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

# container
load("@io_bazel_rules_docker//container:container.bzl", "container_pull")
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//go:image.bzl", go_image_repositories = "repositories")

def init_rules():
    go_rules_dependencies()
    go_register_toolchains()
    gazelle_dependencies()
    rules_proto_dependencies()
    rules_proto_toolchains()
    container_repositories()
    container_deps()
    go_image_repositories()
    container_pull(
        name = "go_static",
        digest = "sha256:9b60270ec0991bc4f14bda475e8cae75594d8197d0ae58576ace84694aa75d7a",
        registry = "gcr.io",
        repository = "distroless/static",
    )

这有点麻烦,但是使用http_archiveor获取这个 repo git_repository,加载第一个文件并调用declare并加载第二个文件并init_rules调用它。

这可能有点复杂,但它仍然有助于统一堆栈并简化您的WORKSPACE.

于 2020-03-31T13:54:46.007 回答