Skip to content

Kotlin language version handling #1748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 28, 2019
Merged

Kotlin language version handling #1748

merged 13 commits into from
Jul 28, 2019

Conversation

remal
Copy link
Contributor

@remal remal commented Jul 2, 2019

This PR helps to solve an issue when a user have a Kotlin project with Kotlin version less then Detekt's Kotlin version. Without these fixes a lot of Kotlin embedded compiler warnings occur.

Also a lot of coding style issues have been fixed.

Signed-off-by: Semyon Levin <levin.semen@gmail.com>
@codecov-io
Copy link

codecov-io commented Jul 2, 2019

Codecov Report

Merging #1748 into master will decrease coverage by 0.08%.
The diff coverage is 80.43%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1748      +/-   ##
============================================
- Coverage     79.86%   79.78%   -0.09%     
- Complexity     1936     1937       +1     
============================================
  Files           325      324       -1     
  Lines          5478     5500      +22     
  Branches       1013     1015       +2     
============================================
+ Hits           4375     4388      +13     
- Misses          581      587       +6     
- Partials        522      525       +3
Impacted Files Coverage Δ Complexity Δ
.../io/gitlab/arturbosch/detekt/cli/runners/Runner.kt 0% <ø> (ø) 0 <0> (ø) ⬇️
...n/io/gitlab/arturbosch/detekt/core/DetektFacade.kt 48.78% <ø> (-2.39%) 3 <0> (ø)
.../io/gitlab/arturbosch/detekt/cli/PathConverters.kt 72% <0%> (-6.27%) 0 <0> (ø)
...lin/io/gitlab/arturbosch/detekt/core/KtCompiler.kt 69.69% <100%> (-1.74%) 6 <0> (-1)
...itlab/arturbosch/detekt/core/ProcessingSettings.kt 79.06% <100%> (+1.02%) 16 <1> (+1) ⬆️
.../kotlin/io/gitlab/arturbosch/detekt/cli/CliArgs.kt 84.61% <80%> (-3.39%) 7 <5> (ø)
...osch/detekt/api/internal/KotlinEnvironmentUtils.kt 86% <84.61%> (-2.89%) 0 <0> (ø)
...b/arturbosch/detekt/rules/empty/EmptyCatchBlock.kt 57.14% <0%> (-26.2%) 3% <0%> (ø)
...n/kotlin/io/gitlab/arturbosch/detekt/api/Metric.kt 73.33% <0%> (-4.45%) 8% <0%> (ø)
...osch/detekt/rules/exceptions/SwallowedException.kt 65.38% <0%> (-2.48%) 13% <0%> (-3%)
... and 16 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e7aa71e...e003512. Read the comment docs.

@remal
Copy link
Contributor Author

remal commented Jul 2, 2019

@arturbosch @schalkms Could you please take a look?

@remal
Copy link
Contributor Author

remal commented Jul 2, 2019

This PR fixes this warning:

warning: runtime JAR files in the classpath have the version 1.2, which is older than the API version 1.3. Consider using the runtime of version 1.3, or pass '-api-version 1.2' explicitly to restrict the available APIs to the runtime of version 1.2. You can also pass '-language-version 1.2' instead, which will restrict not only the APIs to the specified version, but also the language features
~/.gradle/wrapper/dists/gradle-4.10.3-all/81msde2dx9p4vji0mjgtvxkcb/gradle-4.10.3/lib/kotlin-reflect-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3
~/.gradle/wrapper/dists/gradle-4.10.3-all/81msde2dx9p4vji0mjgtvxkcb/gradle-4.10.3/lib/kotlin-script-runtime-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3
~/.gradle/wrapper/dists/gradle-4.10.3-all/81msde2dx9p4vji0mjgtvxkcb/gradle-4.10.3/lib/kotlin-stdlib-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3
~/.gradle/wrapper/dists/gradle-4.10.3-all/81msde2dx9p4vji0mjgtvxkcb/gradle-4.10.3/lib/kotlin-stdlib-common-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3
~/.gradle/wrapper/dists/gradle-4.10.3-all/81msde2dx9p4vji0mjgtvxkcb/gradle-4.10.3/lib/kotlin-stdlib-jdk7-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3
~/.gradle/wrapper/dists/gradle-4.10.3-all/81msde2dx9p4vji0mjgtvxkcb/gradle-4.10.3/lib/kotlin-stdlib-jdk8-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3
~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-reflect/1.2.61/6fd5bcfc9ffc446dd147ea006bee7ef5f0ad8ca4/kotlin-reflect-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3
~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.2.61/5bc44acc4b3f0d19166ae3e50454b41e8ff29335/kotlin-stdlib-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3
~/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.2.61/772de03e12d932f489e41aef997d26c20a4ebee6/kotlin-stdlib-common-1.2.61.jar: warning: runtime JAR file has version 1.2 which is older than required for API version 1.3

Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I think this is a nice addition.
I have added some questions to this review.
The cli flag is just a check right. There's no other behavior
I think documentation and tests are missing for this cli flag.

@schalkms
Copy link
Member

schalkms commented Jul 2, 2019

