Bundle examples#

This page is a catalog of focused bundle patterns. Start with Use Documentation Bundles for the basic workflow; use these examples when documentation is split across bundles, generated during the build, published by another Bazel module, or when a bundle ships pictures, code or generated HTML. Each example shows the bundle declaration, its placement in the consuming project, and the resulting location in the documentation output.

Nest bundles#

Use nested bundles when one bundle contains another. The child bundle chooses its entry page, the parent chooses the child’s relative location, and the consuming project chooses where the assembled bundle appears.

The tested parent bundle composes the child like this:

docs_bundle(
    name = "parent",
    source_dir = "parent",
    bundles = [{
        "bundle": ":child",
        "mount_at": "child",
    }],
    visibility = ["//visibility:public"],
)

See the complete nested-bundles fixture on GitHub.

If the parent is mounted at guides/example, its page is rendered at guides/example/index.html and the child landing page at guides/example/child/landing.html. The consuming project can change guides/example without changing either bundle.

Mount generated bundle content#

Use explicit srcs when a build action produces the documentation rather than a source-tree .rst file. The source-less bundle has no source_dir; its generated files are the bundle’s complete source payload. You can generate and mount a page like this:

genrule(
    name = "generated_page",
    srcs = [],
    outs = ["generated/index.rst"],
    cmd = """echo 'Generated Data Page
===================' > $@""",
)

# Source bundle: the genrule output lives in ``bazel-out/``, not the tree.
docs_bundle(
    name = "data_bundle",
    srcs = [":generated_page"],
    entry_doc = "index",
    visibility = ["//visibility:public"],
)

docs(
    source_dir = "docs",
    bundles = [{
        "bundle": ":data_bundle",
        "mount_at": "data_test",
        "attach_to": "index",
    }],
)

See the complete generated-data fixture on GitHub.

The generated page is rendered at data_test/index.html and is added to the consuming project’s index page’s toctree. A generated source bundle is mounted and navigated in exactly the same way as a bundle with workspace sources.

Ship pictures, code and generated HTML with a bundle#

Put the files in a plain file target and add the target to bundles. Its files become assets: the pages use them or link to them, but they never become pages. The tested child bundle ships checked-in files, generated files and a Doxygen folder like this:

filegroup(
    name = "child_asset_files",
    srcs = [
        "child_assets/diagram.svg",
        "child_assets/example_api.cpp",
        "child_assets/snippet.rst",
        ":doxygen_html",  # a rule whose output is a folder
        ":manual_pdf",  # a genrule
    ],
)

docs_bundle(
    name = "child",
    source_dir = "child",
    entry_doc = "landing",
    bundles = [{
        "bundle": ":child_asset_files",
        "mount_at": "api",
    }],
)

Each file gets the mounted path api/<path below the package folder>. The pages of the child use these paths, relative to their own folder:

.. image:: api/child_assets/diagram.svg

.. literalinclude:: api/child_assets/example_api.cpp

.. include:: api/child_assets/snippet.rst

`API reference <api/doxygen_html/index.html>`_

See the complete nested-bundles fixture on GitHub.

The parent mounts the child at child, and the project mounts the parent at concepts/example_bundle. The files then have these paths:

File in the package

Mounted path in the child

Path in the site

child_assets/diagram.svg

api/child_assets/diagram.svg

concepts/example_bundle/child/api/child_assets/diagram.svg

child_assets/snippet.rst

api/child_assets/snippet.rst (not a page)

concepts/example_bundle/child/api/child_assets/snippet.rst

manual.pdf (generated)

api/manual.pdf

concepts/example_bundle/child/api/manual.pdf

doxygen_html/ (folder)

api/doxygen_html/index.html, …

concepts/example_bundle/child/api/doxygen_html/index.html, …

The pages of the child do not change when a project mounts the child at another place.

Use an asset entry for files that a page uses or links to. Use data for files that a tool or an extension reads during the build. See Asset entries in bundles for all rules and errors.

Mount documentation from another module#

Every project using docs() exposes its own documentation as a :docs_bundle target. The external-bundle test fixture mounts that target like this:

docs(
    source_dir = "host_docs",
    bundles = [{
        "bundle": "@score_process_description//:docs_bundle",
        "mount_at": "process",
    }],
)

See the complete external-bundle fixture on GitHub.

The external bundle’s entry page is rendered at process/index.html and is added to the consuming project’s index by attach_to. Mounted sources also bring their Need directives with them, so do not import the same module’s needs_json separately.

See also

Use Documentation Bundles for the full mount reference and Bazel macro: docs for the docs and docs_bundle attributes.