haskell / haskell/cabal

Another module than given in `main-is` is picked as main module

Open
#10,832 1 comment 0 reactions 0 assignees View on GitHub
re: main type: bug
Dominant language
Haskell
Stars
1.7k
Forks
750
Avg merge
4d 3h
Merged PRs (30d)
28

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
```
```
$ cabal run
...
I am the shady main...
```

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

Analysis: It seems Cabal calls GHC this way:
```
ghc --make -fbuilding-cabal-package -O -static ... -XHaskell2010 ./Foo.hs Bar Paths_wrong_main ...
```
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.

(I am having a dejavu here, but I couldn't find a report for this issue.)

Stack has the same problem:
- https://github.com/commercialhaskell/stack/issues/6695

Contributor guide

Open the contributing guide

Research direction

Start with the packaged wrong-main.tgz reproducer and the generated GHC command shown in the issue, focusing on how Cabal invokes GHC for an executable with a named main-is file and an unnamed source module. Confirm that the build selects Foo.hs as the entry point rather than Bar.hs, and add a regression test covering this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.