Search code examples
haskellhaskell-stackhpack

module ‘main:Main’ is defined in multiple files - but they're the same file


I start a new stack project with stack new demo and define a second file Other.hs in the apps folder. There are no dependencies, just:

module Other where

main :: IO ()
main = print 4

And in package.yaml under executables I add

  other-exe:
    main:                Other.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - debug-multiple-files

Now when I do stack build I get:

<no location info>: error:
    output was redirected with -o, but no output will be generated
because there is no Main module.

So I add -main-is to ghc-options:

    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    - -main-is Other

Now stack build works, but on stack-ghci, after selecting one of the executables I get:

<no location info>: error:
    module ‘main:Main’ is defined in multiple files: /home/.../demo/app/Main.hs
                                                     /home/.../demo/app/Main.hs
Failed, no modules loaded.

Solution

  • As pointed out here: https://stackoverflow.com/a/61393123 adding other-modules: [] to each executables block helps. So the final block would be:

      other-exe:
        main:                Other.hs
        other-modules:       []
        source-dirs:         app
        ghc-options:
        - -threaded
        - -rtsopts
        - -with-rtsopts=-N
        - -main-is Other
        dependencies:
        - demo