Fork me on GitHub
#babashka
<
2022-11-11
>
sohalt13:11:42

Hi! On NixOS I get

1: (babashka.process/exec ["echo" "foo"])
   ^--- exec is not supported in non-GraalVM environments
when running bb -e '(babashka.process/exec ["echo" "foo"])'. Any ideas why? As far as I understand it, the check and error message is only supposed to show up when using babaska.process as a JVM dependency from Clojure.

borkdude13:11:06

I'm not sure how the nixos binary is compiled. Can you try the binary that babashka distributes via github releases?

borkdude13:11:14

the linux static binary should work in nix as well

sohalt13:11:27

Yep, the static binary works.

sohalt13:11:06

But it's probably out of scope for you to debug. I'll try to find out what's going on.

borkdude13:11:47

does it use the regular graalvm ce version?

borkdude13:11:57

probably @UKFSJSM38 or @UFDRD93RR would know more

borkdude13:11:41

the check that is done to enable exec is here: https://github.com/babashka/process/blob/be047624edac65c3c648adace291bba38cd1a933/src/babashka/process.cljc#L510 it checks at compile time if the graalvm class is available

borkdude14:11:09

so if the uberjar is built using openjdk / non-graalvm it will still fail probably. it's recommended to build both the uberjar and native-image using the same graalvm

sohalt14:11:01

Ah, that probably explains it. It uses the babashka-standalone jar from the github releases.

borkdude14:11:33

hmm, the uberjar is made using graalvm though

borkdude14:11:04

at least, I would expect that

borkdude14:11:03

well maybe not and that could be the issue

borkdude14:11:34

that is probably the issue...

openjdk version "11.0.15" 2022-04-19
OpenJDK Runtime Environment 18.9 (build 11.0.15+10)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.15+10, mixed mode, sharing)

borkdude14:11:02

I'll fix that

borkdude14:11:12

can you test with an uberjar from CI later?

borkdude14:11:47

@U0303ET1R2T Yes, this was it. old:

$ $GRAALVM_HOME/bin/java -classpath "$(clojure -Spath):babashka-1.0.165-standalone.jar"  clojure.main -e "(require '[babashka.process]) (babashka.process/exec [\"ls\"])"
Execution error (ExceptionInfo) at babashka.process/exec (process.cljc:542).
exec is not supported in non-GraalVM environments
New:
$ $GRAALVM_HOME/bin/java -classpath "$(clojure -Spath):babashka-1.0.166-SNAPSHOT-standalone.jar"  clojure.main -e "(require '[babashka.process]) (babashka.process/exec [\"ls\"])"
Execution error (Error) at org.graalvm.nativeimage.impl.ImageSingletonsSupport/checkInstalled (ImageSingletonsSupport.java:69).
The class ImageSingletons can only be used when building native images, i.e., when using the native-image command.
The new error is what I would expect since I'm not running with native image here, but if you compile it to native with this uberjar, exec should work

borkdude14:11:16

Can you test this locally?

sohalt16:11:33

Yep, works! Thanks a lot!

pavlosmelissinos15:11:56

I've found this useful (it has clojure.core/swap! semantics but applies to files):

(defn swap-file!
  "Swaps the contents of file to be:
  (apply f current-contents-of-file args). f should be free of side effects.
  Returns the value that was swapped in."
  [file f & xs]
  (let [old-val (slurp file)
        new-val (apply f old-val xs)]
    (spit file new-val)
    new-val))
if there's any interest I could raise a PR for babashka/fs...

😍 6
pavlosmelissinos21:11:44

https://github.com/babashka/fs/pull/77 I'm not so sure about the name (or the trailing ! since no other function in babashka.fs uses it), swap-file! kind of resembles swap! but replace-file might be more intuitive and update-file more familiar? :thinking_face: Happy to change any part of it, just let me know on Github/here (it's a bit late here though so I might not see it until tomorrow)...

borkdude22:11:10

hm yeah, I like update-something

🆗 1
1
borkdude22:11:25

Will take a look tomorrow, thanks!

🙂 1
Crispin13:11:56

maybe it shouldn't be called swap-file! as it's not free of race conditions (another thread/process can write between the slurp and the spit). It could be made thread safe though with some file locking (but I'm unsure how portable such file locking can be made. Im only familiar with fcntl style locking on posix)

borkdude13:11:43

it's now called update-file in the PR

👍 1
pavlosmelissinos15:11:27

> another thread/process can write between the slurp and the spit That's true @U1QQJJK89! However, a file is a shared resource that is not really owned by the JVM so I'm not sure how easy it is to prevent external processes from accessing it (especially across platforms) 😞 I think it can be made thread-safe (within the JVM process) with clojure.core/locking on the file but then the test becomes a bit weird (I have to add a Thread/sleep and a couple of futures) and it causes other problems, e.g. potential deadlock https://github.com/babashka/fs/pull/77#issuecomment-1312514409

borkdude16:11:38

In clj-kondo I have some inter-process locking stuff, but I think that's going too far for this function. The 99.9% scripting case is covered here

👍 1
borkdude16:11:20

we can just add to the docstring that it isn't thread-safe, but I think the docstring already reflects that

Crispin05:11:23

Also f doesn't need to be free of side effects here, as it is only ever run once. Could remove that part of the docstring.

pavlosmelissinos07:11:08

Hmm, f should just be a function that processes a string. That's not to say you can't have side effects in it at all, it's just weird. The PR was merged (thanks @U04V15CAJ!) but go ahead and suggest a fix if you think the docstring is misleading 🙂.

Crispin07:11:20

It's ok, was just noting. In swap! functions need to be free of side effects because they can be rerun multiple times. This being only run once you are free to add a side effect without problems. Maybe you want to increment a counter on how many times the file has been updated. You could do this in your function here if you wanted to without ill effect.

👍 1
Crispin07:11:39

Its not a big deal. If borkdudes happy with it that's ok with me.

borkdude07:11:06

Yes, I wanted to change that too, it can have side effects :)

borkdude07:11:22

PR welcome in case I forget

borkdude15:11:12

I've been using this pattern over and over myself too .. I like it :)

🙂 1