Skip to content

Commit 90fbf08

Browse files
committed
Replace FilePath in PackageDB with SymbolicPath in Cabal
This refactoring enforces a simple property * We use symbolic paths in Cabal in order to represent that paths to package databases. These paths is relative to the package root. * We use normal filepaths in cabal-install to represent the path to a package database. These are relative to the current working directory. Paths are explicitly converted from one type to the other at the interface of `cabal-install` and `Cabal`, see `setupHsConfigureArgs` for where this happens. In order to achieve this `PackageDB` is abstracted over what the type of filepaths a specific package db points to. ``` type PackageDBX fp = ... | SpecificPackageDB fp | ... ``` If you are using the Cabal library then you probably want to migrate to use `PackageDBCWD` and `PackageDBStackCWD`. ``` type PackageDBCWD = PackageDBX FilePath type PackageDBStackCWD = [PackageDBCWD] ``` Then at the point where you call commands in the `Cabal` library convert these paths into paths relative to the root of the relevant package. The easiest way to do this is convert any paths into an absolute path. This patch fixes a double interpretation issue when the `--working-dir` option was used and package db paths were offset incorrectly.
1 parent 08f028f commit 90fbf08

File tree

51 files changed

+367
-318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+367
-318
lines changed

Cabal-QuickCheck/src/Test/QuickCheck/Instances/Cabal.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE TypeOperators #-}
3+
{-# LANGUAGE FlexibleInstances #-}
34
{-# OPTIONS_GHC -fno-warn-orphans #-}
45
module Test.QuickCheck.Instances.Cabal () where
56

@@ -18,7 +19,7 @@ import Distribution.Compat.NonEmptySet (NonEmptySet)
1819
import Distribution.Compiler
1920
import Distribution.FieldGrammar.Newtypes
2021
import Distribution.ModuleName
21-
import Distribution.Simple.Compiler (DebugInfoLevel (..), OptimisationLevel (..), PackageDB (..), ProfDetailLevel (..), knownProfDetailLevels)
22+
import Distribution.Simple.Compiler
2223
import Distribution.Simple.Flag (Flag (..))
2324
import Distribution.Simple.InstallDirs
2425
import Distribution.Simple.Setup (HaddockTarget (..), TestShowDetails (..), DumpBuildInfo)
@@ -476,7 +477,7 @@ instance Arbitrary TestShowDetails where
476477
-- PackageDB
477478
-------------------------------------------------------------------------------
478479

479-
instance Arbitrary PackageDB where
480+
instance Arbitrary (PackageDBX FilePath) where
480481
arbitrary = oneof [ pure GlobalPackageDB
481482
, pure UserPackageDB
482483
, SpecificPackageDB <$> arbitraryShortPath

Cabal-syntax/src/Distribution/Utils/Path.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module Distribution.Utils.Path
6060
-- ** Working directory handling
6161
, interpretSymbolicPathCWD
6262
, absoluteWorkingDir
63-
, tryMakeRelativeToWorkingDir
63+
, tryMakeRelative
6464

6565
-- ** Module names
6666
, moduleNameSymbolicPath
@@ -290,7 +290,7 @@ moduleNameSymbolicPath modNm = SymbolicPath $ ModuleName.toFilePath modNm
290290
-- (because the program might expect certain paths to be relative).
291291
--
292292
-- See Note [Symbolic paths] in Distribution.Utils.Path.
293-
interpretSymbolicPath :: Maybe (SymbolicPath CWD (Dir Pkg)) -> SymbolicPathX allowAbsolute Pkg to -> FilePath
293+
interpretSymbolicPath :: Maybe (SymbolicPath CWD (Dir from)) -> SymbolicPathX allowAbsolute from to -> FilePath
294294
interpretSymbolicPath mbWorkDir (SymbolicPath p) =
295295
-- Note that this properly handles an absolute symbolic path,
296296
-- because if @q@ is absolute, then @p </> q = q@.
@@ -317,7 +317,7 @@ interpretSymbolicPath mbWorkDir (SymbolicPath p) =
317317
-- appropriate to use 'interpretSymbolicPathCWD' to provide its arguments.
318318
--
319319
-- See Note [Symbolic paths] in Distribution.Utils.Path.
320-
interpretSymbolicPathCWD :: SymbolicPathX allowAbsolute Pkg to -> FilePath
320+
interpretSymbolicPathCWD :: SymbolicPathX allowAbsolute from to -> FilePath
321321
interpretSymbolicPathCWD (SymbolicPath p) = p
322322

323323
-- | Change what a symbolic path is pointing to.
@@ -347,11 +347,13 @@ absoluteWorkingDir :: Maybe (SymbolicPath CWD to) -> IO FilePath
347347
absoluteWorkingDir Nothing = Directory.getCurrentDirectory
348348
absoluteWorkingDir (Just wd) = Directory.makeAbsolute $ getSymbolicPath wd
349349

350-
-- | Try to make a path relative to the current working directory.
350+
-- | Try to make a symbolic path relative.
351+
--
352+
-- This function does nothing if the path is already relative.
351353
--
352354
-- NB: this function may fail to make the path relative.
353-
tryMakeRelativeToWorkingDir :: Maybe (SymbolicPath CWD (Dir dir)) -> SymbolicPath dir to -> IO (SymbolicPath dir to)
354-
tryMakeRelativeToWorkingDir mbWorkDir (SymbolicPath fp) = do
355+
tryMakeRelative :: Maybe (SymbolicPath CWD (Dir dir)) -> SymbolicPath dir to -> IO (SymbolicPath dir to)
356+
tryMakeRelative mbWorkDir (SymbolicPath fp) = do
355357
wd <- absoluteWorkingDir mbWorkDir
356358
return $ SymbolicPath (FilePath.makeRelative wd fp)
357359

Cabal-tests/tests/UnitTests/Distribution/Utils/Structured.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ md5CheckGenericPackageDescription proxy = md5Check proxy
3434

3535
md5CheckLocalBuildInfo :: Proxy LocalBuildInfo -> Assertion
3636
md5CheckLocalBuildInfo proxy = md5Check proxy
37-
0x2c8550e1552f68bf169fafbfcd8f845a
37+
0x94827844fdb1afedee525061749fb16f

Cabal-tests/tests/custom-setup/CabalDoctestSetup.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ import Distribution.Simple
103103
(UserHooks (..), autoconfUserHooks, defaultMainWithHooks,
104104
simpleUserHooks)
105105
import Distribution.Simple.Compiler
106-
(CompilerFlavor (GHC), CompilerId (..), PackageDB (..), compilerId)
106+
(CompilerFlavor (GHC), CompilerId (..), PackageDB, PackageDBX (..), compilerId)
107107
import Distribution.Simple.LocalBuildInfo
108108
(ComponentLocalBuildInfo (componentPackageDeps), LocalBuildInfo,
109109
compiler, withExeLBI, withLibLBI, withPackageDB, withTestLBI
@@ -119,8 +119,6 @@ import Distribution.Simple.Utils
119119
import Distribution.Text
120120
(display)
121121
import Distribution.Verbosity
122-
import System.FilePath
123-
((</>))
124122

125123
import qualified Data.Foldable as F
126124
(for_)
@@ -160,7 +158,9 @@ import Distribution.Package
160158
import Distribution.Utils.Path
161159
( SymbolicPathX
162160
, makeSymbolicPath
163-
, makeRelativePathEx )
161+
, makeRelativePathEx
162+
, interpretSymbolicPathCWD
163+
, (</>))
164164
import qualified Distribution.Utils.Path as Cabal
165165
(getSymbolicPath)
166166
import Distribution.Simple.Utils
@@ -336,7 +336,7 @@ generateBuildModule testSuiteName flags pkg lbi = do
336336
let distPref = fromFlag (buildDistPref flags)
337337

338338
-- Package DBs & environments
339-
let dbStack = withPackageDB lbi ++ [ SpecificPackageDB $ toFilePath distPref </> "package.conf.inplace" ]
339+
let dbStack = withPackageDB lbi ++ [ SpecificPackageDB $ distPref </> makeRelativePathEx "package.conf.inplace" ]
340340
let dbFlags = "-hide-all-packages" : packageDbArgs dbStack
341341
let envFlags
342342
| ghcCanBeToldToIgnorePkgEnvs = [ "-package-env=-" ]
@@ -539,7 +539,7 @@ generateBuildModule testSuiteName flags pkg lbi = do
539539
: concatMap specific dbs
540540
_ -> ierror
541541
where
542-
specific (SpecificPackageDB db) = [ "-package-conf=" ++ db ]
542+
specific (SpecificPackageDB db) = [ "-package-conf=" ++ interpretSymbolicPathCWD db ]
543543
specific _ = ierror
544544
ierror = error $ "internal error: unexpected package db stack: "
545545
++ show dbstack
@@ -557,7 +557,7 @@ generateBuildModule testSuiteName flags pkg lbi = do
557557
dbs -> "-clear-package-db"
558558
: concatMap single dbs
559559
where
560-
single (SpecificPackageDB db) = [ "-package-db=" ++ db ]
560+
single (SpecificPackageDB db) = [ "-package-db=" ++ interpretSymbolicPathCWD db ]
561561
single GlobalPackageDB = [ "-global-package-db" ]
562562
single UserPackageDB = [ "-user-package-db" ]
563563
isSpecific (SpecificPackageDB _) = True

Cabal/src/Distribution/Simple/Build.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ createInternalPackageDB verbosity lbi distPref = do
911911
existsAlready <- doesPackageDBExist dbPath
912912
when existsAlready $ deletePackageDB dbPath
913913
createPackageDB verbosity (compiler lbi) (withPrograms lbi) False dbPath
914-
return (SpecificPackageDB dbPath)
914+
return (SpecificPackageDB dbRelPath)
915915
where
916916
dbRelPath = internalPackageDBPath lbi distPref
917917
dbPath = interpretSymbolicPathLBI lbi dbRelPath

Cabal/src/Distribution/Simple/Compiler.hs

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE DeriveDataTypeable #-}
3+
{-# LANGUAGE DeriveFoldable #-}
4+
{-# LANGUAGE DeriveFunctor #-}
35
{-# LANGUAGE DeriveGeneric #-}
6+
{-# LANGUAGE DeriveTraversable #-}
47

58
-----------------------------------------------------------------------------
69

@@ -35,11 +38,21 @@ module Distribution.Simple.Compiler
3538
, compilerInfo
3639

3740
-- * Support for package databases
38-
, PackageDB (..)
41+
, PackageDB
3942
, PackageDBStack
43+
, PackageDBCWD
44+
, PackageDBStackCWD
45+
, PackageDBX (..)
46+
, PackageDBStackX
47+
, PackageDBS
48+
, PackageDBStackS
4049
, registrationPackageDB
4150
, absolutePackageDBPaths
4251
, absolutePackageDBPath
52+
, interpretPackageDB
53+
, interpretPackageDBStack
54+
, coercePackageDB
55+
, coercePackageDBStack
4356

4457
-- * Support for optimisation levels
4558
, OptimisationLevel (..)
@@ -95,7 +108,6 @@ import Language.Haskell.Extension
95108

96109
import qualified Data.Map as Map (lookup)
97110
import System.Directory (canonicalizePath)
98-
import System.FilePath (isRelative)
99111

100112
data Compiler = Compiler
101113
{ compilerId :: CompilerId
@@ -181,15 +193,17 @@ compilerInfo c =
181193
-- the file system. This can be used to build isolated environments of
182194
-- packages, for example to build a collection of related packages
183195
-- without installing them globally.
184-
data PackageDB
196+
--
197+
-- Abstracted over
198+
data PackageDBX fp
185199
= GlobalPackageDB
186200
| UserPackageDB
187201
| -- | NB: the path might be relative or it might be absolute
188-
SpecificPackageDB FilePath
189-
deriving (Eq, Generic, Ord, Show, Read, Typeable)
202+
SpecificPackageDB fp
203+
deriving (Eq, Generic, Ord, Show, Read, Typeable, Functor, Foldable, Traversable)
190204

191-
instance Binary PackageDB
192-
instance Structured PackageDB
205+
instance Binary fp => Binary (PackageDBX fp)
206+
instance Structured fp => Structured (PackageDBX fp)
193207

194208
-- | We typically get packages from several databases, and stack them
195209
-- together. This type lets us be explicit about that stacking. For example
@@ -206,11 +220,20 @@ instance Structured PackageDB
206220
-- we can use several custom package dbs and the user package db together.
207221
--
208222
-- When it comes to writing, the top most (last) package is used.
209-
type PackageDBStack = [PackageDB]
223+
type PackageDBStackX from = [PackageDBX from]
224+
225+
type PackageDB = PackageDBX (SymbolicPath Pkg (Dir PkgDB))
226+
type PackageDBStack = PackageDBStackX (SymbolicPath Pkg (Dir PkgDB))
227+
228+
type PackageDBS from = PackageDBX (SymbolicPath from (Dir PkgDB))
229+
type PackageDBStackS from = PackageDBStackX (SymbolicPath from (Dir PkgDB))
230+
231+
type PackageDBCWD = PackageDBX FilePath
232+
type PackageDBStackCWD = PackageDBStackX FilePath
210233

211234
-- | Return the package that we should register into. This is the package db at
212235
-- the top of the stack.
213-
registrationPackageDB :: PackageDBStack -> PackageDB
236+
registrationPackageDB :: PackageDBStackX from -> PackageDBX from
214237
registrationPackageDB dbs = case safeLast dbs of
215238
Nothing -> error "internal error: empty package db set"
216239
Just p -> p
@@ -230,10 +253,30 @@ absolutePackageDBPath _ GlobalPackageDB = return GlobalPackageDB
230253
absolutePackageDBPath _ UserPackageDB = return UserPackageDB
231254
absolutePackageDBPath mbWorkDir (SpecificPackageDB db) = do
232255
let db' =
233-
if isRelative db
234-
then interpretSymbolicPath mbWorkDir (makeRelativePathEx db)
235-
else db
236-
SpecificPackageDB <$> canonicalizePath db'
256+
case symbolicPathRelative_maybe db of
257+
Nothing -> getSymbolicPath db
258+
Just rel_path -> interpretSymbolicPath mbWorkDir rel_path
259+
SpecificPackageDB . makeSymbolicPath <$> canonicalizePath db'
260+
261+
interpretPackageDB :: Maybe (SymbolicPath CWD (Dir Pkg)) -> PackageDB -> PackageDBCWD
262+
interpretPackageDB _ GlobalPackageDB = GlobalPackageDB
263+
interpretPackageDB _ UserPackageDB = UserPackageDB
264+
interpretPackageDB mbWorkDir (SpecificPackageDB db) =
265+
SpecificPackageDB (interpretSymbolicPath mbWorkDir db)
266+
267+
interpretPackageDBStack :: Maybe (SymbolicPath CWD (Dir Pkg)) -> PackageDBStack -> PackageDBStackCWD
268+
interpretPackageDBStack mbWorkDir = map (interpretPackageDB mbWorkDir)
269+
270+
-- | Transform a package db using a FilePath into one using symbolic paths.
271+
coercePackageDB :: PackageDBCWD -> PackageDBX (SymbolicPath CWD (Dir PkgDB))
272+
coercePackageDB GlobalPackageDB = GlobalPackageDB
273+
coercePackageDB UserPackageDB = UserPackageDB
274+
coercePackageDB (SpecificPackageDB db) = SpecificPackageDB (makeSymbolicPath db)
275+
276+
coercePackageDBStack
277+
:: [PackageDBCWD]
278+
-> [PackageDBX (SymbolicPath CWD (Dir PkgDB))]
279+
coercePackageDBStack = map coercePackageDB
237280

238281
-- ------------------------------------------------------------
239282

Cabal/src/Distribution/Simple/Configure.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,8 +2036,8 @@ reportFailedDependencies verbosity failed =
20362036
getInstalledPackages
20372037
:: Verbosity
20382038
-> Compiler
2039-
-> Maybe (SymbolicPath CWD (Dir Pkg))
2040-
-> PackageDBStack
2039+
-> Maybe (SymbolicPath CWD (Dir from))
2040+
-> PackageDBStackX (SymbolicPath from (Dir PkgDB))
20412041
-- ^ The stack of package databases.
20422042
-> ProgramDb
20432043
-> IO InstalledPackageIndex
@@ -2051,14 +2051,14 @@ getInstalledPackages verbosity comp mbWorkDir packageDBs progdb = do
20512051
case compilerFlavor comp of
20522052
GHC -> GHC.getInstalledPackages verbosity comp mbWorkDir packageDBs' progdb
20532053
GHCJS -> GHCJS.getInstalledPackages verbosity mbWorkDir packageDBs' progdb
2054-
UHC -> UHC.getInstalledPackages verbosity comp packageDBs' progdb
2054+
UHC -> UHC.getInstalledPackages verbosity comp mbWorkDir packageDBs' progdb
20552055
HaskellSuite{} ->
20562056
HaskellSuite.getInstalledPackages verbosity packageDBs' progdb
20572057
flv ->
20582058
dieWithException verbosity $ HowToFindInstalledPackages flv
20592059
where
20602060
packageDBExists (SpecificPackageDB path0) = do
2061-
let path = interpretSymbolicPath mbWorkDir $ makeSymbolicPath path0
2061+
let path = interpretSymbolicPath mbWorkDir path0
20622062
exists <- doesPathExist path
20632063
unless exists $
20642064
warn verbosity $
@@ -2096,8 +2096,8 @@ getPackageDBContents verbosity comp mbWorkDir packageDB progdb = do
20962096
getInstalledPackagesMonitorFiles
20972097
:: Verbosity
20982098
-> Compiler
2099-
-> Maybe (SymbolicPath CWD ('Dir Pkg))
2100-
-> PackageDBStack
2099+
-> Maybe (SymbolicPath CWD ('Dir from))
2100+
-> PackageDBStackS from
21012101
-> ProgramDb
21022102
-> Platform
21032103
-> IO [FilePath]
@@ -2144,7 +2144,7 @@ getInstalledPackagesById verbosity lbi@LocalBuildInfo{compiler = comp, withPacka
21442144
-- @--global@, @--user@ and @--package-db=global|user|clear|$file@.
21452145
-- This function combines the global/user flag and interprets the package-db
21462146
-- flag into a single package db stack.
2147-
interpretPackageDbFlags :: Bool -> [Maybe PackageDB] -> PackageDBStack
2147+
interpretPackageDbFlags :: Bool -> [Maybe (PackageDBX fp)] -> PackageDBStackX fp
21482148
interpretPackageDbFlags userInstall specificDBs =
21492149
extra initialStack specificDBs
21502150
where

0 commit comments

Comments
 (0)