When I started to learn Haskell, I hoped that I’ll be able to get similar IDE experience as I have with Scala and IntelliJ Idea. Unfortunately the opposite was true. There are some Haskell plugins for Idea, but none of them worked for me (mostly due to crashes or hangs) and they seem to be pretty immature as well. But I found pretty good feedback on using VSCode for Haskell development, so I tried it and with help of few extensions, you get pretty decent IDE-like experience with syntax highlighting, error reporting and code completion. In this blogpost, I’ll present my VSCode setup I use for (almost) everyday Haskell development.
1 Installation steps
Please note that I use macOS as my operating system so some things in this blog post may be macOS-specific. However, most things (except installing dependencies) should be similar for any OS.
1.1 Stack - the build tool
If you already don’t have Stack installed, this is the first step you need to do. Stack is a build tool for Haskell we will use to build some packages from source. One of its great features is that it will download correct version of GHC. It can be easily installed using Homebrew:
1.2 Syntax Highlighting
We definitely want to have some syntax highlighting for the Haskell code. This can be done by installing the Haskell Syntax Highlighting extension. Now the code looks more fancy.
1.3 Brittany - the code formatter
Brittany is one of the popular source code formatters for Haskell that integrates well with other tools you’ll install based on this tutorial. We need to install both the Britanny executable itself and the VSCode extension.
1.3.1 Installing the executable
Brittany is not available on Homebrew, but we can install it using Stack:
If this is the very first thing ever you use Stack for, then it may take some time. Stack needs to download correct version of GHC and build Brittany and all its dependencies from source code, so maybe it’s time to have some coffee :-)
1.3.2 Installing the VSCode extension
To integrate installed executable with VSCode, you need to install the brittany extension to VSCode. After that, source code can be formatted by hitting the ⇧⌘F combo.
1.4 HLint - the linter
HLint is linter for Haskell that is really useful for simplifying code and searching for redundancies. Similar to Brittany, we need to install both the executable and the VSCode extension.
1.4.1 Installing the executable
HLint is available through Homebrew and can be installed using following command:
1.4.2 Installing the VSCode extension
To integrate HLint with VSCode, we need to install the haskell-linter extension. After that, suggestions will be visible directly from the VSCode:
1.5 Haskell IDE Engine - the IDE experience
The last, but important step is to install the Haskell IDE Engine. This is the engine that communicates with VSCode via Language Server Protocol and provides such features as displaying compilation warnings/errors, code completion and showing documentation when you mouse hover the selected function. Again we need to install the executable and VSCode extension.
### Installing the executable Unfortunately installing the Haskell IDE Engine is little bit more tedious. It needs to be installed from source code and needs to be installed for the specific GHC version you use. For all my projects I use the LTS Haskell 14.* which uses the GHC version
8.6.5, so I’ll assume this version in following steps. So as the first step, we need to download the source codes:
Now, we need to build it using the following commands:
This may take some time so it may be good time for another coffee :-)
1.5.1 Installing the VSCode extension
Last step is to install the Haskell Language Server extension. This will enable all the fancy features described above, on screenshot below you can see example how documentation is shown for particular function on mouseover:
I hope that one day the status of tooling for Haskell will be at least as good as it is for Scala, where the IntelliJ Idea really provides great development experience. On the other hand, VSCode is pretty nice lightweight editor and with good choice of extensions, it still does great job for Haskell development. I really like the HLint integration, that shows possible code improvements, which is extra helpful for beginner Haskellers. And the Haskell IDE Engine gives nice features such as showing documentation, displaying code errors, etc. Main drawback is the complexity of the setup, mainly for the Haskell IDE Engine where it needs to be done per GHC version, but hopefully this will be improved in the future releases.