Skip to content

Add Kotlin script template support for Gradle KTS#6792

Merged
timtebeek merged 16 commits intomainfrom
timtebeek/gradle-kts-templates
Feb 23, 2026
Merged

Add Kotlin script template support for Gradle KTS#6792
timtebeek merged 16 commits intomainfrom
timtebeek/gradle-kts-templates

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

Leverage K2 FIR compiler extensions to configure implicit receivers and default imports for build.gradle.kts and settings.gradle.kts files. This improves type information for Gradle DSL methods, enabling better recipe matching and method resolution.

Changes

  • ScriptCompilerPlugin.kt — New K2 compiler plugin registering FIR extensions for script configuration
  • KotlinParser.java — Added scriptImplicitReceivers() and scriptDefaultImports() builder methods
  • GradleParser.java — Configured both KTS parsers with Project/Settings receivers and Gradle default imports
  • DefaultImportsCustomizer.java — Made DEFAULT_IMPORTS public for reuse

Test Plan

  • ✅ All existing rewrite-kotlin tests pass
  • ✅ All existing rewrite-gradle tests pass
  • ✅ Gradle KTS tests specifically verified (MigrateDependenciesToVersionCatalogKotlinDSLTest, etc.)

Leverage K2 FIR compiler extensions to configure implicit receivers and default
imports for build.gradle.kts and settings.gradle.kts files. This improves type
information for Gradle DSL methods, enabling better recipe matching and method
resolution. Implements FirScriptConfiguratorExtension and
FirScriptResolutionConfigurationExtension to provide Project/Settings as implicit
receivers and Gradle API packages as default imports.
Now that Kotlin script templates provide type information for .gradle.kts
files, merge separate GroovyVisitor/KotlinVisitor pairs into single
JavaVisitor implementations where possible.

- UpdateJavaCompatibility: replace dual GroovyIsoVisitor + KotlinIsoVisitor
  with a single JavaVisitor that handles both DSLs
- UpgradeTransitiveDependencyVersion: replace inline GroovyIsoVisitor and
  KotlinIsoVisitor literal visitors with shared ChangeStringLiteral, and
  simplify the constraints precondition check
Replace MethodMatcher-based pluginManagement/buildscript detection with
simple name matching, since the synthetic class names (RewriteSettings,
RewriteGradleProject) don't match the real types resolved by the K2
compiler (org.gradle.api.initialization.Settings, org.gradle.api.Project).
After FIR analysis, run JvmFir2IrPipelinePhase to populate the
irFile field on KotlinSource, enabling future use of IR-based
type mapping for improved Kotlin parsing.
Convert ChangeDependencyGroupId and ChangeDependencyArtifactId from
GroovyIsoVisitor to JavaIsoVisitor so they work with both Groovy and
Kotlin DSL build files.
Update MethodMatcher patterns from synthetic Groovy template types
(RewriteGradleProject, RewriteSettings, PluginSpec, Plugin) to real
Gradle API types (org.gradle.api.Project, Settings, PluginDependenciesSpec,
PluginDependencySpec) with matchOverrides=true, enabling matching for
both Groovy DSL and Kotlin DSL scripts.
@timtebeek timtebeek force-pushed the timtebeek/gradle-kts-templates branch from 74221d4 to 19540ab Compare February 23, 2026 10:53
@timtebeek timtebeek marked this pull request as ready for review February 23, 2026 14:33
@timtebeek timtebeek requested a review from shanman190 February 23, 2026 14:33
Comment on lines +16 to +19
import org.gradle.plugin.use.PluginDependenciesSpec
import org.gradle.plugin.use.PluginDependencySpec

interface PluginSpec extends PluginDependenciesSpec {
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.

Specifically for plugins we're not yet seeing the new improved types available, but I figured we can iterate on that in a subsequent PR and already get the improved types for dependencies merged early.

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.

That's likely because our local method is being preferred over the parent method as the only difference is the return type.

@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Feb 23, 2026
@timtebeek timtebeek merged commit 2fda680 into main Feb 23, 2026
1 check passed
@timtebeek timtebeek deleted the timtebeek/gradle-kts-templates branch February 23, 2026 16:02
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Feb 23, 2026
knutwannheden added a commit that referenced this pull request Feb 24, 2026
Deeply nested Py.Binary nodes (e.g., 256 implicitly concatenated
strings in asn1crypto/_teletex_codec.py) overflow Python's default
recursion limit (1000) during RPC serialization. Double it to 2000.
macsux pushed a commit that referenced this pull request Feb 27, 2026
Deeply nested Py.Binary nodes (e.g., 256 implicitly concatenated
strings in asn1crypto/_teletex_codec.py) overflow Python's default
recursion limit (1000) during RPC serialization. Double it to 2000.
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