Skip to content

Commit

Permalink
Expand --keep-temp-files
Browse files Browse the repository at this point in the history
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). The flag will also be used for response
files; see haskell#9367.
  • Loading branch information
9999years committed Aug 29, 2024
1 parent 1e93e57 commit 96fe9ed
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 195 deletions.
10 changes: 3 additions & 7 deletions Cabal/src/Distribution/Simple/Haddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ import Distribution.Simple.Program.GHC
import qualified Distribution.Simple.Program.HcPkg as HcPkg
import Distribution.Simple.Program.ResponseFile
import Distribution.Simple.Register
import Distribution.Simple.Setup.Common
import Distribution.Simple.Setup.Haddock
import Distribution.Simple.Setup.Hscolour
import Distribution.Simple.Setup
import Distribution.Simple.SetupHooks.Internal
( BuildHooks (..)
, BuildingWhat (..)
, noBuildHooks
)
import qualified Distribution.Simple.SetupHooks.Internal as SetupHooks
Expand Down Expand Up @@ -265,6 +262,7 @@ haddock_setupHooks
mbWorkDir = flagToMaybe $ haddockWorkingDir flags
comp = compiler lbi
platform = hostPlatform lbi
config = configFlags lbi

quickJmpFlag = haddockQuickJump flags'
flags = case haddockTarget of
Expand All @@ -282,9 +280,7 @@ haddock_setupHooks
flag f = fromFlag $ f flags

tmpFileOpts =
defaultTempFileOptions
{ optKeepTempFiles = flag haddockKeepTempFiles
}
commonSetupTempFileOptions $ configCommonFlags config
htmlTemplate =
fmap toPathTemplate . flagToMaybe . haddockHtmlLocation $
flags
Expand Down
1 change: 1 addition & 0 deletions Cabal/src/Distribution/Simple/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module Distribution.Simple.Setup
, globalCommand
, CommonSetupFlags (..)
, defaultCommonSetupFlags
, commonSetupTempFileOptions
, ConfigFlags (..)
, emptyConfigFlags
, defaultConfigFlags
Expand Down
31 changes: 28 additions & 3 deletions Cabal/src/Distribution/Simple/Setup/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Distribution.Simple.Setup.Common
( CommonSetupFlags (..)
, defaultCommonSetupFlags
, withCommonSetupOptions
, commonSetupTempFileOptions
, CopyDest (..)
, configureCCompiler
, configureLinker
Expand Down Expand Up @@ -85,6 +86,13 @@ data CommonSetupFlags = CommonSetupFlags
--
-- TODO: this one should not be here, it's just that the silly
-- UserHooks stop us from passing extra info in other ways
, setupKeepTempFiles :: Flag Bool
-- ^ When this flag is set, temporary files will be kept after building.
--
-- Note: Keeping temporary files is important functionality for HLS, which
-- runs @cabal repl@ with a fake GHC to get CLI arguments. It will need the
-- temporary files (including multi unit repl response files) to stay, even
-- after the @cabal repl@ command exits.
}
deriving (Eq, Show, Read, Generic)

Expand All @@ -95,7 +103,7 @@ instance Semigroup CommonSetupFlags where
(<>) = gmappend

instance Monoid CommonSetupFlags where
mempty = gmempty
mempty = defaultCommonSetupFlags
mappend = (<>)

defaultCommonSetupFlags :: CommonSetupFlags
Expand All @@ -106,6 +114,15 @@ defaultCommonSetupFlags =
, setupDistPref = NoFlag
, setupCabalFilePath = NoFlag
, setupTargets = []
, setupKeepTempFiles = Flag False
}

-- | Get `TempFileOptions` that respect the `setupKeepTempFiles` flag.
commonSetupTempFileOptions :: CommonSetupFlags -> TempFileOptions
commonSetupTempFileOptions options =
TempFileOptions
{ optKeepTempFiles =
fromFlagOrDefault False (setupKeepTempFiles options)
}

commonSetupOptions :: ShowOrParseArgs -> [OptionField CommonSetupFlags]
Expand All @@ -124,8 +141,16 @@ commonSetupOptions showOrParseArgs =
setupCabalFilePath
(\v flags -> flags{setupCabalFilePath = v})
(reqSymbolicPathArgFlag "PATH")
-- NB: no --working-dir flag, as that value is populated using the
-- global flag (see Distribution.Simple.Setup.Global.globalCommand).
, -- NB: no --working-dir flag, as that value is populated using the
-- global flag (see Distribution.Simple.Setup.Global.globalCommand).
option
""
["keep-temp-files"]
( "Keep temporary files."
)
setupKeepTempFiles
(\v flags -> flags{setupKeepTempFiles = v})
trueArg
]

withCommonSetupOptions
Expand Down
Loading

0 comments on commit 96fe9ed

Please sign in to comment.