Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define multiple executables / Main modules with stack/hpack

I use stack and a package.yaml file for hpack to compile my haskell project. It has three executables backed by one library. As one would expect the executables are all defining a Main module:

$ head -n1 app/*
==> app/Foo.hs <==
module Main where

==> app/Bar.hs <==
module Main where

==> app/Baz.hs <==
module Main where

And I use this package.yaml which looks very similar (to me) to the one linked from the official docs for hpack (the third one).

name: myproject
dependencies:
  - base
library:
  source-dirs: src
executables:
  foo:
    main: Foo.hs
    source-dirs: app
    dependencies: myproject
  bar:
    main: Bar.hs
    source-dirs: app
    dependencies: myproject
  baz:
    main: Baz.hs
    source-dirs: app
    dependencies: myproject

But when I stack build I get the error, that the module name does not match the file name:

Building all executables for `myproject' once. After a successful build of all of them, only specified executables will be rebuilt.
myproject-0.0.0: build (lib + exe)
Preprocessing library for myproject-0.0.0..
Building library for myproject-0.0.0..
[1 of 2] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.2.0.1/build/Lib.o )
[2 of 2] Compiling Paths_myproject  ( .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.2.0.1/build/autogen/Paths_myproject.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.2.0.1/build/Paths_myproject.o )
Preprocessing executable 'bar' for myproject-0.0.0..
Building executable 'bar' for myproject-0.0.0..

/home/luc/test/app/Baz.hs:1:8: error:
    File name does not match module name:
    Saw: ‘Main’
    Expected: ‘Baz’
  |
1 | module Main where
  |        ^^^^


--  While building package myproject-0.0.0 using:
      /home/luc/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_2.2.0.1_ghc-8.4.4 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-2.2.0.1 build lib:myproject exe:bar exe:baz exe:foo --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

The only difference between my setup and the example that I found was the capital letters of the files in app/. And indeed, if I change them to lower case (in the filesystem and the package.yaml) it all builds correctly.

But why is that and where is that documented?

like image 684
Lucas Avatar asked Jan 27 '23 21:01

Lucas


1 Answers

I think app should not be considered as a convention to be a folder of all final applications. In my projects I always split these to foo/Main.hs and bar/Main.hs for foo and bar targets. Hence package.yaml should contain

executables:
  foo:
    main:                Main.hs
    source-dirs:         foo
    dependencies:
    - myproject
  bar:
    main:                Main.hs
    source-dirs:         bar
    dependencies:
    - myproject

And treat myproject just as a library for them

like image 189
radrow Avatar answered Apr 07 '23 06:04

radrow