Fork me on GitHub
#babashka
<
2021-08-16
>
Mandimby Raveloarinjaka00:08:06

Hello, I wanted to do a task that watch some files and execute some commands when they are modified. I did not find something in the babashka libraries; I noticed that there are two pods providing that feature; one implementation in rust and another one in go (this one seems broken though). Are there the only options available? I tried to implement something basic using java interop but it seems that I miss the class StandardWatchEventKinds (https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/StandardWatchEventKinds.html#ENTRY_CREATE)... I really want to avoid using another scripting language to monitor file changes: is there something I can from babashka?

lispyclouds05:08:50

Let us know what kind of errors youre facing with the pods, we would try to address them 🙂

Mandimby Raveloarinjaka02:08:48

I did not encounter an error per se with fswatcher; https://github.com/fsnotiy/fsnotify is a 404 and so I thought that the whole project was stopped... sorry I was not clear. Now I tried fswatcher; the first thing I had to do was to build the project: that was easy enough. The binary was installed in my user directory pods repository but when I tried a sample code it failed clojure.lang.ExceptionInfo: C:\Users\Mandimby\.babashka\pods\repository\org.babashka\fswatcher\0.0.1\pod-babashka-fswatcher.exe {:type :sci/error, :line 4, :column 1, :message "C:\\Users\\Mandimby\\.babashka\\pods\\repository\\org.babashka\\fswatcher\\0.0.1\\pod-babashka-fswatcher.exe", :sci.impl/callstack #object[clojure.lang.Delay 0x120e9e54 {:status :pending, :val nil}], :file "c:/data/projects/sandbox/adabox-macropad/scripts/watch_file_changes.clj", :locals {}} at sci.impl.utils$rethrow_with_location_of_node.invokeStatic (utils.cljc:71) sci.impl.evaluator$eval.invokeStatic (evaluator.cljc:329) sci.impl.interpreter$eval_form.invokeStatic (interpreter.cljc:78) sci.core$eval_form.invokeStatic (core.cljc:268) babashka.nrepl.impl.server$eval_msg$fn__33669$fn__33670.invoke (server.clj:61) ... com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine (WindowsJavaThreads.java:138) Caused by: java.nio.file.AccessDeniedException: C:\Users\Mandimby\.babashka\pods\repository\org.babashka\fswatcher\0.0.1\pod-babashka-fswatcher.exe at sun.nio.fs.WindowsFileSystemProvider.implDelete (WindowsFileSystemProvider.java:274) sun.nio.fs.AbstractFileSystemProvider.deleteIfExists (AbstractFileSystemProvider.java:110) java.nio.file.Files.deleteIfExists (Files.java:1181) java.nio.file.Files.copy (Files.java:3055) babashka.pods.impl.resolver$unzip.invokeStatic (resolver.clj:60) babashka.pods.impl.resolver$resolve$fn__31852.invoke (resolver.clj:182) clojure.core$mapv$fn__8470.invoke (core.clj:6937) ... com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine (WindowsJavaThreads.java:138) Now to give more context I am working on a side project on windows 10 (not wsl) and "native" babashka works perfectly. A pod requires a extra binary to be installed. Filewatcher only provides release binaries for macos and linux... I cannot use it as is;

borkdude06:08:01

The fswatcher pod has a binary for Windows, you don't need to compile it locally

borkdude06:08:53

0.0.2 is the latest version

borkdude06:08:54

try that one

borkdude06:08:06

Just try the examples from its README and those should work. If not, post an issue

borkdude06:08:53

> Caused by: java.nio.file.AccessDeniedException: Not sure what causes this, but I've seen a user of clj-kondo also report an AccessDeniedException, he had to change the permissions of the executable somehow. I don't know why

lispyclouds06:08:11

Im making the change in the broken link to fsnotify in the readme. thanks for spotting it

Mandimby Raveloarinjaka15:08:06

Thank you all for the swift replies

borkdude15:08:05

@mandimby.raveloarinja Cool. Is there anything that should change to the docs to make it clearer?

Mandimby Raveloarinjaka16:08:29

I am not sure, it was not obvious for me that requiring the pod will download the required binary but now testing the example script shows it clearly

Mandimby Raveloarinjaka16:08:17

Now I am not sure whether this is the case for any of the pod that expect an external bianary

Mandimby Raveloarinjaka16:08:30

I guess it might be on a case by case basis

borkdude16:08:41

It is true for any pod that you load with (load-pod <fully qualified symbol> <version>). there might still be pods around that don't have a windows version, but this can be easily fixed

borkdude05:08:53

Both the Rust and Go one should work.

borkdude05:08:31

I will post an example of how I’m using the Rust one.

borkdude07:08:34

@mandimby.raveloarinja Here am I using the Rust one to watch for asciidoc changes to rebuild the book and then trigger an update to watch it with etaoin: https://github.com/babashka/book/blob/master/script/watch.clj

Mandimby Raveloarinjaka02:08:44

Thank you for the sample code. I am running babashka on windows (not wsl) and filewatcher does not provide a native windows binary. I would have to compile it from source... that is not what I want to tackle at the moment and that is why I was hopping for something I can craft natively in babashka

borkdude06:08:05

The fswatcher pod has a Windows binary

borkdude13:08:51

Nice, I might be able to find a way to make this work with #nbb

clojure-spin 3
borkdude13:08:25

shelljs is a similar package which works with nbb already

bherrmann13:08:49

I was working on some babashka code, I found that the error messages werent leading me to my problem... for example,

(defn jimmy [ jj ]   )
(defn josh [ zy  ] (jimmy))   ;; wrong arity
(defn foo [ x ]    (josh 7))
(def handler foo)  ;; error message points here
(defn boo [ j ]  (handler) )
(boo 7)

clojure.lang.ExceptionInfo: Wrong number of args (0) passed to: user/handler
{:type :sci/error, :line 7, :column 18, :message "Wrong number of args (0) passed to: user/handler", :sci.impl/callstack #object[clojure.lang.Delay 0x128c830a {:status :pending, :val nil}], :file "/home/bob/jim.bb", :locals {j 7}}
 at sci.impl.utils$rethrow_with_location_of_node.invokeStatic (utils.cljc:71)
...
It seems like the error tracking stops once a dynamic var (handler) is in the stack. Is there a way to make the error messages better? (babashka v0.5.1)

borkdude13:08:07

are you already using clj-kondo? you would have spotted this mistake before even executing the script.

borkdude13:08:51

when invoking the script I get:

$ bb /tmp/dude.clj
----- Error --------------------------------------------------------------------
Type:     clojure.lang.ArityException
Message:  Wrong number of args (0) passed to: user/handler
Location: /tmp/dude.clj:5:18

----- Context ------------------------------------------------------------------
1: (defn jimmy [ jj ]   )
2: (defn josh [ zy  ] (jimmy))   ;; wrong arity
3: (defn foo [ x ]    (josh 7))
4: (def handler foo)
5: (defn boo [ j ]  (handler) )
                    ^--- Wrong number of args (0) passed to: user/handler
6: (boo 7)

----- Locals -------------------------------------------------------------------
j: 7

----- Stack trace --------------------------------------------------------------
user/handler - /tmp/dude.clj:4:6
user/boo     - /tmp/dude.clj:5:18
user/boo     - /tmp/dude.clj:5:1
user         - /tmp/dude.clj:6:1

borkdude13:08:56

what about this error can be improved?

borkdude13:08:32

Ah I see, you mean that with dynamic var, I thought ˆ:dynamic *foo*

bherrmann13:08:03

So this is a simplified example, and the functions are in different namespaces... I did run clj-kondo, but it didnt seem to pick up these issues. Probably worth mentioning that handler is assigned dynamically (based actually on the incoming url)

borkdude13:08:08

clj-kondo reported this one: (defn josh [ zy ] (jimmy)) ;; wrong arity, I thought you meant that one

borkdude13:08:35

yeah, I think we can improve this somehow, worth looking into

borkdude13:08:42

issue welcome

bherrmann13:08:46

I refactored some code and I hit about 20 of these issues in my code, and spent hours adding printlns to find where I messed up the arity.

borkdude13:08:16

I also want to improve this in clj-kondo itself

borkdude13:08:39

if you do (def x y) and call (x) then report as if you're calling y

bherrmann13:08:57

anything below the handler and it is like the error is saying "there is an arity error somewhere, good luck."

borkdude13:08:55

I think this might be because (def x y) assigns the value of y to x but the metadata from y doesn't apply to x

borkdude13:08:25

not sure what to do here, but worth considering

bherrmann13:08:47

Yea, I condp select which handler to use.

bherrmann13:08:59

I'm open to a hack that copies the metadata or some such...

bherrmann13:08:07

I did notice (meta handler) was nil

borkdude13:08:15

> I'm open to a hack that copies the metadata or some such... I might not, as this might affect performance somehow, but we'll see ;)

bherrmann13:08:46

oh, yes, I meant locally for me a hack. for this 1 call point.

borkdude13:08:52

this will at least give the proper name:

(defn foo [])

(def h foo)
(alter-meta! #'h (constantly (meta #'foo)))

(h 1 2 3)

Mandimby Raveloarinjaka02:08:48

I did not encounter an error per se with fswatcher; https://github.com/fsnotiy/fsnotify is a 404 and so I thought that the whole project was stopped... sorry I was not clear. Now I tried fswatcher; the first thing I had to do was to build the project: that was easy enough. The binary was installed in my user directory pods repository but when I tried a sample code it failed clojure.lang.ExceptionInfo: C:\Users\Mandimby\.babashka\pods\repository\org.babashka\fswatcher\0.0.1\pod-babashka-fswatcher.exe {:type :sci/error, :line 4, :column 1, :message "C:\\Users\\Mandimby\\.babashka\\pods\\repository\\org.babashka\\fswatcher\\0.0.1\\pod-babashka-fswatcher.exe", :sci.impl/callstack #object[clojure.lang.Delay 0x120e9e54 {:status :pending, :val nil}], :file "c:/data/projects/sandbox/adabox-macropad/scripts/watch_file_changes.clj", :locals {}} at sci.impl.utils$rethrow_with_location_of_node.invokeStatic (utils.cljc:71) sci.impl.evaluator$eval.invokeStatic (evaluator.cljc:329) sci.impl.interpreter$eval_form.invokeStatic (interpreter.cljc:78) sci.core$eval_form.invokeStatic (core.cljc:268) babashka.nrepl.impl.server$eval_msg$fn__33669$fn__33670.invoke (server.clj:61) ... com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine (WindowsJavaThreads.java:138) Caused by: java.nio.file.AccessDeniedException: C:\Users\Mandimby\.babashka\pods\repository\org.babashka\fswatcher\0.0.1\pod-babashka-fswatcher.exe at sun.nio.fs.WindowsFileSystemProvider.implDelete (WindowsFileSystemProvider.java:274) sun.nio.fs.AbstractFileSystemProvider.deleteIfExists (AbstractFileSystemProvider.java:110) java.nio.file.Files.deleteIfExists (Files.java:1181) java.nio.file.Files.copy (Files.java:3055) babashka.pods.impl.resolver$unzip.invokeStatic (resolver.clj:60) babashka.pods.impl.resolver$resolve$fn__31852.invoke (resolver.clj:182) clojure.core$mapv$fn__8470.invoke (core.clj:6937) ... com.oracle.svm.core.windows.WindowsJavaThreads.osThreadStartRoutine (WindowsJavaThreads.java:138) Now to give more context I am working on a side project on windows 10 (not wsl) and "native" babashka works perfectly. A pod requires a extra binary to be installed. Filewatcher only provides release binaries for macos and linux... I cannot use it as is;