1717
1818module Stack.Package
1919 (readPackage
20- ,readPackageBS
2120 ,readPackageDescriptionDir
2221 ,readDotBuildinfo
2322 ,readPackageUnresolved
23+ ,readPackageUnresolvedFromIndex
2424 ,resolvePackage
25- ,cachedCabalFileParse
2625 ,CabalWarnings (.. )
2726 ,packageFromPackageDescription
2827 ,findOrGenerateCabalFile
@@ -38,7 +37,6 @@ module Stack.Package
3837 ,packageDescTools
3938 ,packageDependencies
4039 ,autogenDir
41- ,checkCabalFileName
4240 ,cabalFilePackageId )
4341 where
4442
@@ -85,7 +83,7 @@ import Stack.Constants.Config
8583import Stack.Prelude
8684import Stack.PrettyPrint
8785import Stack.Types.Build
88- import Stack.Types.BuildPlan (PackageLocationIndex ( .. ), PackageLocation ( .. ), ExeName (.. ))
86+ import Stack.Types.BuildPlan (ExeName (.. ))
8987import Stack.Types.Compiler
9088import Stack.Types.Config
9189import Stack.Types.FlagName
@@ -119,7 +117,7 @@ instance HasEnvConfig Ctx where
119117
120118data CabalWarnings
121119 = CWNoPrint
122- | CWPrint ! String
120+ | CWPrint ! ( Path Abs File )
123121
124122-- | Parse a cabal file from the given location. This performs caching
125123-- (based on the 'PackageLocationIndex'), and will only use the second
@@ -128,10 +126,10 @@ data CabalWarnings
128126-- 'True', then they will be printed.
129127cachedCabalFileParse
130128 :: forall env . HasRunner env
131- => PackageLocationIndex FilePath
129+ => Either PackageIdentifierRevision ( Path Abs File )
132130 -> CabalWarnings
133131 -> RIO env BS. ByteString -- ^ get the bytestring contents
134- -> RIO env ( Either PError GenericPackageDescription )
132+ -> RIO env GenericPackageDescription
135133cachedCabalFileParse key cw getBS = do
136134 ref <- view $ runnerL. to runnerParsedCabalFiles
137135 m0 <- readIORef ref
@@ -141,12 +139,12 @@ cachedCabalFileParse key cw getBS = do
141139 bs <- getBS
142140 val <-
143141 case rawParseGPD bs of
144- Left e -> return $ Left e
142+ Left e -> throwM $ PackageInvalidCabalFile key e
145143 Right (warnings, gpd) -> do
146144 case cw of
147145 CWNoPrint -> return ()
148- CWPrint src -> mapM_ (prettyWarnL . toPretty src) warnings
149- return $ Right gpd
146+ CWPrint src -> mapM_ (prettyWarnL . toPretty (toFilePath src) ) warnings
147+ return gpd
150148 atomicModifyIORef' ref $ \ m -> (M. insert key val m, () )
151149 return val
152150 where
@@ -182,23 +180,36 @@ readPackageUnresolved
182180 => Path Abs File -- ^ cabal file location
183181 -> Bool -- ^ print warnings?
184182 -> RIO env GenericPackageDescription
185- readPackageUnresolved cabalfp printWarnings = readPackageUnresolvedBS
186- (PLOther $ PLFilePath $ toFilePath cabalfp)
187- (if printWarnings then CWPrint (toFilePath cabalfp) else CWNoPrint )
188- (liftIO (BS. readFile (FL. toFilePath cabalfp)))
189-
190- -- | Read the raw, unresolved package information from a ByteString.
191- readPackageUnresolvedBS
183+ readPackageUnresolved cabalfp printWarnings = do
184+ gpd <- cachedCabalFileParse
185+ (Right cabalfp)
186+ (if printWarnings then CWPrint cabalfp else CWNoPrint )
187+ (liftIO (BS. readFile (FL. toFilePath cabalfp)))
188+ let PackageIdentifier name _version =
189+ fromCabalPackageIdentifier
190+ $ D. package
191+ $ D. packageDescription gpd
192+ checkCabalFileName name cabalfp
193+ return gpd
194+
195+ -- | Read the 'GenericPackageDescription' from the given
196+ -- 'PackageIdentifierRevision'.
197+ readPackageUnresolvedFromIndex
192198 :: forall env . HasRunner env
193- => PackageLocationIndex FilePath
194- -> CabalWarnings
195- -> RIO env BS. ByteString -- ^ get the contents
199+ => (PackageIdentifierRevision -> IO ByteString ) -- ^ load the raw bytes
200+ -> PackageIdentifierRevision
196201 -> RIO env GenericPackageDescription
197- readPackageUnresolvedBS loc cw getBS = do
198- eres <- cachedCabalFileParse loc cw getBS
199- case eres of
200- Left e -> throwM $ PackageInvalidCabalFile loc e
201- Right x -> return x
202+ readPackageUnresolvedFromIndex loadFromIndex pir@ (PackageIdentifierRevision pi' _) = do
203+ gpd <- cachedCabalFileParse
204+ (Left pir)
205+ CWNoPrint
206+ (liftIO $ loadFromIndex pir)
207+ let foundPI =
208+ fromCabalPackageIdentifier
209+ $ D. package
210+ $ D. packageDescription gpd
211+ unless (pi' == foundPI) $ error $ " Mismatched package identifiers found: " ++ show (pi', foundPI) -- FIXME better error message
212+ return gpd
202213
203214-- | Reads and exposes the package information
204215readPackage
@@ -210,18 +221,6 @@ readPackage
210221readPackage packageConfig cabalfp printWarnings =
211222 resolvePackage packageConfig <$> readPackageUnresolved cabalfp printWarnings
212223
213- -- | Reads and exposes the package information, from a ByteString
214- readPackageBS
215- :: forall env . HasRunner env
216- => PackageConfig
217- -> PackageLocationIndex FilePath
218- -> CabalWarnings
219- -> RIO env BS. ByteString
220- -> RIO env Package
221- readPackageBS packageConfig loc cw getBS =
222- cachedCabalFileParse loc cw getBS >>=
223- either (throwM . PackageInvalidCabalFile loc) (return . resolvePackage packageConfig)
224-
225224-- | Get 'GenericPackageDescription' and 'PackageDescription' reading info
226225-- from given directory.
227226readPackageDescriptionDir
0 commit comments