Skip to content

Commit

Permalink
Expand and unify --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) but is also used to determine when to keep
response files.
  • Loading branch information
9999years committed Nov 7, 2024
1 parent 836327b commit 7a04395
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ md5CheckGenericPackageDescription proxy = md5Check proxy

md5CheckLocalBuildInfo :: Proxy LocalBuildInfo -> Assertion
md5CheckLocalBuildInfo proxy = md5Check proxy
0x93b7e8ebb5b9f879fa5fe49b1708b43b
0x8fa7b2c8cc611407bfdcb734ecb460a2
14 changes: 6 additions & 8 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 Expand Up @@ -553,9 +549,11 @@ createHaddockIndex
-> IO ()
createHaddockIndex verbosity programDb comp platform mbWorkDir flags = do
let args = fromHaddockProjectFlags flags
tmpFileOpts =
commonSetupTempFileOptions $ haddockProjectCommonFlags $ flags
(haddockProg, _version) <-
getHaddockProg verbosity programDb comp args (Flag True)
runHaddock verbosity mbWorkDir defaultTempFileOptions comp platform haddockProg False args
runHaddock verbosity mbWorkDir tmpFileOpts comp platform haddockProg False args

-- ------------------------------------------------------------------------------
-- Contributions to HaddockArgs (see also Doctest.hs for very similar code).
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
25 changes: 25 additions & 0 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 @@ -106,6 +114,15 @@ defaultCommonSetupFlags =
, setupDistPref = NoFlag
, setupCabalFilePath = NoFlag
, setupTargets = []
, setupKeepTempFiles = NoFlag
}

