@@ -43,6 +43,7 @@ import qualified Data.List.NonEmpty as NE
4343import qualified Data.Map.Strict as Map
4444import Data.Maybe
4545import Data.Proxy
46+
4647import qualified Data.Text as T
4748import Data.Time.Clock
4849import Data.Version
@@ -238,7 +239,7 @@ data SessionLoadingOptions = SessionLoadingOptions
238239 -- | Given the project name and a set of command line flags,
239240 -- return the path for storing generated GHC artifacts,
240241 -- or 'Nothing' to respect the cradle setting
241- , getCacheDirs :: String -> [String ] -> IO CacheDirs
242+ , getCacheDirs :: String -> Maybe B. ByteString -> [String ] -> IO CacheDirs
242243 -- | Return the GHC lib dir to use for the 'unsafeGlobalDynFlags'
243244 , getInitialGhcLibDir :: Recorder (WithPriority Log ) -> FilePath -> IO (Maybe LibDir )
244245 }
@@ -512,7 +513,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
512513 -- We will modify the unitId and DynFlags used for
513514 -- compilation but these are the true source of
514515 -- information.
515- new_deps = fmap (\ (df, targets) -> RawComponentInfo (homeUnitId_ df) df targets cfp opts dep_info) newTargetDfs
516+ new_deps = fmap (\ (df, targets,mHash ) -> RawComponentInfo (homeUnitId_ df) df targets cfp opts dep_info mHash ) newTargetDfs
516517 all_deps = new_deps `NE.appendList` fromMaybe [] oldDeps
517518 -- Get all the unit-ids for things in this component
518519 _inplace = map rawComponentUnitId $ NE. toList all_deps
@@ -521,7 +522,7 @@ loadSessionWithOptions recorder SessionLoadingOptions{..} rootDir que = do
521522 let prefix = show rawComponentUnitId
522523 -- See Note [Avoiding bad interface files]
523524 let cacheDirOpts = componentOptions opts
524- cacheDirs <- liftIO $ getCacheDirs prefix cacheDirOpts
525+ cacheDirs <- liftIO $ getCacheDirs prefix rawComponentHash cacheDirOpts
525526 processed_df <- setCacheDirs recorder cacheDirs rawComponentDynFlags
526527 -- The final component information, mostly the same but the DynFlags don't
527528 -- contain any packages which are also loaded
@@ -1026,6 +1027,9 @@ data RawComponentInfo = RawComponentInfo
10261027 -- | Maps cradle dependencies, such as `stack.yaml`, or `.cabal` file
10271028 -- to last modification time. See Note [Multi Cradle Dependency Info].
10281029 , rawComponentDependencyInfo :: DependencyInfo
1030+ -- | The initial hash generated for a unit id , if UnitId is "main" then Just B.ByteString else Nothing
1031+ , rawComponentHash :: Maybe B. ByteString
1032+
10291033 }
10301034
10311035-- This is processed information about the component, in particular the dynflags will be modified.
@@ -1121,13 +1125,13 @@ setOptions :: GhcMonad m
11211125 -> ComponentOptions
11221126 -> DynFlags
11231127 -> FilePath -- ^ root dir, see Note [Root Directory]
1124- -> m (NonEmpty (DynFlags , [GHC. Target ]))
1128+ -> m (NonEmpty (DynFlags , [GHC. Target ], Maybe B. ByteString ))
11251129setOptions haddockOpt cfp (ComponentOptions theOpts compRoot _) dflags rootDir = do
11261130 ((theOpts',_errs,_warns),units) <- processCmdLineP unit_flags [] (map noLoc theOpts)
11271131 case NE. nonEmpty units of
11281132 Just us -> initMulti us
11291133 Nothing -> do
1130- (df, targets) <- initOne (map unLoc theOpts')
1134+ (df, targets, mHash ) <- initOne (map unLoc theOpts')
11311135 -- A special target for the file which caused this wonderful
11321136 -- component to be created. In case the cradle doesn't list all the targets for
11331137 -- the component, in which case things will be horribly broken anyway.
@@ -1147,7 +1151,7 @@ setOptions haddockOpt cfp (ComponentOptions theOpts compRoot _) dflags rootDir =
11471151 -- we will report it as an error for that file
11481152 let abs_fp = toAbsolute rootDir (fromNormalizedFilePath cfp)
11491153 let special_target = Compat. mkSimpleTarget df abs_fp
1150- pure $ (df, special_target : targets) :| []
1154+ pure $ (df, special_target : targets,mHash ) :| []
11511155 where
11521156 initMulti unitArgFiles =
11531157 forM unitArgFiles $ \ f -> do
@@ -1158,7 +1162,7 @@ setOptions haddockOpt cfp (ComponentOptions theOpts compRoot _) dflags rootDir =
11581162 initOne $ HieBios. removeRTS $ HieBios. removeVerbosityOpts args
11591163 initOne this_opts = do
11601164 (dflags', targets') <- addCmdOpts this_opts dflags
1161- let dflags'' =
1165+ let ( dflags'',mHash) =
11621166 case unitIdString (homeUnitId_ dflags') of
11631167 -- cabal uses main for the unit id of all executable packages
11641168 -- This makes multi-component sessions confused about what
@@ -1167,10 +1171,11 @@ setOptions haddockOpt cfp (ComponentOptions theOpts compRoot _) dflags rootDir =
11671171 -- This works because there won't be any dependencies on the
11681172 -- executable unit.
11691173 " main" ->
1170- let hash = B. unpack $ B16. encode $ H. finalize $ H. updates H. init (map B. pack this_opts)
1174+ let hashBytes = H. finalize $ H. updates H. init (map B. pack this_opts)
1175+ hash = B. unpack $ B16. encode hashBytes
11711176 hashed_uid = Compat. toUnitId (Compat. stringToUnit (" main-" ++ hash))
1172- in setHomeUnitId_ hashed_uid dflags'
1173- _ -> dflags'
1177+ in ( setHomeUnitId_ hashed_uid dflags', Just hashBytes)
1178+ _ -> ( dflags' , Nothing )
11741179
11751180 let targets = makeTargetsAbsolute root targets'
11761181 root = case workingDirectory dflags'' of
@@ -1191,7 +1196,7 @@ setOptions haddockOpt cfp (ComponentOptions theOpts compRoot _) dflags rootDir =
11911196 Compat. setUpTypedHoles $
11921197 makeDynFlagsAbsolute compRoot -- makeDynFlagsAbsolute already accounts for workingDirectory
11931198 dflags''
1194- return (dflags''', targets)
1199+ return (dflags''', targets,mHash )
11951200
11961201setIgnoreInterfacePragmas :: DynFlags -> DynFlags
11971202setIgnoreInterfacePragmas df =
@@ -1218,14 +1223,22 @@ setODir f d =
12181223 -- override user settings to avoid conflicts leading to recompilation
12191224 d { objectDir = Just f}
12201225
1221- getCacheDirsDefault :: String -> [String ] -> IO CacheDirs
1222- getCacheDirsDefault prefix opts = do
1223- dir <- Just <$> getXdgDirectory XdgCache (cacheDir </> prefix ++ " -" ++ opts_hash)
1226+
1227+
1228+ getCacheDirsDefault :: String -> Maybe B. ByteString -> [String ] -> IO CacheDirs
1229+ getCacheDirsDefault prefix mFirstHash opts = do
1230+ dir <- Just <$> getXdgDirectory XdgCache (cacheDir </> prefix' ++ " -" ++ opts_hash)
12241231 return $ CacheDirs dir dir dir
12251232 where
1233+
1234+ prefix' = if isJust mFirstHash then " main" else prefix
1235+ basectx = case mFirstHash of
1236+ Just h -> H. updates H. init [h]
1237+ Nothing -> H. init
12261238 -- Create a unique folder per set of different GHC options, assuming that each different set of
12271239 -- GHC options will create incompatible interface files.
1228- opts_hash = B. unpack $ B16. encode $ H. finalize $ H. updates H. init (map B. pack opts)
1240+ --
1241+ opts_hash = B. unpack $ B16. encode $ H. finalize $ H. updates basectx (map B. pack opts)
12291242
12301243-- | Sub directory for the cache path
12311244cacheDir :: String
0 commit comments