jsonnetfmt: Format Jsonnet Files in CI
jsonnetfmt --test config.jsonnet exits non-zero if the file is not already in canonical Jsonnet format.
jsonnetfmt keeps Jsonnet config consistently formatted. In CI you use --test so unformatted files fail the build instead of being silently rewritten.
What it does
jsonnetfmt reformats Jsonnet source: normalizing indentation, quotes, and comment style. By default it prints the formatted output; -i rewrites in place; --test does not write and instead exits non-zero if the file would change, which is the CI check.
Common usage
# format in place
jsonnetfmt -i config.jsonnet
# CI check: fail if any file is unformatted
jsonnetfmt --test config.jsonnet lib/*.libsonnet
# print the formatted result to stdout
jsonnetfmt config.jsonnetOptions
| Flag | What it does |
|---|---|
| -i, --in-place | Rewrite files in place |
| --test | Exit non-zero if reformatting is needed |
| -n, --indent <n> | Number of spaces for indentation |
| --string-style d|s|l | Preferred string quote style |
| --comment-style h|s|l | Preferred comment style |
In CI
Run jsonnetfmt --test $(find . -name "*.jsonnet" -o -name "*.libsonnet"): --test makes an unformatted file exit non-zero so the runner fails the job. Developers fix it with jsonnetfmt -i. Pair it with jsonnet evaluation so both style and correctness are gated.
Common errors in CI
With --test an unformatted file exits non-zero with no output; run jsonnetfmt -i to fix. STATIC ERROR: expected token OPERATOR but got ... means the file will not parse and so cannot be formatted; fix the syntax. command not found: jsonnetfmt means the Jsonnet tools are not installed (some distributions ship only jsonnet).