Skip to content

main-is: inside two-way if/else: Ambiguous values for modulePath field #9659

Open

Description

@jwaldmann

Describe the bug

cabal file containing

executable foo-bar
 if flag(wat)
     main-is: bar-foo.hs
 else
     main-is: foo-bar.hs

is rejected with

Ambiguous values for modulePath field: '"bar-foo.hs"' and '"foo-bar.hs"'
CallStack (from HasCallStack):
  error, called at src/Distribution/Types/UnqualComponentName.hs:128:7 in Cabal-syntax-3.11.0.0-inplace:Distribution.Types.UnqualComponentName

To Reproduce

$ cat foo.cabal
cabal-version: 3.0
name: foo
version: 0

flag wat
  default: False
  manual: True
     
executable foo-bar
 if flag(wat)
     main-is: bar-foo.hs
 else
     main-is: foo-bar.hs
 hs-source-dirs: src
 build-depends: base

$ cabal install

Expected behavior
builds

System information

6.6.12-200.fc39.x86_64  x86_64 GNU/Linux

cabal-install version 3.11.0.0
compiled using version 3.11.0.0 of the Cabal library 

ghc-9.6.3

Additional context

with cabal-install 3.10.2.1, the problem can be repaired with indenting else, see. ucsd-progsys/liquidhaskell#2258 (comment) but cabal-install-3.11 will reject that with

Warning: foo.cabal:12:4: invalid subsection "else"
Warning: foo.cabal:12:4: Inconsistent indentation. Indentation jumps at lines 12

