soupi/learn-haskell-blog-generator

Could not find module ‘Options.Applicative’

Closed this issue · 4 comments

First of all, thank you very much for this book. As a newbie of Haskell, I find it having the perfect balance between breadth and depth.

When I tried to use the optparse-applicative package for CLI, my stack build gives the following error

/Users/dzhou/src/ourl/app/Main.hs:6:1: error:
    Could not find module ‘Options.Applicative’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
6 | import Options.Applicative
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^

After some googling, I found this command, which shows that I don't have optparse-applicative installed

stack exec ghc-pkg -- list

Then I used this command to install it. However, the error still persists, maybe I still need to change some configuration?

stack install optparse-applicative
soupi commented

Hi! Thanks for the kind words. I'm glad you find the book helpful!

Did you add the dependency on optparse-applicative to the executable section in the .cabal file? I've edited the chapter to make it a bit clearer that this needs to be done.

The idea behind package managers such as stack and cabal is that in the project description (the .cabal file) we specify the packages that we want to depend on, and then when we build the project, cabal/stack will fetch the dependencies for us if needed. So we don't have to install packages manually.

Thanks @soupi

I actually have added the dependence to my .cabal file. Strangely, whenever I call stack build, a new default .cabal file is generated to overwrite my file, and surely optparse-applicative is not in there.

My project is here, it is basically just the template project created by stack new. And it works if I don't import Options.Applicative in app/Main.hs.
https://github.com/nosarthur/ourl

Please let me know if you have any further insights. Thanks a lot!

after some googling, it seems the problem is that I need to update package.yaml, which is used to regenerate the .cabal file.

https://stackoverflow.com/questions/47724988/why-is-stack-build-altering-my-cabal-file

My guess is that your book created all the project files manually and there is no package.yaml. Thus the manually generated .cabal file is used. Unfortunately I used stack new to create my project...

stack --version
Version 2.7.1, Git revision 8afe0c2932716b0441cf4440d6942c59568b6b19 x86_64 hpack-0.34.4
soupi commented

@nosarthur that's strange, I was under the impression that if the .cabal file is edited, stack will use that and print a warning about it instead of overriding it.

My guess is that your book created all the project files manually and there is no package.yaml. Thus the manually generated .cabal file is used.

Yes, exactly. I would generally suggest using .cabal files instead of package.yamls and this is what I cover in the book. If you want, you can just delete the package.yaml file and work with the .cabal file, or you can update the package.yaml instead and have the .cabal file generated for you - up to you.

So to recap on the solution, use either .cabal or package.yaml as your project description, and define the package dependencies, compiler flags, package metadata, etc. in that file. You should be able to stack build a project without installing dependencies manually - the responsibility to make sure these packages are available is the package manager's :)