Skip to content

Commit 1907b12

Browse files
committed
cabal-install: Clarify the semantics of package-db flag
In this PR we make the `--package-db` flag apply only to the default immutable initial package stack rather than also applying to the store package database. There are two special package databases which cabal install knows about and treats specially. * The store directory, a global cache of installed packages where cabal builds and installs packages into. * The inplace directory, a local build folder for packages, where cabal builds local packages in. It is very important that cabal registers packages it builds into one of these two locations as there are many assumptions that packages will end up here. With the current design of the `--package-db` flag, you are apparently allowed to override the store location which should have the effect of making the last package database in the package stack the "store" directory. Perhaps this works out in theory but practically this behaviour was broken and things were always registered into the store directory that cabal knew about. (The assertion in `ProjectBuilding.UnpackedPackage` was failing (see added test)). With `--package-db` not being able to modify the location of the store directory then the interaction of `--package-db`, `--store-dir` and `--dist-dir` flags become easy to understand. * `--package-db` modify the initial package database stack, these package database will not be mutated by cabal and provide the initial package environment which is used by cabal. * `--store-dir` modify the location of the store directory * `--dist-dir` modify the location of the dist directory (and hence inplace package database) Treating the flags in this way also fix an assertion failure when building packages. Fixes #9678
1 parent 125cd2d commit 1907b12

File tree

17 files changed

+146
-35
lines changed

17 files changed

+146
-35
lines changed

cabal-install/src/Distribution/Client/CmdInstall.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ installAction flags@NixStyleFlags{extraFlags, configFlags, installFlags, project
412412
, projectConfigHcPkg
413413
, projectConfigStoreDir
414414
, projectConfigProgPathExtra
415+
, projectConfigPackageDBs
415416
}
416417
, projectConfigLocalPackages =
417418
PackageConfig
@@ -443,7 +444,7 @@ installAction flags@NixStyleFlags{extraFlags, configFlags, installFlags, project
443444
envFile <- getEnvFile clientInstallFlags platform compilerVersion
444445
existingEnvEntries <-
445446
getExistingEnvEntries verbosity compilerFlavor supportsPkgEnvFiles envFile
446-
packageDbs <- getPackageDbStack compiler projectConfigStoreDir projectConfigLogsDir
447+
packageDbs <- getPackageDbStack compiler projectConfigStoreDir projectConfigLogsDir projectConfigPackageDBs
447448
installedIndex <- getInstalledPackages verbosity compiler packageDbs progDb
448449

449450
let
@@ -1281,13 +1282,14 @@ getPackageDbStack
12811282
:: Compiler
12821283
-> Flag FilePath
12831284
-> Flag FilePath
1285+
-> [Maybe PackageDB]
12841286
-> IO PackageDBStack
1285-
getPackageDbStack compiler storeDirFlag logsDirFlag = do
1287+
getPackageDbStack compiler storeDirFlag logsDirFlag packageDbs = do
12861288
mstoreDir <- traverse makeAbsolute $ flagToMaybe storeDirFlag
12871289
let
12881290
mlogsDir = flagToMaybe logsDirFlag
12891291
cabalLayout <- mkCabalDirLayout mstoreDir mlogsDir
1290-
pure $ storePackageDBStack (cabalStoreDirLayout cabalLayout) compiler
1292+
pure $ storePackageDBStack (cabalStoreDirLayout cabalLayout) compiler packageDbs
12911293

12921294
-- | This defines what a 'TargetSelector' means for the @bench@ command.
12931295
-- It selects the 'AvailableTarget's that the 'TargetSelector' refers to,

cabal-install/src/Distribution/Client/DistDirLayout.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import Distribution.Simple.Compiler
4646
, PackageDB (..)
4747
, PackageDBStack
4848
)
49+
import Distribution.Simple.Configure (interpretPackageDbFlags)
4950
import Distribution.System
5051
import Distribution.Types.ComponentName
5152
import Distribution.Types.LibraryName
@@ -121,7 +122,7 @@ data StoreDirLayout = StoreDirLayout
121122
, storePackageDirectory :: Compiler -> UnitId -> FilePath
122123
, storePackageDBPath :: Compiler -> FilePath
123124
, storePackageDB :: Compiler -> PackageDB
124-
, storePackageDBStack :: Compiler -> PackageDBStack
125+
, storePackageDBStack :: Compiler -> [Maybe PackageDB] -> PackageDBStack
125126
, storeIncomingDirectory :: Compiler -> FilePath
126127
, storeIncomingLock :: Compiler -> UnitId -> FilePath
127128
}
@@ -286,9 +287,10 @@ defaultStoreDirLayout storeRoot =
286287
storePackageDB compiler =
287288
SpecificPackageDB (storePackageDBPath compiler)
288289

