I'm struggling to remember (or find in the docs) how to run a main fn with arguments in-process in a bb task. I know I can have :task lazytest.main/-main to run main in process with no args...
in-process?
i.e., with babashka rather than a shell process
(binding [*command-line-args* ...] (lazytest.main/-main))
or (apply ...) if the main function just uses the fn args instead of the command line varI was overthinking it 🙂
or just (-main ...)
Am I holding it wrong?
35: (def test:bb (binding [
36: babashka.tasks/*task* '{:name test:bb, :extra-paths ["dev" "test"], :extra-deps {io.github.noahtheduke/lazytest {:mvn/version "2.0.0"}}, :task (do (require 'lazytest.main) (lazytest.main/-main "-n" "org.corfield.rephrase-test"))}]
37: nil
38: (do (require 'lazytest.main) (lazytest.main/-main "-n" "org.corfield.rephrase-test")))) test:bb
^--- Unable to resolve symbol: lazytest.main/-maintest:bb {:extra-paths ["dev" "test"]
:extra-deps {io.github.noahtheduke/lazytest {:mvn/version "2.0.0"}}
:task (do
(require 'lazytest.main)
(lazytest.main/-main "-n" "org.corfield.rephrase-test"))}this has to do with the whats-it-called problem that technomancy has on his blog.
use :requires ([...]) to add the require in there
If I have :task lazytest.main/-main it "runs" (but fails because it loads a ns that I can't test in bb)
gilardi scenario?
yes
Okay, now I guess I have a Q for @nbtheduke -- that command-line is loading other test nses. I only want that one.
:requires ([lazytest.main :as lazytest])
test:bb {:extra-paths ["dev" "test"]
:extra-deps {io.github.noahtheduke/lazytest {:mvn/version "2.0.0"}}
:requires [[lazytest.main]]
:task (lazytest.main/-main "-n" "org.corfield.rephrase-test")}Ah, that command doesn't work with the JVM version either... it also loads a test ns it shouldn't.
maybe your test-ns loads that ns accidentally?
No, it's lazytest loading it.
moving the thread to #lazytest
Ah, it loads all nses, but only runs the specified one. Ugh! Okay, I'll need reader conditionals here I suspect...
or use the cognitect test runner for bb, which doesn't load all nses I believe
or fix lazytest
why is it even called lazytest if it eagerly loads stuff 🥁
The tests use LazyTest terminology, not clojure.test 🙂
I guess you could mock the conflicting ns with a .bb file
instead of using reader conditionals
Back to a bb question with this now:
3: (ns org.corfield.rephrase
4: (:require [clojure.main :refer [ex-triage]]
^--- ex-triage does not exist
Babashka's clojure.main doesn't have that fn?currently not apparently
issue welcome and I'll add it
It requires copying in several helper functions so it's quite a bit of code...
I created the issue. I'll hold off my experiments now until that's done -- whenever you get to it, absolutely no rush.
you can work around it by interning a fake var in that namespace
Well, the tests won't run with that so I wouldn't get much further, unfortunately. And ex-triage pulls in a bunch of helper functions that may or may not run on bb.
Q about Babashka and nREPL: does bb support any nREPL middleware?
I'm thinking the answer is "no" or "yes, but very limited". It doesn't seem to have the wrap-caught middleware in babashka.nrepl and nrepl/nrepl itself (which does have that middleware) doesn't load on bb (due to use of definline).
Context: I was wondering if I could get my rephrase library running on bb... I suspect I can get the exception rephrasing ns running but not the nrepl (middleware) ns?
babashka nrepl does not yet support custom middleware. it's on my TODO list and hope to get to it towards the end of the year
Thanks @borkdude I'll revisit testing rephrase with bb after that then. Although I may still add testing the non-middleware part now...
Something in me hopes that I can make a big jump in bb compatibility by just replacing SCI etc with https://github.com/borkdude/cream : that would make big swaths of existing clojure compatible without changing anything. But currently crema isn't as fast as SCI yet...
Cream is pretty neat, and it does seem like the future. What would that mean for SCI, Scittle, Joyride, etc?
• for SCI on bb: replacement hopefully, eventually • for SCI as a sandbox: nothing • for scittle, joyride, etc.: nothing
sandboxing: perhaps crema will also offer some sandboxing, so that would then also replace SCI on JVM, but for JS nothing changes
I could be (and probably am) wrong, but it seems like there's at least some support for middleware, because a really ad-hoc mess:
user=> (defn hi [handler] (println "here's some stuff") handler)
#'user/hi
user=> (babashka.nrepl.server/start-server! {:xform hi})
will print to the bb console.
My guess would be that maybe the middleware has the same/similar limitations to general clojure code being run in babashkaYeah, babashka.nrepl has some middleware (in the babashka.nrepl.middleware ns) but it doesn't have wrap-caught which is what rephrase depends on.
So, I guess the real question here would be: could a version of wrap-caught be added to babashka.nrepl.middleware so that rephrase could work with bb's nREPL server? Although it would also require set-descriptor! and the topo-sorting that is based on middleware dependencies... It would be non-trivial, I suspect.
Ah, it does have the topo-sort but no set-descriptor! so metadata would have to be added "manually"...
Hmm, I suspect this stuff is too dynamic to work with bb? https://github.com/nrepl/nrepl/blob/master/src/clojure/nrepl/middleware/caught.clj
I'll dig into this some more over the next few days. It looks like I could write it as simpler middleware without wrap-caught, and either use reader conditionals or a separate ns for Babashka. I'll have to give it a bit more thought.
Hmm, this is where rephrase would need to hook in and replace some of the error reporting... https://github.com/babashka/babashka.nrepl/blob/master/src/babashka/nrepl/impl/utils.clj#L22 ...that's essentially a hard-coded version that doesn't have a hook to rephrase the exception, so I'm not sure this can work.
I'm very much just poking and guessing, but tracing it back, send-exception is called by send-reduce which is used as an arg to the xform from the opts to nrepl-server, so maybe there's a way to wedge something in there. I doubt this'd be a desirable/robust approach, but send-exception gets called if the response has an :ex key, so maybe it'd be possible to grab the exc earlier and replace it in the response? (I haven't considered any larger context/ramifications of that, just sort of thinking outloud)