With HaskellStack install packages to use with GHC without stack - haskell

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.

Related

Why can't the ghc (Haskell) compiler find the module Data.Vector which I have imported? I get the error: Could not find module 'Data.Vector'

vector module has been installed by "cabal install vector"
bash-3.2$ ghc-pkg list -f $HOME/.cabal/store/ghc-9.2.1/package.db
/Users/gcolpitts/.cabal/store/ghc-9.2.1/package.db
primitive-0.7.3.0
vector-0.12.3.1
but the compiler can't find it:
$ ghc prob214ff.hs
[1 of 1] Compiling Main ( prob214ff.hs, prob214ff.o )
prob214ff.hs:14:1: error:
Could not find module ‘Data.Vector’
Perhaps you meant Data.Functor (from base-4.16.0.0)
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
14 | import Data.Vector
Thx #n. 1.8e9-where's-my-share !
ghc -package-db $HOME/.cabal/store/ghc-9.2.1/package.db prob214ff.hs
solves my problem although it is a regression from previous versions of ghc which didn't require the use of the -package-db option.
I'd love to use GHC_PACKAGE_PATH but I can't see how to make that work. Sec 5.9.5 of the ghc users guide is confusing. It states "A package database is where the details about installed packages are stored. It is a directory, usually called package.conf.d." i.e. not the package.db file that I gave with -package-db on the command line. But where is the package.conf.db directory with info on the vector package that I installed with cabal?
"ghc-pkg list" references /usr/local/lib/ghc-9.2.1/lib/package.conf.d but the output of the command doesn't list the vector package I installed.
"ghc-pkg list -f $HOME/.cabal/store/ghc-9.2.1/package.db" does list the vector package in it's output but doesn't tell me a package.conf.d directory that knows about the vector package.
This is a bit fiddly with cabal version 2. Installing packages no longer really makes sense (despite the cabal install still existing...). Instead you will probably find it easiest to create a cabal package instead, for example:
$ mkdir my-package
$ cd my-package
$ cabal init
<Edit my-package.cabal and add vector to the build-depends line>
<Put the contents of prob214ff.hs in app/Main.hs instead>
$ cabal run
This will be fixed in 9.2.2, see https://gitlab.haskell.org/ghc/ghc/-/issues/20660#note_391573

Acid-State example compiled error. No No instance for (Control.Monad.Reader.Class.MonadReader FailureDb (Query FailureDb))

I was trying acid example but compile error.
The error message says no instance No instance for MonadState, No instance for MonadReader.
This article says it is due to two different mtl versions installed.
And I am using nix-shell in nixos:
ghc-pkg list | grep acid
acid-state-0.14.3
ghc-pkg list | grep mtl
mtl-2.2.1
mtl-2.2.2
However, if I don't use acid-state package when I enter nix-shell, I got:
ghc-pkg list | grep mtl
mtl-2.2.2
That probably means acid-state is using different mtl version than Control.Monad.Reader.
But acid-state's stack.yaml in github is lts-11.15, which has mtl-2.2.2.
So what should I do to make it work?
Stack and Cabal-install have similar problems when dealing with GHC's package dbs, but these problems can be avoided:
Stack
When I use Stack, I don't run it in a nix-shell, so that the nix-shell can't add packages to the wrapped GHC command that Stack will then consider to be system packages ('global' in Cabal terminology). I am not sure whether this is the only valid approach though.
Cabal-install only
When calling Cabal in a nix-shell, you should add these flags to prevent GHC from looking at the wrong package db: cabal configure --package-db=clear --package-db=global. The GHC wrapper will make the Nix-managed dependencies appear in the 'global' package db.

Haskell cannot import GHC.SrcLoc

I can't get module "GHC.SrcLoc" in package "srcloc" on Ubuntu Haskell ...
-- "import" works on Windows, but not on Ubuntu 14.04
-- Module name is "GHC.SrcLoc"
-- Cabal build error below
Take 2: tried adding package "srcloc" to Cabal dependencies
^ Adding "srcloc" to dependencies causes "cabal configuration failed". ^
GHCI version...
$ ghci --version
The Glorious Glasgow Haskell Compilation System, version 7.6.3
Please help
https://hackage.haskell.org/package/base-4.8.2.0/docs/GHC-SrcLoc.html
My Haskell Code...
https://github.com/JohnReedLOL/HaskellPrintDebugger
* Attempt #1 *
:~/IdeaProjects/IntelliJHaskellPrintDebugger$ cabal install srcloc
Resolving dependencies...
All the requested packages are already installed:
srcloc-0.5.1.0
Use --reinstall if you want to reinstall anyway.
* Attempt #2 *
-- Tried replacing "import GHC.SrcLoc" with "import Data.Loc", but failed.
-- Tried adding "srcLock" to "build-depends" in .cabal, but failed.
^ Hopeless
Since Data.SrcLoc is a part of the GHC distribution and it wasn't added until (I believe) GHC 7.10.1, it will not exist in GHC 7.6.3. If you install the latest version of GHC, this should work fine.
The package you are seeing that is called srcloc appears to be unrelated to what you want, and does not provide the GHC.SrcLoc module. The GHC.SrcLoc module is instead provided by the GHC base standard library, which is included in (and requires) a newer version of GHC.
Newer versions of GHC also include a lot of other nice features that 7.6.3 (which was released more than 2 years ago) doesn't have.
You also mentioned stack traces, so you might be interested in this.
You must install the srcloc package; in the simple case where you are not using sandboxes, this is done by running cabal install srcloc at the command line.

How do you set up Haskell + ghc-mod on OS/X?

