Cabal list --installed doesn't find recently installed package

Hello community,

I would like to execute the following script,

import Text.Pandoc.JSON

main = toJSONFilter delink

delink :: Inline -> [Inline]
delink (Link _ txt _) = txt
delink x              = [x]

So, I installed pandoc-types using

cabal install pandoc-types

Then, I confirm successful installation with:

cabal list --installed pandoc-types
No matches found.

Why is this package not being listed ?

Setup:

macOS Sonoma
MacBook Air M1
Installed Haskell platfrom through GHCUP

Neither cabal install nor cabal list do what you are expecting them too. The former will install executables not libraries (you need to pass the --lib flag for that). Even if you installed a library like that, it would be in the “store”, not the pkg-db – the former is a set of packages available to cabal to use, and the latter is the packages that ghc will “always” see. Install --lib places packages in the store, then makes them available via setting up a ghc environment file for them.

The recommended way to build projects is to create a cabal project for them, at which point all the library stuff is handled automatically, rather than through manual installation and configuration of environment fiels.

For a simple script, it is recommended again to not use manual package management, but instead a cabal script.

2 Likes

I never heard about this. Thank you so much, @sclv. !

Where can I find more info about this ?

Try this command (jq required):

ghc-pkg --package-db=$(cabal path  --output-format=json | jq -r '."store-dir" + "/" + .compiler.id + "-inplace/package.db"') list 

Explanation:

  1. cabal path --output-format=json | jq -r '."store-dir" + "/" + .compiler.id + "-inplace/package.db"' this will give you the store-dir for the default GHC that cabal build package db for you.
    e.g.
    $ cabal path  --output-format=json | jq -r '."store-dir" + "/" + .compiler.id + "-inplace/package.db"'
    /home/hellwolf/.cabal/store/ghc-9.10.1-inplace/package.db
    
  2. ghc-pkg --package-db list reads that package db and list packages for you.

Like the other stuff I linked to, it can be found in the cabal user guide: 4.2. Commands — Cabal 3.14.2.0 User's Guide

and also: 3. How to build locally like in Nix — Cabal 3.14.2.0 User's Guide

1 Like

Thank you,

I tried the sample

myscript.hs

#!/usr/bin/env cabal
{- cabal:
build-depends:
  base ^>=4.19.0.0,
  haskell-say ^>=1.0.0.0
-}

import HaskellSay (haskellSay)

main :: IO ()
main = haskellSay "Hello, Haskell!"

Set permissions

chmod +x myscript.hs

And executed:

cabal run myscript.hs

However, I get:

Error: [Cabal-7136]
There is no <pkgname>.cabal package file or cabal.project file. To build packages locally you need at minimum a <pkgname>.cabal file. You can use 'cabal init' to create one.

For non-trivial projects you will also want a cabal.project file in the root directory of your project. This file lists the packages in your project and all other build configuration. See the Cabal user guide for full details.

Do you know what’s happening ?

Is #!/usr/bin/env cabal the very first line in the file? What is cabal --version?

Thank you for posting,

I added a comment with the name of the file for clarity, but yes it’s the first line in the file.

On my system,

cabal-install version 3.12.1.0
compiled using version 3.12.1.0 of the Cabal library 

@sclv, I had to go from base ^>=4.19.0.0 to just base and works now ! Thank you for your help !

1 Like

I got

ghc-pkg-9.4.8: /Users/detach/.cabal/store/ghc-9.4.8-inplace/package.db: getDirectoryContents:openDirStream: does not exist (No such file or directory)

@unlocked2412 since you already chmod +x myscript.hs, please try ./myscript.hs instead of cabal run myscript.hs.

1 Like

That works too ! Thank you !