@remal you need to use the --language-version flag in order to fix this warning, right? Otherwise, the warning still gets displayed, which isn't a bad thing IMHO.

Fix a misprint
@remal
Copy link
Contributor Author

remal commented Jul 2, 2019

@schalkms Yes, you're right. I'm trying to analyze sources of a project that depends on an older version of Kotlin (1.2). So I see this warning every time I run Detekt.

Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.
Regarding the tests, I think it's okay then.
With my PR in #1752 codecov doesn't fail anymore when the test coverage decreases.
I'm resolving the remaining discussions.
Please take a look at my last question. Otherwise this is then good to go, I think.

@@ -77,6 +77,11 @@ Usage: detekt [options]
--input, -i
Input paths to analyze. Multiple paths are separated by comma. If not
specified the current working directory is used.
--language-version
EXPERIMENTAL: Compatibility mode for Kotlin language version X.Y, reports
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reports errors for all language features that came out later.

What do you mean by that? Could you please show an example to help me understand the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I haven't seen such warnings. I suppose we can get them if Kotlin JARs in the classpath are not aligned, for example.

@remal
Copy link
Contributor Author

remal commented Jul 2, 2019

@schalkms I have an idea to move automated Kotlin version detection from Detekt's Gradle plugin to detekt-cli. It can be done by processing --classpath parameter. I'll try to make it tomorrow.

@schalkms
Copy link
Member

schalkms commented Jul 2, 2019

I have an idea to move automatically Kotlin version detection from Detekt's Gradle plugin to detekt-cli. It can be done by processing --classpath parameter. I'll try to make it tomorrow.

@remal is this PR then obsolete?

@remal
Copy link
Contributor Author

remal commented Jul 3, 2019

@schalkms I've pushed a new commit with moving version retrieval logic.

BTW I haven't tested it in Java >= 9 environment.

Signed-off-by: Semyon Levin <levin.semen@gmail.com>
@@ -95,18 +95,18 @@ fun createCompilerConfiguration(
private fun Iterable<File>.getKotlinLanguageVersion(): LanguageVersion? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to unit test this single method.
Then we are also sure that it works on all JVM versions with the help of CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a test. See KotlinEnvironmentUtilsTest.

This test does not:

  1. Test the code against different versions of Kotlin
  2. Test the code against different versions of JVM

PS: Asking for tests was very useful - I catched a bug :)

Signed-off-by: Semyon Levin <levin.semen@gmail.com>
Copy link
Member

@arturbosch arturbosch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, seems reasonable to support older kotlin versions when 1.4 comes out.
Please revert the continuous indent to 4 again, see this summary ticket for example JuulLabs/kotlin-code-styles#1 (comment)

KtLint deleted the option to configure the continuous indent and android kotlin guide uses 4. Also jetbrains reverted many places where 8 was used for example parameter lists. We will just stick with 4.
Kotlin/kotlin-style-guide#38

@arturbosch
Copy link
Member

The test seems to fail on Java 9+, any idea why?

io.gitlab.arturbosch.detekt.api.internal.KotlinEnvironmentUtilsTest > should match FAILED
    kotlin.KotlinNullPointerException
        at io.gitlab.arturbosch.detekt.api.internal.KotlinEnvironmentUtilsTest$1$1$1.invoke(KotlinEnvironmentUtilsTest.kt:18)
        at io.gitlab.arturbosch.detekt.api.internal.KotlinEnvironmentUtilsTest$1$1$1.invoke(KotlinEnvironmentUtilsTest.kt:9)

@remal
Copy link
Contributor Author

remal commented Jul 9, 2019

@arturbosch I'm afraid I didn't understand what you meant by "revert the continuous indent to 4 again". Could you please give a specific example? I'd appreciate it if you could make a comment to a specific line.

I've fixed the test. Please take a look. It seems that Application ClassLoader is not an instance of URLClassLoader in Java 9...

Copy link
Member

@schalkms schalkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test.
IMHO this PR looks fine now! Thank you for that!
We will have to wait for Artur's final review.

"Available 'report-id' values: 'txt', 'xml', 'html'. " +
"These can also be used in combination with each other " +
"e.g. '-r txt:reports/detekt.txt -r xml:reports/detekt.xml'")
"Entry should consist of: [report-id:path]. " +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole indentation in this file for example got reformatted to 8 continuous spaces instead of four.
I like the annotation parameter formatting though :).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look. Is it OK now?

https://github.com/JetBrains/kotlin/blob/master/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt
*/
@Parameter(
names = ["--classpath", "-cp"],
description = "EXPERIMENTAL: Paths where to find user class files and depending jar files. " +
"Used for type resolution."
"Used for type resolution."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this ^^

remal added 3 commits July 15, 2019 17:42
Signed-off-by: Semyon Levin <levin.semen@gmail.com>
Signed-off-by: Semyon Levin <levin.semen@gmail.com>
@remal
Copy link
Contributor Author

remal commented Jul 17, 2019

@arturbosch Please take a look

Copy link
Member

@arturbosch arturbosch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you very much!

@arturbosch arturbosch added this to the 1.0.0 milestone Jul 27, 2019
@arturbosch arturbosch merged commit 5e35fab into detekt:master Jul 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants