How can I pin a version of a Haskell dependency to a version of an underlying native dependency with Cabal? - haskell

In my particuar case, I have a dependency in my Cabal file on the Haskell package bindings-libzip. In particular, I could accept several different versions of libzip, e.g. bindings-libzip-0.11 or bindings-libzip-0.10. These in turn have a dependency on the respective native C libzip libraries versions 0.11 and 0.10.
Therefore I have bindings-libzip >= 0.10 < 0.12 in my .cabal file.
The Haskell package bindings-libzip-x specifies with PkgConfig-Depends that libzip version x must be present on a client machine. Let's say a downstream user has version 0.10 of libzip installed. However, when pulling down my package, this user pulls down the latest dependencies possible and transitively pulls down version 0.11 of bindings-libzip. This causes the build process to error out with a message about an incorrect version of libzip installed.
Is there any way I can specify in my .cabal file to use bindings-libzip-0.11 if and only if pkg-config detects version 0.11 of libzip and to use bindings-libzip-0.10 if and only if pkg-config detects version 0.10 of libzip?

I'm submitting another answer because this uses another idea...
Using a custom Setup.hs with defaultMainWithHooksArgs allows you to inspect and modify the args to the cabal configure command.
This is a Setup.hs which does no modification:
import Distribution.Simple
import Distribution.Simple.Configure
import System.Environment
main = do
args <- getArgs
defaultMainWithHooksArgs simpleUserHooks args
If your .cabal file has a flag defined, e.g.:
Flag Foo
Default: False
then in the args you will see "--flags=-foo". So the idea is:
Define two flags in the .cabal file - use10 and use11 to select which version of bindings-libzip to use.
In your custom Setup.hs determine which version to use.
Find the "--flags=..." arg and modify it appropriately before passing it along to defaultMainWithHooksArgs.

I think the way to do this is with a custom Setup.hs file (specify build-type: Custom in the .cabal file.)
You can override specific stages of the build process by using a main like this:
main = defaultMainWithHooks $ simpleUserHooks { preConf = myPreConf }
myPreConf :: Args -> ConfigFlags -> IO HookedBuildInfo
myPreConf args configFlags = ...
It is also likely that overriding the confHook is what you want.
Some links:
all cabal hooks: data UserHooks
standard sets of hooks: (link)
Examples of custom Setup.hs files overriding confHook:
abcBridge
arb-fft
cabalmdvrpm
darkplaces-text
GLFW
happybara-webkit-server
haskeline
HDBC-postgresql
helics
hlbfgsb
hlibsass
hpqtypes
hruby
hsqml
hubris
illuminate
intel-aes
keera-posture
KiCS-debugger
libpq
llvm-general
morfeusz
postgresql-libpq
tamarin-prover
tamarin-prover-term
tamarin-prover-theory
tamarin-prover-utils
voyeur
wxc
wxcore
In particular, the hruby Setup.hs looks like it is doing something like what you want to do.

Related

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.

Duplicate packages in Haskell environment?

I put this in a file (see bottom) and got the following error when loading it in ghci (i.e. start ghci then :l file):
7:13: error:
• Couldn't match expected type ‘network-uri-2.6.1.0#network-uri-2.6.1.0-7BN1tbB3iHQ2XgvmqLAYph:Network.URI.URI’
with actual type ‘Network.URI.URI’
NB: ‘Network.URI.URI’
is defined in ‘Network.URI’
in package ‘network-uri-2.6.1.0#network-uri-2.6.1.0-80FpvaNUTSDFCPv0sSze40’
‘network-uri-2.6.1.0#network-uri-2.6.1.0-7BN1tbB3iHQ2XgvmqLAYph:Network.URI.URI’
is defined in ‘Network.URI’
in package ‘network-uri-2.6.1.0#network-uri-2.6.1.0-7BN1tbB3iHQ2XgvmqLAYph’
Have I somehow installed two slightly different versions of network-uri, or what is this? How do I resolve it?
I've hardly used cabal but tried cabal install --reinstall network-uri, went fine but problem persists.
File contents:
import Network.HTTP
import Network.URI (parseURI)
import Data.Maybe (fromJust)
myRequestURL = "http://www.virginia.edu/cgi-local/ldapweb"
myRequest :: String -> Request_String
myRequest query = Request {
rqURI = fromJust $ parseURI myRequestURL
, rqMethod = POST
, rqHeaders = [ mkHeader HdrContentType "text/html"
, mkHeader HdrContentLength $ show $ length body ]
, rqBody = body
}
where body = "whitepages=" ++ query
main :: IO ()
main = do
response <- simpleHTTP $ myRequest "poon"
putStrLn ""
EDIT Re-installed haskell-platform. After this, ghc-pkg list reports all packages to be under (?) /usr/lib/ghc/package.conf.d, and the program I tried to copy loads just fine.
EDIT 2 After running cabal install some-pkg, ghc-pkg list reports that the newly installed stuff is in another directory. Indeed, ~/.cabal/config specifies install dirs for user, as well as install dirs for global. How is all this supposed to be managed properly?
GHC understands the concept of package databases. Cabal manages multiple package databases. There's a system package database, and a user package database. When you compile something with Cabal V1, it uses the union of these package databases. This is a bit of a time bomb, because, as you see, GHC does not like it when you have multiple of the same package. When you issued cabal install network-uri, cabal decided to install one copy of that package into your user database, though you already had one in the system database. When you tried to use ghc, it picked up on the two copies and got confused. Because cabal does not support removing packages, your Haskell installation was pretty much borked, and you reinstalled. I believe you could have done a more conservative rm ~/.ghc, or, had you been really adventurous, used the low-level ghc-pkg command to edit the package database.
Now that you've reinstalled the Platform, I would heavily recommend never using cabal install again. It's old, it's broken, cabal itself tells you not to use it. If you're using the Platform, I think it has stack in it, which is a different build manager, based on Cabal, which is not prone to this kind of breakage. Or, you can just stick to the V2 series of cabal commands, which is what I prefer.
Cabal V2 does not directly support your usecase, where you install a package globally and compile a program against it. This is because doing so is fundamentally broken: global package databases really just don't work for GHC. What Cabal V2 does is install packages into your home directory, ~/.cabal/store. You then have to explicitly tell it which packages you want, and it will construct a package database that contains each required package, without any wonkiness. You can get a REPL with a certain set of packages by issuing (this will take you to a temporary directory; you may need to :cd yourself back.)
cabal v2-repl -b network-uri -b package2 -b etc
If you want to actually compile files with ghc with a set packages, you have to actually define a package with a .cabal file. Here's a stub .cabal file:
name: temporary-pkg
version: 0.0.0.0
build-type: Simple
cabal-version: >=2.0
executable main -- if there is no Main, say "library" instead of "executable <name>"
default-language: Haskell2010
main-is: Main.hs -- only valid in executable stanzas
hs-source-dirs: src -- source files go here
build-depends: base, network-uri, package2, etc -- packages go here
Create and enter a directory, place that in temporary-pkg.cabal, place your sources in src/, and you can issue stuff like cabal v2-build and cabal v2-repl and cabal v2-exec main, etc. cabal will go off and install dependencies from Hackage into ~/.cabal/store as needed. cabal v2-install is therefore not that useful here. In general, v2-installing libraries is unnecessary, but v2-installing packages with executables is useful. Again, for hygiene reasons, you have to list all the packages you are importing from, but you don't need to list their dependencies or anything insane like that.
Cabal V2 still doesn't support uninstalling packages, though the point is that should not be necessary. However, if you find ~/.cabal/store getting too large for your liking, you can just nuke it, and should be built back up as packages are requested.

Stack insists on building Cabal package

I am working on a Haskell project using Stack.
Recently, we started using the lens package which requires the Cabal package as a dependency, but we switched to lens-simple because building the Cabal package was too resource intensive for some older machines that we tested building the project on.
However, despite the fact that neither lens-simple nor any of our other packages have a dependency on the Cabal package, Stack continues to try and build it.
Is there anyway to get Stack to stop this? It makes the build process very long on most machines and impossible on weaker machines.
A list of the project's dependencies:
HUnit 1.6.0.0
QuickCheck 2.12.6.1
ansi-terminal 0.8.2
array 0.5.3.0
base 4.12.0.0
binary 0.8.6.0
bytestring 0.10.8.2
call-stack 0.1.0
clock 0.7.2
colour 2.3.4
containers 0.6.0.1
deepseq 1.4.4.0
directory 1.3.3.0
erf 2.0.0.0
filepath 1.4.2.1
ghc-boot-th 8.6.3
ghc-prim 0.5.3
hspec 2.6.1
hspec-core 2.6.1
hspec-discover 2.6.1
hspec-expectations 0.8.2
integer-gmp 1.0.2.0
lens-family 1.2.3
lens-family-core 1.2.3
lens-family-th 0.5.0.2
lens-simple 0.1.0.9
mtl 2.2.2
ncurses 0.2.16
netflak 0.1.0.0
pretty 1.1.3.6
primitive 0.6.4.0
quickcheck-io 0.2.0
random 1.1
rts 1.0
setenv 0.1.1.3
stm 2.5.0.0
template-haskell 2.14.0.0
text 1.2.3.1
tf-random 0.5
time 1.8.0.2
transformers 0.5.5.0
unbounded-delays 0.1.1.0
unix 2.7.2.2
My guess is that one of your dependencies using a custom setup stanza, where Stack needs to build the Setup.hs file against the Cabal library, thus the implicit dependency. We have a bit of a discussion going already for Stackage as to whether we should provide up to date versions of the Cabal library as we do today—and risk forcing people to build a heavy dependency—versus sticking to the version of Cabal that ships with GHC.
Anyway, you can work around this with a slightly convoluted approach where you create a custom snapshot that drops the Cabal library. It would look something like this:
# stack.yaml: point to the custom snapshot
resolver: snapshot.yaml
# snapshot.yaml: use the original snapshot and add a drop-packages
resolver: nightly-2019-03-17
name: drop-cabal
drop-packages:
- Cabal

Haskell dependency hell

I'm trying to include a specific version of a library in a Haskell project. The library is bed-and-breakfast (which is used for martix operations), but I need the specific version 0.4.3 which fixed a bug with the multiplication implementation.
So, my stack.yaml looks like this:
flags: {}
extra-package-dbs: []
packages:
- .
extra-deps:
- bed-and-breakfast-0.4
- base-4.6.0.1
resolver: lts-12.8
But I'm getting this error when building:
Error: While constructing the build plan, the following exceptions were encountered:
In the dependencies for bed-and-breakfast-0.4:
base-4.11.1.0 from stack configuration does not match >=4.5 && <4.7 (latest matching version
is 4.6.0.1)
needed due to realworldhaskell-0.1.0.0 -> bed-and-breakfast-0.4
Some different approaches to resolving this:
* Set 'allow-newer: true' to ignore all version constraints and build anyway.
* 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.
* Recommended action: try adding the following to your extra-deps
in C:\Users\info\Desktop\Projects\haskell\stack.yaml:
- base-4.6.0.1
I've done the recommended action but it didn't solve anything. I've tried using different resolvers to see if it's an issue with my GHCi version but nothings worked. What is the best way to interpret error messages like this and how should I proceed?
EDIT:
If I remove -base.4.6.0.1 and add allow-newer: true I get this:
WARNING: Ignoring out of range dependency (allow-newer enabled): base-4.11.1.0. bed-and-breakfast requires: >=4.5 && <4.7
bed-and-breakfast-0.4: configure
Progress 1/2
-- While building custom Setup.hs for package bed-and-breakfast-0.4 using:
C:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_Z6RU0evB_2.2.0.1_ghc-8.4.3.exe --builddir=.stack-work\dist\7d103d30 configure --with-ghc=C:\Users\info\AppData\Local\Programs\stack\x86_64-windows\ghc-8.4.3\bin\ghc.EXE --with-g
hc-pkg=C:\Users\info\AppData\Local\Programs\stack\x86_64-windows\ghc-8.4.3\bin\ghc-pkg.EXE --user --package-db=clear --package-db=global --package-db=C:\sr\snapshots\76fd1958\pkgdb --package-db=C:\Users\info\Desktop\Projects\haskell\
.stack-work\install\8c390635\pkgdb --libdir=C:\Users\info\Desktop\Projects\haskell\.stack-work\install\8c390635\lib --bindir=C:\Users\info\Desktop\Projects\haskell\.stack-work\install\8c390635\bin --datadir=C:\Users\info\Desktop\Proj
ects\haskell\.stack-work\install\8c390635\share --libexecdir=C:\Users\info\Desktop\Projects\haskell\.stack-work\install\8c390635\libexec --sysconfdir=C:\Users\info\Desktop\Projects\haskell\.stack-work\install\8c390635\etc --docdir=C:
\Users\info\Desktop\Projects\haskell\.stack-work\install\8c390635\doc\bed-and-breakfast-0.4 --htmldir=C:\Users\info\Desktop\Projects\haskell\.stack-work\install\8c390635\doc\bed-and-breakfast-0.4 --haddockdir=C:\Users\info\Desktop\Pr
ojects\haskell\.stack-work\install\8c390635\doc\bed-and-breakfast-0.4 --dependency=array=array-0.5.2.0 --dependency=base=base-4.11.1.0 --dependency=binary=binary-0.8.5.1 --dependency=deepseq=deepseq-1.4.3.0 --dependency=template-hask
ell=template-haskell-2.13.0.0 --extra-include-dirs=C:\Users\info\AppData\Local\Programs\stack\x86_64-windows\msys2-20150512\mingw64\include --extra-lib-dirs=C:\Users\info\AppData\Local\Programs\stack\x86_64-windows\msys2-20150512\min
gw64\bin --extra-lib-dirs=C:\Users\info\AppData\Local\Programs\stack\x86_64-windows\msys2-20150512\mingw64\lib --exact-configuration
Process exited with code: ExitFailure 1
Logs have been written to: C:\Users\info\Desktop\Projects\haskell\.stack-work\logs\bed-and-breakfast-0.4.log
Configuring bed-and-breakfast-0.4...
Cabal-simple_Z6RU0evB_2.2.0.1_ghc-8.4.3.exe: The package has an impossible
version range for a dependency on an internal library: bed-and-breakfast
==0.3.2. This version range does not include the current package, and must be
removed as the current package's library will always be used.
EDIT 2:
Ok, so I'm guessing that the bed-and-breakfast library needs base 4.11.1.0 which is included in GHCi 6.10.2 (according to https://wiki.haskell.org/Base_package) so I need a resolver which matches that GHCi version. Where can I find out what resolver version that is?
Here is the constraint on the latest available bed-and-breakfast package: base (>=4.5 && <4.7), which means it will likely not even compile with GHC version higher then 7.6. Considering that there is even no LTS snapshot prior to GHC 7.8, you are out of luck with that package.
To say it in another words, the package is outdated and your choices are:
submit an issue and hope maintainer will do something about it
try to make it work with newer ghc yourself
Use a different package

Is there a way to get version of Cabal package in source code of the package?

Say I have a console program that has an option to display its
version. Currently whenever I update version in .cabal file I need to go
to source code and update constant — string representation of current
version number as well. This feels against DRY principle and now I'm
wondering, is it possible to get version of my project as defined in
.cabal file from source code? Maybe Cabal defines some CPP macro or
something else?
Indeed Cabal allows to access information from .cabal file in your
program. According to Cabal documentation,
you can import special module that exists during building of your package,
like this:
import Paths_packagename (version)
import Data.Version (showVersion)
myVersion :: String
myVersion = showVersion version
The module Paths_packagename provides version of type
Version.

Resources