micronaut-projects / micronaut-flyway

Integration between Micronaut and Flyway
Apache License 2.0
38 stars 21 forks source link

Micronaut Maven Application fail to build into native-image with flyway #571

Open donbnk opened 4 months ago

donbnk commented 4 months ago

Expected Behavior

I've generated application on Micronaut Launch page with features: data-jpa, postgres, flyway, graalvm, lombok, yaml So I have following dependencies in pom.xml:

io.micronaut.flyway micronaut-flyway compile
<dependency>
    <groupId>io.micronaut.flyway</groupId>
    <artifactId>micronaut-flyway</artifactId>
    <scope>compile</scope>
</dependency>

After running: ./mvnw clean package -Dpackaging=native-image I am expecting to have a native image build with working flyway migrations

Actual Behaviour

During build: ./mvnw clean package -Dpackaging=native-image I am encountering an error: GraalVM Native Image: Generating 'demo' (executable)...

[1/8] Initializing... (6,4s @ 0,18GB) Java version: 22+36, vendor version: GraalVM CE 22+36.1 Graal compiler: optimization level: 2, target machine: armv8-a C compiler: cc (apple, arm64, 15.0.0) Garbage collector: Serial GC (max heap size: 80% of RAM) 4 user-specific feature(s):

Fatal error: com.oracle.graal.pointsto.util.AnalysisError$ParsingError: Error encountered while parsing io.micronaut.flyway.AbstractFlywayMigration.forceRun(AbstractFlywayMigration.java:77) Parsing context: at io.micronaut.flyway.AbstractFlywayMigration.run(AbstractFlywayMigration.java:66) at io.micronaut.flyway.DataSourceMigrationRunner.lambda$onCreated$1(DataSourceMigrationRunner.java:65) at io.micronaut.flyway.DataSourceMigrationRunner$$Lambda/0x0000000e02635a40.accept(Unknown Source) at java.util.TreeMap$ValueSpliterator.tryAdvance(TreeMap.java:3276) at java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntrySetSpliterator.tryAdvance(Collections.java:1798) at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:295) at java.util.stream.SliceOps$1$1.accept(SliceOps.java:200) at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:212) at java.util.Spliterator$OfInt$$Lambda/0x0000000e022f48a8.accept(Unknown Source) at java.lang.StringUTF16$CharsSpliterator.tryAdvance(StringUTF16.java:1415) at java.util.stream.IntPipeline.forEachWithCancel(IntPipeline.java:163) at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:574) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:560) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:546) at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151) at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:265) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:702) at java.lang.constant.MethodTypeDesc.displayDescriptor(MethodTypeDesc.java:210) at java.lang.constant.DirectMethodHandleDescImpl.toString(DirectMethodHandleDescImpl.java:201) at java.lang.String.valueOf(String.java:4507) at com.oracle.svm.core.jdk.localization.substitutions.Target_java_util_Locale.initDefault(Target_java_util_Locale.java:52) at java.util.Locale.getDisplayLocale(Locale.java:1109) at java.util.Locale.getDefault(Locale.java:1093) at java.util.Formatter.(Formatter.java:2112) at java.lang.String.format(String.java:4432) at jdk.internal.util.Preconditions.outOfBoundsMessage(Preconditions.java:242) at jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.lang.StringUTF16.checkIndex(StringUTF16.java:1809) at java.lang.StringUTF16.charAt(StringUTF16.java:1605) at java.lang.String.charAt(String.java:1584) at com.oracle.svm.core.log.RealLog.charAt(RealLog.java:175) at com.oracle.svm.core.log.RealLog.rawBytes(RealLog.java:158) at com.oracle.svm.core.log.RealLog.rawString(RealLog.java:517) at com.oracle.svm.core.log.RealLog.string(RealLog.java:71) at com.oracle.svm.core.SubstrateDiagnostics.printFatalError(SubstrateDiagnostics.java:285) at com.oracle.svm.core.jdk.VMErrorSubstitutions.doShutdown(VMErrorSubstitutions.java:170) at com.oracle.svm.core.jdk.VMErrorSubstitutions.shutdown(VMErrorSubstitutions.java:148) at com.oracle.svm.core.jdk.VMErrorSubstitutions.shouldNotReachHere(VMErrorSubstitutions.java:141) at com.oracle.svm.core.jdk.Target_com_oracle_svm_core_util_VMError.shouldNotReachHere(VMErrorSubstitutions.java:90) at com.oracle.svm.core.stack.JavaStackWalker.reportUnknownFrameEncountered(JavaStackWalker.java:259) at com.oracle.svm.core.stack.StackFrameVisitor.unknownFrame(StackFrameVisitor.java:61) at com.oracle.svm.core.stack.JavaStackWalker.callUnknownFrame(JavaStackWalker.java:346) at com.oracle.svm.core.stack.JavaStackWalker.doWalk(JavaStackWalker.java:325) at com.oracle.svm.core.stack.JavaStackWalker.walkCurrentThread(JavaStackWalker.java:287) at com.oracle.svm.core.stack.JavaStackWalker.walkCurrentThread(JavaStackWalker.java:270) at com.oracle.svm.core.thread.JavaThreads.visitCurrentVirtualThreadStackFrames(JavaThreads.java:275) at com.oracle.svm.core.thread.JavaThreads.visitCurrentStackFrames(JavaThreads.java:264) at com.oracle.svm.core.jdk.Target_java_lang_Throwable.fillInStackTrace(JavaLangSubstitutions.java:282) at com.oracle.svm.core.jdk.Target_java_lang_Throwable.fillInStackTrace(JavaLangSubstitutions.java:240) at java.lang.Throwable.(Throwable.java:333) at java.lang.reflect.InvocationTargetException.(InvocationTargetException.java:68) at com.oracle.svm.core.code.FactoryThrowMethodHolder.InvocationTargetException_constructor_5bbbdce0d204a8e138bf1cf5d160092e15d7dd7e(generated:0) at com.oracle.svm.core.reflect.ReflectionAccessorHolder.invoke_0226629be8ef7c85b9c994fe3570b6c585d51097(generated:0) at static root method.(Unknown Source)

