How do I distinguish a babashka session as being run as a script from it being evaluated in an repl session?
may be this? https://github.com/borkdude/babashka#current-file-path
$ bb
Babashka v0.2.2-SNAPSHOT REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.
user=> *file*
"<repl>"
Thanks! Yes, I can use that.
Doesn't seem like Calva plays very well with babashka, though ๐ I think I remember it working pretty well...
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
this is with the test vsix that was posted yesterday
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
i guess i chose "connect to a running repl in..."
but "not in project" does make more sense
i tried with "not in project", generic, and got a prompt
yeah, in project did work for me (I'm on the normal Calva plugin from the marketplace)
ah, may be that's the difference (the version of calva)
Seems to work fine otherwise.
@pez :
oh interesting
if i start vscode and choose "not in project" first, i don't get asked
if i invoke with "in project" i do
Let's start a thread.
Anyway, seems like it's a Calva issue then.
subsequent attempt of "not in project" leads to a prompt
yes
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.
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 ;)
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.
Yeah, Calva should handle the describe op.
info is a cider-nrepl op, I think.
If you can make an issue with a link to some docs, it might get implemented
I'll do that. Calva feels pretty broken w/o info.
What's https://github.com/babashka/babashka.nrepl/issues/27 about?
Apparently about https://nrepl.org/nrepl/ops.html#lookup
It might be some new op
What are you using these ops for anyway? Could you give me some clue at least?
like, input + output
Here's the issue I created: https://github.com/babashka/babashka.nrepl/issues/30
I'll be happy providing any clues you need of course. ๐
Thanks. I responded in the issue :) Maybe you could past a few example conversations of input + outputs
I think you can do this in emacs using some cider setting where it logs this to a buffer
Or maybe you could bug Bug for some docs :)
I think it's time to implement some tracing in Calva...
I added some info on info to the issue now.
Thanks.
Added one more example.
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
It does have el-doc support
Some other editors just eval something custom like clojure.repl/doc etc
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.
for bb you can eval (meta (resolve 'foo/bar))
which should give you :file
, :line
etc
if the info op is not present in describe that is
For some reason I want to use .bb
as file extension. But babashka seems to want me to use .clj
. Is that so?
On the classpath it searches in the order of .bb / .clj / .cljc
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
then you should be able to (require 'foo)
yes
Then babashka reports, current ns user
But if I rename the file to foo.clj
I get current ns foo
.
let me try
And it's not only that reporting. My symbols in the file get defined in foo
as well. ๐
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?@pez Please use a thread. I don't know how I can repro your problem. Please give more details.
I can then try to repro it using lein repl
Give me a sec
So I don't seem to be able to repro it using the bb prompt.
But with lein repl :connect
I can.
No... wait
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
Then again, I get the same output using .clj
as extension... Hmmm.
Can you explain what the problem is you're trying to repro. I think I missed the point
Yeah, the problem is that when I use the load-file
op, then babashka doesn't switch namespace when the file ends with .bb
.
load-file should never switch namespace
Well, it does if the file ends with .clj
.
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=>
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.
Let me try that.
I have the feeling we're talking past each other.
Are you referring toย clojure.core/load-fileย or the nREPL load-file op
the op.
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.
Let's keep trying to reach each other. Haha.
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
it's best if you explicitly set the ns in the eval op
It may be a bug, please write a report with a clear repro. Just not sure if it is.
Maybe this error is in Calva...
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
The file contents:
#!/usr/bin/env bb
(ns foo)
(def bar "baz")
You still haven't explained to me what error. I do see a difference, but what's the error? Problem statement?
So this is the problem.
While it gets defined correctly in the .clj
file.
That's pretty weird yes.
Now it's the question if this is a problem in the client or server right?
Maybe, but the client seems to send the same things in both cases.
ok, let me try again.
I should try this using emacs, but it will take me an hour or so to figure out how...
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
I'm trying with emacs now anyway. ๐
user=> (System/getProperty "babashka.version")
"0.2.2"
I have the same version.
when I do cider-load-buffer
in the bb file and then get eldoc for bar, then it says foo/bar
this is with http://foo.bb
not sure if that's what you are trying to achieve from the editor
"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
i.e. can't repro any problem
I also got that result in emacs.
maybe Calva tries to parse the top ns form and gets confused by the shebang?
so maybe try without shebang?
although you mentioned the extension causing a difference
maybe Calva only parses something based on an extension?
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.
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
I'm not sure why this is different for you with .bb files though
there is no special handling of extensions that I'm aware of
Rather why .clj
file works, is a mystery, if it is not suposed to.
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))
hm, so maybe it should switch ns then, since it's the same as when just sending an ns form?
the extension should be irrelevant here since it only does something with the contents of the file
load-file using emacs works the same as with calva. With .clj
files the ns changes, with .bb
files, it doesn't.
what's the difference in :file contents?
For load-file Calva and Emacs send the same things. Not sure if that was what you asked..
between .clj and .bb file. same contents?
Yes. Same contents.
Are there any other messages that are sent before load-file, e.g. changing the ns?
since it logs as different current ns
, there must be some difference
Emacs sends some eldoc
messages. Calva doesn't.
But between the files, you mean, of course. Let me check.
You are right.
.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
.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
ok I don't see this in-ns
message with emacs
so this must be the source of the confusion
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!
:thumbsup:
Now I have a lot of exceptions from clj-kondo. ๐