Skip to content

Commit

Permalink
add base to cabal install --lib default env file (#8903)
Browse files Browse the repository at this point in the history
* 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 <gershom@arista.com>
(cherry picked from commit 10de4e5)
  • Loading branch information
gbaz authored and mergify[bot] committed May 8, 2023
1 parent 4bfd6a0 commit 8e71243
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
31 changes: 25 additions & 6 deletions cabal-install/src/Distribution/Client/CmdInstall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import Distribution.Simple.Configure
( configCompilerEx )
import Distribution.Simple.Compiler
( Compiler(..), CompilerId(..), CompilerFlavor(..)
, PackageDBStack )
, PackageDBStack, PackageDB(..) )
import Distribution.Simple.GHC
( ghcPlatformAndVersionString, getGhcAppDir
, GhcImplInfo(..), getImplInfo
Expand All @@ -123,11 +123,13 @@ import Distribution.Verbosity
import Distribution.Simple.Utils
( wrapText, die', notice, warn
, withTempDirectory, createDirectoryIfMissingVerbose
, ordNub )
, ordNub, safeHead )
import Distribution.Utils.Generic
( writeFileAtomic )

import qualified Data.ByteString.Lazy.Char8 as BS
import Data.Ord
( Down(..) )
import qualified Data.Map as Map
import qualified Data.Set as S
import qualified Data.List.NonEmpty as NE
Expand Down Expand Up @@ -424,7 +426,7 @@ installAction flags@NixStyleFlags { extraFlags = clientInstallFlags', .. } targe
unless dryRun $
if installLibs
then installLibraries verbosity
buildCtx compiler packageDbs envFile nonGlobalEnvEntries'
buildCtx installedIndex compiler packageDbs envFile nonGlobalEnvEntries'
else installExes verbosity
baseCtx buildCtx platform compiler configFlags clientInstallFlags
where
Expand Down Expand Up @@ -687,20 +689,31 @@ installExes verbosity baseCtx buildCtx platform compiler
installLibraries
:: Verbosity
-> ProjectBuildContext
-> PI.PackageIndex InstalledPackageInfo
-> Compiler
-> PackageDBStack
-> FilePath -- ^ Environment file
-> [GhcEnvironmentFileEntry]
-> IO ()
installLibraries verbosity buildCtx compiler
packageDbs envFile envEntries = do
installLibraries verbosity buildCtx installedIndex compiler
packageDbs' envFile envEntries = do
if supportsPkgEnvFiles $ getImplInfo compiler
then do
let validDb (SpecificPackageDB fp) = doesPathExist fp
validDb _ = pure True
-- if a user "installs" a global package and no existing cabal db exists, none will be created.
-- this ensures we don't add the "phantom" path to the file.
packageDbs <- filterM validDb packageDbs'
let
getLatest = (=<<) (maybeToList . safeHead . snd) . take 1 . sortBy (comparing (Down . fst))
. PI.lookupPackageName installedIndex
globalLatest = concat (getLatest <$> globalPackages)
globalEntries = GhcEnvFilePackageId . installedUnitId <$> globalLatest
baseEntries =
GhcEnvFileClearPackageDbStack : fmap GhcEnvFilePackageDb packageDbs
pkgEntries = ordNub $
envEntries
globalEntries
++ envEntries
++ entriesForLibraryComponents (targetsMap buildCtx)
contents' = renderGhcEnvironmentFile (baseEntries ++ pkgEntries)
createDirectoryIfMissing True (takeDirectory envFile)
Expand All @@ -711,6 +724,12 @@ installLibraries verbosity buildCtx compiler
++ "so only executables will be available. (Library installation is "
++ "supported on GHC 8.0+ only)"

-- See ticket #8894. This is safe to include any nonreinstallable boot pkg,
-- but the particular package users will always expect to be in scope without specific installation
-- is base, so that they can access prelude, regardles of if they specifically asked for it.
globalPackages :: [PackageName]
globalPackages = mkPackageName <$> [ "base" ]

warnIfNoExes :: Verbosity -> ProjectBuildContext -> IO ()
warnIfNoExes verbosity buildCtx =
when noExes $
Expand Down
9 changes: 9 additions & 0 deletions changelog.d/pr-8903
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
synopsis: add base to cabal install --lib default env file
packages: cabal-install
prs: #8903

description: {

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.

}

0 comments on commit 8e71243

Please sign in to comment.