at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.util.AnalysisError.parsingError(AnalysisError.java:165)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.flow.MethodTypeFlow.createFlowsGraph(MethodTypeFlow.java:184)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.flow.MethodTypeFlow.ensureFlowsGraphCreated(MethodTypeFlow.java:152)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.flow.MethodTypeFlow.getOrCreateMethodFlowsGraphInfo(MethodTypeFlow.java:110)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.typestate.DefaultVirtualInvokeTypeFlow.onObservedUpdate(DefaultVirtualInvokeTypeFlow.java:114)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.flow.TypeFlow.update(TypeFlow.java:628)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.PointsToAnalysis$1.run(PointsToAnalysis.java:538)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.util.CompletionExecutor.executeCommand(CompletionExecutor.java:169)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.util.CompletionExecutor.lambda$executeService$0(CompletionExecutor.java:154)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.compute(ForkJoinTask.java:1726)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.compute(ForkJoinTask.java:1717)
at java.base/java.util.concurrent.ForkJoinTask$InterruptibleTask.exec(ForkJoinTask.java:1641)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:507)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1491)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:2073)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:2035)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:187)

Caused by: jdk.graal.compiler.java.BytecodeParser$BytecodeParserError: com.oracle.graal.pointsto.util.AnalysisError: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: An object of type 'io.micronaut.flyway.StaticResourceProvider' was found in the image heap. This type, however, is marked for initialization at image run time for the following reason: classes are initialized at run time by default. This is not allowed for correctness reasons: All objects that are stored in the image heap must be initialized at build time.

You now have two options to resolve this:

1) If it is intended that objects of type 'io.micronaut.flyway.StaticResourceProvider' are persisted in the image heap, add

'--initialize-at-build-time=io.micronaut.flyway.StaticResourceProvider'

to the native-image arguments. Note that initializing new types can store additional objects to the heap. It is advised to check the static fields of 'io.micronaut.flyway.StaticResourceProvider' to see if they are safe for build-time initialization, and that they do not contain any sensitive data that should not become part of the image.

2) If these objects should not be stored in the image heap, you can use

'--trace-object-instantiation=io.micronaut.flyway.StaticResourceProvider'

to find classes that instantiate these objects. Once you found such a class, you can mark it explicitly for run time initialization with

'--initialize-at-run-time=<culprit>'

to prevent the instantiation of the object.

If you are seeing this message after upgrading to a new GraalVM release, this means that some objects ended up in the image heap without their type being marked with --initialize-at-build-time. To fix this, include '--initialize-at-build-time=io.micronaut.flyway.StaticResourceProvider' in your configuration. If the classes do not originate from your code, it is advised to update all library or framework dependencies to the latest version before addressing this error.

