Enforcing Kotlin code style with EditorConfig and ktlint
Align IntelliJ formatting with ktlint, run the Kotlin Gradle lint tasks, and gate GitHub pull requests without mixing formatting changes into features.
I wanted code review time to go to behavior and design. Putting the Kotlin formatting rules in
.editorconfigand enforcing them through ktlint made routine whitespace and import changes consistent across the team.
The examples use Kotlin 1.9 or later, Gradle Kotlin DSL, IntelliJ IDEA, and the
org.jlleitschuh.gradle.ktlint12.1.x plugin with ktlint 1.x.
Give the editor and build the same rules
A style guide in a README still depends on every contributor remembering to reformat. I use EditorConfig to define the settings the IDE follows during editing or reformatting, and ktlint to check tracked source files during the build. ktlint also reads .editorconfig, which removes the need to maintain equivalent settings in two places.
| Tool | Role | When it runs | Result |
|---|---|---|---|
| EditorConfig | Defines settings such as indentation and line endings | While editing or reformatting | Guides the editor |
| ktlint | Applies and verifies Kotlin style rules | On demand in Gradle and in CI | Fails the check on violations |
A consistent file stops an IntelliJ reformat from unexpectedly changing code that already passes the linter. The build check then catches changes made in other editors or submitted without local formatting.
Configure EditorConfig at the repository root
IntelliJ IDEA reads .editorconfig at the root. Set root = true so a developer’s parent directories cannot contribute unrelated settings. The following configuration comes from a Spring Boot and Kotlin backend.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# /.editorconfig
root = true
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
[*.{kt,kts}]
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ktlint_code_style = intellij_idea
ktlint_function_signature_body_expression_wrapping = multiline
ktlint_standard_function-expression-body = disabled
ktlint_standard_indent = disabled
ktlint_standard_string-template-indent = disabled
# Disable wildcard imports
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_packages_to_use_import_on_demand = unset
charset,end_of_line, andinsert_final_newlinekeep diffs consistent across operating systems.ktlint_code_style = intellij_ideaaligns the linter’s wrapping decisions with IntelliJ’s Reformat Code action, which isCmd + Option + Lon macOS. Some wrapping differs underktlint_official.- The high
ij_kotlin_name_count_to_use_star_importthreshold and the unset list of packages for on-demand imports prevent IntelliJ from collapsing imports into.*during optimization.
If CI reformats a file differently from the IDE, start with the active code style and look for nested .editorconfig files. Comparing a single affected file in both tools makes it easier to see which rule diverged.
Check both .kt and .kts files when changing a pattern. The project can have consistent application code and still fail its script check if the rule only applies to one extension. I also check the Gradle plugin’s pinned version and any explicit ktlint version setting before changing a style property. The IDE and Gradle read the same file, but a setting only takes effect in tools that recognize it.
Use the Gradle check and format tasks
Apply org.jlleitschuh.gradle.ktlint in build.gradle.kts so every contributor uses the same plugin and linter configuration. The following pins the plugin used by the project.
1
2
3
4
5
6
7
8
9
10
11
12
13
// /build.gradle.kts
plugins {
kotlin("jvm") version "1.9.25"
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
}
ktlint {
verbose.set(true)
outputToConsole.set(true)
filter {
exclude("**/generated/**")
}
}
The two day-to-day tasks have different effects. ktlintCheck reports violations. ktlintFormat rewrites the ones the linter can fix automatically.
1
2
3
4
5
# Check for style violations without editing files
./gradlew ktlintCheck
# Apply all automatic fixes
./gradlew ktlintFormat
The Gradle plugin connects ktlintCheck to check, so an ordinary ./gradlew check or ./gradlew build also verifies style. I use ktlintFormat before reviewing the diff, then use ktlintCheck to find anything that still needs manual attention. Since formatting changes source, review them before committing, especially in an older repository with inconsistent style.
Add a pull request check in GitHub Actions
Running ktlintCheck early in the pull request workflow gives immediate feedback. There is no reason to spend CI time on tests after a deterministic formatting failure. Run the test task once the style check succeeds.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# /.github/workflows/ci-cd.yaml
name: CI Pipeline
on:
push:
branches: ['**']
pull_request:
branches: [main]
jobs:
test:
name: Lint & Test
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
- name: Check Lint
run: ./gradlew ktlintCheck
- name: Run Tests
run: ./gradlew test
The task output includes the source file, line and column, and rule ID. Use the location to fix the code and the ID to understand the corresponding rule.
1
2
3
> Task :ktlintMainSourceSetCheck FAILED
/src/main/kotlin/org/project/domain/OrderService.kt:3:1: Wildcard import (standard:no-wildcard-imports)
/src/main/kotlin/org/project/domain/OrderService.kt:18:25: Missing spacing around "=" (standard:spacing-around-operators)
For a new repository, I would require the CI check from the first pull request. For an existing repository, run the check once to see the size of the cleanup. If the initial formatting touches many files, land it separately. The first functional review after that should show behavior changes without a second formatting migration in the diff.
Treat the pull request check as verification. CI can show a violation with the exact location, while the contributor can run the matching format task locally and review the patch. That keeps every source change visible in Git.
Roll out formatting without burying feature diffs
- Separate the initial formatting commit. A format-only commit is easier to review and easier to add to
.git-blame-ignore-revs. This keeps working history easier to follow. - Disable only the rule you mean to change. ktlint 1.x dropped
disabled_rules. Use a property such asktlint_standard_no-wildcard-imports = disabledwhen the team has agreed on that particular exception. Record the reason in a comment. - Keep pre-commit checks optional when startup time hurts.
./gradlew addKtlintCheckGitPreCommitHookinstalls a local check, but launching Gradle on each commit adds latency. The GitHub Actions check remains the shared enforcement point. - Check agreement across tools. Reformat a file in IntelliJ, run
ktlintCheck, and inspect the resulting diff. Use files with imports and multiline expressions because those expose common style differences.