Skip to content

Commit b50005a

Browse files
committed
wip
1 parent 9621945 commit b50005a

File tree

8 files changed

+87
-22
lines changed

8 files changed

+87
-22
lines changed

hie.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cradle:
2+
stack:
3+
- path: "."
4+
component: "stack:lib"

src/Stack/Build/ConstructPlan.hs

+22-18
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import Stack.Types.Compiler
4343
import Stack.Types.Config
4444
import Stack.Types.GhcPkgId
4545
import Stack.Types.NamedComponent
46+
import Stack.Types.PackageComponent (libraryPackage)
4647
import Stack.Types.Package
4748
import Stack.Types.SourceMap
4849
import Stack.Types.Version
@@ -90,26 +91,29 @@ data AddDepRes
9091

9192
type ParentMap = MonoidMap PackageName (First Version, [(PackageIdentifier, VersionRange)])
9293

93-
data W = W
94-
{ wFinals :: !(Map PackageName (Either ConstructPlanException Task))
95-
, wInstall :: !(Map Text InstallLocation)
94+
data PlanDraft = PlanDraft
95+
{ pdFinals :: !(Map PackageName (Either ConstructPlanException Task))
96+
, pdInstall :: !(Map Text InstallLocation)
9697
-- ^ executable to be installed, and location where the binary is placed
97-
, wDirty :: !(Map PackageName Text)
98+
, pdDirty :: !(Map PackageName Text)
9899
-- ^ why a local package is considered dirty
99-
, wWarnings :: !([Text] -> [Text])
100+
, pdWarning :: !([Text] -> [Text])
100101
-- ^ Warnings
101-
, wParents :: !ParentMap
102+
, pdParents :: !ParentMap
102103
-- ^ Which packages a given package depends on, along with the package's version
103104
} deriving Generic
104-
instance Semigroup W where
105+
instance Semigroup PlanDraft where
105106
(<>) = mappenddefault
106-
instance Monoid W where
107+
instance Monoid PlanDraft where
107108
mempty = memptydefault
108109
mappend = (<>)
109110

111+
-- | A monad transformer adding reading an environment of type 'Ctx',
112+
-- collecting an output of type 'PlanDraft' and updating a state of type
113+
-- '(Map PackageName (Either ConstructPlanException AddDepRes))' to an inner monad 'IO'.
110114
type M = RWST -- TODO replace with more efficient WS stack on top of StackT
111115
Ctx
112-
W
116+
PlanDraft
113117
(Map PackageName (Either ConstructPlanException AddDepRes))
114118
IO
115119

@@ -188,7 +192,7 @@ constructPlan baseConfigOpts0 localDumpPkgs loadPackage0 sourceMap installedMap
188192
let inner = mapM_ onTarget $ Map.keys (smtTargets $ smTargets sourceMap)
189193
pathEnvVar' <- liftIO $ maybe mempty T.pack <$> lookupEnv "PATH"
190194
let ctx = mkCtx econfig globalCabalVersion sources mcur pathEnvVar'
191-
((), m, W efinals installExes dirtyReason warnings parents) <-
195+
((), m, PlanDraft efinals installExes dirtyReason warnings parents) <-
192196
liftIO $ runRWST inner ctx M.empty
193197
mapM_ (logWarn . RIO.display) (warnings [])
194198
let toEither (_, Left e) = Left e
@@ -200,7 +204,7 @@ constructPlan baseConfigOpts0 localDumpPkgs loadPackage0 sourceMap installedMap
200204
then do
201205
let toTask (_, ADRFound _ _) = Nothing
202206
toTask (name, ADRToInstall task) = Just (name, task)
203-
tasks = M.fromList $ mapMaybe toTask adrs
207+
tasks = first libraryPackage $ M.fromList $ mapMaybe toTask adrs
204208
takeSubset =
205209
case boptsCLIBuildSubset $ bcoBuildOptsCLI baseConfigOpts0 of
206210
BSAll -> pure
@@ -412,7 +416,7 @@ addFinal lp package isAllInOne buildHaddocks = do
412416
, taskAnyMissing = not $ Set.null missing
413417
, taskBuildTypeConfig = packageBuildTypeConfig package
414418
}
415-
tell mempty { wFinals = Map.singleton (packageName package) res }
419+
tell mempty { pdFinals = Map.singleton (packageName package) res }
416420

417421
-- | Given a 'PackageName', adds all of the build tasks to build the
418422
-- package, if needed.
@@ -513,7 +517,7 @@ tellExecutablesPackage loc p = do
513517
| otherwise = Set.empty
514518
goSource PSRemote{} = Set.empty
515519

516-
tell mempty { wInstall = Map.fromList $ map (, loc) $ Set.toList $ filterComps myComps $ packageExes p }
520+
tell mempty { pdInstall = Map.fromList $ map (, loc) $ Set.toList $ filterComps myComps $ packageExes p }
517521
where
518522
filterComps myComps x
519523
| Set.null myComps = x
@@ -618,7 +622,7 @@ installPackageGivenDeps isAllInOne buildHaddocks ps package minstalled (missing,
618622
return $ if shouldInstall then Nothing else Just installed
619623
(Just _, False) -> do
620624
let t = T.intercalate ", " $ map (T.pack . packageNameString . pkgName) (Set.toList missing)
621-
tell mempty { wDirty = Map.singleton name $ "missing dependencies: " <> addEllipsis t }
625+
tell mempty { pdDirty = Map.singleton name $ "missing dependencies: " <> addEllipsis t }
622626
return Nothing
623627
(Nothing, _) -> return Nothing
624628
let loc = psLocation ps
@@ -716,7 +720,7 @@ addPackageDeps package = do
716720
then return True
717721
else do
718722
let warn_ reason =
719-
tell mempty { wWarnings = (msg:) }
723+
tell mempty { pdWarning = (msg:) }
720724
where
721725
msg = T.concat
722726
[ "WARNING: Ignoring "
@@ -771,7 +775,7 @@ addPackageDeps package = do
771775
adrVersion (ADRFound _ installed) = installedVersion installed
772776
-- Update the parents map, for later use in plan construction errors
773777
-- - see 'getShortestDepsPath'.
774-
addParent depname range mversion = tell mempty { wParents = MonoidMap $ M.singleton depname val }
778+
addParent depname range mversion = tell mempty { pdParents = MonoidMap $ M.singleton depname val }
775779
where
776780
val = (First mversion, [(packageIdentifier package, range)])
777781

@@ -837,7 +841,7 @@ checkDirtiness ps installed package present buildHaddocks = do
837841
case mreason of
838842
Nothing -> return False
839843
Just reason -> do
840-
tell mempty { wDirty = Map.singleton (packageName package) reason }
844+
tell mempty { pdDirty = Map.singleton (packageName package) reason }
841845
return True
842846

843847
describeConfigDiff :: Config -> ConfigCache -> ConfigCache -> Maybe Text
@@ -935,7 +939,7 @@ packageDepsWithTools p = do
935939
case mfound of
936940
Left _ -> return $ Just $ ToolWarning name (packageName p)
937941
Right _ -> return Nothing
938-
tell mempty { wWarnings = (map toolWarningText warnings ++) }
942+
tell mempty { pdWarning = (map toolWarningText warnings ++) }
939943
return $ packageDeps p
940944

941945
-- | Warn about tools in the snapshot definition. States the tool name

src/Stack/Build/Execute.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import Stack.Types.Compiler
8282
import Stack.Types.Config
8383
import Stack.Types.GhcPkgId
8484
import Stack.Types.NamedComponent
85+
import Stack.Types.PackageComponent (forgetComponentName, PackageComponentName, getPackageNameLength)
8586
import Stack.Types.Package
8687
import Stack.Types.Version
8788
import qualified System.Directory as D
@@ -500,7 +501,7 @@ executePlan boptsCli baseConfigOpts locals globalPackages snapshotPackages local
500501
where
501502
mlargestPackageName =
502503
Set.lookupMax $
503-
Set.map (length . packageNameString) $
504+
Set.map getPackageNameLength $
504505
Map.keysSet (planTasks plan) <> Map.keysSet (planFinals plan)
505506

506507
copyExecutables
@@ -647,7 +648,7 @@ executePlan' installedMap0 targets plan ee@ExecuteEnv {..} = do
647648
when (boptsOpenHaddocks eeBuildOpts) $ do
648649
let planPkgs, localPkgs, installedPkgs, availablePkgs
649650
:: Map PackageName (PackageIdentifier, InstallLocation)
650-
planPkgs = Map.map (taskProvides &&& taskLocation) (planTasks plan)
651+
planPkgs = Map.mapKeys forgetComponentName $ Map.map (taskProvides &&& taskLocation) (planTasks plan)
651652
localPkgs =
652653
Map.fromList
653654
[(packageName p, (packageIdentifier p, Local)) | p <- map lpPackage eeLocals]

src/Stack/Types/Build.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import Stack.Types.Config
7070
import Stack.Types.GhcPkgId
7171
import Stack.Types.NamedComponent
7272
import Stack.Types.Package
73+
import Stack.Types.PackageComponent (PackageComponentName)
7374
import Stack.Types.Version
7475
import System.FilePath (pathSeparator)
7576
import RIO.Process (showProcessArgDebug)
@@ -513,8 +514,8 @@ installLocationIsMutable Local = Mutable
513514

514515
-- | A complete plan of what needs to be built and how to do it
515516
data Plan = Plan
516-
{ planTasks :: !(Map PackageName Task)
517-
, planFinals :: !(Map PackageName Task)
517+
{ planTasks :: !(Map PackageComponentName Task)
518+
, planFinals :: !(Map PackageComponentName Task)
518519
-- ^ Final actions to be taken (test, benchmark, etc)
519520
, planUnregisterLocal :: !(Map GhcPkgId (PackageIdentifier, Text))
520521
-- ^ Text is reason we're unregistering, for display only

src/Stack/Types/NamedComponent.hs

+8
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ import qualified Data.Set as Set
2222
import qualified Data.Text as T
2323

2424
-- | A single, fully resolved component of a package
25+
-- This type is Cabal based, and follows the .cabal files
26+
-- possibilities.
27+
-- These options are build targets.
2528
data NamedComponent
2629
= CLib
30+
-- ^ The default library in a haskell project.
2731
| CInternalLib !Text
32+
-- ^ An optional (named) internal library.
2833
| CExe !Text
34+
-- ^ An executable.
2935
| CTest !Text
36+
-- ^ A test target.
3037
| CBench !Text
38+
-- ^ A benchmark target.
3139
deriving (Show, Eq, Ord)
3240

3341
renderComponent :: NamedComponent -> Text

src/Stack/Types/PackageComponent.hs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
{-# LANGUAGE DeriveFunctor #-}
3+
{-# LANGUAGE DeriveGeneric #-}
4+
{-# LANGUAGE DeriveDataTypeable #-}
5+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+
{-# LANGUAGE OverloadedStrings #-}
7+
{-# LANGUAGE RankNTypes #-}
8+
{-# LANGUAGE FlexibleContexts #-}
9+
{-# LANGUAGE DataKinds #-}
10+
{-# LANGUAGE ConstraintKinds #-}
11+
12+
module Stack.Types.PackageComponent where
13+
14+
import Stack.Prelude
15+
import Stack.Types.NamedComponent (NamedComponent(CLib))
16+
17+
-- | Required for component-addressed build plans.
18+
-- Before introducing this, most packages were 'PackageName' addressed.
19+
-- See <https://github.com/commercialhaskell/stack/issues/4745 this issue>
20+
-- for more details.
21+
data PackageComponentName = PackageComponentName {
22+
packageName :: !PackageName,
23+
componentName :: !NamedComponent
24+
} deriving (Eq, Show, Ord)
25+
26+
-- | This is needed for computing the largest packageName length in a BuildPlan.
27+
-- See <../Build/Execute.hs#504 this file>
28+
getPackageNameLength :: PackageComponentName -> Int
29+
getPackageNameLength PackageComponentName{packageName=pn} = length . packageNameString $ pn
30+
31+
-- | This is the main case for most packages, you only depend on their default library.
32+
libraryPackage :: PackageName -> PackageComponentName
33+
libraryPackage pckName = PackageComponentName {
34+
packageName = pckName,
35+
componentName = CLib
36+
}
37+
38+
forgetComponentName :: PackageComponentName -> PackageName
39+
forgetComponentName = packageName

stack.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ library
218218
other-modules:
219219
Path.Extended
220220
Stack.Types.Cache
221+
Stack.Types.PackageComponent
221222
autogen-modules:
222223
Paths_stack
223224
hs-source-dirs:

stack.code-workspace

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
]
7+
}

0 commit comments

Comments
 (0)