Skip to content

Commit

Permalink
Merge pull request #6281 from commercialhaskell/fix6280
Browse files Browse the repository at this point in the history
Fix #6280 Simpler hash for pre-compiled package cache keys
  • Loading branch information
mpilgrem authored Oct 8, 2023
2 parents 1f34d3b + 9a35ba3 commit d5d49f0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

Release notes:

* The hash used as a key for Stack's pre-compiled package cache has changed,
following the dropping of support for Cabal versions older than `1.24.0.0`.

**Changes since v2.13.1:**

Major changes:
Expand Down
70 changes: 34 additions & 36 deletions src/Stack/Build/Cache.hs
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,13 @@ getTestStatus dir = do
--
-- We only pay attention to non-directory options. We don't want to avoid a
-- cache hit just because it was installed in a different directory.
getPrecompiledCacheKey :: HasEnvConfig env
=> PackageLocationImmutable
-> ConfigureOpts
-> Bool -- ^ build haddocks
-> Set GhcPkgId -- ^ dependencies
-> RIO env PrecompiledCacheKey
getPrecompiledCacheKey loc copts buildHaddocks installedPackageIDs = do
getPrecompiledCacheKey ::
HasEnvConfig env
=> PackageLocationImmutable
-> ConfigureOpts
-> Bool -- ^ build haddocks
-> RIO env PrecompiledCacheKey
getPrecompiledCacheKey loc copts buildHaddocks = do
compiler <- view actualCompilerVersionL
cabalVersion <- view cabalVersionL

Expand All @@ -359,28 +359,27 @@ getPrecompiledCacheKey loc copts buildHaddocks installedPackageIDs = do
platformGhcDir <- platformGhcRelDir

-- In Cabal versions 1.22 and later, the configure options contain the
-- installed package IDs, which is what we need for a unique hash.
-- Unfortunately, earlier Cabals don't have the information, so we must
-- supplement it with the installed package IDs directly.
-- See issue: https://github.com/commercialhaskell/stack/issues/1103
let input = (coNoDirs copts, installedPackageIDs)
-- installed package IDs, which is what we need for a unique hash. See also
-- issue: https://github.com/commercialhaskell/stack/issues/1103
let input = coNoDirs copts
optionsHash = Mem.convert $ hashWith SHA256 $ encodeUtf8 $ tshow input

pure $ precompiledCacheKey platformGhcDir compiler cabalVersion packageKey optionsHash buildHaddocks
pure $ precompiledCacheKey
platformGhcDir compiler cabalVersion packageKey optionsHash buildHaddocks

-- | Write out information about a newly built package
writePrecompiledCache :: HasEnvConfig env
=> BaseConfigOpts
-> PackageLocationImmutable
-> ConfigureOpts
-> Bool -- ^ build haddocks
-> Set GhcPkgId -- ^ dependencies
-> Installed -- ^ library
-> [GhcPkgId] -- ^ sublibraries, in the GhcPkgId format
-> Set Text -- ^ executables
-> RIO env ()
writePrecompiledCache baseConfigOpts loc copts buildHaddocks depIDs mghcPkgId sublibs exes = do
key <- getPrecompiledCacheKey loc copts buildHaddocks depIDs
writePrecompiledCache ::
HasEnvConfig env
=> BaseConfigOpts
-> PackageLocationImmutable
-> ConfigureOpts
-> Bool -- ^ build haddocks
-> Installed -- ^ library
-> [GhcPkgId] -- ^ sublibraries, in the GhcPkgId format
-> Set Text -- ^ executables
-> RIO env ()
writePrecompiledCache baseConfigOpts loc copts buildHaddocks mghcPkgId sublibs exes = do
key <- getPrecompiledCacheKey loc copts buildHaddocks
ec <- view envConfigL
let stackRootRelative = makeRelative (view stackRootL ec)
mlibpath <- case mghcPkgId of
Expand All @@ -399,23 +398,22 @@ writePrecompiledCache baseConfigOpts loc copts buildHaddocks depIDs mghcPkgId su
-- reuse precompiled cache with haddocks also in case when haddocks are not
-- required
when buildHaddocks $ do
key' <- getPrecompiledCacheKey loc copts False depIDs
key' <- getPrecompiledCacheKey loc copts False
savePrecompiledCache key' precompiled
where
pathFromPkgId stackRootRelative ipid = do
ipid' <- parseRelFile $ ghcPkgIdString ipid ++ ".conf"
stackRootRelative $ bcoSnapDB baseConfigOpts </> ipid'

-- | Check the cache for a precompiled package matching the given
-- configuration.
readPrecompiledCache :: forall env. HasEnvConfig env
=> PackageLocationImmutable -- ^ target package
-> ConfigureOpts
-> Bool -- ^ build haddocks
-> Set GhcPkgId -- ^ dependencies
-> RIO env (Maybe (PrecompiledCache Abs))
readPrecompiledCache loc copts buildHaddocks depIDs = do
key <- getPrecompiledCacheKey loc copts buildHaddocks depIDs
-- | Check the cache for a precompiled package matching the given configuration.
readPrecompiledCache ::
forall env. HasEnvConfig env
=> PackageLocationImmutable -- ^ target package
-> ConfigureOpts
-> Bool -- ^ build haddocks
-> RIO env (Maybe (PrecompiledCache Abs))
readPrecompiledCache loc copts buildHaddocks = do
key <- getPrecompiledCacheKey loc copts buildHaddocks
mcache <- loadPrecompiledCache key
maybe (pure Nothing) (fmap Just . mkAbs) mcache
where
Expand Down
2 changes: 0 additions & 2 deletions src/Stack/Build/Execute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,6 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap
loc
(configCacheOpts cache)
(configCacheHaddock cache)
(configCacheDeps cache)
case mpc of
Nothing -> pure Nothing
-- Only pay attention to precompiled caches that refer to packages
Expand Down Expand Up @@ -2071,7 +2070,6 @@ singleBuild ac@ActionContext {..} ee@ExecuteEnv {..} task@Task {..} installedMap
loc
(configCacheOpts cache)
(configCacheHaddock cache)
(configCacheDeps cache)
mpkgid sublibsPkgIds (packageExes package)
_ -> pure ()

Expand Down

0 comments on commit d5d49f0

Please sign in to comment.