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

hackage-db: support cabal-install 3.10.1.0 XDG paths. #597

Merged
merged 4 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions hackage-db/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# Revision history for hackage-db

## 2.1.3

* `hackageTarball` / `cabalStateDir` now support overriding the cabal directory
location by setting the `CABAL_DIR` environment variable. This is useful if
`hackage-db` doesn't detect the correct location on its own:

- A matching cabal state directory may exist, but should not be used for some
reason.

- A non-standard cabal state directory may be used, but `hackage-db` can't
find it (as it doesn't check the `cabal-install` configuration file).

* `hackageTarball` now supports all state dir location(s) (newly) supported by
`cabal-install`. If `CABAL_DIR` is not set, it will look in the following
locations in that order:

1. `$HOME/.cabal`, the classic location, will be preferred if it exists.
2. `$XDG_CACHE_HOME/cabal` (usually `$HOME/.cache/cabal`) is used otherwise.
`cabal-install` 3.10.1.0 and newer will default to this location for
fresh installations.

## 2.1.2

Fix a bug which lead to `parsePackageData` always failing if the package had
Expand Down
2 changes: 1 addition & 1 deletion hackage-db/hackage-db.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: hackage-db
version: 2.1.2
version: 2.1.3
synopsis: Access cabal-install's Hackage database via Data.Map
description: This library provides convenient access to the local copy of the Hackage
database that \"cabal update\" creates. Check out
Expand Down
37 changes: 33 additions & 4 deletions hackage-db/src/Distribution/Hackage/DB/Path.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,37 @@ import Distribution.Hackage.DB.Errors

import Control.Exception
import System.Directory
import System.Environment (lookupEnv)
import System.FilePath

-- |
-- Determines the /state/ directory (which e.g. holds the hackage tarball)
-- cabal-install uses via the following logic:
--
-- 1. If the @CABAL_DIR@ environment variable is set, its content is used as the
-- cabal state directory
-- 2. If @~/.cabal@ (see 'getAppUserDataDirectory') exists, use that.
-- 3. Otherwise, use @${XDG_CACHE_HOME}/cabal@ (see @'getXdgDirectory' 'XdgCache'@)
-- which is the new directory cabal-install can use starting with
-- version @3.10.*@.
--
-- This logic is mostly equivalent to what upstream cabal-install is
-- [doing](https://github.com/haskell/cabal/blob/0ed12188525335ac9759dc957d49979ab09382a1/cabal-install/src/Distribution/Client/Config.hs#L594-L610)
-- with the following exception:
-- The state directory can freely be configured to use a different location
-- in the cabal-install configuration file. hackage-db doesn't parse this
-- configuration file, so differing state directories are ignored.
cabalStateDir :: IO FilePath
cabalStateDir = getAppUserDataDirectory "cabal"
cabalStateDir = do
envCabal <- lookupEnv "CABAL_DIR"
dotCabal <- getAppUserDataDirectory "cabal"
dotCabalExists <- doesDirectoryExist dotCabal
case envCabal of
Just dir -> pure dir
Nothing ->
if dotCabalExists
then pure dotCabal
else getXdgDirectory XdgCache "cabal"
Comment on lines +45 to +48
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Nothing ->
if dotCabalExists
then pure dotCabal
else getXdgDirectory XdgCache "cabal"
Nothing | dotCabalExists -> pure dotCabal
Nothing -> getXdgDirectory XdgCache "cabal"

Really unimportant. Just suggesting it to learn whether you also like it better and if not, why not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was copy-pasted from the cabal source code which doesn't seem to use guards much.

Copy link
Member

Choose a reason for hiding this comment

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

I think having the control flow more explicit is more readable here (at least to me) and it also makes the diff easier to discern. If you'd want to make the code nice, you'd probably use Alternative or something, but can't be bothered, really :)


cabalTarballDir :: String -> IO FilePath
cabalTarballDir repo = do
Expand All @@ -29,9 +56,11 @@ hackageTarballDir :: IO FilePath
hackageTarballDir = cabalTarballDir "hackage.haskell.org"

-- | Determine the default path of the Hackage database, which typically
-- resides at @"$HOME\/.cabal\/packages\/hackage.haskell.org\/00-index.tar"@.
-- Running the command @"cabal update"@ will keep that file up-to-date.

-- resides in @$HOME\/.cabal\/packages\/hackage.haskell.org\/@.
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to adapt this path to the new default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's necessary.

-- Running the command @cabal update@ or @cabal v2-update@ will keep the index
-- up-to-date.
--
-- See 'cabalStateDir' on how @hackage-db@ searches for the cabal state directory.
hackageTarball :: IO FilePath
hackageTarball = do
htd <- hackageTarballDir
Expand Down