Search code examples
haskellcabal

How to reduce duplication in the build-depends fields of a .cabal file?


Here's a .cabal file:

Name:                myprogram
Version:             0.1
-- blah blah blah
Cabal-version:       >=1.9.2

Executable myprogram
  HS-source-dirs:       src
  Main-is:              Main.hs
  Build-depends:        attoparsec == 0.10.*,
                        base == 4.3.*,
                        -- long long list of packages

Test-Suite test
  HS-source-dirs:       test, src
  Type:                 exitcode-stdio-1.0
  Main-is:              Main.hs
  Build-depends:        attoparsec == 0.10.*,
                        base == 4.3.*,
                        -- long long list of packages
                        QuickCheck == 2.4.*

Is there any way I can replace the long list of build-depends packages for the test suite with "same as for the executable, plus QuickCheck"?

Edit: version information.

  • cabal-dev 0.9
  • cabal-install 0.10.2
  • Cabal library 1.10.2.0
  • GHC 7.0.4
  • Haskell Platform 2011.4.0.0

Solution

  • Since version 2.2 Cabal supports common stanzas, to dedup build info fields: https://cabal.readthedocs.io/en/latest/developing-packages.html#common-stanzas

    cabal-version:       2.2
    name:                myprogram
    version:             0.1
    -- blah blah blah
    
    common deps
      build-depends: base ^>= 4.11,
                     -- long long list of packages
      ghc-options: -Wall
    
    library
      import: deps
      exposed-modules: Foo
    
    test-suite tests
      import: deps
      type: exitcode-stdio-1.0
      main-is: Tests.hs
      build-depends: foo