git.iko.soy/typeable/cabal2nix
on master help sign in
don't try different fetcher if cabal parsing fails

If we get a directory which contains a cabal file that produces a parse error, we don't try another fetcher. Trying another fetcher when parsing fails is only needed if the fetcher result is a file, because then it might be that the fetcher only got a html result (fetchurl with git repo for example) which doesn't parse (=> need to try another fetcher).

commit86b4c0de7514e0170c848a2f1102af24847b51e0 authorBenno Fünfstück <benno.fuenfstueck@gmail.com> dateSun 17 Aug 2014, 23:17 (12 years ago) parent66dd4c6
1 file changed+19−9
src/Cabal2Nix/Package.hs+19−9
src/Cabal2Nix/Package.hs+19−9
@@ -19,6 +19,9 @@ import qualified Distribution.Package as Cabal
1919import qualified Distribution.PackageDescription as Cabal
2020import qualified Distribution.PackageDescription.Parse as Cabal
2121
22+import qualified Distribution.Compat.Exception as Compat
23+import qualified Distribution.ParseUtils as ParseUtils
24+
2225data Package = Package
2326 { source :: DerivationSource
2427 , cabal :: Cabal.GenericPackageDescription
@@ -36,7 +39,8 @@ fetchOrFromDB optHackageDB src
3639 | otherwise = do
3740 r <- fetch cabalFromPath src
3841 case r of
39− Nothing -> hPutStrLn stderr "*** failed to fetch source. Does the URL exist?" >> exitFailure
42+ Nothing ->
43+ hPutStrLn stderr "*** failed to fetch source. Does the URL exist?" >> exitFailure
4044 Just (derivSource, (externalSource, pkgDesc)) ->
4145 return (derivSource <$ guard externalSource, pkgDesc)
4246
@@ -75,7 +79,7 @@ sourceFromHackage :: Maybe String -> String -> IO DerivationSource
7579sourceFromHackage optHash pkgId = do
7680 cacheFile <- hashCachePath pkgId
7781 let cachedHash = MaybeT $ maybe (readFileMay cacheFile) (return . Just) optHash
78− let url = "mirror://hackage/" ++ pkgId ++ ".tar.gz"
82+ url = "mirror://hackage/" ++ pkgId ++ ".tar.gz"
7983
8084 -- Use the cached hash (either from cache file or given on cmdline via sha256 opt)
8185 -- if available, otherwise download from hackage to compute hash.
@@ -109,19 +113,25 @@ cabalFromPath :: FilePath -> MaybeT IO (Bool, Cabal.GenericPackageDescription)
109113cabalFromPath path = do
110114 d <- liftIO $ doesDirectoryExist path
111115 (,) d <$> if d
112− then cabalFromDirectory path
113− else cabalFromFile path
116+ then cabalFromDirectory path
117+ else cabalFromFile False path
114118
115119cabalFromDirectory :: FilePath -> MaybeT IO Cabal.GenericPackageDescription
116120cabalFromDirectory dir = do
117121 cabals <- liftIO $ getDirectoryContents dir >>= filterM doesFileExist . map (dir </>) . filter (".cabal" `isSuffixOf`)
118122 case cabals of
119− [cabalFile] -> cabalFromFile cabalFile
123+ [cabalFile] -> cabalFromFile True cabalFile
120124 _ -> liftIO $ hPutStrLn stderr "*** found zero or more than one cabal file. Exiting." >> exitFailure
121125
122−cabalFromFile :: FilePath -> MaybeT IO Cabal.GenericPackageDescription
123−cabalFromFile file = do
124− content <- liftIO $ readFile file
126+cabalFromFile :: Bool -> FilePath -> MaybeT IO Cabal.GenericPackageDescription
127+cabalFromFile failHard file = do
128+ content <- MaybeT $ either (const Nothing) Just <$> Compat.tryIO (readFile file)
125129 case Cabal.parsePackageDescription content of
126− Cabal.ParseFailed _ -> mzero
130+ Cabal.ParseFailed e -> do
131+ guard failHard
132+ liftIO $ do
133+ let (line, err) = ParseUtils.locatedErrorMsg e
134+ msg = maybe "" ((++ ": ") . show) line ++ err
135+ putStrLn $ "*** error parsing cabal file: " ++ msg
136+ exitFailure
127137 Cabal.ParseOk _ a -> return a