Skip to content

Commit e42c272

Browse files
committed
Refactor Stack.Build.ConstructPlan.addDep
Refactors existing `addDep` to break it up into its three main steps: (1) check if the package name is in the lib map; (2) check if the package name is already in the call stack or the package name is not in the combined map; and (3) the core of the function.
1 parent 23bd28a commit e42c272

File tree

1 file changed

+80
-62
lines changed

1 file changed

+80
-62
lines changed

src/Stack/Build/ConstructPlan.hs

Lines changed: 80 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,13 @@ constructPlan baseConfigOpts0 localDumpPkgs loadPackage0 sourceMap installedMap
306306
inSourceMap pname = pname `Map.member` smDeps sourceMap ||
307307
pname `Map.member` smProject sourceMap
308308

309+
getSources :: Version -> RIO env (Map PackageName PackageSource)
309310
getSources globalCabalVersion = do
310311
let loadLocalPackage' pp = do
311312
lp <- loadLocalPackage pp
312-
pure lp { lpPackage = applyForceCustomBuild globalCabalVersion $ lpPackage lp }
313+
let lpPackage' =
314+
applyForceCustomBuild globalCabalVersion $ lpPackage lp
315+
pure lp { lpPackage = lpPackage' }
313316
pPackages <- for (smProject sourceMap) $ \pp -> do
314317
lp <- loadLocalPackage' pp
315318
pure $ PSFilePath lp
@@ -506,7 +509,7 @@ addFinal lp package isAllInOne buildHaddocks = do
506509
tell mempty { wFinals = Map.singleton (packageName package) res }
507510

508511
-- | Given a 'PackageName', adds all of the build tasks to build the package, if
509-
-- needed.
512+
-- needed. First checks if the package name is in the lib map.
510513
--
511514
-- 'constructPlan' invokes this on all the target packages, setting
512515
-- @treatAsDep'@ to False, because those packages are direct build targets.
@@ -515,73 +518,88 @@ addFinal lp package isAllInOne buildHaddocks = do
515518
-- marked as a dependency, even if it is directly wanted. This makes sense - if
516519
-- we left out packages that are deps, it would break the --only-dependencies
517520
-- build plan.
518-
addDep :: PackageName
519-
-> M (Either ConstructPlanException AddDepRes)
521+
addDep :: PackageName -> M (Either ConstructPlanException AddDepRes)
520522
addDep name = do
521-
ctx <- ask
522-
m <- get
523-
case Map.lookup name m of
523+
libMap <- get
524+
case Map.lookup name libMap of
524525
Just res -> do
525526
planDebug $
526527
"addDep: Using cached result for " ++ show name ++ ": " ++ show res
527528
pure res
528-
Nothing -> do
529-
res <- if name `elem` callStack ctx
530-
then do
531-
planDebug $
532-
"addDep: Detected cycle "
533-
<> show name
534-
<> ": "
535-
<> show (callStack ctx)
536-
pure $ Left $ DependencyCycleDetected $ name : callStack ctx
537-
else local (\ctx' -> ctx' { callStack = name : callStack ctx' }) $ do
538-
let mpackageInfo = Map.lookup name $ combinedMap ctx
529+
Nothing -> addDep' name
530+
531+
-- | Given a 'PackageName', adds all of the build tasks to build the package.
532+
-- First checks that the package name is not already in the call stack.
533+
addDep' :: PackageName -> M (Either ConstructPlanException AddDepRes)
534+
addDep' name = do
535+
ctx <- ask
536+
let mpackageInfo = Map.lookup name $ combinedMap ctx
537+
res <- if name `elem` callStack ctx
538+
then do
539+
planDebug $
540+
"addDep': Detected cycle "
541+
<> show name
542+
<> ": "
543+
<> show (callStack ctx)
544+
pure $ Left $ DependencyCycleDetected $ name : callStack ctx
545+
else local (\ctx' -> ctx' { callStack = name : callStack ctx' }) $ do
546+
case mpackageInfo of
547+
-- TODO look up in the package index and see if there's a
548+
-- recommendation available
549+
Nothing -> do
539550
planDebug $
540-
"addDep: Package info for "
551+
"addDep': No package info for "
541552
<> show name
542-
<> ": "
543-
<> show mpackageInfo
544-
case mpackageInfo of
545-
-- TODO look up in the package index and see if there's a
546-
-- recommendation available
547-
Nothing -> pure $ Left $ UnknownPackage name
548-
Just (PIOnlyInstalled loc installed) -> do
549-
-- FIXME Slightly hacky, no flags since they likely won't affect
550-
-- executable names. This code does not feel right.
551-
let version = installedVersion installed
552-
askPkgLoc = liftRIO $ do
553-
mrev <- getLatestHackageRevision
554-
YesRequireHackageIndex name version
555-
case mrev of
556-
Nothing -> do
557-
-- this could happen for GHC boot libraries missing from
558-
-- Hackage
559-
prettyWarnL
560-
$ flow "No latest package revision found for"
561-
: style Current (fromString $ packageNameString name) <> ","
562-
: flow "dependency callstack:"
563-
: mkNarrativeList
564-
Nothing
565-
False
566-
( map
567-
(fromString . packageNameString)
568-
(callStack ctx)
569-
:: [StyleDoc]
570-
)
571-
pure Nothing
572-
Just (_rev, cfKey, treeKey) ->
573-
pure . Just $
574-
PLIHackage (PackageIdentifier name version) cfKey treeKey
575-
tellExecutablesUpstream name askPkgLoc loc Map.empty
576-
pure $ Right $ ADRFound loc installed
577-
Just (PIOnlySource ps) -> do
578-
tellExecutables name ps
579-
installPackage name ps Nothing
580-
Just (PIBoth ps installed) -> do
581-
tellExecutables name ps
582-
installPackage name ps (Just installed)
583-
updateLibMap name res
584-
pure res
553+
pure $ Left $ UnknownPackage name
554+
Just packageInfo -> addDep'' name packageInfo
555+
updateLibMap name res
556+
pure res
557+
558+
-- | Given a 'PackageName' and its 'PackageInfo' from the combined map, adds all
559+
-- of the build tasks to build the package. Assumes that the head of the call
560+
-- stack is the current package name.
561+
addDep'' ::
562+
PackageName
563+
-> PackageInfo
564+
-> M (Either ConstructPlanException AddDepRes)
565+
addDep'' name packageInfo = do
566+
planDebug $
567+
"addDep'': Package info for "
568+
<> show name
569+
<> ": "
570+
<> show packageInfo
571+
case packageInfo of
572+
PIOnlyInstalled loc installed -> do
573+
-- FIXME Slightly hacky, no flags since they likely won't affect
574+
-- executable names. This code does not feel right.
575+
let version = installedVersion installed
576+
askPkgLoc = liftRIO $ do
577+
mrev <- getLatestHackageRevision YesRequireHackageIndex name version
578+
case mrev of
579+
Nothing -> do
580+
-- This could happen for GHC boot libraries missing from
581+
-- Hackage.
582+
cs <- asks (L.tail . callStack)
583+
prettyWarnL
584+
$ flow "No latest package revision found for"
585+
: style Current (fromString $ packageNameString name) <> ","
586+
: flow "dependency callstack:"
587+
: mkNarrativeList
588+
Nothing
589+
False
590+
(map (fromString . packageNameString) cs :: [StyleDoc])
591+
pure Nothing
592+
Just (_rev, cfKey, treeKey) ->
593+
pure . Just $
594+
PLIHackage (PackageIdentifier name version) cfKey treeKey
595+
tellExecutablesUpstream name askPkgLoc loc Map.empty
596+
pure $ Right $ ADRFound loc installed
597+
PIOnlySource ps -> do
598+
tellExecutables name ps
599+
installPackage name ps Nothing
600+
PIBoth ps installed -> do
601+
tellExecutables name ps
602+
installPackage name ps (Just installed)
585603

586604
-- FIXME what's the purpose of this? Add a Haddock!
587605
tellExecutables :: PackageName -> PackageSource -> M ()

0 commit comments

Comments
 (0)