babashka

https://github.com/babashka/babashka. Also see #sci, #nbb and #babashka-circleci-builds .
pez 2020-10-11T09:05:01.329600Z

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

2020-10-11T09:09:44.329800Z

may be this? https://github.com/borkdude/babashka#current-file-path

2020-10-11T09:10:04.330200Z

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

user=> *file*
"<repl>"

pez 2020-10-11T09:14:59.330500Z

Thanks! Yes, I can use that.

1๐Ÿ™‚
pez 2020-10-11T09:22:29.331600Z

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

2020-10-11T09:28:12.333700Z

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

2020-10-11T09:28:27.334Z

this is with the test vsix that was posted yesterday

borkdude 2020-10-11T09:32:00.334900Z

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

2020-10-11T09:34:18.335300Z

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

2020-10-11T09:34:33.335600Z

but "not in project" does make more sense

2020-10-11T09:35:13.336100Z

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

borkdude 2020-10-11T09:35:32.336600Z

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

2020-10-11T09:36:00.337100Z

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

borkdude 2020-10-11T09:36:01.337200Z

Seems to work fine otherwise.

borkdude 2020-10-11T09:36:12.337400Z

@pez :

2020-10-11T09:36:48.337800Z

oh interesting

2020-10-11T09:37:00.338300Z

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

2020-10-11T09:37:09.338800Z

if i invoke with "in project" i do

borkdude 2020-10-11T09:37:21.339400Z

Let's start a thread.

borkdude 2020-10-11T09:37:59.339600Z

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

2020-10-11T09:38:10.339900Z

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

2020-10-11T09:38:12.340100Z

yes

pez 2020-10-11T09:43:57.340300Z

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.

borkdude 2020-10-11T09:45:11.340500Z

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 ;)

borkdude 2020-10-11T09:46:39.340900Z

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.

pez 2020-10-11T09:47:59.341100Z

Yeah, Calva should handle the describe op.

pez 2020-10-11T09:48:09.341300Z

info is a cider-nrepl op, I think.

borkdude 2020-10-11T09:48:42.341500Z

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

pez 2020-10-11T09:50:30.341800Z

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

pez 2020-10-11T09:53:22.342Z

What's https://github.com/babashka/babashka.nrepl/issues/27 about?

borkdude 2020-10-11T09:54:21.342300Z

Apparently about https://nrepl.org/nrepl/ops.html#lookup

borkdude 2020-10-11T09:54:56.342500Z

It might be some new op

borkdude 2020-10-11T09:55:21.342700Z

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

borkdude 2020-10-11T09:57:23.342900Z

like, input + output

pez 2020-10-11T10:02:35.343100Z

Here's the issue I created: https://github.com/babashka/babashka.nrepl/issues/30

pez 2020-10-11T10:03:14.343400Z

I'll be happy providing any clues you need of course. ๐Ÿ˜ƒ

borkdude 2020-10-11T10:04:34.343600Z

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

borkdude 2020-10-11T10:04:52.343800Z

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

borkdude 2020-10-11T10:07:56.344Z

Or maybe you could bug Bug for some docs :)

1๐Ÿ˜›
pez 2020-10-11T10:12:51.344200Z

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

pez 2020-10-11T10:26:01.344400Z

I added some info on info to the issue now.

borkdude 2020-10-11T10:27:35.344600Z

Thanks.

pez 2020-10-11T10:33:42.344800Z

Added one more example.

borkdude 2020-10-11T10:34:45.345Z

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

borkdude 2020-10-11T10:34:59.345200Z

It does have el-doc support

borkdude 2020-10-11T10:35:27.345400Z

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

pez 2020-10-11T10:42:03.345600Z

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.

borkdude 2020-10-11T10:44:10.345800Z

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

borkdude 2020-10-11T10:44:36.346100Z

if the info op is not present in describe that is

pez 2020-10-11T10:50:09.347500Z

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

borkdude 2020-10-11T10:51:03.348300Z

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

pez 2020-10-11T10:55:56.350100Z

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

borkdude 2020-10-11T10:56:36.350700Z

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

pez 2020-10-11T10:58:16.351300Z

Then babashka reports, current ns user

pez 2020-10-11T10:59:02.352200Z

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

borkdude 2020-10-11T10:59:28.352400Z

let me try

pez 2020-10-11T11:00:12.353Z

And it's not only that reporting. My symbols in the file get defined in foo as well. ๐Ÿ˜ƒ

borkdude 2020-10-11T11:02:52.353700Z

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?

borkdude 2020-10-11T11:06:04.353900Z

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

borkdude 2020-10-11T11:06:24.354100Z

I can then try to repro it using lein repl

pez 2020-10-11T11:07:58.354500Z

Give me a sec

pez 2020-10-11T11:10:49.354700Z

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

pez 2020-10-11T11:12:13.354900Z

But with lein repl :connect I can.

pez 2020-10-11T11:13:23.355100Z

No... wait

pez 2020-10-11T11:22:56.355300Z

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

pez 2020-10-11T11:24:26.355500Z

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

borkdude 2020-10-11T11:24:45.355700Z

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

pez 2020-10-11T11:27:21.355900Z

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

borkdude 2020-10-11T11:27:56.356100Z

