ktlint: Check and Format Kotlin in CI
ktlint checks Kotlin files against a style ruleset and exits 1 on violations; ktlint -F formats them in place.
ktlint is the zero-config Kotlin linter and formatter. It enforces the official style and is driven mostly by .editorconfig.
What it does
ktlint takes file globs (default all .kt and .kts under the working directory), checks them against its rules, and exits 1 on any violation. ktlint -F (--format) rewrites files to fix the auto-fixable ones. Rule configuration comes mainly from .editorconfig.
Common usage
# check (Gradle plugin or CLI)
ktlint "src/**/*.kt"
# auto-format
ktlint -F "src/**/*.kt"
# emit a CI-friendly report
ktlint --reporter=checkstyle,output=ktlint.xml "src/**/*.kt"Flags
| Flag | What it does |
|---|---|
| "<glob>" | Files to check (quote so ktlint expands it) |
| -F, --format | Autocorrect violations in place |
| --reporter=<id> | Report format (plain, json, checkstyle, sarif) |
| --reporter=<id>,output=<f> | Write that report to a file |
| --editorconfig <file> | Use a specific .editorconfig |
| --disabled_rules=<list> | Disable named rules (older flag form) |
In CI
Run ktlint through its Gradle plugin so the version is pinned with the build. Quote globs so ktlint, not the shell, expands them. Use a check task in the gate and a separate format task that commits, rather than formatting inside the gate.
Common errors in CI
"Needless blank line(s)" and "Unexpected indentation" are common first failures; run ktlint -F to fix most. "Property 'X' not found" or rule-id errors after an upgrade mean a renamed rule in .editorconfig under ktlint_ keys. A glob that matches nothing exits 0 and silently lints zero files.