diff --git a/waspc/src/ExternalCode.hs b/waspc/src/ExternalCode.hs index 6d0ae018d1..2f05e37c05 100644 --- a/waspc/src/ExternalCode.hs +++ b/waspc/src/ExternalCode.hs @@ -7,8 +7,11 @@ module ExternalCode , SourceExternalCodeDir ) where +import Control.Exception (catch) +import System.IO.Error (isDoesNotExistError) import qualified Data.Text.Lazy as TextL import qualified Data.Text.Lazy.IO as TextL.IO +import Data.Maybe (catMaybes) import Data.Text (Text) import qualified Util.IO @@ -60,7 +63,14 @@ readFiles extCodeDirPath = do -- or create new file draft that will support that. -- In generator, when creating TextFileDraft, give it function/logic for text transformation, -- and it will be taken care of when draft will be written to the disk. - fileTexts <- mapM (TextL.IO.readFile . SP.toFilePath) absFilePaths + fileTexts <- catMaybes <$> mapM (tryReadFile . SP.toFilePath) absFilePaths let files = map (\(path, text) -> File path extCodeDirPath text) (zip relFilePaths fileTexts) return files - + where + -- NOTE(matija): we had cases (e.g. tmp Vim files) where a file initially existed + -- but then got deleted before actual reading was invoked. + -- That would make this function crash, so we just ignore those errors. + tryReadFile :: FilePath -> IO (Maybe TextL.Text) + tryReadFile fp = (Just <$> (TextL.IO.readFile fp)) `catch` (\e -> if isDoesNotExistError e + then return Nothing + else ioError e) diff --git a/waspc/src/Generator/FileDraft/WriteableMonad.hs b/waspc/src/Generator/FileDraft/WriteableMonad.hs index cfea15f33e..c9811ce1b8 100644 --- a/waspc/src/Generator/FileDraft/WriteableMonad.hs +++ b/waspc/src/Generator/FileDraft/WriteableMonad.hs @@ -3,6 +3,8 @@ module Generator.FileDraft.WriteableMonad ) where +import Control.Exception (catch) +import System.IO.Error (isDoesNotExistError) import qualified System.Directory import qualified Data.Text.IO import Data.Aeson as Aeson @@ -48,7 +50,16 @@ class (Monad m) => WriteableMonad m where instance WriteableMonad IO where createDirectoryIfMissing = System.Directory.createDirectoryIfMissing - copyFile = System.Directory.copyFile + -- TODO(matija): we should rename this function to make it clear it won't throw an exception when + -- a file does not exist. + copyFile src dst = do + -- NOTE(matija): we had cases (e.g. tmp Vim files) where a file initially existed + -- when the filedraft was created but then got deleted before actual copying was invoked. + -- That would make this function crash, so we just ignore those errors. + (System.Directory.copyFile src dst) `catch` (\e -> if isDoesNotExistError e + then return () + else ioError e) + writeFileFromText = Data.Text.IO.writeFile getTemplateFileAbsPath = Templates.getTemplateFileAbsPath getTemplatesDirAbsPath = Templates.getTemplatesDirAbsPath