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
Install stack dependencies:
stack install intero QuickCheck hlint brittany ghcid
add
$HOME/.local/bin
to your $PATHInstall 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
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
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.
After that hlint
will suggest you some code improving. But keep it in mind, sometimes hlint
may be kind of rude :3
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
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.
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)
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...
Try using
resolver: lts-14.27
in your project'sstack.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 whatintero
is asking for as a maximum version forghc
and the snapshot you're using now is usingghc-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.
stack install intero
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 [...]
.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?
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.
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.
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?
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?
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.
Looks like we are finally got a simple way to integrate HIS into VS Code, check the UPD!