micronaut-projects / micronaut-picocli

Integration between Micronaut and PicoCLI
Apache License 2.0
21 stars 12 forks source link

Cannot generate working native-image for sample CLI app #351

Closed dkowis closed 9 months ago

dkowis commented 10 months ago

Expected Behavior

I should be able to run a default app without having to change any thing. something seems to be missing with the KSP change, since all the docs reference annotation-processor

Actual Behaviour

sadly it shows an error the same as an old bug: #23

~/gitwork on 🅰 (us-west-2)
❯ git clone git@github.com:dkowis/demo.git                                                                                                                                                          (base)
Cloning into 'demo'...
remote: Enumerating objects: 32, done.
remote: Counting objects: 100% (32/32), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 32 (delta 0), reused 32 (delta 0), pack-reused 0
Receiving objects: 100% (32/32), 65.15 KiB | 913.00 KiB/s, done.

~/gitwork on 🅰 (us-west-2)
❯ cd demo                                                                                                                                                                                           (base)

~/g/demo on  master via 🅶 v8.4 via ☕ v21.0.1 on 🅰 (us-west-2)
❯ rtx use java@graalvm-community-17                                                                                                                                                                 (base)
rtx ~/gitwork/demo/.rtx.toml java@graalvm-community-17

~/g/demo on  master [?] via 🅶 v8.4 via ☕ v17.0.9 on 🅰 (us-west-2)
❯ ./gradlew nativeRun                                                                                                                                                                               (base)

> Task :generateResourcesConfigFile
[native-image-plugin] Resources configuration written into /home/dkowis/gitwork/demo/build/native/generated/generateResourcesConfigFile/resource-config.json

> Task :nativeCompile
[native-image-plugin] GraalVM Toolchain detection is disabled
[native-image-plugin] GraalVM location read from environment variable: JAVA_HOME
[native-image-plugin] Native Image executable path: /home/dkowis/.local/share/rtx/installs/java/graalvm-community-17.0.9/lib/svm/bin/native-image
========================================================================================================================
GraalVM Native Image: Generating 'demo' (executable)...
========================================================================================================================
For detailed information and explanations on the build output, visit:
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
------------------------------------------------------------------------------------------------------------------------
[1/8] Initializing...                                                                                    (2.7s @ 0.28GB)
 Java version: 17.0.9+9, vendor version: GraalVM CE 17.0.9+9.1
 Graal compiler: optimization level: 2, target machine: x86-64-v3
 C compiler: gcc (linux, x86_64, 11.4.0)
 Garbage collector: Serial GC (max heap size: 80% of RAM)
 1 user-specific feature(s)
 - io.micronaut.core.io.service.ServiceLoaderFeature
[2/8] Performing analysis...  [*******]                                                                 (12.7s @ 1.34GB)
  13,385 (89.17%) of 15,011 types reachable
  20,217 (65.77%) of 30,739 fields reachable
  64,481 (59.72%) of 107,968 methods reachable
   4,425 types,   318 fields, and 3,860 methods registered for reflection
      63 types,    68 fields, and    55 methods registered for JNI access
       4 native libraries: dl, pthread, rt, z
[3/8] Building universe...                                                                               (2.5s @ 1.56GB)
[4/8] Parsing methods...      [*]                                                                        (1.3s @ 2.08GB)
[5/8] Inlining methods...     [***]                                                                      (0.6s @ 1.45GB)
[6/8] Compiling methods...    [***]                                                                      (9.2s @ 2.78GB)
[7/8] Layouting methods...    [**]                                                                       (3.1s @ 3.58GB)
[8/8] Creating image...       [**]                                                                       (4.0s @ 2.14GB)
  31.32MB (50.02%) for code area:    41,887 compilation units
  28.14MB (44.93%) for image heap:  321,049 objects and 263 resources
   3.16MB ( 5.05%) for other data
  62.62MB in total
------------------------------------------------------------------------------------------------------------------------
Top 10 origins of code area:                                Top 10 object types in image heap:
  12.39MB java.base                                            6.61MB byte[] for code metadata
   4.48MB kotlin-reflect-1.9.21.jar                            3.54MB java.lang.Class
   3.23MB java.xml                                             3.05MB byte[] for java.lang.String
   1.35MB svm.jar (Native Image)                               3.02MB java.lang.String
   1.26MB micronaut-serde-support-2.4.0.jar                    2.61MB byte[] for general heap data
 946.73kB picocli-4.7.5.jar                                    1.12MB com.oracle.svm.core.hub.DynamicHubCompanion
 890.95kB micronaut-inject-4.2.2.jar                         842.78kB byte[] for embedded resources
 572.49kB micronaut-http-4.2.2.jar                           838.76kB byte[] for reflection metadata
 562.53kB jackson-core-2.15.3.jar                            622.70kB java.lang.String[]
 557.04kB logback-core-1.4.13.jar                            535.03kB java.util.HashMap$Node
   4.87MB for 42 more packages                                 5.27MB for 2908 more object types
