Many people include .ghci
files in their haskell projects to include needed options to load modules into ghci
. Here's an example:
:set -isrc -itest -iexamples -packagehspec2
However when trying to run a file containing main
through runhaskell
one has to repeat all these options, e.g.:
runhaskell -isrc -itest -iexamples -packagehspec2 test/Spec.hs
Is there a good way to let runhaskell
pick up the options from the .ghci
file?
GHCi is the interactive interface to GHC. From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.
Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.
Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)
The stack ghci or stack repl commands, which are equivalent, allow you to load components and files of your project into GHCi. The command uses the same TARGET syntax as stack build . It can also take flags like --test and --bench and options like --flag .
Similarly to stack build, the default is to load up ghci with all libraries and executables in the project. In order to load multiple components, stack ghci combines all of the ghc options together. This doesn't work in the general case, but when the packages being loaded share similar conventions, it should work out.
Sometimes you want to load ghci with an additional package, that isn't a direct dependency of your components. This can be achieved by using the --package flag. For example, if I want to experiment with the lens library, I can run stack ghci --package lens.
For example, specifying NoImplicitPrelude in one component but not another is quite likely to cause failures. ghci will be run with -XNoImplicitPrelude, but it is likely that modules in the other component assume that the Prelude is implicitly imported. When loading multiple packages, there may be multiple definitions for the Main module.
--no-load, to skip loading all defined modules into ghci. You can then directly use :load MyModule to load a specific module in your project. By default, stack ghci loads and imports all of the modules in the package. This allows you to easily use anything exported by your package.
I don't know of any way to make runhaskell
work. What I do is just pipe "main"
to ghci:
$ echo main | ghci -v0 test/Spec.hs
If you want to pass command-line arguments, that works too:
$ echo ':main -m "behaves correct"' | ghci -v0 test/Spec.hs
Or you can wrap it up in a script:
#!/usr/bin/env runhaskell
>import System.IO
>import System.Environment
>import System.Exit
>import System.Process
>
>main :: IO ()
>main = do
> source:args <- getArgs
> (Just h, Nothing, Nothing, pid) <- createProcess (proc "ghci" ["-v0", source]) {std_in = CreatePipe}
> hPutStr h ("import System.Environment\nSystem.Environment.withArgs " ++ show args ++ " main\n")
> hClose h
> waitForProcess pid >>= exitWith
Which can be used like so:
$ ./run.lhs test/Spec.hs -m "behaves correct"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With