Search code examples
haskellghccabalhaskell-diagrams

Haskell - Expose hidden package permanently


I am a beginner when it comes to Haskell and I have been following the quickstart tutorial for diagrams.

I installed Haskell with ghcup and diagrams with cabal install diagrams --lib (the tutorial mentions cabal sandbox init, but apparently this is outdated).

When I try to compile the first example (with ghc --make example.hs), I get the following error:

/home/me/example.hs:4:1: error:
    Could not load module ‘Diagrams.Prelude’
    It is a member of the hidden package ‘diagrams-lib-1.4.3’.
    You can run ‘:set -package diagrams-lib’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
4 | import Diagrams.Prelude
  | ^^^^^^^^^^^^^^^^^^^^^^^

/home/me/example.hs:5:1: error:
    Could not load module ‘Diagrams.Backend.SVG.CmdLine’
    It is a member of the hidden package ‘diagrams-svg-1.4.3’.
    You can run ‘:set -package diagrams-svg’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
5 | import Diagrams.Backend.SVG.CmdLine
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I could prevent this error by doing ghc example.hs -package diagrams-lib -package diagrams-svg, but I could not find a way to do this permanently.


Solution

  • This is controlled using the cabal file. Your example.cabal file will contain a section like this

    library example
       exposed-modules: 
          Example   -- Assumes your example.hs contains "module Example where ..."
       build-depends:
          diagrams-lib,
          diagrams-svg
    

    Then you compile using cabal build or cabal install.