@@ -106,7 +106,7 @@ import Distribution.Simple.Configure
106
106
( configCompilerEx )
107
107
import Distribution.Simple.Compiler
108
108
( Compiler (.. ), CompilerId (.. ), CompilerFlavor (.. )
109
- , PackageDBStack )
109
+ , PackageDBStack , PackageDB ( .. ) )
110
110
import Distribution.Simple.GHC
111
111
( ghcPlatformAndVersionString , getGhcAppDir
112
112
, GhcImplInfo (.. ), getImplInfo
@@ -123,11 +123,13 @@ import Distribution.Verbosity
123
123
import Distribution.Simple.Utils
124
124
( wrapText , die' , notice , warn
125
125
, withTempDirectory , createDirectoryIfMissingVerbose
126
- , ordNub )
126
+ , ordNub , safeHead )
127
127
import Distribution.Utils.Generic
128
128
( writeFileAtomic )
129
129
130
130
import qualified Data.ByteString.Lazy.Char8 as BS
131
+ import Data.Ord
132
+ ( Down (.. ) )
131
133
import qualified Data.Map as Map
132
134
import qualified Data.Set as S
133
135
import qualified Data.List.NonEmpty as NE
@@ -424,7 +426,7 @@ installAction flags@NixStyleFlags { extraFlags = clientInstallFlags', .. } targe
424
426
unless dryRun $
425
427
if installLibs
426
428
then installLibraries verbosity
427
- buildCtx compiler packageDbs envFile nonGlobalEnvEntries'
429
+ buildCtx installedIndex compiler packageDbs envFile nonGlobalEnvEntries'
428
430
else installExes verbosity
429
431
baseCtx buildCtx platform compiler configFlags clientInstallFlags
430
432
where
@@ -687,20 +689,31 @@ installExes verbosity baseCtx buildCtx platform compiler
687
689
installLibraries
688
690
:: Verbosity
689
691
-> ProjectBuildContext
692
+ -> PI. PackageIndex InstalledPackageInfo
690
693
-> Compiler
691
694
-> PackageDBStack
692
695
-> FilePath -- ^ Environment file
693
696
-> [GhcEnvironmentFileEntry ]
694
697
-> IO ()
695
- installLibraries verbosity buildCtx compiler
696
- packageDbs envFile envEntries = do
698
+ installLibraries verbosity buildCtx installedIndex compiler
699
+ packageDbs' envFile envEntries = do
697
700
if supportsPkgEnvFiles $ getImplInfo compiler
698
701
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'
699
707
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
700
712
baseEntries =
701
713
GhcEnvFileClearPackageDbStack : fmap GhcEnvFilePackageDb packageDbs
702
714
pkgEntries = ordNub $
703
- envEntries
715
+ globalEntries
716
+ ++ envEntries
704
717
++ entriesForLibraryComponents (targetsMap buildCtx)
705
718
contents' = renderGhcEnvironmentFile (baseEntries ++ pkgEntries)
706
719
createDirectoryIfMissing True (takeDirectory envFile)
@@ -711,6 +724,12 @@ installLibraries verbosity buildCtx compiler
711
724
++ " so only executables will be available. (Library installation is "
712
725
++ " supported on GHC 8.0+ only)"
713
726
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
+
714
733
warnIfNoExes :: Verbosity -> ProjectBuildContext -> IO ()
715
734
warnIfNoExes verbosity buildCtx =
716
735
when noExes $
0 commit comments