Skip to content

Commit 7462411

Browse files
committed
Add support for Detekt v2.+
1 parent 27e5c5f commit 7462411

10 files changed

Lines changed: 154 additions & 155 deletions

File tree

README.md

Lines changed: 88 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ for the [Bazel build system](https://bazel.build).
66
## Features
77

88
- configuration and baseline files;
9-
- HTML, text, XML, Markdown, and SARIF reports;
9+
- HTML, XML, Markdown, and SARIF reports;
1010
- [plugins](https://detekt.dev/docs/extensions/extensions/);
1111
- customizable Detekt version and JVM flags;
1212
- [type resolution](https://detekt.dev/docs/gettingstarted/type-resolution/) with JVM and Android classpath support;
@@ -15,24 +15,25 @@ for the [Bazel build system](https://bazel.build).
1515

1616
## Compatibility
1717

18-
| `bazel_rules_detekt` | Default Detekt | Kotlin compiler | Max `language_version` | Min JDK | Max tested JDK | Bazel |
19-
| ----------------------------------- | --------------- | --------------- | ---------------------- | ------- | -------------- | --------- |
20-
| **v0.8.1.9 – v0.8.1.13** _(latest)_ | 1.23.8 | 2.0.21 | `2.0` | 8 | 21 | 9.x |
21-
| v0.8.1.3 – v0.8.1.8 | 1.23.5 | 1.9.22 | `1.9` | 8 | 17 | 7.x – 9.x |
22-
| v0.8.1 – v0.8.1.2 | 1.23.1 | 1.9.0 | `1.9` | 8 | 17 | 6.x |
23-
| v0.7.0 | 1.22.0 ||| 8 || 5.x |
24-
| v0.6.0 – v0.6.1 | 1.19.0 – 1.21.0 ||| 8 || 5.x |
25-
| v0.4.0 – v0.5.0 | 1.15.0 ||||| 4.x |
26-
| v0.3.0 | 1.7.4 ||||| 3.x |
27-
| v0.1.0 – v0.2.0 | 1.2.0 ||||| 1.x |
18+
> **Breaking change:** `bazel_rules_detekt` v2.0+ requires **Detekt 2.0 or later**. It is not compatible with Detekt 1.x. If you need Detekt 1.x support, use `bazel_rules_detekt` v0.8.x.
19+
20+
| `bazel_rules_detekt` | Default Detekt | Kotlin compiler | `language_version` | JDK | Bazel |
21+
|-----------------------------| --------------- | --------------- | ------------------- | ---------- | --------- |
22+
| **v1.0-alpha.2** _(latest)_ | 2.0.0-alpha.2 | 2.2.x | `1.0``2.3` | 8 – 21 | 9.x |
23+
| v0.8.1.9 – v0.8.1.13 | 1.23.8 | 2.0.21 | `1.0``2.0` | 8 – 21 | 9.x |
24+
| v0.8.1.3 – v0.8.1.8 | 1.23.5 | 1.9.22 | `1.0``1.9` | 8 – 17 | 7.x – 9.x |
25+
| v0.8.1 – v0.8.1.2 | 1.23.1 | 1.9.0 | `1.0``1.9` | 8 – 17 | 6.x |
26+
| v0.7.0 | 1.22.0 ||| 8+ | 5.x |
27+
| v0.6.0 – v0.6.1 | 1.19.0 – 1.21.0 ||| 8+ | 5.x |
28+
| v0.4.0 – v0.5.0 | 1.15.0 |||| 4.x |
29+
| v0.3.0 | 1.7.4 |||| 3.x |
30+
| v0.1.0 – v0.2.0 | 1.2.0 |||| 1.x |
2831

2932
For detailed per-Detekt-version Kotlin and JDK compatibility, see the [Detekt compatibility table](https://detekt.dev/docs/introduction/compatibility/).
3033

3134
> **Note:** The Kotlin compiler bundled with Detekt determines which `language_version` values are valid — setting it higher than what the bundled compiler supports will cause Detekt to fail. The default Detekt version can always be overridden — see [Detekt Version](#detekt-version).
3235
33-
> **Note:** Detekt 2.x is currently in alpha (targeting Kotlin 2.2+) and is not yet recommended for production use.
34-
35-
> **Note:** JDK 25 and above are **not** supported with Detekt 1.23.x. The bundled Kotlin compiler performs a hard version check that fails on JDK 25+. This is resolved in the Detekt 2.x series.
36+
> **Important:** Detekt 2.x is currently in alpha (targeting Kotlin 2.2+) and is not yet recommended for production use.
3637
3738
### Bazel
3839

@@ -86,9 +87,9 @@ Once declared in the `WORKSPACE` or `MODULE.bazel` file, the rules can be loaded
8687

8788
### `detekt`
8889

89-
`detekt` is a regular Bazel build rule. When Detekt finds violations, the **build action itself
90+
`detekt` is a regular Bazel build rule. When Detekt reports findings, the **build action itself
9091
fails**, stopping `bazel build` immediately with an error. This makes it behave like a compiler
91-
error — violations block the build.
92+
error — findings block the build.
9293

9394
```python
9495
load("@rules_detekt//detekt:defs.bzl", "detekt")
@@ -105,9 +106,9 @@ $ bazel build //mypackage:my_detekt
105106

106107
### `detekt_test`
107108

108-
`detekt_test` is a Bazel test rule. The build action always succeeds (even when violations are
109-
found), and Bazel then runs a test script that reads the real Detekt exit code and prints findings
110-
to the test output. Violations cause the **test** to fail rather than the build action.
109+
`detekt_test` is a Bazel test rule. The build action always succeeds (even when findings are
110+
reported), and Bazel then runs a test script that reads the real Detekt exit code and prints findings
111+
to the test output. Findings cause the **test** to fail rather than the build action.
111112

112113
```python
113114
load("@rules_detekt//detekt:defs.bzl", "detekt_test")
@@ -132,20 +133,20 @@ and it supports standard Bazel test flags such as `--test_output=all`.
132133
| Bazel rule type | build rule | test rule |
133134
| Run with | `bazel build` | `bazel test` |
134135
| Included in | `bazel build //...` | `bazel test //...` |
135-
| Violation behavior | build action fails | test fails; build action always passes even if violations are present |
136-
| Text report | printed when action fails | printed to test output when test fails |
136+
| Finding behavior | build action fails | test fails; build action always passes even if findings are reported |
137+
| Console output | printed when action fails | printed to test output when test fails |
137138
| Result caching | yes | yes |
138139
| Bazel test flags | n/a | yes (`--test_output`, etc.) |
139140

140-
Use `detekt` when you want violations to block builds the same way a compiler error does. Use
141+
Use `detekt` when you want findings to block builds the same way a compiler error does. Use
141142
`detekt_test` when you want Detekt to run alongside your test suite and report results through
142143
the test framework.
143144

144145
### `detekt_create_baseline`
145146

146147
`detekt_create_baseline` is an executable rule that generates or updates a
147148
[Detekt baseline](https://detekt.dev/docs/introduction/baseline/) file. The baseline suppresses
148-
existing findings so that only new violations fail the build going forward.
149+
existing findings so that only new findings fail the build going forward.
149150

150151
```python
151152
load("@rules_detekt//detekt:defs.bzl", "detekt_create_baseline")
@@ -178,23 +179,71 @@ detekt_test(
178179
### Configuration Options
179180

180181
All three rules share the same configuration options. In addition to `srcs`, `deps`, `cfgs`,
181-
`baseline`, `plugins`, `enable_type_resolution`, `is_android`, and report options, most attributes
182+
`baseline`, `plugins`, `analysis_mode`, `is_android`, and report options, most attributes
182183
correspond directly to
183-
[Detekt CLI flags](https://detekt.dev/docs/1.23.8/gettingstarted/cli/#use-the-cli) and pass them
184+
[Detekt CLI flags](https://detekt.dev/docs/gettingstarted/cli/#use-the-cli) and pass them
184185
through when explicitly set.
185186

186187
More information can be found in the [attributes](docs/attrs.md).
187188

188189
### Reports
189190

190-
A plain-text report (`{name}_detekt_report.txt`) is **always** generated. Other report formats are
191-
available for opt-in via attributes.
191+
Findings are printed directly to the console. Report files can be enabled for export as build outputs via attributes.
192+
193+
## Type Resolution
194+
195+
Type resolution enables more advanced static analysis by giving Detekt access to the full
196+
compilation classpath — including return types, nullability, and symbol information. Rules
197+
requiring it extend the `RequiresAnalysisApi` interface.
198+
199+
Type resolution is controlled by the `analysis_mode` attribute:
200+
201+
- `"light"` *(default)* — syntax-based analysis only; no classpath is passed to Detekt:
202+
203+
```python
204+
load("@rules_detekt//detekt:defs.bzl", "detekt_test")
205+
206+
detekt_test(
207+
name = "my_detekt",
208+
srcs = glob(["src/main/kotlin/**/*.kt"]),
209+
analysis_mode = "light",
210+
)
211+
```
212+
213+
- `"full"` — passes the compilation classpath to Detekt and enables type-aware rules. The
214+
appropriate bootclasspath (JDK or Android SDK) is always included. To also include your
215+
project's library dependencies, pass them via `deps`:
216+
217+
```python
218+
detekt_test(
219+
name = "my_detekt",
220+
srcs = glob(["src/main/kotlin/**/*.kt"]),
221+
analysis_mode = "full",
222+
deps = [":my_library"], # used to construct the classpath for type resolution
223+
)
224+
```
225+
226+
For **Android targets**, set `is_android = True` to include the Android SDK jar in the classpath
227+
(requires `analysis_mode = "full"`):
228+
229+
```python
230+
detekt_test(
231+
name = "my_detekt",
232+
srcs = glob(["src/main/kotlin/**/*.kt"]),
233+
analysis_mode = "full",
234+
deps = [":my_android_library"],
235+
is_android = True,
236+
)
237+
```
238+
239+
`jvm_target` and `language_version` are toolchain-level settings — see
240+
[Toolchain](#toolchain) for how to configure them.
192241

193242
## Advanced Configuration
194243

195244
### Detekt Version
196245

197-
The default bundled version is **1.23.8**. To override it:
246+
The default bundled version is **2.0.0-alpha.2**. To override it:
198247

199248
#### `MODULE.bazel` Configuration
200249

@@ -258,11 +307,11 @@ Each template may contain `{version}` which will be replaced with the version st
258307
The detekt toolchain controls JVM flags, the JVM bytecode target version, and the Kotlin language
259308
version compatibility. The defaults are:
260309

261-
| Setting | Default |
262-
| ------------------ | -------- |
310+
| Setting | Default |
311+
| ------------------ |--------------------|
263312
| `jvm_flags` | `-Xms16m -Xmx128m` |
264-
| `jvm_target` | `1.8` |
265-
| `language_version` | `2.0` |
313+
| `jvm_target` | `1.8` |
314+
| `language_version` | `2.3` |
266315

267316
To override any of these, define a custom toolchain in a `BUILD` file:
268317

@@ -273,7 +322,7 @@ detekt_toolchain(
273322
name = "my_detekt_toolchain_impl",
274323
jvm_flags = ["-Xms16m", "-Xmx512m"],
275324
jvm_target = "11",
276-
language_version = "1.9",
325+
language_version = "2.1",
277326
)
278327

279328
toolchain(
@@ -302,13 +351,13 @@ register_toolchains("//mypackage:my_detekt_toolchain")
302351
The `plugins` attribute accepts any Bazel label that provides `JavaInfo`. This covers both
303352
published Maven artifacts and locally built JARs.
304353

305-
**Maven artifact** (e.g., the [formatting rule set](https://detekt.dev/docs/rules/formatting/)):
354+
**Maven artifact** (e.g., the [ktlint wrapper plugin](https://detekt.dev/docs/rules/formatting/)):
306355

307356
```python
308357
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
309358
maven.install(
310359
artifacts = [
311-
"io.gitlab.arturbosch.detekt:detekt-formatting:1.23.8",
360+
"dev.detekt:detekt-rules-ktlint-wrapper:2.0.0-alpha.2",
312361
],
313362
)
314363
use_repo(maven, "maven")
@@ -320,7 +369,7 @@ load("@rules_detekt//detekt:defs.bzl", "detekt_test")
320369
detekt_test(
321370
name = "my_detekt",
322371
srcs = glob(["src/main/kotlin/**/*.kt"]),
323-
plugins = ["@maven//:io_gitlab_arturbosch_detekt_detekt_formatting"],
372+
plugins = ["@maven//:dev_detekt_detekt_rules_ktlint_wrapper"],
324373
)
325374
```
326375

@@ -355,7 +404,7 @@ detekt_test(
355404
)
356405
```
357406

358-
To extend Detekt's built-in defaults rather than replace them, also set `build_upon_default_config = True`:
407+
To extend Detekt's built-in configuration defaults rather than replace them, also set `build_upon_default_config = True`:
359408

360409
```python
361410
detekt_test(
@@ -365,55 +414,15 @@ detekt_test(
365414
)
366415
```
367416

368-
### Type Resolution
369-
370-
Type resolution enables more advanced static analysis by giving Detekt access to the full
371-
compilation classpath — including return types, nullability, and symbol information. Rules
372-
requiring it are annotated with `@RequiresFullAnalysis` in Detekt's source.
373-
374-
Type resolution is **disabled by default** (`enable_type_resolution = False`), meaning only
375-
syntax-based rules are applied and no classpath is passed to Detekt.
376-
377-
To enable type resolution, set `enable_type_resolution = True`. When enabled, the appropriate
378-
bootclasspath (JDK or Android SDK) is always included. To also include your project's library
379-
dependencies on the classpath, pass them via `deps`:
380-
381-
```python
382-
load("@rules_detekt//detekt:defs.bzl", "detekt_test")
383-
384-
detekt_test(
385-
name = "my_detekt",
386-
srcs = glob(["src/main/kotlin/**/*.kt"]),
387-
enable_type_resolution = True,
388-
deps = [":my_library"], # provides the classpath for type resolution
389-
)
390-
```
391-
392-
For **Android targets**, set `is_android = True` to include the Android SDK jar in the classpath:
393-
394-
```python
395-
detekt_test(
396-
name = "my_detekt",
397-
srcs = glob(["src/main/kotlin/**/*.kt"]),
398-
enable_type_resolution = True,
399-
deps = [":my_android_library"],
400-
is_android = True,
401-
)
402-
```
403-
404-
`jvm_target` and `language_version` are toolchain-level settings — see
405-
[Toolchain](#toolchain) for how to configure them.
406-
407417
### Reports
408418

409-
By default, Detekt generates a text report internally (used for console output). To export reports as build outputs,
410-
enable them explicitly:
419+
Findings are always printed to the console via Detekt's built-in console reporter. To also export
420+
reports as build outputs, enable them explicitly:
411421

412422
```python
413423
detekt_test(
414424
name = "my_detekt",
415425
srcs = glob(["src/main/kotlin/**/*.kt"]),
416-
txt_report = True, # {target_name}_detekt_report.txt
417426
html_report = True, # {target_name}_detekt_report.html
418427
xml_report = True, # {target_name}_detekt_report.xml (Checkstyle format, compatible with SonarQube)
419428
md_report = True, # {target_name}_detekt_report.md

0 commit comments

Comments
 (0)