Why is `(exit)` not always available in the REPL? - clojure

Today I noticed something odd. When I'm not "in" a project (that is, the shell is not in a clojure project's directory), I can use exit to exit the REPL:
shell$ lein repl
REPL started; server listening on localhost port 43712
user=> (+ 3 4)
7
user=> (exit)
shell$ echo '<span>not in <s>kansas</s>clojure anymore</span>'
When I'm "in" a project (that is, the shell is in a clojure project's directory), I can't use exit:
shell$ cd my_clojure_project
shell$ lein repl
REPL started; server listening on localhost port 69237
user=> (* 8 4)
32
user=> (exit)
java.lang.Exception: Unable to resolve symbol: exit in this context (NO_SOURCE_FILE:2)
user=>
What is the issue here?
Clojure version (for both examples):
user=> (clojure-version)
"1.2.1"
Leiningen version (for both examples):
shell$ lein -v
Leiningen 1.6.1 on Java 1.6.0_26 Java HotSpot(TM) 64-Bit Server VM

It appears to be because the leiningen.core namespace isn't available when there is a project. More specifically, when there is a project, your project's code is evaluated in a separate ClassLoader with only your project on the classpath. Therefore none of Leiningen's functions are available.
The Leiningen 2 REPL doesn't have this problem.

Related

Cannot get REPL prompt with figwheel-main and Clojure Tools

