Problem with loading module ‘Distribution.Simple’ - haskell

I was trying to build one new project in Haskell (GHC version 8.10.4, stack version 2.7.3, cabal version 3.6.2.0) using stack, but at the time of running the command stack setup I (surprisingly) got the following error:
Setup.hs:2:1: error:
Could not load module ‘Distribution.Simple’
It is a member of the hidden package ‘Cabal-3.2.1.0’.
You can run ‘:set -package Cabal’ 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.
|
2 | import Distribution.Simple
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Also, it is worth noting that I was trying to look for different solutions on the Internet, but didn't find the working one for me. The general idea was to explicitly add Cabal into the package.yaml file here:
library:
source-dirs: src
dependencies:
- Cabal
But that didn't save my situation. I was able to build the project without that step (well, just skipped that part), but I was interested to solve the issue.
Moreover, when I was trying to build Haskell package timeit (as an example, using the command line), there was the same error at the time of executing
runhaskell Setup.hs configure.
Interestingly, I didn't have this problem before (probably, it appeared after I updated GHC).
Does anybody know any ways of how to deal with such issue? Is there any way probably to reinstall the GHC, or cabal, or stack (if it helps)? Would be glad to see any comments and solutions.

Instead of deleting the
~/.ghc/*/environments/default
file, just add the Cabal package to it, including version number, which you can find if you run the command:
ghc-pkg list

I had the same issue.
Deleting the files
~/.ghc/*/environments/default
(or the whole folder .ghc) solved the stack setup problem.
It seems that they were somehow interacting with stack's operations.

Related

How to install an library in Haskell?

I try to use
Control.Monad.Extra.andM
import Control.Monad.Extra (andM)
but has an error:
Could not find module ‘Control.Monad.Extra’
Perhaps you meant
Control.Monad.Catch (needs flag -package-key exceptions-0.10.4)
Control.Monad.Error (needs flag -package-key mtl-2.2.2)
Control.Monad.Except (needs flag -package-key mtl-2.2.2)not found
This error does not make sense.
According to
https://cabal.readthedocs.io/en/3.6/installing-packages.html#installing-packages-from-hackage
3.2.1. Installing packages from Hackage
The cabal tool also can download, configure, build and install a Hackage package and all of its dependencies in a single step. To do this, run:
$ cabal install [PACKAGE...]
To browse the list of available packages, visit the Hackage web site.
Which says "in a single step", but in my experience this is too complicated and actually I have no idea how to install Control.Monad.Extra .
Usually, when a Haskeller want to install a specific library/package like this, how do you do this? There's no adequate documentations, it seems.
How to install an library in Haskell?
You don't. You should just depend on them, and then let Cabal worry about any installations that may need to be done. I.e., as you wrote
Go to the Hackage page and study which exact library to be used.
*.cabal file -> build-depends: extra >=1.7.10
This is the crucial step. Your own Cabal file is the way to both specify what libraries are needed right now, as well as ensuring that everything will still work in the future. This file needs to specify both the packages you require, as well as the modules you're defining yourself.
Probably you don't actually need >=1.7.10, but it can't hurt much to be specific there. Standard practice is to add both lower and upper bounds on the x.y level, i.e. you'd use extra >=1.7 && <1.8, and then push the upper boundary as new versions come out. Arguably this is a bit overcautious; if you only use some simple tools from a package that is unlikely to have breaking changes in the future then it may be less trouble to just leave the upper boundary out.
$ cabal install extra
You've already specified that extra is needed for your project, no need to state that again. Instead, simply build your own project now.$ cabal buildor, to get a GHCi prompt with your modules,$ cabal replor, if you have an executable with a main that you wish to execute$ cabal runor if you want to install that executable (and only then)$ cabal installCabal will then automatically figure out that extra-1.7.10 should be installed first.
(In old Cabal it would still have been necessary to run cabal install --dependencies-only first.)
Ok Self-Answered. Comment me if the information is wrong.
1. Go to the Hackage page and study which exact library to be used.
In this case, it seems extra-1.7.10.
2. *.cabal file -> build-depends: extra >=1.7.10 (among other libs)
3. $ cabal install extra
Error:
cabal: Cannot build the executables in the package extra because it does not contain any executables. Check the .cabal file for the package and make sure that it properly declares the components that you expect.
Well, I did for sure.
4. cabal install extra --lib
Finally works, but the documentation never said this.
Am I correct?
`
Is this the only method? or any other smarter way?

Stack: 'hdevtools is a library dependency, but the package provides no library needed since my-app is a build target.'

I was trying to add hdevtools to my stack project, so I ran stack build hdevtools. The install seemed to work successfully, and my text editor stopped reporting imported libraries installed via stack (like aeson and tasty) as missing.
However, things went wrong when I added this line to the dependencies section of my package.yaml file:
- hdevtools >= 0.1 && < 1
And then tried to run stack build again. I received the following error output:
Error: While constructing the build plan, the following exceptions were encountered:
In the dependencies for my-app-name-0.1.0.0:
hdevtools is a library dependency, but the package provides no library
needed since my-app-name is a build target.
Some different approaches to resolving this:
* Consider trying 'stack solver', which uses the cabal-install solver to attempt
to find some working build configuration. This can be convenient when dealing
with many complicated constraint errors, but results may be unpredictable.
Plan construction failed.
I tried running stack solver, but that threw the exception documented here.
How can I declare hdevtools as a dependency of my project?
#alexis-king recommends using stack build --copy-compiler-tool hdevtools in this guide, in the section titled Setting up editor integration.
This works for the current project, and other projects using the same GHC version, but you will need to run it again when you upgrade to a new GHC version.
More context from King's guide:
As mentioned above, stack install is not what you want. Tools like ghc-mod, hlint, hoogle, weeder, and intero work best when installed as part of the sandbox, not globally, since that ensures they will match the current GHC version your project is using.
How can I declare hdevtools as a dependency of my project?
hdevtool is an executable and cabal doesn't have a concept of development dependencies (like in other package managers like npm etc). So, all you can do is install hdevtools globally and make it work.

Local install packages with Stack for use with runhaskell

I am developing many small haskell programs that I indent to use as script files. My ideal use-case is to stack runhaskell one-of-the-scripts.hs to run them.
Some of the files use dependencies on packages in hackage, such as regex-posix. Therefore I get an error message saying
Could not find module `Text.Regex.Posix'
Use -v to see a list of the files searched for.
|
2 | import Text.Regex.Posix
| ^^^^^^^^^^^^^^^^^^^^^^^
I have considered two options:
Use a cabal package and specify dependencies therein. I don't like this solution since it (by what I understand) is used in a workflow where I always compile my code. I would like to use stack runhaskell instead!
Ditch stack and use cabal straight up instead, as suggested in an answer to a similar question here on SO: https://stackoverflow.com/a/13485987/4050510. I dont like this, since I would like to have all my dependencies listed in some file explicitly.
What is the simplest way to install packages from hackage locally, and have then available for stack runhaskell ?
You probably want to use the script interpreter. Sorry over concision on the answer but I'm on my phone. Here's the docs for it: https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter
You could also just do "stack build regex-posix", and then the package will be available to runhaskell. However, this is more error prone because no package hiding is used so it's trickier to tell what the script actually depends on

Installing local package with Stack

Is it possible to install package from sources with something similar to stack build package-name? (latter works with packages on Stackage, but not with custom ones)
Um, stack build (within the source directory)?
Stack doesn't really have a notion of installing libraries though, it only installs executables. To “install” locally-sourced packages, you need to specify what for you want them installed: add them as dependencies to another project, via a location: field in the packages: field in that project's stack.yaml file.
That's arguably sensible since, one might say, there's nothing you can do with an installed library except invoking it in another Haskell project (or in a REPL, which you can get with stack ghci). I personally don't hold with that though, I like actually being able to say install that library now. Which is one of the reasons I have always stuck to good old cabal-install rather than Stack. With that, you can just
cabal install
from within the source directory.
Cabal-install has often been criticised: its local installs can easily get out of sync and then you have weird dependency conflicts and need to rebuild lots of stuff. I never found this that much of a problem, and anyway this has been adressed in recent Cabal through Nix-style builds, which never produce conflicts.

Could not find module `Control.Monad.Reader'

Today when I tried to compile my code in Geany I got this error
Could not find module `Control.Monad.Reader':
it was found in multiple packages: monads-fd-0.1.0.1 mtl-1.1.0.2
Compilation failed.
I get the same for the Writer monad; I thought I should remove one of those packages, but I do not want to break other packages, so now what should I do, yesterday everything worked without any problem.
It looks like you have recently installed monads-fd, perhaps as a dependency of something else you installed. Both monads-fd and mtl packages contain the module Control.Monad.Reader, so GHC doesn't know which one to use when you compile some code that imports it. You need to resolve the ambiguity somehow:
If you are using GHC or GHCi directly
either use a -hide-package <package> flag on the command line to hide one of the packages, or
hide the package by default using ghc-pkg hide <package>. You may need to use ghc-pkg --user hide <package> if the package was installed in your home directory (the default on some platforms).
You can use Cabal, and say exactly which one of the conflicting packages you depend on using the build-depends field in your .cabal file
I encountered a similar problem recently, and it was suggested that I run ghc-pkg hide {x} where '{x}' is the name of one of those packages. It worked in my situation.

Resources