|
| 1 | +# Starlark API Documentation |
| 2 | + |
| 3 | +Modules that expose Starlark APIs (rulesets, for example) may choose to publish documentation as a reference URL on releases. |
| 4 | + |
| 5 | +Read more about this feature: https://blog.aspect.build/stardocs-on-bcr |
| 6 | + |
| 7 | +## Generating API docs |
| 8 | + |
| 9 | +The built-in rule [starlark_doc_extract](https://bazel.build/versions/8.3.0/reference/be/general#starlark_doc_extract) outputs `.binaryproto` files containing `stardoc_output.ModuleInfo` messages. |
| 10 | + |
| 11 | +- Source: https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/stardoc_output.proto |
| 12 | +- Docs: https://buf.build/bazel/bazel/docs/main:stardoc_output#stardoc_output.ModuleInfo |
| 13 | + |
| 14 | +Authors are free to generate these files however they choose. |
| 15 | +The simplest option is to add a `starlark_doc_extract` target for each `bzl_library` target you wish to document. |
| 16 | + |
| 17 | +Note that the `bzl_library` rule from `bazel_skylib` has `filegroup` semantics - it doesn't produce any outputs, nor verify that the `bzl_library` has all dependencies provided. |
| 18 | +See https://github.com/bazel-contrib/bazel-lib/blob/main/bzl_library.bzl for an alternative that fixes this. |
| 19 | + |
| 20 | +It is wise to run `bazel query 'kind(starlark_doc_extract, //...)'` to confirm which starlark modules your ruleset documents. |
| 21 | + |
| 22 | +## Creating the docs artifact |
| 23 | + |
| 24 | +Users of the Publish-to-BCR workflow have an existing `.github/workflows/release_prep.sh` script. |
| 25 | +In this case you can simply add a snippet to create an archive file containing the stardoc output: |
| 26 | + |
| 27 | +```bash |
| 28 | +# Add generated API docs to the release, see https://github.com/bazelbuild/bazel-central-registry/issues/5593 |
| 29 | +docs="$(mktemp -d)"; targets="$(mktemp)" |
| 30 | +bazel --output_base="$docs" query --output=label --output_file="$targets" 'kind("starlark_doc_extract rule", //...)' |
| 31 | +bazel --output_base="$docs" build --target_pattern_file="$targets" |
| 32 | +tar --create --auto-compress \ |
| 33 | + --directory "$(bazel --output_base="$docs" info bazel-bin)" \ |
| 34 | + --file "$GITHUB_WORKSPACE/${ARCHIVE%.tar.gz}.docs.tar.gz" . |
| 35 | +``` |
| 36 | + |
| 37 | +> [!IMPORTANT] |
| 38 | +> The `--output_file` flag was added in Bazel 7.5.0 |
| 39 | +
|
| 40 | +Of course authors are free to produce the docs archive using a different technique. |
| 41 | + |
| 42 | +## Publishing the docs |
| 43 | + |
| 44 | +Again, we assume use of Publish-to-BCR. |
| 45 | + |
| 46 | +Edit `.bcr/source.template.json` to include a link, such as |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + ... |
| 51 | + "docs_url": "https://github.com/{OWNER}/{REPO}/releases/download/{TAG}/{REPO}-{TAG}.docs.tar.gz", |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +> [!IMPORTANT] |
| 56 | +> publish-to-bcr version v0.2.3 or greater is required to pick up a fix for multiple replacements in source.template.json |
| 57 | +
|
| 58 | +## Rendered docs |
| 59 | + |
| 60 | +The https://registry.bazel.build site renders stardocs for modules that have published them. |
| 61 | +The code for that site is in https://github.com/bazel-contrib/bcr-ui. |
0 commit comments