Debian Codegraph @

src/GHC/Utils/GhcPkg/Main/Compat.hs

raw
1 {-# LANGUAGE NoImplicitPrelude #-}
2 {-# LANGUAGE DataKinds #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE GADTs #-}
5 {-# LANGUAGE KindSignatures #-}
6 {-# LANGUAGE LambdaCase #-}
7 {-# LANGUAGE MultiParamTypeClasses #-}
8 {-# LANGUAGE OverloadedRecordDot #-}
9 {-# LANGUAGE OverloadedStrings #-}
10 {-# LANGUAGE ScopedTypeVariables #-}
11 {-# LANGUAGE TupleSections #-}
12
13 {-|
14 Module : GHC.Utils.GhcPkg.Main.Compat
15 License : BSD-3-Clause
16
17 This module is based on GHC's utils\ghc-pkg\Main.hs at
18 commit f66fc15f2e6849125074bcfeb44334a663323ca6 (see GHC merge request !11142),
19 with:
20
21 * changeDBDir' does not perform an effective @ghc-pkg recache@,
22 * the cache is not used,
23 * consistency checks are not performed,
24 * use Stack program name,
25 * use "Stack.Prelude" rather than "Prelude",
26 * use t'RIO' @env@ monad,
27 * use well-typed representations of paths from the @path@ package,
28 * add pretty messages and exceptions,
29 * redundant code deleted,
30 * Hlint applied, and
31 * explicit import lists.
32
33 The version of the ghc-pkg executable supplied with GHCs published before
34 28 August 2023 does not efficiently bulk unregister. This module exports a
35 function that does efficiently bulk unregister.
36 -}
37
38 module GHC.Utils.GhcPkg.Main.Compat
39 ( ghcPkgUnregisterForce
40 ) where
41
42 -----------------------------------------------------------------------------
43 --
44 -- (c) The University of Glasgow 2004-2009.
45 --
46 -- Package management tool
47 --
48 -----------------------------------------------------------------------------
49
50 import qualified Data.Foldable as F
51 import qualified Data.Traversable as F
52 import Distribution.InstalledPackageInfo as Cabal
53 import Distribution.Package ( UnitId, mungedId )
54 import qualified Distribution.Parsec as Cabal
55 import Distribution.Text ( display )
56 import Distribution.Version ( nullVersion )
57 import GHC.IO.Exception (IOErrorType(InappropriateType))
58 import qualified GHC.Unit.Database as GhcPkg
59 import Path
60 ( SomeBase (..), fileExtension, mapSomeBase, parseRelFile
61 , parseSomeDir, prjSomeBase
62 )
63 import qualified Path as P
64 import Path.IO
65 ( createDirIfMissing, doesDirExist, listDir, removeFile )
66 import qualified RIO.ByteString as BS
67 import RIO.List ( isPrefixOf, stripSuffix )
68 import RIO.NonEmpty ( nonEmpty )
69 import qualified RIO.NonEmpty as NE
70 import Stack.Constants ( relFilePackageCache )
71 import Stack.Prelude hiding ( display )
72 import Stack.Types.GhcPkgExe
73 ( GhcPkgPrettyException (..), GlobPackageIdentifier (..)
74 , PackageArg (..)
75 )
76 import System.Environment ( getEnv )
77 import System.FilePath as FilePath
78 import System.IO ( readFile )
79 import System.IO.Error
80 ( ioeGetErrorType, ioError, isDoesNotExistError )
81
82 -- | Function equivalent to:
83 --
84 -- > ghc-pkg --no-user-package-db --package-db=<pkgDb> unregister [--ipid] <P>
85 --
86 ghcPkgUnregisterForce ::
87 HasTerm env
88 => Path Abs Dir -- ^ Path to the global package database
89 -> Path Abs Dir -- ^ Path to the package database
90 -> Bool -- ^ Apply ghc-pkg's --ipid, --unit-id flag?
91 -> [String] -- ^ Packages to unregister
92 -> RIO env ()
93 ghcPkgUnregisterForce globalDb pkgDb hasIpid pkgarg_strs = do
94 pkgargs <- forM pkgarg_strs $ readPackageArg as_arg
95 prettyDebugL
96 $ flow "Unregistering from"
97 : (pretty pkgDb <> ":")
98 : mkNarrativeList (Just Current) False
99 (map (fromString . show) pkgargs :: [StyleDoc])
100 unregisterPackages globalDb pkgargs pkgDb
101 where
102 as_arg = if hasIpid then AsUnitId else AsDefault
103
104 -- -----------------------------------------------------------------------------
105 -- Do the business
106
107 -- | Enum flag representing argument type
108 data AsPackageArg
109 = AsUnitId
110 | AsDefault
111
112 parseCheck :: Cabal.Parsec a => String -> String -> RIO env a
113 parseCheck str what =
114 case Cabal.eitherParsec str of
115 Left e -> prettyThrowIO $ CannotParse str what e
116 Right x -> pure x
117
118 readGlobPkgId :: String -> RIO env GlobPackageIdentifier
119 readGlobPkgId str = case stripSuffix "-*" str of
120 Nothing ->
121 ExactPackageIdentifier <$> parseCheck str "package identifier (exact)"
122 Just str' ->
123 GlobPackageIdentifier <$> parseCheck str' "package identifier (glob)"
124
125 readPackageArg :: AsPackageArg -> String -> RIO env PackageArg
126 readPackageArg AsUnitId str = IUId <$> parseCheck str "installed package id"
127 readPackageArg AsDefault str = Id <$> readGlobPkgId str
128
129 -- -----------------------------------------------------------------------------
130 -- Package databases
131
132 data PackageDB (mode :: GhcPkg.DbMode) = PackageDB
133 { location :: !(SomeBase Dir)
134 -- We only need possibly-relative package db location. The relative
135 -- location is used as an identifier for the db, so it is important we do
136 -- not modify it.
137 , packageDbLock :: !(GhcPkg.DbOpenMode mode GhcPkg.PackageDbLock)
138 -- If package db is open in read write mode, we keep its lock around for
139 -- transactional updates.
140 , packages :: [InstalledPackageInfo]
141 }
142
143 -- | A stack of package databases. Convention: head is the topmost in the stack.
144 type PackageDBStack = [PackageDB 'GhcPkg.DbReadOnly]
145
146 -- | Selector for picking the right package DB to modify as \'modify\' changes
147 -- the first database that contains a specific package.
148 newtype DbModifySelector = ContainsPkg PackageArg
149
150 getPkgDatabases ::
151 forall env. HasTerm env
152 => Path Abs Dir
153 -- ^ Path to the global package database.
154 -> PackageArg
155 -> Path Abs Dir
156 -- ^ Path to the package database.
157 -> RIO
158 env
159 ( PackageDBStack
160 -- the real package DB stack: [global,user] ++ DBs specified on the
161 -- command line with -f.
162 , GhcPkg.DbOpenMode GhcPkg.DbReadWrite (PackageDB GhcPkg.DbReadWrite)
163 -- which one to modify, if any
164 , PackageDBStack
165 -- the package DBs specified on the command line, or [global,user]
166 -- otherwise. This is used as the list of package DBs for commands
167 -- that just read the DB, such as 'list'.
168 )
169 getPkgDatabases globalDb pkgarg pkgDb = do
170 -- Second we determine the location of the global package config. On Windows,
171 -- this is found relative to the ghc-pkg.exe binary, whereas on Unix the
172 -- location is passed to the binary using the --global-package-db flag by the
173 -- wrapper script.
174 let sys_databases = [Abs globalDb]
175 e_pkg_path <- tryIO (liftIO $ System.Environment.getEnv "GHC_PACKAGE_PATH")
176 let env_stack =
177 case nonEmpty <$> e_pkg_path of
178 Left _ -> sys_databases
179 Right Nothing -> []
180 Right (Just path)
181 | isSearchPathSeparator (NE.last path)
182 -> mapMaybe parseSomeDir (splitSearchPath (NE.init path)) <> sys_databases
183 | otherwise
184 -> mapMaybe parseSomeDir (splitSearchPath $ NE.toList path)
185
186 -- -f flags on the command line add to the database stack, unless any of them
187 -- are present in the stack already.
188 let final_stack = [Abs pkgDb | Abs pkgDb `notElem` env_stack] <> env_stack
189
190 (db_stack, db_to_operate_on) <- getDatabases pkgDb final_stack
191
192 let flag_db_stack = [ db | db <- db_stack, db.location == Abs pkgDb ]
193
194 prettyDebugL
195 $ flow "Db stack:"
196 : map (pretty . (.location)) db_stack
197 F.forM_ db_to_operate_on $ \db ->
198 prettyDebugL
199 [ "Modifying:"
200 , pretty db.location
201 ]
202 prettyDebugL
203 $ flow "Flag db stack:"
204 : map (pretty . (.location)) flag_db_stack
205
206 pure (db_stack, db_to_operate_on, flag_db_stack)
207 where
208 getDatabases flag_db_name final_stack = do
209 -- The package db we open in read write mode is the first one included in
210 -- flag_db_names that contains specified package. Therefore we need to
211 -- open each one in read/write mode first and decide whether it's for
212 -- modification based on its contents.
213 (db_stack, mto_modify) <- stateSequence Nothing
214 [ \case
215 to_modify@(Just _) -> (, to_modify) <$> readDatabase db_path
216 Nothing -> if db_path /= Abs flag_db_name
217 then (, Nothing) <$> readDatabase db_path
218 else do
219 let hasPkg :: PackageDB mode -> Bool
220 hasPkg = not . null . findPackage pkgarg . (.packages)
221
222 openRo (e::IOException) = do
223 db <- readDatabase db_path
224 if hasPkg db
225 then
226 prettyThrowIO $ CannotOpenDBForModification db_path e
227 else pure (db, Nothing)
228
229 -- If we fail to open the database in read/write mode, we need
230 -- to check if it's for modification first before throwing an
231 -- error, so we attempt to open it in read only mode.
232 handle openRo $ do
233 db <- readParseDatabase
234 (GhcPkg.DbOpenReadWrite $ ContainsPkg pkgarg) db_path
235 let ro_db = db { packageDbLock = GhcPkg.DbOpenReadOnly }
236 if hasPkg db
237 then pure (ro_db, Just db)
238 else do
239 -- If the database is not for modification after all,
240 -- drop the write lock as we are already finished with
241 -- the database.
242 case db.packageDbLock of
243 GhcPkg.DbOpenReadWrite lock ->
244 liftIO $ GhcPkg.unlockPackageDb lock
245 pure (ro_db, Nothing)
246 | db_path <- final_stack ]
247
248 to_modify <- case mto_modify of
249 Just db -> pure db
250 Nothing -> cannotFindPackage pkgarg Nothing
251
252 pure (db_stack, GhcPkg.DbOpenReadWrite to_modify)
253 where
254 -- Parse package db in read-only mode.
255 readDatabase :: SomeBase Dir -> RIO env (PackageDB 'GhcPkg.DbReadOnly)
256 readDatabase = readParseDatabase GhcPkg.DbOpenReadOnly
257
258 stateSequence :: Monad m => s -> [s -> m (a, s)] -> m ([a], s)
259 stateSequence s [] = pure ([], s)
260 stateSequence s (m:ms) = do
261 (a, s') <- m s
262 (as, s'') <- stateSequence s' ms
263 pure (a : as, s'')
264
265 readParseDatabase ::
266 forall mode t env. HasTerm env
267 => GhcPkg.DbOpenMode mode t
268 -> SomeBase Dir
269 -> RIO env (PackageDB mode)
270 readParseDatabase mode path = do
271 e <- tryIO $ prjSomeBase listDir path
272 case e of
273 Left err
274 | ioeGetErrorType err == InappropriateType -> do
275 -- We provide a limited degree of backwards compatibility for
276 -- old single-file style db:
277 mdb <- tryReadParseOldFileStyleDatabase mode path
278 case mdb of
279 Just db -> pure db
280 Nothing -> prettyThrowIO $ SingleFileDBUnsupported path
281
282 | otherwise -> liftIO $ ioError err
283 Right (_, fs) -> ignore_cache
284 where
285 confs = filter isConf fs
286
287 isConf :: Path Abs File -> Bool
288 isConf f = case fileExtension f of
289 Nothing -> False
290 Just ext -> ext == ".conf"
291
292 ignore_cache :: RIO env (PackageDB mode)
293 ignore_cache = do
294 -- If we're opening for modification, we need to acquire a lock even if
295 -- we don't open the cache now, because we are going to modify it later.
296 lock <- liftIO $
297 F.mapM (const $ GhcPkg.lockPackageDb (prjSomeBase toFilePath cache)) mode
298 pkgs <- mapM parseSingletonPackageConf confs
299 mkPackageDB pkgs lock
300 where
301 cache = mapSomeBase (P.</> relFilePackageCache) path
302
303 mkPackageDB ::
304 [InstalledPackageInfo]
305 -> GhcPkg.DbOpenMode mode GhcPkg.PackageDbLock
306 -> RIO env (PackageDB mode)
307 mkPackageDB pkgs lock =
308 pure PackageDB
309 { location = path
310 , packageDbLock = lock
311 , packages = pkgs
312 }
313
314 parseSingletonPackageConf ::
315 HasTerm env
316 => Path Abs File
317 -> RIO env InstalledPackageInfo
318 parseSingletonPackageConf file = do
319 prettyDebugL
320 [ flow "Reading package config:"
321 , pretty file
322 ]
323 BS.readFile (toFilePath file) >>= fmap fst . parsePackageInfo
324
325 -- -----------------------------------------------------------------------------
326 -- Workaround for old single-file style package dbs
327
328 -- Single-file style package dbs have been deprecated for some time, but
329 -- it turns out that Cabal was using them in one place. So this code is for a
330 -- workaround to allow older Cabal versions to use this newer ghc.
331
332 -- We check if the file db contains just "[]" and if so, we look for a new
333 -- dir-style db in path.d/, ie in a dir next to the given file.
334 -- We cannot just replace the file with a new dir style since Cabal still
335 -- assumes it's a file and tries to overwrite with 'writeFile'.
336
337 -- ghc itself also cooperates in this workaround
338
339 tryReadParseOldFileStyleDatabase ::
340 HasTerm env
341 => GhcPkg.DbOpenMode mode t
342 -> SomeBase Dir
343 -> RIO env (Maybe (PackageDB mode))
344 tryReadParseOldFileStyleDatabase mode path = do
345 -- assumes we've already established that path exists and is not a dir
346 content <- liftIO $ readFile (prjSomeBase toFilePath path) `catchIO` \_ -> pure ""
347 if take 2 content == "[]"
348 then do
349 path_dir <- adjustOldDatabasePath path
350 prettyWarnL
351 [ flow "Ignoring old file-style db and trying"
352 , pretty path_dir
353 ]
354 direxists <- prjSomeBase doesDirExist path_dir
355 if direxists
356 then do
357 db <- readParseDatabase mode path_dir
358 -- but pretend it was at the original location
359 pure $ Just db { location = path }
360 else do
361 lock <- F.forM mode $ \_ -> do
362 prjSomeBase (createDirIfMissing True) path_dir
363 liftIO $ GhcPkg.lockPackageDb $
364 prjSomeBase (toFilePath . (P.</> relFilePackageCache)) path_dir
365 pure $ Just PackageDB
366 { location = path
367 , packageDbLock = lock
368 , packages = []
369 }
370
371 -- if the path is not a file, or is not an empty db then we fail
372 else pure Nothing
373
374 adjustOldFileStylePackageDB :: PackageDB mode -> RIO env (PackageDB mode)
375 adjustOldFileStylePackageDB db = do
376 -- assumes we have not yet established if it's an old style or not
377 mcontent <- liftIO $
378 fmap Just (readFile (prjSomeBase toFilePath db.location)) `catchIO` \_ -> pure Nothing
379 case fmap (take 2) mcontent of
380 -- it is an old style and empty db, so look for a dir kind in location.d/
381 Just "[]" -> do
382 adjustedDatabasePath <- adjustOldDatabasePath db.location
383 pure db { location = adjustedDatabasePath }
384 -- it is old style but not empty, we have to bail
385 Just _ -> prettyThrowIO $ SingleFileDBUnsupported db.location
386 -- probably not old style, carry on as normal
387 Nothing -> pure db
388
389 adjustOldDatabasePath :: SomeBase Dir -> RIO env (SomeBase Dir)
390 adjustOldDatabasePath = prjSomeBase addDToDirName
391 where
392 addDToDirName dir = do
393 let dirNameWithD = toFilePath dir <> ".d"
394 maybe
395 (prettyThrowIO $ CannotParseDirectoryWithDBug dirNameWithD)
396 pure
397 (parseSomeDir dirNameWithD)
398
399 parsePackageInfo :: BS.ByteString -> RIO env (InstalledPackageInfo, [String])
400 parsePackageInfo str =
401 case parseInstalledPackageInfo str of
402 Right (warnings, ok) -> pure (mungePackageInfo ok, ws)
403 where
404 ws = [ msg | msg <- warnings
405 , not ("Unrecognized field pkgroot" `isPrefixOf` msg) ]
406 Left err -> prettyThrowIO $ ParsePackageInfoExceptions (unlines (F.toList err))
407
408 mungePackageInfo :: InstalledPackageInfo -> InstalledPackageInfo
409 mungePackageInfo ipi = ipi
410
411 -- -----------------------------------------------------------------------------
412 -- Making changes to a package database
413
414 newtype DBOp = RemovePackage InstalledPackageInfo
415
416 changeNewDB ::
417 HasTerm env
418 => [DBOp]
419 -> PackageDB 'GhcPkg.DbReadWrite
420 -> RIO env ()
421 changeNewDB cmds new_db = do
422 new_db' <- adjustOldFileStylePackageDB new_db
423 prjSomeBase (createDirIfMissing True) new_db'.location
424 changeDBDir' cmds new_db'
425
426 changeDBDir' ::
427 HasTerm env
428 => [DBOp]
429 -> PackageDB 'GhcPkg.DbReadWrite
430 -> RIO env ()
431 changeDBDir' cmds db = do
432 mapM_ do_cmd cmds
433 case db.packageDbLock of
434 GhcPkg.DbOpenReadWrite lock -> liftIO $ GhcPkg.unlockPackageDb lock
435 where
436 do_cmd (RemovePackage p) = do
437 let relFileConfName = display (installedUnitId p) <> ".conf"
438 relFileConf <- maybe
439 (prettyThrowIO $ CannotParseRelFileBug relFileConfName)
440 pure
441 (parseRelFile relFileConfName)
442 let file = mapSomeBase (P.</> relFileConf) db.location
443 prettyDebugL
444 [ "Removing"
445 , pretty file
446 ]
447 removeFileSafe file
448
449 unregisterPackages ::
450 forall env. HasTerm env
451 => Path Abs Dir
452 -- ^ Path to the global package database.
453 -> [PackageArg]
454 -> Path Abs Dir
455 -- ^ Path to the package database.
456 -> RIO env ()
457 unregisterPackages globalDb pkgargs pkgDb = do
458 pkgsByPkgDBs <- F.foldlM (getPkgsByPkgDBs []) [] pkgargs
459 forM_ pkgsByPkgDBs unregisterPackages'
460 where
461 -- Update a list of 'packages by package database' for a package. Assumes that
462 -- a package to be unregistered is in no more than one database.
463 getPkgsByPkgDBs ::
464 [(PackageDB GhcPkg.DbReadWrite, [UnitId])]
465 -- ^ List of considered 'packages by package database'
466 -> [(PackageDB GhcPkg.DbReadWrite, [UnitId])]
467 -- ^ List of to be considered 'packages by package database'
468 -> PackageArg
469 -- Package to update
470 -> RIO env [(PackageDB GhcPkg.DbReadWrite, [UnitId])]
471 -- No more 'packages by package database' to consider? We need to try to get
472 -- another package database.
473 getPkgsByPkgDBs pkgsByPkgDBs [] pkgarg =
474 getPkgDatabases globalDb pkgarg pkgDb >>= \case
475 (_, GhcPkg.DbOpenReadWrite (db :: PackageDB GhcPkg.DbReadWrite), _) -> do
476 pks <- do
477 let pkgs = db.packages
478 ps = findPackage pkgarg pkgs
479 -- This shouldn't happen if getPkgsByPkgDBs picks the DB correctly.
480 when (null ps) $ cannotFindPackage pkgarg $ Just db
481 pure (map installedUnitId ps)
482 let pkgsByPkgDB = (db, pks)
483 pure (pkgsByPkgDB : pkgsByPkgDBs)
484 -- Consider the next 'packages by package database' in the list of ones to
485 -- consider.
486 getPkgsByPkgDBs pkgsByPkgDBs ( pkgsByPkgDB : pkgsByPkgDBs') pkgarg = do
487 let (db, pks') = pkgsByPkgDB
488 pkgs = db.packages
489 ps = findPackage pkgarg pkgs
490 pks = map installedUnitId ps
491 pkgByPkgDB' = (db, pks <> pks')
492 if null ps
493 then
494 -- Not found in the package database? Add the package database to those
495 -- considered and try with the remaining package databases to consider.
496 getPkgsByPkgDBs ( pkgsByPkgDB : pkgsByPkgDBs ) pkgsByPkgDBs' pkgarg
497 else
498 -- Found in the package database? Add to the list of packages to be
499 -- unregistered from that package database. TO DO: Perhaps check not
500 -- already in that list for better error messages when there are
501 -- duplicated requests to unregister.
502 pure (pkgsByPkgDBs <> (pkgByPkgDB' : pkgsByPkgDBs'))
503
504 unregisterPackages' :: (PackageDB GhcPkg.DbReadWrite, [UnitId]) -> RIO env ()
505 unregisterPackages' (db, pks) = do
506 let pkgs = db.packages
507 cmds = [ RemovePackage pkg
508 | pkg <- pkgs, installedUnitId pkg `elem` pks
509 ]
510 new_db = db{ packages = pkgs' }
511 where
512 deleteFirstsBy' :: (a -> b -> Bool) -> [a] -> [b] -> [a]
513 deleteFirstsBy' eq = foldl' (deleteBy' eq)
514
515 deleteBy' :: (a -> b -> Bool) -> [a] -> b -> [a]
516 deleteBy' _ [] _ = []
517 deleteBy' eq (y:ys) x = if y `eq` x then ys else y : deleteBy' eq ys x
518
519 pkgs' = deleteFirstsBy' (\p1 p2 -> installedUnitId p1 == p2) pkgs pks
520 -- Use changeNewDB, rather than changeDB, to avoid duplicating
521 -- updateInternalDB db cmds
522 changeNewDB cmds new_db
523
524 findPackage :: PackageArg -> [InstalledPackageInfo] -> [InstalledPackageInfo]
525 findPackage pkgarg = filter (pkgarg `matchesPkg`)
526
527 cannotFindPackage :: PackageArg -> Maybe (PackageDB mode) -> RIO env a
528 cannotFindPackage pkgarg mdb =
529 prettyThrowIO $ CannotFindPackage pkgarg ((.location) <$> mdb)
530
531 matches :: GlobPackageIdentifier -> MungedPackageId -> Bool
532 GlobPackageIdentifier pn `matches` pid' = pn == mungedName pid'
533 ExactPackageIdentifier pid `matches` pid' =
534 mungedName pid == mungedName pid'
535 && ( mungedVersion pid == mungedVersion pid'
536 || mungedVersion pid == nullVersion
537 )
538
539 matchesPkg :: PackageArg -> InstalledPackageInfo -> Bool
540 (Id pid) `matchesPkg` pkg = pid `matches` mungedId pkg
541 (IUId ipid) `matchesPkg` pkg = ipid == installedUnitId pkg
542 (Substring _ m) `matchesPkg` pkg = m (display (mungedId pkg))
543
544 -- removeFileSave doesn't throw an exceptions, if the file is already deleted
545 removeFileSafe :: SomeBase File -> RIO env ()
546 removeFileSafe fn = do
547 prjSomeBase removeFile fn `catchIO` \ e ->
548 unless (isDoesNotExistError e) $ liftIO $ ioError e