Common useful functions and rules for Bazel
Go to file
Yun Peng 963f582d17
Explicitly enable workspace for some tasks
2024-09-13 14:28:41 +02:00
.bazelci Explicitly enable workspace for some tasks 2024-09-13 14:28:41 +02:00
.bcr Add bcr setup for gazelle (#519) 2024-05-31 11:45:14 +02:00
distribution
docs Strip compatible_with from bzl_library rules (#522) 2024-07-15 19:50:23 +02:00
gazelle Bump version to 1.7.1 (#521) 2024-06-03 15:22:42 +02:00
lib Make only root test_deps externally visible (#508) 2024-07-15 17:57:10 +00:00
rules Add test_deps to rules/private (#534) 2024-08-09 08:40:50 -04:00
tests Fix bazel_skylib with Bazel@HEAD (#538) 2024-09-11 15:16:25 +00:00
toolchains/unittest Make only root test_deps externally visible (#508) 2024-07-15 17:57:10 +00:00
.bazelignore
.gitignore
AUTHORS
BUILD Strip compatible_with from bzl_library rules (#522) 2024-07-15 19:50:23 +02:00
CHANGELOG.md Bump version to 1.7.1 (#521) 2024-06-03 15:22:42 +02:00
CODEOWNERS
CONTRIBUTING.md
CONTRIBUTORS
LICENSE
MODULE.bazel Bump version to 1.7.1 (#521) 2024-06-03 15:22:42 +02:00
MODULE.bazel-remove-override.patch
README.md Update README.md (#380) 2024-07-15 18:03:44 +00:00
WORKSPACE
WORKSPACE.bzlmod
bzl_library.bzl Strip compatible_with from bzl_library rules (#522) 2024-07-15 19:50:23 +02:00
lib.bzl
skylark_library.bzl
version.bzl Bump version to 1.7.1 (#521) 2024-06-03 15:22:42 +02:00
workspace.bzl

README.md

Skylib

Build status

Skylib is a library of Starlark functions for manipulating collections, file paths, and various other data types in the domain of Bazel build rules.

Each of the .bzl files in the lib directory defines a "module"—a struct that contains a set of related functions and/or other symbols that can be loaded as a single unit, for convenience.

Skylib also provides build rules under the rules directory.

Getting Started

WORKSPACE file

See the WORKSPACE setup section for the current release.

If you want to use lib/unittest.bzl from Skylib versions released in or after December 2018, then you also should add to the WORKSPACE file:

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

BUILD and *.bzl files

Then, in the BUILD and/or *.bzl files in your own workspace, you can load the modules (listed below) and access the symbols by dotting into those structs:

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:shell.bzl", "shell")

p = paths.basename("foo.bar")
s = shell.quote(p)

List of modules (in lib/)

List of rules (in rules/)

Writing a new module

The criteria for adding a new function or module to this repository are:

  1. Is it widely needed? The new code must solve a problem that occurs often during the development of Bazel build rules. It is not sufficient that the new code is merely useful. Candidate code should generally have been proven to be necessary across several projects, either because it provides indispensable common functionality, or because it requires a single standardized implementation.

  2. Is its interface simpler than its implementation? A good abstraction provides a simple interface to a complex implementation, relieving the user from the burden of understanding. By contrast, a shallow abstraction provides little that the user could not easily have written out for themselves. If a function's doc comment is longer than its body, it's a good sign that the abstraction is too shallow.

  3. Is its interface unimpeachable? Given the problem it tries to solve, does it have sufficient parameters or generality to address all reasonable cases, or does it make arbitrary policy choices that limit its usefulness? If the function is not general, it likely does not belong here. Conversely, if it is general thanks only to a bewildering number of parameters, it also does not belong here.

  4. Is it efficient? Does it solve the problem using the asymptotically optimal algorithm, without using excessive looping, allocation, or other high constant factors? Starlark is an interpreted language with relatively expensive basic operations, and an approach that might make sense in C++ may not in Starlark.

If your new module meets all these criteria, then you should consider sending us a pull request. It is always better to discuss your plans before executing them.

Many of the declarations already in this repository do not meet this bar.

Steps to add a module to Skylib:

  1. Create a new .bzl file in the lib directory.

  2. Write the functions or other symbols (such as constants) in that file, defining them privately (prefixed by an underscore).

  3. Create the exported module struct, mapping the public names of the symbols to their implementations. For example, if your module was named things and had a function named manipulate, your things.bzl file would look like this:

    def _manipulate():
      ...
    
    things = struct(
        manipulate=_manipulate,
    )
    
  4. Add unit tests for your module in the tests directory.

bzl_library

The bzl_library.bzl rule can be used to aggregate a set of Starlark files and its dependencies for use in test targets and documentation generation.

Troubleshooting

If you try to use unittest and you get the following error:

ERROR: While resolving toolchains for target //foo:bar: no matching toolchains found for types @bazel_skylib//toolchains:toolchain_type
ERROR: Analysis of target '//foo:bar' failed; build aborted: no matching toolchains found for types @bazel_skylib//toolchains:toolchain_type

then you probably forgot to load and call bazel_skylib_workspace() in your WORKSPACE file.

Maintainer's guide

See the maintaner's guide for instructions for cutting a new release.

Gazelle Plugin

bazel_skylib ships with a gazelle plugin to generate bzl_library entries in build files. To use this, in your WORKSPACE:

load("@bazel_skylib_gazelle_plugin//:workspace.bzl", "bazel_skylib_gazelle_plugin_workspace")

bazel_skylib_gazelle_plugin_workspace()

load("@bazel_skylib_gazelle_plugin//:setup.bzl", "bazel_skylib_gazelle_plugin_setup")

bazel_skylib_gazelle_plugin_setup()

You may then include the plugin using code similar to this in your BUILD.bazel file:

load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")

gazelle(
    name = "gazelle",
    gazelle = ":gazelle_bin",
)

gazelle_binary(
    name = "gazelle_bin",
    languages = DEFAULT_LANGUAGES + [
        "@bazel_skylib_gazelle_plugin//bzl",
    ],
)