clojurejvmread-eval-print-loopcider

Clojure: How to restore REPL after exception?


What's the proper workflow in cases when you have an error after you restart/reload your app using mount?

An example:

user> (restart)
#error {
 :cause "Unable to resolve symbol: account-database in this context"
 :via
 [{:type clojure.lang.Compiler$CompilerException
   :message "Syntax error compiling at (app/model/session.clj:55:39)."
   :data #:clojure.error{:phase :compile-syntax-check, :line 55, :column 39, :source "app/model/session.clj"}
   :at [clojure.lang.Compiler analyze "Compiler.java" 6808]}
  {:type java.lang.RuntimeException
   :message "Unable to resolve symbol: account-database in this context"
   :at [clojure.lang.Util runtimeException "Util.java" 221]}]
 :trace
 [[clojure.lang.Util runtimeException "Util.java" 221]
Reloadable Clojure REPL
user> (restart)
Syntax error compiling at (*cider-repl Projects/pollcro:localhost:43993(clj)*:597:7).
Unable to resolve symbol: restart in this context
user> (start)
Syntax error compiling at (*cider-repl Projects/pollcro:localhost:43993(clj)*:600:7).
Unable to resolve symbol: start in this context

After fixing an error, I can not (restart) this reply anymore due to the following error:

Unable to resolve symbol: restart in this context

Is it always like this or am I doing something wrong with my REPL workflow?

As a workaround, I cider-quit my REPL session and start it again but that's very annoying.

It seems I can use mount/start after fixing an error:

user> (mount/start)
{:started
 ["#'app.server-components.config/config"
  "#'app.model.database/pool"
  "#'app.model.mock-database/conn"]}

But it completely loses my user namespace scope, REPL doesn't see any of its methods + I can't send user namespace to repl because of the following error:

Syntax error compiling at (app/server_components/pathom.clj:1:1).
namespace 'app.model.session' not found
("src/main" "src/dev" "src/test")#function[expound.alpha/printer]nil#'user/start#'user/stop#'user/restartnil


That's what my user namespace looks like:

(ns user
  (:require
    [clojure.tools.namespace.repl :as tools-ns :refer [set-refresh-dirs]]
    [expound.alpha :as expound]
    [clojure.spec.alpha :as s]
    [mount.core :as mount]
    ;; this is the top-level dependent component...mount will find the rest via ns requires
    [app.server-components.http-server :refer [http-server]]))

;; ==================== SERVER ====================
(set-refresh-dirs "src/main" "src/dev" "src/test")
;; Change the default output of spec to be more readable
(alter-var-root #'s/*explain-out* (constantly expound/printer))

(defn start
  "Start the web server"
  [] (mount/start))

(defn stop
  "Stop the web server"
  [] (mount/stop))

(defn restart
  "Stop, reload code, and restart the server. If there is a compile error, use:

  (tools-ns/refresh)

  to recompile, and then use `start` once things are good."
  []
  (stop)
  (tools-ns/refresh :after 'user/start))

Solution

  • Here's the workaround that seems to work in case of an error:

    1. the content of a fixed file need to be sent to the REPL
    2. switch back to user namespace
    3. send the content of your user namespace to the REPL
    4. there is no 4 - you just got your REPL back.

    At this point you can use your regular (start), (stop), (restart).