Resting BUILD Face

DZone 's Guide to

Resting BUILD Face

A quick read on an open source tool, called Buildifier, used at Google to format BUILD files.

· DevOps Zone ·
Free Resource

I am super excited that pmbethe09 and lautentlb just put in a bunch of extra work to open source Buildifier. Buildifier is a great tool we use in Google to format BUILD files. It automatically organizes attributes, corrects indentation, and generally makes them more readable and excellent.

Image title

To try it out, clone the repo and build it with Bazel:

$ git clone git@github.com:bazelbuild/buildifier.git
$ cd buildifier
$ bazel build //buildifier
Extracting Bazel installation...

INFO: Found 1 target...
Target //buildifier:buildifier up-to-date:

INFO: Elapsed time: 203.309s, Critical Path: 7.54s
INFO: Build completed successfully, 8 total actions

Now try it out on an ugly BUILD file:

$ echo 'cc_library(srcs = ["foo.cc", "bar.cc"], name = "foo")' > BUILD
$ ~/gitroot/buildifier/bazel-bin/buildifier/buildifier BUILD
$ cat BUILD
    name = "foo",
    srcs = [

Finally, why run commands manually when you can have your editor do it for you? I use emacs, so I can set up a hook like this:

(add-hook 'before-save-hook '(lambda ()
   "Runs buildifier on BUILD files before saving"
   (if (eq 'BUILD (file-name-base (buffer-file-name)))
    (shell-command (concat "/home/kchodorow/gitroot/buildifier/bazel-bin/buildifier/buildifier " (buffer-file-name))))))

You could also set up a git hook to run this before committing, if that’s more your style. Regardless, give it a try! It’s a quick, easy way to make your BUILD files more readable.

bazel, build file

Published at DZone with permission of Kristina Chodorow , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}