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

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?

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

Error: module ‘main:Course’ is defined in multiple files: src/Course.hs

I'm trying to start working on https://github.com/system-f/fp-course. After running stack ghci, it finishes with:
...
[51 of 51] Compiling Test.Loader ( src/Test/Loader.hs, interpreted )
Ok, 51 modules loaded.
Loaded GHCi configuration from /home/lpied/haskell/fp-course/.ghci
<no location info>: error:
module ‘main:Course’ is defined in multiple files: src/Course.hs
src/Course.hs
Failed, 51 modules loaded.
Loaded GHCi configuration from /tmp/haskell-stack-ghci/75d10d48/ghci-script
Any idea why it's complaining that the module is defined in the same file (src/Course.hs)?
Can you try using stack exec ghci instead? stack ghci is probably recognizing the src directory and adding -isrc to the ghci command line, and then the .ghci file in the course root directory is also adding -isrc to the ghci command line. GHC probably doesn't de-duplicate its search path, so it acts like you have two separate directories which both happen to be named src.

How to debug a stack process being 'killed'?

I'm compiling a haskell file with
$ stack ghc --resolver lts-12.5 --package raw-strings-qq myscript.hs
and execute it with
$ ./myscript
Killed
$
on my Ubuntu machine. I get Killed as an error message and nothing else.
How can I debug this? Does this mean the process takes up to much memory? Are there other possible sources of error?
The response you get from terminal as Killed is because you didn't give some commands:
I use online IDE called Replit, so at the start it doesn't load needed libraries for you, you have to load manually:
ghci -o main main.hs
Sometimes, it automatically loads Main, sometimes it doesn't. So, if you don't see such thing like below, then you have to do it manually as well:
[1 of 1] Compiling Main ( main.hs, interpreted )
Ok, one module loaded.
*Main>
:l main
Maybe, it doesn't directly answer your question, but I got same exception like yours and dealt with it in that way. Well, same exception has the same problem (I guess)?

Emacs haskell intero mode, import could not find module

