Fork me on GitHub
#babashka
<
2020-08-19
>
Tom H.07:08:59

Hi folks, I'm having some trouble using babashka pods I'm running bb --classpath \"(clojure -Spath)\" -f src/site/build.clj (using fish shell) my deps.edn

{:deps
 {babashka/babashka.pods
  {:sha "1245cb26baf6b135abc349b3b25dbd6ca82799bc"
   :git/url ""}}}
and build.clj
(ns site.build
  (:require [babashka.pods :as pods]))

Tom H.07:08:15

I get the error:

clojure.lang.ExceptionInfo: Could not resolve symbol: cheshire/parse-string-strict [at /home/tom/.gitlibs/libs/babashka/babashka.pods/1245cb26baf6b135abc349b3b25dbd6ca82799bc/src/babashka/pods/impl.clj, line 90, column 33]

Tom H.07:08:32

I've re-reading the babashka docs now but any help would be much appreciated!

borkdude08:08:15

@tomisme You don't run babashka.pods as a library with bb, it's already included in bb itself. However, it's weird that it tries to load it from the library nonetheless. What version of bb are you running?

Tom H.08:08:25

using 0.0.88-1

Tom H.08:08:38

ahhh that makes sense 🙂

Tom H.08:08:36

I upgraded to 0.1.3-1 and bb -f src/site/build.clj is working fine, thanks!

borkdude08:08:26

0.1.3-1... hmm I never released something with that suffic, so that -1 must be coming from a specific package manager I think - but nice that it works now

Tom H.08:08:20

ahh yeah that's from Arch linux's AUR

borkdude15:08:41

Oh cool. So if you compile the main class with Clojure yourself, you can still run a babashka uberjar with the JVM:

$ cat src/foo.clj
(ns foo (:gen-class)) (defn -main [& args] (prn :hello))
$ mkdir -p classes && clojure -e "(require 'foo) (compile 'foo)"
foo
$ bb -cp $(clojure -Spath):classes -m foo --uberjar foo.jar
$ java -jar foo.jar
:hello

borkdude19:08:59

What would be a reasonable default for printing a stacktrace from bb when you have a fairly large stack? I was thinking the first 5 and last 5 items maybe?

$ lein bb -e "(defn f [_] (/ 1 0)) (require '[spartan.spec :as s]) (s/explain (s/cat :x int? :y f) [1 #{:foo}]) nil"
java.lang.ArithmeticException: Divide by zero [at line 1, column 13]
Stacktrace:
   clojure.core// - <built-in>
   user - <expr>:1:13
   spartan.spec/dt local: pred - /Users/borkdude/.gitlibs/libs/borkdude/spartan.spec/ff7317d67ec15f188afb856d37090e5c9509374e/src/spartan/spec.clj:604:5
   spartan.spec/dt - /Users/borkdude/.gitlibs/libs/borkdude/spartan.spec/ff7317d67ec15f188afb856d37090e5c9509374e/src/spartan/spec.clj:611:16
   spartan.spec/dt - /Users/borkdude/.gitlibs/libs/borkdude/spartan.spec/ff7317d67ec15f188afb856d37090e5c9509374e/src/spartan/spec.clj:602:8
...
   spartan.spec/explain-out - /Users/borkdude/.gitlibs/libs/borkdude/spartan.spec/ff7317d67ec15f188afb856d37090e5c9509374e/src/spartan/spec.clj:279:16
   spartan.spec/explain-out - /Users/borkdude/.gitlibs/libs/borkdude/spartan.spec/ff7317d67ec15f188afb856d37090e5c9509374e/src/spartan/spec.clj:269:1
   spartan.spec/explain - /Users/borkdude/.gitlibs/libs/borkdude/spartan.spec/ff7317d67ec15f188afb856d37090e5c9509374e/src/spartan/spec.clj:279:3
   spartan.spec/explain - /Users/borkdude/.gitlibs/libs/borkdude/spartan.spec/ff7317d67ec15f188afb856d37090e5c9509374e/src/spartan/spec.clj:276:1
   user - <expr>:1:54

borkdude19:08:45

And with --verbose everything

jeroenvandijk20:08:00

Think that could work. Would you also be interested in pointing the error with an arrow like in figwheel? I have done this with Sci in another context

borkdude20:08:56

I think that would be awesome :)

jeroenvandijk20:08:35

would be something like this

borkdude20:08:59

Looks good to me!

jeroenvandijk20:08:47

Do you already know where you would like to have this or shall I try to get it working first?

borkdude20:08:17

There was a request by a clj-kondo hooks users if the [at line ...] part could be made optional. I'm considering making this part of the ex-data instead of the message, so consumers can append it optionally.

borkdude20:08:41

I think the place for that would be in babashka.main clj (see branch names posted in channel)

borkdude20:08:21

There is a function error-handler or something in which currently the stacktrace is printed

borkdude20:08:42

If necessary we can add more information to the ex-data, like the file name

jeroenvandijk20:08:27

I need access to the lines where it was failing. In my solution here I parse the whole file and index the lines to get to the relevant lines. So a fully qualified path or maybe better the string of the file would work

jeroenvandijk20:08:52

it works like this

(defn error-lines
  [ex code]
  (let [data (ex-data ex)
        matching-line (dec (get-line data))
        start-line (max (- matching-line 4) 0)
        end-line (+ matching-line 6)
        [before [matching-line & after] :as snippet-lines]
        (->>
          (clojure.string/split-lines code)
          (map-indexed list)
          (drop start-line)
          (take (- end-line start-line))
          (split-at (- matching-line start-line)))]
    (concat
      (for [[line-number line] before]
        [:normal line-number line])

      [(into [:error-line] matching-line)
       [:error-message nil (str (clojure.string/join (repeat (dec (get-column data)) " "))
                                (str "^--- " (ex-message ex)))]]
      (for [[line-number line] after]
        [:normal line-number line]))))

jeroenvandijk20:08:12

I reverse engineered figwheel 🙂

borkdude20:08:14

cool. I'll past the snippet in an issue and figure it out

borkdude21:08:40

In sci namespaces.cljc we have source-fn which contains relevant code to obtain the right file.

jeroenvandijk21:08:22

Not sure about the preloads

borkdude21:08:24

That's not necessarily where the exception happens

borkdude21:08:22

The relevant bit in sci is here: https://github.com/borkdude/sci/blob/b6cd6550493a8ecea6dcf472e342861316333aac/src/sci/impl/utils.cljc#L86 Currently on exception, I'm building a stack of expression objects, each have a file, line and column

borkdude21:08:40

So we can just pull the file from that and add it to the exception info

jeroenvandijk21:08:25

Ah cool. I’ve build and tested the code with simple scenarios and it took a while before I had it right. Some expections can also not be displayed properly this way. I’ve tested it with a web app and when you have react (reagent) errors this way of reporting is not helpful

jeroenvandijk21:08:28

So I try to detect these different cases and show a different kind of error

jeroenvandijk21:08:39

> So we can just pull the file from that and add it to the exception info Sounds like a good idea

jeroenvandijk21:08:00

btw if you want to go overboard with colors 🙂 I used the flag :normal-line , :error-line and :error-message for formatting (in the browser):

jeroenvandijk21:08:21

the error line is white and the error message is red.

jeroenvandijk21:08:39

Maybe a future extension for fancy shells

borkdude21:08:54

ok, I added the :file to the ex-info

borkdude21:08:42

Yeah that's nice. Maybe for a future version, gotta save some cool things for later ;)

jeroenvandijk21:08:55

haha good marketing strategy indeed

jeroenvandijk22:08:38

Did something here https://github.com/borkdude/babashka/pull/545 There is still something missing (second case). I’m guessing file it is not always available

borkdude09:08:19

$ lein bb -e "
1
2
x
"
clojure.lang.ExceptionInfo: Could not resolve symbol: x [at <expr>, line 4, column 1]
     1:
     2: 1
     3: 2
     4: x
        ^--- Could not resolve symbol: x [at <expr>, line 4, column 1]

borkdude09:08:59

$ lein bb -e "
(defn foo [] (/ 1 0))
(foo)
"
java.lang.ArithmeticException: Divide by zero [at <expr>, line 2, column 14]
     1:
     2: (defn foo [] (/ 1 0))
                     ^--- Divide by zero [at <expr>, line 2, column 14]
     3: (foo)
Stacktrace:
   clojure.core// - <built-in>
   user/foo - <expr>:2:14
   user/foo - <expr>:2:1
   user - <expr>:3:1

borkdude20:08:04

The work on stacktraces is in babashka branch issue-508-free-st and sci branch bb-508-3-free-performance-stacktrace. Sorry for the names, been iterating on this for a while...