Fork me on GitHub
#babashka
<
2020-10-11
>
pez09:10:01

How do I distinguish a babashka session as being run as a script from it being evaluated in an repl session?

sogaiu09:10:04

$ bb
Babashka v0.2.2-SNAPSHOT REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> *file*
"<repl>"

pez09:10:59

Thanks! Yes, I can use that.

🙂 3
pez09:10:29

Doesn't seem like Calva plays very well with babashka, though 😞 I think I remember it working pretty well...

sogaiu09:10:12

hmm, what isn't working? i just tried with some success... started an nrepl server in a terminal (`bb --nrepl-server`), connected to the appropriate port, and evaluated a form

sogaiu09:10:27

this is with the test vsix that was posted yesterday

borkdude09:10:00

When I select: connect to a running REPL not in project > generic, Calva doesn't ask me for more info. It did a while back

sogaiu09:10:18

i guess i chose "connect to a running repl in..."

sogaiu09:10:33

but "not in project" does make more sense

sogaiu09:10:13

i tried with "not in project", generic, and got a prompt

borkdude09:10:32

yeah, in project did work for me (I'm on the normal Calva plugin from the marketplace)

sogaiu09:10:00

ah, may be that's the difference (the version of calva)

borkdude09:10:01

Seems to work fine otherwise.

sogaiu09:10:48

oh interesting

sogaiu09:10:00

if i start vscode and choose "not in project" first, i don't get asked

sogaiu09:10:09

if i invoke with "in project" i do

borkdude09:10:21

Let's start a thread.

borkdude09:10:59

Anyway, seems like it's a Calva issue then.

sogaiu09:10:10

subsequent attempt of "not in project" leads to a prompt

pez09:10:57

I think there is a bug with "Not in project”. It is not the problem for me though. I can connect, but Calva relies heavily on the info op, which doesn't seem to be implemented.

borkdude09:10:11

Here are all the implemented ops: https://github.com/babashka/babashka.nrepl/blob/master/src/babashka/nrepl/impl/server.clj#L179 They are also returned in describe, a proper client should handle that ;)

borkdude09:10:39

The bb nrepl implements most of the ops listed here: https://nrepl.org/nrepl/ops.html I don't see an info op and have no clue what it does.

pez09:10:59

Yeah, Calva should handle the describe op.

pez09:10:09

info is a cider-nrepl op, I think.

borkdude09:10:42

If you can make an issue with a link to some docs, it might get implemented

pez09:10:30

I'll do that. Calva feels pretty broken w/o info.

borkdude09:10:56

It might be some new op

borkdude09:10:21

What are you using these ops for anyway? Could you give me some clue at least?

borkdude09:10:23

like, input + output

pez10:10:14

I'll be happy providing any clues you need of course. 😃

borkdude10:10:34

Thanks. I responded in the issue :) Maybe you could past a few example conversations of input + outputs

borkdude10:10:52

I think you can do this in emacs using some cider setting where it logs this to a buffer

borkdude10:10:56

Or maybe you could bug Bug for some docs :)

😛 3
pez10:10:51

I think it's time to implement some tracing in Calva...

pez10:10:01

I added some info on info to the issue now.

pez10:10:42

Added one more example.

borkdude10:10:45

Thanks. Not sure when this will be implemented and by who, so meanwhile, if you want to support bb, it's best to work with what's returned in describe

borkdude10:10:59

It does have el-doc support

borkdude10:10:27

Some other editors just eval something custom like clojure.repl/doc etc

pez10:10:03

Yeah. I probably should implement using el-doc directly. babashka is the first repl I try to support where I can't just inject cider-nrepl. However, info provides url-paths to the files, as well as line-numbers.

borkdude10:10:10

