How to work together with cabal-3 and ghc (ghc-pkg, too)?

888 Views Asked by At

With the release of cabal-3, the packages from Hackage are installed in a new location that the compiler ghc and ghc-pkg know nothing about. In other words, packages are installed but not registered for ghc. Ghci, ghc, ghc-pkg cannot work.

For example,

cabal install safe --lib

Create file t1.hs

import Safe

t1 = tailMay [1,2,3]

Let's try:

> ghci t1.hs
GHCi, version 8.10.2: https://www.haskell.org/ghc/:? for help
[1 of 1] Compiling Main (t1.hs, interpreted)

t1.hs: 1: 1: error:
    Could not find module `Safe '
    Use -v (or `: set -v` in ghci) to see a list of the files searched for.
  |
1 | import Safe
  | ^^^^^^^^^^^
Failed, no modules loaded.

This bug is described here

https://github.com/haskell/cabal/issues/6262

and here

https://gitlab.haskell.org/ghc/ghc/-/issues/17341

I use as a temporary solution setting a system variable

GHC_PACKAGE_PATH=C:\Users\me\AppData\Roaming\cabal\store\ghc-8.10.2\package.db;

(Windwos 10, haskell-dev by chocolatey)

via On Windows, packages installed with cabal seem to be unavailable in ghc/ghci

but with updates I will have to manually change this system variable.

Are there any more elegant solutions to this problem?

P.S. Unfortunately, this solution (via GHC's environment variable GHC_PACKAGE_PATH) is incompatible with Cabal :(

https://github.com/haskell/cabal/issues/1944

2

There are 2 best solutions below

0
On

One way to achieve this is to use the --env flag to make the libraries available to GHC whenever you are in the current directory:

~ $ mkdir /tmp/foo
~ $ cd /tmp/foo
/tmp/foo $ cabal install safe --lib --env .
Resolving dependencies...
Build profile: -w ghc-8.8.3 -O1
In order, the following will be built (use -v for more details):
 - safe-0.3.19 (lib) (requires build)
Configuring library for safe-0.3.19..
Preprocessing library for safe-0.3.19..
Building library for safe-0.3.19..
…
 > Installing library in /home/jojo/.cabal/store/ghc-8.8.3/incoming/new-4056/home/jojo/.cabal/store/ghc-8.8.3/safe-0.3.19-92fbaef88124b4508ce447f6245bc793f7a1748247ae68d10e449150df1069af/lib
t1.hs
/tmp/foo $ cat > t1.hs
import Safe

t1 = tailMay [1,2,3]
/tmp/foo $ ls -a
.  ..  .ghc.environment.x86_64-linux-8.8.3  t1.hs
/tmp/foo $ ghci t1.hs
GHCi, version 8.8.3: https://www.haskell.org/ghc/  :? for help
Loaded package environment from /tmp/foo/.ghc.environment.x86_64-linux-8.8.3
[1 of 1] Compiling Main             ( t1.hs, interpreted )
Ok, one module loaded.
*Main> 

Note that you probably shouldn’t do this in a directory where you actually have a foo.cabal file. See the documentation of cabal v2-install for details.

0
On

Working with GHC_ENVIRONMENT is better:

  setx  GHC_ENVIRONMENT C:\Users\me\.ghc\x86_64-mingw32-8.10.2\environments\default

it helps for ghc and ghci.

After, in C:\Users\me\AppData\Roaming\cabal\config we should add

  package-db: C:\Users\me\AppData\Roaming\cabal\store\ghc-8.10.2\package.db

it helps for cabal.

Unfortunately, ghc-pkg still has problem and works with such flag:

  ghc-pkg list --user-package-db="C:\Users\me\AppData\Roaming\cabal\store\ghc-8.10.2\package.db"

For Linux the steps are similar.