This is an automated email from the ASF dual-hosted git repository.

jdaugherty pushed a commit to branch feat/indy-default-groovy6
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit e609d83c3a764a41d1367a8b7119621bde66fdcb
Author: James Daugherty <[email protected]>
AuthorDate: Thu Oct 15 00:30:36 2026 +0100

    Fail the build when an application that builds a native image turns indy off
    
    A native image needs invokedynamic: a classic call site defines a class
    at run time, which an image cannot do. Leaving indy at the compiler's
    default removed the native-image hook's indy convention, so an
    application that set grails { indy = false } built an image that failed
    on its first dynamic call.
    
    The hook is back as it was, setting the convention to true when GraalVM's
    plugin is applied, which also overrides a compile task a build script set
    to classic call sites, since the plugin applies the extension's value to
    every Groovy compile task. After evaluation it throws, with the reason, if
    the application set grails { indy = false } itself.
    GrailsNativeImageDefaultsSpec covers both, the deployment guide's AOT
    chapter documents it, and the 9.0 upgrade note mentions it.
---
 .../src/en/guide/deployment/deploymentAot.adoc     |  6 +++++
 .../src/en/guide/upgrading/upgrading90x.adoc       |  4 +++-
 .../gradle/plugin/core/GrailsGradlePlugin.groovy   | 21 ++++++++++++----
 .../core/GrailsNativeImageDefaultsSpec.groovy      | 28 ++++++++++++++++++++--
 .../test-projects/native-defaults-on/build.gradle  | 13 ++++++++++
 5 files changed, 65 insertions(+), 7 deletions(-)

diff --git a/grails-doc/src/en/guide/deployment/deploymentAot.adoc 
b/grails-doc/src/en/guide/deployment/deploymentAot.adoc
index 99c2d6bfde..f6baab709a 100644
--- a/grails-doc/src/en/guide/deployment/deploymentAot.adoc
+++ b/grails-doc/src/en/guide/deployment/deploymentAot.adoc
@@ -100,6 +100,12 @@ Abstract bean definitions — templates that exist only to 
be inherited through
 
 AOT processing on its own does not make an application ready for a native 
image. A native image additionally requires reachability metadata covering the 
resources and reflection an application performs at runtime.
 
+==== Invokedynamic in a native image
+
+A native image needs every Groovy class compiled with invokedynamic. A classic 
call site defines a class the first time it runs, which a native image cannot 
do, so an image built from such classes compiles and then fails on the first 
dynamic call that reaches one. Invokedynamic is the Groovy compiler's default, 
and Grails leaves it in place unless `grails { indy }` is set.
+
+An application that applies GraalVM's plugin compiles every Groovy class with 
invokedynamic, including a compile task its build script set to classic call 
sites. Setting `grails { indy = false }` in such an application fails the build 
with the reason, rather than building an image that fails on its first dynamic 
call.
+
 ==== Recording reflection for a native image
 
 Some of that metadata is written for you. The build reads its own output — the 
compiled artefacts, and the manifest naming each compiled page — and registers 
them, so a controller, a domain class or a GSP survives into an image without 
anything having to run.
diff --git a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc 
b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc
index 44c669689d..18661dce05 100644
--- a/grails-doc/src/en/guide/upgrading/upgrading90x.adoc
+++ b/grails-doc/src/en/guide/upgrading/upgrading90x.adoc
@@ -51,7 +51,9 @@ NOTE: Groovy 6 marks the concurrency toolkit as incubating. 
The Grails modules t
 
 Grails 8 compiles every Groovy source set of an application without 
invokedynamic (`grails { indy = false }` was the default). Grails 9 leaves the 
setting unset, so the Groovy compiler's own default applies, and on Groovy 6 
that is invokedynamic. An application that set `grails { indy = true }` can 
remove the line.
 
-An application that has to keep classic call sites, for example while it 
measures the difference, sets the property explicitly:
+An application that has to keep classic call sites, for example while it 
measures the difference, sets the property explicitly. An application that 
applies GraalVM's native image plugin cannot: the build fails with the reason, 
because an image needs invokedynamic.
+
+To keep classic call sites:
 
 [source,groovy]
 .build.gradle
diff --git 
a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy
 
