Skip to content

Commit 4156c93

Browse files
committed
Expand and unify --keep-temp-files
Currently, `cabal repl` has a `--keep-temp-files` option, and `cabal.project` has a `keep-temp-files` option but it only effects Haddock builds. This patch adds `--keep-temp-files` to `CommonSetupFlags`, making it available to all commands. The expanded `--keep-temp-files` flag is used for the `cabal repl` command and Haddock builds (retaining compatibility with the previous behavior) but is also used to determine when to keep response files.
1 parent e1b59b4 commit 4156c93

File tree

16 files changed

+298
-268
lines changed

16 files changed

+298
-268
lines changed

Cabal-tests/tests/UnitTests/Distribution/Utils/Structured.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ md5CheckGenericPackageDescription proxy = md5Check proxy
3434

3535
md5CheckLocalBuildInfo :: Proxy LocalBuildInfo -> Assertion
3636
md5CheckLocalBuildInfo proxy = md5Check proxy
37-
0x93b7e8ebb5b9f879fa5fe49b1708b43b
37+
0x8fa7b2c8cc611407bfdcb734ecb460a2

Cabal/src/Distribution/Simple/Haddock.hs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,9 @@ import Distribution.Simple.Program.GHC
6767
import qualified Distribution.Simple.Program.HcPkg as HcPkg
6868
import Distribution.Simple.Program.ResponseFile
6969
import Distribution.Simple.Register
70-
import Distribution.Simple.Setup.Common
71-
import Distribution.Simple.Setup.Haddock
72-
import Distribution.Simple.Setup.Hscolour
70+
import Distribution.Simple.Setup
7371
import Distribution.Simple.SetupHooks.Internal
7472
( BuildHooks (..)
75-
, BuildingWhat (..)
7673
, noBuildHooks
7774
)
7875
import qualified Distribution.Simple.SetupHooks.Internal as SetupHooks
@@ -265,6 +262,7 @@ haddock_setupHooks
265262
mbWorkDir = flagToMaybe $ haddockWorkingDir flags
266263
comp = compiler lbi
267264
platform = hostPlatform lbi
265+
config = configFlags lbi
268266

269267
quickJmpFlag = haddockQuickJump flags'
270268
flags = case haddockTarget of
@@ -282,9 +280,7 @@ haddock_setupHooks
282280
flag f = fromFlag $ f flags
283281

284282
tmpFileOpts =
285-
defaultTempFileOptions
286-
{ optKeepTempFiles = flag haddockKeepTempFiles
287-
}
283+
commonSetupTempFileOptions $ configCommonFlags config
288284
htmlTemplate =
289285
fmap toPathTemplate . flagToMaybe . haddockHtmlLocation $
290286
flags

Cabal/src/Distribution/Simple/Setup.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module Distribution.Simple.Setup
4141
, globalCommand
4242
, CommonSetupFlags (..)
4343
, defaultCommonSetupFlags
44+
, commonSetupTempFileOptions
4445
, ConfigFlags (..)
4546
, emptyConfigFlags
4647
, defaultConfigFlags

Cabal/src/Distribution/Simple/Setup/Common.hs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Distribution.Simple.Setup.Common
2323
( CommonSetupFlags (..)
2424
, defaultCommonSetupFlags
2525
, withCommonSetupOptions
26+
, commonSetupTempFileOptions
2627
, CopyDest (..)
2728
, configureCCompiler
2829
, configureLinker
@@ -85,6 +86,13 @@ data CommonSetupFlags = CommonSetupFlags
8586
--
8687
-- TODO: this one should not be here, it's just that the silly
8788
-- UserHooks stop us from passing extra info in other ways
89+
, setupKeepTempFiles :: Flag Bool
90+
-- ^ When this flag is set, temporary files will be kept after building.
91+
--
92+
-- Note: Keeping temporary files is important functionality for HLS, which
93+
-- runs @cabal repl@ with a fake GHC to get CLI arguments. It will need the
94+
-- temporary files (including multi unit repl response files) to stay, even
95+
-- after the @cabal repl@ command exits.
8896
}
8997
deriving (Eq, Show, Read, Generic)
9098

@@ -106,6 +114,15 @@ defaultCommonSetupFlags =
106114
, setupDistPref = NoFlag
107115
, setupCabalFilePath = NoFlag
108116
, setupTargets = []
117+
, setupKeepTempFiles = NoFlag
118+
}
119+
120+
-- | Get `TempFileOptions` that respect the `setupKeepTempFiles` flag.
121+
commonSetupTempFileOptions :: CommonSetupFlags -> TempFileOptions
122+
commonSetupTempFileOptions options =
123+
TempFileOptions
124+
{ optKeepTempFiles =
125+
fromFlagOrDefault False (setupKeepTempFiles options)
109126
}
110127

111128
commonSetupOptions :: ShowOrParseArgs -> [OptionField CommonSetupFlags]
@@ -124,6 +141,14 @@ commonSetupOptions showOrParseArgs =
124141
setupCabalFilePath
125142
(\v flags -> flags{setupCabalFilePath = v})
126143
(reqSymbolicPathArgFlag "PATH")
144+
, option
145+
""
146+
["keep-temp-files"]
147+
( "Keep temporary files."
148+
)
149+
setupKeepTempFiles
150+
(\keepTempFiles flags -> flags{setupKeepTempFiles = keepTempFiles})
151+
trueArg
127152
-- NB: no --working-dir flag, as that value is populated using the
128153
-- global flag (see Distribution.Simple.Setup.Global.globalCommand).
129154
]

0 commit comments

Comments
 (0)