cabal init Now Creates Main.hs


One of the small annoyances I have with cabal init is that it doesn't automatically create a Main.hs. It creates the .cabal file and directory structure (including the Haskell source directory), just not the Main.hs file.

I opened up an issue about this on Cabal's issue tracker. After some debate, I created a created a pull request and it was merged.

With this patch, when the following three conditions hold, cabal init will create a Main.hs:

  • you are creating an executable (not a library)
  • the main module of the executable has been specified
  • the main module does not already exist

Here is a small example of what will happen when you run cabal init:

$ mkdir foobar
$ cd foobar
$ cabal init
What does the package build:
   1) Library
   2) Executable
Your choice? 2
What is the main module of the executable:
 * 1) Main.hs (does not yet exist, but will be created)
   2) Main.lhs (does not yet exist, but will be created)
   3) Other (specify)
Your choice? 1
$ ls  LICENSE  Main.hs  Setup.hs  foobar.cabal

The contents of Main.hs are as follows:

module Main where

main :: IO ()
main = putStrLn "Hello, Haskell!"

Now you can immediately run cabal build or cabal run after cabal install without having to manually create the Main.hs file.

cabal-install Version

In order to get a cabal binary with this functionality, you either need to install cabal from the master branch on Github, or wait until cabal-install-1.24 is released.

tags: haskell