@@ -89,12 +89,31 @@ createDatabase :: (MonadIO m, MonadLogger m, MonadBaseControl IO m, MonadCatch m
8989createDatabase menv wc db = do
9090 exists <- doesFileExist (db </> $ (mkRelFile " package.cache" ))
9191 unless exists $ do
92- -- Creating the parent doesn't seem necessary, as ghc-pkg
93- -- seems to be sufficiently smart. But I don't feel like
94- -- finding out it isn't the hard way
95- ensureDir (parent db)
96- _ <- tryProcessStdout Nothing menv (ghcPkgExeName wc) [" init" , toFilePath db]
97- return ()
92+ -- ghc-pkg requires that the database directory does not exist
93+ -- yet. If the directory exists but the package.cache file
94+ -- does, we're in a corrupted state. Check for that state.
95+ dirExists <- doesDirExist db
96+ args <- if dirExists
97+ then do
98+ $ logWarn $ T. pack $ concat
99+ [ " The package database located at "
100+ , toFilePath db
101+ , " is corrupted (missing its package.cache file)."
102+ ]
103+ $ logWarn " Proceeding with a recache"
104+ return [" --package-db" , toFilePath db, " recache" ]
105+ else do
106+ -- Creating the parent doesn't seem necessary, as ghc-pkg
107+ -- seems to be sufficiently smart. But I don't feel like
108+ -- finding out it isn't the hard way
109+ ensureDir (parent db)
110+ return [" init" , toFilePath db]
111+ eres <- tryProcessStdout Nothing menv (ghcPkgExeName wc) args
112+ case eres of
113+ Left e -> do
114+ $ logError $ T. pack $ " Unable to create package database at " ++ toFilePath db
115+ throwM e
116+ Right _ -> return ()
98117
99118-- | Get the name to use for "ghc-pkg", given the compiler version.
100119ghcPkgExeName :: WhichCompiler -> String
0 commit comments