Skip to content

ktlint

ktlint documentation

ktlint - GitHub

Configuration in Mega-Linter

Variable Description Default value
KOTLIN_KTLINT_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
KOTLIN_KTLINT_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src|lib)
Include every file
KOTLIN_KTLINT_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test|examples)
Exclude no file
KOTLIN_KTLINT_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".kt", ".kts"]
KOTLIN_KTLINT_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
KOTLIN_KTLINT_DISABLE_ERRORS Run linter but consider errors as warnings false
KOTLIN_KTLINT_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0

IDE Integration

Use ktlint in your favorite IDE to catch errors before Mega-Linter !

IDE Extension Name Install
Emacs flycheck-kotlin Visit Web Site
vim ale Visit Web Site

Mega-Linter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default Mega-Linter Flavor 84 Docker Image Size (tag) Docker Pulls
dart Optimized for DART based projects 38 Docker Image Size (tag) Docker Pulls
documentation Mega-Linter for documentation projects 37 Docker Image Size (tag) Docker Pulls
dotnet Optimized for C, C++, C# or VB based projects 43 Docker Image Size (tag) Docker Pulls
go Optimized for GO based projects 39 Docker Image Size (tag) Docker Pulls
java Optimized for JAVA based projects 38 Docker Image Size (tag) Docker Pulls
javascript Optimized for JAVASCRIPT or TYPESCRIPT based projects 46 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 41 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 44 Docker Image Size (tag) Docker Pulls
ruby Optimized for RUBY based projects 38 Docker Image Size (tag) Docker Pulls
rust Optimized for RUST based projects 38 Docker Image Size (tag) Docker Pulls
salesforce Optimized for Salesforce based projects 38 Docker Image Size (tag) Docker Pulls
scala Optimized for SCALA based projects 38 Docker Image Size (tag) Docker Pulls
swift Optimized for SWIFT based projects 38 Docker Image Size (tag) Docker Pulls
terraform Optimized for TERRAFORM based projects 40 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .kt, .kts

Example calls

ktlint myfile.kt
ktlint --format myfile.kt

Help content

An anti-bikeshedding Kotlin linter with built-in formatter
(https://github.com/pinterest/ktlint).

Usage:
  ktlint <flags> [patterns]
  java -jar ktlint <flags> [patterns]

Examples:
  # check the style of all Kotlin files inside the current dir (recursively)
  # (hidden folders will be skipped)
  ktlint

  # check only certain locations (prepend ! to negate the pattern,
  # Ktlint uses .gitignore pattern style syntax)
  ktlint "src/**/*.kt" "!src/**/*Test.kt"

  # auto-correct style violations
  ktlint -F "src/**/*.kt"

  # custom reporter
  ktlint --reporter=plain?group_by_file
  # multiple reporters can be specified like this
  ktlint --reporter=plain \
    --reporter=checkstyle,output=ktlint-checkstyle-report.xml
  # 3rd-party reporter
  ktlint --reporter=csv,artifact=com.github.user:repo:master-SNAPSHOT

Flags:
  -a, --android              Turn on Android Kotlin Style Guide compatibility
      --color                Make output colorful
      --color-name=<colorName>
                             Customize the output color
      --debug                Turn on debug output
      --disabled_rules=<disabledRules>
                             Comma-separated list of rules to globally disable. To
                               disable standard ktlint rule-set use
                               --disabled_rules=standard
  -F, --format               Fix any deviations from the code style
      --limit=<limit>        Maximum number of errors to show (default: show all)
      --relative             Print files relative to the working directory (e.g.
                               dir/file.kt instead of /home/user/project/dir/file.kt)
      --reporter=<reporters> A reporter to use (built-in: plain (default), plain?
                               group_by_file, json, checkstyle, html). To use a
                               third-party reporter specify a path to a JAR file on
                               the filesystem.
  -R, --ruleset=<rulesets>   A path to a JAR file containing additional ruleset(s)
      --stdin                Read file from stdin
  -v, --verbose              Show error codes
      --editorconfig=<editorConfigPath>
                             Path to .editorconfig
      --experimental         Enabled experimental rules (ktlint-ruleset-experimental)
      --baseline=<baseline>  Defines a baseline file to check against
  -h, --help                 Show this help message and exit.
  -V, --version              Print version information and exit.
Commands:
  installGitPreCommitHook, --install-git-pre-commit-hook  Install git hook to
                                                            automatically check
                                                            files for style
                                                            violations on commit
  installGitPrePushHook, --install-git-pre-push-hook      Install git hook to
                                                            automatically check
                                                            files for style
                                                            violations before
                                                            push
  printAST, --print-ast                                   Print AST (useful
                                                            when
                                                            writing/debugging
                                                            rules)
  applyToIDEA, --apply-to-idea                            Update Intellij IDEA
                                                            Kotlin codestyle
                                                            settings (global)
  applyToIDEAProject, --apply-to-idea-project             Update Intellij IDEA
                                                            project settings
  generateEditorConfig                                    EXPERIMENTAL!!!
                                                            Generate kotlin
                                                            style section for '.
                                                            editorconfig' file.

Installation on mega-linter Docker image

  • Dockerfile commands :
RUN curl --retry 5 --retry-delay 5 -sSLO https://github.com/pinterest/ktlint/releases/download/0.40.0/ktlint && \
    chmod a+x ktlint && \
    mv "ktlint" /usr/bin/

Example success log

Results of ktlint linter (version 0.40.0)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/kotlin_ktlint/
-----------------------------------------------

[SUCCESS] .automation/test/kotlin/kotlint_good_1.kt


Example error log

Results of ktlint linter (version 0.40.0)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/kotlin_ktlint/
-----------------------------------------------

[ERROR] .automation/test/kotlin/kotlin_bad_1.kt
    .automation/test/kotlin/kotlin_bad_1.kt:1:1: File must end with a newline (\n)
    .automation/test/kotlin/kotlin_bad_1.kt:2:20: Unnecessary semicolon
    .automation/test/kotlin/kotlin_bad_1.kt:3:16: Redundant curly braces
    .automation/test/kotlin/kotlin_bad_1.kt:3:27: Unnecessary semicolon
    .automation/test/kotlin/kotlin_bad_1.kt:5:15: Unnecessary semicolon