diff --git a/liquidhaskell-boot/src/Language/Haskell/Liquid/UX/Annotate.hs b/liquidhaskell-boot/src/Language/Haskell/Liquid/UX/Annotate.hs index 1b4a2ab8eb..f30532183f 100644 --- a/liquidhaskell-boot/src/Language/Haskell/Liquid/UX/Annotate.hs +++ b/liquidhaskell-boot/src/Language/Haskell/Liquid/UX/Annotate.hs @@ -20,6 +20,7 @@ module Language.Haskell.Liquid.UX.Annotate , annErrors ) where +import Control.Monad (unless) import Data.Hashable import qualified Data.Text.IO as Text import Data.String @@ -132,10 +133,16 @@ copyFileCreateParentDirIfMissing src tgt = do Dir.createDirectoryIfMissing False $ tempDirectory tgt Dir.copyFile src tgt +-- | Creates the parent directory and writes the file if the +-- file does not exist already. +-- This prevents failure if multiple threads are trying to write to +-- the same file. writeFileCreateParentDirIfMissing :: T.Text -> FilePath -> IO () writeFileCreateParentDirIfMissing s tgt = do Dir.createDirectoryIfMissing False $ tempDirectory tgt - Text.writeFile tgt s + exists <- Dir.doesFileExist tgt + unless exists $ + Text.writeFile tgt s writeFilesOrStrings :: FilePath -> [Either FilePath String] -> IO () writeFilesOrStrings tgtFile = mapM_ $ either (`copyFileCreateParentDirIfMissing` tgtFile) (tgtFile `appendFile`)