-- | 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,6 +141,14 @@ commonSetupOptions showOrParseArgs =
setupCabalFilePath
(\v flags -> flags{setupCabalFilePath = v})
(reqSymbolicPathArgFlag "PATH")
, option
""
["keep-temp-files"]
( "Keep temporary files."
)
setupKeepTempFiles
(\keepTempFiles flags -> flags{setupKeepTempFiles = keepTempFiles})
trueArg
-- NB: no --working-dir flag, as that value is populated using the
-- global flag (see Distribution.Simple.Setup.Global.globalCommand).
]
Expand Down
22 changes: 1 addition & 21 deletions Cabal/src/Distribution/Simple/Setup/Haddock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ data HaddockFlags = HaddockFlags
, haddockHscolourCss :: Flag FilePath
, haddockContents :: Flag PathTemplate
, haddockIndex :: Flag PathTemplate
, haddockKeepTempFiles :: Flag Bool
, haddockBaseUrl :: Flag String
, haddockResourcesDir :: Flag String
, haddockOutputDir :: Flag FilePath
Expand Down Expand Up @@ -166,7 +165,6 @@ defaultHaddockFlags =
, haddockQuickJump = Flag False
, haddockHscolourCss = NoFlag
, haddockContents = NoFlag
, haddockKeepTempFiles = Flag False
, haddockIndex = NoFlag
, haddockBaseUrl = NoFlag
, haddockResourcesDir = NoFlag
Expand Down Expand Up @@ -219,13 +217,6 @@ haddockOptions showOrParseArgs =
(\c f -> f{haddockCommonFlags = c})
showOrParseArgs
[ option
""
["keep-temp-files"]
"Keep temporary files"
haddockKeepTempFiles
(\b flags -> flags{haddockKeepTempFiles = b})
trueArg
, option
""
["hoogle"]
"Generate a hoogle database"
Expand Down Expand Up @@ -447,9 +438,7 @@ data HaddockProjectFlags = HaddockProjectFlags
, -- haddockContent is not supported, a fixed value is provided
-- haddockIndex is not supported, a fixed value is provided
-- haddockDistPerf is not supported, note: it changes location of the haddocks
haddockProjectKeepTempFiles :: Flag Bool
, haddockProjectVerbosity :: Flag Verbosity
, -- haddockBaseUrl is not supported, a fixed value is provided
-- haddockBaseUrl is not supported, a fixed value is provided
haddockProjectResourcesDir :: Flag String
, haddockProjectUseUnicode :: Flag Bool
}
Expand All @@ -473,8 +462,6 @@ defaultHaddockProjectFlags =
, haddockProjectInternal = Flag False
, haddockProjectCss = NoFlag
, haddockProjectHscolourCss = NoFlag
, haddockProjectKeepTempFiles = Flag False
, haddockProjectVerbosity = Flag normal
, haddockProjectResourcesDir = NoFlag
, haddockProjectInterfaces = NoFlag
, haddockProjectUseUnicode = NoFlag
Expand Down Expand Up @@ -632,13 +619,6 @@ haddockProjectOptions showOrParseArgs =
haddockProjectHscolourCss
(\v flags -> flags{haddockProjectHscolourCss = v})
(reqArgFlag "PATH")
, option
""
["keep-temp-files"]
"Keep temporary files"
haddockProjectKeepTempFiles
(\b flags -> flags{haddockProjectKeepTempFiles = b})
trueArg
, option
""
["resources-dir"]
Expand Down
1 change: 0 additions & 1 deletion cabal-install/src/Distribution/Client/CmdHaddockProject.hs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ haddockProjectAction flags _extraArgs globalFlags = do
if localStyle
then Flag (toPathTemplate "../doc-index.html")
else NoFlag
, haddockKeepTempFiles = haddockProjectKeepTempFiles flags
, haddockResourcesDir = haddockProjectResourcesDir flags
, haddockUseUnicode = haddockProjectUseUnicode flags
-- NOTE: we don't pass `haddockOutputDir`. If we do, we'll need to
Expand Down
8 changes: 4 additions & 4 deletions cabal-install/src/Distribution/Client/CmdRepl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ import Distribution.Simple.Compiler
)
import Distribution.Simple.Setup
( ReplOptions (..)
, commonSetupTempFileOptions
, setupVerbosity
)
import Distribution.Simple.Utils
( TempFileOptions (..)
, debugNoWrap
( debugNoWrap
, dieWithException
, withTempDirectoryEx
, wrapText
Expand Down Expand Up @@ -411,7 +411,7 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
-- Multi Repl implemention see: https://well-typed.com/blog/2023/03/cabal-multi-unit/ for
-- a high-level overview about how everything fits together.
if Set.size (distinctTargetComponents targets) > 1
then withTempDirectoryEx verbosity (TempFileOptions keepTempFiles) distDir "multi-out" $ \dir' -> do
then withTempDirectoryEx verbosity tempFileOptions distDir "multi-out" $ \dir' -> do
-- multi target repl
dir <- makeAbsolute dir'
-- Modify the replOptions so that the ./Setup repl command will write options
Expand Down Expand Up @@ -507,7 +507,7 @@ replAction flags@NixStyleFlags{extraFlags = r@ReplFlags{..}, ..} targetStrings g
go m _ = m

verbosity = fromFlagOrDefault normal (setupVerbosity $ configCommonFlags configFlags)
keepTempFiles = fromFlagOrDefault False replKeepTempFiles
tempFileOptions = commonSetupTempFileOptions $ configCommonFlags configFlags

validatedTargets ctx compiler elaboratedPlan targetSelectors = do
let multi_repl_enabled = multiReplDecision ctx compiler r
Expand Down
2 changes: 1 addition & 1 deletion cabal-install/src/Distribution/Client/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ instance Semigroup SavedConfig where
, setupCabalFilePath = combine setupCabalFilePath
, setupVerbosity = combine setupVerbosity
, setupTargets = lastNonEmpty setupTargets
, setupKeepTempFiles = combine setupKeepTempFiles
}
where
lastNonEmpty = lastNonEmpty' which
Expand Down Expand Up @@ -630,7 +631,6 @@ instance Semigroup SavedConfig where
, haddockQuickJump = combine haddockQuickJump
, haddockHscolourCss = combine haddockHscolourCss
, haddockContents = combine haddockContents
, haddockKeepTempFiles = combine haddockKeepTempFiles
, haddockIndex = combine haddockIndex
, haddockBaseUrl = combine haddockBaseUrl
, haddockResourcesDir = combine haddockResourcesDir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ buildAndRegisterUnpackedPackage
verbosity
distDirLayout@DistDirLayout{distTempDirectory}
maybe_semaphore
buildTimeSettings@BuildTimeSettings{buildSettingNumJobs}
buildTimeSettings@BuildTimeSettings{buildSettingNumJobs, buildSettingKeepTempFiles}
registerLock
cacheLock
pkgshared@ElaboratedSharedConfig
Expand Down Expand Up @@ -276,7 +276,7 @@ buildAndRegisterUnpackedPackage
| otherwise = return ()

mbWorkDir = useWorkingDir scriptOptions
commonFlags = setupHsCommonFlags verbosity mbWorkDir builddir
commonFlags = setupHsCommonFlags verbosity mbWorkDir builddir buildSettingKeepTempFiles

configureCommand = Cabal.configureCommand defaultProgramDb
configureFlags v =
Expand Down
15 changes: 4 additions & 11 deletions cabal-install/src/Distribution/Client/ProjectConfig/Legacy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ convertLegacyBuildOnlyFlags
configFlags
installFlags
clientInstallFlags
haddockFlags
_haddockFlags
_testFlags
_benchmarkFlags =
ProjectConfigBuildOnly{..}
Expand All @@ -880,6 +880,7 @@ convertLegacyBuildOnlyFlags

CommonSetupFlags
{ setupVerbosity = projectConfigVerbosity
, setupKeepTempFiles = projectConfigKeepTempFiles
} = commonFlags

InstallFlags
Expand All @@ -899,10 +900,6 @@ convertLegacyBuildOnlyFlags
, installOfflineMode = projectConfigOfflineMode
} = installFlags

