George
George

Reputation: 7317

cabal error when building a simple tutorial project

1 The Problem

I am following the tutorial How to Write a Haskell Program, in which you build the project haq.

Whenever I try to run cabal test, I get the following error snippet:

<command line>: cannot satisfy -package-id haq-0.1.0.0-inplace
    (use -v for more information)
    cabal: Error: some packages failed to install:
    haq-0.1.0.0 failed during the building phase. The exception was:
    ExitFailure 1

Notice that the package it cannot build is itself (at least I think that's what this error means).

2 The Details

2.1 The Full Error

$ cabal install --enable-tests
Resolving dependencies...
Configuring haq-0.1.0.0...
Building haq-0.1.0.0...
Failed to install haq-0.1.0.0
Build log ( /home/user/.cabal/logs/haq-0.1.0.0.log ):
Configuring haq-0.1.0.0...
Warning: The 'license-file' field refers to the file 'LICENSE' which does not
exist.
Building haq-0.1.0.0...
Preprocessing executable 'haq' for haq-0.1.0.0...
[1 of 1] Compiling Main             ( Haq.hs, dist/build/haq/haq-tmp/Main.o )
Linking dist/build/haq/haq ...
Preprocessing test suite 'tests' for haq-0.1.0.0...
<command line>: cannot satisfy -package-id haq-0.1.0.0-inplace
    (use -v for more information)
    cabal: Error: some packages failed to install:
    haq-0.1.0.0 failed during the building phase. The exception was:
    ExitFailure 1

2.2 Project File Structure

.
├── .cabal-sandbox
├── cabal.sandbox.config
├── dist
├── haq.cabal
├── Haq.hs
├── HSpecTests.hs
└── Setup.hs

2.3 Haq.hs

    --
    -- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons/
    -- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
    --
    import System.Environment

    -- | 'main' runs the main program
    main :: IO ()
    main = getArgs >>= print . haqify . head

    haqify s = "Haq! " ++ s

2.4 haq.cabal

    -- Initial haq.cabal generated by cabal init.  For further documentation, 
    -- see http://haskell.org/cabal/users-guide/

    name:                haq
    version:             0.1.0.0
    -- synopsis:            
    -- description:         
    -- license:             
    license-file:        LICENSE
    author:              First Last
    maintainer:          [email protected]
    -- copyright:           
    -- category:            
    build-type:          Simple
    -- extra-source-files:  
    cabal-version:       >=1.10

    executable haq
        main-is:             Haq.hs
        -- other-modules:       
        -- other-extensions:    
        build-depends:       base >=4.8 && <4.9
        -- hs-source-dirs:      
        default-language:    Haskell2010

    test-suite tests
        ghc-options: -Wall
        default-extensions:  OverloadedStrings
        type: exitcode-stdio-1.0
        main-is: HSpecTests.hs
        build-depends:       base,
                                                 haq,
                                                 hspec                >= 1.8
        default-language:    Haskell2010

2.5 HSpecTests.hs

    module Main where

    import Haq
    import Test.Hspec

    main :: IO ()
    main = hspec $ do

        describe "Validate haqify function" $ do
            it "haqify is supposed to prefix Haq! to things" $ do
                haqify "me" `shouldBe` "Haq! me"

Upvotes: 1

Views: 366

Answers (1)

ErikR
ErikR

Reputation: 52019

Update:

You get the error when you run cabal test.

The problem is that your test suite depends on the library haq, but the cabal file does not declare a library component. It only declares an executable.

To fix:

  1. Rename the existing file Haq.hs to App.hs. Update the cabal file appropriately.
  2. Create a new file 'Haq.hs` with this contents:

    module Haq (haqify) where
    
    haqify :: String -> String
    haqify s = "Haqify! " ++ s
    
  3. Add this stanza to the cabal file:

    library
      build-depends: base >= 4.8 && < 4.9
      exposed-modules: Haq
    
  4. Run cabal build or cabal test

Original Answer

I was able to cabal build your project once I fixed the indentation problems in Haq.hs:

  • removed the leading space on the lines for main
  • removed the leading space in front of haqify

Upvotes: 3

Related Questions