Fork me on GitHub
#babashka
<
2023-09-07
>
weavejester18:09:17

I have a function that works in Clojure, but doesn't in Babashka because the classloader is different. I'm wondering what would be an idiomatic way of solving this:

(defn- resources [path]
  (let [cl (.. Thread currentThread getContextClassLoader)]
    (enumeration-seq (.getResources cl path))))

4
borkdude18:09:40

There is a solution for this, let me find it

weavejester18:09:47

Thanks! I noticed there was a class-loader in babashka.impl.classpath, but that sounds like an internal namespace.

borkdude18:09:52

This works in bb currently:

bb -e '(map str (.getURLs (clojure.lang.RT/baseLoader)))'

borkdude18:09:25

also this works, if you are willing to write bb-specific code:

(babashka.classpath/split-classpath (babashka.classpath/get-classpath))

weavejester18:09:51

Perfect, thanks!

borkdude18:09:19

When running code in bb, the following is done:

(binding [clojure.lang.Compiler/LOADER @cp/the-url-loader] ...)
to make the above interop possible. not sure if any other bindings make sense.

borkdude18:09:03

Perhaps running setContextClassLoader() with the "internal" classloader would make your original interop possible

weavejester18:09:00

Using baseLoader works. I did vaguely recall that there was some reason I used the thread context class loader, but I can't remember exactly why.