Skip to content

Commit 9e89bb3

Browse files
authored
Merge pull request haskell#10828 from haskell/wip/t10759
Cabal: Always pass -package-env=- to supported GHC versions
2 parents e38a514 + a9c8304 commit 9e89bb3

File tree

9 files changed

+110
-46
lines changed

9 files changed

+110
-46
lines changed

Cabal/src/Distribution/Simple/Program/Builtin.hs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,32 +104,44 @@ ghcProgram :: Program
104104
ghcProgram =
105105
(simpleProgram "ghc")
106106
{ programFindVersion = findProgramVersion "--numeric-version" id
107-
, -- Workaround for https://gitlab.haskell.org/ghc/ghc/-/issues/8825
108-
-- (spurious warning on non-english locales)
109-
programPostConf = \_verbosity ghcProg ->
110-
do
111-
let ghcProg' =
112-
ghcProg
113-
{ programOverrideEnv =
114-
("LANGUAGE", Just "en")
115-
: programOverrideEnv ghcProg
116-
}
117-
-- Only the 7.8 branch seems to be affected. Fixed in 7.8.4.
118-
affectedVersionRange =
119-
intersectVersionRanges
120-
(laterVersion $ mkVersion [7, 8, 0])
121-
(earlierVersion $ mkVersion [7, 8, 4])
122-
return $
123-
maybe
124-
ghcProg
125-
( \v ->
126-
if withinRange v affectedVersionRange
127-
then ghcProg'
128-
else ghcProg
129-
)
130-
(programVersion ghcProg)
107+
, programPostConf = ghcPostConf
131108
, programNormaliseArgs = normaliseGhcArgs
132109
}
110+
where
111+
ghcPostConf _verbosity ghcProg = do
112+
let setLanguageEnv prog =
113+
prog
114+
{ programOverrideEnv =
115+
("LANGUAGE", Just "en")
116+
: programOverrideEnv ghcProg
117+
}
118+
119+
ignorePackageEnv prog = prog{programDefaultArgs = "-package-env=-" : programDefaultArgs prog}
120+
121+
-- Only the 7.8 branch seems to be affected. Fixed in 7.8.4.
122+
affectedVersionRange =
123+
intersectVersionRanges
124+
(laterVersion $ mkVersion [7, 8, 0])
125+
(earlierVersion $ mkVersion [7, 8, 4])
126+
127+
canIgnorePackageEnv = orLaterVersion $ mkVersion [8, 4, 4]
128+
129+
applyWhen cond f prog = if cond then f prog else prog
130+
131+
return $
132+
maybe
133+
ghcProg
134+
( \v ->
135+
-- By default, ignore GHC_ENVIRONMENT variable of any package environmnet
136+
-- files. See #10759
137+
applyWhen (withinRange v canIgnorePackageEnv) ignorePackageEnv
138+
-- Workaround for https://gitlab.haskell.org/ghc/ghc/-/issues/8825
139+
-- (spurious warning on non-english locales)
140+
$
141+
applyWhen (withinRange v affectedVersionRange) setLanguageEnv $
142+
ghcProg
143+
)
144+
(programVersion ghcProg)
133145

134146
runghcProgram :: Program
135147
runghcProgram =

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import Distribution.Client.ProjectOrchestration
4646
import Distribution.Client.ProjectPlanOutput
4747
( PostBuildProjectStatus
4848
, argsEquivalentOfGhcEnvironmentFile
49-
, createPackageEnvironment
49+
, createPackageEnvironmentAndArgs
5050
, updatePostBuildProjectStatus
5151
)
5252
import Distribution.Client.ProjectPlanning
@@ -191,7 +191,7 @@ execAction flags@NixStyleFlags{..} extraArgs globalFlags = do
191191
[] -> dieWithException verbosity SpecifyAnExecutable
192192
exe : args -> do
193193
(program, _) <- requireProgram verbosity (simpleProgram exe) programDb
194-
let argOverrides =
194+
let environmentPackageArgs =
195195
argsEquivalentOfGhcEnvironmentFile
196196
compiler
197197
(distDirLayout baseCtx)
@@ -201,21 +201,19 @@ execAction flags@NixStyleFlags{..} extraArgs globalFlags = do
201201
matchCompilerPath
202202
(elaboratedShared buildCtx)
203203
program
204-
argOverrides' =
205-
if envFilesSupported
206-
|| not programIsConfiguredCompiler
207-
then []
208-
else argOverrides
209204

210205
( if envFilesSupported
211206
then withTempEnvFile verbosity baseCtx buildCtx buildStatus
212-
else \f -> f []
207+
else \f ->
208+
if programIsConfiguredCompiler
209+
then f environmentPackageArgs []
210+
else f [] []
213211
)
214-
$ \envOverrides -> do
212+
$ \argOverrides envOverrides -> do
215213
let program' =
216214
withOverrides
217215
envOverrides
218-
argOverrides'
216+
argOverrides
219217
program
220218
invocation = programInvocation program' args
221219
dryRun =
@@ -253,7 +251,7 @@ withTempEnvFile
253251
-> ProjectBaseContext
254252
-> ProjectBuildContext
255253
-> PostBuildProjectStatus
256-
-> ([(String, Maybe String)] -> IO a)
254+
-> ([String] -> [(String, Maybe String)] -> IO a)
257255
-> IO a
258256
withTempEnvFile verbosity baseCtx buildCtx buildStatus action = do
259257
let tmpDirTemplate = distTempDirectory (distDirLayout baseCtx)
@@ -263,14 +261,14 @@ withTempEnvFile verbosity baseCtx buildCtx buildStatus action = do
263261
tmpDirTemplate
264262
"environment."
265263
( \tmpDir -> do
266-
envOverrides <-
267-
createPackageEnvironment
264+
(argOverrides, envOverrides) <-
265+
createPackageEnvironmentAndArgs
268266
verbosity
269267
tmpDir
270268
(elaboratedPlanToExecute buildCtx)
271269
(elaboratedShared buildCtx)
272270
buildStatus
273-
action envOverrides
271+
action argOverrides envOverrides
274272
)
275273

