stack ghci with error module ‘main:Main’ is defined in multiple files:

Viewed 381

I have a small haskell program which builds and executes with stack ok. When I start it with stack ghci I have an error message which I do not understand and cannot proceed.

GHCi, version 8.10.4: https://www.haskell.org/ghc/  :? for help
[1 of 3] Compiling Lib              ( /home/frank/Workspace11/primo/src/Lib.hs, interpreted )
[2 of 3] Compiling YamlRead         ( /home/frank/Workspace11/primo/src/YamlRead.hs, interpreted )
[3 of 3] Compiling Main             ( /home/frank/Workspace11/primo/app/Main.hs, interpreted )
Ok, three modules loaded.
Loaded GHCi configuration from /home/frank/Workspace11/primo/.ghci

<no location info>: error:
    module ‘main:Main’ is defined in multiple files: /home/frank/Workspace11/primo/app/Main.hs
                                                     /home/frank/Workspace11/primo/app/Main.hs

I do not see why the same Main is listed twice in the exact same file.

I had a somewhat similar warning message about Paths_primo which is a known bug ( Stack issue #5439 ) and I fixed following the advice see.

What is the cure against this error? I have not used stack much - am I doing something wrong?

2 Answers

This looks like a sign that Main.hs or Main is inadvertently listed multiple times in your Stack package.yaml, in such a way that ghc is invoked with multiple occurrences of it.

This error is possible to reproduce easily with GHC alone, for example:

> echo 'main = putStrLn "hello"' > Hello.hs

> ghc Hello Hello.hs
<no location info>: error:
    module ‘main:Main’ is defined in multiple files: Hello.hs Hello.hs

I would run Stack with --verbose and see how GHCi is being invoked, and double-check the package.yaml and generated Cabal file. (If you edit your question to include that, we may be able to offer more specific help with it.)

I can think of several possible reasons for this, such as literally listing Main or Main.hs multiple times (e.g. in exposed-modules, other-modules, main-is); or an interaction like a missing option value in the ghc-options field causing subsequent flags to be misinterpreted.

Main.hs can be listed multiple times in GHCi invocation when it is present in multiple components. Typical way when this happens is:

  1. You create a project with stack new which contains single src/Main.hs file, so package.yaml has an executable entry and no library entry.
  2. Then you add a library and use source-dir: src. Now Main.hs is listed in two components: myapp-lib and myapp-lib-exe. Assuming you package name is myapp.

To solve the problem you should move Main.hs to app dir and update executables:source-dirs to app in package.yaml.

So the problem may happen when Main.hs is listed in multiple components in package.yaml, for example in executables and library.

Related