gradle / kotlin-dsl-samples

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

IDEA-15406 - Cannot navigate to kotlin-dsl sources from .kt files when using kotlin-dsl Gradle plugin in IntelliJ IDEA #514

Open stephan-gh opened 7 years ago

stephan-gh commented 7 years ago

I'm using the kotlin-dsl Gradle plugin to create a new Gradle plugin written in Kotlin together with IntelliJ IDEA. When using the kotlin-dsl extension methods in build.gradle.kts, navigation to the source works correctly. However, it does not work when using the kotlin-dsl Gradle plugin. Instead, IntelliJ IDEA navigates to the decompiled stub source file.

Using the test class below, the two methods marked with a comment do not have sources attached:

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.bundling.Jar
import org.gradle.kotlin.dsl.task
import org.gradle.kotlin.dsl.withType

class TestPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        project.run {
            // Cannot navigate to source of "withType"
            plugins.withType<JavaPlugin> {

            }

            // Cannot navigate to source of "task"
            task<Jar>("customJar") {

            }
        }
    }
}

I can provide a full test project if necessary. However, it's simple to reproduce by applying the kotlin-dsl Gradle plugin in a project and then using the test class above.

Expected Behavior

Navigation to sources should work in IntelliJ IDEA for kotlin-dsl extension methods when using the kotlin-dsl Gradle plugin to create a Gradle plugin.

Current Behavior

IntelliJ IDEA navigates to a decompiled stub source file.

eskatos commented 7 years ago

Thanks for the report @Minecrell! I confirmed this locally. Navigating to Gradle Kotlin DSL source works. Navigating to Gradle API (gradleApi()) source doesn't. We'll look into this.

stephan-gh commented 7 years ago

@eskatos Weird, it's the exact opposite for me(?). I was able to navigate to Gradle API source (e.g. the Project or Task interface) but not to the source of Gradle Kotlin DSL (e.g. additional extension it adds).

eskatos commented 7 years ago

@Minecrell 🤔 which version of IntelliJ and the Kotlin Plugin for it are you using?

stephan-gh commented 7 years ago

@eskatos IntelliJ IDEA 2017.2.5 with Gradle 4.2 (and the version of kotlin-dsl it bundles). I used "Use local Gradle distribution" if that makes any difference.

I just tried with "Use default Gradle wrapper" and the latest snapshot distribution used on the develop branch and the same happens.

For reference, the project I'm testing with is available at https://github.com/Minecrell/plugin-yml. In PlatformPlugin.kt I'm able to navigate to the source of Project or Project.getExtensions(), but not to the task<T> or plugins.withType<T> extension method from kotlin-dsl.

eskatos commented 6 years ago

I can't reproduce using Gradle 4.9 on IntelliJ IDEA 2018.1.6 nor on 2018.2.RC.

Here's a cast using the former:

kotlin-dsl-plugin-navigation-to-sources

Also validated in some buildSrc.

eskatos commented 6 years ago

Note I'm using a -bin distribution when testing.

After invalidating all IntelliJ caches, restarting, I can reproduce the issues:

kotlin-dsl-plugin-navigation-to-sources-after-invalidating-caches

For the Gradle Kotlin DSL API, sources are embedded in the jars. It seems IntelliJ is not picking up the sources from them.

For the Gradle API, it's another story and is currently enabled by using a -all distribution.

Interestingly as soon as you open a .gradle.kts file, then IntelliJ resolves the sources for the script editor and then they are available when navigating from .kt files. Somehow, IntelliJ shares the sources it recognizes from the script source-path with the Kotlin files from source sets. The gif in the previous comment was done after doing just that, that's why the navigation to sources was working. Confusing, but it provides a work around.

eskatos commented 6 years ago

IntelliJ issue for recognizing sources embedded in jars: https://youtrack.jetbrains.com/issue/IDEA-15406

eskatos commented 6 years ago

And the gradle/gradle issue for IDE plugins to provide Gradle sources: https://github.com/gradle/gradle/issues/1003

eskatos commented 6 years ago

We can't work around https://youtrack.jetbrains.com/issue/IDEA-15406 by publishing -sources jar as it would require adding a remote repository.

eskatos commented 6 years ago

To sum up:

Navigating to Gradle API sources from .kt files when the kotlin-dsl plugin is applied

Navigating to Kotlin DSL sources from .kt files when the kotlin-dsl plugin is applied

eskatos commented 6 years ago

Turning this issue into a vendor issue for IDEA-15406.