Sean Kelleher
Sean Kelleher

Reputation: 2102

Cabal output is redirected but not generated

I have a fairly simple haskell project set up, where I just want to have the framework working with testing and so on before I actually start coding. I have my source files for an executable in a /src directory (where / is the project's root) and my tests in a /testsuite directory. /testsuite contains a simple test file called TestSuite.hs with main = Test.Framework.defautMain tests as the implementation of main. The problem is, when I run

cabal clean && cabal configure --enable-tests && cabal build

I get the warning

output was redirected with -o, but no output will be generated because there is no main module.

The build works fine when I don't specify --enable-tests. My cabal file is:

Name:                Example
Version:             0.1
Synopsis:            Synopsis
Description:
    Long description.
License:             GPL
License-File:        LICENSE
Author:              SeanK
Maintainer:          [email protected]
Category:            Development
Build-Type:          Simple
Cabal-Version:       >= 1.8

Test-Suite test
    Type:               exitcode-stdio-1.0
    Main-Is:            TestSuite.hs
    Build-Depends:      base >= 3 && < 5,
                        test-framework,
                        test-framework-quickcheck
                        -- QuickCheck
    Hs-Source-Dirs:     src,
                        testsuite

Executable example
    Main-Is:            Main.hs
    -- Other-Modules:       
    Build-Depends:      base >= 3 && < 5,
                        haskell98
    Hs-Source-Dirs:     src
    Ghc-Options:        -Wall

I have QuickCheck disabled because I'm not using (==>) at the moment, which is the only function I currently need from it. The rest should be straight-forward. Any help would be greatly appreciated.

Upvotes: 10

Views: 2225

Answers (3)

cjs
cjs

Reputation: 27281

If you're using an hpack-compliant system such as Stack, cabal2nix or just the hpack tool, you can generate the .cabal file from a package.yaml file. For entries under executables: in package.yaml you may specify a main: key that works in one of two ways:

  1. Given an .hs filename that filename will be copied directly into the main-is: line in the generated Cabal file, providing the Cabal semantics.

  2. Given a module name or module and exported definition name (e.g., Foo or Foo.startup) the appropriate ghc-options: -main-is ... will be added to the generated Cabal file and main-is: will be a filename generated from the module name (e.g. main-is: Foo.hs).

If you're not wanting to use a package.yaml but are having difficulty with getting your Cabal file to work correctly, it may still be worth setting up a temporary, minimal package.yaml and running hpack on it just to see the output it produces and confirm that you're writing your Cabal file the same way.

For what it's worth, here's an example of an exectuables: section from package.yaml in one of my recent projects:

executables:
  hello:
    source-dirs: src/
    main: Hello.main

and the relevant fragments of the code in my generated .cabal file:

executable hello
  main-is: Hello.hs
  hs-source-dirs:
    src/
  ghc-options: -main-is Hello

Upvotes: 0

Lii
Lii

Reputation: 12132

Instead of renaming the TestSuite module as Fedor suggests you could add a GHC option to set the name of the main module to your Cabal file:

Test-Suite testFedor
    ghc-options:    -main-is TestSuite

Apparently Cabal main-is and GHC main-is are different. I don't know in what way.

Upvotes: 4

Fedor Gogolev
Fedor Gogolev

Reputation: 10561

You should define module name in your TestSuite.hs file as Main, like there for example.

A quote from The Haskell 98 Report:

A Haskell program is a collection of modules, one of which, by convention, must be called Main and must export the value main.

Upvotes: 17

Related Questions