Skip to content

Commit 9025af5

Browse files
authored
Merge pull request #9683 from mpickering/wip/package-db-fix
cabal-install: Clarify the semantics of package-db flag
2 parents fb3f4d4 + d626ef8 commit 9025af5

File tree

18 files changed

+147
-35
lines changed

18 files changed

+147
-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/ProjectOrchestration.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ establishProjectBaseContextWithRoot verbosity cliConfig projectRoot currentComma
295295
sequenceA $
296296
makeAbsolute
297297
<$> Setup.flagToMaybe projectConfigStoreDir
298+
298299
cabalDirLayout <- mkCabalDirLayout mstoreDir mlogsDir
299300

300301
let buildSettings =

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+
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: .
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: p2
3+
version: 0.1.0.0
4+
license: NONE
5+
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, p1, p3
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 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: p3
3+
version: 0.1.0.0
4+
license: NONE
5+
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"

changelog.d/issue-9678

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
synopsis: Clarify the semantics of the -package-db flag
2+
packages: cabal-install
3+
prs:
4+
issues: #9678
5+
6+
description: {
7+
8+
The `--package-db` flag now only applies to the default
9+
immutable initial package stack rather than also applying to the store
10+
package database.
11+
12+
This fixes an assertion failure which was triggered when using -package-db and also
13+
clarifies how it should interact with `--store-dir` and `--dist-dir` flags.
14+
15+
}
16+

doc/cabal-project-description-file.rst

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ package, and thus apply globally:
372372
:synopsis: PackageDB stack manipulation
373373
:since: 3.7
374374

375+
By modifying ``package-dbs`` you can modify the default package environment
376+
which ``cabal`` will see. The package databases you add using ``package-dbs``
377+
will not be written into and only used as immutable package stores to initialise
378+
the environment with additional packages that ``cabal`` can choose to use.
379+
375380
There are three package databases involved with most builds:
376381

377382
global
@@ -381,37 +386,42 @@ package, and thus apply globally:
381386
in-place
382387
Project-specific build directory
383388

384-
By default, the package stack you will have with v2 commands is:
389+
By default, the initial package stack prefix you will have with v2 commands is:
385390

386391
::
387392

388-
-- [global, store]
393+
-- prefix = [global]
389394

390-
So all remote packages required by your project will be
391-
registered in the store package db (because it is last).
395+
So the initial set of packages which is used by cabal is just the packages
396+
installed in the global package database which comes with ``ghc``.
392397

393-
When cabal starts building your local projects, it appends the in-place db
394-
to the end:
398+
When cabal builds a package it will start populating the ``store`` package database,
399+
whose packages will then be subsequently be available to be used in future runs.
395400

396401
::
397402

398-
-- [global, store, in-place]
403+
-- prefix ++ [store]
404+
405+
When cabal builds your local projects, packages are registered into the local
406+
in-place package database.
407+
408+
::
399409

400-
So your local packages get put in ``dist-newstyle`` instead of the store.
410+
-- prefix ++ [store, in-place]
401411

402-
This flag manipulates the default prefix: ``[global, store]`` and accepts
412+
This flag manipulates the default prefix: ``[global]`` and accepts
403413
paths, the special value ``global`` referring to the global package db, and
404414
``clear`` which removes all prior entries. For example,
405415

406416
::
407417

408-
-- [global, store, foo]
418+
-- prefix = [global, foo]
409419
package-dbs: foo
410420

411-
-- [foo]
421+
-- prefix = [foo]
412422
package-dbs: clear, foo
413423

414-
-- [bar, baz]
424+
-- prefix = [bar, baz]
415425
package-dbs: clear, foo, clear, bar, baz
416426

417427
The command line variant of this flag is ``--package-db=DB`` which can be

0 commit comments

Comments
 (0)