How do I set up IntelliJ to build Haskell projects with Stack?

W.P. McNeill picture W.P. McNeill · Jun 10, 2016 · Viewed 9.8k times · Source

I am using Stack to setup, build, and run my Haskell projects from the command line. I want to use IntelliJ as my IDE but am running into problems configuring Stack as my build tool.

I used Stack to create and run a "Hello, World" Haskell program on the command line following the instructions in the Stack User Guide. Everything worked fine.

I have the HaskForce plugin installed on IntelliJ. The Build, Execution, Deployment -> Compiler -> Haskell Compiler tab gives you a choice between building with Stack and building with Cabal. I selecte Build with stack and configured it like so.

enter image description here

I can run the project using a Haskell Stack Run configuration. The console output is correct.

/Users/williammcneill/Library/Haskell/bin/stack exec hellohaskell-exe --
Hello, Haskell.

However, I cannot figure out how to rebuild the project through the IDE. For example, if I change the output text and rerun the program, I still see the original "Hello, Haskell" output. Build -> Make Project | Make Module | Rebuild Project all do nothing.

The Project Settings->Artifacts tab has nothing listed, and I don't see a way to add a Haskell executable here.

My workaround is to build my Haskell programs from the command line even when I'm editing from the IDE.

(It also looks strange to me to specify a path to a project-specific stack.yaml file in the general Haskell compiler settings, but that was the only way I could see how to use Stack to build.)

How do I set up IntelliJ to build Haskell projects using Stack?

ghc 7.10.3, stack 1.1.2, HaskForce 0.3-beta.33, IntelliJ IDEA Ultimate 2016.1.3, OS X 10.11.5


This is Haskforce issue 282.

Answer

PHPirate picture PHPirate · Jun 24, 2018

If you want to hit the IntelliJ run button to run the project and use Stack, I think you should use the IntelliJ Haskell Plugin as @mrek said. The HaskForce plugin says at the moment

Note: Run configurations are not supported for stack at the moment. This is for cabal projects only.

The install instructions for everything (from installing Stack to project setup) are in the readme, but I needed to change it slightly so I put my version here.

Installation instructions for everyone who wants to use Haskell

  1. Install the IntelliJ-Haskell plugin, I would recommend to install the latest beta release from GitHub.
  2. Install Stack, instructions in their docs (scroll down for your specific OS).
  3. In a terminal/command prompt, run stack install hindent and stack install stylish-haskell (this can take a while).
  4. Restart your computer.
  5. Make a new project in IntelliJ of type Haskell module, select the Stack binary (probably C:\Users\username\AppData\Roaming\local\bin\stack.exe or /usr/bin/stack) and Set Default (if it is not already default).
    • You can also import an existing Stack project with File | New | Project from Existing Sources, click import from Haskell Stack.
  6. Switch on an extra log with File | Settings | Appearance | Notifications | Haskell Log. When something is building which takes a long time you can see progress here (bottom right, Event Log).
  7. You should have a file app/Main.hs. You could replace everything in it with main = putStrLn "Hello Haskell!".
  8. Now do as @mrek says: create a run configuration Haskell Stack | Haskell Stack Runner, and run it. You should see the hello message.
  9. Even better, answering the original question: when you change the message and run again the output has changed!
  10. In the same way, you can make a test run configuration (Haskell Stack Tester) which runs your tests.

You can start up the interactive console by clicking Terminal at the bottom of IntelliJ and then running stack ghci. When you type main you are calling main, and so on. You could also use stack test instead of the test run configuration.