Skip to content

Running figwheel in a Cursive Clojure REPL

jonoflayham edited this page Jun 7, 2020 · 39 revisions

This has been tested with the following versions (but may work with other versions as well):

  • figwheel-sidecar "0.5.18"
  • cursive clojure 1.8.2-eap3-2019.1
  • clojure 1.10.0
  • clojurescript 1.10.520

However, it breaks checkout dependencies, so beware if you rely on them!

Overview

These are the steps you need to take to get a ClojureScript figwheel REPL going in Intellij: (see full details below)

  • Create a figwheel project and tweak your project.clj
  • Create a script/repl.clj file
  • Create a clojure.main Cursive REPL Configuration

Create figwheel project and tweak your project.clj

Create a new ClojureScript project based on the figwheel template by running lein new figwheel figwheel-test in your terminal.

This will create a figwheel project called figwheel-test based on the figwheel template.

cd into your project's newly created dir and in the project.clj remove lein-figwheel from :plugins. You'll get a clojure.lang.ArityException if there are any lein-figwheel left in your project.

Change your :dependencies to include figwheel-sidecar:

Your :dependencies and :plugins should look like the following:

(defproject figwheel-test "0.1.0-SNAPSHOT"
  ...
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [org.clojure/clojurescript "1.10.520"]
                 [org.clojure/core.async "0.4.474"]
                 [figwheel-sidecar "0.5.18"]]

  :plugins [[lein-cljsbuild "1.1.7"]]
  ...)

Create a script/repl.clj file

Create a script/repl.clj file, it's contents should look like the following:

(use 'figwheel-sidecar.repl-api)
(start-figwheel!) ;; <-- fetches configuration 
(cljs-repl)

Don't forget to add the script folder to :source-paths in project.clj if it's not there already.

note: leiningen profile merging won't occur when fetching config from project.clj

Create a clojure.main Cursive REPL Configuration

  • Click Run->Edit configurations.
  • Click the + button at the top left and choose Clojure REPL
  • Choose a Local REPL
  • Enter a name in the Name field (e.g. "REPL")
  • Choose the radio button Use clojure.main in normal JVM process
  • In the Parameters field put script/repl.clj
  • Click the OK button to save your REPL config.

Connect!

In Intellij make sure your REPL config is selected and click the green "play" button to start your REPL.

You should see the following if everything is working correctly:

Focusing on build ids: dev
Figwheel Controls:
          (stop-autobuild)                ;; stops Figwheel autobuilder
          (start-autobuild [id ...])      ;; starts autobuilder focused on optional ids
          (switch-to-build id ...)        ;; switches autobuilder to different build
          (reset-autobuild)               ;; stops, cleans, and starts autobuilder
          (build-once [id ...])           ;; builds source one time
          (clean-builds [id ..])          ;; deletes compiled cljs target files
          (fig-status)                    ;; displays current state of system
          (add-dep [org.om/om "0.8.1"]) ;; add a dependency. very experimental
  Switch REPL build focus:
          :cljs/quit                      ;; allows you to switch REPL to another build
    Docs: (doc function-name-here)
    Exit: Control+C or :cljs/quit
 Results: Stored in vars *1, *2, *3, *e holds last exception object
Prompt will show when figwheel connects to your application
Type `:cljs/quit` to stop the ClojureScript REPL

If you don't see the output above try invalidating IDEA's caches and restarting (File -> Invalidate Caches/Restart). This clears out any residual classpath meta-data, and it will take a moment for IntelliJ to rebuild it from scratch. Once it finishes, try starting your REPL again.

Opening Files in IntelliJ (optional)

To open files within IntelliJ from the figwheel error message, create a ~/bin/open-in-intellij script similar to the following:

#!/bin/sh
FILENAME=$1
LINENUM=$2

curl -silent -o /dev/null http://localhost:63342/api/file/$FILENAME:$LINENUM

To allow API access without manually accepting each request, search the settings for "allow unsigned requests" and check the corresponding checkbox.

Next, add the :open-file-command option to the root level :figwheel configuration in project.clj:

(defproject figwheel-test "0.1.0-SNAPSHOT"
  ...
  :figwheel {:open-file-command "open-in-intellij"}
  ...)

An alternative is to use the idea command line launcher tool that IntelliJ provides instead of making a CURL call.

#!/bin/sh
FILENAME=$1
LINENUM=${2:-1}
COLNUM=${3:-0}

idea --line $LINENUM --column $COLNUM $FILENAME