276274
-- | Get paths to all dependency executables to be included in PATH.

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Distribution.Client.ProjectPlanOutput
1212
-- | Several outputs rely on having a general overview of
1313
, PostBuildProjectStatus (..)
1414
, updatePostBuildProjectStatus
15-
, createPackageEnvironment
15+
, createPackageEnvironmentAndArgs
1616
, writePlanGhcEnvironment
1717
, argsEquivalentOfGhcEnvironmentFile
1818
) where
@@ -777,7 +777,7 @@ writePackagesUpToDateCacheFile DistDirLayout{distProjectCacheFile} upToDate =
777777
writeFileAtomic (distProjectCacheFile "up-to-date") $
778778
Binary.encode upToDate
779779

780-
-- | Prepare a package environment that includes all the library dependencies
780+
-- | Prepare a package environment and args that includes all the library dependencies
781781
-- for a plan.
782782
--
783783
-- When running cabal new-exec, we want to set things up so that the compiler
@@ -786,14 +786,17 @@ writePackagesUpToDateCacheFile DistDirLayout{distProjectCacheFile} upToDate =
786786
-- temporarily, in case the compiler wants to learn this information via the
787787
-- filesystem, and returns any environment variable overrides the compiler
788788
-- needs.
789-
createPackageEnvironment
789+
--
790+
-- The function returns both the arguments you need to pass to the compiler and
791+
-- the environment variables you need to set.
792+
createPackageEnvironmentAndArgs
790793
:: Verbosity
791794
-> FilePath
792795
-> ElaboratedInstallPlan
793796
-> ElaboratedSharedConfig
794797
-> PostBuildProjectStatus
795-
-> IO [(String, Maybe String)]
796-
createPackageEnvironment
798+
-> IO ([String], [(String, Maybe String)])
799+
createPackageEnvironmentAndArgs
797800
verbosity
798801
path
799802
elaboratedPlan
@@ -808,14 +811,14 @@ createPackageEnvironment
808811
elaboratedShared
809812
buildStatus
810813
case envFileM of
811-
Just envFile -> return [("GHC_ENVIRONMENT", Just envFile)]
814+
Just envFile -> return (["-package-env=" ++ envFile], [("GHC_ENVIRONMENT", Just envFile)])
812815
Nothing -> do
813816
warn verbosity "the configured version of GHC does not support reading package lists from the environment; commands that need the current project's package database are likely to fail"
814-
return []
817+
return ([], [])
815818
| otherwise =
816819
do
817820
warn verbosity "package environment configuration is not supported for the currently configured compiler; commands that need the current project's package database are likely to fail"
818-
return []
821+
return ([], [])
819822

820823
-- Writing .ghc.environment files
821824
--
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for EnvironmentFile
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.14
2+
name: EnvironmentFile
3+
version: 0.1.0.0
4+
license: NONE
5+
author: Matthew Pickering
6+
maintainer: [email protected]
7+
build-type: Simple
8+
extra-doc-files: CHANGELOG.md
9+
10+
common warnings
11+
ghc-options: -Wall
12+
13+
executable EnvironmentFile
14+
import: warnings
15+
main-is: Main.hs
16+
build-depends: base < 5,
17+
hs-source-dirs: app
18+
default-language: Haskell2010
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Main where
2+
3+
main :: IO ()
4+
main = putStrLn "Hello, Haskell!"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Test.Cabal.Prelude
2+
import System.FilePath
3+
main = cabalTest $ recordMode DoNotRecord $ do
4+
env <- getTestEnv
5+
let cur_dir = testCurrentDir env
6+
-- Set GHC_ENVIRONMENT to a file which contains garbage
7+
withEnv [("GHC_ENVIRONMENT", Just (cur_dir </> "ghc.environment"))] $ do
8+
cabal "build" ["all"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this-file-(ghc.environment)-contains-garbage-it-should-not-be-read

changelog.d/pr-10828.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
synopsis: "Isolate Cabal from the GHC_ENVIRONMENT variable"
3+
packages: [Cabal]
4+
prs: 10828
5+
6+
issues: 10759
7+
---
8+
9+
For GHC 8.4.4 and later, Cabal now passes the `-package-env=-` flag to GHC.
10+
This prevents the `GHC_ENVIRONMENT` variable or any package environment files
11+
from affecting Cabal builds.
12+
13+
This change eliminates unexpected build behavior that could occur when users
14+
had GHC environment files configured in their system that Cabal wasn't aware
15+
of.

0 commit comments

Comments
 (0)