detekt: K2 compiler crashes Detekt

Expected Behavior

After updating our project to use K2 compiler we expect we don’t get a crash when we run detekt

Observed Behavior

In our project when we run detekt we are getting similar exceptions to this one:

> Exception while analyzing expression in (135,15) in //Forlago/modules/feature-foo/src/main/kotlin/com/leinardi/forlago/feature/foo/ui/foo/FooScreen.kt

  Attachments:
  causeThrowable
  java.lang.IllegalArgumentException: fromIndex(1) > toIndex(0)
        at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:509)
        at java.base/java.util.ArrayList.subList(ArrayList.java:1108)
        at org.jetbrains.kotlin.builtins.FunctionTypesKt.getValueParameterTypesFromFunctionType(functionTypes.kt:178)
....

Steps to Reproduce

  1. Update your project to use K2 compiler. To achieve that you have to update your gradle.properties file adding:
# K2
android.experimental.lint.version=8.3.0-alpha17
android.lint.useK2Uast=true
kapt.use.k2=true
kotlin.experimental.tryK2=true
  1. Run detekt and we are getting several exceptions similar to:
> Task :modules:feature-debug:detektDebug FAILED
ERROR: Exception while analyzing expression in (200,11) in //Users/javierarroyo/Projects/Personal/Forlago/modules/feature-debug/src/main/kotlin/com/leinardi/forlago/feature/debug/ui/DebugScreen.kt

Attachments:
causeThrowable
java.lang.IllegalArgumentException: fromIndex(1) > toIndex(0)
        at java.base/java.util.AbstractList.subListRangeCheck(AbstractList.java:509)
        at java.base/java.util.ArrayList.subList(ArrayList.java:1108)
        at org.jetbrains.kotlin.builtins.FunctionTypesKt.getValueParameterTypesFromFunctionType(functionTypes.kt:178)
        at org.jetbrains.kotlin.resolve.FunctionDescriptorResolver.getValueParameters(FunctionDescriptorResolver.kt:380)

Github example

You can find a project sample with the issue here:

Your Environment

  • Version of detekt used: 1.23.4
  • Version of Gradle used (if applicable): 8.5
  • Gradle scan link (add --scan option when running the gradle task):
  • Operating System and version: Mac OS Monterey - v12.7.1
  • Link to your project (if it’s a public repository): https://github.com/leinardi/Forlago/pull/48

About this issue

  • Original URL
  • State: open
  • Created 7 months ago
  • Reactions: 5
  • Comments: 16 (13 by maintainers)

Most upvoted comments

It would help if you could test against this branch please: https://github.com/detekt/detekt/pull/6640

detekt is not currently compatible with K2 compiler.

Beta4 completely fixed my issue.

A beta 4 branch was created in the Kotlin repo about a week ago so I expect a new release shortly… I’d suggest waiting for that, and updated dependencies, before trying again.

There are a lot of moving parts sitting on top of a stabilising compiler so while it could definitely be a detekt issue it might be something else too.

https://docs.gradle.org/current/userguide/composite_builds.html

Good luck! Sorry I can’t provide time to support getting it set up but it should be relatively simple. You’ll need to separately include the Gradle plugin and the rest of the project.