for bb you can eval (meta (resolve 'foo/bar)) which should give you :file, :line etc

borkdude10:10:36

if the info op is not present in describe that is

pez10:10:09

For some reason I want to use .bb as file extension. But babashka seems to want me to use .clj. Is that so?

borkdude10:10:03

On the classpath it searches in the order of .bb / .clj / .cljc

pez10:10:56

Let me try again then... So, when I have a file bin/foo.bb with (ns foo) and start bb like:

BABASHKA_DEV=true bb --classpath bin --nrepl-server 1337

borkdude10:10:36

then you should be able to (require 'foo) yes

pez10:10:16

Then babashka reports, current ns user

pez10:10:02

But if I rename the file to foo.clj I get current ns foo.

pez11:10:12

And it's not only that reporting. My symbols in the file get defined in foo as well. 😃

borkdude11:10:52

foo.clj:

(ns foo)

(prn :foo)
(prn (ns-name *ns*))
borkdude@MBP2019 ~ $ lein repl :connect 1337
Connecting to nREPL at 127.0.0.1:1337
user=> (require 'foo)
:foo
foo
nil
Same with http://foo.bb. What's unexpected here?

borkdude11:10:04

@pez Please use a thread. I don't know how I can repro your problem. Please give more details.

borkdude11:10:24

I can then try to repro it using lein repl

pez11:10:58

Give me a sec

pez11:10:49

So I don't seem to be able to repro it using the bb prompt.

pez11:10:13

But with lein repl :connect I can.

pez11:10:23

No... wait

pez11:10:56

Well, somewhat I can repro it that way. The ns in my case is pimp-my-ride. The file starts with a bb shabang w/o the --classpath (b/c I just learned about that). Then (ns pimp-my-ride). I start the server like so:

BABASHKA_DEV=true bb --classpath bin --nrepl-server 1337
Then connect and do in-ns:
$ lein repl :connect localhost:1337
Connecting to nREPL at localhost:1337
clojure.lang.ExceptionInfo: Could not resolve symbol: nrepl.core/version
user=> (in-ns 'pimp-my-ride)
nil
pimp-my-ride=> 
babaska prints a huge blob of things but at the end of this is:
Reading! fff519ed-2763-4b2b-88b0-4f04e318b025 0
"Received" {:code "", :id "8d7c713b-4fe9-46b0-9b3f-e19b2afa7b56", :op :eval, :session "f8fc70b8-5f5a-4739-93b6-fb8b07f735ee"}
current ns user
Reading! fff519ed-2763-4b2b-88b0-4f04e318b025 0
"Received" {:code "(in-ns 'pimp-my-ride)", :id "f7074c63-304d-4acc-9db9-96ef59cc0b27", :op :eval, :session "f8fc70b8-5f5a-4739-93b6-fb8b07f735ee"}
current ns user
Reading! fff519ed-2763-4b2b-88b0-4f04e318b025 0

pez11:10:26

Then again, I get the same output using .clj as extension... Hmmm.

borkdude11:10:45

Can you explain what the problem is you're trying to repro. I think I missed the point

pez11:10:21

Yeah, the problem is that when I use the load-file op, then babashka doesn't switch namespace when the file ends with .bb.

borkdude11:10:56

load-file should never switch namespace

pez11:10:45

Well, it does if the file ends with .clj.

borkdude11:10:58

No it doesn't?

user=> (load-file "/tmp/bin/foo.clj")
:foo
foo
{:line 6, :column 1, :end-line 6, :end-column 14, :name foo, :ns #object[sci.impl.vars.SciNamespace 0x4b13fc2c "foo"], :file "/tmp/bin/foo.clj", :arglists ([])}
nil
user=>

pez11:10:59

And also, that is how other nrepl servers behave. So, the symbols in the file gets defined in the ns specified in the (ns ...) form.

pez11:10:54

Let me try that.

borkdude11:10:00

I have the feeling we're talking past each other.

borkdude11:10:06

Are you referring to clojure.core/load-file or the nREPL load-file op

pez11:10:16

Not sure how to communicate this. 😃 But in any case. I get the behaviour I expect when the file is a .clj file, but not with .bb.

pez11:10:36

Let's keep trying to reach each other. Haha.

borkdude11:10:22

That's weird then. The op doesn't specify an ns switch though: https://nrepl.org/nrepl/ops.html#load-file and clojure.core/load-file also doesn't switch to that ns

borkdude11:10:42

it's best if you explicitly set the ns in the eval op

borkdude11:10:58

It may be a bug, please write a report with a clear repro. Just not sure if it is.

pez11:10:09

Maybe this error is in Calva...

pez11:10:01

No, I don't think it is Calva. Check this.

Reading! 0b694590-8bc8-42e8-9602-16acd712430b 0
"Received" {:op :load-file, :session "6e4ea24d-0df5-4184-bac4-3a994f02cee6", :file "#!/usr/bin/env bb\n(ns foo)\n\n(def bar \"baz\")\n", :id "29", :file-name "", :file-path "/Users/pez/Projects/liberty/cospaia.se/bin/foo.bb"}
current ns user
Reading! 0b694590-8bc8-42e8-9602-16acd712430b 0
"Received" {:id "30", :op :eval, :session "6e4ea24d-0df5-4184-bac4-3a994f02cee6", :code "(in-ns 'foo)", :pprint 0}
current ns foo
Reading! 0b694590-8bc8-42e8-9602-16acd712430b 0
"Received" {:op :load-file, :session "6e4ea24d-0df5-4184-bac4-3a994f02cee6", :file "#!/usr/bin/env bb\n(ns foo)\n\n(def bar \"baz\")\n", :id "31", :file-name "foo.clj", :file-path "/Users/pez/Projects/liberty/cospaia.se/bin/foo.clj"}
current ns foo

pez11:10:41

The file contents:

#!/usr/bin/env bb
(ns foo)

(def bar "baz")

borkdude11:10:32

You still haven't explained to me what error. I do see a difference, but what's the error? Problem statement?

pez11:10:21

So this is the problem.

pez11:10:02

While it gets defined correctly in the .clj file.

borkdude11:10:18

That's pretty weird yes.

borkdude11:10:34

Now it's the question if this is a problem in the client or server right?

pez11:10:00

Maybe, but the client seems to send the same things in both cases.

borkdude11:10:12

ok, let me try again.

pez11:10:16

I should try this using emacs, but it will take me an hour or so to figure out how...

borkdude11:10:10

user=> (println (slurp "/tmp/bin/foo.bb"))
(ns foo)

(defn bar [])

nil
user=> (load-file "/tmp/bin/foo.bb")
#'foo/bar
user=> (ns-name (:ns (meta #'foo/bar)))
foo

pez11:10:43

I'm trying with emacs now anyway. 😃

borkdude11:10:45

user=> (System/getProperty "babashka.version")
"0.2.2"

pez11:10:15

I have the same version.

borkdude11:10:30

when I do cider-load-buffer in the bb file and then get eldoc for bar, then it says foo/bar

borkdude11:10:47

not sure if that's what you are trying to achieve from the editor

borkdude11:10:13

"Received" {:op :load-file, :file "#!/usr/bin/env bb\n\n(ns foo)\n\n(defn bar [])\n", :file-path "/tmp/bin/foo.bb", :file-name "", :session "40f03a20-431a-49e8-81ad-e6cbcd38b553", :id "17"}
current ns foo
Reading! ed2457d2-d18e-4ce5-86a4-c297bb686cae 0

borkdude11:10:44

i.e. can't repro any problem

pez11:10:46

I also got that result in emacs.

borkdude11:10:09

maybe Calva tries to parse the top ns form and gets confused by the shebang?

borkdude11:10:16

so maybe try without shebang?

borkdude11:10:30

although you mentioned the extension causing a difference

borkdude11:10:44

maybe Calva only parses something based on an extension?

pez11:10:45

I think I know what they do different now. Emacs send the ns with the eval, while Calva doesn't. So I can work around the error by fixing this in Calva.

borkdude12:10:42

yes. load-file doesn't cause an ns switch. so the current ns stays user. if you then eval single expressions without setting an ns, they are eval-ed in user

borkdude12:10:05

I'm not sure why this is different for you with .bb files though

borkdude12:10:19

there is no special handling of extensions that I'm aware of

pez12:10:51

Rather why .clj file works, is a mystery, if it is not suposed to.

borkdude12:10:10

This is all it does:

:load-file (let [file (:file msg)
                         msg (assoc msg :code file)]
                     (eval-msg ctx os msg opts)
                     (recur ctx is os id opts))

borkdude12:10:41

hm, so maybe it should switch ns then, since it's the same as when just sending an ns form?

borkdude12:10:18

the extension should be irrelevant here since it only does something with the contents of the file

pez12:10:38

load-file using emacs works the same as with calva. With .clj files the ns changes, with .bb files, it doesn't.

borkdude12:10:06

what's the difference in :file contents?

pez12:10:11

For load-file Calva and Emacs send the same things. Not sure if that was what you asked..

borkdude12:10:22

between .clj and .bb file. same contents?

pez12:10:53

Yes. Same contents.

borkdude12:10:24

Are there any other messages that are sent before load-file, e.g. changing the ns?

borkdude12:10:38

since it logs as different current ns, there must be some difference

pez12:10:50

Emacs sends some eldoc messages. Calva doesn't.

pez12:10:15

But between the files, you mean, of course. Let me check.

pez12:10:51

You are right.

pez12:10:23

.bb:

"Received" {:id "12", :op :eval, :session "72be4e52-411f-4862-93a6-b60fe02acdbe", :code "(in-ns 'user)", :pprint 0}
current ns user
Reading! 8647947a-b575-4813-a6b3-26e2d917b1cf 0
"Received" {:op :load-file, :session "72be4e52-411f-4862-93a6-b60fe02acdbe", :file "#!/usr/bin/env bb\n(ns foo)\n(def bar \"baz\")", :id "13", :file-name "", :file-path "/Users/pez/Projects/liberty/cospaia.se/bin/foo.bb"}
current ns user
Reading! 8647947a-b575-4813-a6b3-26e2d917b1cf 0

pez12:10:43

.clj:

"Received" {:id "14", :op :eval, :session "72be4e52-411f-4862-93a6-b60fe02acdbe", :code "(in-ns 'foo)", :pprint 0}
current ns foo
Reading! 8647947a-b575-4813-a6b3-26e2d917b1cf 0
"Received" {:op :load-file, :session "72be4e52-411f-4862-93a6-b60fe02acdbe", :file "#!/usr/bin/env bb\n(ns foo)\n(def bar \"baz\")  \n", :id "15", :file-name "foo.clj", :file-path "/Users/pez/Projects/liberty/cospaia.se/bin/foo.clj"}
current ns foo
Reading! 8647947a-b575-4813-a6b3-26e2d917b1cf 0

borkdude12:10:05

ok I don't see this in-ns message with emacs

borkdude12:10:21

so this must be the source of the confusion

pez12:10:45

Yeah. Calva sends the wrong in-ns . And then relies on it. Both things should be fixed.. Sorry for dragging you along this long discovery journey!

pez12:10:47

Now I have a lot of exceptions from clj-kondo. 😃