Fork me on GitHub
#babashka
<
2020-04-05
>
mauricio.szabo00:04:02

I know Babashka aliases some namespaces by default (for example, clojure.string as str). Do you think it makes sense to add these default aliases on (clojure.core/ns-aliases *ns*)?

deep-symmetry04:04:46

Speaking of namespaces, is there any way in sci to give people access to the functions in js/Math? I am guessing not, because this is a very special case Clojurescript bridge thing.

borkdude07:04:37

@mauricio.szabo these aliases only exist in the user namespace, mostly to support oneliners

mauricio.szabo13:04:50

Really? Because I was running on a specific namespace, without an explicit import. Let me check here if I made some mistake...

borkdude07:04:41

@deep-symmetry Sometimes adding a JS class as a namespace works, but that’s kind of hacky. Jeroen was working on some improvements there, maybe you could take a look at his sci PRs

deep-symmetry01:04:47

Ah, interesting, those PRs introduced me to the :classes option that is not in the doc string for eval-string . But maybe by using that I could provide access to js/Math. I will give it a try. Thanks!

deep-symmetry01:04:01

It works, w00t! 🎉

borkdude06:04:49

Maybe it’s good to document what you did here with a small example?

deep-symmetry07:04:08

I’m not sure what you mean. I just added :classes {’Math js/Math} to my eval-string opts.

borkdude07:04:44

And how do you use functions from math?

deep-symmetry07:04:44

Well, I don’t yet, but I make it available to people drawing diagrams, and as an example in my documentation, (Math/pow 2 8) returns 128.

deep-symmetry07:04:54

(I did try drawing a two-byte cell with the result of that computation, and it showed up correctly as 0100 .)

pez10:04:38

How possible to get java.util.Calendar in? My solution for the Meetup task on Exercism calls for it! 😃

borkdude10:04:45

@pez I think using the java.time API is a better alternative?

borkdude10:04:17

we do include all of that

pez10:04:31

I'll take a look.

pez11:04:09