load-file should never switch namespace

pez 2020-10-11T11:28:45.356300Z

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

borkdude 2020-10-11T11:29:58.356500Z

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=>

pez 2020-10-11T11:29:59.356700Z

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.

pez 2020-10-11T11:30:54.357Z

Let me try that.

borkdude 2020-10-11T11:31:00.357200Z

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

borkdude 2020-10-11T11:32:06.357800Z

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

pez 2020-10-11T11:32:21.358Z

the op.

pez 2020-10-11T11:33:16.358200Z

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.

pez 2020-10-11T11:33:36.358400Z

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

borkdude 2020-10-11T11:34:22.358600Z

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

borkdude 2020-10-11T11:34:42.358800Z

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

borkdude 2020-10-11T11:37:58.359Z

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

pez 2020-10-11T11:38:09.359200Z

Maybe this error is in Calva...

pez 2020-10-11T11:45:01.359400Z

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 "<http://foo.bb|foo.bb>", :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

pez 2020-10-11T11:45:41.359600Z

The file contents:

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

(def bar "baz")

borkdude 2020-10-11T11:46:32.359800Z

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

pez 2020-10-11T11:47:21.360Z

So this is the problem.

pez 2020-10-11T11:48:02.360400Z

While it gets defined correctly in the .clj file.

borkdude 2020-10-11T11:48:18.360600Z

That's pretty weird yes.

borkdude 2020-10-11T11:48:34.360800Z

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

pez 2020-10-11T11:49:00.361100Z

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

borkdude 2020-10-11T11:49:12.361300Z

ok, let me try again.

pez 2020-10-11T11:50:16.361500Z

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

borkdude 2020-10-11T11:51:10.361700Z

user=&gt; (println (slurp "/tmp/bin/foo.bb"))
(ns foo)

(defn bar [])

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

pez 2020-10-11T11:51:43.361900Z

I'm trying with emacs now anyway. ๐Ÿ˜ƒ

borkdude 2020-10-11T11:51:45.362100Z

user=&gt; (System/getProperty "babashka.version")
"0.2.2"

pez 2020-10-11T11:52:15.362300Z

I have the same version.

borkdude 2020-10-11T11:53:30.362600Z

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

borkdude 2020-10-11T11:53:53.362800Z

borkdude 2020-10-11T11:54:05.363200Z

this is with http://foo.bb

borkdude 2020-10-11T11:54:47.363400Z

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

borkdude 2020-10-11T11:55:13.363600Z

"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 "<http://foo.bb|foo.bb>", :session "40f03a20-431a-49e8-81ad-e6cbcd38b553", :id "17"}
current ns foo
Reading! ed2457d2-d18e-4ce5-86a4-c297bb686cae 0

borkdude 2020-10-11T11:56:44.363800Z

i.e. can't repro any problem

pez 2020-10-11T11:56:46.364Z

I also got that result in emacs.

borkdude 2020-10-11T11:57:09.364200Z

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

borkdude 2020-10-11T11:59:16.364400Z

so maybe try without shebang?

borkdude 2020-10-11T11:59:30.364600Z

although you mentioned the extension causing a difference

borkdude 2020-10-11T11:59:44.364800Z

maybe Calva only parses something based on an extension?

pez 2020-10-11T11:59:45.365Z

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.

borkdude 2020-10-11T12:00:42.365200Z

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

borkdude 2020-10-11T12:01:05.365500Z

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

borkdude 2020-10-11T12:01:19.365700Z

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

pez 2020-10-11T12:01:51.365900Z

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

borkdude 2020-10-11T12:03:10.366100Z

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))

borkdude 2020-10-11T12:03:41.366300Z

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

borkdude 2020-10-11T12:04:18.366500Z

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

pez 2020-10-11T12:04:38.366700Z

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

borkdude 2020-10-11T12:05:06.366900Z

what's the difference in :file contents?

pez 2020-10-11T12:06:11.367100Z

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

borkdude 2020-10-11T12:06:22.367300Z

between .clj and .bb file. same contents?

pez 2020-10-11T12:06:53.367500Z

Yes. Same contents.

borkdude 2020-10-11T12:07:24.367700Z

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

borkdude 2020-10-11T12:08:38.367900Z

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

pez 2020-10-11T12:08:50.368100Z

Emacs sends some eldoc messages. Calva doesn't.

pez 2020-10-11T12:09:15.368300Z

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

pez 2020-10-11T12:10:51.368500Z

You are right.

pez 2020-10-11T12:11:23.368700Z

.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 "<http://foo.bb|foo.bb>", :file-path "/Users/pez/Projects/liberty/cospaia.se/bin/foo.bb"}
current ns user
Reading! 8647947a-b575-4813-a6b3-26e2d917b1cf 0

pez 2020-10-11T12:11:43.368900Z

.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

borkdude 2020-10-11T12:12:05.369100Z

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

borkdude 2020-10-11T12:12:21.369300Z

so this must be the source of the confusion

pez 2020-10-11T12:19:45.369500Z

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!

borkdude 2020-10-11T12:20:01.369700Z

:thumbsup:

pez 2020-10-11T12:22:47.369900Z

Now I have a lot of exceptions from clj-kondo. ๐Ÿ˜ƒ