ben-manes / gradle-versions-plugin

Gradle plugin to discover dependency updates
Apache License 2.0
3.82k stars 199 forks source link

Kotlin DSL resolution strategy is ambiguous #808

Closed jeffdgr8 closed 10 months ago

jeffdgr8 commented 10 months ago

The Gradle Kotlin DSL example in the readme:

tasks.withType<DependencyUpdatesTask> {
  resolutionStrategy {
    componentSelection {
      all {
        if (isNonStable(candidate.version) && !isNonStable(currentVersion)) {
          reject("Release candidate")
        }
      }
    }
  }
}

doesn't work without an explicit argument name in the all() function call.

As the code is written, Android Studio resolves all { ... } to the more general all(ruleSource: Any), rather than the intended all(selectionAction: Action<in ComponentSelectionWithCurrent>).

Adding the named argument fixes the code:

tasks.withType<DependencyUpdatesTask> {
  resolutionStrategy {
    componentSelection {
      all(selectionAction = {
        if (isNonStable(candidate.version) && !isNonStable(currentVersion)) {
          reject("Release candidate")
        }
      })
    }
  }
}

This seems to be an IDE issue at its core, as the code runs successfully without the named arguments. But both Android Studio and IntelliJ IDEA both can't resolve the candidate and reject() code and traverse to the incorrect all() function when cmd+clicking.

ben-manes commented 10 months ago

yeah, it sounds like an IDE issue since I am using that fine at command-line. I guess open the issue with IDEA?

jeffdgr8 commented 10 months ago

Thanks for the quick response @ben-manes. What version of Kotlin are you using in your code? Perhaps this is a regression in Kotlin 1.9?

jeffdgr8 commented 10 months ago

Looks like you're using Kotlin 1.9.10. 🤔 Do you not see errors for this code in your IDE?

ben-manes commented 10 months ago

Gradle runs on 1.8.x until the next major release, iirc. I only know that because I get qodana warnings on 1.9 deprecations in the build files, but its not yet what Gradle runs as (and had found https://github.com/gradle/gradle/issues/25517#issue-1776410738 to understand why).

ben-manes commented 10 months ago

Looks like you're using Kotlin 1.9.10. 🤔 Do you not see errors for this code in your IDE?

I use Eclipse for development and VSCode for build files. Unfortunately IntelliJ goes into a death spiral when trying to edit kotlin build scripts so that it is faster to iterate in a text editor and cli than using their tooling. I kept trying when migrating to a kotlin build but it was unusable, not sure if that is the common experience or due to heavy build refactoring?

jeffdgr8 commented 10 months ago

Unfortunately IntelliJ goes into a death spiral when trying to edit kotlin build scripts

Interesting, I haven't experienced that myself.

I just cloned caffeine and opened in IntelliJ and I don't see the same issue there. The variables and functions resolve and cmd+clicking traverses to the correct all() overload.

I wonder what the difference could be with my project? I'm using Gradle 8.3 too. I'll open a YouTrack issue with JetBrains.

jeffdgr8 commented 10 months ago

I created a YouTrack issue: https://youtrack.jetbrains.com/issue/KTIJ-26964/Function-call-in-Kotlin-Gradle-script-resolves-to-incorrect-overload

ben-manes commented 10 months ago

Closing as acknowledged by Jetbrains as a known regression (fixed for Gradle 8.4 iiuc).