Hi there, would you be willing to set something li...
# ktlint
w
Hi there, would you be willing to set something like
Copy code
languageVersion = "1.4"
        apiVersion = "1.4"
in ktlint so that it can still be used by projects not on 1.8 yet?
πŸ‘ 1
I recommend 1.4 just because its the minimum, on account of ktlint's usage of inline classes. but something between that and 1.8 would be fine too
p
To be honest, I have no clue what the consequences of this will be for ktlint, nor on how to implement that (I have little knowledge of Gradle). If there is no impact on ktlint, it can be considered. But, I would like to have input on this from @Zongle Wang and @mateusz.kwiecinski as well.
w
kotlin is backward compatible, but not forward, so if you compile on X, only downstreams on >=X can consume your library. these params (yes this is gradle configuration but it corresponds to kotlin compiler flags) declare that even though you have compiled on kotlin 1.8, your source would still be compatible with 1.4, so projects on kotlin version 1.4 still can safely consume your library. if you were to try to use a newer language feature, you would need to update these values to the version that feature was added, or your build would break. This allows projects to compile on the latest kotlin, without forcing that requirement on their downstreams
s
that seems reasonable, assuming we don't use any newer language features (unlikely?)
m
I can confirm the settings John suggests makes a lot of sense πŸ‘ This is somewhat the same to what I did to Java compatibility (by setting Java compile & target version) Also a heads up: setting language version to 1.4 will show a deprecation warning, but the only thing we can do is ignore if for now (that is, as long as we consider 7.x an supported Gradle version (since Gradle forces runtime Kotlin version for its plugins https://docs.gradle.org/current/userguide/compatibility.html#kotlin ))
p
How does this language version relate to the embedded kotlin compiler? In order to support lint and formatting of latest language features we need the new embedded compiler. Last language feature for which this was needed for the range until operator and context receivers.
w
ooo that is a good question.
m
Can you point me at an example of such code? Where the new language features (like context receivers) are read?
@wakingrufus Does your plugin contain tests running against older (i.e. 7.6) Gradle version. If I understand things correctly, things like i.e. generating baseline or using sarif reporter should fail in runtime (due to usage of language features not yet available in 1.4)
w
yes, but the kotlin plugin version is always at least 1.4
and that works
p
Can you point me at an example of such code? Where the new language features (like context receivers) are read?
Context receivers were added in this PR or see the
ContextReceiverWrappingRule
. RangeUntil was fixed in rule
SpacingAroundRangeOperatorRule
in this PR.
πŸ™ 1
Did anyone made an attempt to validate this? I am slowly moving to the release of
1.0
and it would be great to decide on this topic. Note that according to https://kotlinlang.org/docs/compatibility-modes.html only the three latest versions (
1.7
,
1.8
and
1.9
are supported).
I have changed language version and api in
build.gradle.kts
in
build-logic
by adding a
compileOptions
block. This was the lowest that I could go:
Copy code
tasks.withType<KotlinCompile>().configureEach {
    // Convert Java version (e.g. "1.8" or "11") to Kotlin JvmTarget ("8" resp. "11")
    compilerOptions.jvmTarget.set(JvmTarget.fromTarget(buildLogicTargetJavaVersion.toString()))
    compilerOptions {
        kotlinOptions {
            languageVersion = "1.5"
            apiVersion = "1.4"
        }
    }
}
With
languageVersion = "1.4"
I get compile errors when loading the gradle changes. It might be possible to resolve them, but I would like a more gradle knowledgeable developer to do so.
w
I think this is more than reasonable
m
Did anyone made an attempt to validate this?
I tried to set both language&api version to 1.4, but seeing the number of errors, I realised I don't really get why current setup works when Gradle forces runtime Kotlin version. As a conclusion I decided I'll need to delve into this topic again (but I've been busy recently πŸ˜•) I'll share y findings when I have any πŸ‘€ I confirm the defaults you set seem OK for a starter πŸ‘
p
Did you mean that you don't get why ktlint works or the gradle plugin setup? Ktlint most likely works as it encloses the embeddable compiler, currently version 1.8, for creating the PSI/AST.
πŸ‘ 1
m
I meant any of the Gradle plugins. According to what I understand, older Gradle versions (i.e. 7.6) force 1.4 Kotlin version in runtime. I expected them to fail since ktlint engine use apis available only in 1.5 (i.e. kotlin.io.Path), but for some reason everything seems to work πŸ‘€
πŸ‘ 1