Skip to content

Commit 8e71243

Browse files
gbazmergify[bot]
authored andcommitted
add base to cabal install --lib default env file (#8903)
* add base to cabal install --lib default env file * check packagedb stack exists * fix validdb filtering -- move later, test for a dir * Update CmdInstall.hs * changelog * fix comment --------- Co-authored-by: Gershom Bazerman <[email protected]> (cherry picked from commit 10de4e5)
1 parent 4bfd6a0 commit 8e71243

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ import Distribution.Simple.Configure
106106
( configCompilerEx )
107107
import Distribution.Simple.Compiler
108108
( Compiler(..), CompilerId(..), CompilerFlavor(..)
109-
, PackageDBStack )
109+
, PackageDBStack, PackageDB(..) )
110110
import Distribution.Simple.GHC
111111
( ghcPlatformAndVersionString, getGhcAppDir
112112
, GhcImplInfo(..), getImplInfo
@@ -123,11 +123,13 @@ import Distribution.Verbosity
123123
import Distribution.Simple.Utils
124124
( wrapText, die', notice, warn
125125
, withTempDirectory, createDirectoryIfMissingVerbose
126-
, ordNub )
126+
, ordNub, safeHead )
127127
import Distribution.Utils.Generic
128128
( writeFileAtomic )
129129

130130
import qualified Data.ByteString.Lazy.Char8 as BS
131+
import Data.Ord
132+
( Down(..) )
131133
import qualified Data.Map as Map
132134
import qualified Data.Set as S
133135
import qualified Data.List.NonEmpty as NE
@@ -424,7 +426,7 @@ installAction flags@NixStyleFlags { extraFlags = clientInstallFlags', .. } targe
424426
unless dryRun $
425427
if installLibs
426428
then installLibraries verbosity
427-
buildCtx compiler packageDbs envFile nonGlobalEnvEntries'
429+
buildCtx installedIndex compiler packageDbs envFile nonGlobalEnvEntries'
428430
else installExes verbosity
429431
baseCtx buildCtx platform compiler configFlags clientInstallFlags
430432
where
@@ -687,20 +689,31 @@ installExes verbosity baseCtx buildCtx platform compiler
687689
installLibraries
688690
:: Verbosity
689691
-> ProjectBuildContext
692+
-> PI.PackageIndex InstalledPackageInfo
690693
-> Compiler
691694
-> PackageDBStack
692695
-> FilePath -- ^ Environment file
693696
-> [GhcEnvironmentFileEntry]
694697
-> IO ()
695-
installLibraries verbosity buildCtx compiler
696-
packageDbs envFile envEntries = do
698+
installLibraries verbosity buildCtx installedIndex compiler
699+
packageDbs' envFile envEntries = do
697700
if supportsPkgEnvFiles $ getImplInfo compiler
698701
then do
702+
let validDb (SpecificPackageDB fp) = doesPathExist fp
703+
validDb _ = pure True
704+
-- if a user "installs" a global package and no existing cabal db exists, none will be created.
705+
-- this ensures we don't add the "phantom" path to the file.
706+
packageDbs <- filterM validDb packageDbs'
699707
let
708+
getLatest = (=<<) (maybeToList . safeHead . snd) . take 1 . sortBy (comparing (Down . fst))
709+
. PI.lookupPackageName installedIndex
710+
globalLatest = concat (getLatest <$> globalPackages)
711+
globalEntries = GhcEnvFilePackageId . installedUnitId <$> globalLatest
700712
baseEntries =
701713
GhcEnvFileClearPackageDbStack : fmap GhcEnvFilePackageDb packageDbs
702714
pkgEntries = ordNub $
703-
envEntries
715+
globalEntries
716+
++ envEntries
704717
++ entriesForLibraryComponents (targetsMap buildCtx)
705718
contents' = renderGhcEnvironmentFile (baseEntries ++ pkgEntries)
706719
createDirectoryIfMissing True (takeDirectory envFile)
@@ -711,6 +724,12 @@ installLibraries verbosity buildCtx compiler
711724
++ "so only executables will be available. (Library installation is "
712725
++ "supported on GHC 8.0+ only)"
713726

727+
-- See ticket #8894. This is safe to include any nonreinstallable boot pkg,
728+
-- but the particular package users will always expect to be in scope without specific installation
729+
-- is base, so that they can access prelude, regardles of if they specifically asked for it.
730+
globalPackages :: [PackageName]
731+
globalPackages = mkPackageName <$> [ "base" ]
732+
714733
warnIfNoExes :: Verbosity -> ProjectBuildContext -> IO ()
715734
warnIfNoExes verbosity buildCtx =
716735
when noExes $

changelog.d/pr-8903

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
synopsis: add base to cabal install --lib default env file
2+
packages: cabal-install
3+
prs: #8903
4+
5+
description: {
6+
7+
This adds base by default to the env file created by `cabal install --lib`. Further it ensures that packagedbs have been created before adding them to the env file.
8+
9+
}

0 commit comments

Comments
 (0)