haskellcabal

cabal test fails to find Tests.hs file


I have the following structure inside my Haskell project:

...
test
  |
  -- Tests.hs

Inside my .cabal file:

-- The entrypoint to the test suite.
    main-is: Tests.hs

The error message I get is:

: error:

Output was redirected with -o, but no output will be generated. There is no module named ‘Main’.

However, when I change the top declaration in my Tests.hs file from

module Tests where

to

module Main where

and change .cabal file accordingly the test works. Why is cabal forcing me to use Main as the module name to find tests ?


Solution

  • A test is just a regular Haskell program, and for executables ghc (by default) wants you to name your entrypoint function main and put it in a module called Main. The file for the Main module does not need to match the module name to allow multiple Main modules to exist in the same directory.

    You can override that with the GHC option -main-is, however the cabal main-is option (unfortunately the same name with a different meaning) does not set that option, it only specifies which file contains the main module; E.g. you could write ghc -main-is Tests.tests test/Tests.hs and that would work. You could specify this in cabal with

    ghc-options: -main-is Tests.tests

    or put it on top of your file as a pragma

    {-# OPTIONS_GHC -main-is Tests.test #-}

    but you're probably better off just leaving it at the default of Main.main if you don't have any special reason for wanting it to be different.