I first set up Haskell from https://www.haskell.org/platform/ but ghc-mod had problems with sandboxes and to update it I needed a newer cabal which needed a newer ghc. Or something like that. (https://stackoverflow.com/a/28049104/131227 ??)
So I deleted everything, and installed from http://ghcformacosx.github.io/.
Then some strange failure setting up a new sandbox was because something didn't properly have a dependancy for happy. (https://github.com/haskell-suite/haskell-src-exts/issues/14)
Ok. Installed happy. Now trying (again) to install ghc-mod and I get a giant mess (below).
Should I use http://www.stackage.org/?
I've seen some people mention ghc-pkg recache or cabal install cabal-install... Are those things I need to do?
Mess:
Resolving dependencies... Configuring ghc-mod-5.2.1.2... Building ghc-mod-5.2.1.2... Failed to install ghc-mod-5.2.1.2 Build log ( /Users/mark/.cabal/logs/ghc-mod-5.2.1.2.log ): Configuring ghc-mod-5.2.1.2... Building ghc-mod-5.2.1.2... Preprocessing library ghc-mod-5.2.1.2...
Language/Haskell/GhcMod/Convert.hs:1:51: Warning:
-XOverlappingInstances is deprecated: instead use per-instance pragmas OVERLAPPING/OVERLAPPABLE/OVERLAPS [ 1 of 38] Compiling Language.Haskell.GhcMod.Read ( Language/Haskell/GhcMod/Read.hs, dist/build/Language/Haskell/GhcMod/Read.o ) [ 2 of 38] Compiling Language.Haskell.GhcMod.Cabal21 ( Language/Haskell/GhcMod/Cabal21.hs, dist/build/Language/Haskell/GhcMod/Cabal21.o ) [ 3 of 38] Compiling Language.Haskell.GhcMod.Cabal18 ( Language/Haskell/GhcMod/Cabal18.hs, dist/build/Language/Haskell/GhcMod/Cabal18.o ) [ 4 of 38] Compiling Language.Haskell.GhcMod.Cabal16 ( Language/Haskell/GhcMod/Cabal16.hs, dist/build/Language/Haskell/GhcMod/Cabal16.o ) [ 5 of 38] Compiling Language.Haskell.GhcMod.GHCChoice ( Language/Haskell/GhcMod/GHCChoice.hs, dist/build/Language/Haskell/GhcMod/GHCChoice.o ) [ 6 of 38] Compiling Language.Haskell.GhcMod.Error ( Language/Haskell/GhcMod/Error.hs, dist/build/Language/Haskell/GhcMod/Error.o )
Language/Haskell/GhcMod/Error.hs:12:1: Warning:
Module ‘Control.Monad.Error’ is deprecated:
Use Control.Monad.Except instead
Language/Haskell/GhcMod/Error.hs:40:10: Warning:
In the use of type constructor or class ‘Error’
(imported from Control.Monad.Error, but defined in Control.Monad.Trans.Error):
Deprecated: "Use Control.Monad.Trans.Except instead"
Language/Haskell/GhcMod/Error.hs:40:10: Warning:
In the use of type constructor or class ‘Error’
(imported from Control.Monad.Error, but defined in Control.Monad.Trans.Error):
Deprecated: "Use Control.Monad.Trans.Except instead" [ 7 of 38] Compiling Language.Haskell.GhcMod.Utils ( Language/Haskell/GhcMod/Utils.hs, dist/build/Language/Haskell/GhcMod/Utils.o )
Language/Haskell/GhcMod/Utils.hs:15:1: Warning:
The import of ‘Control.Applicative’ is redundant
except perhaps to import instances from ‘Control.Applicative’
To import instances alone, use: import Control.Applicative()
Language/Haskell/GhcMod/Utils.hs:45:51: Warning:
In the use of ‘strMsg’
(imported from Language.Haskell.GhcMod.Error, but defined in Control.Monad.Trans.Error):
Deprecated: "Use Control.Monad.Trans.Except instead" [ 8 of 38] Compiling Language.Haskell.GhcMod.Types ( Language/Haskell/GhcMod/Types.hs, dist/build/Language/Haskell/GhcMod/Types.o ) [ 9 of 38] Compiling Language.Haskell.GhcMod.Gap ( Language/Haskell/GhcMod/Gap.hs, dist/build/Language/Haskell/GhcMod/Gap.o )
Language/Haskell/GhcMod/Gap.hs:256:18:
Not in scope: data constructor ‘ExposePackageId’
Perhaps you meant ‘ExposePackage’ (imported from DynFlags) cabal: Error: some packages failed to install: ghc-mod-5.2.1.2 failed during the building phase. The exception was: ExitFailure 1
After applying the accepted answer (which seemed to install ghc-mod), running gch-mod gives the following:
> ghc-mod check x.hs
cabal-helper-wrapper: Installing a private copy of Cabal, this might take a
while but will only happen once per Cabal version.
If anything goes horribly wrong just delete this directory and try again:
/Users/mark/.ghc-mod/cabal-helper
If you want to avoid this automatic installation altogether install version
1.22.0.0 of Cabal manually (into your user or global package-db):
$ cabal install Cabal-1.22.0.0
...In order, the following would be installed:
filepath-1.3.0.1 (latest: 1.4.0.0) (new version)
directory-1.2.2.1 (new version)
process-1.2.3.0 (reinstall) changes: directory-1.2.2.0 -> 1.2.2.1,
filepath-1.4.0.0 -> 1.3.0.1
Cabal-1.22.0.0 (latest: 1.22.2.0) (new version)
cabal: The following packages are likely to be broken by the reinstalls:
ghc-7.10.1
Cabal-1.22.2.0
Use --force-reinstalls if you want to install anyway.
cabal-helper-wrapper: Installing Cabal version 1.22.0.0 failed.
nYou have two choices now:
- Either you install this version of Cabal in your globa/luser package-db
somehow
n- Or you can see if you can update your cabal-install to use a different
version of the Cabal library that we can build with:
$ cabal install cabal-install --constraint 'Cabal > 1.22.0.0'
nTo check the version cabal-install is currently using try:
$ cabal --version
ghc-mod: readCreateProcess: /Users/mark/.cabal/libexec/cabal-helper-wrapper "/Users/mark/work/haskell/ixberg/dist" "entrypoints" "source-dirs" "ghc-options" "ghc-src-options" "ghc-pkg-options" "--with-ghc=ghc" "--with-ghc-pkg=ghc-pkg" "--with-cabal=cabal" (exit 1): failed
Strangely, it wants the version of Cabal that is currently installed:
> cabal --version
cabal-install version 1.22.0.0
using version 1.22.0.0 of the Cabal library
Edit (August 2015) Better fix
A better command line tool for installing Haskell binaries and libraries by the awesome FPComplete group called stack means you can install ghc-mod with stack install ghc-mod (outside a project to make it global) and it will just work. The binary will be installed to ~/.local/bin and should be put on your path so your editor can find it.
I will leave the below for anyone who wishes to know what cabal Hell really meant.
Problem With DanielG's Fork
I asked on the Haskell reddit and NihillstDandy explained that the GHC for Mac OS X does not register the Cabal library. This is not true for your install from http://ghcformacosx.github.io/. It does register the Cabal library.
ghc-mod needs the cabal library to work, but it does not compile with the cabal library. Instead it compiles with cabal-helper and that looks to see if you have a registered version of the cabal library. If it does not find any then cabal-helper-wrapper will install a private copy and in this case it tired to install the same version as what cabal-install was compiled with, cabal-1.22.0.0.
When I installed DanielG's Fork it worked for my current project, but after testing it in other projects I am getting the same error you are. This is a problem with the fork and not Haskell (as far as I can tell).
Original Answer
So, while I was putting this post together ghc-mod was fixed for GHC-7.10. Thanks to DanielG!
git clone https://github.com/DanielG/ghc-mod
cd ghc-mod
cabal install -j4
And if you get errors that say "setup-Simple-Cabal-1.22.2.0-x86_64-osx-ghc-7.10.1: The program 'happy' version
>=1.17 is required but it could not be found."
cabal install happy -j4
The same for any other problems and it should work. (Although you have happy installed, so it should just work).
The rest of the post is on how to downgrade the cabal executable, also known as cabal-install, to version 1.20.0.3. Although you could really use this to downgrade to any version. Since there is a working fork of ghc-mod that works with GHC-7.10 and cabal-1.22 you do not need to downgrade to fix ghc-mod.
I'm just leaving the rest of the post up here since, unfortunately, this is a problem with Haskell as a whole. Halcyon and nix are ways to deal with Haskell's shortcomings in this regard.
How to downgrade Cabal in Haskell
When I wrote this post ghc-mod, nor any fork, compiled with GHC-7.10 and versions below 7.10 broke with cabal-1.22 sandboxes.
However, even if you compile ghc-mod with GHC-7.8.4 (the version before 7.10) and cabal-1.20.0.3 you still cannot use ghc-mod inside a sandbox created by cabal-1.22 and above. So the only way to use ghc-mod is to either downgrade your entire system to cabal-1.20.0.3 or don't use sandboxes. This means you can still use GHC-7.10, but you have to use the older version of cabal. That is unless you want to wait until the convener or someone else fixes it for GHC-7.10. Its been over a week already, but some kind Haskell programmer fixed it in a fork (see above).
If this is something you still want here is how to compile ghc-mod with GHC-7.8.4 and cabal-1.20.0.3. Also, I replace the executable cabal-1.22 with the older cabal-1.20.0.3. Note, it does not matter what directory you download the sources to.
download GHC-7.8.4 source for your OS from here
tar -xf path_to_zipped_source
cd path_to_ghc-7.8.4
The next steps will install ghc-7.8.4 as ghc-7.8.4 in the same folder as ghc-7.10 is install for you. The -j4 is to tell make to use 4 threads to compile it.
./configure
make install -j4
To get the correct version of cabal we can just ask cabal to get it.
cabal get cabal-install-1.20.0.3
cd cabal-install-1.20.0.3
Now to install cabal with ghc-7.8.4. Here cabal-1.22 will pull all the decencies and should give you some warning about installing another version of the cabal library (not to be confused with cabal-install), this should not cause problems.
cabal --with-compiler=ghc-7.8.4 install
So now you should have cabal-1.20.0.4 installed, but not on your path. It will be in ~/.cabal/bin. We need to make this global, so remove cabal-1.22 (it only removes the symlink).
rm `which cabal`
And create the symlink to cabal-1.20.0.4. Btw, you will need to remove any sandboxes you made with cabal-1.22 and remake them with cabal-1.20.0.4.
If you do not have realpath you can just type in the full path of cabal. I use it for connivence.
cd ~/.cabal/bin
ln -s `realpath cabal` /usr/local/bin
Now this is how I compiled ghc-mod on my mac (you may run into your own problems). I'm not sure why cabal couldn't do it in one call, but this worked in the end.
cabal --with-compiler=ghc-7.8.4 install happy
cabal --with-compiler=ghc-7.8.4 install haskell-src-exts-1.16.0.1
cabal --with-compiler=ghc-7.8.4 install hlint-1.9.19
cabal --with-compiler=ghc-7.8.4 install ghc-mod
Now you should be able to call ghc-mod from anywhere. To test it just type ghc-mod after you have cabal sandbox init. If it does not throw any errors then your in the clear.
The version of executable cabal that is used to configure this project should be the same as the version of the global Cabal package.
It's easier to install a compatible version of cabal-install than Cabal as follows:
$ ghc-pkg list | grep Cabal
Cabal-1.22.2.0
$ cabal install cabal-install-1.22.2.0
Or build one in sandbox and replace/shadow the original one.
Then don't forget to re-configure the project with the new cabal:
$ cabal clean && cabal configure
Now the ghc-mod should works.
If Cabal-1.22 isn't registered (ghc-pkg list cabal), the cabal-helper-wrapper will install a private copy because it needs the library installed to work, but it doesn't want to stomp on anything you have that might break by upgrading Cabal directly.
GHC for Mac OS X doesn't register the Cabal library, it comes bundled with a version of cabal-install built against it.

