Bazel macro: docs#
The docs macro defined in docs.bzl is a convenience wrapper
that creates a small set of Bazel targets
to build, verify and preview the project’s Sphinx documentation,
and to create a Python virtual environment for IDE support (Esbonio).
See commands for the targets/commands it creates.
The macro must be called from the repository root package.
Bundle content and supporting files#
There are three ways to add files to a bundle. The data argument of docs()
is shorthand for supporting files in the root :docs_bundle:
docs_bundle(srcs = [...])puts documentation source files in a bundle. The files may be generated outputs from another build action; they are processed as documentation sources and resolved below the bundle’s eventualmount_atpath.docs_bundle(data = [...])puts supporting or runtime files in a bundle payload. Use this for files that belong at the mount but are not themselves documentation sources.docs(data = [...])puts supporting files in the root:docs_bundleexposed bydocs(). A mounted module puts its files in its own bundle; these files travel with that bundle and are resolved below its eventualmount_atpath.An asset entry in
bundles(a plain file target instead of adocs_bundle) adds files that the pages use or link to, but that never become pages: pictures, code, PDF files, generated HTML. See Asset entries in bundles.
If a file is mounted documentation, use docs_bundle(srcs = [...]). Both
bundle attributes make files available to a build; they differ in whether the
files are processed as documentation sources or carried as supporting data.
If a page uses a file (image, literalinclude, a link), use an asset
entry. Use data for files that a tool or an extension reads during the
build.
Minimal example (root BUILD)#
load("//:docs.bzl", "docs")
docs(
source_dir = "docs",
project = "My Project",
project_url = "https://github.com/eclipse-score/my-project",
data = [
# labels to any extra tools or data you want included
# e.g. "//:some_tool" or other supporting files
],
external_needs = [
# public docs() or docs_bundle targets whose Needs are referenced
# by this project's documentation
"@score_process_description//:docs_bundle",
],
bundles = [
# dicts describing bundles to mount into this project
# e.g. {"bundle": "@score_process_description//:docs_bundle", "mount_at": "process"}
],
deps = [
# additional bazel labels providing Python deps or other runfiles
],
)
source_dir(string, default:"docs") Path (relative to repository root) to your Sphinx source directory. This is the folder that contains the top-level ReST/markdown sources. Aconf.pyis optional.projectandproject_url(strings, optional) Project name and canonical project URL. They are required whensource_dirhas noconf.py; in that casedocs()runs Sphinx in configuration-free mode and supplies these values, the Docs-as-Code baseline extensions and version, and arequired_in_identry derived from the Bazel module name. The first underscore-separated prefix is removed (for example,score_docs_as_codebecomesdocs_as_code). If aconf.pyexists, it remains authoritative for the root Sphinx build; these macro arguments are not used to override it. They are still published when supplied so an associated child bundle can receive explicit root metadata; supply them explicitly when associated bundles need that metadata. Values are not read back fromconf.pyor inherited from it. The root bundle nevertheless publishes the module-derivedrequired_in_idthrough the root docs configuration; associated child bundles inherit it and cannot override it locally.data(list of bazel labels) Supporting files for this project’s root:docs_bundle. The files are available to the documentation targets and travel when another project mounts this project’s public bundle. Put files belonging to a mounted child in that child’sdocs_bundle(data = [...])instead.A Needs inventory target (
:needs_json,:needs_json_file) indatais rejected and fails the build. Put Needs inventory targets inexternal_needsinstead.Note
To reference Needs owned by another documentation project, use
external_needswith that project’s public:docstarget or a public:docs_bundletarget. The:needs_jsonlabel remains accepted as a deprecated form without a warning for now.:needs_json_fileis also supported for directly naming an inventory file.bundles(list of placement dicts) Documentation bundles to overlay into this project’s documentation tree, each with its placement (mount_at, optionalattach_toandtoctree_index). See Use Documentation Bundles for the full reference. An entry can also name a plain file target. Its files then become assets; see Asset entries in bundles.deps(list of bazel labels) Additional Bazel dependencies to add to the Python binaries and the virtual environment target. Use this to add project-specific Python modules.If you don’t provide the necessary Sphinx packages, this function adds its own (but checks for conflicts).
code_targets(list of Bazel labels) Implementation targets or filegroups to scan for traceability tags (req-Id:annotations). Implementation targetsrcs,hdrs, andtextual_hdrsare collected through theirdepsrecursively; filegroups expand to their files. Each documentation bundle produces one cache for its declared targets; Bazel reuses that cache while its inputs are unchanged. The generated JSON is supplied tolive_previewjust like a normal documentation build.primary_need_id(string, optional) ID of the Sphinx-Needs item that represents the primary subject of this documentation project. A project may contain many other Needs; when this value is set, bundle-level target metadata is written only to this Need.external_needs(list of bazel labels) Public:docsor:docs_bundletargets from other modules or repositories.docs()maps each label to that target’s locally owned Needs inventory, so imported descendants are not treated as part of the target’s own inventory. The referenced Needs can then be used by this project’s documentation and itsneeds_jsonoutput. The:needs_jsonlabel remains accepted as a deprecated form without a warning for now;:needs_json_fileremains supported as a direct inventory-file target.metamodel(bazel label, optional) Path to a custommetamodel.yamlfile. When set, thescore_metamodelextension loads this file instead of the default metamodel. The label is automatically added to thedataandtoolsof every generated target so the file is available in the Bazel sandbox at build time.Example:
docs( source_dir = "docs", metamodel = "//:my_metamodel.yaml", )
The custom
metamodel.yamlmust follow the same schema as the default one (see score_metamodel). You may use@score_docs_as_code//src/extensions/score_metamodel:metamodel_yamlfor extension processing. Whenmetamodelis omitted the default metamodel is used unchanged.
Coverage input#
The CLI used by bazel run :docs and :live_preview
reads coverage-report/coverage_report.dat relative to the workspace root.
The documentation workflow can download this artifact with its optional
coverage-report-artifact input. The CLI calculates coverage per primary Need
from the bundles’ code-file lists and writes
<build-output>/coverage_summary.json.
This generated input participates in the local documentation cache check and
remains available when stale output is removed.
When the report is absent, selected files have missing coverage. Measurements
are raw, without applied justifications. A compact display-only attribute is
added to the matching primary Need’s metadata area, for example
coverage: lines 67% · branches 100% · files 2/6. needs_json,
docs_check and docs_link_check retain their existing inputs.
Bazel macro: docs_bundle#
docs_bundle (also from docs.bzl) declares a mountable documentation
bundle: a chunk of RST/Markdown content that can be overlaid into a host
documentation project. A bundle carries only content — it has no placement of its
own. Where it appears is decided by whoever mounts it (a composing
docs_bundle or the docs(bundles=[…]) call
site).
load("//:docs.bzl", "docs_bundle")
docs_bundle(
name = "docs_dir",
source_dir = "docs",
entry_doc = "index",
bundles = [],
visibility = ["//visibility:public"],
)
Signature: docs_bundle(name, source_dir = None, srcs = [], data = [], entry_doc = "index", bundles = [], code_targets = [], primary_need_id = None, root_docs = None, visibility = None).
source_dir(string, optional) Directory holding the bundle’s own doc sources. It is globbed the same way asdocs()(RST, Markdown, images, and the other doc file kinds). Thesource_diritself is the mount root, so the files mount relative to it (soconcept/index.rstwithsource_dir = "concept"becomesindex.rst). Standalone bundle-local Needs exports use Sphinx’s configuration-free mode with a self-contained set of baseline overrides. A root bundle created bydocs()uses the project’sconf.pywhen one exists, so its local export remains compatible while that legacy path is phased out. The bundle exposes those files as a Bazel depset (via theDocsBundleInfoprovider) and records thesource_dirpath; sphinx-mounts walks that original directory directly — no copy is made. Leave it unset for a bundle whose sources are supplied explicitly or for an aggregator that only composes otherbundles.srcs(list of bazel labels, optional) Explicit documentation source files, including generated outputs from a build action. Use this for a source-less bundle whose documentation is generated. All files must share one parent directory. It cannot be combined withsource_dir.data(list of bazel labels, optional) Supporting or runtime files owned by this bundle. These files are part of the bundle payload and are available at the bundle’s eventual mount path, but are not processed as the bundle’s documentation sources. Usedocs(data = [...])for supporting files in the root bundle and this attribute for child bundles.entry_doc(string, optional) Bundle-relative docname used as the canonical navigation entry. It defaults toindex. Every mount attaches this entry to the parentindextoctree by default; a placement’sattach_tomay override that host document, and itstoctree_indexselects which toctree of that document receives the entry.bundles(list of composition dicts, optional) Nested bundles to compose into this one, so a bundle can aggregate other bundles transitively. Each entry is a dict:bundle— label of anotherdocs_bundletarget, or of a plain file target (an asset entry, see Asset entries in bundles).mount_at— the docname prefix at which the child bundle appears inside this bundle.attach_to(optional) — a docname (relative to this bundle) whose toctree receives the child’s bundle-defined entry document. When omitted, the parentindexdocument receives it.toctree_index(optional) — the 0-based index of the toctree inside theattach_todocument that receives the entry. It defaults to0(the first toctree).
A child’s
mount_at/attach_toprefix-stack with the placement this bundle later receives, so composition is fully transitive. The same underlying bundle resolving to two different finalmount_atvalues is a hard build error. See Use Documentation Bundles for a worked example and Documentation bundles and mounts for the composition and transitivity semantics.needs_local(internal target) A source-bearing bundle creates<name>.__internal__.needs_localwith theneeds.jsonfile for Needs declared by its own sources. The standalone build is intentionally self-contained in this version: references to Needs defined outside the bundle remain unresolved and fail strict builds. Cross-bundle imports and merged exports are planned for a later change. Data-only bundles do not create a Needs target.
Note
A bundle is placement-free: its mount_at, attach_to, and
toctree_index are assigned by the mounter, while its entry_doc belongs to
the bundle. This lets the
same bundle be mounted at different locations by different consumers without
changing its canonical entry page.
code_targets(list of Bazel labels, optional)Implementation targets or filegroups to scan for requirement tags. Implementation target
srcs,hdrs, andtextual_hdrsare collected recursively from theirdeps; filegroups expand to their files. The bundle owns one cached scan result; Bazel only regenerates it when its collected source inputs change.
primary_need_id(string, optional)ID of the local Sphinx-Needs item representing the primary subject of this bundle. The bundle may contain many additional Needs; only this explicitly selected Need receives bundle-level target metadata. The ID is independent of the Bazel bundle target name.
root_docs(bazel label, optional) Root documentation target whose structured project configuration and metamodel are used by this bundle’s standalone Needs export. For a root documentation target declared in the same repository’s root package, use//:docs. The reference is resolved to an internal configuration target, so the bundle does not depend on the root bundle’s composed content. It is deliberately opt-in: standalone bundles otherwise keep their own local configuration. The generateddocs.__internal__.configtarget is an implementation detail and is not part of the public target surface; it publishes a structured provider for the bundle rule to consume. This includes the effectiverequired_in_idnamespace; a bundle associated with root docs cannot override it locally. Aconf.pyis never inherited.The root project’s
project_urlis resolved for the declaring bundle by appending its workspace-relative Bazel package path.
Asset entries in bundles#
An entry in bundles of docs() or docs_bundle() can name a plain file
target (filegroup, genrule, a Doxygen rule, …) instead of a
docs_bundle. This is an asset entry. All files of the target are
assets: files that the pages use or link to, but that never become pages.
No other macro or attribute is necessary.
filegroup(
name = "api_files",
srcs = ["assets/logo.svg", "src/example.cpp"],
)
docs_bundle(
name = "comp_docs",
source_dir = "docs",
bundles = [{"bundle": ":api_files", "mount_at": "api"}],
)
The rules for assets:
- Mounted path
Each asset gets the mounted path
<mount_at>/<path below the package folder of the target>. In the example,assets/logo.svggets the mounted pathapi/assets/logo.svg. A folder output (for example Doxygen HTML) keeps its files below its mounted path. Files of a subpackage are accepted.- Never pages
Sphinx never makes a page of an
.rstor.mdasset. A page can still use it withinclude.- How a page finds an asset
A page uses the mounted path, relative to its own mounted folder. In
docs/index.rstof the example:.. image:: api/assets/logo.svg. A path that starts with/starts at the root of the site. A file next to the page file has priority; when no such file exists, the asset with this mounted path is used.image,literalinclude,includeandumlfind assets this way.- Output
The HTML builds (
:docs,:live_preview) copy each asset to its mounted path in the site, so a plain link to an asset works, for example a link toapi/html/index.html. A file that did not change is not copied again. The Needs and check builds (:needs_json,:docs_check) read the assets, but do not copy them.:live_previewbuilds again when an asset changes.- Mounted in another bundle
Assets go with their bundle. When a parent mounts the bundle, the parent’s
mount_atgoes in front of each mounted path: a project that mountscomp_docsatcomponentgetscomponent/api/assets/logo.svg. For other Bazel modules the rule is the same as for bundles: the assets of a module go with it, but assets that it mounted from a third module do not. The consumer mounts those itself.- Errors
The build fails when:
two assets get the same mounted path. HTML builds also stop when a file in a folder asset gets the same path as another asset;
an asset would overwrite a file that the HTML build writes (HTML builds only): a page, a file in
_static/,_images/,_sources/,_downloads/or_modules/, or a root file such asgenindex.html,search.html,py-modindex.htmlor a report of the docs-as-code extensions (metrics.json,compatibility-findings.html, …);the path of an asset goes out of the site: its
mount_atcontains..or starts with/(HTML builds only);the path of an asset is not in normal form, for example
x/./a.svgorx//a.svg(HTML builds only);an
.rstor.mdasset is below thesource_dirof the bundle that mounts it. Sphinx would make a page of it; a page can use the file without an asset entry;an asset entry sets
attach_toortoctree_index: an asset is not a page, so no toctree shows it;a file is not below the package folder of its target. Put the target of the file’s own package in
bundles;the target is a runnable target, for example a
py_binary. This is almost always a wrong label. A checked-in script is a source file and is accepted.
Note
csv-table with :file: and raw with :file: do not find
assets: docutils reads these files directly, not through Sphinx. Use
literalinclude or include instead.