The following detailed trace displays from which field in the code the object was reached. Object was reached by manually created constant at parsing io.micronaut.flyway.StaticResourceProvider.findStaticResourceProvider(StaticResourceProvider.java:110) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.throwParserError(BytecodeParser.java:2587) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.phases.SharedGraphBuilderPhase$SharedBytecodeParser.throwParserError(SharedGraphBuilderPhase.java:222) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.iterateBytecodesForBlock(BytecodeParser.java:3481) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.phases.SharedGraphBuilderPhase$SharedBytecodeParser.iterateBytecodesForBlock(SharedGraphBuilderPhase.java:790) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.handleBytecodeBlock(BytecodeParser.java:3433) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.processBlock(BytecodeParser.java:3275) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.build(BytecodeParser.java:1136) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.phases.SharedGraphBuilderPhase$SharedBytecodeParser.build(SharedGraphBuilderPhase.java:202) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.buildRootMethod(BytecodeParser.java:1028) at jdk.graal.compiler/jdk.graal.compiler.java.GraphBuilderPhase$Instance.run(GraphBuilderPhase.java:102) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.phases.SharedGraphBuilderPhase.run(SharedGraphBuilderPhase.java:154) at jdk.graal.compiler/jdk.graal.compiler.phases.Phase.run(Phase.java:49) at jdk.graal.compiler/jdk.graal.compiler.phases.BasePhase.apply(BasePhase.java:435) at jdk.graal.compiler/jdk.graal.compiler.phases.Phase.apply(Phase.java:42) at jdk.graal.compiler/jdk.graal.compiler.phases.Phase.apply(Phase.java:38) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.flow.AnalysisParsedGraph.parseBytecode(AnalysisParsedGraph.java:144) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.meta.AnalysisMethod.parseGraph(AnalysisMethod.java:888) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.meta.AnalysisMethod.ensureGraphParsedHelper(AnalysisMethod.java:853) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.meta.AnalysisMethod.ensureGraphParsed(AnalysisMethod.java:836) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.phases.InlineBeforeAnalysisGraphDecoder.lookupEncodedGraph(InlineBeforeAnalysisGraphDecoder.java:198) at jdk.graal.compiler/jdk.graal.compiler.replacements.PEGraphDecoder.doInline(PEGraphDecoder.java:1215) at jdk.graal.compiler/jdk.graal.compiler.replacements.PEGraphDecoder.tryInline(PEGraphDecoder.java:1198) at jdk.graal.compiler/jdk.graal.compiler.replacements.PEGraphDecoder.trySimplifyInvoke(PEGraphDecoder.java:1053) at jdk.graal.compiler/jdk.graal.compiler.replacements.PEGraphDecoder.handleInvokeWithCallTarget(PEGraphDecoder.java:1005) at jdk.graal.compiler/jdk.graal.compiler.replacements.PEGraphDecoder.handleInvoke(PEGraphDecoder.java:991) at jdk.graal.compiler/jdk.graal.compiler.nodes.GraphDecoder.processNextNode(GraphDecoder.java:930) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.phases.InlineBeforeAnalysisGraphDecoder.processNextNode(InlineBeforeAnalysisGraphDecoder.java:377) at jdk.graal.compiler/jdk.graal.compiler.nodes.GraphDecoder.decode(GraphDecoder.java:658) at jdk.graal.compiler/jdk.graal.compiler.replacements.PEGraphDecoder.decode(PEGraphDecoder.java:895) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.phases.InlineBeforeAnalysis.decodeGraph(InlineBeforeAnalysis.java:73) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.flow.MethodTypeFlowBuilder.parse(MethodTypeFlowBuilder.java:198) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.flow.MethodTypeFlowBuilder.apply(MethodTypeFlowBuilder.java:608) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.flow.MethodTypeFlow.createFlowsGraph(MethodTypeFlow.java:167) ... 15 more Caused by: com.oracle.graal.pointsto.util.AnalysisError: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: An object of type 'io.micronaut.flyway.StaticResourceProvider' was found in the image heap. This type, however, is marked for initialization at image run time for the following reason: classes are initialized at run time by default. This is not allowed for correctness reasons: All objects that are stored in the image heap must be initialized at build time.

You now have two options to resolve this:

1) If it is intended that objects of type 'io.micronaut.flyway.StaticResourceProvider' are persisted in the image heap, add

'--initialize-at-build-time=io.micronaut.flyway.StaticResourceProvider'

to the native-image arguments. Note that initializing new types can store additional objects to the heap. It is advised to check the static fields of 'io.micronaut.flyway.StaticResourceProvider' to see if they are safe for build-time initialization, and that they do not contain any sensitive data that should not become part of the image.

2) If these objects should not be stored in the image heap, you can use

'--trace-object-instantiation=io.micronaut.flyway.StaticResourceProvider'

to find classes that instantiate these objects. Once you found such a class, you can mark it explicitly for run time initialization with

'--initialize-at-run-time=<culprit>'

to prevent the instantiation of the object.

If you are seeing this message after upgrading to a new GraalVM release, this means that some objects ended up in the image heap without their type being marked with --initialize-at-build-time. To fix this, include '--initialize-at-build-time=io.micronaut.flyway.StaticResourceProvider' in your configuration. If the classes do not originate from your code, it is advised to update all library or framework dependencies to the latest version before addressing this error.