Persistent modules not found

I'm trying to use http://hackage.haskell.org/package/persistent-1.2.3.0, but after installing it through "cabal install persistent", ghc doesn't find its exposed-modules:
Prelude> import Database.Persist.Types
<no location info>:
Could not find module `Database.Persist.Types'
It is not a module in the current program, or in any known package.
And ghc-pkg works fine:
$ ghc-pkg find-module Database.Persist.Types
/var/lib/ghc/package.conf.d
/home/apsk/.ghc/x86_64-linux-7.6.3/package.conf.d
persistent-1.2.3.0
Am I missing something or is this just a bug with cabal/persistent/ghc? My ghc is 7.6.3, btw.
Also, I've tried with no effect: "ghc-pkg recache"; "ghc-pkg check"; installing previous version; reinstalling with and without "sudo" and/or "--global".
Check that you're not using sandboxes, cabal-dev, hsenv, anything else that can influence ghc in looking for dependencies. Make sure you're using ghc/ghci, and not any wrappers, and that ghc/ghci are not aliased to anything in your shell.
Try to specify the package-db explicitly:
ghci -package-db /home/apsk/.ghc/x86_64-linux-7.6.3/package.conf.d
If you launch ghci with -v, it'll print the (caches of) databases it looks at, like this:
% ghci -v
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Glasgow Haskell Compiler, Version 7.6.3, stage 2 booted by GHC version 7.4.2
Using binary package database: /opt/ghc763/lib/ghc-7.6.3/package.conf.d/package.cache
Using binary package database: /home/feuerbach/.ghc/i386-linux-7.6.3/package.conf.d/package.cache
Make sure that the database which has persistent is listed there.
Hopefully helpful to someone. Using ghci -v I looked to see if persistent was there, and saw this:
package persistent-2.0.8-cec952b1a61645f47dbec3b0b0cbcef4 is unusable due to missing or recursive dependencies: aeson-0.8.0.0-1bd8b5254a1dd30c0fe6acc346ad7de7 attoparsec-0.12.1.2-48393fcdbcf426085b696dc4409d9270 conduit-1.2.0.2-39f9cd0430ed7b7f4306899cbeb1ed83 monad-logger-0.3.7.2-3e6a80e9b3adf31497ff04514bdf2919 resource-pool-0.2.3.0-c02186641e7173f72887d5e65a646ac1 scientific-0.3.3.1-13e0eefbd7215e4503420c3d0a6fdb82 unordered-containers-0.2.5.0-147c3bb8f4a2da7d753455e75af30b92
So I looked around on SO (saw this: Haskell Cabal: Mysterious missing or recursive dependencies) but didn't find a great answer. I did find a good one here though:
$ ghc-pkg list Cabal
Which revealed I had 2 goddamn cabals! One in user and one in global. Ugh. So I was able to do ghc-pkg unregister --user Cabal-1.18.1.3 --force to get rid of the old one. Then cabal install cabal automatically went for 1.20, implicitly into user without any flag (the alternate would be --global).
The problem only occurs in GHCi, right? Have you restarted GHCi since installing persistent? (Maybe there's another way to get GHCi to pick up newly installed packages, but I'm not aware of it.

Resources