Hmmm, I could switch to LocalDate very easily. Thanks for that! However, babashka does not want to compile (LocalDate/of 2020 4 16). It works if I use the fully qualified name, but then clj-kondo complains about LocalDate being imported, but not used. (I'll go read the README more closely now.)

pez11:04:51

But I might have found a bug in the nREPL load-file op. It does not complain about anything being wrong in the file, even though it is not compiling it.

borkdude11:04:22

@pez?

$ bb "(import '[java.time LocalDate]) (LocalDate/of 2020 2 20)"
#object[java.time.LocalDate 0x4a625c7c "2020-02-20"]

borkdude11:04:12

about load-file, feel free to post an issue (and if you want, optionally of course a PR)

pez11:04:58

OK. So I am a noob with this. I use (:import) , am I not supposed to?

pez11:04:53

It's in the ns form, so I have to, right?

borkdude11:04:36

(ns foo (:import [java.time LocalDate]))
(LocalDate/of 2020 2 20)

pez11:04:09

Yeah, I just tried that. It works. But something in meetup.clj makes it not work...

pez11:04:54

So if I shrink my failing case down, I get

(ns lab
  (:import java.time.LocalDate))

(.. (LocalDate/of 2020 4 16) (plusDays 1))

pez11:04:38

^edited^

pez11:04:46

This does work on load-file, but I can't eval the form through nrepl.

pez11:04:45

load-file gives:

Evaluating file: lab.clj
=> #object[java.time.LocalDate 0x4944cca3 "2020-04-16"]

Mno11:04:06

Import value has to be in a vector I believe

Mno11:04:46

Or at least I've never done it like that

pez11:04:28

It might not be ideomatic, but doesn't seem to be the issue.

Mno11:04:24

I think it just returns that object because it gets the LocalDate singleton? I'm not very sure.

Mno11:04:06

But trying it like borkdude suggested might get you the result you want.

(ns foo (:import [java.time LocalDate]))
(LocalDate/of 2020 2 20)

pez11:04:24

The object is what I expect. It is the thing happening in the sceenshot that is the problem.

Mno11:04:42

I guess I don't understand, too smart for me 😅

borkdude11:04:52

@pez That syntax is invalid. Try it on JVM Clojure.

borkdude11:04:20

but bb / sci may have forgotten to warn you about it

pez11:04:24

Which syntax in invalid? JVM Clojure has had nothing but love for it so far. 😃

borkdude11:04:34

@pez sorry my bad. This is invalid though:

(ns foo (:import [java.time.LocalDate]))
(LocalDate/of 2020 2 20)

borkdude11:04:49

so using a single symbol in import isn't what bb understands apparently. issue welcome

pez11:04:05

I'm not following what is the problem. 😃

borkdude11:04:54

(ns foo (:import [java.time.LocalDate])) ;; invalid syntax in JVM Clojure
(ns foo (:import [java.time LocalDate])) ;; valid syntax
(ns foo (:import java.time.LocalDate)) ;; valid syntax, but not supported by bb / sci apparently

borkdude11:04:58

I pretty much always use the second

pez11:04:51

Oh, I had to look at it for very long to see the difference!

👍 4
pez11:04:49

But it still has the same problem as with the bare symbol. nrepl load-file works, resulting in the object. But nrepl eval fails.

pez11:04:44

So the issue seems to be with something the nrepl eval, right?

borkdude12:04:16

@pez Are you 100% sure that the syntax you've used in that issue is the same one as you used in Calva? When I load-file this with CIDER it works correctly

borkdude12:04:11

oh btw I was wrong earlier, bb also supports this:

(ns lab (:import java.time.LocalDate))
(LocalDate/of 2020 4 16)

sogaiu12:04:29

;; Monroe nREPL 0.4.0
user=> (ns lab (:import [java.time LocalDate]))
nil
user=> (LocalDate/of 2020 4 16)
clojure.lang.ExceptionInfo: Could not resolve symbol: LocalDate/of [at line 1, column 2]
user=> 

pez12:04:44

Yeah, the import syntax is not the problem.

borkdude12:04:51

maybe monroe does not send "ns" along

borkdude12:04:00

like CIDER does

pez12:04:29

Calva sends that, though.

borkdude12:04:13

so why does CIDER work? 😉

pez12:04:28

So, you can eval the form in CIDER?

borkdude12:04:46

I can reproduce the problem with lein repl :connect as well

pez12:04:47

Prove it! 😃

pez12:04:41

Mind blown.

pez12:04:37

Maybe cider loads the file differently?

borkdude12:04:58

It seems your client isn't even using the load-file op?

borkdude12:04:21

also I don't see an "ns" in sogaiu's dump: "Received" {:id "2", :op :eval, :session "8e35c0a0-039b-41d5-9166-3a15dc081ca1", :code "(LocalDate/of 2020 4 16)"}

sogaiu12:04:14

not sure what you meant, but line 10 of the paste had:

"Received" {:id "1", :op :eval, :session "8e35c0a0-039b-41d5-9166-3a15dc081ca1", :code "(ns lab (:import [java.time LocalDate]))"}

borkdude12:04:04

what I mean is that the message does not have an "ns" key

borkdude12:04:28

so the expression is evaluated inside a different ns probably

sogaiu12:04:15

thanks for the clarification.

sogaiu12:04:28

i do find it odd that the prompt in monroe is not changing from user=>

borkdude12:04:19

CIDER implements load-file with sending a message with "op" "load-file" "file" "contents of the entire file"

borkdude12:04:57

I'm afk now.

sogaiu12:04:08

when i do:

(import '[java.time LocalDate])
first, the second form succeeds.

sogaiu12:04:34

so may be it's something about the ns form as borkdude hinted

sogaiu12:04:58

(import '[java.time LocalDate])
nil
user=> (LocalDate/of 2020 4 16)
#object[java.time.LocalDate 0x6bcd0f2a "2020-04-16"]
user=> 

pez12:04:08

I've added the babashka log from when using Calva to the issue.

👍 4
pez12:04:23

Calva loads the file the same way as CIDER does, btw.

borkdude12:04:34

@pez note that in your log there is no :ns key in the received messages

borkdude12:04:17

whereas CIDER does send this

borkdude12:04:26

I thought this was something I could rely on

pez12:04:32

It should be, at least from Calva. I was dead sure I was sending that along!

borkdude12:04:27

maybe also try from CIDER and see what it prints. I'm afk

pez12:04:46

I get suicidal tendencies from using Emacs. I don't dare in this isolation 😃

pez12:04:57

I did it anyway now, and my meetup.clj code works great there.

sogaiu12:04:52

glad to hear we still have you with us 😉

pez12:04:38

I still feel like I have a purpose. 😃

sogaiu12:04:58

perhaps it can be a good thing to have a few extra handy for rainy days...

pez12:04:15

If I make Calva send the ns along, things start to work. I added that info to the issue now.

sogaiu12:04:49

it's interesting that when i use monroe in a different context, it seems to work -- just not with babashka

sogaiu12:04:09

now, can we track down where this is documented...

pez13:04:06

Since we know of many clients that do not send the ns along, maybe that is not required. What say @bozhidar?

bozhidar11:04:06

If some don’t send ns, *ns* is used by default.

bozhidar11:04:46

This will work well in REPLs, but not in source files, as normally you’d want to evaluate code in the right context.

bozhidar11:04:42

Ops, that’s a question for load-file. There I think it doesn’t matter much (especially if the file has a ns).

pez11:04:33

The question was regarding eval, actually.

bozhidar11:04:54

Ah, okay. Well, my first answer stands then. 😄

bozhidar11:04:01

Here’s the relevant code:

bozhidar11:04:04

(let [explicit-ns (and ns (-> ns symbol find-ns))
        original-ns (@session #'*ns*)
        maybe-restore-original-ns (if explicit-ns
                                    #(assoc % #'*ns* original-ns)
                                    identity)]
    (if (and ns (not explicit-ns))
      (t/send transport (response-for msg {:status #{:error :namespace-not-found :done}
                                           :ns ns}))
      (let [ctxcl (.getContextClassLoader (Thread/currentThread))

bozhidar11:04:27

As you can see we fallback to *ns* if an explicit ns is not supplied by the client.

pez12:04:55

I guess that is what @borkdude has done as well to babashka's server.

borkdude12:04:31

except that babashka creates the not-found namespace instead of sending :namespace-not-found

borkdude12:04:08

not sure what should be the behavior and if this is defined in a spec

bozhidar13:04:47

I’m off the school of thought that believes explicit is better than implicit. 🙂 Still, probably the practical implications of the different approaches are small.

borkdude13:04:12

so if you're of that school, do you want to make it explicit in some spec? 😉

borkdude13:04:38

@mauricio.szabo convinced me to create the ns because that is what Clojure itself also does when you type (in-ns 'foo)

pez14:04:56

Ouch! 😎

bozhidar14:04:28

Well, the lack of spec is on me. I’ll work on this when I can. That’s a promise. 🙂

borkdude14:04:17

If it doesn't matter much, I'll leave it like it is for now

bozhidar14:04:58

Yeah, that’s perfectly fine.

sogaiu13:04:51

fwiw, using chlorine's nrepl support, it seems to work

David Pham13:04:33

Is there a way to capture and display the output of the sh function?

borkdude15:04:17

@neo2551 Do you mean like:

bb '(:out (shell/sh "ls"))'

borkdude15:04:13

@pez Thanks for following up. I think "ns" might be optional, I'll implement a fallback, but it might be good to include anyway

David Pham16:04:32

@borkdude thanks I was not using the correct sh

borkdude16:04:11

is there another one?

David Pham20:04:53

Clojure.java.shell?

borkdude20:04:48

@neo2551 shell/sh is clojure.java.shell/sh

David Pham20:04:12

Hmm... what if the shell command has side effect to print? For example I use multiple shell command for my compilation and I don’t see the print output of the intermediary commands

borkdude20:04:59

@neo2551 For this it's better to use ProcessBuilder directly: https://github.com/borkdude/babashka/issues/299

borkdude20:04:31

the relevant line you're looking for is (.redirectOutput ProcessBuilder$Redirect/INHERIT)

Felipe Cortez21:04:24

just added babashka support to our http://docopt.org Clojure fork: https://github.com/nubank/docopt.clj#babashka 🙂

👍 4
borkdude21:04:08

@clojurians884 cool! I'll add it to the list of known working libraries 🙂

Felipe Cortez21:04:05

@borkdude nice! thanks for babashka!

borkdude09:04:38

thanks for porting it 🙂

borkdude22:04:11

@sogaiu @pez @mauricio.szabo I pushed fixes for the problems with the "ns" value in nREPL messages to master. It's now optional (so it should work with monroe again) and if the value contains a namespace that doesn't exit yet, it will be created.

👏 8
sogaiu22:04:02

i verified it works now with monroe. thanks!

sogaiu22:04:16

i tested via a build fwiw

sogaiu22:04:51

if this isn't in the nrepl docs at http://nrepl.org, may be it would be good to suggest as a potential change to @bozhidar?

borkdude22:04:20

I can release another version tomorrow

borkdude22:04:47

you can test from source with BABASHKA_DEV=true clj -A:main --nrepl-server 1667