Skip to content

Pinned, prebuilt versions of buildozer for Bazel projects

License

Notifications You must be signed in to change notification settings

fmeum/buildozer

Repository files navigation

buildozer

This Bazel module provides a pinned, prebuilt version of buildozer, a tool for manipulating Bazel BUILD files.

Requirements

  • Bazel 6.2.0 or later

Usage

  1. Add the following line to your MODULE.bazel file:
bazel_dep(name = "buildozer", version = "7.1.2", dev_dependency = True)
  1. Run buildozer via bazel run:
bazel run @buildozer -- ...

The -- is optional if you don't need to pass arguments to buildozer that start with a dash.

Using buildozer in repository rules and module extensions

You can also use buildozer in a repository rule or module extension, i.e., during the loading phase:

  1. Add the following line to your MODULE.bazel file:
bazel_dep(name = "buildozer", version = "7.1.2")
  1. In your repository rule or module extension implementation function, get the path to the buildozer binary as follows:
load("@buildozer//:buildozer.bzl", "BUILDOZER_LABEL")
...
def my_impl(repository_or_module_ctx):
    buildozer = repository_or_module_ctx.path(BUILDOZER_LABEL)
    ...
    repository_or_module_ctx.execute(
        [buildozer, 'set foo bar', '//path/to/pkg:target']
    )

Keep the path call at the top of your implementation function as it may cause a restart of the repository rule.

Alternative usage

If you dont want to or can't load from @buildozer, you can also use the following approach:

  1. Add the following lines to your MODULE.bazel file:
bazel_dep(name = "buildozer", version = "7.1.2")

buildozer_binary = use_extension("@buildozer//:buildozer_binary.bzl", "buildozer_binary")
use_repo(buildozer_binary, "buildozer_binary")
  1. In your repository rule or module extension implementation function, get the path to the buildozer binary as follows:
def my_impl(repository_or_module_ctx):
    # The ".exe" suffix is *not* a typo. It is present on all platforms to support
    # Windows while maintaining a stable label.
    buildozer = repository_or_module_ctx.path(Label("@buildozer_binary//:buildozer.exe))