Skip to content
Latchkey

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

Terminal
# 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.jsonnet

Options

FlagWhat it does
-i, --in-placeRewrite files in place
--testExit non-zero if reformatting is needed
-n, --indent <n>Number of spaces for indentation
--string-style d|s|lPreferred string quote style
--comment-style h|s|lPreferred 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).

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →