4

I'm getting an error that says the module doesn't exist when I try to runhaskell. It's odd because I try to install it first and says its up to date. Any idea how to fix this? enter image description here

avh4
  • 2,635
  • 1
  • 22
  • 25
Matthew Lu
  • 123
  • 1
  • 5

2 Answers2

7

You could try creating the package environment in the local directory that holds your project, like this:

c:\Users\...\ex1haskell> cabal install --lib --package-env . QuickCheck

This should create a file of the form .ghc.environment.xxx in ex1haskell, which hopefully should be picked up by runhaskell/ghci/ghc invocations.

In ghci sessions, a sign that the environment is being picked up is the following message while starting:

Loaded package environment from ...

When the --package-env location is not given explicitly, a default location is used. According to the docs:

By default, it is writing to the global environment in ~/.ghc/$ARCH-$OS-$GHCVER/environments/default. v2-install provides the --package-env flag to control which of these environments is modified.

But it seems that runhaskell is having problems to find the environment file in that default location.

Note. When creating a package environment, it's possible to specify multiple packages simultaneously, like this:

cabal install --lib --package-env . QuickCheck random aeson transformers

Also, package environments are just text files, so local environments can be deleted and recreated at will. The actual package binaries reside elsewhere and can potentially be reused by cabal.

danidiaz
  • 26,936
  • 4
  • 45
  • 95
  • The problem with not finding the default global environment on Windows is caused by a bug: https://gitlab.haskell.org/ghc/ghc/-/issues/19286 – danidiaz Feb 01 '21 at 13:18
  • This solution helped a lot !. I am on ubuntu, and was looking to use the "text" package. Created the local environment as above using the "--package-env" command, Note: a small adjustment I made . in ghci "import Text.CSV" asked me to ":set -package csv" to expose the member of the text package. After that "import Text.CSV" worked like a charm. – blispr May 06 '22 at 20:24
2

A Common Environment

It is hard to debug if/when the actual tooling differs so let's first get a unified setup. I'll use docker to get GHC 8 and Cabal 3.x:

docker run --rm -it haskell bash

Understand that this isn't arbitrary or even preemptive. What you have shown - cabal install --lib ... and runhaskell ... does work for sane tool installations. You might have a bad installation or an old version of a tool that has different behavior.

Running a single file with runhaskell

Now we need a project:

root@8a934c302dba:/# mkdir Ex1
root@8a934c302dba:/# cd Ex1
root@8a934c302dba:/Ex1# cat <<EOF >Main.hs
> import Test.QuickCheck
>
> main :: IO ()
> main = print =<< (generate arbitrary :: IO Int)
> EOF

And test failure:

root@8a934c302dba:/Ex1# runhaskell Main.hs

Main.hs:1:1: error:
    Could not find module `Test.QuickCheck'
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
1 | import Test.QuickCheck

And install the library:

root@8a934c302dba:/Ex1# cabal update && cabal install --lib QuickCheck

And successful run:

root@8a934c302dba:/Ex1# runhaskell Main.hs
15

So my comment above was wrong - we don't need to explicitly list the package as it is already exposed after installation.

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
  • I tried doing cabal update, then cabal install, then runhaskell as I don't think I've tried that sequence of steps yet, but still doesn't work. I'll try creating a new working directory like you. – Matthew Lu Sep 10 '20 at 21:44
  • Doesn't work for me - import QuickCheck fails.; cabal info QuickCheck says "not installed". Have to use v1-install, then it works. – guthrie Jan 26 '21 at 18:26
  • I was able to execute exactly my above sequence successfully. Are you sure you ran the "And install the library" part as shown? Can you paste-bin your full set of steps? – Thomas M. DuBuisson Jan 26 '21 at 20:14