My goal is to have a ClojureScript REPL (targeting Node.js) using Figwheel and Clojure Tools (not Leiningen). But when I launch Figwheel it seems to start correctly but never completes.
deps.edn
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/clojurescript {:mvn/version "1.11.60"}}
:aliases {:fig {:extra-deps {com.bhauman/figwheel-main {:mvn/version "0.2.18"}}}}
:paths ["src" "target" "resources"]}
dev.cljs.edn
{:main cljs-example.foo
:target :nodejs
:optimizations :none
:pretty-print true
:source-map true
:asset-path "js/dev"
:output-to "resources/public/js/dev.js"
:output-dir "resources/public/js/dev"}
Launch command
clj -M:fig -m figwheel.main -b dev -r
% clj -M:fig -m figwheel.main -b dev -r
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[Figwheel] Compiling build dev to "resources/public/js/dev.js"
[Figwheel] Successfully compiled build dev to "resources/public/js/dev.js" in 0.708 seconds.
[Figwheel] Watching paths: ("src") to compile build - dev
[Figwheel] Starting Server at http://localhost:9500
[Figwheel] Starting REPL
Prompt will show when REPL connects to evaluation environment (i.e. Node)
Figwheel Main Controls:
(figwheel.main/stop-builds id ...) ;; stops Figwheel autobuilder for ids
(figwheel.main/start-builds id ...) ;; starts autobuilder focused on ids
(figwheel.main/reset) ;; stops, cleans, reloads config, and starts autobuilder
(figwheel.main/build-once id ...) ;; builds source one time
(figwheel.main/clean id ...) ;; deletes compiled cljs target files
(figwheel.main/status) ;; displays current state of system
Figwheel REPL Controls:
(figwheel.repl/conns) ;; displays the current connections
(figwheel.repl/focus session-name) ;; choose which session name to focus on
In the cljs.user ns, controls can be called without ns ie. (conns) instead of (figwheel.repl/conns)
Docs: (doc function-name-here)
Exit: :cljs/quit
Results: Stored in vars *1, *2, *3, *e holds last exception object
Starting node ...
Node output being logged to: resources/public/js/dev/node.log
For a better development experience:
1. Open chrome://inspect/#devices ... (in Chrome)
2. Click "Open dedicated DevTools for Node"
And that's it! :(
It stays like that forever and I never get a prompt such as:
cljs.user=>
Addendum
I don't think my foo.cljs file has an error that would cause Figwheel to hang. When I launch a "native" REPL with:
clj -M -m cljs.main --target node --compile cljs-example.foo --repl
Then I get:
ClojureScript 1.11.60
cljs.user=> (require '[cljs-example.foo])
i am foo!!!!!!!!
nil
cljs.user=>

How can I continue running with repl after -main instead of exit?

(ns secretary.core)
(defn -main
[& args]
(println args "Hello, World!"))
lein run 1 2 3
(1 2 3) Hello, World!
But I want it keep in REPL. Not exit.
Try this:
(ns demo.core )
(defn -main
[& args]
(println "Got:" args ))
with the above code:
> lein repl
Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
Compiling 2 source files to /home/alan/expr/demo/target/default/class-files
nREPL server started on port 46184 on host 127.0.0.1 - nrepl://127.0.0.1:46184
REPL-y 0.4.3, nREPL 0.6.0
Clojure 1.10.1
Java HotSpot(TM) 64-Bit Server VM 13+33
demo.core=> (ns demo.core)
nil
demo.core=> (-main 1 2 3)
Got: (1 2 3)
demo.core=> (+ 3 4)
7
demo.core=>
So, you start the REPL first, then call a function (even -main). You are still in the REPL when finished.
P.S. Although, my personal favorite, is to use the lein test-refresh
plugin and write your experiments in unit-test style using your favorite editor.

Starting an nREPL with Clojure CLI Tools

How do I start an nREPL from the clj command?
I can't run my project using Lein or Boot because I have an unbalanced paren somewhere, and the reader complains `java.lang.RuntimeException: read-cond starting on line 13 requires an even number of forms.
Things are easier now than they were when the question was posted:
$ clj -Sdeps '{:deps {nrepl/nrepl {:mvn/version "0.5.3"}}}' -m nrepl.cmdline
More details here.
Wrote a gist on how to do this:
clj -Sdeps '{:deps {org.clojure/tools.nrepl {:mvn/version "0.2.12"}}}'
Clojure 1.9.0
user=> (use '[clojure.tools.nrepl.server :only (start-server stop-server)])
nil
user=> (defonce server (start-server :port 7888))
#'user/server
Now you can connect to port 7888 using your remote REPL client. There is probably a way to do this in one line.

Launch the REPL from within my app

Suppose that I have implemented a few functions for some personal calculation at work. I'd like to build a .jar (uberjar) that my colleagues would use too in a REPL, like:
megacorpcalcs.core=> (+ 2 2)
4
megacorpcalcs.core=> (salary 8 0.4)
666$
What code should I type for a REPL to start when the .jar launches?
EDIT: after your comments I've split this answer into 2
1. Running your uberjar with a REPL
Create your uberjar, and start it with:
java -cp /path/to/your/application-X.Y.Z-standalone.jar clojure.main -i #your_application/foo.clj -r
I had to add the -i parameter and point it to one of my clj files in order to get any of my classes in the project to actually load in the repl. There may be a better way to do this, but I haven't found it yet. Without it, you get a standard clojure repl but your application isn't loaded.
Note, you need the # symbol so that it loads from the jar file relative to the classpath (i.e from root of jar).
This should start a repl which you can change namespace and run your application functions in.
Additionally, you can install rlwrap and prepend the java command with it so you can use history and arrow keys sanely.
2. Embedding a REPL server in your application to connect to from another client
You can embed your own repl on startup of your application (e.g. a simple main that just starts a repl instance), and then your users can run your jar file, and separately connect to it with their choice of tool (cider-jack-in, 'lein repl connect ...')
The simple version of this is:
(start-server :port 7890 :handler cider-nrepl-handler)
Substitute the port you want, see below for the appropriate namespaces to import.
Here is a more complete example:
(ns your-app.server
(:gen-class)
(:require [cider.nrepl :refer (cider-nrepl-handler)]
[clojure.tools.nrepl.server :refer [start-server]]))
(def repl-server (atom nil))
(defn create-nrepl-server!
[repl-port]
(println (format "starting nrepl server on port %d" repl-port))
(reset! repl-server (start-server :port repl-port :handler cider-nrepl-handler)))
(defn -main []
;; ...
(let [repl-port 7890]
(create-nrepl-server! repl-port)
(spit ".nrepl-port" repl-port)))
You'll need the following in your project.clj file
:plugins [[cider/cider-nrepl "0.10.0"]] ;; or whatever version you prefer
:dependencies [[org.clojure/tools.nrepl "0.2.12"]]
Once connected to your custom repl, standard rules apply, just change namespace and call your functions.

How can I run lein repl outside of a project?

I spent some time last night messing with my leinigen profiles.clj to get rid of all the errors that were being printed when starting cider in my project. Today I went to start a repl from the terminal (I like to keep one open while I work) but it didn't work. I thought it was a cider issue so I tried it from Emacs but even in Emacs if I'm not in a project the repl won't start.
Here's the error:
Error loading refactor-nrepl.middleware: clojure.lang.ArityException: Wrong number of args (4) passed to: StringReader, compiling:(abnf.clj:186:28)
Exception in thread "Thread-4" java.lang.RuntimeException: Unable to resolve var: refactor-nrepl.middleware/wrap-refactor in this context, compiling:(NO_SOURCE_PATH:0:0)
...
Caused by: java.lang.RuntimeException: Unable to resolve var: refactor-nrepl.middleware/wrap-refactor in this context
My ~/.lein/profiles.clj
{:user {:plugins [[lein-try "0.4.3"]
[refactor-nrepl "1.1.0"]
[cider/cider-nrepl "0.9.1"]]
:dependencies [[org.clojure/tools.nrepl "0.2.12"]
[acyclic/squiggly-clojure "0.1.4"]
^:replace [org.clojure/tools.nrepl "0.2.12"]
[refactor-nrepl "1.1.0"]]}}
The versions of things when cider starts in a project
; CIDER 0.9.1 (Java 1.8.0_45, Clojure 1.7.0, nREPL 0.2.12)
I'm still pretty new to Clojure, Leinigen, Emacs, etc so I'm not sure why everything above made made my cider errors go away but it did. The cider errors I was getting were having to do with the nrepl version being too low and not having certain things installed (like refactor-nrepl).
When starting a repl from lein using lein repl, it really wants to run in a lein project dir. I keep an empty lein project named clj around in my home dir for this purpose. That way, my common dependencies are already there in the project.clj file, and lein is pre-configured just the way I like it.
You can start lein repl in an empty dir, but you get 10-20 error messages each time before it starts.
Another way is to use the plain repl built into the clojure jar file:
~/dummy > cp /home/alan/.m2/repository/org/clojure/clojure/1.8.0-RC1/clojure-1.8.0-RC1.jar .
~/dummy > d *
-rw-rw-r-- 1 alan alan 3935726 Nov 19 14:11 clojure-1.8.0-RC1.jar
~/dummy > java -jar clojure-1.8.0-RC1.jar
Clojure 1.8.0-RC1
user=>
As you can see, I created an empty directory named dummy and copied in the clojure-*.jar file. You can then run it with the syntax java -jar xxx.jar and it will fire up a repl completely independently of lein.
I also just keep a scratch project which I use for quick/simple repl sessions. There is a lien-oneoff plugin which is supposed to make it easy to work with simple single file lein projects which might be useful.
The other thing you could do is setup a boot config for basically getting a repl up to work with
what is your lein version, I am use 2.5.3, I can start lein repl anywhere.
Shell:~ >: lein repl
nREPL server started on port 52343 on host 127.0.0.1 - nrepl://127.0.0.1:52343
REPL-y 0.3.7, nREPL 0.2.10
Clojure 1.7.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_60-b27
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> Bye for now!
Shell:~ >: lein version
Leiningen 2.5.3 on Java 1.8.0_60 Java HotSpot(TM) 64-Bit Server VM
Shell:~ >: cat .lein/profiles.clj
{:1.2 {:dependencies [[org.clojure/clojure "1.2.0"]]}
:1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
:user {:plugins [[lein-immutant "2.0.0-alpha2"]
[lein-clojars "0.9.1"]
[lein-ancient "0.5.5"]
[lein-kibit "0.0.8"]
[lein-try "0.4.3"]
[venantius/ultra "0.2.0"]]
:ultra {:color-scheme :solarized_dark}}}