Does the clojure
or clj
command from Clojure CLI tools read a user.clj
file if it contains (ns user)
expression when starting the REPL?
With Leiningen projets using a user
or dev/user.clj
file was one way to start component services.
For a Clojure CLI project, if the dev
directory was added as an extra path under a :dev
alias and clj -A:dev
used to start the REPL, should the dev/user.clj
file be automatically evaluate on REPL start?
I though I had this working the other week, but not sure if I imagined this now 🙂
@jr0cket I think the answer is yes, it's not tooling dependent: https://github.com/clojure/clojure/blob/d07ef175c700329f7afbef8770332b6247a34a49/src/jvm/clojure/lang/RT.java#L486
Ah, I have it working in terms of requiring a namespace and evaluating a funtion call. However, using (in-ns main-project-namespace)
does not seem to switch to the namespace (or if it does, then maybe it been switched back to user afterward).
@jr0cket load-file
also switches back to the original namespace afterwards, because it uses binding
+ *ns*
I imagine something similar is happening here
Okay, I dont have to change the namespace, I can require the relevant libraries and use fully qualified namespace function calls.
http://practicalli.github.io/clojure/clojure-tools/configure-repl-startup.html
If there is a way in Clojure CLI tools to change the namespace using a user.clj
file, I would be interested to know, but not essential. I can use the --eval
option to run the relevant expressions.
@jr0cket This seems to work:
$ clj -e "(ns foo.bar) (alter-var-root #'*ns* (constantly 'foo.bar))" -r
@jr0cket or even:
$ clj -e "(ns foo.bar) (alter-var-root #'*ns* (constantly 'foo.bar)) (clojure.main/repl)"
foo.bar
foo.bar=>
I can set the namespace easily using --eval option on the command line. Unfortunately the above do not work from within user.clj
. Ideally I'd like use it with rebel readline, which is the REPL UI I use if I am just using the REPL directly rather than an editor. --eval doesnt work when using the :rebel alias
@jr0cket Very well.
$ clj -R:rebel -e "(ns foo.bar) (alter-var-root #'*ns* (constantly (find-ns 'foo.bar)))" -m rebel-readline.main
#object[clojure.lang.Namespace 0x46cf05f7 "foo.bar"]
[Rebel readline] Type :repl/help for online help info
foo.bar=>
Success 🙂 I have learnt many things today, thank you.
Is there a way to do a universal exlucions? i see you can attach exluded to individual deps, but what if i wont want this lib from any dep?
@drewverlee you can use :classpath-overrides
:
{:aliases {:babashka {:classpath-overrides {org.clojure/clojure nil
org.clojure/spec.alpha nil
org.clojure/core.specs.alpha nil}}}}
This only works in an alias
gotcha.