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#
| Failure | Cause | Fix |
|---|---|---|
| Modified or new generated files | The source changed without regeneration | Run the generator and commit |
| Every file differs on every run | A timestamp, absolute path or unordered map in the output | Make the generator deterministic |
| Differs only in CI | Another generator version or platform line endings | Pin the generator; set .gitattributes for the output |
| A generated file was edited by hand | Someone patched the output | Move 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.