Haskell stack, how to revert ambiguous module names found in multiple packages globally - haskell

While trying to debug a different question, I installed a package that seems to conflict with some of my other installed packages.
I ran
$ stack install regex-pcre-text
regex-pcre-builtin-0.94.4.8.8.35: configure
regex-tdfa-1.2.3.1: download
regex-pcre-builtin-0.94.4.8.8.35: build
regex-tdfa-1.2.3.1: configure
regex-tdfa-1.2.3.1: build
regex-pcre-builtin-0.94.4.8.8.35: copy/register
regex-tdfa-1.2.3.1: copy/register
regex-tdfa-text-1.0.0.3: download
regex-tdfa-text-1.0.0.3: configure
regex-tdfa-text-1.0.0.3: build
regex-tdfa-text-1.0.0.3: copy/register
regex-pcre-text-0.94.0.1: download
regex-pcre-text-0.94.0.1: configure
regex-pcre-text-0.94.0.1: build
regex-pcre-text-0.94.0.1: copy/register
Completed 4 action(s).
I can no longer simply import
Text.Regex.PCRE
When I try, I now see:
$ stack ghci
Prelude> :set -XOverloadedStrings
Prelude> import Text.Regex.PCRE
Yields
<no location info>: error:
Ambiguous module name ‘Text.Regex.PCRE’:
it was found in multiple packages:
regex-pcre-0.94.4 regex-pcre-builtin-0.94.4.8.8.35
I would like to revert my installation to the earlier state such that code on my machine that imports Text.Regex.PCRE without qualification continues to work as it used to.
However, it looks like stack does not have a clear uninstall:
$ stack uninstall regex-pcre-text
Error: stack does not manage installations in global locations. The only global mutation stack performs is executable copying. For the default executable destination, please run stack path --local-bin
I hesitate to simply run this stack path --local-bin because I don't know what it's going to do, or whether it can be reversed (which was my error in installing the above package in the first place). What is the right fix for my import problem?
Update 1
I tried suggestions here:
$ ghc-pkg unregister regex-pcre-text
ghc-pkg: cannot find package regex-pcre-text
$ stack exec ghc-pkg unregister regex-pcre-text
ignoring (possibly broken) abi-depends field for packages

The issue is that regexp-pcre-text installed its dependency regex-pcre-builtin which caused the conflict. You want to run both of the following commands in the global project (i.e., outside of any specific project directory):
$ stack exec ghc-pkg unregister regex-pcre-text
$ stack exec ghc-pkg unregister regex-pcre-builtin
You already ran the first, and I suspect it completed successfully, despite the warning message about the abi-depends fields, so you just need to run the second.
(These could be combined into a single command:
$ stack exec ghc-pkg unregister regex-pcre-text regex-pcre-builtin
but given you already removed the first package, I believe this would fail with a message that regex-pcre-text wasn't found.)
The solution mentioned by #DarthFennec is to use the PackageImports extension to resolve the conflict. From GHCi, it would look like this, to use the module from the regex-pcre package even with both packages installed:
Prelude> :set -XPackageImports
Prelude> import "regex-pcre" Text.Regex.PCRE
Prelude Text.Regex.PCRE>

Related

With HaskellStack install packages to use with GHC without stack

I install GHC on Windows10 using the recommended Haskell Stack. I want to us GHC without all the Stack overhead for Advent of Code. This was working fine until I tried to get the extra package.
I can install it with Stack, but I don't seem to have a way to get it in the global package database. Haskell Stack apparently does not install the cabal executable and seems to have it locked out of their package database.
How do I install the extra package for use with vanilla GHC?
John Miller#DESKTOP-NENAGQH MSYS /d/dev/AdventOfCode2020
$ stack ghc -- AoC/Utils.hs
[1 of 1] Compiling AoC.Utils ( AoC\Utils.hs, AoC\Utils.o )
John Miller#DESKTOP-NENAGQH MSYS /d/dev/AdventOfCode2020
$ ghc AoC/Utils.hs
[1 of 1] Compiling AoC.Utils ( AoC\Utils.hs, AoC\Utils.o ) [Data.List.Extra changed]
AoC\Utils.hs:3:1: error:
Could not find module `Data.List.Extra'
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
3 | import Data.List.Extra
| ^^^^^^^^^^^^^^^^^^^^^^
I don't know whether stack supports installing to the global package DB. cabal does though:
$ cabal install --lib extra
Resolving dependencies...
Up to date
$ ghci
GHCi, version 8.10.2: https://www.haskell.org/ghc/ :? for help
Loaded package environment from /home/simon/.ghc/x86_64-linux-8.10.2/environments/default
Prelude> import Data.List.Extra
Prelude Data.List.Extra>
Ok, so stack can install the cabal executable if you beat at it long enough. The package is called cabal-install and it is not in any resolver, but is on Hackage.
stack install cabal-install
Because it is not in the resolver there is a pretty good chance that the version of Cabal, the library for manipulating cabal packages in Haskell, is not compatible. First, ask stack where it keeps its global config
stack path --config-location
Edit that file to allow for the needed dependencies under extra-deps: Stack will helpfully tell you what they are. It may also be helpful to change the resolver to a newer version in that file while your at it.
Now try
stack install cabal-install
again and if these instructions have not fallen out of date since December 2020 you will get the cabal executable somewhere potentially useful.
Before using cabal you will have to run a cabal update to get the package list.
At this point cabal should manipulate your global package database and stack can install GCH and all its libraries over and over and over again if you want to use it for a project instead. They should just keep out of each other's way.

Haskell - could not find module 'Test.QuickCheck'

I'm getting an error that says the module doesn't exist when I try to runhaskell. It's odd because I try to install it first and says its up to date. Any idea how to fix this?
You could try creating the package environment in the local directory that holds your project, like this:
c:\Users\...\ex1haskell> cabal install --lib --package-env . QuickCheck
This should create a file of the form .ghc.environment.xxx in ex1haskell, which hopefully should be picked up by runhaskell/ghci/ghc invocations.
In ghci sessions, a sign that the environment is being picked up is the following message while starting:
Loaded package environment from ...
When the --package-env location is not given explicitly, a default location is used. According to the docs:
By default, it is writing to the global environment in
~/.ghc/$ARCH-$OS-$GHCVER/environments/default. v2-install provides the
--package-env flag to control which of these environments is modified.
But it seems that runhaskell is having problems to find the environment file in that default location.
Note. When creating a package environment, it's possible to specify multiple packages simultaneously, like this:
cabal install --lib --package-env . QuickCheck random aeson transformers
Also, package environments are just text files, so local environments can be deleted and recreated at will. The actual package binaries reside elsewhere and can potentially be reused by cabal.
A Common Environment
It is hard to debug if/when the actual tooling differs so let's first get a unified setup. I'll use docker to get GHC 8 and Cabal 3.x:
docker run --rm -it haskell bash
Understand that this isn't arbitrary or even preemptive. What you have shown - cabal install --lib ... and runhaskell ... does work for sane tool installations. You might have a bad installation or an old version of a tool that has different behavior.
Running a single file with runhaskell
Now we need a project:
root#8a934c302dba:/# mkdir Ex1
root#8a934c302dba:/# cd Ex1
root#8a934c302dba:/Ex1# cat <<EOF >Main.hs
> import Test.QuickCheck
>
> main :: IO ()
> main = print =<< (generate arbitrary :: IO Int)
> EOF
And test failure:
root#8a934c302dba:/Ex1# runhaskell Main.hs
Main.hs:1:1: error:
Could not find module `Test.QuickCheck'
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
1 | import Test.QuickCheck
And install the library:
root#8a934c302dba:/Ex1# cabal update && cabal install --lib QuickCheck
And successful run:
root#8a934c302dba:/Ex1# runhaskell Main.hs
15
So my comment above was wrong - we don't need to explicitly list the package as it is already exposed after installation.

Haskell there are files missing in the QuickCheck-2.11.3 package

I tried running my program which uses Haskell QuickCheck via ghc MyProgramm.hs , but received the following error:
$ ghc Ex2.hs
[1 of 1] Compiling Ex2 ( Ex2.hs, Ex2.o )
Ex2.hs:21:1: error:
Could not find module ‘Test.QuickCheck’
There are files missing in the ‘QuickCheck-2.11.3’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
21 | import Test.QuickCheck (
| ^^^^^^^^^^^^^^^^^^^^^^^^...
I installed stack, ran stack update and stack install QuickCheck without issue but the error persisted. Then, I ran cabal install QuickCheck and got the following errors:
$ cabal install QuickCheck
Resolving dependencies...
Configuring QuickCheck-2.12.4...
Building QuickCheck-2.12.4...
Failed to install QuickCheck-2.12.4
Build log ( /home/username/.cabal/logs/ghc-8.4.3/QuickCheck-2.12.4-3d2YDDqfPBn4BfmTJbpJXK.log ):
cabal: Entering directory '/tmp/cabal-tmp-9133/QuickCheck-2.12.4'
Configuring QuickCheck-2.12.4...
Preprocessing library for QuickCheck-2.12.4..
Building library for QuickCheck-2.12.4..
[ 1 of 16] Compiling Test.QuickCheck.Exception ( Test/QuickCheck/Exception.hs, dist/build/Test/QuickCheck/Exception.o )
[ 2 of 16] Compiling Test.QuickCheck.Random ( Test/QuickCheck/Random.hs, dist/build/Test/QuickCheck/Random.o )
Test/QuickCheck/Random.hs:10:1: error:
Could not find module ‘System.Random’
There are files missing in the ‘random-1.1’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
10 | import System.Random
| ^^^^^^^^^^^^^^^^^^^^
Test/QuickCheck/Random.hs:11:1: error:
Could not find module ‘System.Random.TF’
There are files missing in the ‘tf-random-0.5’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
11 | import System.Random.TF
| ^^^^^^^^^^^^^^^^^^^^^^^
Test/QuickCheck/Random.hs:12:1: error:
Could not find module ‘System.Random.TF.Gen’
There are files missing in the ‘tf-random-0.5’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
12 | import System.Random.TF.Gen(splitn)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cabal: Leaving directory '/tmp/cabal-tmp-9133/QuickCheck-2.12.4'
cabal: Error: some packages failed to install:
QuickCheck-2.12.4-3d2YDDqfPBn4BfmTJbpJXK failed during the building phase. The
exception was:
ExitFailure 1
However, I already have the arch packages haskell-random, haskell-tf-random and haskell-mwc-random installed. Does anybody know how to fix this?
Edit: I also ran cabal install random --reinstall.
Problem
On Archlinux as of 2022-09-17, pacman -S ghc cabal-install will install
system packages that provide only dynamic files (.so, .dyn_hi) in
installed packages inside /usr/lib/ghc-*; static files (.a, .hi) are (for
the most part) missing. However, the default cabal configuration enables static file
building. Unfortunately, upstream cabal-install doesn't track whether or not
static files are available inside installed packages. It just assumes they
are, and when they are gone, it fails with errors such as you have found:
[1 of 1] Compiling Main ( Main.hs, ../setup.dist/work/depender/dist/build/depender/depender-tmp/Main.o )
Main.hs:3:1: error:
Could not find module `Dynamic'
There are files missing in the `dynamic-1.0' package,
try running 'ghc-pkg check'.
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
| import qualified Dynamic (number)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Workaround
A quick workaround just to get up and running quickly is to disable static file
building, which is by default enabled. (Note that trying to pass package-local
flags to disable static file with e.g. --enable-shared --enable-executable-dynamic --disable-library-vanilla (which is how most if
not all current Archlinux Haskell packages seem to build packages, e.g. with
https://github.com/archlinux/svntogit-community/blob/master/haskell-scientific/trunk/PKGBUILD)
building using cabal-install may be ignored due to another, related bug; but
~/.cabal/config is a safe bet.) You may do so by adding 3 lines to
~/.cabal/config in the appropriate location:
library-vanilla: False
shared: True
executable-dynamic: True
(Alternatively, consider using alternative tools like stack.)
More complete solution
For a more long-term solution, one option involves 2 pieces: 1) one or more
system packages that provide all types of build artifacts, static and dynamic,
for the base, foundational packages (from GHC and cabal-install), at least as an option besides dynamic-only packages (secondary Haskell packages are optional,
since cabal-install can rebuild these with needed build artifacts (static or
dynamic)), and 2) patching cabal-install (and ghc-pkg, which can handle .conf
files recording information about installed packages) to track whether static
files are available, and to be aware of these when resolving dependencies so
that cabal-install knows when to prefer rebuild a source package with needed
build artifact configuration over an already installed package that doesn't
provid required build artifacts.
There is a merge request (I submitted) that provides such a patchset, called
fix-dynamic-deps, at https://github.com/haskell/cabal/pull/8461. For users
running into exactly the problem that you described (myself included), I also
created an AUR package that provides both pieces based on GHC 9.4.2 with Cabal
3.9.0.0 that includes my patchset (there is a mirror at
https://github.com/bairyn/ghc-cabal-arts.) It provides ghc and
cabal-install but includes both of these pieces.
Further reading
Here are a few more resources I wrote on or are related to this bug:
https://wiki.archlinux.org/index.php?title=Haskell&diff=745459&oldid=738269#Troubleshooting
https://github.com/haskell/cabal/pull/8461
https://github.com/bairyn/ghc-cabal-arts
https://github.com/bairyn/cabal/tree/fix-dynamic-builds
Even though I have a main = do and couldn't get it working that way, I was able to run my QuickCheck test as follows:
To run a quickCheck, first load your program with:
$ ghci MyProg.hs
Then to run the quickCheck, find the test you want to run, then run it with:
$ quickCheck my_quick_check

Haskell Stack failing to install easyrender

I have GHC 8.0.2 installed on a Haskell stack, and I need a certain number of packages on it for my project, one being easyrender.
I install it using the following command, which works for all other package installations:
stack install easyrender
I get the following output:
easyrender-0.1.1.2: configure
-- While building package easyrender-0.1.1.2 using:
/home/nathan/.stack/programs/x86_64-linux/ghc-8.0.2/bin/ghc --make -odir /tmp/stack5987/easyrender-0.1.1.2/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/setup -hidir /tmp/stack5987/easyrender-0.1.1.2/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/setup -i -i. -clear-package-db -global-package-db -package-db=/home/nathan/.stack/snapshots/x86_64-linux/ghc-8.0.2/8.0.2/pkgdb -package-db=/home/nathan/.stack/global-project/.stack-work/install/x86_64-linux/ghc-8.0.2/8.0.2/pkgdb -hide-all-packages -package-id=base-4.9.1.0 -package-id=superdoc-0.1.2.5-9gGVGYAJpHrGct376cUN41 -optP-include -optP/tmp/stack5987/easyrender-0.1.1.2/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/setup/setup_macros.h /tmp/stack5987/easyrender-0.1.1.2/Setup.hs /home/nathan/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs -main-is StackSetupShim.mainOverride -o /tmp/stack5987/easyrender-0.1.1.2/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/setup/setup -threaded
Process exited with code: ExitFailure 1
Logs have been written to: /home/nathan/.stack/global-project/.stack-work/logs/easyrender-0.1.1.2.log
[1 of 2] Compiling Main ( /tmp/stack5987/easyrender-0.1.1.2/Setup.hs, /tmp/stack5987/easyrender-0.1.1.2/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/setup/Main.o )
[2 of 2] Compiling StackSetupShim ( /home/nathan/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs, /tmp/stack5987/easyrender-0.1.1.2/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/setup/StackSetupShim.o )
/home/nathan/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs:3:1: error:
Failed to load interface for ‘Distribution.PackageDescription’
It is a member of the hidden package ‘Cabal-2.0.1.0’.
It is a member of the hidden package ‘Cabal-1.24.2.0’.
Use -v to see a list of the files searched for.
/home/nathan/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs:4:1: error:
Failed to load interface for ‘Distribution.Simple’
It is a member of the hidden package ‘Cabal-2.0.1.0’.
It is a member of the hidden package ‘Cabal-1.24.2.0’.
Use -v to see a list of the files searched for.
/home/nathan/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs:5:1: error:
Failed to load interface for ‘Distribution.Simple.Build’
It is a member of the hidden package ‘Cabal-2.0.1.0’.
It is a member of the hidden package ‘Cabal-1.24.2.0’.
Use -v to see a list of the files searched for.
/home/nathan/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs:6:1: error:
Failed to load interface for ‘Distribution.Simple.Setup’
It is a member of the hidden package ‘Cabal-2.0.1.0’.
It is a member of the hidden package ‘Cabal-1.24.2.0’.
Use -v to see a list of the files searched for.
/home/nathan/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs:7:1: error:
Failed to load interface for ‘Distribution.Simple.LocalBuildInfo’
It is a member of the hidden package ‘Cabal-2.0.1.0’.
It is a member of the hidden package ‘Cabal-1.24.2.0’.
Use -v to see a list of the files searched for.
Since Cabal seems to be a problem, I tried running stack install Cabal, but that doesn't do anything. The log file referenced just says the same thing as the output.
Ideas?
Nathan
This is a bug in the easyrender package description. Note that cabal-install's "new-build" encounters the same problem. See my comment on this stack issue: https://github.com/commercialhaskell/stack/issues/3560#issuecomment-344075524
Workaround is as described by Thomas's answer, and also in the issue at https://github.com/commercialhaskell/stack/issues/3560#issuecomment-343980627
The next stack release will emit a warning about this circumstance, which should reduce confusion in the future.
Unfortunately, easyrender does not have an issue tracker, it appears to only have a hackage page. I have emailed the author requesting that he fix this.
No such problems with cabal-install:
% cabal install easyrender
Resolving dependencies...
Downloading superdoc-0.1.2.5...
Configuring superdoc-0.1.2.5...
Building superdoc-0.1.2.5...
Installed superdoc-0.1.2.5
Downloading easyrender-0.1.1.2...
Configuring easyrender-0.1.1.2...
Building easyrender-0.1.1.2...
Installed easyrender-0.1.1.2
So I'm guessing this is an issue with stack handling non-simple builds in a different manner. Namely, cabal-install seems to assume Cabal is a dependency of the setup.hs while stack does not.
Just to check, I tried and could produce your same issue but adding Cabal to the setup-depends: line of the easyrender.cabal made it work fine.

Using Build-Tools (Alex) with Stack and GHCjs

I depend on a package that needs alex to build, i also need ghcjs.
When i try to run stack ghci:
language-java-0.2.8: configure (lib)
Error:
-- While building package language-java-0.2.8 using:
<long command>
Process exited with code: ExitFailure 1
Logs have been written to: /Users/LeanderK/Documents/Haskell/exemplator-java_parser/.stack-work/logs/language-java-0.2.8.log
Configuring language-java-0.2.8...
Cabal-simple_mPHDZzAJ_1.24.2.0_ghcjs-0.2.1.9007019_ghc-8.0.1: The program
'alex' version >=3.1.3 is required but it could not be found.
Warning: Build failed, but optimistically launching GHCi anyway
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: exemplator-client
Using main module: 1. Package `exemplator-client' component exe:exemplator-client-exe with main-is file: ..../app/Main.hs
Progress: 1/2<command line>: cannot satisfy -package language-java-0.2.8
(use -v for more information)
I've tried: stack build alex (just returns, don't have any output. Might work), stack install alex (doesn't work).
Output of stack install alex
Couldn't find executable alex in directory .../.stack/snapshots/x86_64-osx/lts-7.19/ghcjs-0.2.1.9007019_ghc-8.0.1/bin/
For reference: This is the cabal file of the dependency, unfortunately only found on github. This is my stack and my cabal file. There is also a reddit discussion that didn't go anywhere.

Resources