The Detekt (a Kotlin static analysis tool) integration for the Bazel build system.
- configuration and baseline files;
- HTML, text and XML reports;
- plugins;
- customizable Detekt version and JVM flags;
- persistent workers support;
- and more.
bazel_dep(name = "rules_detekt", version = "...")
First of all you need to declare the rule in the WORKSPACE
file.
Please refer to GitHub releases for the version and
the SHA-256 hashsum.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
rules_detekt_version = "see-github-releases-page"
rules_detekt_sha = "see-github-releases-page"
http_archive(
name = "rules_detekt",
sha256 = rules_detekt_sha,
strip_prefix = "bazel_rules_detekt-{v}".format(v = rules_detekt_version),
url = "https://github.com/buildfoundation/bazel_rules_detekt/archive/v{v}.tar.gz".format(v = rules_detekt_version),
)
load("@rules_detekt//detekt:dependencies.bzl", "rules_detekt_dependencies")
rules_detekt_dependencies()
load("@rules_detekt//detekt:toolchains.bzl", "rules_detekt_toolchains")
rules_detekt_toolchains()
Users on Bazel releases prior to 5.1.0 need to enable the JSON Persistent Worker protocol in their .bazelrc
like so:
build --experimental_worker_allow_json_protocol
This option became stable and enabled by default for newer Bazel releases.
Once declared in the WORSKPACE
or MODULE.bazel
file, the rule can be loaded in the BUILD
file.
load("@rules_detekt//detekt:defs.bzl", "detekt_test")
detekt_test(
name = "my_detekt",
srcs = glob(["src/main/kotlin/**/*.kt"]),
)
See available attributes.
$ bazel build //mypackage:my_detekt
Results will be cached on successful runs.
Change the MODULE.bazel
file:
detekt = use_extension("//detekt:extensions.bzl", "detekt")
detekt.detekt_version(
version = "x.x.x",
sha256 = "x.x.x.sha256",
)
use_repo(detekt, "detekt_cli_all")
Or change the WORKSPACE
file:
load("@rules_detekt//detekt:versions.bzl", "detekt_version")
load("@rules_detekt//detekt:dependencies.bzl", "rules_detekt_dependencies")
rules_detekt_dependencies(
detekt_version = detekt_version(
version = "x.x.x",
sha256 = "x.x.x.sha256",
)
)
Define a toolchain in a BUILD
file:
load("@rules_detekt//detekt:toolchain.bzl", "detekt_toolchain")
detekt_toolchain(
name = "my_detekt_toolchain_impl",
jvm_flags = ["-Xms16m", "-Xmx128m"],
)
toolchain(
name = "my_detekt_toolchain",
toolchain = "my_detekt_toolchain_impl",
toolchain_type = "@rules_detekt//detekt:toolchain_type",
)
Change the WORKSPACE
file:
load("@rules_detekt//detekt:toolchains.bzl", "rules_detekt_toolchains")
rules_detekt_toolchains(toolchain = "//mypackage:my_detekt_toolchain")