gradle / kotlin-dsl-samples

Samples builds using the Gradle Kotlin DSL
https://gradle.org/kotlin/
Other
3.71k stars 433 forks source link

KT-22101 IntellIj suggests unused import for `org.gradle.kotlin.dsl.getValue` when using `kotlin-dsl` and doesn't suggest import for `creating` #564

Open mkobit opened 7 years ago

mkobit commented 7 years ago

When trying to use the domain object extension methods getting and creating in a plugin when using kotlin-dsl it leads to less than ideal behavior.

Moving pictures speak a thousand words:

Expected Behavior

Current Behavior

Context

Using kotlin-dsl in a plugin I am writing

Steps to Reproduce (for bugs)

plugins {
  `kotlin-dsl`
  `java-library`
  `java-gradle-plugin`
}

In src/main/kotlin/ExampleKotlinDslPlugin.kt

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.creating

open class ExampleKotlinDslPlugin : Plugin<Project> {
  override fun apply(project: Project) {
    project.run {
      val myTask by tasks.creating
    }
  }
}

Your Environment

Gradle 4.3-rc-2

IntelliJ IDEA 2017.2.5 Build #IU-172.4343.14, built on September 26, 2017 Licensed to Mike Kobit You have a perpetual fallback license for this version Subscription is active until September 5, 2018 JRE: 1.8.0_152-release-915-b12 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Linux 4.10.0-37-generic

eskatos commented 6 years ago

Thanks for the well written report @mkobit! This looks like an IntelliJ issue to me, related to how delegated properties are handled. We need to find/fill an upstream issue. Ideally reproducing the faulty behavior with a simple Kotlin project not involving the Gradle Kotlin DSL.

One workaround would be to import org.gradle.kotlin.dsl.*

mkobit commented 6 years ago

Good call with the star import (although I try not to use them) - definitely makes sense to report upstream as this is most likely an IntelliJ issue - I reported it here first due to it being the first and primary place I ran into it.

I'll hopefully report it tonight, but if not in the next few days and follow up on this issue.

mkobit commented 6 years ago

Finally reported upstream at https://youtrack.jetbrains.com/issue/KT-22101 - the issue could use a friendlier description that isn't as much kotlin-dsl but I did not write it yet.

eskatos commented 6 years ago

From @big-guy

If we're going to ask people to use Kotlin in buildSrc, I think this is important.

The issue understates what happens in practice. The import is marked as unused, IntelliJ optimizes the imports and removes it, the build then fails. If you add the import back and save, IntelliJ removes it again. The only way to get it to not do this is to fiddle with Intellij defaults.

@big-guy, I don't recall this happening to me. Do you have Optimize imports on the fly enabled? I believe it's not the default.

image

mkobit commented 6 years ago

This also happens when using by registering in a plugin which requires import org.gradle.kotlin.dsl.provideDelegate or import import org.gradle.kotlin.dsl.* as previously suggested.