README.md
1# Skylib
2
3[![Build status](https://badge.buildkite.com/921dc61e2d3a350ec40efb291914360c0bfa9b6196fa357420.svg?branch=main)](https://buildkite.com/bazel/bazel-skylib)
4
5Skylib is a library of Starlark functions for manipulating collections, file paths,
6and various other data types in the domain of Bazel build rules.
7
8Each of the `.bzl` files in the `lib` directory defines a "module"—a
9`struct` that contains a set of related functions and/or other symbols that can
10be loaded as a single unit, for convenience.
11
12Skylib also provides build rules under the `rules` directory.
13
14## Getting Started
15
16### `WORKSPACE` file
17
18See the **WORKSPACE setup** section [for the current release](https://github.com/bazelbuild/bazel-skylib/releases).
19
20If you want to use `lib/unittest.bzl` from Skylib versions released in or after
21December 2018, then you also should add to the `WORKSPACE` file:
22
23```python
24load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
25
26bazel_skylib_workspace()
27```
28
29### `BUILD` and `*.bzl` files
30
31Then, in the `BUILD` and/or `*.bzl` files in your own workspace, you can load
32the modules (listed [below](#list-of-modules)) and access the symbols by
33dotting into those structs:
34
35```python
36load("@bazel_skylib//lib:paths.bzl", "paths")
37load("@bazel_skylib//lib:shell.bzl", "shell")
38
39p = paths.basename("foo.bar")
40s = shell.quote(p)
41```
42
43## List of modules (in lib/)
44
45* [collections](docs/collections_doc.md)
46* [dicts](docs/dicts_doc.md)
47* [partial](docs/partial_doc.md)
48* [paths](docs/paths_doc.md)
49* [selects](docs/selects_doc.md)
50* [sets](lib/sets.bzl) - _deprecated_, use `new_sets`
51* [new_sets](docs/new_sets_doc.md)
52* [shell](docs/shell_doc.md)
53* [structs](docs/structs_doc.md)
54* [types](docs/types_doc.md)
55* [unittest](docs/unittest_doc.md)
56* [versions](docs/versions_doc.md)
57
58## List of rules (in rules/)
59
60* [analysis_test](docs/analysis_test_doc.md)
61* [build_test](docs/build_test_doc.md)
62* [copy_file](docs/copy_file_doc.md)
63* [write_file](docs/write_file_doc.md)
64
65## Writing a new module
66
67The criteria for adding a new function or module to this repository are:
68
691. 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 indispensible common functionality, or because it requires a single standardized implementation.
70
711. 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.
72
731. 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.
74
751. 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.
76
77If 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.
78
79Many of the declarations already in this repository do not meet this bar.
80
81
82Steps to add a module to Skylib:
83
841. Create a new `.bzl` file in the `lib` directory.
85
861. Write the functions or other symbols (such as constants) in that file,
87 defining them privately (prefixed by an underscore).
88
891. Create the exported module struct, mapping the public names of the symbols
90 to their implementations. For example, if your module was named `things` and
91 had a function named `manipulate`, your `things.bzl` file would look like
92 this:
93
94 ```python
95 def _manipulate():
96 ...
97
98 things = struct(
99 manipulate=_manipulate,
100 )
101 ```
102
1031. Add unit tests for your module in the `tests` directory.
104
105## `bzl_library`
106
107The `bzl_library.bzl` rule can be used to aggregate a set of
108Starlark files and its dependencies for use in test targets and
109documentation generation.
110
111## Troubleshooting
112
113If you try to use `unittest` and you get the following error:
114
115```
116ERROR: While resolving toolchains for target //foo:bar: no matching toolchains found for types @bazel_skylib//toolchains:toolchain_type
117ERROR: Analysis of target '//foo:bar' failed; build aborted: no matching toolchains found for types @bazel_skylib//toolchains:toolchain_type
118```
119
120then you probably forgot to load and call `bazel_skylib_workspace()` in your
121`WORKSPACE` file.
122
123### Maintainer's guide
124
125See the [maintaner's guide](docs/maintainers_guide.md) for instructions for
126cutting a new release.
127