Parsing ghc-pkg dump

Is there a library for parsing ghc-pkg dump? It looks like a cabal file … would Cabal work?

Context: I want to pipe a list of all modules in scope to fzf in an elisp script that adds imports to my file.

It’s strange because the --help for ghc-pkg dump says

  ghc-pkg-9.4.8 dump
    Dump the registered description for every package.  This is like
    "ghc-pkg describe '*'", except that it is intended to be used
    by tools that parse the results, rather than humans.  The output is
    always encoded in UTF-8, regardless of the current locale.

but the format doesn’t look obviously easy to parse, to me. It might be easier to hack ghc-pkg to dump the data in JSON format than to parse it.

1 Like

I haven’t used ghc-pkg dump before. It looks like it outputs a bunch of *.cabal files separated by ---. That’s what the documentation says too:

Emit the full description of every package, in the form of an InstalledPackageInfo . Multiple package descriptions are separated by the string --- on a line by itself.

You certainly could parse all of those with Cabal, but it might be a bit tedious. I think you can use ghc-pkg by itself to get what you’re after.

# Get a space-separated list of package names:
$ ghc-pkg list --names-only --simple-output

# For each package, get a comma-separated list of module names:
$ ghc-pkg field base exposed-modules --simple-output
1 Like

Does this part of the GHC User’s Guide assist: 5.9. Packages — Glasgow Haskell Compiler 9.8.1 User's Guide

An InstalledPackageInfo has a human readable/writable syntax. The functions parseInstalledPackageInfo and showInstalledPackageInfo read and write this syntax respectively.

Also: Distribution.InstalledPackageInfo

1 Like