Learning center

CI built the generated code that was committed, not what the source generates

When generated files are checked in, CI compiles and tests the committed copy. If someone edits the source of the generation and does not run the generator, CI tests old output and passes. Add a step that regenerates and fails on any change. Check untracked files as well as modified ones, because git diff --exit-code does not see new files.

Where the gap comes from#

A schema, an OpenAPI document, a protobuf file or an error catalogue goes into a generator, and the generator writes code that the build compiles. Committing that output keeps builds fast and reviewable. It also means the build never reads the source of the generation. A change to errors.json alone changes no compiled file, so every check that compiles and tests code runs against the previous output.

Fixture: two missed regenerations#

git 2.43.0 and Python 3.12.3. gen.py writes one TypeScript module per error group in errors.json. Chen adds an error code to the payment group and a new refund group, and commits without running the generator:

$ cat src/gen/payment.ts
// generated by gen.py from errors.json; do not edit
export const CARD_DECLINED = 402;
$ python3 gen.py && git diff --exit-code --stat
 src/gen/payment.ts | 1 +
 1 file changed, 1 insertion(+)
exit 1
$ git status --porcelain
 M src/gen/payment.ts
?? src/gen/refund.ts

The check caught the modified file. In a second scenario, Chen's commit only adds the refund group, so the generator writes one new file and changes none:

$ python3 gen.py && git diff --exit-code
exit 0
$ git status --porcelain
?? src/gen/refund.ts
$ test -z "$(git status --porcelain)"; echo "exit $?"
exit 1

git diff --exit-code passed. Without arguments, git diff shows "changes you made relative to the index", and an untracked file has no index entry. git diff git status --porcelain lists it with ??, in a format that is stable "across Git versions and regardless of user configuration". git status

The check#

- run: |
    python3 gen.py
    git status --porcelain
    test -z "$(git status --porcelain)"

Run the generator with the version the repository pins, in the same job as the build, after dependencies are installed. Printing the status before testing it puts the changed paths in the log, so the author knows what to regenerate.

When the check fails for another reason#

FailureCauseFix
Modified or new generated filesThe source changed without regenerationRun the generator and commit
Every file differs on every runA timestamp, absolute path or unordered map in the outputMake the generator deterministic
Differs only in CIAnother generator version or platform line endingsPin the generator; set .gitattributes for the output
A generated file was edited by handSomeone patched the outputMove the change into the source or the generator

The second row is the one to fix first, because a check that always fails is soon skipped. Running the generator twice locally and comparing the outputs shows whether it is deterministic.

A green check from this step covers the generator's output for the files it writes. It does not cover output that a different generator or build step produces, so each generator needs its own step. For who should review generated output in a pull request, see owners for generated code. For build output restored from another commit, see a restore key that brought back old output.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading