[Git][ghc/ghc][master] hadrian: Remove old package.conf files when generating new ones
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 5ac9ce7d by Zubin Duggal at 2026-06-10T21:26:32+05:30 hadrian: Remove old package.conf files when generating new ones Old package.conf files might exists with different hashes, causing issues like #26661 Fixes #26661 - - - - - 2 changed files: - + changelog.d/hadrian-stale-package-confs-26661 - hadrian/src/Hadrian/Haskell/Cabal/Parse.hs Changes: ===================================== changelog.d/hadrian-stale-package-confs-26661 ===================================== @@ -0,0 +1,6 @@ +section: packaging +synopsis: Hadrian no longer leaves stale `.conf` files in its package databases + when rebuilding in the same build root with different settings (e.g. another + flavour, or when hashes change with +hash-unit-ids). +issues: #26661 +mrs: !15186 ===================================== hadrian/src/Hadrian/Haskell/Cabal/Parse.hs ===================================== @@ -15,6 +15,7 @@ module Hadrian.Haskell.Cabal.Parse ( ) where import Data.Bifunctor +import Data.Char (isDigit) import Data.List.Extra import Development.Shake import qualified Distribution.Compat.Graph as Graph @@ -55,6 +56,8 @@ import Builder import Context import Settings import Distribution.Simple.LocalBuildInfo +import Distribution.Types.LocalBuildInfo (allTargetsInBuildOrder') +import Distribution.Types.TargetInfo (TargetInfo (..)) import qualified Distribution.Simple.Register as C import System.Directory (getCurrentDirectory) import qualified Distribution.InstalledPackageInfo as CP @@ -394,35 +397,48 @@ registerPackage rs context = do -- Note: the @cPath@ is ignored. The path that's used is the 'buildDir' path -- from the local build info @lbi@. lbi <- liftIO $ C.getPersistBuildConfig Nothing (C.makeSymbolicPath cPath) - liftIO $ register db_path pid pd lbi + -- This runs `ghc --abi-hash`, so do it outside the critical section below. + installedPkgInfo <- liftIO $ generateRegistrationInfo pd lbi + + let pkg_name = pkgName (package context) + -- Is this a pkg.conf for a previous build? + -- we want to match "ghc-9.15.1-abcd.conf" but not "ghc-boot-9.15.1.conf" + isPkgConf f = case stripPrefix (pkg_name ++ "-") (takeBaseName f) of + Just (c:_) -> isDigit c + _ -> takeBaseName f == pkg_name + + -- Unlike `ghc-pkg update/register` (used to populate the inplace and stage0 + -- databases), writing the .conf file directly doesn't remove units this + -- package was previously registered under. Stale .conf files from earlier + -- builds make this package's modules ambiguous (#26661), so delete them + -- before writing the new .conf file. + withResources rs $ do + confs <- liftIO $ getDirectoryFilesIO db_path ["*.conf"] + mapM_ (removeFile . (db_path </>)) + [ f | f <- confs, isPkgConf f, takeBaseName f /= pid ] + liftIO $ writeUTF8File (db_path </> pid <.> "conf") + (CP.showInstalledPackageInfo installedPkgInfo) -- Then after the register, which just writes the .conf file, do the recache step. buildWithResources rs $ target context (GhcPkg Recache (stage context)) [] [] -- This is copied and simplified from Cabal, because we want to install the package -- into a different package database to the one it was configured against. -register :: FilePath - -> String -- ^ Package Identifier - -> C.PackageDescription - -> LocalBuildInfo - -> IO () -register pkg_db pid pd lbi - = withLibLBI pd lbi $ \lib clbi -> do - - when reloc $ error "register does not support reloc" - installedPkgInfo <- generateRegistrationInfo pd lbi lib clbi - writeRegistrationFile installedPkgInfo - - where - regFile = pkg_db </> pid <.> "conf" - reloc = relocatable lbi - - generateRegistrationInfo pkg lbi lib clbi = do - abi_hash <- C.mkAbiHash <$> GHC.libAbiHash C.silent pkg lbi lib clbi - return (C.absoluteInstalledPackageInfo pkg abi_hash lib lbi clbi) - - writeRegistrationFile installedPkgInfo = do - writeUTF8File regFile (CP.showInstalledPackageInfo installedPkgInfo) +-- See generateRegistrationInfo in Distribution.Simple.Register. we can't use it +-- directly because it computes the abi-hash using Cabal's internal package +-- database, which hadrian never creates. +generateRegistrationInfo :: C.PackageDescription + -> LocalBuildInfo + -> IO Installed.InstalledPackageInfo +generateRegistrationInfo pd lbi = do + when (relocatable lbi) $ error "register does not support reloc" + case [ (lib, targetCLBI tgt) | tgt <- allTargetsInBuildOrder' pd lbi + , CLib lib <- [targetComponent tgt] ] of + [(lib, clbi)] -> do + abi_hash <- C.mkAbiHash <$> GHC.libAbiHash C.silent pd lbi lib clbi + return (C.absoluteInstalledPackageInfo pd abi_hash lib lbi clbi) + libs -> error $ "generateRegistrationInfo: expected exactly one library for " + ++ C.display (C.package pd) ++ ", got " ++ show (length libs) -- | Build autogenerated files @autogen/cabal_macros.h@ and @autogen/Paths_*.hs@. View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/5ac9ce7d3821c75e2d2cc17fae00b9e8... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/5ac9ce7d3821c75e2d2cc17fae00b9e8... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Marge Bot (@marge-bot)