> ## Documentation Index
> Fetch the complete documentation index at: https://bakefile.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> bakefile is a Python task runner with two CLIs: bake runs tasks from bakefile.py, bakefile manages the project. It is not a service or platform.
> Tasks are methods on Bakebook classes in bakefile.py, decorated with @command. Subprocesses run through self.ctx.run().
> Examples are backed by tests; copy them verbatim.

# MetaBakebook

> MetaBakebook runs one bake command in each SubBakebook of a monorepo, each in its own bake process.

A monorepo often has one `bakefile.py` per project below a root `bakefile.py`. `MetaBakebook` lets the root declare those SubBakebooks and run any command across them with `bake foreach`. Each SubBakebook runs in its own `bake` process with its own Bakebook (bake never merges bakefiles), and the root never imports them.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from bakelib import MetaBakebook, SubBakebook

bakebook = MetaBakebook(
    sub_bakebooks=[
        "services/*",
        SubBakebook("infra", env={"ENV": "dev"}),
    ]
)
```

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
bake foreach lint                       # bake lint in every SubBakebook, in parallel
bake foreach test -k smoke              # everything after the command goes to the SubBakebook
bake foreach -c 1 setup-dev             # one SubBakebook at a time, in declared order
bake foreach --include 'services/*' --exclude '*/legacy' update
bake foreach --keep-going lint          # start the remaining SubBakebooks after a failure
```

## SubBakebooks

`sub_bakebooks` lists paths relative to the root `bakefile.py`, globs allowed:

* A folder is a SubBakebook when it holds a `bakefile.py`; a glob skips matched folders without one.
* A path to a `.py` file runs that file (`bake -C <folder> -f <file>`).
* `SubBakebook(path, env={...})` sets environment variables for that SubBakebook's process (every match, for a glob). The SubBakebook's own [Env Bakebooks](https://bakefile.wisl.dev/bakelib/environments.md) resolve them, so `SubBakebook("infra", env={"ENV": "dev"})` runs the dev Bakebook of `infra/bakefile.py`.
* An entry that matches no `bakefile.py` fails the run, and a SubBakebook declared twice runs once.

A SubBakebook can be a `MetaBakebook` itself, so `bake foreach` nests.

## bake foreach

`bake foreach [flags] <command> [args]` runs `bake -C <dir> <command> [args]` for each SubBakebook. Its own flags end at the first plain word, so no `--` is needed and the SubBakebook's flags never clash with them:

* `--include <glob>`, `--exclude <glob>`: run only, or skip, SubBakebooks whose path matches; repeatable. A pattern that matches no SubBakebook fails the run.
* `-c N`, `--concurrency N`: how many SubBakebooks run at once. They run in parallel by default; `-c 1` runs them one at a time with live output.
* `--keep-going`: without it, no SubBakebook starts after one fails; those already running finish.

A SubBakebook that lacks the command fails like any other failure. The run ends with a summary and exits non-zero when any SubBakebook failed.

On a terminal, each running SubBakebook owns one line, redrawn in place: a spinner, its name, the last line of its output (its command until it writes any) and its elapsed time. A line that ends turns into its result:

```
✓ services/auth                                    0.8s
⠹ services/billing  Resolved 42 packages in 12ms   2.1s
⠸ infra             $ bake -C infra lint           0.1s
```

A failed SubBakebook prints its output at once, stdout and stderr merged in the order they were written, and the run ends in a one-line summary:

```
✓ services/auth     0.8s
✗ services/billing  exit 3  0.4s
  │ out
  │ boom
✓ infra             2.1s

2 passed, 1 failed in 2.4s
  ✗ services/billing (exit 3)
```

Piped or in CI, nothing is redrawn: only the result lines, failure output and summary are printed. With `-c 1`, each SubBakebook's output streams raw between its start and result lines. Ctrl-C stops every running SubBakebook and leaves the terminal clean.

To make a root run its SubBakebooks in order by default (Terraform layers, for example), set `foreach_concurrency` in a subclass:

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from bakelib import MetaBakebook


class LayersBakebook(MetaBakebook):
    foreach_concurrency: int | None = 1


bakebook = LayersBakebook(sub_bakebooks=["network", "cluster", "apps"])
```

## With a root Space

A root that has its own commands mixes `MetaBakebook` into its Space:

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from bakelib import MetaBakebook, PythonSpace


class RootSpace(PythonSpace, MetaBakebook): ...


bakebook = RootSpace(sub_bakebooks=["packages/*"])
```

`bake lint` lints the root project; `bake foreach lint` lints every SubBakebook.

## From your own command

`bake foreach` is a thin wrapper around the protected step `_foreach(args, *, include=None, exclude=None, concurrency=None, keep_going=False)`, so any command of a subclass can run a batch over the SubBakebooks without parsing CLI flags. It behaves exactly as `bake foreach` with the same options: it honours `--dry-run` and uses `foreach_concurrency` when `concurrency` is not given.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from bake import command
from bakelib import MetaBakebook, PythonSpace


class RootSpace(PythonSpace, MetaBakebook):
    @command(help="Update every SubBakebook except the legacy one")
    def update_services(self) -> None:
        self._foreach(["update"], exclude=["services/legacy"], keep_going=True)


bakebook = RootSpace(sub_bakebooks=["services/*"])
```

`bake update-services` tries every SubBakebook and exits non-zero when any failed, as `bake foreach --exclude services/legacy --keep-going update` would. A step override can call it too: bakefile's own `bakefile.py` updates its `examples/*` during `bake update` with `self._foreach(["update", "-ff"], keep_going=True)`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.