@@ -9,7 +9,6 @@ module Stack.Build.ConstructPlan
9
9
) where
10
10
11
11
import Control.Monad.RWS.Strict hiding ( (<>) )
12
- import Control.Monad.State.Strict ( execState )
13
12
import qualified Data.List as L
14
13
import qualified Data.Map.Strict as M
15
14
import qualified Data.Map.Strict as Map
@@ -21,6 +20,7 @@ import Distribution.Types.PackageName ( mkPackageName )
21
20
import Generics.Deriving.Monoid ( memptydefault , mappenddefault )
22
21
import Path ( parent )
23
22
import RIO.Process ( HasProcessContext (.. ), findExecutable )
23
+ import RIO.State ( State , execState )
24
24
import Stack.Build.Cache ( tryGetFlagCache )
25
25
import Stack.Build.Haddock ( shouldHaddockDeps )
26
26
import Stack.Build.Source ( loadLocalPackage )
@@ -363,7 +363,7 @@ data UnregisterState = UnregisterState
363
363
}
364
364
365
365
-- | Determine which packages to unregister based on the given tasks and
366
- -- already registered local packages
366
+ -- already registered local packages.
367
367
mkUnregisterLocal ::
368
368
Map PackageName Task
369
369
-- ^ Tasks
@@ -376,12 +376,18 @@ mkUnregisterLocal ::
376
376
-- unregister target packages.
377
377
-> Map GhcPkgId (PackageIdentifier , Text )
378
378
mkUnregisterLocal tasks dirtyReason localDumpPkgs initialBuildSteps =
379
- -- We'll take multiple passes through the local packages. This
380
- -- will allow us to detect that a package should be unregistered,
381
- -- as well as all packages directly or transitively depending on
382
- -- it.
379
+ -- We'll take multiple passes through the local packages. This will allow us
380
+ -- to detect that a package should be unregistered, as well as all packages
381
+ -- directly or transitively depending on it.
383
382
loop Map. empty localDumpPkgs
384
383
where
384
+ loop ::
385
+ Map GhcPkgId (PackageIdentifier , Text )
386
+ -- ^ Current local packages to unregister.
387
+ -> [DumpPackage ]
388
+ -- ^ Current local packages to keep.
389
+ -> Map GhcPkgId (PackageIdentifier , Text )
390
+ -- ^ Revised local packages to unregister.
385
391
loop toUnregister keep
386
392
-- If any new packages were added to the unregister Map, we need to loop
387
393
-- through the remaining packages again to detect if a transitive dependency
@@ -393,43 +399,71 @@ mkUnregisterLocal tasks dirtyReason localDumpPkgs initialBuildSteps =
393
399
where
394
400
-- Run the unregister checking function on all packages we currently think
395
401
-- we'll be keeping.
396
- us = execState (mapM_ go keep) UnregisterState
402
+ us = execState (mapM_ go keep) initialUnregisterState
403
+ initialUnregisterState = UnregisterState
397
404
{ usToUnregister = toUnregister
398
405
, usKeep = []
399
406
, usAnyAdded = False
400
407
}
401
408
409
+ go :: DumpPackage -> State UnregisterState ()
402
410
go dp = do
403
411
us <- get
404
- case go' (usToUnregister us) ident deps of
405
- -- Not unregistering, add it to the keep list
412
+ case maybeUnregisterReason (usToUnregister us) ident mParentLibId deps of
413
+ -- Not unregistering, add it to the keep list.
406
414
Nothing -> put us { usKeep = dp : usKeep us }
407
- -- Unregistering, add it to the unregister Map and indicate that a package
408
- -- was in fact added to the unregister Map so we loop again.
415
+ -- Unregistering, add it to the unregister Map; and indicate that a
416
+ -- package was in fact added to the unregister Map, so we loop again.
409
417
Just reason -> put us
410
418
{ usToUnregister = Map. insert gid (ident, reason) (usToUnregister us)
411
419
, usAnyAdded = True
412
420
}
413
421
where
414
422
gid = dpGhcPkgId dp
415
423
ident = dpPackageIdent dp
424
+ mParentLibId = dpParentLibIdent dp
416
425
deps = dpDepends dp
417
426
418
- go' toUnregister ident deps
419
- -- If we're planning on running a task on it, then it must be unregistered,
420
- -- unless it's a target and an initial-build-steps build is being done.
421
- | Just task <- Map. lookup name tasks
422
- = if initialBuildSteps && taskIsTarget task && taskProvides task == ident
423
- then Nothing
424
- else Just $ fromMaybe " " $ Map. lookup name dirtyReason
427
+ maybeUnregisterReason ::
428
+ Map GhcPkgId (PackageIdentifier , Text )
429
+ -- ^ Current local packages to unregister.
430
+ -> PackageIdentifier
431
+ -- ^ Package identifier.
432
+ -> Maybe PackageIdentifier
433
+ -- ^ If package for sub library, package identifier of the parent.
434
+ -> [GhcPkgId ]
435
+ -- ^ Dependencies of the package.
436
+ -> Maybe Text
437
+ -- ^ If to be unregistered, the reason for doing so.
438
+ maybeUnregisterReason toUnregister ident mParentLibId deps
439
+ -- If the package is not for a sub library, then it is directly relevant. If
440
+ -- it is, then the relevant package is the parent. If we are planning on
441
+ -- running a task on the relevant package, then the package must be
442
+ -- unregistered, unless it is a target and an initial-build-steps build is
443
+ -- being done.
444
+ | Just task <- Map. lookup relevantPkgName tasks =
445
+ if initialBuildSteps
446
+ && taskIsTarget task
447
+ && taskProvides task == relevantPkgId
448
+ then Nothing
449
+ else Just $ fromMaybe " " $ Map. lookup relevantPkgName dirtyReason
425
450
-- Check if a dependency is going to be unregistered
426
- | (dep, _): _ <- mapMaybe (`Map.lookup` toUnregister) deps
427
- = Just $ " Dependency being unregistered: " <> T. pack (packageIdentifierString dep)
451
+ | (dep, _): _ <- mapMaybe (`Map.lookup` toUnregister) deps =
452
+ Just $ " Dependency being unregistered: "
453
+ <> T. pack (packageIdentifierString dep)
428
454
-- None of the above, keep it!
429
455
| otherwise = Nothing
430
456
where
431
- name :: PackageName
432
- name = pkgName ident
457
+ -- If the package is not for a sub library, then the relevant package
458
+ -- identifier is that of the package. If it is, then the relevant package
459
+ -- identifier is that of the parent.
460
+ relevantPkgId :: PackageIdentifier
461
+ relevantPkgId = fromMaybe ident mParentLibId
462
+ -- If the package is not for a sub library, then the relevant package name
463
+ -- is that of the package. If it is, then the relevant package name is
464
+ -- that of the parent.
465
+ relevantPkgName :: PackageName
466
+ relevantPkgName = maybe (pkgName ident) pkgName mParentLibId
433
467
434
468
-- | Given a 'LocalPackage' and its 'lpTestBench', adds a 'Task' for running its
435
469
-- tests and benchmarks.
0 commit comments