DEV Community

Cover image for VS Code for Haskell in 2020
Egregors
Egregors

Posted on • Edited on

VS Code for Haskell in 2020

01 – VS Code for Haskell in 2020
02 – Your first Stack project (build, test, dist)

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 and stack

  • 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 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 installer for this purpose. Stack is a kind of package manager for Haskell. Like cabal, but not quite. 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
Enter fullscreen mode Exit fullscreen mode

or download installer for Шindows

Haskelly

Next, we'll install one of the popular vscode extension 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Next, we need to install vscode integrations for hlint: 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

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

stack install brittany
Enter fullscreen mode Exit fullscreen mode

And 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 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

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

Hoogle

Just shortcut for Hoogle search.

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

Top comments (10)

Collapse
 
mindaugasw profile image
Mindaugas Vaitiekūnas

How do you install intero? I'm getting some error which seems to be about version mismatch. Neither of the suggested solutions in the console work. Same issue both on windows and linux. And on the internet also can't seem to find any working solution...

Error: While constructing the build plan, the following exceptions were
encountered:

In the dependencies for intero-0.1.40:
    ghc-8.8.3 from stack configuration does not match >=7.8 && <=8.6.5  (latest
              matching version is 8.6.5)
needed since intero is a build target.

Some different approaches to resolving this:

  * Set 'allow-newer: true'
    in /home/mindaugas/.stack/config.yaml to ignore all version constraints and build anyway.

  * Recommended action: try adding the following to your extra-deps
    in /home/mindaugas/.stack/global-project/stack.yaml:

- ghc-8.6.5@sha256:3591225289bdf8cf8f62b10f2aebc9ea3e25a15294545a5312a419e0317784f9,13976

Plan construction failed.
Collapse
 
edgecase_mmt profile image
Edge Case

Try using resolver: lts-14.27 in your project's stack.yaml or in the {stack-root}/global-project/stack.yaml

The snapshot for stackage.org/lts-14.27 uses ghc-8.6.5 which is what intero is asking for as a maximum version for ghc and the snapshot you're using now is using ghc-8.8.3 which is too high.

Not sure if you can use different versions of ghc between the projects and the ide software, so you might be stuck needing to use that lts snapshot for all your projects you plan to code on in vscode.

Hope this helps.

Collapse
 
jasonsbarr profile image
Jason Barr

stack install intero

RedownloadInvalidResponse Request {
  host                 = "raw.githubusercontent.com"
  port                 = 443
  secure               = True
  requestHeaders       = []
  path                 = "/fpco/lts-haskell/master//lts-15.1.yaml"
  queryString          = ""
  method               = "GET"
  proxy                = Nothing
  rawBody              = False
  redirectCount        = 10
  responseTimeout      = ResponseTimeoutDefault
  requestVersion       = HTTP/1.1
}
 "C:\\sr\\build-plan\\lts-15.1.yaml" (Response {responseStatus = Status {statusCode = 404, statusMessage = "Not Found"}, responseVersion = HTTP/1.1, responseHeaders = [("Connection","keep-alive"),("Content-Length","15"),("Content-Security-Policy","default-src 'none'; style-src 'unsafe-inline'; sandbox"),("Strict-Transport-Security","max-age=31536000"),("X-Content-Type-Options","nosniff"),("X-Frame-Options","deny"),("X-XSS-Protection","1; mode=block"),("Via","1.1 varnish (Varnish/6.0)"),("X-GitHub-Request-Id","8524:489A:0056:006F:5E54091D"),("Accept-Ranges","bytes"),("Date","Mon, 24 Feb 2020 17:34:23 GMT"),("Via","1.1 varnish"),("X-Served-By","cache-chi21128-CHI"),("X-Cache","HIT, MISS"),("X-Cache-Hits","1, 0"),("X-Timer","S1582565663.254117,VS0,VE21"),("Vary","Authorization,Accept-Encoding"),("Access-Control-Allow-Origin","*"),("X-Fastly-Request-ID","c87bcd6b5c2f58d489911cf06e3fcfc5485e2fb0"),("Expires","Mon, 24 Feb 2020 17:39:23 GMT"),("Source-Age","0")], responseBody = (), responseCookieJar = CJ {expose = []}, responseClose' = ResponseClose})

I don't know enough about it to know what I'm doing wrong or why it's not working, and trying to Google for info isn't helping.

Things like this make me really appreciate npm install -g [...].

Collapse
 
egregors profile image
Egregors

This is looks like some network failure. Just checked clean install stack dependencies on the Windows, and all seems works (Windows 10 Pro, x64). Please, check your network and try again. If you'll got this error one more time, could you please sent me more information about your system and setups?

Collapse
 
jasonsbarr profile image
Jason Barr • Edited

By the way, intero is no longer being developed and will likely stop working with some unspecified future version of GHC, which means Haskelly will also not fully work anymore.

I installed haskell-ide-helper and the VSCode Haskell language server extension instead of intero and Haskelly on the advice of vacationlabs.com/haskell/environme..., which I found after the aforementioned Github issue thread.

Collapse
 
jasonsbarr profile image
Jason Barr

It turns out I needed to run stack update to get current resolver values. I finally figured it out earlier today.

It took 2+ days to find a resource that mentioned doing this (it was a Github issue thread for some other project, the name of which escapes me at the moment). Not a single one of the tutorials I found on setting up VS Code for Haskell or otherwise getting started with Haskell/Stack mentioned it.

If I didn't really want to learn Haskell I would have skipped out much earlier.

Collapse
 
dretch profile image
Gareth Smith

Thanks, this is useful.

One remaining question: how does vscode-hie-server (github.com/alanz/vscode-hie-server) fit into the picture?

Will it replace the Haskelly extension (which appears to be unmaintained)? Would you recommend using it yet?

Collapse
 
egregors profile image
Egregors

Thanks for appreciating :)

last time I have check out HIS, I got a bunch of problems with Windows installations and stability on Mac. Will it replace the Haskelly extension? It probably will. I should try it someday.

Also, if you already tried this setup with HIS, please tell us how it's going?

Collapse
 
dretch profile image
Gareth Smith

Hi Egregors,

I briefly tried using HIS, but it didn't seem to work (with no indication why), so I went back to the setup described here (which does work for me).

I'll try again in future, I guess.

Thread Thread
 
egregors profile image
Egregors

Looks like we are finally got a simple way to integrate HIS into VS Code, check the UPD!