289-
storePackageDBStack :: Compiler -> PackageDBStack
290-
storePackageDBStack compiler =
291-
[GlobalPackageDB, storePackageDB compiler]
290+
storePackageDBStack :: Compiler -> [Maybe PackageDB] -> PackageDBStack
291+
storePackageDBStack compiler extraPackageDB =
292+
(interpretPackageDbFlags False extraPackageDB)
293+
++ [storePackageDB compiler]
292294

293295
storeIncomingDirectory :: Compiler -> FilePath
294296
storeIncomingDirectory compiler =

cabal-install/src/Distribution/Client/ProjectBuilding/UnpackedPackage.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,12 @@ buildAndInstallUnpackedPackage
683683
| otherwise = do
684684
assert
685685
( elabRegisterPackageDBStack pkg
686-
== storePackageDBStack compiler
686+
== storePackageDBStack compiler (elabPackageDbs pkg)
687687
)
688688
(return ())
689689
_ <-
690690
runRegister
691-
(storePackageDBStack compiler)
691+
(elabRegisterPackageDBStack pkg)
692692
Cabal.defaultRegisterOptions
693693
{ Cabal.registerMultiInstance = True
694694
, Cabal.registerSuppressFilesCheck = True

cabal-install/src/Distribution/Client/ProjectPlanning.hs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,7 @@ rebuildInstallPlan
709709
where
710710
corePackageDbs :: [PackageDB]
711711
corePackageDbs =
712-
applyPackageDbFlags
713-
[GlobalPackageDB]
714-
(projectConfigPackageDBs projectConfigShared)
712+
Cabal.interpretPackageDbFlags False (projectConfigPackageDBs projectConfigShared)
715713

716714
withRepoCtx :: (RepoContext -> IO a) -> IO a
717715
withRepoCtx =
@@ -1177,13 +1175,6 @@ getPackageSourceHashes verbosity withRepoCtx solverPlan = do
11771175
hashesFromRepoMetadata
11781176
<> hashesFromTarballFiles
11791177

1180-
-- | Append the given package databases to an existing PackageDBStack.
1181-
-- A @Nothing@ entry will clear everything before it.
1182-
applyPackageDbFlags :: PackageDBStack -> [Maybe PackageDB] -> PackageDBStack
1183-
applyPackageDbFlags dbs' [] = dbs'
1184-
applyPackageDbFlags _ (Nothing : dbs) = applyPackageDbFlags [] dbs
1185-
applyPackageDbFlags dbs' (Just db : dbs) = applyPackageDbFlags (dbs' ++ [db]) dbs
1186-
11871178
-- ------------------------------------------------------------
11881179

11891180
-- * Installation planning
@@ -2321,10 +2312,7 @@ elaborateInstallPlan
23212312
corePackageDbs
23222313
++ [distPackageDB (compilerId compiler)]
23232314

2324-
corePackageDbs =
2325-
applyPackageDbFlags
2326-
(storePackageDBStack compiler)
2327-
(projectConfigPackageDBs sharedPackageConfig)
2315+
corePackageDbs = storePackageDBStack compiler (projectConfigPackageDBs sharedPackageConfig)
23282316

23292317
-- For this local build policy, every package that lives in a local source
23302318
-- dir (as opposed to a tarball), or depends on such a package, will be
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Test.Cabal.Prelude
2+
main = cabalTest $ do
3+
recordMode DoNotRecord $ withRepo "repo" $ withPackageDb $ do
4+
withDirectory "p1" $
5+
setup_install []
6+
7+
env <- getTestEnv
8+
let pkgDbPath = testPackageDbDir env
9+
10+
withDirectory "p2" $ do
11+
void $ cabal' "v2-build" ["--package-db=" ++ pkgDbPath]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for p1
2+
3+
## 0.1.0.0 -- YYYY-mm-dd
4+
5+
* First version. Released on an unsuspecting world.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cabal-version: 3.0
2+
name: p1
3+
version: 0.1.0.0
4+
license: NONE
5+
author: matthewtpickering@gmail.com
6+
maintainer: Matthew Pickering
7+
build-type: Simple
8+
extra-doc-files: CHANGELOG.md
9+
10+
common warnings
11+
ghc-options: -Wall
12+
13+
library
14+
import: warnings
15+
exposed-modules: MyLib
16+
build-depends: base
17+
hs-source-dirs: src
18+
default-language: Haskell2010
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MyLib (someFunc) where
2+
3+
someFunc :: IO ()
4+
someFunc = putStrLn "someFunc"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for p2
2+
3+
## 0.1.0.0 -- YYYY-mm-dd
4+
5+
* First version. Released on an unsuspecting world.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .

0 commit comments

Comments
 (0)