1

I am building a Haskell library with Cabal, and want to use this library in some other Cabal projects.

The problem is that after installing the library (with Cabal), my library is not available to the other Cabal projects. Neither ghc-pkg list nor cabal list shows my library (and also does not show packages that my library depends on but was downloaded by cabal as part of the build of my library).

How do I make cabal find my library when building the dependent projects?

I am using Linux, but the same problem has been reported for Windows.

My setup:

  • Linux (Debian 11.5)
  • Haskell installed by ghcup:
    • ghc 8.0.2
    • cabal 3.6.2

(My library and the other projects need this version of ghc.)


My library builds and installs without problems using

$ cabal build
$ cabal install --lib

But ghc-pkg list and cabal list --installed both show only the "predefined" libraries that was installed before the build. These are found in ~/.ghcup/ghc/8.0.2/lib/ghc-8.0.2/package.conf.d/.

My library, and the libraries that it depends on, are installed in ~/.cabal/store/ghc-8.0.2/package.db/. But cabal doesn't find them when building my other dependent projects.

I have tried different options for building, all with the same result of

Resolving dependencies...
cabal: Could not resolve dependencies:
[__0] trying: <PRJ-USING-LIB> (user goal)
[__1] unknown package: wilde (dependency of <PRJ-USING-LIB>)
[__1] fail (backjumping, conflict set: <PRJ-USING-LIB>, <MY-LIB>)
After searching the rest of the dependency tree exhaustively, these were the
goals I've had most trouble fulfilling: <PRJ-USING-LIB>, <MY-LIB>

The options I tried are:

  • cabal build --global
  • cabal build --user
  • cabal build --package-db=~/.cabal/store/ghc-8.0.2/package.db

Interestingly, when adding ghc-options: -L<PATH-TO-MY-LIB-SRC> to the dependent projects cabal file, cabal build says that my library is hidden:

...
Warning: Instead of 'ghc-options: -L<PATH-TO-MY-LIB-SRC>' use
'extra-lib-dirs: <PATH-TO-MY-LIB-SRC>'
...
app/Main.hs:3:1: error:
    Failed to load interface for ‘<MODULE-IN-MY-LIB>’
    It is a member of the hidden package ‘<MY-LIB>-0.5’.
    Perhaps you need to add ‘<MY-LIB>’ to the build-depends in your .cabal file.
    Use -v to see a list of the files searched for.

But trying to ghc-pkg expose <MY-LIB>, it says that it cannot find .

sjakobi
  • 3,546
  • 1
  • 25
  • 43
  • 1
    _Don't_ install libraries. You install executables. You _depend_ on libraries, and Cabal will then install them as needed. If the dependency is supposed to be satisfied from local sources, then you should specify that in a `cabal.project` file. – leftaroundabout Sep 17 '22 at 19:53
  • I double the comment above. Also, if you don't want to bother with `cabal.project` every time, you can create a local no-index repository https://cabal.readthedocs.io/en/stable/config.html#local-no-index-repositories and add it to the `~/.cabal/config`. – Artem Pelenitsyn Sep 18 '22 at 01:42
  • I have looked at `cabal.project` files, and it works. Thanks! By the way, I thought `cabal install --lib` meant "make available to other cabal builds on this machine/for this user/...". I got this impression from [Adding libraries to GHC package environments](https://cabal.readthedocs.io/en/3.6/cabal-commands.html#adding-libraries-to-ghc-package-environments) And also by the fact that, `cabal install --lib` "installs" the lib into `~/.cabal/store/ghc-8.0.2/package.db/`. A bit confusing I must say. Thanks again! – Emil Karlén Sep 18 '22 at 14:42
  • @EmilKarlén it does mean that. The problem is that this command is ill behaved under many simple scenarios. For one, once it creates that environment file, it can't guarantee that it will safely update it. A good account of it was recently reported the on cabal bug tracker: https://github.com/haskell/cabal/issues/8473 (see also tickets referenced from there and the "re: install --lib" label if you're really curious). – Artem Pelenitsyn Sep 19 '22 at 22:41

0 Answers0