VS Code for Haskell in 2020
Tags: haskell, vscode
Published: 2020-01-20 05:35:30

01 – VS Code for Haskell in 2020
02 – [Your first Stack project (build, test, dist)](https://dev.to/egregors/your-first-stack-project-build-test-dist-10fa)

**UPD**

So, for now, you don't need any of the mess below! Thanks to guys making `hie-server`, you finally can just download VS Code plugin and this is all: please check this out: https://mpickering.github.io/ide/posts/2020-07-10-ghc-libdir.html 

Looks like `vscode-hie-server`, will be hosted under the Haskell organization, and it's just working :D


_I have left this guide here, just to remember how tough it was._

**Outdated** 👇

If you trying to find some Haskell IDE guides, most of them probably will be about `emacs` and `hashell-mode`. But in this case, you will need to deal with `emacs` first. And if you haven't any experience with this kind of IDE it will take a while.

Also, for some reason, you would wanna fool around with Haskell on the Windows with the same "good enough" experience like in the macOS or Linux.

That's why I believe `vscode` with a few very useful extensions will be a better solution

So this little guide let you get absolutely the same Haskell programming experience, independently from OS

## TL;DR

* get [vscode](https://code.visualstudio.com/) and [stack](haskellstack.org)

* Install stack dependencies:
`stack install intero QuickCheck hlint brittany ghcid`

* add `$HOME/.local/bin` to your $PATH

* Install VS Code extensions (IDE, linter, formatter, hoogle). You should have [code](https://code.visualstudio.com/docs/setup/mac#_launching-from-the-command-line) in your $PATH:
`code \
  --install-extension ucl.haskelly \
  --install-extension hoovercj.haskell-linter \
  --install-extension maxgabriel.brittany \
  --install-extension jcanero.hoogle-vscode
`

* restart vscode ... profit!

## Setup with details

### Haskell Platform | Stack

First of all, you should install Haskell and a few tools. I recommend use [stack](https://www.haskellstack.org) installer for this purpose. Stack is a kind of package manager for Haskell. Like [cabal](https://www.haskell.org/cabal/), but [not quite](https://stackoverflow.com/a/30922706). It will install GHC automatically as well. Next, it will be useful tool for working with isolated dependencies, `ghci` REPL, building and testing your projects.

The simplest way is just to execute install script from the official site for unix-like OS: 
```
curl -sSL https://get.haskellstack.org/ | sh
```
or download [installer for Шindows](https://get.haskellstack.org/stable/windows-x86_64-installer.exe)


### Haskelly

Next, we'll install one of the popular vscode [extension](https://marketplace.visualstudio.com/items?itemName=UCL.haskelly) for Haskell. It contains:

* highlight
* snippets 
* hovers
* jump to definition
* find references
* code completion 
* integrated REPL 
* Build, Test and Run commands

Exclude the vscode extension you should install a few Haskell packages (don't forgate to add `$HOME/.local/bin` to your $PATH):
```
stack install intero QuickCheck  # for a global installation
stack build intero QuickCheck    # for a local installation
```

Especially I like `load GCHi` feature. When you're starting with Haskell you'll spend a lot of time in GHCi, and eventually, it becomes a kind of habit. You know, you are writing a beautiful function in the context of your current file, and you wanna test it within context. `Load GHCi` starts GHCi and automatically loads all stuff from your current file. And you got a useful way of playing around with your functions. Make some changes, safe files, and just type `:r` to reload a new version into GHCi.

We are almost here. This is the minimum setup for pleasant Haskell development, but we'll make it even better. Much better!

### Hlint
`hlint` is an absolute must-have for any beginner | middle Haskell developer. This is the linter that teaches you Haskell even better than books! From docs: `HLint is a tool for suggesting possible improvements to Haskell code. These suggestions include ideas such as using alternative functions, simplifying code and spotting redundancies.`

Install by `stack`
```
stack install hlint
``` 
Next, we need to install vscode integrations for `hlint`: [vscode-haskell-linter](https://github.com/hoovercj/vscode-haskell-linter).

After that `hlint` will suggest you some code improving. But keep it in mind, sometimes `hlint` may be kind of rude :3

![use head](https://s3-eu-west-1.amazonaws.com/egregors.com/devto/hlint-use-head.png)

### Brittany

Next one. If we are talking about indentations it's always difficult in Haskell. That's why you definitely need some code formatting tool. Personally, I prefer [brittany](https://github.com/lspitzner/brittany)
```
stack install brittany
```
And [brittany-vscode-extension](https://github.com/MaxGabriel/brittany-vscode-extension) as well. This tool applies to be the default "Format Document" command or shortcut.

### ghcid 

It does not look like a big deal but in fact, this tool becomes an absolutely [necessary thing](https://github.com/ndmitchell/ghcid) for a smooth workflow. `To a first approximation, it opens ghci and runs :reload whenever your source code changes, formatting the output to fit a fixed height console.` 

So, usually, I have split up the window to three parts: 
source code, GHCi with loaded up current code and ghcid like a realtime indicator that all is good. 

![flow](https://s3-eu-west-1.amazonaws.com/egregors.com/devto/flow.png)

To run `ghcid` you should use `stack exec ghcid YOUR_FILE_NAME.hs` command

### Hoogle
Just shortcut for [Hoogle](hoogle.haskell.org) search. 

All of this should be enough to comfortable Haskell learning/development independently of your platform