square / okhttp

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.
https://square.github.io/okhttp/
Apache License 2.0
45.71k stars 9.15k forks source link

Graalvm 22.2.0 fails to build mockwebserver in native image #7403

Closed avimalka closed 1 year ago

avimalka commented 2 years ago

Hi,

When trying to build mockwebserver with native image on Graal 22.2 it fails with the following:

Fatal error: java.lang.IllegalAccessError: class okhttp3.internal.graal.OkHttpFeature (in unnamed module @0x13ed066e) cannot access class com.oracle.svm.core.configure.ResourcesRegistry (in module org.graalvm.nativeimage.builder) because module org.graalvm.nativeimage.builder does not export com.oracle.svm.core.configure to unnamed module @0x13ed066e
        at okhttp3.internal.graal.OkHttpFeature.beforeAnalysis(OkHttpFeature.kt)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$9(NativeImageGenerator.java:722)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:78)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:722)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:564)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:521)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:407)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:585)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:128)

Older version works perfectly. Any workaround / solution for this ?

yschimke commented 2 years ago

Which version of OkHttp are you on? 4.10?

I can bump the version in 4.10.1? and see if that fixes this.

Thanks for reporting.

avimalka commented 2 years ago

I'm using 5.0.0-alpha.5. Should I use version 4. ? the documentation says version 5. should be used for Graalvm native-image

yschimke commented 2 years ago

Yep, can you try alpha.10? I'll check this week.

avimalka commented 2 years ago

alpha.10 has the same error

yschimke commented 2 years ago

Testing with https://github.com/square/okhttp/pull/7409

yschimke commented 2 years ago

Looks like a common problem affecting frameworks like Quarkus. Two workarounds

1) export USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM=false 2) Add options

-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jni=ALL-UNNAMED 
-J--add-exports=org.graalvm.sdk/org.graalvm.nativeimage.impl=ALL-UNNAMED 
-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED 
-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.localization=ALL-UNNAMED 
-J--add-exports=org.graalvm.nativeimage.base/com.oracle.svm.util=ALL-UNNAMED
yschimke commented 2 years ago

Raised https://github.com/oracle/graal/issues/4787 for a build failure on one target, but okcurl is building, so partially working.

yschimke commented 1 year ago

USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM seems like the current fix. We are now on 22.3.0

fniephaus commented 1 year ago

I'm afraid using USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM is only a temporary workaround as it will soon be removed. It seems OkHttpFeature.kt is still depending on internal GraalVM Native Image API:

https://github.com/square/okhttp/blob/76331ffe0024d5f8ca2f88a32c6ed3185a3ab716/okhttp/src/jvmMain/kotlin/okhttp3/internal/graal/OkHttpFeature.kt#L19-L20

Instead, the feature should use RuntimeResourceAccess and other classes of the public GraalVM SDK that we introduced in 22.3. Note that AutomaticFeature is not part of the public API. Instead, I think the feature can add itself with --feature=okhttp3.internal.graal.OkHttpFeature via its native-image.properties.

yschimke commented 1 year ago

@fniephaus I'll take a look, that's for the detailed pointers.

yschimke commented 1 year ago

Fixed in https://github.com/square/okhttp/pull/7703/files

GrapeBaBa commented 1 year ago

@yschimke I still got this issue, with graalvm 22.3.1, jdk19. okhttp 5.0.--alpha.11

yschimke commented 1 year ago

will need the next alpha release https://github.com/square/okhttp/issues/7883

Snapshots are available if you want to test, or it's really blocking you