b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy
index 2a44bfc95d..375362f249 100644
--- 
a/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy
+++ 
b/grails-gradle/plugins/src/main/groovy/org/grails/gradle/plugin/core/GrailsGradlePlugin.groovy
@@ -1123,15 +1123,28 @@ ${importStatements}
     /**
      * What a Grails application needs of a native image that an image cannot 
work out for itself.
      *
-     * <p>An image needs calls resolved through invokedynamic, because the 
classic call site defines
-     * a class as it runs and an image has no way to define one. That is the 
Groovy compiler's own
-     * default, which the plugin leaves in place unless the application sets 
{@code grails { indy }},
-     * so an image needs nothing from here for it.</p>
+     * <p>Applications resolve calls through invokedynamic here, because the 
classic call site
+     * defines a class as it runs and an image has no way to define one. The 
framework is built the
+     * same way. This is a convention, so it also wins over a compile task a 
build script set to
+     * classic call sites, which the plugin overwrites with the extension's 
value; an application
+     * that sets {@code grails { indy = false }} itself fails the build 
instead, with the reason,
+     * rather than building an image that fails on its first dynamic call.</p>
      *
      * <p>Nothing here runs unless the application applies GraalVM's plugin, 
which is what asks for
      * an image in the first place. An application that never builds one is 
untouched.</p>
      */
     protected void configureNativeImage(Project project) {
+        project.pluginManager.withPlugin(NATIVE_IMAGE_PLUGIN) {
+            GrailsExtension grailsExt = 
project.extensions.getByType(GrailsExtension)
+            grailsExt.indy.convention(true)
+            project.afterEvaluate {
+                if (!grailsExt.indy.get()) {
+                    throw new GradleException('A native image needs every 
Groovy class compiled with invokedynamic: ' +
+                            'a classic call site defines a class at run time, 
which a native image cannot do. ' +
+                            "Remove grails { indy = false } from a project 
that applies ${NATIVE_IMAGE_PLUGIN}.")
+                }
+            }
+        }
         configureNativeMetadataTrace(project)
     }
 
diff --git 
a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsNativeImageDefaultsSpec.groovy
 
b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsNativeImageDefaultsSpec.groovy
index cd973d2039..069036e894 100644
--- 
a/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsNativeImageDefaultsSpec.groovy
+++ 
b/grails-gradle/plugins/src/test/groovy/org/grails/gradle/plugin/core/GrailsNativeImageDefaultsSpec.groovy
@@ -19,6 +19,7 @@
 package org.grails.gradle.plugin.core
 
 import org.gradle.testkit.runner.BuildResult
+import org.gradle.testkit.runner.GradleRunner
 
 /**
  * Covers what an application gets for building a native image, and what it 
does not get for not
@@ -66,8 +67,31 @@ class GrailsNativeImageDefaultsSpec extends 
GradleSpecification {
         when:
             BuildResult result = executeTask('inspectDefaults')
 
-        then: 'a classic call site defines a class as it runs, which an image 
has no way to do, and nothing replaces the invokedynamic default'
-            result.output.contains('INDY_OPTION=unset')
+        then: 'a classic call site defines a class as it runs, which an image 
has no way to do'
+            result.output.contains('INDY_OPTION=true')
+    }
+
+    void 'an application that builds an image and turns invokedynamic off 
fails the build with the reason'() {
+        given:
+            GradleRunner runner = 
setupTestResourceProject('native-defaults-on')
+
+        when:
+            BuildResult result = runner.withArguments(['inspectDefaults', 
'-PfixtureIndy=false', '--stacktrace']).forwardOutput().buildAndFail()
+
+        then: 'rather than building an image that fails on its first dynamic 
call'
+            result.output.contains('A native image needs every Groovy class 
compiled with invokedynamic')
+            result.output.contains('Remove grails { indy = false }')
+    }
+
+    void 'a compile task a build script set to classic call sites is compiled 
with invokedynamic when the application builds an image'() {
+        given:
+            setupTestResourceProject('native-defaults-on')
+
+        when:
+            BuildResult result = executeTask('inspectDefaults', 
['-PfixtureTaskIndy=false'])
+
+        then: 'the plugin applies the invokedynamic convention to every Groovy 
compile task after the build script'
+            result.output.contains('INDY_OPTION=true')
     }
 
     void 'the pages are recorded after they are compiled, not before'() {
diff --git 
a/grails-gradle/plugins/src/test/resources/test-projects/native-defaults-on/build.gradle
 
b/grails-gradle/plugins/src/test/resources/test-projects/native-defaults-on/build.gradle
index 6f0dc5b6ee..141223e937 100644
--- 
a/grails-gradle/plugins/src/test/resources/test-projects/native-defaults-on/build.gradle
+++ 
b/grails-gradle/plugins/src/test/resources/test-projects/native-defaults-on/build.gradle
@@ -3,6 +3,19 @@ plugins {
     id 'org.graalvm.buildtools.native' version '1.1.7'
 }
 
+// -PfixtureIndy=false stands in for grails { indy = false }; 
-PfixtureTaskIndy=false for a build script
+// that sets the option on the compile tasks directly, which the grails block 
never sees
+if (providers.gradleProperty('fixtureIndy').present) {
+    grails {
+        indy = providers.gradleProperty('fixtureIndy').get().toBoolean()
+    }
+}
+if (providers.gradleProperty('fixtureTaskIndy').present) {
+    tasks.withType(GroovyCompile).configureEach {
+        groovyOptions.optimizationOptions.indy = 
providers.gradleProperty('fixtureTaskIndy').get().toBoolean()
+    }
+}
+
 tasks.register('inspectDefaults') {
     def indyOption = providers.provider {
         Map<String, Boolean> options = project.tasks.named('compileGroovy', 
GroovyCompile).get().groovyOptions.optimizationOptions

Reply via email to