Paul Drummond
Paul Drummond

Reputation: 6073

How do I update Clojure dependencies when working with nrepl.el?

As I understand it, when I do nrepl-jack-in a REPL is loaded along with all the dependencies defined in project.clj. If I then update project.clj to add a new dependency, do I need to kill the server and re-run nrepl-jack-in or is there way to update the dependencies in the current REPL?

Upvotes: 21

Views: 7850

Answers (5)

roboli
roboli

Reputation: 1478

For Clojure 1.12.0-alpha2 there is sync-deps:

calls add-libs with any libs present in deps.edn, but not yet present on the classpath.

add-lib(s):

takes a lib that is not available on the classpath, and makes it available by downloading (if necessary) and adding to the classloader. Libs already on the classpath are not updated. If the coordinate is not provided, the newest Maven or git (if the library has an inferred git repo name) version or tag are used.

After updating your deps.edn, in your repl:

user> (sync-deps)

Upvotes: 1

Absolute Negativity
Absolute Negativity

Reputation: 1

With Clojure 1.12.0-alpha2, add-lib could download new library.

Upvotes: 0

Micah Elliott
Micah Elliott

Reputation: 10264

Restarting the REPL seems to be the simplest way. This can be done with:

M-x cider-restart

That also appears to accomplish a lein deps. So the whole process of adding a new dependency simply involves adding the require to your project.clj and then invoking cider-restart.


Another (very convenient) way is to use clj-refactor. Adding the artifact (C-c m a p or cljr-add-project-dependency) will prompt for the version you want, automatically put the new dependency into your project.clj file, and reload your session.

Upvotes: 4

Alex Stoddard
Alex Stoddard

Reputation: 8344

Update: Maybe there is some hope, See https://github.com/cemerick/pomegranate

Previously:

The short answer is yes - you do have to restart the JVM process.

I am aware of no good way to update dependencies in a live repl. Leiningen (called by nrepl-jack-in) will manage dependencies and set up the classpath only upon restarting. Trying to do something dynamic and clever is horribly fragile.

The struck out text below is factually true but upon a moment's reflection seemed such bad advice I have marked it up as such...

If you have a local dependency (e.g. jar file) you might use the long-time deprecated function add-classpath at the repl. But you will be entering the dragon infested swamp of java classloaders.

Upvotes: 7

bmillare
bmillare

Reputation: 4233

Before pomegranate existed, I wrote my own library to dynamically load dependencies.

https://github.com/bmillare/dj

After the release of lein2 and how it under the covers can use pomegrante, I rewrote dj to use this underneath. So, even if you don't use 'dj', it might be a useful as a reference to see what its doing.

Upvotes: 1

Related Questions