为什么依赖工作区不会自动 include/execute 它在 Bazel 中依赖的 WORKSPACE 文件

Why dependent workspace doesn't automatically include/execute the WORKSPACE file it has a dependency on in Bazel

我有两个使用 Bazel 构建的存储库。一个使用 Bazel 导入来构建 protobuf 类型。当我使这个 repo/workspace 成为我的另一个 repo/workspace 的依赖项时,我必须将 rules_proto 和 rules_cc 的所有 protobuf 导入重新添加到这个新的依赖工作区。有没有办法不必这样做,或者这是 Bazel 构建的预期行为?

示例:
工作区 A:

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

http_archive(
    name = "bazel_skylib",
    strip_prefix = "bazel-skylib-67bfa0ce4de5d4b512178d5f63abad1696f6c05b",
    urls = ["https://github.com/bazelbuild/bazel-skylib/archive/67bfa0ce4de5d4b512178d5f63abad1696f6c05b.tar.gz"],
)

http_archive(
    name = "rules_cc",
    sha256 = "d1886f0ea5b6cfe7519b87030811f52620db31bcca7ef9964aa17af2d14f23c4",
    strip_prefix = "rules_cc-cb6d32e4d1ae29e20dd3328109d8cb7f8eccc9be",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/cb6d32e4d1ae29e20dd3328109d8cb7f8eccc9be.tar.gz",
        "https://github.com/bazelbuild/rules_cc/archive/cb6d32e4d1ae29e20dd3328109d8cb7f8eccc9be.tar.gz",
    ],
)

http_archive(
    name = "rules_proto",
    sha256 = "fb7f1959d2d2bf4d7a1f4f29d650845a9a2303b7879c6792320ba8244910ab01",
    strip_prefix = "rules_proto-3212323502e21b819ac4fbdd455cb227ad0f6394",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/3212323502e21b819ac4fbdd455cb227ad0f6394.tar.gz",
        "https://github.com/bazelbuild/rules_proto/archive/3212323502e21b819ac4fbdd455cb227ad0f6394.tar.gz",
    ],
)

load("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies")

rules_cc_dependencies()

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

rules_proto_dependencies()

rules_proto_toolchains()

WORKSPACE B(依赖于 WORKSPACE A):

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

git_repository(
    name = "workspace-a",
    branch = "bazel",
    remote = "url/to/my/git/repo",
)

# WHY DO I HAVE TO DO THE FOLLOWING WHEN IT'S DONE IN WORKSPACE A ALREADY?
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "bazel_skylib",
    strip_prefix = "bazel-skylib-67bfa0ce4de5d4b512178d5f63abad1696f6c05b",
    urls = ["https://github.com/bazelbuild/bazel-skylib/archive/67bfa0ce4de5d4b512178d5f63abad1696f6c05b.tar.gz"],
)

http_archive(
    name = "rules_cc",
    sha256 = "d1886f0ea5b6cfe7519b87030811f52620db31bcca7ef9964aa17af2d14f23c4",
    strip_prefix = "rules_cc-cb6d32e4d1ae29e20dd3328109d8cb7f8eccc9be",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_cc/archive/cb6d32e4d1ae29e20dd3328109d8cb7f8eccc9be.tar.gz",
        "https://github.com/bazelbuild/rules_cc/archive/cb6d32e4d1ae29e20dd3328109d8cb7f8eccc9be.tar.gz",
    ],
)

http_archive(
    name = "rules_proto",
    sha256 = "fb7f1959d2d2bf4d7a1f4f29d650845a9a2303b7879c6792320ba8244910ab01",
    strip_prefix = "rules_proto-3212323502e21b819ac4fbdd455cb227ad0f6394",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/3212323502e21b819ac4fbdd455cb227ad0f6394.tar.gz",
        "https://github.com/bazelbuild/rules_proto/archive/3212323502e21b819ac4fbdd455cb227ad0f6394.tar.gz",
    ],
)

load("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies")

rules_cc_dependencies()

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

rules_proto_dependencies()

rules_proto_toolchains()

这主要是因为 bazel 最初并不是真正设计成包管理器的。 Bazel 来自 monorepo 设计,其中所有内容(您的应用程序、库、third-party 依赖项、数据、工具、工具链、编译器、Bazel 本身等)都被检查到一个单一版本的单一存储库中。因此,不存在事物从何而来以及如何在 domain-specific 规则集(java、c++、python 等)之外集成它们的问题。这一切都在 BUILD 文件中。 WORKSPACE 设计和外部依赖关系用于桥接 monorepo 方法和 non-monorepo 方法。

使用rules_proto_dependencies()rules_proto_toolchains()等宏可以缓解一些不便,但您仍然需要在top-level处加载很多东西,并解决diamond-dependency问题手动。

正在开发一个新的系统调用“bzlmod”来取代所有这些。它最近在 Bazel 5.0 中实验性发布: https://bazel.build/docs/bzlmod