Arne Hormann
Arne Hormann

Reputation: 75

Can I load common rules from a .bzl file?

We frequently need common combinations of rules per tech stack. That currently wastes a lot of space in WORKSPACE - and they should be kept in sync over multiple repos. It's 50+ lines after buildifier and contains too many urls, versions and hashes.

Now say I have a "technology stack" repo and do something like

load("@techstack_repo//mylang.bzl", "load_rules")
load_rules()

where load_rules would load and initialize pinned versions of e.g. rules_go, bazel-gazelle, rules_docker, rules_proto and initialize all of them in the right order so they are visible in WORKSPACE?

I did not get this to work in my tests because load apparently can not be run in a function in a bzl file - it's not a function itself.

Is there a way to do this?

Here's an example of what I tested for 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()

... all http_archive calls for the rule repos are in WORKSPACE and I want to move them in here, but that did not work at all. As is, I get this error:

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.

also adding rules_python does not help either.

Upvotes: 2

Views: 3604

Answers (1)

Arne Hormann
Arne Hormann

Reputation: 75

I found a solution:

Split it into two files. One with imports like this:

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

and another using the declared external sources:

# 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",
    )

It's a bit of a hassle, but fetch this repo with http_archive or git_repository, load the first file and call declare and load the second for init_rules and call that.

It may be a little convoluted, but it still helps to unify the stack and simplify your WORKSPACE.

Upvotes: 3

Related Questions