graalvm

dekel 2022-11-18T08:01:42.228999Z

I’m trying to use timbre 6.0.1 in a GraalVM project, but I’m struggling due to a top-level random that was added (in https://github.com/ptaoussanis/timbre/pull/354) since the last working version (5.2.1). Does anyone know how to make this work, or is my best bet here to make a PR against timbre to make it avoid instantiating Random at build time?

borkdude 2022-11-18T08:26:12.680289Z

@dekelpilli Hmm, that's funny, I upgraded timbre to 6.0.1 in babashka. and I didn't get any errors about it

borkdude 2022-11-18T08:29:03.878219Z

@dekelpilli which graalvm version are you using?

dekel 2022-11-18T09:10:00.194119Z

@borkdude Yeah, I did see that babashka was using 6.0.1, which is what made me doubt my instinct to try and remove the random from timbre. I’m using graalVM 22.3.0 with Java 17. The specific project I’m trying to do this with is using https://github.com/clj-easy/graal-build-time, but I also tried to simply increase the version in https://github.com/clj-easy/graalvm-clojure/tree/master/timbre and that also broke due to the Random.

borkdude 2022-11-18T09:28:49.578129Z

I'll try locally with only the Random

borkdude 2022-11-18T09:47:07.319149Z

@dekelpilli I can reproduce this with:

(def x (java.util.Random. 1337))

(defn -main [& args]
  (prn args (.nextInt x)))
My guess is that I might not be using that specific function in bb. Let me check

borkdude 2022-11-18T09:52:47.077679Z

Ah I think bb has its own version of the log! macro

borkdude 2022-11-18T09:55:01.054179Z

Made an issue here https://github.com/ptaoussanis/timbre/issues/360

dekel 2022-11-18T09:56:15.108579Z

Ah ok, thank you for looking into that, I was quite confused about how you got it working in BB without issue

borkdude 2022-11-18T09:56:24.480909Z

I can also do a PR, but I have a meeting now. You can fix it with:

(let [rand (delay (java.util.Random. 1337))] 
  (defn determistic-rand [] (.nextDouble ^java.util.Random @rand)))

dekel 2022-11-18T09:58:49.481109Z

Yeah, although I am curious about the use of random in this instance, but that’s no longer a graalvm discussion. Thanks again!

borkdude 2022-11-18T09:59:29.331309Z

Perhaps @dergutemoritz can enlighten us why this was necessary

dergutemoritz 2022-11-18T09:59:31.619909Z

@dergutemoritz has joined the channel

dekel 2022-11-18T10:05:35.503549Z

Oh, I had dinner and forgot why the usual delay strategy likely wouldn’t work here - this is called in a macro, so any usages of that macro would happen at build time, meaning the Random would get instantiated at build time anyway.

dekel 2022-11-18T10:15:35.423879Z

Or maybe not… Maybe I need to brush up on my order of operations here, it looks like it’s fixed now in 6.0.2-SNAPSHOT

dergutemoritz 2022-11-18T10:31:20.670419Z

See https://github.com/ptaoussanis/timbre/pull/354 for rationale

dergutemoritz 2022-11-18T10:35:09.172769Z

@dekelpilli Instantiating the Random at macro-expansion time is no problem, it will not end up in the generated bytecode. What the fix guards against is that it also is instantiated at "load" time (where it's not even needed).

👍 1
dergutemoritz 2022-11-18T10:35:53.066329Z

Problem being that you can't have a def which is only usable at macro-expansion time (unless you pull in https://github.com/cgrand/macrovich)

dergutemoritz 2022-11-18T10:36:52.972259Z

Sorry for breaking graal compat, it's something that isn't on my radar so far 🙂

dergutemoritz 2022-11-18T10:37:14.125219Z

or rather: I haven't absorbed it into my habits, yet