Skip to main content
Version: 1.0.x

ktlint

API for calling declaring an ktlint lint aspect.

Typical usage: Make sure you have ktlint pulled as a dependency into your WORKSPACE/module by pulling a version of it from here https://github.com/pinterest/ktlint/releases and using a http_file declaration for it like.

http_file(
name = "com_github_pinterest_ktlint",
sha256 = "2e28cf46c27d38076bf63beeba0bdef6a845688d6c5dccd26505ce876094eb92",
url = "https://github.com/pinterest/ktlint/releases/download/1.2.1/ktlint",
executable = True,
)

Then, create the linter aspect, typically in tools/lint/linters.bzl:

load("@aspect_rules_lint//lint:ktlint.bzl", "ktlint_aspect")

ktlint = ktlint_aspect(
binary = "@@com_github_pinterest_ktlint//file",
# rules can be enabled/disabled from with this file
editorconfig = "@@//:.editorconfig",
# a baseline file with exceptions for violations
baseline_file = "@@//:.ktlint-baseline.xml",
)

If you plan on using Ktlint custom rulesets, you can also declare an additional ruleset_jar attribute pointing to your custom ruleset jar like this

java_binary(
name = "my_ktlint_custom_ruleset",
...
)

ktlint = ktlint_aspect(
binary = "@@com_github_pinterest_ktlint//file",
# rules can be enabled/disabled from with this file
editorconfig = "@@//:.editorconfig",
# a baseline file with exceptions for violations
baseline_file = "@@//:.ktlint-baseline.xml",
# Run your custom ktlint ruleset on top of standard rules
ruleset_jar = "@@//:my_ktlint_custom_ruleset_deploy.jar",
)

If your custom ruleset is a third-party dependency and not a first-party dependency, you can also fetch it using http_file and use it instead.

Macros and Functions

ktlint_action

Runs ktlint as build action in Bazel.

Adapter for wrapping Bazel around https://pinterest.github.io/ktlint/latest/install/cli/

Example usage (generated):

load("@aspect_rules_lint//lint:ktlint.bzl", "ktlint_action")

ktlint_action(
# an action context or aspect context
ctx = None,
# struct with ktlint field
executable = None,
# A list of source files to lint
srcs = [],
# The file object pointing to the editorconfig file used by ktlint
editorconfig = None,
# :output: the stdout of ktlint containing any violations found
stdout = None,
# The file object pointing to the baseline file used by ktlint.
baseline_file = "",
# The Java Runtime configured for this build, pulled from the registered toolchain.
java_runtime = None,
)

ctx

Required.

an action context or aspect context

executable

Required.

struct with ktlint field

srcs

Required.

A list of source files to lint

editorconfig

Required.

The file object pointing to the editorconfig file used by ktlint

stdout

Required.

:output: the stdout of ktlint containing any violations found

baseline_file

Required.

The file object pointing to the baseline file used by ktlint.

java_runtime

Required.

The Java Runtime configured for this build, pulled from the registered toolchain.

ruleset_jar

Optional. Default: None

An optional, custom ktlint ruleset jar.

exit_code

Optional. Default: None

output file to write the exit code. If None, then fail the build when ktlint exits non-zero.

options

Optional. Default: []

additional command-line arguments to ktlint, see https://pinterest.github.io/ktlint/latest/install/cli/#miscellaneous-flags-and-commands

lint_ktlint_aspect

A factory function to create a linter aspect.

Example usage (generated):

load("@aspect_rules_lint//lint:ktlint.bzl", "lint_ktlint_aspect")

lint_ktlint_aspect(
# a ktlint executable, provided as file typically through http_file declaration or using fetch_ktlint in your WORKSPACE.
binary = None,
# The label of the file pointing to the .editorconfig file used by ktlint.
editorconfig = None,
# An optional attribute pointing to the label of the baseline file used by ktlint.
baseline_file = "",
)

binary

Required.

a ktlint executable, provided as file typically through http_file declaration or using fetch_ktlint in your WORKSPACE.

editorconfig

Required.

The label of the file pointing to the .editorconfig file used by ktlint.

baseline_file

Required.

An optional attribute pointing to the label of the baseline file used by ktlint.

ruleset_jar

Optional. Default: None

An optional, custom ktlint ruleset provided as a fat jar, and works on top of the standard rules.

rule_kinds

Optional. Default: ["kt_jvm_library", "kt_jvm_binary", "kt_js_library"]

which kinds of rules should be visited by the aspect

fetch_ktlint

Example usage (generated):

load("@aspect_rules_lint//lint:ktlint.bzl", "fetch_ktlint")

fetch_ktlint(
)