Skip to content

Extend .cabal format with "common" definition stanzas #2832

@hvr

Description

@hvr

( this is a project task description for the upcoming https://github.com/haskell/cabal/wiki/Hackathon2015 )

One complaint about the .cabal format is that there's avoidable duplication when multiple stanzas requiring to repeat some shared properties are involved (most notably build-depends)

Here's an example how common stanzas could be used:

name: thispkg
version: 1.2.3
cabal-version: >=2.0

-- ...

flag small_base
  description: Choose the new smaller, split-up base package.

-- unnamed global common stanza included implicitly in all library/executable/benchmark/test stanzas
common
  build-depends: base-orphans < 1

-- named common stanza which needs to be explicitly included
common cdef1
  hs-source-dirs: src

  build-depends: text
  if flag(small_base)
    build-depends: base >=2.1 && <5, array, containers, directory
  else
    build-depends: base >= 1.0

library
  include cdef1
  build-depends: parsec
  other-modules:

test-suite tests
  type: exitcode-stdio-1.0
  main-is: test.hs
  include cdef1
  build-depends: thispkg, tasty

benchmark bench1 
  type: exitcode-stdio-1.0
  main-is: bench1.hs
  include cdef1
  build-depends: thispkg, criterion

In the example above a single common section named cdef1 is defined, which is included by all 3 real stanzas. include cdef acts as if the respective common stanza's body was inserted in place of include cdef.


Possible extensions

  • reserve a few special common stanza ids all, library, test-suites, and benchmark to declare common stanzas that are implicitly included by the respective named real stanza. In the example above we could have used common all instead of common cdef1 and all include cdef1 simply removed.
  • allow recursive (but acyclic) includes. I.e. allow common stanzas to include other common stanzas.

/cc @dcoutts


@dcoutts suggests to support both,

  1. named sections and explicit include at uses sites
  2. unnamed global (implicit include into start of all sections) (would subsume Package global build-depends should apply constraints to all components #3404)

Moreover, we'd want this mechanism not only for .cabal files but also for cabal's config files. For non-.cabal files we want to reuse the infrastructure for file-backed include files (include-file ./some-ast-frament.inc).


refinement: common stanzas ought to be typed so we can catch mistakes early on during parsing already, see #2832 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions