What is the preferred way of reloading functions defined in a Clojure file without having to restart the REPL. Right now, in order to use the updated file I have to:
- edit
src/foo/bar.clj
- close the REPL
- open the REPL
(load-file "src/foo/bar.clj")
(use 'foo.bar)
In addition, (use 'foo.bar :reload-all)
does not result in required effect, which is evaluating the modified bodies of functions and returning new values, instead of behaving as the source haven't changed at all.
Or
(use 'your.namespace :reload)
Reloading Clojure code using
(require … :reload)
and:reload-all
is very problematic:The clojure.tools.namespace library improves the situation significantly. It provides an easy refresh function that does smart reloading based on a dependency graph of the namespaces.
Unfortunately reloading a second time will fail if the namespace in which you referenced the
refresh
function changed. This is due to the fact that tools.namespace destroys the current version of the namespace before loading the new code.You could use the fully qualified var name as a workaround for this problem but personally I prefer not having to type that out on each refresh. Another problem with the above is that after reloading the main namespace the standard REPL helper functions (like
doc
andsource
) are no longer referenced there.To solve these issues I prefer to create an actual source file for the user namespace so that it can be reliably reloaded. I put the source file in
~/.lein/src/user.clj
but you can place in anywhere. The file should require the refresh function in the top ns declaration like this:You can setup a leiningen user profile in
~/.lein/profiles.clj
so that location you put the file in is added to the class path. The profile should look something like this:Note that I set the user namespace as the entry point when launching the REPL. This ensures that the REPL helper functions get referenced in the user namespace instead of the main namespace of your application. That way they won’t get lost unless you alter the source file we just created.
Hope this helps!
There is also an alternative like using tools.namespace, it's pretty efficient:
Try load-file again?
If youre using an IDE, there's usually a keyboard shortcut to send a code-block to the REPL, thus effectively re-defining the associated functions.
The best answer is:
This will not only reload your specified namespace, but will reload all dependency namespaces as well.
As soon as
(use 'foo.bar)
works for you, it means that you have foo/bar.clj or foo/bar_init.class on your CLASSPATH. The bar_init.class would be an AOT-compiled version of bar.clj. If you do(use 'foo.bar)
, I'm not exactly sure if Clojure prefers class over clj or the other way round. If it would prefer class files and you have both files, then it's clear that editing the clj file and then reloading the namespace has no effect.BTW: You don't need to
load-file
before theuse
if your CLASSPATH is set properly.BTW2: If you need to use
load-file
for a reason, then you can simply do it again if you edited the file.