The following detailed trace displays from which field in the code the object was reached. Object was reached by manually created constant at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.util.AnalysisFuture.setException(AnalysisFuture.java:55) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:322) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.util.AnalysisFuture.ensureDone(AnalysisFuture.java:69) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.heap.ImageHeapScanner.getOrCreateImageHeapConstant(ImageHeapScanner.java:221) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.heap.SVMImageHeapScanner.getOrCreateImageHeapConstant(SVMImageHeapScanner.java:111) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.heap.ImageHeapScanner.createImageHeapConstant(ImageHeapScanner.java:197) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.meta.HostedSnippetReflectionProvider.forObject(HostedSnippetReflectionProvider.java:65) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.snippets.SubstrateGraphBuilderPlugins$42.apply(SubstrateGraphBuilderPlugins.java:1202) at jdk.graal.compiler/jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugin.execute(InvocationPlugin.java:356) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.applyInvocationPlugin(BytecodeParser.java:2327) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.phases.AnalysisGraphBuilderPhase$AnalysisBytecodeParser.applyInvocationPlugin(AnalysisGraphBuilderPhase.java:141) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.tryInvocationPlugin(BytecodeParser.java:2265) at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.phases.AnalysisGraphBuilderPhase$AnalysisBytecodeParser.tryInvocationPlugin(AnalysisGraphBuilderPhase.java:118) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.appendInvoke(BytecodeParser.java:1940) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.genInvokeStatic(BytecodeParser.java:1720) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.genInvokeStatic(BytecodeParser.java:1701) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.processBytecode(BytecodeParser.java:5468) at jdk.graal.compiler/jdk.graal.compiler.java.BytecodeParser.iterateBytecodesForBlock(BytecodeParser.java:3473) ... 45 more Caused by: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: An object of type 'io.micronaut.flyway.StaticResourceProvider' was found in the image heap. This type, however, is marked for initialization at image run time for the following reason: classes are initialized at run time by default. This is not allowed for correctness reasons: All objects that are stored in the image heap must be initialized at build time.

You now have two options to resolve this:

1) If it is intended that objects of type 'io.micronaut.flyway.StaticResourceProvider' are persisted in the image heap, add

'--initialize-at-build-time=io.micronaut.flyway.StaticResourceProvider'

to the native-image arguments. Note that initializing new types can store additional objects to the heap. It is advised to check the static fields of 'io.micronaut.flyway.StaticResourceProvider' to see if they are safe for build-time initialization, and that they do not contain any sensitive data that should not become part of the image.

2) If these objects should not be stored in the image heap, you can use

'--trace-object-instantiation=io.micronaut.flyway.StaticResourceProvider'

to find classes that instantiate these objects. Once you found such a class, you can mark it explicitly for run time initialization with

'--initialize-at-run-time=<culprit>'

to prevent the instantiation of the object.

If you are seeing this message after upgrading to a new GraalVM release, this means that some objects ended up in the image heap without their type being marked with --initialize-at-build-time. To fix this, include '--initialize-at-build-time=io.micronaut.flyway.StaticResourceProvider' in your configuration. If the classes do not originate from your code, it is advised to update all library or framework dependencies to the latest version before addressing this error.

The following detailed trace displays from which field in the code the object was reached. Object was reached by manually created constant at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.heap.ImageHeapScanner.maybeReplace(ImageHeapScanner.java:346) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.heap.ImageHeapScanner.createImageHeapObject(ImageHeapScanner.java:240) at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.heap.ImageHeapScanner.lambda$getOrCreateImageHeapConstant$2(ImageHeapScanner.java:214) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317) ... 61 more

                    5,9s (16,1% of total time) in 83 GCs | Peak RSS: 2,24GB | CPU load: 4,12

======================================================================================================================== Failed generating 'demo' after 35,7s. [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 43.167 s [INFO] Finished at: 2024-06-19T10:15:20+02:00 [INFO] ------------------------------------------------------------------------

Steps To Reproduce

  1. Generate application with features: data-jpa, postgres, flyway, graalvm, lombok, yaml
  2. start postgresql database and set datasource in application.yml to point to the postgresql database execute ./mvnw clean package -Dpackaging=native-image

Environment Information

Example Application

No response

Version

4.5.0

gjong commented 2 months ago

This is not isolated to the maven side of things. I am using Gradle for a build with the 'io.micronaut.application' plugin and am seeing the exact same issue when trying to compile to native code.

The weird thing is that the micronaut-flyway jar does seem to contain a 'native-image.properties' with the indicated fix by GraalVM. It just does not seem to get picked up.

Update: the issue seems to be introduced in GraalVM 22, I rolled back to GraalVM 21 and the compilation succeeded again.