1# Skylib 2 3[](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* [modules](docs/modules_doc.md) 52* [new_sets](docs/new_sets_doc.md) 53* [shell](docs/shell_doc.md) 54* [structs](docs/structs_doc.md) 55* [subpackages](docs/subpackages_doc.md) 56* [types](docs/types_doc.md) 57* [unittest](docs/unittest_doc.md) 58* [versions](docs/versions_doc.md) 59 60## List of rules (in rules/) 61 62* [analysis_test](docs/analysis_test_doc.md) 63* [build_test](docs/build_test_doc.md) 64* [common_settings](docs/common_settings_doc.md) 65* [directories](docs/copy_directory_doc.md) 66 * [directory](docs/directory_doc.md) 67 * [directory_glob](docs/directory_glob.md) 68 * [subdirectory](docs/subdirectory_doc.md) 69* [copy_directory](docs/copy_directory_doc.md) 70* [copy_file](docs/copy_file_doc.md) 71* [diff_test](docs/diff_test_doc.md) 72* [expand_template](docs/expand_template_doc.md) 73* [native_binary and native_test](docs/native_binary_doc.md) 74* [run_binary](docs/run_binary_doc.md) 75* [select_file](docs/select_file_doc.md) 76* [write_file](docs/write_file_doc.md) 77 78## Writing a new module 79 80The criteria for adding a new function or module to this repository are: 81 821. 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. 83 841. 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. 85 861. 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. 87 881. 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. 89 90If 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. 91 92Many of the declarations already in this repository do not meet this bar. 93 94 95Steps to add a module to Skylib: 96 971. Create a new `.bzl` file in the `lib` directory. 98 991. Write the functions or other symbols (such as constants) in that file, 100 defining them privately (prefixed by an underscore). 101 1021. Create the exported module struct, mapping the public names of the symbols 103 to their implementations. For example, if your module was named `things` and 104 had a function named `manipulate`, your `things.bzl` file would look like 105 this: 106 107 ```python 108 def _manipulate(): 109 ... 110 111 things = struct( 112 manipulate=_manipulate, 113 ) 114 ``` 115 1161. Add unit tests for your module in the `tests` directory. 117 118## `bzl_library` 119 120The `bzl_library.bzl` rule can be used to aggregate a set of 121Starlark files and its dependencies for use in test targets and 122documentation generation. 123 124## Troubleshooting 125 126If you try to use `unittest` and you get the following error: 127 128``` 129ERROR: While resolving toolchains for target //foo:bar: no matching toolchains found for types @bazel_skylib//toolchains:toolchain_type 130ERROR: Analysis of target '//foo:bar' failed; build aborted: no matching toolchains found for types @bazel_skylib//toolchains:toolchain_type 131``` 132 133then you probably forgot to load and call `bazel_skylib_workspace()` in your 134`WORKSPACE` file. 135 136### Maintainer's guide 137 138See the [maintaner's guide](docs/maintainers_guide.md) for instructions for 139cutting a new release. 140 141## Gazelle Plugin 142 143`bazel_skylib` ships with a [gazelle](https://github.com/bazelbuild/bazel-gazelle) 144plugin to generate `bzl_library` entries in build files. To use this, in your 145`WORKSPACE`: 146 147```starlark 148load("@bazel_skylib_gazelle_plugin//:workspace.bzl", "bazel_skylib_gazelle_plugin_workspace") 149 150bazel_skylib_gazelle_plugin_workspace() 151 152load("@bazel_skylib_gazelle_plugin//:setup.bzl", "bazel_skylib_gazelle_plugin_setup") 153 154bazel_skylib_gazelle_plugin_setup() 155``` 156 157You may then include the plugin using code similar to this in your `BUILD.bazel` 158file: 159 160```starlark 161load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary") 162 163gazelle( 164 name = "gazelle", 165 gazelle = ":gazelle_bin", 166) 167 168gazelle_binary( 169 name = "gazelle_bin", 170 languages = DEFAULT_LANGUAGES + [ 171 "@bazel_skylib_gazelle_plugin//bzl", 172 ], 173) 174``` 175