Activity

  1. ffaf1 commented on Jan 25, 2024

    @ffaf1
    Collaborator

    Works here, cabal HEAD. Works with your .cabal too.

    f@x270:/tmp/prova$ cat prova.cabal
    cabal-version:   3.0
    name:            prova
    version:         0.1.0.0
    license:         NONE
    author:          Francesco Ariis
    maintainer:      [email protected]
    build-type:      Simple
    extra-doc-files: CHANGELOG.md
    
    flag wat
    
    common warnings
        ghc-options: -Wall
    
    executable prova
     import:           warnings
     if flag(wat)
         main-is: bar-foo.hs
     else
         main-is: foo-bar.hs
     build-depends:    base ^>=4.18.1.0
     hs-source-dirs:   app
     default-language: Haskell2010
    f@x270:/tmp/prova$ tree app/
    app/
    ├── bar-foo.hs
    └── foo-bar.hs
    
    1 directory, 2 files
    f@x270:/tmp/prova$ cabal check
    Warning: this is a debug build of cabal-install with assertions enabled.
    These warnings will likely cause trouble when distributing the package:
    Warning: [no-category] No 'category' field.
    The following errors will cause portability problems on other environments:
    Error: [no-syn-desc] No 'synopsis' or 'description' field.
    Error: [license-none] The 'license' field is missing or is NONE.
    Error: Hackage would reject this package.
    f@x270:/tmp/prova$ cabal build
    Warning: this is a debug build of cabal-install with assertions enabled.
    Warning: The package list for 'hackage.haskell.org' is 33 days old.
    Run 'cabal update' to get the latest list of available packages.
    Resolving dependencies...
    Build profile: -w ghc-9.6.3 -O1
    In order, the following will be built (use -v for more details):
     - prova-0.1.0.0 (exe:prova) (first run)
    Warning: this is a debug build of cabal-install with assertions enabled.
    Configuring executable 'prova' for prova-0.1.0.0...
    Warning: this is a debug build of cabal-install with assertions enabled.
    Preprocessing executable 'prova' for prova-0.1.0.0...
    Building executable 'prova' for prova-0.1.0.0...
    [1 of 1] Compiling Main             ( app/bar-foo.hs, /tmp/prova/dist-newstyle/build/x86_64-linux/ghc-9.6.3/prova-0.1.0.0/x/prova/build/prova/prova-tmp/Main.o )
    [2 of 2] Linking /tmp/prova/dist-newstyle/build/x86_64-linux/ghc-9.6.3/prova-0.1.0.0/x/prova/build/prova/prova
    f@x270:/tmp/prova$ cabal --version
    cabal-install version 3.11.0.0
    compiled using version 3.11.0.0 of the Cabal library
    
  2. gbaz commented on Jan 26, 2024

    @gbaz
    Collaborator

    Note, this does not appear to be a check error anymore:

    Error: cabal: bar-foo.hsfoo-bar.hs doesn't exist

    It looks like both sides of the conditional get evaluated and merged in the install command, not the check command. Maybe we're not evaluating conditionals right in that stanza?

  3. ffaf1 commented on Jan 26, 2024

    @ffaf1
    Collaborator

    Thanks for providing the reproducer.

    Indeed I mistakenly thought the error was triggered by build/check, but it is not. It is cabal sdist (and cabal install) that fails:

    /tmp/cabal-9659 (main)$ cabal --version
    cabal-install version 3.11.0.0
    compiled using version 3.11.0.0 of the Cabal library
    /tmp/cabal-9659 (main)$ cabal sdist
    Warning: this is a debug build of cabal-install with assertions enabled.
    Ambiguous values for modulePath field: '"bar-foo.hs"' and '"foo-bar.hs"'
    CallStack (from HasCallStack):
      error, called at src/Distribution/Types/UnqualComponentName.hs:128:7 in Cabal-syntax-3.11.0.0-inplace:Distribution.Types.UnqualComponentName
    /tmp/cabal-9659 (main)$ ~/download/cabal --version
    cabal-install version 3.10.2.0
    compiled using version 3.10.2.1 of the Cabal library
    /tmp/cabal-9659 (main)$ ~/download/cabal sdist
    Error: cabal: bar-foo.hsfoo-bar.hs doesn't exist
    

    Offending code is a broken Semigroup instance:

    instance Semigroup Executable where
    a <> b =
    Executable
    { exeName = combineNames a b exeName "executable"
    , modulePath = combineNames a b modulePath "modulePath"
    , exeScope = combine exeScope
    , buildInfo = combine buildInfo
    }
    where
    combine field = field a `mappend` field b

    b0cb3d4

    which I suspect it is getting called via flattenPackageDescription.

  4. gbaz commented on Jan 26, 2024

    @gbaz
    Collaborator

    That sounds right. I was going to suggest we use finalizePD instead of flattenPackageDescription but in fact for sdist at least we really do need both sides of the conditional, since we need to add both to the tarball. We just need to ensure that we accumulate as lists of module names, not strings.

  5. ffaf1 commented on Feb 17, 2024

    @ffaf1
    Collaborator

    #9724 provides a possible workaround using extra-source-file.

  6. added
    re: main
    on Feb 25, 2024
  7. bravit commented on Apr 12, 2025

    @bravit

    I’m getting the same error when running cabal repl Main.hs in the MWE project below. This is the command used by haskell-language-server, which effectively prevents it from working with any project that uses if/else conditionals in the .cabal file.

    MWE:

    .
    ├── Main.hs
    ├── alt
    │   └── Main.hs
    └── my-flagged-app.cabal
    
    2 directories, 3 files
    

    my-flagged-app.cabal:

    cabal-version: 2.0
    name:                my-flagged-app
    version:             0.1.0.0
    build-type:          Simple
    
    flag use-alt
      description:       Use AltMain instead of Main
      default:           False
      manual:            True
    
    executable my-flagged-app
      if flag(use-alt)
        main-is:         alt/Main.hs
      else
        main-is:           Main.hs
      hs-source-dirs:    .
      build-depends:     base >=4.7 && <5
      default-language:  Haskell2010
    

    Main.hs:

    module Main where
    
    main :: IO ()
    main = putStrLn "Running: Main.hs"
    

    alt/Main.hs:

    module Main where
    
    main :: IO ()
    main = putStrLn "Running: AltMain.hs (from flag)"
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to 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