Skip to content

Add type attribution for Gradle plugin DSL in KTS files#6804

Merged
timtebeek merged 4 commits intomainfrom
timtebeek/gradle-plugin-types
Feb 23, 2026
Merged

Add type attribution for Gradle plugin DSL in KTS files#6804
timtebeek merged 4 commits intomainfrom
timtebeek/gradle-plugin-types

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

Adds Kotlin stubs to GradleParser to provide type information for the plugins {} block in .gradle.kts files, enabling recipes to use MethodMatcher consistently for both Groovy and KTS. Removed isKotlin branching patterns from RemovePluginVisitor and updated FindPlugins to use real Gradle API types instead of synthetic stubs.

Changes

  • GradleParser.java: Added KTS stub strings defining extension functions (Project.plugins(), Settings.plugins(), PluginDependenciesSpec.kotlin()) via dependsOn() mechanism to provide K2 compiler with type context for plugin DSL methods.
  • RemovePluginVisitor.java: Removed isKotlin checks and Kotlin tree import. Now uses name-based check for plugins container (unambiguous in Gradle files) and MethodMatcher with matchUnknownTypes=true for inner calls, working consistently for both Groovy and KTS.
  • FindPlugins.java: Updated matchers from synthetic stub types (PluginSpec, Plugin) to real Gradle API types (PluginDependenciesSpec, PluginDependencySpec) with matchOverrides=true.

Testing

All :rewrite-gradle:test tests pass including existing Groovy and KTS plugin tests.

Add Kotlin stubs to GradleParser to provide PluginDependenciesSpec type information for the plugins {} block in .gradle.kts files. This enables recipes to use MethodMatcher consistently for both Groovy and KTS. Removed isKotlin branching from RemovePluginVisitor and updated FindPlugins to use real Gradle API types instead of synthetic stubs.
Use MethodMatcher for version detection in UpgradePluginVersion instead of string-based name matching. Tighten AddPluginVisitor's wildcard id matcher to use real PluginDependenciesSpec type.
Use wildcard MethodMatcher for plugins {} container detection instead of
bare name checks. A wildcard type is necessary because KTS extension
functions have a file-level declaring type rather than Project/Settings.
@timtebeek timtebeek marked this pull request as ready for review February 23, 2026 17:42
@timtebeek timtebeek requested a review from shanman190 February 23, 2026 17:42
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Feb 23, 2026
Comment on lines +39 to +55
@SuppressWarnings("LanguageMismatch")
private static final String KTS_BUILD_STUBS =
"package org.gradle.api\n" +
"import org.gradle.plugin.use.PluginDependenciesSpec\n" +
"import org.gradle.plugin.use.PluginDependencySpec\n" +
"fun Project.plugins(block: PluginDependenciesSpec.() -> Unit) {}\n" +
"fun PluginDependenciesSpec.kotlin(module: String): PluginDependencySpec = id(module)\n";

@SuppressWarnings("LanguageMismatch")
private static final String KTS_SETTINGS_STUBS =
"package org.gradle.api.initialization\n" +
"import org.gradle.plugin.use.PluginDependenciesSpec\n" +
"import org.gradle.plugin.use.PluginDependencySpec\n" +
"import org.gradle.plugin.management.PluginManagementSpec\n" +
"fun Settings.plugins(block: PluginDependenciesSpec.() -> Unit) {}\n" +
"fun Settings.pluginManagement(block: PluginManagementSpec.() -> Unit) {}\n" +
"fun PluginDependenciesSpec.kotlin(module: String): PluginDependencySpec = id(module)\n";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So there are a lot of extension functions that help with various levels of type attribution.
https://github.com/gradle/gradle/tree/master/platforms/core-configuration/kotlin-dsl/src/main/kotlin/org/gradle/kotlin/dsl

Are we thinking that we'll just add to this one by one or do we need to have something more sophisticated?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I didn't yet have a structured idea on adding more; figured the ones we have are most often used, also in our recipes. We can add others as needed, but figured keep it small for now.

@timtebeek timtebeek merged commit 5e680f8 into main Feb 23, 2026
1 check passed
@timtebeek timtebeek deleted the timtebeek/gradle-plugin-types branch February 23, 2026 18:01
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants