Fork me on GitHub
#babashka
<
2023-02-06
>
viesti11:02:36

Interesting this https://github.com/babashka/babashka/blob/10638685549205926489ac325721261c301819d4/src/babashka/impl/sigint_handler.clj#L15, 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

borkdude11:02:43

You should probably wait for the process to end?

viesti11:02:38

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

viesti14:02:25

interesting that same happens with say

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

borkdude14:02:41

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

viesti14:02:57

I can try 🙂

viesti14:02:49

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

viesti14:02:06

0% cat bb.edn
{:tasks
 {shutdown-hook {:requires ([clojure.java.shell :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]"
             "--interactive"
             "--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 $?
0
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 $?
130
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 $?
0
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 $?
130
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

viesti14:02:55

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

borkdude14:02:56

The fix:

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

borkdude14:02:08

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

viesti20:02:23

Seems to work great, thank you! 🙂

borkdude18:02:10

The command line is where programming cultures intersect :) https://twitter.com/charmcli/status/1622657411633885190

lispyclouds20:02:42

command line, the great bridge pipe

Matthew Twomey22:02:23

Babasjka / emacs question: In my setup I’d like to be editing babashka scripts (`http://script.bb`) 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?

borkdude22:02:34

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

borkdude22:02:48

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.

borkdude22:02:42

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

borkdude22:02:23

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

borkdude22:02:39

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
borkdude22:02:18

ah - maybe @U02CV2P4J6S knows more about this

Matthew Twomey22:02:41

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

Benjamin07:02:41

https://github.com/clojure-emacs/cider/pull/3286 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

Benjamin08:02:48

another workaround:

;; the equivalent of the proposed change
(advice-add
 'cider--update-jack-in-cmd
 :before-until
 (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"))

dakra14:02:52

I have this in my config (stolen from corgi) for a quick jack-in to a babashka file https://github.com/dakra/dmacs/blob/632864dacd60d61c948a657d703eddcb761cd9f7/init.org?plain=1#L5745-L5765

;; 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."
  (interactive)
  (let ((project-dir (or project-dir (project-root (project-current t)))))
    (nrepl-start-server-process
     project-dir
     "bb --nrepl-server 0"
     (lambda (server-buffer)
       (cider-nrepl-connect
        (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*"))))))))

borkdude14:02:43

Nice! Perhaps something for the babashka wiki

dakra14:02:42

https://github.com/babashka/babashka/wiki/GNU-Emacs Apparently I made this entry 2021 already 😄

dakra14:02:16

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

borkdude15:02:29

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
chaos07:02:20

(If you are living on the latest master cider branch, e.g. with the straight package manager, there is also the yet-unreleased https://github.com/clojure-emacs/cider/blob/master/doc/modules/ROOT/pages/basics/up_and_running.adoc#Universal-jack-in 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