How can packages be unhidden when using only stack?
Asked Answered
D

4

14

I'd like to try out the Writer monad in ghci. As advised here, I tried to use only stack to manage GHC and packages, and avoid a global installation.

From a fresh Ubuntu 15.04 install, after installing stack:

stack setup
mkdir lyah && cd lyah
stack new
stack install mtl
stack ghci
ghci> import Control.Monad.Writer
Could not find module ‘Control.Monad.Writer’
It is a member of the hidden package ‘mtl-2.1.3.1’.

I understand that pre-stack ghc-pkg was used to show/hide packages, but I'm not sure how to proceed here to 'unhide' the mtl package.

Dupaix answered 23/7, 2015 at 16:46 Comment(0)
S
18

Edit the .cabal file stack new created and add mtl to the build-depends section. That part of the file should look like this:

build-depends:       base >= 4.7 && < 5
                   , mtl

Then, do a stack build before stack ghci.

By the way, do not use stack install to install libraries - it is merely a shortcut to copy binaries. E.g. stack install hlint will first build the package and then copy the resulting binary to ~/.local/bin/. Instead, always add the packages to the .cabal file, as shown above, and use stack build so that they get installed.

Southeastward answered 23/7, 2015 at 18:4 Comment(0)
H
2

Just to complement @Jazmit answer. Be aware that your .cabal file will have two build-depends sections. One under library: and the other under executable my-project-exec. In this case, you'd need to put the module under the executable section.

Example:

my-project.cabal

library: 
  build-depends:
  ...

executable my-project-exe:
  build-depends:
    base >= 4.7 && < 5
    , mtl

For further info about library and executable check the docs: https://cabal.readthedocs.io/en/latest/developing-packages.html#editing-the-cabal-file

Helenahelene answered 13/4, 2021 at 16:19 Comment(0)
L
2

If you are using stack, try adding your packages definition to dependencies field inpackage.yaml for expose it into you project.

dependencies:
- mtl

Then, stack run

Example of package.yaml

Longevity answered 13/7, 2021 at 3:9 Comment(0)
L
1

Since you're working in GHCi, you can just change the command line passed to the underlying GHC. For example, I did this recently:

Prelude> import qualified GI.Gtk as Gtk

<no location info>: error:
    Could not load module ‘GI.Gtk’
    It is a member of the hidden package ‘gi-gtk-3.0.31’.
    Perhaps you need to add ‘gi-gtk’ to the build-depends in your .cabal file.
    It is a member of the hidden package ‘gi-gtk-3.0.27’.
    Perhaps you need to add ‘gi-gtk’ to the build-depends in your .cabal file.
Prelude> :set -package gi-gtk-3.0.27
package flags have changed, resetting and loading new packages...
Prelude> import qualified GI.Gtk as Gtk
Prelude Gtk> 
Lefkowitz answered 11/7, 2019 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.