HaddockFlags
{ haddockKeepTempFiles = projectConfigKeepTempFiles -- TODO: this ought to live elsewhere
} = haddockFlags

convertToLegacyProjectConfig :: ProjectConfig -> LegacyProjectConfig
convertToLegacyProjectConfig
projectConfig@ProjectConfig
Expand Down Expand Up @@ -975,6 +972,7 @@ convertToLegacySharedConfig
mempty
{ setupVerbosity = projectConfigVerbosity
, setupDistPref = fmap makeSymbolicPath $ projectConfigDistDir
, setupKeepTempFiles = projectConfigKeepTempFiles
}

configFlags =
Expand Down Expand Up @@ -1047,8 +1045,7 @@ convertToLegacySharedConfig
convertToLegacyAllPackageConfig :: ProjectConfig -> LegacyPackageConfig
convertToLegacyAllPackageConfig
ProjectConfig
{ projectConfigBuildOnly = ProjectConfigBuildOnly{..}
, projectConfigShared = ProjectConfigShared{..}
{ projectConfigShared = ProjectConfigShared{..}
} =
LegacyPackageConfig
{ legacyConfigureFlags = configFlags
Expand Down Expand Up @@ -1124,8 +1121,6 @@ convertToLegacyAllPackageConfig

haddockFlags =
mempty
{ haddockKeepTempFiles = projectConfigKeepTempFiles
}

convertToLegacyPerPackageConfig :: PackageConfig -> LegacyPackageConfig
convertToLegacyPerPackageConfig PackageConfig{..} =
Expand Down Expand Up @@ -1225,7 +1220,6 @@ convertToLegacyPerPackageConfig PackageConfig{..} =
, haddockQuickJump = packageConfigHaddockQuickJump
, haddockHscolourCss = packageConfigHaddockHscolourCss
, haddockContents = packageConfigHaddockContents
, haddockKeepTempFiles = mempty
, haddockIndex = packageConfigHaddockIndex
, haddockBaseUrl = packageConfigHaddockBaseUrl
, haddockResourcesDir = packageConfigHaddockResourcesDir
Expand Down Expand Up @@ -1631,7 +1625,6 @@ legacyPackageConfigFieldDescrs =
, "hscolour-css"
, "contents-location"
, "index-location"
, "keep-temp-files"
, "base-url"
, "resources-dir"
, "output-dir"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ printPlan
printPlan
verbosity
ProjectBaseContext
{ buildSettings = BuildTimeSettings{buildSettingDryRun}
{ buildSettings = BuildTimeSettings{buildSettingDryRun, buildSettingKeepTempFiles}
, projectConfig =
ProjectConfig
{ projectConfigAllPackages =
Expand Down Expand Up @@ -1048,6 +1048,7 @@ printPlan
verbosity
Nothing -- omit working directory
(makeSymbolicPath "$builddir")
buildSettingKeepTempFiles
fullConfigureFlags =
runIdentity $
( setupHsConfigureFlags
Expand Down
7 changes: 4 additions & 3 deletions cabal-install/src/Distribution/Client/ProjectPlanning.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4093,14 +4093,16 @@ setupHsCommonFlags
:: Verbosity
-> Maybe (SymbolicPath CWD (Dir Pkg))
-> SymbolicPath Pkg (Dir Dist)
-> Bool
-> Cabal.CommonSetupFlags
setupHsCommonFlags verbosity mbWorkDir builddir =
setupHsCommonFlags verbosity mbWorkDir builddir keepTempFiles =
Cabal.CommonSetupFlags
{ setupDistPref = toFlag builddir
, setupVerbosity = toFlag verbosity
, setupCabalFilePath = mempty
, setupWorkingDir = maybeToFlag mbWorkDir
, setupTargets = []
, setupKeepTempFiles = toFlag keepTempFiles
}

setupHsBuildFlags
Expand Down Expand Up @@ -4229,7 +4231,7 @@ setupHsHaddockFlags
setupHsHaddockFlags
(ElaboratedConfiguredPackage{..})
(ElaboratedSharedConfig{..})
(BuildTimeSettings{buildSettingKeepTempFiles = keepTmpFiles})
_buildTimeSettings
common =
Cabal.HaddockFlags
{ haddockCommonFlags = common
Expand Down Expand Up @@ -4257,7 +4259,6 @@ setupHsHaddockFlags
, haddockQuickJump = toFlag elabHaddockQuickJump
, haddockHscolourCss = maybe mempty toFlag elabHaddockHscolourCss
, haddockContents = maybe mempty toFlag elabHaddockContents
, haddockKeepTempFiles = toFlag keepTmpFiles
, haddockIndex = maybe mempty toFlag elabHaddockIndex
, haddockBaseUrl = maybe mempty toFlag elabHaddockBaseUrl
, haddockResourcesDir = maybe mempty toFlag elabHaddockResourcesDir
Expand Down
Loading

0 comments on commit 7a04395

Please sign in to comment.