Skip to content

Instantly share code, notes, and snippets.

@dalegaspi
Last active March 17, 2019 17:12
Show Gist options
  • Save dalegaspi/30960a12b4921bc774da39f71be63555 to your computer and use it in GitHub Desktop.
Save dalegaspi/30960a12b4921bc774da39f71be63555 to your computer and use it in GitHub Desktop.
Moving from IntelliJ to Emacs + ENSIME for Scala Development on MacOS

Moving From IntelliJ to Emacs ENSIME for Scala Development on MacOS

Preface

I have been dependent on the use of IntelliJ for most of my development tasks but unfortunately it has given me an abundant source of frustration: despite its recent updates and fixes, it has become progressively slow and a resource hog even with conservative use of plugins and fancy features, which sometimes gets in the way of productivity.

Over the last few months, I have been purposely avoiding IntelliJ for some simple code edits if I don't have to.

Now I wouldn't exactly say Emacs with ENSIME is walk in a park--far from it. If you've been spoiled with the convenience of what IntelliJ provides out of the box, I would say switching to a mostly text editor would be a challenging endeavor.

Introduction

This will not include a tutorial of Emacs, but I will be pointing out some things that have been not-so-obvious to me as I was learning Emacs along with. This has been real challenge to me as even in the old days, I have been an vi user. As of this writing I am far from being a prolific Emacs user.

This also assumes that you are using sbt to compile/build your Scala projects.

Install Emacs

In this day and age, while I would still argue that MacPorts is better, the only viable package manager today with MacOS is Homebrew.

Once you install Homebrew, you can now install Emacs using the following command:

brew cask install emacs

Now if you're not familiar with Emacs at all, I would suggest that you pause here for a moment and do a crash course on it; otherwise following the rest of this article will be confusing.

Add MELPA Package Archive

A lot of the cool plugins in Emacs use the MELPA Archive, so we might as well get it set up.

Optional: Install Emacs Dracula Theme

The default theme that comes with the installer is not going to be easy on the eyes especially when you have been accustomed to the dark themes that's been available on all other IDEs. Fortunately, it's available on Emacs, too. On Emacs, install it by issuing the following command:

M-x package-install <RET> dracula-theme

Install ENSIME

At this point, you are ready to install ENSIME. Head on to this page and follow the instructions.

Set Up Project to Use ENSIME

I must admit, this is one of the painful parts of the exercise. It certainly tested my patience, enough that I almost gave up. Hopefully, the following information will ease up the pain and confusion a little bit.

  1. Edit (using your brand spanking new editor) ~/.sbt/0.13/plugins/plugins.sbt (or ~/.sbt/1.0/plugins/plugins.sbt if you're using sbt 1.x in your project) and add the following line:
addSbtPlugin("org.ensime" % "sbt-ensime" % "2.5.1")

This enables ENSIME as a global plugin for all your SBT projects.

  1. In the project's build.sbt file you will need to add in your settings:
scalaVersion in ThisBuild := "2.12.2"

Replace 2.12.2 with whatever version of Scala your project uses. This prevents ENSIME from complaining about inconsistency of Scala version and die.

  1. Ensure that you have Java 8 installed and/or updated to the latest version. Either use Homebrew or the usual MacOS installer.
  2. Now you're about to create an .ensime file needed by ENSIME to be able to edit your project in Emacs. Fire up a Terminal then run sbt. You should see some messages about ensime plugin being loaded.
> ensimeConfig

This will take a while depending on your project and the number of dependencies; As you probably know already, this download dependency process can be a bit flaky. Refer to this page if you rencounter any HTTP 416 errors. If all else fails, you can always just delete your ~/.ivy and/or ~/.coursier cache then repeat.

  1. At this point, you should be able to load your project in Emacs with ENSIME! Open one of your .scala sources. Run ENSIME by:
M-x ensime

This should run the ENSIME server and you should see its status in one of the Emacs buffers. Once it's ready, everything should just work: auto completion, real-time error detection, source navigation, refactoring.

  1. To debug your app, go to ENSIME sbt console then run
ensimeRunDebug your.fully.qualified.AppName

Now this next information is key: if you are following this project structure pattern for your sbt project, you will need to run the debug mode a bit differently.

You should be able to attach to your application with ensime by specifying host as localhost and port as 5005. You should be able to set breakpoints and inspect variables and stacktraces.

Troubleshooting

If you ever encounter an error where the debugging doesn't work, chances are some other process is using port 5005. To know what process that is, run this command:

sudo lsof -i tcp:5005

If for some reason the sbt console is not starting in your project directory, chances are you have a /project folder in your home directoy. You will need to delete or rename it per this StackOverflow answer.

Epilogue

Now as I have mentioned before, this is all new to me as I write this. At some point I may abandon this whole thing and come crawling back to using IntelliJ. But so far most of the pain points due to my inability to be at home with Emacs. Hopefully, that's a challenge I will overcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment