Add type attribution for Gradle plugin DSL in KTS files#6804
Merged
Conversation
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.
shanman190
reviewed
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"; |
Contributor
There was a problem hiding this comment.
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?
Member
Author
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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. RemovedisKotlinbranching patterns from RemovePluginVisitor and updated FindPlugins to use real Gradle API types instead of synthetic stubs.Changes
Project.plugins(),Settings.plugins(),PluginDependenciesSpec.kotlin()) viadependsOn()mechanism to provide K2 compiler with type context for plugin DSL methods.isKotlinchecks and Kotlin tree import. Now uses name-based check forpluginscontainer (unambiguous in Gradle files) and MethodMatcher withmatchUnknownTypes=truefor inner calls, working consistently for both Groovy and KTS.PluginSpec,Plugin) to real Gradle API types (PluginDependenciesSpec,PluginDependencySpec) withmatchOverrides=true.Testing
All
:rewrite-gradle:testtests pass including existing Groovy and KTS plugin tests.