Skip to content

Commit

Permalink
stack build: Add --keep-tmp-files flag. Fixes commercialhaskell#3857
Browse files Browse the repository at this point in the history
  • Loading branch information
nh2 committed Feb 10, 2018
1 parent b27e629 commit 24d3666
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Stack/Build/Execute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ withExecuteEnv :: forall env a. HasEnvConfig env
-> (ExecuteEnv -> RIO env a)
-> RIO env a
withExecuteEnv menv bopts boptsCli baseConfigOpts locals globalPackages snapshotPackages localPackages inner =
withSystemTempDir stackProgName $ \tmpdir -> do
createTempDirFunction stackProgName $ \tmpdir -> do
configLock <- liftIO $ newMVar ()
installLock <- liftIO $ newMVar ()
idMap <- liftIO $ newTVarIO Map.empty
Expand Down Expand Up @@ -391,6 +391,10 @@ withExecuteEnv menv bopts boptsCli baseConfigOpts locals globalPackages snapshot
where
toDumpPackagesByGhcPkgId = Map.fromList . map (\dp -> (dpGhcPkgId dp, dp))

createTempDirFunction
| Just True <- boptsKeepTmpFiles bopts = withKeepSystemTempDir
| otherwise = withSystemTempDir

dumpLogs :: TChan (Path Abs Dir, Path Abs File) -> Int -> RIO env ()
dumpLogs chan totalWanted = do
allLogs <- fmap reverse $ liftIO $ atomically drainChan
Expand Down
1 change: 1 addition & 0 deletions src/Stack/Config/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ buildOptsFromMonoid BuildOptsMonoid{..} = BuildOpts
(boptsPreFetch defaultBuildOpts)
buildMonoidPreFetch
, boptsKeepGoing = getFirst buildMonoidKeepGoing
, boptsKeepTmpFiles = getFirst buildMonoidKeepTmpFiles
, boptsForceDirty = fromFirst
(boptsForceDirty defaultBuildOpts)
buildMonoidForceDirty
Expand Down
7 changes: 6 additions & 1 deletion src/Stack/Options/BuildMonoidParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ buildOptsMonoidParser hide0 =
exeStripping <*> haddock <*> haddockOptsParser hideBool <*>
openHaddocks <*> haddockDeps <*> haddockInternal <*>
haddockHyperlinkSource <*> copyBins <*> copyCompilerTool <*>
preFetch <*> keepGoing <*> forceDirty <*>
preFetch <*> keepGoing <*> keepTmpFiles <*> forceDirty <*>
tests <*> testOptsParser hideBool <*> benches <*>
benchOptsParser hideBool <*> reconfigure <*> cabalVerbose <*> splitObjs <*> skipComponents
where
Expand Down Expand Up @@ -120,6 +120,11 @@ buildOptsMonoidParser hide0 =
"keep-going"
"continue running after a step fails (default: false for build, true for test/bench)"
hide
keepTmpFiles =
firstBoolFlags
"keep-tmp-files"
"keep intermediate files and build directories (default: false)"
hide
preFetch =
firstBoolFlags
"prefetch"
Expand Down
8 changes: 8 additions & 0 deletions src/Stack/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Stack.Prelude
, liftResourceT
, NoLogging (..)
, withSystemTempDir
, withKeepSystemTempDir
, fromFirst
, mapMaybeA
, mapMaybeM
Expand Down Expand Up @@ -164,6 +165,13 @@ instance MonadLogger NoLogging where
withSystemTempDir :: MonadUnliftIO m => String -> (Path Abs Dir -> m a) -> m a
withSystemTempDir str inner = withRunInIO $ \run -> Path.IO.withSystemTempDir str $ run . inner

-- | Like `withSystemTempDir`, but the temporary directory is not deleted.
withKeepSystemTempDir :: MonadUnliftIO m => String -> (Path Abs Dir -> m a) -> m a
withKeepSystemTempDir str inner = withRunInIO $ \run -> do
path <- Path.IO.getTempDir
dir <- Path.IO.createTempDir path str
run $ inner dir

-- | Write a "sticky" line to the terminal. Any subsequent lines will
-- overwrite this one, and that same line will be repeated below
-- again. In other words, the line sticks at the bottom of the output
Expand Down
8 changes: 8 additions & 0 deletions src/Stack/Types/Config/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ data BuildOpts =
-- ^ Watch files for changes and automatically rebuild
,boptsKeepGoing :: !(Maybe Bool)
-- ^ Keep building/running after failure
,boptsKeepTmpFiles :: !(Maybe Bool)
-- ^ Keep intermediate files and build directories
,boptsForceDirty :: !Bool
-- ^ Force treating all local packages as having dirty files

Expand Down Expand Up @@ -105,6 +107,7 @@ defaultBuildOpts = BuildOpts
, boptsInstallCompilerTool = False
, boptsPreFetch = False
, boptsKeepGoing = Nothing
, boptsKeepTmpFiles = Nothing
, boptsForceDirty = False
, boptsTests = False
, boptsTestOpts = defaultTestOpts
Expand Down Expand Up @@ -172,6 +175,7 @@ data BuildOptsMonoid = BuildOptsMonoid
, buildMonoidInstallCompilerTool :: !(First Bool)
, buildMonoidPreFetch :: !(First Bool)
, buildMonoidKeepGoing :: !(First Bool)
, buildMonoidKeepTmpFiles :: !(First Bool)
, buildMonoidForceDirty :: !(First Bool)
, buildMonoidTests :: !(First Bool)
, buildMonoidTestOpts :: !TestOptsMonoid
Expand Down Expand Up @@ -202,6 +206,7 @@ instance FromJSON (WithJSONWarnings BuildOptsMonoid) where
buildMonoidInstallCompilerTool <- First <$> o ..:? buildMonoidInstallCompilerToolArgName
buildMonoidPreFetch <- First <$> o ..:? buildMonoidPreFetchArgName
buildMonoidKeepGoing <- First <$> o ..:? buildMonoidKeepGoingArgName
buildMonoidKeepTmpFiles <- First <$> o ..:? buildMonoidKeepTmpFilesArgName
buildMonoidForceDirty <- First <$> o ..:? buildMonoidForceDirtyArgName
buildMonoidTests <- First <$> o ..:? buildMonoidTestsArgName
buildMonoidTestOpts <- jsonSubWarnings (o ..:? buildMonoidTestOptsArgName ..!= mempty)
Expand Down Expand Up @@ -255,6 +260,9 @@ buildMonoidPreFetchArgName = "prefetch"
buildMonoidKeepGoingArgName :: Text
buildMonoidKeepGoingArgName = "keep-going"

buildMonoidKeepTmpFilesArgName :: Text
buildMonoidKeepTmpFilesArgName = "keep-tmp-files"

buildMonoidForceDirtyArgName :: Text
buildMonoidForceDirtyArgName = "force-dirty"

Expand Down

0 comments on commit 24d3666

Please sign in to comment.