bazel tips and tricks


Often needed little bits of bazel knowledge that I tend to forget.

Use an alternate repository

build --override_repository=com_google_protobuf=/abs/path/to/repo

This allows you to use an alternate source repository from the one declared in WORKSPACE, or worse, in dependencies. You can use that to generate a quick patch, or to provide a fix to upstream.

User bazelrc

in .bazelrc:

try-import %workspace%/user.bazelrc

Then create user.bazelrc in your workspace root and add it to .gitignore if you use git, so that it remains unique to you.

But, don’t forget that you have it.

Use rules_rust

Not sure why, but various bazel rules don’t seem to be documented well. While there are examples, there’s minimal explanation on how rules work. And the way rules work changes substantially across rules releases.

To fix this, I made a small but complete example of using rules_rust and its IDE integration via rust-project.json.

See it at:, refer to for details.

Expand locations in a build rule

ctx.expand_location(val, ctx.attr.deps)

This expands any appearances of $(location <label>) in deps.

Examples of some of the above

Template expansion

Generate a file based on a template.


Release an archive with GitHub actions and Bazel

Here’s an example: