模块“main:Main”在多个文件中定义 - 但它们是同一个文件

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

我用 stack new demo 开始了一个新的堆栈项目,并在 apps 文件夹中定义了第二个文件 Other.hs。没有依赖关系,只是:

module Other where

main :: IO ()
main = print 4

然后在 package.yamlexecutables 我添加

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

现在当我 stack build 我得到:

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

所以我将 -main-is 添加到 ghc-options:

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

现在 stack build 有效,但在 stack-ghci 上,在选择其中一个可执行文件后我得到:

<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.

正如此处指出的:other-modules: [] 添加到每个可执行文件块会有所帮助。 所以最后一个块将是:

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