Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #404 Accommodate ${pkgroot} and tolerate GHC errors on Windows #405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/Input/Cabal.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE ViewPatterns, PatternGuards, TupleSections, RecordWildCards, ScopedTypeVariables #-}
{-# LANGUAGE CPP, ViewPatterns, PatternGuards, TupleSections, RecordWildCards, ScopedTypeVariables #-}

-- | Module for reading Cabal files.
module Input.Cabal(
Expand Down Expand Up @@ -79,19 +79,38 @@ packagePopularity cbl = mp `seq` (errs, mp)
-- | Run 'ghc-pkg' and get a list of packages which are installed.
readGhcPkg :: Settings -> IO (Map.Map PkgName Package)
readGhcPkg settings = do
-- On Windows, the `haddock-html` field in `*.conf` files for GHC boot
-- libraries for GHC >= 9.0 (up to at least GHC 9.6.2) contain errors. For
-- example, this may be specified:
-- haddock-html: ${pkgroot}/../../doc/html/libraries/base-4.18.0.0
-- when the correct specification would be:
-- haddock-html: ${pkgroot}/../doc/html/libraries/base-4.18.0.0
-- So, a unique approach is taken to `readGhcPkg` in that case. It assumes that
-- the `*.conf` files have not been manually corrected.
#if defined(mingw32_HOST_OS) && __GLASGOW_HASKELL__>=901

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This CPP should have an upper bound, it is fixed in 9.10.1.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's a great idea to bake this logic about some broken bindists into hoogle, someone could build their own bindist which doesn't have this bug and it would be broken.

let ghcPkgArgs = ["dump"]
-- ^ Do not expand the ${pkgroot} (by default)
unwantedPrefix = "${pkgroot}/.."
-- ^ Should result in ${pkgroot}/../bin/../doc/html/libraries/...
#else
-- From GHC 9.0.1, the `haddock-html` field in `*.conf` files for GHC boot
-- libraries has used `${pkgroot}`, which can be expanded in the output.
let ghcPkgArgs = ["dump", "--expand-pkgroot"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using --expand-pkgroot is a good fix.

unwantedPrefix = "$topdir"
-- ^ Backwards compatibility with GHC < 9.0
#endif
topdir <- findExecutable "ghc-pkg"
-- important to use BS process reading so it's in Binary format, see #194
(exit, stdout, stderr) <- BS.readProcessWithExitCode "ghc-pkg" ["dump"] mempty
(exit, stdout, stderr) <- BS.readProcessWithExitCode "ghc-pkg" ghcPkgArgs mempty
when (exit /= ExitSuccess) $
errorIO $ "Error when reading from ghc-pkg, " ++ show exit ++ "\n" ++ UTF8.toString stderr
let g (stripPrefix "$topdir" -> Just x) | Just t <- topdir = takeDirectory t ++ x
let g (stripPrefix unwantedPrefix -> Just x) | Just t <- topdir = takeDirectory t ++ x
g x = x
let fixer p = p{packageLibrary = True, packageDocs = g <$> packageDocs p}
let f ((stripPrefix "name: " -> Just x):xs) = Just (strPack $ trimStart x, fixer $ readCabal settings $ unlines xs)
f xs = Nothing
pure $ Map.fromList $ mapMaybe f $ splitOn ["---"] $ lines $ filter (/= '\r') $ UTF8.toString stdout


-- | Given a tarball of Cabal files, parse the latest version of each package.
parseCabalTarball :: Settings -> FilePath -> IO (Map.Map PkgName Package)
-- items are stored as:
Expand Down