@@ -441,8 +441,8 @@ installAction flags@NixStyleFlags{extraFlags, configFlags, installFlags, project
441
441
let
442
442
GhcImplInfo {supportsPkgEnvFiles} = getImplInfo compiler
443
443
444
- envFile <- getEnvFile clientInstallFlags platform compilerVersion
445
- existingEnvEntries <-
444
+ (usedPackageEnvFlag, envFile) <- getEnvFile clientInstallFlags platform compilerVersion
445
+ (usedExistingPkgEnvFile, existingEnvEntries) <-
446
446
getExistingEnvEntries verbosity compilerFlavor supportsPkgEnvFiles envFile
447
447
packageDbs <- getPackageDbStack compiler projectConfigStoreDir projectConfigLogsDir projectConfigPackageDBs
448
448
installedIndex <- getInstalledPackages verbosity compiler packageDbs progDb
@@ -533,6 +533,7 @@ installAction flags@NixStyleFlags{extraFlags, configFlags, installFlags, project
533
533
packageDbs
534
534
envFile
535
535
nonGlobalEnvEntries'
536
+ (not usedExistingPkgEnvFile && not usedPackageEnvFlag)
536
537
else -- Install any built exe by symlinking or copying it we don't use
537
538
-- BuildOutcomes because we also need the component names
538
539
traverseInstall (installCheckUnitExes InstallCheckInstall ) installCfg
@@ -959,6 +960,9 @@ installLibraries
959
960
-> FilePath
960
961
-- ^ Environment file
961
962
-> [GhcEnvironmentFileEntry ]
963
+ -> Bool
964
+ -- ^ Whether we need to show a warning (i.e. we created a new environment
965
+ -- file, and the user did not use --package-env)
962
966
-> IO ()
963
967
installLibraries
964
968
verbosity
@@ -967,7 +971,8 @@ installLibraries
967
971
compiler
968
972
packageDbs'
969
973
envFile
970
- envEntries = do
974
+ envEntries
975
+ showWarning = do
971
976
if supportsPkgEnvFiles $ getImplInfo compiler
972
977
then do
973
978
let validDb (SpecificPackageDB fp) = doesPathExist fp
@@ -993,6 +998,27 @@ installLibraries
993
998
contents' = renderGhcEnvironmentFile (baseEntries ++ pkgEntries)
994
999
createDirectoryIfMissing True (takeDirectory envFile)
995
1000
writeFileAtomic envFile (BS. pack contents')
1001
+ when showWarning $
1002
+ warn verbosity $
1003
+ " The libraries were installed by creating a global GHC environment file at:\n "
1004
+ ++ envFile
1005
+ ++ " \n "
1006
+ ++ " \n "
1007
+ ++ " The presence of such an environment file is likely to confuse or break other "
1008
+ ++ " tools because it changes GHC's behaviour: it changes the default package set in "
1009
+ ++ " ghc and ghci from its normal value (which is \" all boot libraries\" ). GHC "
1010
+ ++ " environment files are little-used and often not tested for.\n "
1011
+ ++ " \n "
1012
+ ++ " Furthermore, management of these environment files is still more difficult than "
1013
+ ++ " it could be; see e.g. https://github.com/haskell/cabal/issues/6481 .\n "
1014
+ ++ " \n "
1015
+ ++ " Double-check that creating a global GHC environment file is really what you "
1016
+ ++ " wanted! You can limit the effects of the environment file by creating it in a "
1017
+ ++ " specific directory using the --package-env flag. For example, use:\n "
1018
+ ++ " \n "
1019
+ ++ " cabal install --lib <packages...> --package-env .\n "
1020
+ ++ " \n "
1021
+ ++ " to create the file in the current directory."
996
1022
else
997
1023
warn verbosity $
998
1024
" The current compiler doesn't support safely installing libraries, "
@@ -1223,46 +1249,50 @@ entriesForLibraryComponents = Map.foldrWithKey' (\k v -> mappend (go k v)) []
1223
1249
| any hasLib targets = [GhcEnvFilePackageId unitId]
1224
1250
| otherwise = []
1225
1251
1226
- -- | Gets the file path to the request environment file.
1227
- getEnvFile :: ClientInstallFlags -> Platform -> Version -> IO FilePath
1252
+ -- | Gets the file path to the request environment file. The @Bool@ is @True@
1253
+ -- if we got an explicit instruction using @--package-env@, @False@ if we used
1254
+ -- the default.
1255
+ getEnvFile :: ClientInstallFlags -> Platform -> Version -> IO (Bool , FilePath )
1228
1256
getEnvFile clientInstallFlags platform compilerVersion = do
1229
1257
appDir <- getGhcAppDir
1230
1258
case flagToMaybe (cinstEnvironmentPath clientInstallFlags) of
1231
1259
Just spec
1232
1260
-- Is spec a bare word without any "pathy" content, then it refers to
1233
1261
-- a named global environment.
1234
1262
| takeBaseName spec == spec ->
1235
- return (getGlobalEnv appDir platform compilerVersion spec)
1263
+ return (True , getGlobalEnv appDir platform compilerVersion spec)
1236
1264
| otherwise -> do
1237
1265
spec' <- makeAbsolute spec
1238
1266
isDir <- doesDirectoryExist spec'
1239
1267
if isDir
1240
1268
then -- If spec is a directory, then make an ambient environment inside
1241
1269
-- that directory.
1242
- return (getLocalEnv spec' platform compilerVersion)
1270
+ return (True , getLocalEnv spec' platform compilerVersion)
1243
1271
else -- Otherwise, treat it like a literal file path.
1244
- return spec'
1272
+ return ( True , spec')
1245
1273
Nothing ->
1246
- return (getGlobalEnv appDir platform compilerVersion " default" )
1274
+ return (False , getGlobalEnv appDir platform compilerVersion " default" )
1247
1275
1248
- -- | Returns the list of @GhcEnvFilePackageIj@ values already existing in the
1249
- -- environment being operated on.
1250
- getExistingEnvEntries :: Verbosity -> CompilerFlavor -> Bool -> FilePath -> IO [GhcEnvironmentFileEntry ]
1276
+ -- | Returns the list of @GhcEnvFilePackageId@ values already existing in the
1277
+ -- environment being operated on. The @Bool@ is @True@ if we took settings
1278
+ -- from an existing file, @False@ otherwise.
1279
+ getExistingEnvEntries :: Verbosity -> CompilerFlavor -> Bool -> FilePath -> IO (Bool , [GhcEnvironmentFileEntry ])
1251
1280
getExistingEnvEntries verbosity compilerFlavor supportsPkgEnvFiles envFile = do
1252
1281
envFileExists <- doesFileExist envFile
1253
- filterEnvEntries
1254
- <$> if (compilerFlavor == GHC || compilerFlavor == GHCJS )
1282
+ (usedExisting, allEntries) <-
1283
+ if (compilerFlavor == GHC || compilerFlavor == GHCJS )
1255
1284
&& supportsPkgEnvFiles
1256
1285
&& envFileExists
1257
- then catch (readGhcEnvironmentFile envFile) $ \ (_ :: ParseErrorExc ) ->
1286
+ then catch (( True ,) <$> readGhcEnvironmentFile envFile) $ \ (_ :: ParseErrorExc ) ->
1258
1287
warn
1259
1288
verbosity
1260
1289
( " The environment file "
1261
1290
++ envFile
1262
1291
++ " is unparsable. Libraries cannot be installed."
1263
1292
)
1264
- >> return []
1265
- else return []
1293
+ >> return (False , [] )
1294
+ else return (False , [] )
1295
+ return (usedExisting, filterEnvEntries allEntries)
1266
1296
where
1267
1297
-- Why? We know what the first part will be, we only care about the packages.
1268
1298
filterEnvEntries = filter $ \ case
0 commit comments