nebula-plugins / gradle-info-plugin

Plugin to gather information about the environment
Apache License 2.0
49 stars 20 forks source link

Add fallback in POSIXUtil when JNA fails #81

Closed kilink closed 3 years ago

kilink commented 3 years ago

The way POSIXUtil initialized the JNA Native library in a static field meant if the call failed, the class itself would fail to initialize. To handle this case more gracefully, move the JNA call to a static initializer which catches any exceptions.

Additionally, add a fallback to getHostName that shells out to /bin/hostname when JNA fails.

JNA currently does not support Apple silicon (see this PR), so attempting to build projects that use Nebula results in failures like the following:

Could not initialize class nebula.plugin.info.ci.POSIXUtil

The exception that is thrown while POSIXUtil is being initialized is as follows:

java.lang.UnsatisfiedLinkError: /private/var/folders/lq/bk0gg5n13656d33rzbhzc5cw0000gn/T/jna--791146494/jna858457035634806587.tmp: dlopen(/private/var/folders/lq/bk0gg5n13656d33rzbhzc5cw0000gn/T/jna--791146494/jna858457035634806587.tmp, 1): no suitable image found.  Did find:
    /private/var/folders/lq/bk0gg5n13656d33rzbhzc5cw0000gn/T/jna--791146494/jna858457035634806587.tmp: no matching architecture in universal wrapper
    /private/var/folders/lq/bk0gg5n13656d33rzbhzc5cw0000gn/T/jna--791146494/jna858457035634806587.tmp: no matching architecture in universal wrapper
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1950)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1832)
    at java.lang.Runtime.load0(Runtime.java:811)
    at java.lang.System.load(System.java:1088)
    at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:761)
    at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:736)
    at com.sun.jna.Native.<clinit>(Native.java:131)
    at nebula.plugin.info.ci.POSIXUtil.<clinit>(POSIXUtil.groovy:30)
coveralls commented 3 years ago

Coverage Status

Coverage decreased (-0.9%) to 48.073% when pulling bebeee3dc4975739efe27659d6417b60523b83d5 on kilink:posix-util-jna-fallback into 23db67d9e9f0bbb8d50945ca42a3c84ada60ed0e on nebula-plugins:master.

DanielThomas commented 3 years ago

We guarded that call up one level already, I've been running on an M1 Mac for the last month:

https://github.com/nebula-plugins/gradle-info-plugin/commit/9bca856bc41638d3f179200f826035f78d46c25f#diff-dd0796a4f1f7aecfa9b3f10300e6951251756a0e803b154f9fbd10e924acffbb

Released back in December, updated the tag now so it appears properly in the sidebar:

https://github.com/nebula-plugins/gradle-info-plugin/releases/tag/v9.1.2

kilink commented 3 years ago

Nice, I missed that commit somehow. Only nitpick is that the test fails for me, as hostname returns something other than localhost for me, although in practice it shouldn't matter.