4

I have a src/example/foo.clj file:

(ns example.foo)

(defn my-fn []
  (println "Hello, world!"))

I can open it in Emacs and spin up a REPL using C-c M-j and play with it. I don't want to type long namespace names, so I alias the ones I need:

user> (require '[example.foo :as foo])
; => nil

So far so good. Everything works fine at this point.

user> (foo/my-fn)
; Hello, world!
; => nil

If I try to use a function that doesn't exist, I get a CompilerException as expected:

user> (foo/my-function)
; CompilerException java.lang.RuntimeException: No such var: foo/my-function

Now, since obviously foo still needs a lot of work, I'm actively making changes to it. I decide to rename one of my functions to something more descriptive.

src/example/foo.clj:

(ns example.foo)

(defn greet-world []
  (println "Hello, world!"))

I then save the file and reload it using C-c C-x, and that's where everything seems to break. If I try to call the function using its old name, I get an IllegalStateException instead of a CompilerException:

user> (foo/my-fn)
; IllegalStateException Attempting to call unbound fn: #'example.foo/my-fn

And if I use its new name:

user> (foo/greet-world)
; CompilerException java.lang.RuntimeException: No such var: foo/greet-world

So now my alias namespace is completely useless. I can, however, call the function using its full name:

user> (example.foo/greet-world)
; Hello, world!
; => nil

But of course, if I try to run require again, I get an error:

user> (require '[example.foo :as foo])
; IllegalStateException Alias foo already exists in namespace user, aliasing example.foo

So, how can I actually reload a namespace without restarting my entire REPL?

Sam Estep
  • 449
  • 5
  • 20
  • 1
    I think this is more of a Clojure question. Also, I can't seem to reproduce this problem. What versions of cider/Clojure/nrepl are you using? – nanny Oct 02 '15 at 15:09
  • @nanny I'm using Java 1.8.0_60, Clojure 1.7, Leiningen 2.5.2, CIDER nREPL 0.10.0-SNAPSHOT, and CIDER 0.10.0-SNAPSHOT. – Sam Estep Oct 02 '15 at 16:28
  • @nanny If you're still having trouble reproducing the problem, you could try it with [my exact setup](https://github.com/elogent/emacs-d). My `~/.lein/profiles.clj` file only contains the line `{:user {:plugins [[cider/cider-nrepl "0.10.0-SNAPSHOT"]]}}`. – Sam Estep Oct 02 '15 at 17:35
  • 2
    More specifically: the only part I can't reproduce is the last part, where re-required the namespace throws an exception. It works fine for me. Unfortunately, I can't try out you config right now, but I will when I get the chance. In the meantime, maybe this will help as a work around: https://clojuredocs.org/clojure.core/ns-unalias – nanny Oct 02 '15 at 19:09
  • @nanny I didn't have the chance to try `ns-unalias` until now, and it doesn't seem to work. My `require` expression throws an `IllegalStateException` whether or not I use it with `:reload`, and whether or not I run `(ns-unalias (find-ns 'example.foo) 'foo)` before it. Any idea what I'm doing wrong? – Sam Estep Oct 10 '15 at 01:17

0 Answers0