JLLeitschuh / ktlint-gradle

A ktlint gradle plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support Kotlin 1.4 with ktlint 0.38.0

bjoernmayer opened this issue · comments

Version 0.38.0 of ktlint brings support for Kotlin 1.4.

Do you plan to release a new version ktlint-gradle supporting the new ktlint version?

you could set ktlint version via extension property:

val version: Property<String> = objectFactory.property { set("0.37.2") }

0.38.0 should work as well.

Yep.
Doing that already:

ktlint {
    this.version.set("0.38.0-alpha01")
}

A released version of ktlint-gradle supporting 1.4 would still be nice though

The plugin doesn't work with Kotlin 1.4.0 for me – I've tried versions 9.2.1 and 9.3.0. I've set ktlint version to 0.38.1:

ktlint {
    version.set("0.38.1")
    verbose.set(true)
    disabledRules.set(setOf("import-ordering", "no-wildcard-imports"))
}

I get the folowing error:

> Task :module:ktlintKotlinScriptCheck FAILED
Exception in thread "main" java.lang.NoSuchFieldError: FUN_KEYWORD
        at com.pinterest.ktlint.core.ast.ElementType.<clinit>(ElementType.kt:154)
        at com.pinterest.ktlint.ruleset.experimental.SpacingAroundAngleBracketsRule.<clinit>(SpacingAroundAngleBracketsRule.kt:94)
        at com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider.get(ExperimentalRuleSetProvider.kt:18)
        at com.pinterest.ktlint.KtlintCommandLine.loadRulesets(Main.kt:550)
        at com.pinterest.ktlint.KtlintCommandLine.run(Main.kt:222)
        at com.pinterest.ktlint.Main.main(Main.kt:62)

Is it possible that not all com.pinterest.ktlint dependencies are set to the selected version?

See also: pinterest/ktlint#876. I honestly don't know where is the problem: in ktlint or ktlint-gradle

Found the issue:

  • ktlint-gradle adds ktlint configuration that is used to pull ktlint itself
  • when project are still using kotlin 1.3.+ this is leaked into ktlint configuration leading to usage of incompatible kotlin version.

I am looking how to fix it, workaround would be either:

  • use ktlint version < 0.38.+
  • update your project to use Kotlin 1.4.+

Actually after further investigation in affected project I found out following gradle configuration that forcing kotlin version for all configurations:

configurations.all {
    resolutionStrategy {
        eachDependency {
            // Force Kotlin to our version
            if (requested.group == "org.jetbrains.kotlin") {
                useVersion("1.3.72")
            }
        }
    }
}

This enforces kotlin version also for ktlint configuration that is created by plugin to dynamically add ktlint and leads to errors running ktlint 0.38+ versions.

To avoid it you should exclude ktlint* configurations from kotlin version enforcement.

Hi @Tapchicoma!

Can you provide small example how to exclude ktlint* configurations from kotlin version enforcement?

@alex-t0 something like this should work:

configurations.all {
    if(!name.startsWith("ktlint")) {
        resolutionStrategy {
            eachDependency {
                // Force Kotlin to our version
                if (requested.group == "org.jetbrains.kotlin") {
                    useVersion("1.3.72")
                }
            }
        }
    }
}