Skip to content

Commit db2e0a6

Browse files
committed
Enable type resolution for Detekt behind the enable_type_resolution attr
1 parent f0f496e commit db2e0a6

10 files changed

Lines changed: 182 additions & 135 deletions

File tree

MODULE.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ detekt = use_extension("//detekt:extensions.bzl", "detekt")
1111
use_repo(detekt, "detekt_cli_all")
1212

1313
bazel_dep(name = "rules_java", version = "9.0.3")
14+
bazel_dep(name = "rules_android", version = "0.7.1")
1415
bazel_dep(name = "rules_proto", version = "7.1.0")
1516
bazel_dep(name = "rules_jvm_external", version = "6.9")
1617
bazel_dep(name = "bazel_worker_api", version = "0.0.10")
@@ -22,7 +23,7 @@ maven.install(
2223
name = "rules_detekt_dependencies",
2324
artifacts = [
2425
"junit:junit:4.13.2",
25-
"io.gitlab.arturbosch.detekt:detekt-formatting:1.23.1",
26+
"io.gitlab.arturbosch.detekt:detekt-formatting:1.23.8",
2627
],
2728
fail_if_repin_required = True,
2829
lock_file = "//:maven_install.json",

README.md

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ for the [Bazel build system](https://bazel.build).
99
- HTML, text, XML, Markdown, and SARIF reports;
1010
- [plugins](https://detekt.dev/docs/extensions/extensions/);
1111
- customizable Detekt version and JVM flags;
12-
- [persistent workers](https://blog.bazel.build/2015/12/10/java-workers.html) support;
12+
- [type resolution](https://detekt.dev/docs/gettingstarted/type-resolution/) with JVM and Android classpath support;
1313
- baseline generation via `detekt_create_baseline`;
1414
- configuration options via [attributes](docs/attrs.md).
1515

@@ -127,15 +127,15 @@ and it supports standard Bazel test flags such as `--test_output=all`.
127127

128128
### `detekt` vs `detekt_test`
129129

130-
| | `detekt` | `detekt_test` |
131-
| ------------------- | ------------------------- | -------------------------------------- |
132-
| Bazel rule type | build rule | test rule |
133-
| Run with | `bazel build` | `bazel test` |
134-
| Included in | `bazel build //...` | `bazel test //...` |
135-
| Violation behaviour | build action fails | test fails; build action always passes |
136-
| Text report | printed when action fails | printed to test output when test fails |
137-
| Result caching | yes | yes |
138-
| Bazel test flags | n/a | yes (`--test_output`, etc.) |
130+
| | `detekt` | `detekt_test` |
131+
| ------------------- | ------------------------- |-----------------------------------------------------------------------|
132+
| Bazel rule type | build rule | test rule |
133+
| Run with | `bazel build` | `bazel test` |
134+
| 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 |
137+
| Result caching | yes | yes |
138+
| Bazel test flags | n/a | yes (`--test_output`, etc.) |
139139

140140
Use `detekt` when you want violations to block builds the same way a compiler error does. Use
141141
`detekt_test` when you want Detekt to run alongside your test suite and report results through
@@ -177,8 +177,9 @@ detekt_test(
177177

178178
### Configuration Options
179179

180-
All three rules share the same configuration options. In addition to `srcs`, `cfgs`, `baseline`, `plugins`,
181-
and report options, most attributes correspond directly to
180+
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+
correspond directly to
182183
[Detekt CLI flags](https://detekt.dev/docs/1.23.8/gettingstarted/cli/#use-the-cli) and pass them
183184
through when explicitly set.
184185

@@ -187,7 +188,7 @@ More information can be found in the [attributes](docs/attrs.md).
187188
### Reports
188189

189190
A plain-text report (`{name}_detekt_report.txt`) is **always** generated. Other report formats are
190-
available for opt-in via configuration options..
191+
available for opt-in via attributes.
191192

192193
## Advanced Configuration
193194

@@ -207,7 +208,7 @@ detekt.detekt_version(
207208
use_repo(detekt, "detekt_cli_all")
208209
```
209210

210-
To download Detekt from a custom location (e.g. an internal mirror), use the `url_templates` parameter:
211+
To download Detekt from a custom location (e.g., an internal mirror), use the `url_templates` parameter:
211212

212213
```python
213214
detekt = use_extension("@rules_detekt//detekt:extensions.bzl", "detekt")
@@ -252,17 +253,27 @@ rules_detekt_dependencies(
252253

253254
Each template may contain `{version}` which will be replaced with the version string.
254255

255-
### JVM Flags
256+
### Toolchain
257+
258+
The detekt toolchain controls JVM flags, the JVM bytecode target version, and the Kotlin language
259+
version compatibility. The defaults are:
260+
261+
| Setting | Default |
262+
| ------------------ | -------- |
263+
| `jvm_flags` | `-Xms16m -Xmx128m` |
264+
| `jvm_target` | `1.8` |
265+
| `language_version` | `2.0` |
256266

257-
The default toolchain uses `-Xms16m -Xmx128m`. To customize JVM flags, define your own toolchain
258-
in a `BUILD` file:
267+
To override any of these, define a custom toolchain in a `BUILD` file:
259268

260269
```python
261270
load("@rules_detekt//detekt:toolchain.bzl", "detekt_toolchain")
262271

263272
detekt_toolchain(
264273
name = "my_detekt_toolchain_impl",
265274
jvm_flags = ["-Xms16m", "-Xmx512m"],
275+
jvm_target = "11",
276+
language_version = "1.9",
266277
)
267278

268279
toolchain(
@@ -354,49 +365,45 @@ detekt_test(
354365
)
355366
```
356367

357-
### JVM Target
358-
359-
Use `jvm_target` to set the JVM bytecode target version that matches what was used during compilation.
360-
This defaults to `1.8` if not explicitly set:
368+
### Type Resolution
361369

362-
```python
363-
detekt_test(
364-
name = "my_detekt",
365-
srcs = glob(["src/main/kotlin/**/*.kt"]),
366-
jvm_target = "11",
367-
)
368-
```
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.
369373

370-
### Language Version
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.
371376

372-
Detekt will report errors for any language features introduced after the specified version if
373-
`language_version` is specified. When unset, no compatibility restriction is applied:
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`:
374380

375381
```python
382+
load("@rules_detekt//detekt:defs.bzl", "detekt_test")
383+
376384
detekt_test(
377385
name = "my_detekt",
378386
srcs = glob(["src/main/kotlin/**/*.kt"]),
379-
language_version = "2.0",
387+
enable_type_resolution = True,
388+
deps = [":my_library"], # provides the classpath for type resolution
380389
)
381390
```
382391

383-
### Type Resolution
384-
385-
Type resolution enables more advanced static analysis by giving Detekt access to the full compilation classpath,
386-
including return types, nullability, and symbol information — capabilities that match those of the Kotlin compiler
387-
itself. Rules requiring it are annotated with `@RequiresFullAnalysis` in Detekt's source.
388-
389-
Use `jvm_target` and `language_version` to match the compilation settings of your project:
392+
For **Android targets**, set `is_android = True` to include the Android SDK jar in the classpath:
390393

391394
```python
392395
detekt_test(
393396
name = "my_detekt",
394397
srcs = glob(["src/main/kotlin/**/*.kt"]),
395-
jvm_target = "11",
396-
language_version = "2.0",
398+
enable_type_resolution = True,
399+
deps = [":my_android_library"],
400+
is_android = True,
397401
)
398402
```
399403

404+
`jvm_target` and `language_version` are toolchain-level settings — see
405+
[Toolchain](#toolchain) for how to configure them.
406+
400407
### Reports
401408

402409
By default, Detekt generates a text report internally (used for console output). To export reports as build outputs,

WORKSPACE.bazel

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ maven_install(
3737
name = "rules_detekt_dependencies",
3838
artifacts = [
3939
"junit:junit:4.13.2",
40-
"io.gitlab.arturbosch.detekt:detekt-formatting:1.23.1",
40+
"io.gitlab.arturbosch.detekt:detekt-formatting:1.23.8",
4141
],
4242
fail_if_repin_required = True,
4343
maven_install_json = "//:maven_install.json",
@@ -52,9 +52,9 @@ pinned_maven_install()
5252

5353
## Skylib
5454

55-
skylib_version = "1.4.1"
55+
skylib_version = "1.8.2"
5656

57-
skylib_sha = "b8a1527901774180afc798aeb28c4634bdccf19c4d98e7bdd1ce79d1fe9aaad7"
57+
skylib_sha = "6e78f0e57de26801f6f564fa7c4a48dc8b36873e416257a92bbb0937eeac8446"
5858

5959
http_archive(
6060
name = "bazel_skylib",
@@ -70,9 +70,9 @@ bazel_skylib_workspace()
7070

7171
## Stardoc
7272

73-
stardoc_version = "0.5.3"
73+
stardoc_version = "0.7.2"
7474

75-
stardoc_sha = "3fd8fec4ddec3c670bd810904e2e33170bedfe12f90adf943508184be458c8bb"
75+
stardoc_sha = "0e1ed4a98f26e718776bd64d053d02bb34d98572ccd03d6ba355112a1205706b"
7676

7777
http_archive(
7878
name = "io_bazel_stardoc",

WORKSPACE.bzlmod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This file intentionally left empty.
2+
# It tells Bazel not to process WORKSPACE.bazel when this repo is loaded
3+
# as an http_archive in a project using Bzlmod (MODULE.bazel).

0 commit comments

Comments
 (0)