commercialhaskell / commercialhaskell/stack

Cabal's `main-is` field (source file) does not alter GHC's `-main-is` option (module and function name)

Open
#6,695 13 comments 0 reactions 1 assignee Claimed by @mpilgrem View on GitHub
component: build component: Cabal library resolution: upstream issue type: question
Dominant language
Haskell
Stars
4.1k
Forks
850
Avg merge
10h 37m
Merged PRs (30d)
4

Description

If the module in `main-is` has a name and there is another module that has no explicit name (no `module` header), then the other module is picked as main module.

`Bar.hs`
```haskell
main = putStrLn "I am the shady main..."
```
`Foo.hs`
```haskell
module Foo where

main = putStrLn "I am the real main!"
```
`wrong-main.cabal`
```cabal
cabal-version: 1.12
name: wrong-main
version: 0.0.0
build-type: Simple

executable wrong-main
main-is: Foo.hs
other-modules:
Bar
Paths_wrong_main
hs-source-dirs:
./
build-depends:
base
default-language: Haskell2010
```
```
$ stack run
...
I am the shady main...
```

Packaged reproducer: [wrong-main.tgz](https://github.com/user-attachments/files/19264471/wrong-main.tgz)

The wrong result can be reproduced with this vanilla GHC call:
```
ghc --make Foo.hs Bar.hs
```
The correct result can be obtained by adding `-main-is Foo`:
```
ghc --make -main-is Foo Bar.hs Foo.hs
```
I suppose the difficulty is to extract the module name `Foo` from `Foo.hs`, since `-main-is` does not accept a file, just a qualified (module) name.

This issue is shared with cabal:
- https://github.com/haskell/cabal/issues/10832

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.