Edit: this error occurs only when using emacs haskell mode and intero mode (C-c C-l to load into ghci). It works in command line using stack ghc Log.hs LogAnalysis.hs.
I'm learning Haskell through CIS 194 (Spring 2013), and am running into this problem when doing homework 2. The homework folder is very simple:
homework2
|-- LogAnalysis.hs
|-- Log.hs
|-- error.log
|-- sample.log
Some data types are defined in Log.hs, and need to be imported into LogAnalysis.hs, which is the file I need to work on. The first few lines in LogAnalysis.hs are like this:
{-# OPTIONS_GHC -Wall #-}
module LogAnalysis where
import Log
-- Exercise 1: parse an individual message
...
However, I got error message like this in my emacs using haskell mode with intero:
error:
Could not find module 'Log'
Use -v to see a list of the files searched for.
(Hit 'C-c C-r' in the Haskell buffer to apply suggestions.)
Same message appears when using 'C-c C-l' to load to ghci within emacs.
But this error doesn't appear when loading LogAnalysis.hs in command line using stack ghci, the message is instead:
Prelude> :l LogAnalysis.hs
[1 of 2] Compiling Log ( Log.hs, interpreted )
[2 of 2] Compiling LogAnalysis ( LogAnalysis.hs, interpreted )
Ok, two modules loaded.
*LogAnalysis>
So I'm guessing this error has something to do with my emacs' setup of haskell mode and intero mode, but couldn't find any available solution yet.
Thanks for reading this question, your help will be appreciated!
It seems that intero needs a package.yaml and a stack.yaml in order to locate your source files. You can just run stack init or stack new to auto matically generate these project files.
I met this problem several times. The above method solved my problem on my Windows and Fedora, so I hope this will help you.
With the help of Krantz's answer and some more reading, the problem is solved by creating a new project with stack so intero knows the location of my source files.
Thus this problem is caused by intero's not knowing where to look for local modules when in intero-global-mode in emacs.
Here I'll write my own answer here to expand Krantz's answer a little more, and to document the process of solving this problem as a beginner to Haskell:
For intero to be able to import local modules, we should avoid using intero-global-mode and instead creating a local project (which in hindsight makes more sense to me).
So in the case of this Homework 2, instead of moving files to a homework folder as described in the question, I'll stack new homework2, and move the sources files into homework2\src. Then when using emacs to load LogAnalysis.hs, instead of this previous message I got in *intero:global-project::repl*:
Loaded GHCi configuration from /Users/[username]/.stack/global-project/.stack-work/intero/intero-[script]
and error message when loading LogAnalysis.hs, I'm now able to get:
Loaded GHCi configuration from /path/to/homework2/.stack-work/intero/intero-[script]
in *intero:homework2:homework2:repl*. And using C-c C-l to load LogAnalysis.hs now gets:
[2 of 2] Compiling LogAnalysis ( /path/to/homework2/src/LogAnalysis.hs, interpreted ) [flags changed]
Ok, two modules loaded.
So problem solved.

Stack: Compile stand-alone source file

Once you've installed Stack, you can use it to install GHC for you. Great!
...now how do I compile a file with it?
To be clear: What you're supposed to do is write a package specification and have Stack build that. But surely there must be a way to trivially compile a tiny helper file inside that project? It seems silly to have to construct an entire project just so I can build a tiny helper program and have it easily runnable from the shell.
I know I can run any Haskell file using Stack. But I cannot for the life of me figure out how to compile the thing...
You can use stack ghc -- <file names> to compile and link a set of files (it's briefly mentioned in Stack's user guide):
You'll sometimes want to just compile (or run) a single Haskell source file, instead of creating an entire Cabal package for it. You can use stack exec ghc or stack exec runghc for that. As simple helpers, we also provide the stack ghc and stack runghc commands, for these common cases.
The -- is to ensure the arguments we pass are sent to ghc, rather than being parsed as arguments to stack exec. It's the same thing as when trying to pass arguments to an executable you've made using the normal stack toolchain: stack exec myExe -foo passes -foo to exec, not myExe, stack exec myExe -- -foo behaves as desired.
For example:
Bar.hs
module Bar where
bar :: Int
bar = 5
Foo.hs
import Bar
main :: IO ()
main = print bar
Compilation (don't even need to specify Bar.hs in the build files, it's sourced automatically):
> stack ghc -- Foo.hs
[1 of 2] Compiling Bar ( Bar.hs, Bar.o )
[2 of 2] Compiling Main ( Foo.hs, Foo.o )
Linking Foo ...
> ./Foo
5
There's no problem with dependencies either - it looks like all the packages installed locally are available to the build (you can use containers or QuickCheck without any additional build params).

Loading Haskell Modules that have dependencies

I'm unsure what my issue is here. I have a trio of modules A.hs, B.hs, and C.hs. All are located at C:\..path...\folder and modules B and C both import from A.
That is, both modules B and C contain the line import A
I can :l C:\..path..\folder\A.hs in gchi and play with its contents; however, ghci gives the following error when I try to :l C:\..path..\folder\B.hs or :l C:\..path..\folder\C.hs
Could not find module `A'
Use -v to see a list of the files searched for.
Failed, modules loaded: none.
Which I find odd because I had no trouble compiling B.hs to B.exe and running the executable. How can I compile and run a module that I can't load into ghci? Or, why would an import succeed at compile time but fail in loading; especially when that very module being imported is itself load-able?
By default, ghci searches only in the current directory for imported modules. To start with, the current directory is the one used to launch ghci; but it can be changed from within ghci with the :cd command. Thus, you could
> :cd C:\...path...\folder
> :l B.hs
and this should find both B.hs and A.hs in what is now the current directory. Alternately (and especially if you have modules in multiple directories) you can launch ghci with the -i command line option to add directories to its module search path. For example, in your command prompt you might
% ghci -iC:\...path...\folder
> :l B.hs
which will instruct ghci to include C:\...path...\folder in its search path, and therefore find B.hs and A.hs there even if it is not the current directory.

Resources