Multiple files use the same module name: - haskell

when I type stack run I get no error message but when I type stack ghci I get this error about multiple files use the same name , how I can solve it ?
(base) wejden#wejdenaydi:~/wejden$ stack ghci
Using main module: 1. Package `wejden' component wejden:exe:wejden-exe with main-is file: /home/wejden/wejden/app/Main.hs
Building all executables for `wejden' once. After a successful build of all of them, only specified executables will be rebuilt.
wejden> configure (lib + exe)
Configuring wejden-0.1.0.0...
wejden> initial-build-steps (lib + exe)
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: wejden
* * * * * * * *
Warning: Multiple files use the same module name:
* Paths_wejden found at the following paths
* /home/wejden/wejden/.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0/build/autogen/Paths_wejden.hs (wejden:lib)
* /home/wejden/wejden/.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0/build/wejden-exe/autogen/Paths_wejden.hs (wejden:exe:wejden-exe)
* * * * * * * *
GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help
[1 of 3] Compiling Lib ( /home/wejden/wejden/src/Lib.hs, interpreted )
[2 of 3] Compiling Main ( /home/wejden/wejden/app/Main.hs, interpreted )
[3 of 3] Compiling Paths_wejden ( /home/wejden/wejden/.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0/build/autogen/Paths_wejden.hs, interpreted )
Ok, three modules loaded.
Loaded GHCi configuration from /tmp/haskell-stack-ghci/f99925e2/ghci-script
*Main Lib Paths_wejden>

For those who, like me, need very specific instructions, here it goes:
executables:
mypkgname-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- stocks
when:
- condition: false
other-modules: Paths_mypkgname
where mypkgname needs to be replaced with your package name. I'm posting this because on my first attempt I had put the when clause in the wrong place.

This is a known issue with hpack: https://github.com/commercialhaskell/stack/issues/5439
In short, add this in your package.yaml, to your executable(s) or your library:
when:
- condition: false
other-modules: Paths_wejden # your package name here

Related

ghci: module ‘main:Main’ is defined in multiple files - in new small stack init build ghci package

I try to setup a stack based Haskell IDE with vscode and start with a small project created with stack init and then added a second module in src and added some dependencies in package.yaml. It builds ok but when I start stack ghci I have warnings:
Warning: Multiple files use the same module name:
* Paths_primo found at the following paths
* /home/frank/Workspace11/primo/.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0/build/autogen/Paths_primo.hs (primo:lib)
* /home/frank/Workspace11/primo/.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0/build/primo-exe/autogen/Paths_primo.hs (primo:exe:primo-exe)
* * * * * * * *
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.
There seems to be a confusion with autogen and with Paths_primo (primo is the name of the package). What am I doing wrong?
General question: what is the correct way to clean a stack project to "start over" after some experimentation? Is is correct to delete the cabal file and the stack-work directory. Waht is with stack.yaml and 'stack.yaml.lock`?
This appears to be a harmless warning reported at Stack issue #5439, whose underlying cause is hpack issue #303. A workaround to get rid of the warning is disabling the generation of the Paths_ module for your primo-exe executable, by adding the following to its section in package.yaml:
when:
- condition: false
other-modules: Paths_primo

Building multiple executables in the default Haskell Stack project