------------------------------------------------------------------------------------------------------------------------
Recommendations:
 HEAP: Set max heap for improved and more predictable memory usage.
 CPU:  Enable more CPU features with '-march=native' for improved performance.
------------------------------------------------------------------------------------------------------------------------
                        2.2s (5.9% of total time) in 62 GCs | Peak RSS: 4.75GB | CPU load: 15.80
------------------------------------------------------------------------------------------------------------------------
Produced artifacts:
 /home/dkowis/gitwork/demo/build/native/nativeCompile/demo (executable)
========================================================================================================================
Finished generating 'demo' in 36.9s.
[native-image-plugin] Native Image written to: /home/dkowis/gitwork/demo/build/native/nativeCompile

> Task :nativeRun
16:08:07.088 [main] INFO  i.m.c.DefaultApplicationContext$RuntimeConfiguredEnvironment - Established active environments: [cli]
picocli.CommandLine$InitializationException: Cannot instantiate com.example.DemoCommand: the class has no constructor
        at picocli.CommandLine$DefaultFactory.create(CommandLine.java:5685)
        at picocli.CommandLine$Model$CommandUserObject.getInstance(CommandLine.java:12267)
        at picocli.CommandLine$Model$CommandSpec.userObject(CommandLine.java:6437)
        at picocli.CommandLine$Interpreter.clear(CommandLine.java:13524)
        at picocli.CommandLine$Interpreter.parse(CommandLine.java:13570)
        at picocli.CommandLine$Interpreter.parse(CommandLine.java:13559)
        at picocli.CommandLine$Interpreter.parse(CommandLine.java:13454)
        at picocli.CommandLine.parseArgs(CommandLine.java:1552)
        at picocli.CommandLine.execute(CommandLine.java:2169)
        at io.micronaut.configuration.picocli.PicocliRunner.run(PicocliRunner.java:137)
        at io.micronaut.configuration.picocli.PicocliRunner.run(PicocliRunner.java:114)
        at com.example.DemoCommand$Companion.main(DemoCommand.kt:27)
        at com.example.DemoCommand.main(DemoCommand.kt)
Caused by: java.lang.NoSuchMethodException: com.example.DemoCommand.<init>()
        at java.base@17.0.9/java.lang.Class.checkMethod(DynamicHub.java:1038)
        at java.base@17.0.9/java.lang.Class.getConstructor0(DynamicHub.java:1204)
        at java.base@17.0.9/java.lang.Class.getDeclaredConstructor(DynamicHub.java:2754)
        at picocli.CommandLine$DefaultFactory.create(CommandLine.java:5656)
        at io.micronaut.configuration.picocli.MicronautFactory.create(MicronautFactory.java:74)
        at picocli.CommandLine$DefaultFactory.create(CommandLine.java:5683)
        ... 12 more

BUILD SUCCESSFUL in 45s
8 actionable tasks: 8 executed

Steps To Reproduce

  1. create new project from launch using Kotlin graalvm and CLI app
  2. clone that repo and run ./gradlew nativeRun
  3. watch it fail

Environment Information

linux graalvm 17 gradle kts

Example Application

https://github.com/dkowis/micronaut-picocli-kotlin-demo

Version

4.2.2

dkowis commented 10 months ago

maybe the same problem as this issue https://github.com/micronaut-projects/micronaut-starter/issues/2007

which references this issue : https://github.com/remkop/picocli/issues/1564

it seems that KSP is not a supported annotation processor.

sdelamo commented 10 months ago

@dkowis thanks for reporting. Micronaut Picocli with Kotlin will only work if you use KAPT until Picocli supports KSP.

@wetted can you create a documentation PR in this repository and close this issue? Create also a starter PR with a validator for KSP + picocli

dkowis commented 10 months ago

Of note, the launch.micronaut.io generates KSP stuff with KSP, not KAPT

Also, this is the upstream issue keeping it from happening: https://github.com/remkop/picocli/issues/1564