Skip to main content

Codegen

Most targets keep their outputs inside heph's cache and sandbox — the source tree never changes. Codegen targets are the exception. They materialize their outputs back into the workspace, next to your hand-written code, so that tools which read the tree directly — your editor, the compiler, go list, grep — see the generated files as if you had written them yourself.

The codegen attribute on an exec target turns this on and picks how the outputs land in the tree:

BUILD
target(
name = "generated",
driver = "bash",
codegen = "copy", # or "in_place"
out = "generated.txt",
run = "echo 'generated by //fmt:generated' > $OUT",
)

The two modes​

ModeProducesTracked in git?Needs gitignore?
copyNew files the target createsNoYes
in_placeRewrites of existing sourcesYesNo
in_place_on_successAs in_place, but a failed run writes nothing backYesNo

The distinction is about ownership of the file on disk:

  • copy emits a brand-new file that did not exist in the tree before. It is a build artifact that happens to live among your sources — not a tracked source. heph records these outputs so a later glob() excludes them (they won't be picked up as inputs to other targets), and they belong in .gitignore rather than in a commit. heph keeps a per-checkout record of the files it wrote (see Which files heph wrote).

  • in_place transforms files that are already tracked and committed. The target reads a source file and writes the result back over the same path. The file stays a normal, version-controlled source — there is nothing new to ignore.

copy — generate a new file​

Use copy when the target creates output that you do not want to commit: protobuf stubs, generated API clients, embedded asset manifests — anything derived purely from other inputs.

fmt/BUILD
target(
name = "generated",
driver = "bash",
codegen = "copy",
out = "generated.txt",
run = "echo 'generated by //fmt:generated' > $OUT",
)

//fmt:generated writes fmt/generated.txt into the tree. Because it is a copy output, glob("*.txt") in the same package will skip it, and heph tool codegen gitignore will add it to the root .gitignore.

in_place — rewrite existing sources​

Use in_place for tools that modify files you keep under version control: formatters, codemods, an auto-fixing linter. The generated result replaces the checked-in file, so the change shows up as a normal diff you review and commit.

fmt/BUILD
target(
name = "fmt",
driver = "bash",
codegen = "in_place",
deps = {"src": glob("*.txt")},
out = glob("*.txt"),
# Idempotent: uppercase every tracked .txt file, writing it back in place.
run = 'for f in $SRC_SRC; do tr a-z A-Z < "$f" > "$f.up" && mv "$f.up" "$f"; done',
)
tip

Keep in_place transforms idempotent — running them twice should produce the same bytes as running them once. That keeps the output reproducible and the diff stable, and makes the next run over the rewritten tree a cache hit.

When the fixer fails​

An in_place run that exits with a code from 1 to 127 still writes back the files it fixed, and the target still fails. A fixer like eslint --fix with unfixable errors left keeps the fixes it could make, and the failure box lists them with a git checkout command to undo them. Nothing is written after a signal, an exit code of 128 or above, under --frozen, or in a --shell.

Use codegen = "in_place_on_success" for a tool that can leave a partial file behind when it fails: a failed run then writes nothing back. See Failed targets.

note

An in_place target never writes into a file a copy target owns. If an in_place output path collides with a copy target's output, heph leaves that file untouched — the copy target's content stays intact, and only that file is skipped (any other files the in_place target also writes are still updated). --frozen applies the same exemption, so a copy-owned file never fails an in_place frozen check. Point copy and in_place targets at disjoint files rather than relying on this.

Keeping .gitignore in sync​

copy outputs should never be committed, so heph can maintain the .gitignore for you. Running:

heph tool codegen gitignore

scans the workspace for every codegen = "copy" output and writes them into a managed block in the root .gitignore, pairing each pattern with a comment naming the target that emits it:

.gitignore
# BEGIN heph-generated (managed by `heph tool codegen gitignore` — do not edit)
# //fmt:generated
/fmt/generated.txt
# END heph-generated

The attribution comment always sits on its own line directly above the pattern it names — never trailing on the same line — since git only treats a # as a comment when it starts the line.

The command is:

  • Scoped. Only copy outputs are listed — in_place rewrites are tracked sources and never appear.
  • Anchored. Paths are workspace-root-relative, so a file becomes /foo/bar.go, a directory /foo/gen, and a glob /foo/gen/**/*.go. Directory patterns carry no trailing slash, since a copy directory output lands in the tree as a symlink — a plain /foo/gen/ pattern would not match it.
  • Idempotent & non-destructive. Only the marked block is rewritten; anything you put outside the markers is preserved verbatim. Output is sorted and deduplicated, so re-running it produces a stable, diffable result.

Run it after adding or removing a copy target. Because the output is deterministic, it also works well as a CI check:

heph tool codegen gitignore --check

--check writes nothing and exits 1 when the managed block is stale. Pass a matcher to limit the update to targets under it.

warning

heph tool gen-gitignore was removed, with no alias — it now exits 2 and names the new command. Update scripts and CI steps. A block written by an older heph (its BEGIN line names gen-gitignore) counts as stale: run heph tool codegen gitignore once and commit.

Which files heph wrote​

Each time a copy target writes a file into the tree, heph records the path and its content hash in a per-checkout index at <home>/codegen/index.db (.heph/ by default). Globs use it, together with the managed .gitignore block, to skip generated files.

  • The index describes one working tree; it is never shared across checkouts or machines.
  • heph tool clean and heph tool gc never touch it. Losing it changes which files globs see, not just build time.
  • It is created the first time a copy target writes something.

A file is treated as generated while git ignores it. If it stops being ignored (for example, you check it in), it becomes a regular source.

Depend on the target, not the file​

A target that names a generated file directly — file("fmt/generated.txt") — fails, naming the target that writes it:

fmt/generated.txt is generated by //fmt:generated; depend on //fmt:generated, not the file

Depend on the generator target instead; its outputs reach your target at the same path.

Files that are no longer generated​

If you remove a copy output (or its target), the file stays in the tree and stays excluded from globs until you decide what it is. heph tool codegen status lists these:

heph tool codegen status # what needs a decision
heph tool codegen keep fmt/generated.txt # it is a source file from now on
heph tool codegen discard fmt/generated.txt # delete it
heph tool codegen discard --all --dry-run # preview deleting all of them
CommandWhat it does
status [--check]Lists files that need a decision, each with the command that settles it. --check exits 1 when anything is listed.
keep <path>...Makes the files sources: drops their record and .gitignore lines, then prints the git add to run.
discard <path>... | --all [--force] [--dry-run]Deletes files that are no longer generated, only if their content is still what heph wrote. Edited files need --force.
why <path>...Explains how heph classifies a path, including which record and .gitignore line apply.
gitignore [--check]Rewrites the managed .gitignore block.

All of them accept --json. Exit codes differ from the rest of heph: 0 OK, 1 findings (with --check) or a refused/unknown path, 2 an error.

heph validate fails on a stale managed block; problems that only affect your checkout appear as warnings pointing at codegen status.

Upgrading from an earlier version​

Earlier versions marked each written file with an extended attribute. heph now reads only the index, so after upgrading run this once in each checkout:

heph tool codegen import

It records the files the earlier version wrote. Until then, such a file is excluded only where a .gitignore line covers it.

Detecting output conflicts​

Two copy targets must never claim overlapping output paths — if they do, one target's output silently overwrites the other's. Run heph validate to catch these conflicts across the whole workspace:

heph validate

Overlap means more than identical paths. heph also flags containment: if one target claims the directory /gen and another claims the file /gen/a.go, they overlap because the directory output encompasses the file. A file and a same-named directory also conflict.

Only conflicts between different targets are reported — a single target that declares both a directory output and a file inside it is valid.

Run heph validate in CI alongside heph tool codegen gitignore --check to ensure no two targets compete for the same part of the source tree.

Reached through a dependency​

A codegen target updates the tree whenever it is part of the run — not only when you name it. If //app:bin depends on //fmt:generated, running //app:bin writes fmt/generated.txt too, and --frozen on a run that reaches a codegen target verifies it the same way. The target is written once per run, however many targets depend on it.

Verifying the tree in CI: --frozen​

Running a codegen target normally writes its outputs into the tree — copy drops in the generated file, in_place rewrites the source. In CI you usually want the opposite: assert that the committed tree already matches what codegen would produce, without touching anything.

That is what --frozen does:

heph run //fmt:fmt --frozen

In frozen mode heph computes the generated output but writes nothing. It compares the result against what is on disk and, if they differ, exits non-zero with a unified diff of every offending file:

$ heph run //fmt:fmt --frozen # clean tree → passes
$ echo 'hello world' > fmt/greeting.txt
$ heph run //fmt:fmt --frozen # dirty tree → fails
× target failed: //fmt:fmt
╰─▶ generated output differs from tree
╭─[diff]
│ --- a/fmt/greeting.txt
│ +++ b/fmt/greeting.txt
│ @@ -1 +1 @@
│ -hello world
│ +HELLO WORLD
╰────

It works for both modes: a copy target fails if its generated file is missing or stale, and an in_place target fails if a source file isn't already in its formatted/transformed form. Wire heph run <codegen-target> --frozen into CI to guarantee that whoever forgot to run codegen locally gets a red build with an exact diff, instead of a drifting tree.

When to use which​

You want to…Mode
Generate code from a schema (proto, OpenAPI, SQL)copy
Produce a derived file you don't commitcopy
Format source files (gofmt-style)in_place
Run a codemod or auto-fixing linter over checked-in codein_place
Rewrite sources with a tool that can leave a partial file on failurein_place_on_success

The quick rule: if the file is born from the build, use copy and let heph tool codegen gitignore keep it out of git. If the file is yours and the build edits it, use in_place and commit the result.