I used the default stack new to setup a project that has a server and a client as separate executables. I altered the package.yaml file in what seems like the right way (As of April 21, 2020 "There is no user guide") and added a new file to my app directory called Client.hs.
I got an error saying "Enabling workaround for Main module 'Main' listed in 'other-modules' illegally!"
How do I have stack build both the client and the server?
When I ran stack build I got:
[... clip ...]
Building executable 'ObjectServer' for ObjectServer-0.1.0.1..
[4 of 4] Compiling Client
Linking .stack-work\dist\29cc6475\build\ObjectServer\ObjectServer.exe ...
Warning: Enabling workaround for Main module 'Main' listed in 'other-modules'
illegally!
Preprocessing executable 'Client' for ObjectServer-0.1.0.1..
Building executable 'Client' for ObjectServer-0.1.0.1..
[3 of 3] Compiling Client
<no location info>: error:
output was redirected with -o, but no output will be generated
because there is no Main module.
-- While building package ObjectServer-0.1.0.1 using:
D:\HaskellStack\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_3.0.1.0_ghc-8.8.3.exe --builddir=.stack-work\dist\29cc6475 build lib:ObjectServer exe:Client exe:ObjectServer --ghc-options " -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
The relevant portion of package.yaml looks like this:
executables:
ObjectServer:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- ObjectServer
Client:
main: Client.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- ObjectServer
There are two problems here. First, the default value for other-modules in hpack is "all modules in source-dirs except main and modules mentioned in a when clause". If you look at the generated .cabal file, you'll see that as a result of this default, each executable has incorrectly included the other executable's module in its other-modules list. Second, the main setting gives the source file that contains the main module, but doesn't change the name of the module expected by GHC from Main to anything else. Therefore, that module still needs to be named module Main where ..., not module Client where..., unless you also, separately add a -main-is Client GHC option.
So, I would advise modifying Client.hs to make it the Main module:
-- in Client.hs
module Main where
...
and then specifying other-modules: [] explicitly for both executables:
executables:
ObjectServer:
main: Main.hs
other-modules: []
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- ObjectServer
Client:
main: Client.hs
other-modules: []
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- ObjectServer
That seems to work in my testing.

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

How do I add a CPP defininition within cabal / stack?

I'm looking at the following module: https://hackage.haskell.org/package/boxes-0.1.4/docs/src/Text-PrettyPrint-Boxes.html
Which has contents of:
module Text.PrettyPrint.Boxes
( -- * Constructing boxes
#ifdef TESTING
Box(Box, content)
#else
Box
#endif
How can I enable / define the TESTING value - for development purposes? Ideally I'd like to have this built / enabled within a GHCi session.
I have to correct myself - the comment I gave is totally and utterly wrong.
> git clone git://github.com/treeowl/boxes.git
> cd boxes
> stack init
> stack ghci --ghc-options=-DTESTING
split-0.2.3.2: using precompiled package
boxes-0.1.4: configure (lib)
Configuring boxes-0.1.4...
boxes-0.1.4: initial-build-steps (lib)
Completed 2 action(s).
Configuring GHCi with the following packages: boxes
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from ...
[1 of 1] Compiling Text.PrettyPrint.Boxes ( .../boxes/Text/PrettyPrint/Boxes.hs, interpreted )
Ok, modules loaded: Text.PrettyPrint.Boxes.
Loaded GHCi configuration from /tmp/ghci28386/ghci-script
*Text.PrettyPrint.Boxes Text.PrettyPrint.Boxes> :t content
content :: Box -> Content
just works fine.

Haskell Stack Ghci test-suite

I'm trying to use stack to load my test-suite in ghci and have it load the QuickCheck and hspec dependency.
How can I do this?
I'm using the franklinchen template.
https://github.com/commercialhaskell/stack-templates/blob/master/franklinchen.hsfiles
I have tried
stack ghci spec
stack ghci test-suite
stack ghci --main-is spec
I modified the test-suite spec to target the main-is: LibSpec.hs file
test-suite spec
default-language: Haskell2010
ghc-options: -Wall
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: LibSpec.hs
build-depends: base
, chapterexercises
, hspec
, QuickCheck
stack ghci --test
Note that this will only work if there's a single test suite and no other executable. Otherwise it will give you a warning:
* * * * * * * *
The main module to load is ambiguous. Candidates are:
Package `project' component exe:project-exe with main-is file: T:\project\app\Main.hs
Package `project' component test:project-test with main-is file: T:\project\test\Spec.hs
None will be loaded. You can specify which one to pick by:
1) Specifying targets to stack ghci e.g. stack ghci project:exe:project-exe
2) Specifying what the main is e.g. stack ghci --main-is project:exe:project-exe
* * * * * * * *
In this case you have to use
stack ghci --test chapterexercises:test:spec
Without --test stack is going to ignore the tests. That's why you don't get the ambiguity error in the first place.

Resources