intellij-ideaclojureringcursive

How to debug a Clojure web application in Intellij?


I am using Intellij + Cursive and I want to debug a Clojure web application written using ring + compojure. I start the application in the Intellij terminal using lein and the ring plugin:

> lein ring server-headless

I want to debug this application using Intellij to set up breakpoints in the source code, see variables, etc.

But Intellij's Leiningen tab does not show a task with the ring command. Run configurations also do not have an option to run the ring command.


Solution

  • Intellij has a remote debug Run Configuration that can be used with Clojure.

    First add the following options to the jvm in the project.clj file:

    :jvm-opts ["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5010"]
    

    Where 5010 is port number to be specified in Intellij Remote Debug Configuration.

    Then, in Intellij, go to Run -> Run... -> Edit Configurations...Use the + button and choose Remote. Give a name to the configuration, change the port to 5010 and click OK. Run the application using lein:

    > lein ring server-headless

    After the application is running, run (in Intellij) the Intellij Remote Debug Configuration you created. You will be able to setup breakpoints, run line by line, etc.

    Without Leiningen

    Another option is to drop leiningen and run the ring application as a Clojure Application in Cursive. You have to add a -main function:

    (defn -main [] (run-jetty app {:port 8080})
    

    app is the function where you define your routes and use as ring handler :ring {:handler xxx/app} in project.clj. You have to require [ring.adapter.jetty :refer [run-jetty]] and debug the file in Intellij as a Clojure Application.