Skip to content

ktlint

GitHub stars autofix sarif GitHub release (latest SemVer) GitHub last commit GitHub commit activity GitHub contributors

ktlint documentation

ktlint - GitHub

Configuration in MegaLinter

Variable Description Default value
KOTLIN_KTLINT_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
KOTLIN_KTLINT_COMMAND_REMOVE_ARGUMENTS User custom arguments to remove from command line before calling the linter
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_CLI_LINT_MODE Override default CLI lint mode
- file: Calls the linter for each file
- project: Call the linter from the root of the project
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_PRE_COMMANDS List of bash commands to run before the linter None
KOTLIN_KTLINT_POST_COMMANDS List of bash commands to run after the linter None
KOTLIN_KTLINT_UNSECURED_ENV_VARIABLES List of env variables explicitly not filtered before calling KOTLIN_KTLINT and its pre/post commands None
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
KOTLIN_KTLINT_CLI_EXECUTABLE Override CLI executable ['ktlint']

IDE Integration

Use ktlint in your favorite IDE to catch errors before MegaLinter !

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

MegaLinter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 121 Docker Image Size (tag) Docker Pulls
c_cpp Optimized for pure C/C++ projects 55 Docker Image Size (tag) Docker Pulls
cupcake MegaLinter for the most commonly used languages 84 Docker Image Size (tag) Docker Pulls
documentation MegaLinter for documentation projects 51 Docker Image Size (tag) Docker Pulls
dotnet Optimized for C, C++, C# or VB based projects 64 Docker Image Size (tag) Docker Pulls
dotnetweb Optimized for C, C++, C# or VB based projects with JS/TS 73 Docker Image Size (tag) Docker Pulls
go Optimized for GO based projects 53 Docker Image Size (tag) Docker Pulls
java Optimized for JAVA based projects 54 Docker Image Size (tag) Docker Pulls
javascript Optimized for JAVASCRIPT or TYPESCRIPT based projects 60 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 54 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 62 Docker Image Size (tag) Docker Pulls
ruby Optimized for RUBY based projects 51 Docker Image Size (tag) Docker Pulls
rust Optimized for RUST based projects 51 Docker Image Size (tag) Docker Pulls
salesforce Optimized for Salesforce based projects 55 Docker Image Size (tag) Docker Pulls
swift Optimized for SWIFT based projects 51 Docker Image Size (tag) Docker Pulls
terraform Optimized for TERRAFORM based projects 55 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .kt, .kts

How the linting is performed

  • ktlint is called one time by identified file (file CLI lint mode)

Example calls

ktlint myfile.kt
ktlint --format myfile.kt

Help content

Usage: ktlint [<options>] [<arguments>]... <command> [<args>]...

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

  Usage on Windows: java -jar ktlint.jar <options> [<arguments>]... <command>
  [<args>]...


  ═════════════════════════════════ EXAMPLES ══════════════════════════════════



  ─────────────────────────── Use default patterns ────────────────────────────


  Check the style of all Kotlin files (ending with '.kt' or '.kts') inside the
  current dir (recursively). Hidden folders will be skipped.

  ktlint


  ───────────────────────────── Specify patterns ──────────────────────────────


  Check only certain locations starting from the current directory. Prepend !
  to negate the pattern, KtLint uses .gitignore pattern style syntax. Globs are
  applied starting from the last one.

  Check all '.kt' files in 'src/' directory, but ignore files ending with
  'Test.kt':

  ktlint "src/**/*.kt" "!src/**/*Test.kt"

  Check all '.kt' files in 'src/' directory, but ignore 'generated' directory
  and its subdirectories:

  ktlint "src/**/*.kt" "!src/**/generated/**"


  ─────────────────────── Auto-correct style violations ───────────────────────


  Check all '.kt' files in 'src/' directory, and when possible automatically
  correct the lint violations:

  ktlint -F "src/**/*.kt"


  ───────── Using custom reporter jar and overriding report location ──────────


  ktlint
  --reporter=csv,artifact=/path/to/reporter/csv.jar,output=my-custom-report.csv


  ═══════════════════════════ Options and commands ════════════════════════════


Options:
  -v, --version            Show the version and exit
  --code-style=(android_studio|intellij_idea|ktlint_official)
                           (deprecated)
  --color                  Make output colorful
  --color-name=<text>      Customize the output color
  -F, --format             Fix deviations from the code style when possible
  --limit=<int>            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=<text>        A reporter to use (built-in: plain (default),
                           plain?group_by_file, plain-summary, json, sarif,
                           checkstyle, html). To usea third-party reporter
                           specify a path to a JAR file on the filesystem via
                           ',artifact=' option. To override reporter output,
                           use ',output=' option.
  -R, --ruleset=<text>     A path to a JAR file containing additional
                           ruleset(s)
  --stdin                  Read file from stdin
  --patterns-from-stdin[=<text>]
                           Read additional patterns to check/format from stdin.
                           Patterns are delimited by the given argument.
                           (default is newline). If the argument is an empty
                           string, the NUL byte is used.
  --editorconfig=<text>    Path to the default '.editorconfig'. A property
                           value from this file is used only when no
                           '.editorconfig' file on the path to the source file
                           specifies that property. Note: up until ktlint 0.46
                           the property value in this file used to override
                           values found in '.editorconfig' files on the path to
                           the source file.
  --baseline=<text>        Defines a baseline file to check against
  -l, --log-level=<value>  Defines the minimum log level (trace, debug, info,
                           warn, error) or none to suppress all logging
  -h, --help               Show this message and exit

Commands:
  generateEditorConfig     Generate kotlin style section for '.editorconfig'
                           file. Output should be copied manually to the
                           '.editorconfig' file.
  installGitPreCommitHook  Install git hook to automatically check files for
                           style violations on commit
  installGitPrePushHook    Install git hook to automatically check files for
                           style violations before push

Installation on mega-linter Docker image

  • Dockerfile commands :
# Parent descriptor install
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"
# Linter install
RUN curl --retry 5 --retry-delay 5 -sSLO https://github.com/pinterest/ktlint/releases/latest/download/ktlint && \
    chmod a+x ktlint && \
    mv "ktlint" /usr/bin/