@@ -30,7 +30,7 @@ import Stackage.Prelude hiding (pi)
3030import System.Directory (findExecutable)
3131import System.Environment (getEnvironment)
3232import System.IO (IOMode (WriteMode),
33− withBinaryFile)
33+ openBinaryFile)
3434import System.IO.Temp (withSystemTempDirectory)
3535
3636data BuildException = BuildException (Map PackageName BuildFailure) [Text]
@@ -198,6 +198,7 @@ performBuild' pb@PerformBuild {..} = withBuildDir $ \builddir -> do
198198 (pbDocDir pb)
199199 pbLog
200200 (ppVersion <$> bpPackages pbPlan)
201+ (deletePreviousResults pb)
201202
202203 forM_ packageMap $ \pi -> void $ async $ singleBuild pb registeredPackages
203204 SingleBuild
@@ -290,11 +291,12 @@ singleBuild pb@PerformBuild {..} registeredPackages SingleBuild {..} =
290291 , display $ ppVersion $ piPlan sbPackageInfo
291292 ]
292293
293− runIn wdir outH cmd args =
294− withCheckedProcess cp $ \ClosedStream UseProvidedHandle UseProvidedHandle ->
294+ runIn wdir getOutH cmd args = do
295+ outH <- getOutH
296+ withCheckedProcess (cp outH) $ \ClosedStream UseProvidedHandle UseProvidedHandle ->
295297 (return () :: IO ())
296298 where
297− cp = (proc (unpack $ asText cmd) (map (unpack . asText) args))
299+ cp outH = (proc (unpack $ asText cmd) (map (unpack . asText) args))
298300 { cwd = Just $ fpToString wdir
299301 , std_out = UseHandle outH
300302 , std_err = UseHandle outH
@@ -320,8 +322,21 @@ singleBuild pb@PerformBuild {..} registeredPackages SingleBuild {..} =
320322 testRunOut = pbLogDir </> fpFromText namever </> "test-run.out"
321323
322324 wf fp inner' = do
323− createTree $ parent fp
324− withBinaryFile (fpToString fp) WriteMode inner'
325+ ref <- newIORef Nothing
326+ let cleanup = do
327+ mh <- readIORef ref
328+ forM_ mh hClose
329+ getH = do
330+ mh <- readIORef ref
331+ case mh of
332+ Just h -> return h
333+ Nothing -> mask_ $ do
334+ createTree $ parent fp
335+ h <- openBinaryFile (fpToString fp) WriteMode
336+ writeIORef ref $ Just h
337+ return h
338+
339+ inner' getH `finally` cleanup
325340
326341 configArgs = ($ []) $ execWriter $ do
327342 when pbAllowNewer $ tell' "--allow-newer"
@@ -349,15 +364,15 @@ singleBuild pb@PerformBuild {..} registeredPackages SingleBuild {..} =
349364
350365 PackageConstraints {..} = ppConstraints $ piPlan sbPackageInfo
351366
352− buildLibrary = wf libOut $ \outH -> do
367+ buildLibrary = wf libOut $ \getOutH -> do
353368 let run a b = do when pbVerbose $ log' (unwords (a : b))
354− runChild outH a b
369+ runChild getOutH a b
355370
356371 isUnpacked <- newIORef False
357372 let withUnpacked inner = do
358373 unlessM (readIORef isUnpacked) $ do
359374 log' $ "Unpacking " ++ namever
360− runParent outH "cabal" ["unpack", namever]
375+ runParent getOutH "cabal" ["unpack", namever]
361376 writeIORef isUnpacked True
362377 inner
363378
@@ -439,8 +454,8 @@ singleBuild pb@PerformBuild {..} registeredPackages SingleBuild {..} =
439454
440455 return withUnpacked
441456
442− runTests withUnpacked = wf testOut $ \outH -> do
443− let run = runChild outH
457+ runTests withUnpacked = wf testOut $ \getOutH -> do
458+ let run = runChild getOutH
444459
445460 prevTestResult <- getPreviousResult pb Test pident
446461 let needTest = pbEnableTests