Skip to content

Commit cb353ba

Browse files
authored
Merge pull request #10292 from MercuryTechnologies/rebeccat/keep-temp-files
Expand and unify `--keep-temp-files`
2 parents a39266d + 7a04395 commit cb353ba

File tree

16 files changed

+99
-81
lines changed

16 files changed

+99
-81
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: 6 additions & 8 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
@@ -553,9 +549,11 @@ createHaddockIndex
553549
-> IO ()
554550
createHaddockIndex verbosity programDb comp platform mbWorkDir flags = do
555551
let args = fromHaddockProjectFlags flags
552+
tmpFileOpts =
553+
commonSetupTempFileOptions $ haddockProjectCommonFlags $ flags
556554
(haddockProg, _version) <-
557555
getHaddockProg verbosity programDb comp args (Flag True)
558-
runHaddock verbosity mbWorkDir defaultTempFileOptions comp platform haddockProg False args
556+
runHaddock verbosity mbWorkDir tmpFileOpts comp platform haddockProg False args
559557

560558
-- ------------------------------------------------------------------------------
561559
-- Contributions to HaddockArgs (see also Doctest.hs for very similar code).

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
]

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

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ data HaddockFlags = HaddockFlags
111111
, haddockHscolourCss :: Flag FilePath
112112
, haddockContents :: Flag PathTemplate
113113
, haddockIndex :: Flag PathTemplate
114-
, haddockKeepTempFiles :: Flag Bool
115114
, haddockBaseUrl :: Flag String
116115
, haddockResourcesDir :: Flag String
117116
, haddockOutputDir :: Flag FilePath
@@ -166,7 +165,6 @@ defaultHaddockFlags =
166165
, haddockQuickJump = Flag False
167166
, haddockHscolourCss = NoFlag
168167
, haddockContents = NoFlag
169-
, haddockKeepTempFiles = Flag False
170168
, haddockIndex = NoFlag
171169
, haddockBaseUrl = NoFlag
172170
, haddockResourcesDir = NoFlag
@@ -219,13 +217,6 @@ haddockOptions showOrParseArgs =
219217
(\c f -> f{haddockCommonFlags = c})
220218
showOrParseArgs
221219
[ option
222-
""
223-
["keep-temp-files"]
224-
"Keep temporary files"
225-
haddockKeepTempFiles
226-
(\b flags -> flags{haddockKeepTempFiles = b})
227-
trueArg
228-
, option
229220
""
230221
["hoogle"]
231222
"Generate a hoogle database"
@@ -447,9 +438,7 @@ data HaddockProjectFlags = HaddockProjectFlags
447438
, -- haddockContent is not supported, a fixed value is provided
448439
-- haddockIndex is not supported, a fixed value is provided
449440
-- haddockDistPerf is not supported, note: it changes location of the haddocks
450-
haddockProjectKeepTempFiles :: Flag Bool
451-
, haddockProjectVerbosity :: Flag Verbosity
452-
, -- haddockBaseUrl is not supported, a fixed value is provided
441+
-- haddockBaseUrl is not supported, a fixed value is provided
453442
haddockProjectResourcesDir :: Flag String
454443
, haddockProjectUseUnicode :: Flag Bool
455444
}
@@ -473,8 +462,6 @@ defaultHaddockProjectFlags =
473462
, haddockProjectInternal = Flag False
474463
, haddockProjectCss = NoFlag
475464
, haddockProjectHscolourCss = NoFlag
476-
, haddockProjectKeepTempFiles = Flag False
477-
, haddockProjectVerbosity = Flag normal
478465
, haddockProjectResourcesDir = NoFlag
479466
, haddockProjectInterfaces = NoFlag
480467
, haddockProjectUseUnicode = NoFlag
@@ -632,13 +619,6 @@ haddockProjectOptions showOrParseArgs =
632619
haddockProjectHscolourCss
633620
(\v flags -> flags{haddockProjectHscolourCss = v})
634621
(reqArgFlag "PATH")
635-
, option
636-
""
637-
["keep-temp-files"]
638-
"Keep temporary files"
639-
haddockProjectKeepTempFiles
640-
(\b flags -> flags{haddockProjectKeepTempFiles = b})
641-
trueArg
642622
, option
643623
""
644624
["resources-dir"]

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ haddockProjectAction flags _extraArgs globalFlags = do
391391
if localStyle
392392
then Flag (toPathTemplate "../doc-index.html")
393393
else NoFlag
394-
, haddockKeepTempFiles = haddockProjectKeepTempFiles flags
395394
, haddockResourcesDir = haddockProjectResourcesDir flags
396395
, haddockUseUnicode = haddockProjectUseUnicode flags
397396
-- NOTE: we don't pass `haddockOutputDir`. If we do, we'll need to

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ import Distribution.Simple.Compiler
103103
)
104104
import Distribution.Simple.Setup
105105
( ReplOptions (..)
106+
, commonSetupTempFileOptions
106107
, setupVerbosity
107108
)
108109
import Distribution.Simple.Utils
109-
( TempFileOptions (..)
110-
, debugNoWrap
110+
( debugNoWrap
111111
, dieWithException
112112
, withTempDirectoryEx
113113
, wrapText
@@ -411,7 +411,7 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
411411
-- Multi Repl implemention see: https://well-typed.com/blog/2023/03/cabal-multi-unit/ for
412412
-- a high-level overview about how everything fits together.
413413
if Set.size (distinctTargetComponents targets) > 1
414-
then withTempDirectoryEx verbosity (TempFileOptions keepTempFiles) distDir "multi-out" $ \dir' -> do
414+
then withTempDirectoryEx verbosity tempFileOptions distDir "multi-out" $ \dir' -> do
415415
-- multi target repl
416416
dir <- makeAbsolute dir'
417417
-- Modify the replOptions so that the ./Setup repl command will write options
@@ -507,7 +507,7 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
507507
go m _ = m
508508

509509
verbosity = fromFlagOrDefault normal (setupVerbosity $ configCommonFlags configFlags)
510-
keepTempFiles = fromFlagOrDefault False replKeepTempFiles
510+
tempFileOptions = commonSetupTempFileOptions $ configCommonFlags configFlags
511511

512512
validatedTargets ctx compiler elaboratedPlan targetSelectors = do
513513
let multi_repl_enabled = multiReplDecision ctx compiler r

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ instance Semigroup SavedConfig where
470470
, setupCabalFilePath = combine setupCabalFilePath
471471
, setupVerbosity = combine setupVerbosity
472472
, setupTargets = lastNonEmpty setupTargets
473+
, setupKeepTempFiles = combine setupKeepTempFiles
473474
}
474475
where
475476
lastNonEmpty = lastNonEmpty' which
@@ -630,7 +631,6 @@ instance Semigroup SavedConfig where
630631
, haddockQuickJump = combine haddockQuickJump
631632
, haddockHscolourCss = combine haddockHscolourCss
632633
, haddockContents = combine haddockContents
633-
, haddockKeepTempFiles = combine haddockKeepTempFiles
634634
, haddockIndex = combine haddockIndex
635635
, haddockBaseUrl = combine haddockBaseUrl
636636
, haddockResourcesDir = combine haddockResourcesDir

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module Distribution.Client.ParseUtils
1717
FieldDescr (..)
1818
, liftField
1919
, liftFields
20+
, addFields
21+
, aliasField
2022
, filterFields
2123
, mapFieldNames
2224
, commandOptionToField
@@ -103,9 +105,15 @@ liftFields get set = map (liftField get set)
103105

104106
-- | Given a collection of field descriptions, keep only a given list of them,
105107
-- identified by name.
108+
--
109+
-- TODO: This makes it easy to footgun by providing a non-existent field name.
106110
filterFields :: [String] -> [FieldDescr a] -> [FieldDescr a]
107111
filterFields includeFields = filter ((`elem` includeFields) . fieldName)
108112

113+
-- | Given a collection of field descriptions, get a field with a given name.
114+
getField :: String -> [FieldDescr a] -> Maybe (FieldDescr a)
115+
getField name = find ((== name) . fieldName)
116+
109117
-- | Apply a name mangling function to the field names of all the field
110118
-- descriptions. The typical use case is to apply some prefix.
111119
mapFieldNames :: (String -> String) -> [FieldDescr a] -> [FieldDescr a]
@@ -120,6 +128,30 @@ commandOptionToField = viewAsFieldDescr
120128
commandOptionsToFields :: [OptionField a] -> [FieldDescr a]
121129
commandOptionsToFields = map viewAsFieldDescr
122130

131+
-- | Add fields to a field list.
132+
addFields
133+
:: [FieldDescr a]
134+
-> ([FieldDescr a] -> [FieldDescr a])
135+
addFields = (++)
136+
137+
-- | Add a new field which is identical to an existing field but with a
138+
-- different name.
139+
aliasField
140+
:: String
141+
-- ^ The existing field name.
142+
-> String
143+
-- ^ The new field name.
144+
-> [FieldDescr a]
145+
-> [FieldDescr a]
146+
aliasField oldName newName fields =
147+
let fieldToRename = getField oldName fields
148+
in case fieldToRename of
149+
-- TODO: Should this throw?
150+
Nothing -> fields
151+
Just fieldToRename' ->
152+
let newField = fieldToRename'{fieldName = newName}
153+
in newField : fields
154+
123155
------------------------------------------
124156
-- SectionDescr definition and utilities
125157
--

cabal-install/src/Distribution/Client/ProjectBuilding/UnpackedPackage.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ buildAndRegisterUnpackedPackage
173173
verbosity
174174
distDirLayout@DistDirLayout{distTempDirectory}
175175
maybe_semaphore
176-
buildTimeSettings@BuildTimeSettings{buildSettingNumJobs}
176+
buildTimeSettings@BuildTimeSettings{buildSettingNumJobs, buildSettingKeepTempFiles}
177177
registerLock
178178
cacheLock
179179
pkgshared@ElaboratedSharedConfig
@@ -276,7 +276,7 @@ buildAndRegisterUnpackedPackage
276276
| otherwise = return ()
277277

278278
mbWorkDir = useWorkingDir scriptOptions
279-
commonFlags = setupHsCommonFlags verbosity mbWorkDir builddir
279+
commonFlags = setupHsCommonFlags verbosity mbWorkDir builddir buildSettingKeepTempFiles
280280

281281
configureCommand = Cabal.configureCommand defaultProgramDb
282282
configureFlags v =

0 commit comments

Comments
 (0)