pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.21k stars 510 forks source link

KotlinNullPointerException when run programmatically from detekt #639

Closed sschuberth closed 4 years ago

sschuberth commented 4 years ago

Also see the related https://github.com/arturbosch/detekt/issues/1803. The line

https://github.com/pinterest/ktlint/blob/b41aa2281f271d80255da727b838c0a111b6c293/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FinalNewlineRule.kt#L18

results in

Analyzing 'C:\Dev\oss-review-toolkit\spdx-utils\src\main\kotlin\SpdxLicenseException.kt' led to an exception.
The original exception message was: kotlin.KotlinNullPointerException
Running detekt '1.1.1' on Java '9.0.4+11' on OS 'Windows 10'.
If the exception message does not help, please feel free to create an issue on our github page.
java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:314)
java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:319)
java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1702)
java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1692)
java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:283)
java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1603)
java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:175)
com.pinterest.ktlint.ruleset.standard.FinalNewlineRule.visit(FinalNewlineRule.kt:18)
io.gitlab.arturbosch.detekt.formatting.FormattingRule.apply(FormattingRule.kt:62)

when running

./gradlew --rerun-tasks :spdx-utils:detekt

on the detekt-ktlint-issue branch of https://github.com/heremaps/oss-review-toolkit.

sschuberth commented 4 years ago

@Tapchicoma, would you have an idea what the underlying problem is? Is the problem that ktlint should not assume node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY) to be non-null, or is the problem that apparently the editor config data is not injected?

Tapchicoma commented 4 years ago

From what I see Ktlint.EDITOR_CONFIG_USER_DATA_KEY data should be always available in node. I will try to investigate this next week.

sschuberth commented 4 years ago

Thanks @Tapchicoma. IIRC, the private injectUserData is only called by lint and format, both of which are not called when using ktlint programmatically by detekt. However, detekt seem to insert EDITOR_CONFIG_USER_DATA_KEY explicitly, but for some reason that does not seem to work. So maybe the issue still is on the detekt side after all.

Tapchicoma commented 4 years ago

I see that editorconfigUpdater() in detekt code could return null. In this case EDITOR_CONFIG_USER_DATA_KEY will not be added to node.

sschuberth commented 4 years ago

Correct, I've meanwhile filed https://github.com/arturbosch/detekt/issues/2339 as I now believe the issue is in detekt, not in ktlint (although ktlint could and should make it more fool-proof to not run into this issue when calling ktlint programmatically by not blindly assuming EDITOR_CONFIG_USER_DATA_KEY to be present in any case).