So! This took a bit longer than I thought.
First I tried to get this working in my playground project. Unfortunately, I couldn’t refresh, because I had code that didn’t work dangling in other namespaces. Ironically the problem that tools namespace tries to solve …
Then I tried running tools namespace from within a source file. That gave me an error when reloading the file, “No such namespace: ns-repl”. (aliased tools.namespace.repl to ns-repl, then that got unloaded, which got me in trouble.
So I should probably do this in a normal REPL, no shenanigans. Setup:
;; In th/code.clj
(ns th.code)
;; Some old code we plan to prevent from dangling
(def old-val "old")
;; Some new code we plan to keep
(def new-val "new")
… and let’s test this from a “normal” repl (one that’s not tied to the th.core
namespace).
;; In a REPL
user> (require 'th.code)
nil
user> (ns-publics 'th.code)
{new-val #'th.code/new-val, old-val #'th.code/old-val}
Yup, that got loaded. Now, uncomment the old val
;; in th/code.clj
;; (def old-val "old")
… and let’s see if we can remove old-val
from th.code
.
;; in a REPL
user> (require 'clojure.tools.namespace.repl)
nil
user> (clojure.tools.namespace.repl/refresh)
:reloading (th.code)
:ok
user> (ns-publics 'th.code)
{new-val #'th.code/new-val}
Worked!
I think CIDER injects the tools.namespace dependency when it starts Clojure. Found this in my *Messages*
buffer, but the full command is cut off:
[nREPL] Starting server via /usr/local/bin/clojure -Sdeps ‘{:deps {nrepl {:mvn/version “0.6.0”} refactor-nrepl {:mvn/version “2.4.0”} cider/cider-nrepl {:mvn/version “0.22.0-SNAPSHOT”}}}’ -m nrepl.cmdline --middleware ‘[“refactor-nrepl.middleware/wrap-refactor”, “cider.nrepl/cider-middleware”]’…