8

How to automatically reload Clojure code?

I have watched the presentation. And in there they use some hot swap Clojure technology that reloads code whenever changes are made into source files. I can run the code, but I can not observe the effect of auto reload. How is it possible to reload the code?

the source code.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • 2
    Have you tried [figwheel](https://github.com/bhauman/lein-figwheel)? – jmargolisvt May 08 '16 at 21:52
  • 1
    I asked a [question](http://codereview.stackexchange.com/q/126376/82369) on Code Review about my Clojure workflow; you may find it useful. – Sam Estep May 08 '16 at 21:53
  • Figwheel watches your cljs/cljc files, compiles them, then sends them to your browser to execute them. AFAIK it won't do much to reload server side code when you have a repl open (could be wrong though). – Joel M Jul 26 '20 at 16:37

5 Answers5

4

There are many ways to reload code, depending on the situation:

  • Emacs/CIDER shortcuts to reload a file. Probably Rich is doing something similar. Also see this: How to reload a clojure file in REPL
  • watch files and reload the code (figwheel for frontend development does that, test plugins as well as CIDER shortcuts specifically for running tests, boot has a watch task...)
  • a middleware for the web server you use (ring for example has ring.middleware.reload/wrap-reload for this, pedestal comes with one as well, other webservers like yada play well with component)
  • a component workflow works also, there is an example of an auto-reloadable system with holy-grail
Community
  • 1
  • 1
nha
  • 17,623
  • 13
  • 87
  • 133
2

(I didn't fully re-watch this video, but these notes are from my observations after skipping through the video and making educated guesses)

From what I re-watched of this video it looks like Rich is evaluating the code in a running repl. This allows him to change the code, evaluate it, and see different behavior.

Many editors have support for evaluating code in a buffer in a Clojure repl. Here is some documentation on using CIDER with Emacs to get to interactively play with your code.

Unrelated to the video in question, if you are using ring for web development. You can use the wrap-reload middleware to have your code automatically reloaded when a file has changed and a request hits your web app. This is extremely useful when developing a Clojure web application.

For automatically reloading and running your clojure.tests I recommend lein-test-refresh. It is a Leiningen plug-in that monitors your project for file changes and when something changes it reloads and runs your tests. If you have tests for your project this greatly speeds up development.

Jake McCrary
  • 1,180
  • 9
  • 8
  • I've been using lein-test-refresh myself, with much success! I often add some code in vim at the top level of my program (not inside a test or a function), save the file, see what prints out in the window with Leiningen, edit, save again, see what prints out, and so on. I figure stuff out pretty quickly this way, and then I just wrap it in a `defn` to make it part of the program. – Ben Kovitz May 19 '16 at 09:08
2

Use mount to manage the starting and stopping of your components. For example in a backend web app, you'll want to startup the db before you start the webserver probably.

Then in emacs you can have:

(defun cider-repl-refresh ()
(interactive)
(save-some-buffers)
(with-current-buffer (cider-current-repl-buffer)
(goto-char (point-max))
(insert (concat "(require 'clojure.tools.namespace.repl) "
                "(clojure.tools.namespace.repl/refresh)"))
(cider-repl-return)))
ftravers
  • 3,809
  • 3
  • 37
  • 38
0

If you are not super fond of Emacs (I love Emacs, but hey not everybody does) LightTable is a very nice option for Clojure/ClojureScript interactive programming too.

For developing ClojureScript (Clojure that compiles to Javascript) LightTable and Figwheel are a really nice pair.

In my opinion LightTable has some advantages against Emacs (I never got cider to work perfectly with ClojureScript) for the webdev side, LightTable is basically a specialized version of Chrome, because it is built on top of Electron.

Check this documentation on the Figwheel GitHub page: Running Figwheel with LightTable:

LightTable + Fighweel

Marcs
  • 3,768
  • 5
  • 33
  • 42
0

If you are using Cursive IDE in IntelliJ, there is a special REPL tool which you have to setup to get the functionality you are looking for. It has many features vs. "lein repl" in a terminal window.

Full tutorial here: https://cursive-ide.com/userguide/repl.html

However, its very easy to setup a default instance:

  1. Right click project.clj/deps.edn -> "Create Repl for...". Default settings are fine. Hit ok/apply.

  2. Right click project.clj/deps.edn -> "Run Repl for..." (a repl window will open)

  3. See Tools->Repl for a list of commands, such as:

  • switch to repl window: Ctrl+\
  • reload current file in repl: Alt + Shift + L
  • send function defn to repl: Alt + Shift + M
  • ... and more

In addition you'll get full code completion, syntax highlighting, etc. when writing in a REPL.

Joel M
  • 353
  • 2
  • 10