Skip to content

Commit 861e23a

Browse files
authored
docs: tell authors how to publish stardocs (#6209)
Fixes #5593
1 parent b0fe80a commit 861e23a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

docs/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ The tool also accepts a `--version` option to update the `source.json` of a spec
5050
# the target can also be your target that depends on this.
5151
```
5252

53+
### Documenting the module
54+
55+
The `source.json` permits a `docs_url` attribute pointing to the documentation.
56+
57+
This may optionally point to an archive file of stardoc_output.proto files, see [Stardoc API Docs](./stardoc.md).
58+
5359
## Presubmit
5460

5561
Every module version must pass the BCR presubmit before getting merged. The presubmit validates the correctness and consistency of module information, then runs build and test targets specified in the `presubmit.yml` file. The BCR presubmit is driven by the [bcr_presubmit.py](https://github.com/bazelbuild/continuous-integration/blob/master/buildkite/bazel-central-registry/bcr_presubmit.py) script on [Bazel CI](https://github.com/bazelbuild/continuous-integration/tree/master/buildkite#bazel-continuous-integration).

docs/stardoc.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)