Fork me on GitHub

Interesting this, when I do something simple in a shutdown hook (say (p/process "ls")), things go fine, but if I try to run (p/process "docker compose down") , then the bb task says

Error while executing task: dev
and the exit code is 130


You should probably wait for the process to end?


aa, might be, did do deref though on the process object, but have to check again


interesting that same happens with say

( "sleep" "1")
but not with zero sleep
( "sleep" "0")
would think getting the output of the process would block maybe just have to be real quick inside the shutdownhook 🙂


maybe you can make a small repro so I can have a look?


I can try 🙂


betting that there is something else in my project setup that causes this...


0% cat bb.edn
 {shutdown-hook {:requires ([ :refer [sh]])
                 :task (do
                         (.addShutdownHook (Runtime/getRuntime) (Thread. (fn []
                                                                           (println "hook running")
                                                                           (sh "sleep" "10")
                                                                           (println "hook done"))))
                         (shell "clojure"))}}}
kimmoko@paju ~/work/babashka-stuff
0% cat deps.edn
{:deps {nrepl/nrepl                {:mvn/version "1.0.0"}
        cider/cider-nrepl          {:mvn/version "0.28.6"}
        com.bhauman/rebel-readline {:mvn/version "0.1.4"}}
 :main-opts ["-m" "nrepl.cmdline"
             "--middleware" "[cider.nrepl/cider-middleware]"
             "--repl-fn" "rebel-readline.main/-main"]}
kimmoko@paju ~/work/babashka-stuff
0% bb run shutdown-hook
Clojure 1.11.1
user=> ^D
hook running
^Chook done
kimmoko@paju ~/work/babashka-stuff
0% echo $?
kimmoko@paju ~/work/babashka-stuff
0% bb run shutdown-hook
Clojure 1.11.1
user=> ^Chook running
Error while executing task: shutdown-hook
^Chook done
kimmoko@paju ~/work/babashka-stuff
0% echo $?
kimmoko@paju ~/work/babashka-stuff
0% bb run shutdown-hook
Clojure 1.11.1
user=> ^D
hook running
hook done
kimmoko@paju ~/work/babashka-stuff
0% echo $?
kimmoko@paju ~/work/babashka-stuff
0% bb run shutdown-hook
Clojure 1.11.1
user=> ^Chook running
Error while executing task: shutdown-hook
hook done
kimmoko@paju ~/work/babashka-stuff
0% echo $?
not quite same, but probably related to how rebel-readline behaves with sigint in the above the first CTRL^C are without waiting for the sleep in the hook to end, then more patient


but this not big deal, I bet there are bigger fishes elsewhere 😄


The fix:

 {shutdown-hook {:requires ([ :refer [sh]])
                 :task (do
                          (Runtime/getRuntime) (Thread. (fn []
                                                          (println "hook running")
                                                          (sh "sleep" "10")
                                                          (println "hook done"))))
                         (shell {:continue true} "clojure"))}}}


So use {:continue true} to not throw when the exit code is zero


Seems to work great, thank you! 🙂


The command line is where programming cultures intersect :)


command line, the great bridge pipe

Matthew Twomey22:02:23

Babasjka / emacs question: In my setup I’d like to be editing babashka scripts (``) in various directories. These scripts are stand-alone and have no edn or any accompanying files. In emacs / cider - I can’t seem to open a babashka repl because it’s not detecting a project. I’m trying to work out a command (so I can map my own key) to open the babashka repl, but I can’t figure out how to do this. Right now I’m trying: (cider-jack-in '(:jack-in-cmd "/opt/homebrew/bin/bb nrepl-server")) This doesn’t seem to work because the jack-in-cmd gets overridden when no project is detected. Anyone have any ideas here?


You can create a bb.edn file to make it a project


You can also just start the nREPL server in a terminal and then connect from emacs

Matthew Twomey22:02:48

Yes, and this is what I have been doing - but I’m trying to avoid littering my filesystem with unnecessary EDNs. I’d like it to be just like when I use a shell script with no additional files.

Matthew Twomey22:02:28

When I start the repl in the terminal - that works, but it’s not integrated into emacs.

Matthew Twomey22:02:32

It’s more of a cider / emacs question really I think. But since it’s babashka, I started here.


Use cider-connect to the nREPL server you start in the terminal. Don't use jack-in if you don't have a project, I guess


I'm talking about an nREPL server. not about a console REPL where you type expressions in your terminal.


This is documented in the link I pasted above.

Matthew Twomey22:02:55

Yes - I can do that. I’m just trying to do it in emacs.

Matthew Twomey22:02:24

Those docs work, I’m just trying to figure out how to do it in emacs using the elisp cider functions.

Matthew Twomey22:02:03

But I can create a new function specifically for this - there is just a lot of functionality already there and I thought maybe I was missing something.

Matthew Twomey22:02:27

Right now, it appears that cider doesn’t really handle “no project detected” in a flexible way. It forces the default jack-in command at that point.

👍 2

ah - maybe @U02CV2P4J6S knows more about this

Matthew Twomey22:02:41

kk - thanks for the quick chat, will dig further.

Benjamin07:02:41 providing jack in cmd doesn't work right now but I made a pull request that probably soon is merged. With this you can either use a param like you want to, or use a dir locals. For the moment you could checkout the branch or you could make a bb.edn with an empty map in every dir you want to start bb nrepl as a workaround @U0250GGJGAE


another workaround:

;; the equivalent of the proposed change
 (defun cider-dont-update-jack-in-cmd-when-given (params)
   (when (plist-get params :jack-in-cmd) params)))

;; now a nbb jack in command becomes:
(cider-jack-in '(:jack-in-cmd "nbb nrepl-server"))


I have this in my config (stolen from corgi) for a quick jack-in to a babashka file

;; jack-in for babashka from corgi
(defun cider-jack-in-babashka (&optional project-dir)
  "Start a utility CIDER REPL backed by Babashka, not related to a specific project."
  (let ((project-dir (or project-dir (project-root (project-current t)))))
     "bb --nrepl-server 0"
     (lambda (server-buffer)
        (list :repl-buffer server-buffer
              :repl-type 'clj
              :host (plist-get nrepl-endpoint :host)
              :port (plist-get nrepl-endpoint :port)
              :project-dir project-dir
              :session-name "babashka"
              :repl-init-function (lambda ()
                                    (setq-local cljr-suppress-no-project-warning t
                                                cljr-suppress-middleware-warnings t)
                                    (rename-buffer "*babashka-repl*"))))))))


Nice! Perhaps something for the babashka wiki

dakra14:02:42 Apparently I made this entry 2021 already 😄


I guess that was before there was "official" babashka support in cider


well, it's only 2 years until we have this conversation again then :)

Matthew Twomey15:02:18

Nice! Thanks @U02CV2P4J6S, @UFAP0C8KU - exactly what I’m looking for.

🎉 6

(If you are living on the latest master cider branch, e.g. with the straight package manager, there is also the yet-unreleased command you might want consider. It uses emacs numerical arguments (M-1 M-x cider-jack-in-universal, M-2 ... etc) to quickly launch one of clojure cli, lein, bb or nbb nREPLs outside of a project)

🎉 2