Fork me on GitHub
#babashka
<
2021-12-07
>
Michaël Salihi09:12:34

Hi, is it possible to use fs/delete in conjunction with fs/glob ?

borkdude09:12:21

indeed :)

👍 1
borkdude14:12:01

O nice, clojure.core.specs.alpha also works with babashka (via spartan.spec).

user=> (require 'spartan.spec)
nil
user=> (require '[clojure.spec.alpha :as s])
nil
user=> (require '[clojure.core.specs.alpha :as cs])
nil
user=> (s/conform ::cs/defn-args '[foo [x y z]])
{:fn-name foo, :fn-tail [:arity-1 {:params {:params [[:local-symbol x] [:local-symbol y] [:local-symbol z]]}, :body [:body []]}]}

borkdude15:12:06

I was discovering this while trying to run better-cond with babashka which uses these core specs.

borkdude15:12:33

Work in progress. I was amazed at how little changes clojure.spec needs nowadays for bb:

(require '[babashka.deps :as deps])

(deps/add-deps '{:deps {org.babashka/spec.alpha {:git/url ""
                                                 :git/sha "cf136c8e6e83554b4d3077821e76725adc772cff"}}})

(require '[clojure.spec.alpha :as s])
(prn (s/valid? int? 1))

(require '[clojure.spec.gen.alpha :as gen])
(prn (gen/sample (s/gen int?)))
true
(0 0 0 3 2 -1 3 28 14 0)

awesome 2
borkdude15:12:11

(s/def ::name (s/and string? (complement str/blank?)))
(s/def ::street (s/and string? (complement str/blank?)))
(s/def ::street-number int?)
(s/def ::address (s/keys :req-un [::street ::street-number]))
(s/def ::contact (s/keys :req-un [::name ::address]))
(prn (gen/sample (s/gen ::contact)))
({:name "4d", :address {:street "k", :street-number 0}} ...)
:-)

FiVo16:12:43

Hey, is there an "official" bb docker image with also tools-deps installed?

borkdude16:12:19

you mean. like in, already the tools jar installed for downloading dependencies?

borkdude16:12:47

I think what you usually want to do is download those deps during the image build

FiVo16:12:12

I mean something like clojure:openjdk-11-tools-deps as base image with bb

FiVo22:12:09

nice, very much in favor of the approach mk mentions

👍 1
borkdude17:12:26

I don't think this exists. I would just use the base image that is most convenient for you and then install bb during your Dockerfile build. Or use a multi-state and copy from an existing one.

👍 1
thompen19:12:19

how do I handle situations where I have a function that receives a parameter that's a number, which I want to invoke both from code and command line? when passing the parameter through command line invocation, I get an error since it's received as a string. If I do Integer/parseInt, then it fails when it's passed a number. (-> p str Integer/parseInt) seems to work for both number and strings, but wondering if there's something simpler.

borkdude19:12:54

@thomascpendergrast perhaps write a wrapper for the command line which calls the internal function

borkdude19:12:49

since 0.6.8 babashka also has the clojure.core/parse-long function

borkdude21:12:58

Now also got instrumentation working:

$ bb -cp src/main/clojure -e "
(require '[clojure.spec.alpha :as s] '[clojure.spec.test.alpha :as st])
(defn foo [x] x)
(s/fdef foo :args (s/cat :x int?))
(st/instrument 'user/foo)
(-> (try (foo :dude) (catch Exception e (ex-data e))) :clojure.spec.alpha/args)
"
(:dude)

cldwalker14:12:28

Hi. Thanks for pushing on spec compatibility! With your work I was able to bring specmonstah to bb compatibility - https://github.com/reifyhealth/specmonstah/compare/develop...cldwalker:bb-support?expand=1 . I did find an instrument edge case that still fails - https://github.com/cldwalker/specmonstah/blob/d8db0be0259b11d6667dcfe20a94225d361c59b6/test/reifyhealth/specmonstah/core_test.cljc#L13-L17 . You can repro on that branch with bb test

cldwalker14:12:37

I don’t have an urgency to use instrumentation here but figured I’d share in case you’re interested

borkdude14:12:01

@U08ALHZ2N I think I just fixed that one on babashka/spec.alpha master

borkdude14:12:21

but it does require the bb from master now - you can download from #babashka-circleci-builds

borkdude14:12:55

I would appreciate another test round before I release the next bb :)

cldwalker14:12:16

With [email protected] I still see the same failure e.g.

ERROR in (test-spec-gen) (/Users/me/code/world/specmonstah/test/reifyhealth/specmonstah/spec_gen_test.cljc:43)
Uncaught exception, not in assertion.
expected: nil
  actual: java.lang.IllegalArgumentException: No matching method applyTo found taking 1 args for class sci.impl.fns$fun$arity_3__7291
 at sci.impl.Reflector.invokeMatchingMethod (Reflector.java:132)
    sci.impl.interop$invoke_instance_method.invokeStatic (interop.cljc:28)
    sci.impl.evaluator$eval_instance_method_invocation.invokeStatic (evaluator.cljc:209)
    sci.impl.analyzer$expand_dot$fn__8708.invoke (analyzer.cljc:723)
    sci.impl.evaluator$eval.invokeStatic (evaluator.cljc:334)
    sci.impl.evaluator$eval.invoke (evaluator.cljc:328)
    sci.impl.evaluator$eval_try.invokeStatic (evaluator.cljc:150)
    sci.impl.analyzer$analyze_try$fn__8664.invoke (analyzer.cljc:616)
    sci.impl.evaluator$eval.invokeStatic (evaluator.cljc:334)
    sci.impl.evaluator$eval.invoke (evaluator.cljc:328)

borkdude14:12:33

are you using that babashka spec alpha commit?

borkdude14:12:00

you need to use both masters :)

cldwalker14:12:18

Ah. I’d missed that update. Works like a charm. Thanks!

borkdude14:12:46

all tests passing? :)

cldwalker15:12:21

With this spec.alpha fork, is there a way to fix clojure.lang.ExceptionInfo: Could not find namespace: clojure.core.specs.alpha? I encountered this while trying to use better-cond with latest masters. I see the remove-clojure hack in https://github.com/babashka/babashka/issues/743 but that seems kinda gross

borkdude15:12:42

babashka applies this gross hack itself because the clojure spec alpha wasn't compatible with bb. but the core specs are. you can get those by doing:

bb -cp $(clojure -Spath)
instead. Thanks for reminding me, I will remove this hack for the core specs. Not for spec itself.

borkdude15:12:41

This is what's in babashka:

(defn add-deps
  "Takes deps edn map and optionally a map with :aliases (seq of
  keywords) which will used to calculate classpath. The classpath is
  then used to resolve dependencies in babashka."
  ([deps-map] (add-deps deps-map nil))
  ([deps-map {:keys [:aliases :env :extra-env :force]}]
   (when-let [paths (:paths deps-map)]
     (cp/add-classpath (str/join cp/path-sep paths)))
   (when-let [deps-map (not-empty (dissoc deps-map :paths :tasks :raw :min-bb-version))]
     (let [deps-map (assoc-in deps-map [:aliases :org.babashka/defaults]
                              '{:replace-paths [] ;; babashka sets paths manually
                                :classpath-overrides {org.clojure/clojure ""
                                                      org.clojure/spec.alpha ""
                                                      org.clojure/core.specs.alpha ""}})
           args (list "-Srepro" ;; do not include deps.edn from user config
                      "-Spath" "-Sdeps" (str deps-map)
                      "-Sdeps-file" "") ;; we reset deps file so the local deps.edn isn't used
           args (if force (cons "-Sforce" args) args)
           args (concat args [(str "-A:" (str/join ":" (cons ":org.babashka/defaults" aliases)))])
           cp (with-out-str (binding [deps/*env* env
                                      deps/*extra-env* extra-env]
                              (apply deps/-main args)))
           cp (str/trim cp)
           cp (str/replace cp (re-pattern (str cp/path-sep "+$")) "")]
       (cp/add-classpath cp)))))

borkdude15:12:56

We remove clojure, because we don't need clojure on the classpath for babashka

borkdude15:12:19

And we probably don't always want the core specs on the classpath, unless it's explicitly provided

cldwalker15:12:32

With ~/Downloads/bb -cp $(clojure -Spath) test, I encounter Could not resolve symbol: clojure.lang.Compiler/demunge. I’m guessing this is because it picks up the original spec.alpha. Is there a way to use the forked spec.alpha with core specs?

borkdude15:12:54

yes, with -Sdeps ...

borkdude15:12:39

like this:

$ rlwrap bb -cp $(clojure -Spath -Sdeps '{:deps {borkdude/spartan.spec {:git/url ""
                       :sha "12947185b4f8b8ff8ee3bc0f19c98dbde54d4c90"}}}')
but then with babashka/spec.alpha

cldwalker15:12:02

Ah gotcha. ~/Downloads/bb -cp $(clojure -Spath -Sdeps '{:deps {org.babashka/spec.alpha {:git/url "" :git/sha "4b76776ca84403555dc03cbf7fc285ebfe3907ec"}}}') test does work. I’ll probably avoid using better-cond for now as it’s not worth making specmonstah folks use this workaround on every bb invocation

borkdude15:12:14

I actually want a better solution for this, but I don't know how

borkdude15:12:53

I did this to basically ignore the user deps.edn file

borkdude15:12:02

which pulls in clojure + spec + core specs

borkdude15:12:56

perhaps I can hack something in deps.clj

borkdude16:12:36

clojure.tools.deps.alpha.script.make-classpath2 stubbornly adds clojure itself to the deps

borkdude16:12:54

@U08ALHZ2N What I could do is: do not remove these deps if you explicitly mention core.specs in your deps map

borkdude16:12:19

@U08ALHZ2N what do you think?

[email protected] /tmp/test $ clojure -M:babashka/dev -e "(babashka.classpath/get-classpath)"
"/Users/borkdude/.m2/repository/org/clojure/core.specs.alpha/0.2.62/core.specs.alpha-0.2.62.jar"
[email protected] /tmp/test $ cat bb.edn
{:deps {org.clojure/core.specs.alpha {:mvn/version "RELEASE"}}}

borkdude16:12:24

I pushed that to master now

cldwalker16:12:55

Great idea. Tried with latest master and org.clojure/core.specs.alpha {:mvn/version "0.2.62"} and better-cond works great with passing tests. Thanks again!

cldwalker16:12:20

If there’s somewhere you’d like core.specs.alpha usage documented happy to submit a PR later

borkdude16:12:35

Maybe we should add a section about clojure.spec to the babashka book

borkdude16:12:22

I also added a message in the output about using babashka/spec.alpha when you require clojure.spec.alpha and it isn't there

cldwalker16:12:00

A spec section in the book makes sense

borkdude16:12:10

$ bb -e "(require '[clojure.spec.alpha])"
[babashka] WARNING: Use the babashka-compatible version of clojure.spec.alpha, available here: 
----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Could not find namespace: clojure.spec.alpha.
Location: 1:1

borkdude17:12:05

I can add a similar message for the core specs

cldwalker19:12:14

Great. That’s helpful

borkdude19:12:57

done on master

awesome 1
borkdude19:12:10

@U08ALHZ2N When I try to run reifyhealth.specmonstah.core-test with babashka, I get an error about clojure.lang.Sorted - does that ring a bell?

borkdude19:12:34

Ah right, you're working on a PR

borkdude19:12:42

I'll leave this out of the CI tests for now then

cldwalker19:12:15

Yeah. It’s on a branch. When it gets merged on specmonstah I can make a note to add it to babashka’s CI tests