How can I attach CIDER's debugger to a Luminus web application?
Asked Answered
T

1

8

I have a Luminus project with the server running on localhost:7000. I use cider-connect to attach myself to this repl, but I'm met with the following wall:

; CIDER 0.10.0snapshot (package: 20150820.852) \
    (Java 1.7.0_51, Clojure 1.7.0, nREPL 0.2.10)
WARNING: The following required nREPL ops are not supported: 
apropos classpath complete eldoc format-code format-edn info inspect-pop \
    inspect-push inspect-refresh macroexpand ns-list ns-vars ns-path refresh \
    resource stacktrace toggle-trace-var toggle-trace-ns undef
Please, install (or update) cider-nrepl 0.10.0-SNAPSHOT and restart CIDER

I would imagine at least one of these is required for debugging; attempts to instrument a function yield

clojure.lang.LispReader$ReaderException: java.lang.RuntimeException: \
    No reader function for tag dbg

I can debug just fine with a 'normal' Clojure project. I've tried adding the boilerplate that normally goes in ~/.lein/profiles.clj to the project's project.clj to no effect:

:profiles/dev {:dependencies [[org.clojure/tools.nrepl "0.2.10"]]
               :plugins [[cider/cider-nrepl "0.10.0-SNAPSHOT"]]}

How can I attach CIDER's debugger to this web application?

Trypanosomiasis answered 22/8, 2015 at 14:4 Comment(5)
I'm not sure about cider, but have you seen this: luminusweb.net/docs/deployment.md#enabling_nreplDiaspora
@Ballpark Oh, I actually figured this one out! I forgot that's what this question was about. I'll self-answer tonight.Trypanosomiasis
@SeanAllred I'd like to see that answerPollinize
@SeanAllred It would be good to post that answer here so other people who eventually have similar problem can solve it.Abstinence
@foki alas – I always seem to be making promises on here that I don't keep when it comes to self-answering. I've added a todo item, but the list is long. See gitter.im/clojure-emacs/cider?at=55d8c4dfb6d887d9704d21f5 in the meantime.Trypanosomiasis
A
10

In the interest of saving time of people who have similar issue I'll post summary here as an answer. Thanks to Benedek Fazekas from clojure-emacs/refactor-nrepl's gitter and Sean Allred.


To attach Cider to Luminus application (and probably any other application which uses embedded nrepl server as an opportunity of connecting to it while it's running) you should do two things:

  • Provide all desired middleware as :handler to clojure.tools.nrepl.server/start-server. To do this you should have libraries which contain middleware listed somewhere where lein can find them (e.g. in project.clj :dependencies).

  • Run application which launches embedded nrepl server and connect Cider to it using cider-connect.

Beside cider-nrepl I want to use refactor-nrepl which is needed by Clojure refactor. With example project from Luminus website first thing can be done like this:

(ns guestbook.core
  (:require [guestbook.handler :refer [app init destroy]]
            [immutant.web :as immutant]
            [guestbook.db.migrations :as migrations]
            [clojure.tools.nrepl.server :as nrepl]
            [clojure.tools.logging :as log]
            [environ.core :refer [env]]
            [cider.nrepl :refer [cider-middleware]]
            [refactor-nrepl middleware])
  (:gen-class)) 

    ...

(defn cider&cljr-nrepl-handler []
      (apply nrepl/default-handler (cons #'refactor-nrepl.middleware/wrap-refactor
                                   (map resolve cider-middleware))))
        
(defn start-app
  "e.g. lein run 3000"
  [[port]]
  (let [port (http-port port)]
    (.addShutdownHook (Runtime/getRuntime) (Thread. stop-app))
    (when-let [repl-port (env :nrepl-port)]
      (repl/start {:port (parse-port repl-port)
                   :handler (cider&cljr-nrepl-handler)}))
    (http/start {:handler app
                 :init    init
                 :port    port})))

Then you can run your Luminus app with lein run and connect it with Cider using cider-connect(M-x cider-connect or C-c M-c), by default url to connect is localhost:7000.


Presumably, embedded nrepl server in Luminus is intended to be used when application is already deployed somewhere and you want to do some live debugging. For local development there is standard way:

  • run lein repl in the project root. Cider will do the job with lein repl :headless on cider-jack-in(C-c M-j).
  • it should launch repl in yourapp.core namespace where you can run (http/start {:port 3000 :init init :handler app}) and enjoy! In this case we don't need embedded nrepl server. To stop http server run (http/stop destroy). It is described here.
Abstinence answered 5/1, 2016 at 11:32 Comment(1)
Thanks a ton for writing this up :-)Trypanosomiasis

© 2022 - 2024 — McMap. All rights reserved.