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.
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 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:
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:
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:
With a root Space
A root that has its own commands mixes MetaBakebook into its Space:
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.
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). Last modified on December 1, 2026