Skip to content

detekt

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

detekt documentation

detekt - GitHub

Configuration in MegaLinter

Variable Description Default value
KOTLIN_DETEKT_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
KOTLIN_DETEKT_COMMAND_REMOVE_ARGUMENTS User custom arguments to remove from command line before calling the linter
Ex: -s --foo "bar"
KOTLIN_DETEKT_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".kt", ".kts"]
KOTLIN_DETEKT_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_DETEKT_PRE_COMMANDS List of bash commands to run before the linter None
KOTLIN_DETEKT_POST_COMMANDS List of bash commands to run after the linter None
KOTLIN_DETEKT_UNSECURED_ENV_VARIABLES List of env variables explicitly not filtered before calling KOTLIN_DETEKT and its pre/post commands None
KOTLIN_DETEKT_DISABLE_ERRORS Run linter but consider errors as warnings false
KOTLIN_DETEKT_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0
KOTLIN_DETEKT_CLI_EXECUTABLE Override CLI executable ['detekt-cli']

IDE Integration

Use detekt 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 125 Docker Image Size (tag) Docker Pulls
c_cpp Optimized for pure C/C++ projects 56 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 63 Docker Image Size (tag) Docker Pulls
dotnetweb Optimized for C, C++, C# or VB based projects with JS/TS 72 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 61 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 56 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 64 Docker Image Size (tag) Docker Pulls
ruby Optimized for RUBY based projects 52 Docker Image Size (tag) Docker Pulls
rust Optimized for RUST based projects 52 Docker Image Size (tag) Docker Pulls
salesforce Optimized for Salesforce based projects 56 Docker Image Size (tag) Docker Pulls
swift Optimized for SWIFT based projects 52 Docker Image Size (tag) Docker Pulls
terraform Optimized for TERRAFORM based projects 56 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .kt, .kts

How the linting is performed

detekt is called once on the whole project directory (project CLI lint mode)

  • filtering can not be done using MegaLinter configuration variables,it must be done using detekt configuration or ignore file (if existing)
  • VALIDATE_ALL_CODEBASE: false doesn't make detekt analyze only updated files

Example calls

detekt-cli
detekt-cli --report html:folder/kotlin_detekt_result.html

Help content

Usage: detekt [options]
  Options:
    --all-rules
      Activates all available (even unstable) rules.
      Default: false
    --auto-correct, -ac
      Allow rules to auto correct code if they support it. The default rule
      sets do NOT support auto correcting and won't change any line in the
      users code base. However custom rules can be written to support auto
      correcting. The additional 'formatting' rule set, added with
      '--plugins', does support it and needs this flag.
      Default: false
    --base-path, -bp
      Specifies a directory as the base path.Currently it impacts all file
      paths in the formatted reports. File paths in console output and txt
      report are not affected and remain as absolute paths.
    --baseline, -b
      If a baseline xml file is passed in, only new code smells not in the
      baseline are printed in the console.
    --build-upon-default-config
      Preconfigures detekt with a bunch of rules and some opinionated defaults
      for you. Allows additional provided configurations to override the
      defaults.
      Default: false
    --classpath, -cp
      EXPERIMENTAL: Paths where to find user class files and depending jar
      files. Used for type resolution.
    --config, -c
      Path to the config file (path/to/config.yml). Multiple configuration
      files can be specified with ',' or ';' as separator.
    --config-resource, -cr
      Path to the config resource on detekt's classpath (path/to/config.yml).
    --create-baseline, -cb
      Treats current analysis findings as a smell baseline for future detekt
      runs.
      Default: false
    --debug
      Prints extra information about configurations and extensions.
      Default: false
    --disable-default-rulesets, -dd
      Disables default rule sets.
      Default: false
    --excludes, -ex
      Globbing patterns describing paths to exclude from the analysis.
    --generate-config, -gc
      Export default config. Path can be specified with --config option
      (default path: default-detekt-config.yml)
      Default: false
    --help, -h
      Shows the usage.
    --includes, -in
      Globbing patterns describing paths to include in the analysis. Useful in
      combination with 'excludes' patterns.
    --input, -i
      Input paths to analyze. Multiple paths are separated by comma. If not
      specified the current working directory is used.
    --jdk-home
      EXPERIMENTAL: Use a custom JDK home directory to include into the
      classpath
    --jvm-target
      EXPERIMENTAL: Target version of the generated JVM bytecode that was
      generated during compilation and is now being used for type resolution
      (1.8, 9, 10, ..., 20)
      Default: 1.8
    --language-version
      EXPERIMENTAL: Compatibility mode for Kotlin language version X.Y,
      reports errors for all language features that came out later
      Possible Values: [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1]
    --max-issues
      Return exit code 0 only when found issues count does not exceed
      specified issues count.
    --parallel
      Enables parallel compilation and analysis of source files. Do some
      benchmarks first before enabling this flag. Heuristics show performance
      benefits starting from 2000 lines of Kotlin code.
      Default: false
    --plugins, -p
      Extra paths to plugin jars separated by ',' or ';'.
    --report, -r
      Generates a report for given 'report-id' and stores it on given 'path'.
      Entry should consist of: [report-id:path]. Available 'report-id' values:
      'txt', 'xml', 'html', 'md', 'sarif'. These can also be used in
      combination with each other e.g. '-r txt:reports/detekt.txt -r
      xml:reports/detekt.xml'
    --version
      Prints the detekt CLI version.
      Default: false

Installation on mega-linter Docker image

  • Dockerfile commands :
# Parent descriptor install
ENV JAVA_HOME=/usr/lib/jvm/java-21-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"
# Linter install
# renovate: datasource=github-tags depName=detekt/detekt
ARG DETEKT_VERSION=1.23.6

RUN curl --retry 5 --retry-delay 5 -sSLO https://github.com/detekt/detekt/releases/download/v${DETEKT_VERSION}/detekt-cli-${DETEKT_VERSION}.zip && \
    unzip detekt-cli-${DETEKT_VERSION}.zip && \
    chmod a+x detekt-cli-${DETEKT_VERSION}/bin/* && \
    chmod a+x detekt-cli-${DETEKT_VERSION}/lib/* && \
    mv -n detekt-cli-${DETEKT_VERSION}/bin/* usr/bin && \
    mv -n detekt-cli-${DETEKT_VERSION}/lib/* usr/lib