Fork me on GitHub
#graalvm
<
2021-04-10
>
campeterson07:04:27

Hey all. I’m trying to use test.check/generators in GraalVM and am getting the following error:

Exception in thread "main" java.io.FileNotFoundException: Could not locate clojure/test/check/generators__init.class, clojure/test/check/generators.clj or clojure/test/check/generators.cljc on classpath.
The demo app is very simple:
(ns test-check.main
  (:require
    [clojure.spec.alpha :as s]
    [clojure.spec.gen.alpha :as gen])
  (:gen-class))

(defn -main []
  (println "Hello test.check")
  (println (gen/generate (s/gen int?)))
  (println (gen/generate (s/gen nil?)))
  (println (gen/sample (s/gen string?)))
  (println (gen/sample (s/gen #{:club :diamond :heart :spade})))
  (println (gen/sample (s/gen (s/cat :k keyword? :ns (s/+ number?))))))
With the following in the project.clj
(defproject test-check "0.1.0-SNAPSHOT"

  :dependencies [[org.clojure/clojure "1.10.3"]
                 [org.clojure/test.check "1.1.0"]]

  :main test-check.main

  :profiles {:uberjar {:aot :all}
             :dev {:plugins [[lein-shell "0.5.0"]]}}

  :aliases
  {"native"
   ["shell"
    "native-image"
    "--no-fallback"
    "--no-server"
    "--report-unsupported-elements-at-runtime"
    "--initialize-at-build-time"
    "-jar" "./target/${:uberjar-name:-${:name}-${:version}-standalone.jar}"
    "-H:Name=./target/${:name}"]

   "run-native" ["shell" "./target/${:name}"]}) 
As a sanity check, I do have other successful native-image builds going, but am getting hung up on this one… Any advice?

borkdude07:04:32

@campeterson > and am getting the following error when?

borkdude07:04:44

@campeterson fwiw, clojure.test.check is also bundled with babashka and compiled with lein so you could look at that project

campeterson07:04:31

Sorry, getting that error when run the executable I built w/ native-image

campeterson07:04:21

It builds successfully, but when I run the output, that’s where it chokes

campeterson07:04:03

@borkdude I’ll take a look at babashka.

borkdude07:04:32

there is also a fix for the randomizer which doesn't work out of the box with graalvm native-image. I logged an issue about this in test.check

campeterson07:04:10

Yes, I saw your issue. Was double checking I was using the same flags

borkdude07:04:29

I think you might need to require the clojure.test.check namespaces at compile time

campeterson08:04:31

That did it! source now looks like this:

(ns test-check.main
  (:require
    [clojure.spec.alpha :as s]
    #_[clojure.spec.gen.alpha :as gen]
    )
  (:gen-class))

(require '[clojure.test.check.generators :as gen])

(defn -main []
  (println "Hello test.check")
  (println (gen/generate (s/gen int?)))
  (println (gen/generate (s/gen nil?)))
  (println (gen/sample (s/gen string?)))
  (println (gen/sample (s/gen #{:club :diamond :heart :spade})))
  (println (gen/sample (s/gen (s/cat :k keyword? :ns (s/+ number?))))))

campeterson08:04:55

Compiles fine and now the executable runs

borkdude08:04:08

But do you get random samples every new run?

campeterson08:04:07

Nope. Same samples each run

borkdude08:04:09

A workaround can be found in the link I posted earlier

👍 3
campeterson08:04:23

Thanks @borkdude this is all great stuff. I appreciate the help

campeterson08:04:24

(We love bb at work, by the way. Your open source contributions are stellar)

❤️ 3