Skip to content

Commit 26833f8

Browse files
authored
Merge pull request #6168 from commercialhaskell/fix6167
Fix #6167 Allow plan debug info from the command line
2 parents 78187fa + 0b2b50b commit 26833f8

File tree

6 files changed

+79
-43
lines changed

6 files changed

+79
-43
lines changed

ChangeLog.md

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Other enhancements:
2525
[#6123](https://github.com/commercialhaskell/stack/pull/6123).
2626
* Add composable component type flags `--exes`, `--tests` and `--benchmarks` to
2727
Stack's `ide targets` command, to list only those components.
28+
* `stack --verbose` excludes lengthy information about build plan construction
29+
in the debug output by default. The new `stack --[no-]plan-in-log` flag
30+
enables or disables the inclusion of the information in the debug output.
2831

2932
Bug fixes:
3033

doc/global_flags.md

+10
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ Stack can be configured to integrate with Nix. For further information, see
160160
Pass the flag `--numeric-version` to cause Stack to report its numeric version
161161
to standard output (e.g. `2.9.1`) and quit.
162162

163+
## `--[no-]plan-in-log` flag
164+
165+
:octicons-tag-24: UNRELEASED
166+
167+
Default: Disabled
168+
169+
Enables/disables the logging of build plan construction in debug output.
170+
Information about the build plan construction can be lengthy. If you do not need
171+
it, it is best omitted from the debug output.
172+
163173
## `--resolver` option
164174

165175
Pass the option `--resolver <snapshot>` to specify the snapshot. For further

src/Stack/Build/ConstructPlan.hs

+55-43
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import Stack.Types.EnvConfig
5757
import Stack.Types.EnvSettings ( EnvSettings (..), minimalEnvSettings )
5858
import Stack.Types.GHCVariant ( HasGHCVariant (..) )
5959
import Stack.Types.GhcPkgId ( GhcPkgId )
60+
import Stack.Types.GlobalOpts ( GlobalOpts (..) )
6061
import Stack.Types.IsMutable ( IsMutable (..) )
6162
import Stack.Types.NamedComponent ( exeComponents, renderComponent )
6263
import Stack.Types.Package
@@ -67,15 +68,14 @@ import Stack.Types.Package
6768
)
6869
import Stack.Types.ParentMap ( ParentMap )
6970
import Stack.Types.Platform ( HasPlatform (..) )
70-
import Stack.Types.Runner ( HasRunner (..) )
71+
import Stack.Types.Runner ( HasRunner (..), globalOptsL )
7172
import Stack.Types.SourceMap
7273
( CommonPackage (..), DepPackage (..), FromSnapshot (..)
7374
, GlobalPackage (..), SMTargets (..), SourceMap (..)
7475
)
7576
import Stack.Types.Version
7677
( latestApplicableVersion, versionRangeText, withinRange )
7778
import System.Environment ( lookupEnv )
78-
import System.IO ( putStrLn )
7979

8080
data PackageInfo
8181
= PIOnlyInstalled InstallLocation Installed
@@ -260,7 +260,7 @@ constructPlan baseConfigOpts0 localDumpPkgs loadPackage0 sourceMap installedMap
260260
sources <- getSources globalCabalVersion
261261
mcur <- view $ buildConfigL.to bcCurator
262262

263-
let onTarget = void . addDep
263+
let onTarget = void . getCachedDepOrAddDep
264264
let inner = mapM_ onTarget $ Map.keys (smtTargets $ smTargets sourceMap)
265265
pathEnvVar' <- liftIO $ maybe mempty T.pack <$> lookupEnv "PATH"
266266
let ctx = mkCtx econfig globalCabalVersion sources mcur pathEnvVar'
@@ -296,7 +296,6 @@ constructPlan baseConfigOpts0 localDumpPkgs loadPackage0 sourceMap installedMap
296296
else Map.empty
297297
}
298298
else do
299-
planDebug $ show errs
300299
stackYaml <- view stackYamlL
301300
stackRoot <- view stackRootL
302301
prettyThrowM $ ConstructPlanFailed
@@ -539,56 +538,64 @@ addFinal lp package isAllInOne buildHaddocks = do
539538
-- marked as a dependency, even if it is directly wanted. This makes sense - if
540539
-- we left out packages that are deps, it would break the --only-dependencies
541540
-- build plan.
542-
addDep :: PackageName -> M (Either ConstructPlanException AddDepRes)
543-
addDep name = do
541+
getCachedDepOrAddDep ::
542+
PackageName
543+
-> M (Either ConstructPlanException AddDepRes)
544+
getCachedDepOrAddDep name = do
544545
libMap <- get
545546
case Map.lookup name libMap of
546547
Just res -> do
547-
planDebug $
548-
"addDep: Using cached result for " ++ show name ++ ": " ++ show res
548+
logDebugPlanS "getCachedDepOrAddDep" $
549+
"Using cached result for "
550+
<> fromString (packageNameString name)
551+
<> ": "
552+
<> fromString (show res)
549553
pure res
550-
Nothing -> addDep' name
554+
Nothing -> checkCallStackAndAddDep name
551555

552556
-- | Given a 'PackageName', adds all of the build tasks to build the package.
553557
-- First checks that the package name is not already in the call stack.
554-
addDep' :: PackageName -> M (Either ConstructPlanException AddDepRes)
555-
addDep' name = do
558+
checkCallStackAndAddDep ::
559+
PackageName
560+
-> M (Either ConstructPlanException AddDepRes)
561+
checkCallStackAndAddDep name = do
556562
ctx <- ask
557563
let mpackageInfo = Map.lookup name $ combinedMap ctx
558564
res <- if name `elem` callStack ctx
559565
then do
560-
planDebug $
561-
"addDep': Detected cycle "
562-
<> show name
566+
logDebugPlanS "checkCallStackAndAddDep" $
567+
"Detected cycle "
568+
<> fromString (packageNameString name)
563569
<> ": "
564-
<> show (callStack ctx)
570+
<> fromString (show $ map packageNameString (callStack ctx))
565571
pure $ Left $ DependencyCycleDetected $ name : callStack ctx
566572
else local (\ctx' -> ctx' { callStack = name : callStack ctx' }) $ do
567573
case mpackageInfo of
568574
-- TODO look up in the package index and see if there's a
569575
-- recommendation available
570576
Nothing -> do
571-
planDebug $
572-
"addDep': No package info for "
573-
<> show name
577+
logDebugPlanS "checkCallStackAndAddDep" $
578+
"No package info for "
579+
<> fromString (packageNameString name)
580+
<> "."
574581
pure $ Left $ UnknownPackage name
575-
Just packageInfo -> addDep'' name packageInfo
582+
Just packageInfo -> addDep name packageInfo
576583
updateLibMap name res
577584
pure res
578585

579586
-- | Given a 'PackageName' and its 'PackageInfo' from the combined map, adds all
580587
-- of the build tasks to build the package. Assumes that the head of the call
581588
-- stack is the current package name.
582-
addDep'' ::
589+
addDep ::
583590
PackageName
584591
-> PackageInfo
585592
-> M (Either ConstructPlanException AddDepRes)
586-
addDep'' name packageInfo = do
587-
planDebug $
588-
"addDep'': Package info for "
589-
<> show name
593+
addDep name packageInfo = do
594+
logDebugPlanS "addDep" $
595+
"Package info for "
596+
<> fromString (packageNameString name)
590597
<> ": "
591-
<> show packageInfo
598+
<> fromString (show packageInfo)
592599
case packageInfo of
593600
PIOnlyInstalled loc installed -> do
594601
-- FIXME Slightly hacky, no flags since they likely won't affect
@@ -678,18 +685,19 @@ installPackage name ps minstalled = do
678685
ctx <- ask
679686
case ps of
680687
PSRemote pkgLoc _version _fromSnapshot cp -> do
681-
planDebug $
682-
"installPackage: Doing all-in-one build for upstream package "
683-
<> show name
688+
logDebugPlanS "installPackage" $
689+
"Doing all-in-one build for upstream package "
690+
<> fromString (packageNameString name)
691+
<> "."
684692
package <- loadPackage
685693
ctx pkgLoc (cpFlags cp) (cpGhcOptions cp) (cpCabalConfigOpts cp)
686694
resolveDepsAndInstall True (cpHaddocks cp) ps package minstalled
687695
PSFilePath lp -> do
688696
case lpTestBench lp of
689697
Nothing -> do
690-
planDebug $
691-
"installPackage: No test / bench component for "
692-
<> show name
698+
logDebugPlanS "installPackage" $
699+
"No test or bench component for "
700+
<> fromString (packageNameString name)
693701
<> " so doing an all-in-one build."
694702
resolveDepsAndInstall
695703
True (lpBuildHaddocks lp) ps (lpPackage lp) minstalled
@@ -705,10 +713,10 @@ installPackage name ps minstalled = do
705713
pure (res, writerFunc)
706714
case res of
707715
Right deps -> do
708-
planDebug $
709-
"installPackage: For "
710-
<> show name
711-
<> ", successfully added package deps"
716+
logDebugPlanS "installPackage" $
717+
"For "
718+
<> fromString (packageNameString name)
719+
<> ", successfully added package deps."
712720
-- in curator builds we can't do all-in-one build as
713721
-- test/benchmark failure could prevent library from being
714722
-- available to its dependencies but when it's already available
@@ -727,10 +735,9 @@ installPackage name ps minstalled = do
727735
Left _ -> do
728736
-- Reset the state to how it was before attempting to find an
729737
-- all-in-one build plan.
730-
planDebug $
731-
"installPackage: Before trying cyclic plan, resetting lib \
732-
\result map to "
733-
<> show libMap
738+
logDebugPlanS "installPackage" $
739+
"Before trying cyclic plan, resetting lib result map to: "
740+
<> fromString (show libMap)
734741
put libMap
735742
-- Otherwise, fall back on building the tests / benchmarks in a
736743
-- separate step.
@@ -863,7 +870,7 @@ addPackageDeps package = do
863870
checkAndWarnForUnknownTools package
864871
let deps' = packageDeps package
865872
deps <- forM (Map.toList deps') $ \(depname, DepValue range depType) -> do
866-
eres <- addDep depname
873+
eres <- getCachedDepOrAddDep depname
867874
let getLatestApplicableVersionAndRev :: M (Maybe (Version, BlobKey))
868875
getLatestApplicableVersionAndRev = do
869876
vsAndRevs <-
@@ -1276,6 +1283,11 @@ inSnapshot name version = do
12761283
-- TODO: Consider intersecting version ranges for multiple deps on a
12771284
-- package. This is why VersionRange is in the parent map.
12781285

1279-
-- Switch this to 'True' to enable some debugging putStrLn in this module
1280-
planDebug :: MonadIO m => String -> m ()
1281-
planDebug = if False then liftIO . putStrLn else \_ -> pure ()
1286+
logDebugPlanS ::
1287+
(HasCallStack, HasRunner env, MonadIO m, MonadReader env m)
1288+
=> LogSource
1289+
-> Utf8Builder
1290+
-> m ()
1291+
logDebugPlanS s msg = do
1292+
debugPlan <- view $ globalOptsL.to globalPlanInLog
1293+
when debugPlan $ logDebugS s msg

src/Stack/Options/GlobalParser.hs

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ globalOptsParser currentDir kind = GlobalOptsMonoid
5757
"rsl-in-log"
5858
"inclusion of raw snapshot layer (rsl) in logs."
5959
hide
60+
<*> firstBoolFlagsFalse
61+
"plan-in-log"
62+
"inclusion of information about build plan construction in logs."
63+
hide
6064
<*> configOptsParser currentDir kind
6165
<*> optionalFirst (abstractResolverOptsParser hide0)
6266
<*> pure (First Nothing)
@@ -129,6 +133,7 @@ globalOptsFromMonoid defaultTerminal GlobalOptsMonoid{..} = do
129133
, globalLogLevel = fromFirst defaultLogLevel globalMonoidLogLevel
130134
, globalTimeInLog = fromFirstTrue globalMonoidTimeInLog
131135
, globalRSLInLog = fromFirstFalse globalMonoidRSLInLog
136+
, globalPlanInLog = fromFirstFalse globalMonoidPlanInLog
132137
, globalConfigMonoid = globalMonoidConfigMonoid
133138
, globalResolver = resolver
134139
, globalCompiler = getFirst globalMonoidCompiler

src/Stack/Types/GlobalOpts.hs

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ data GlobalOpts = GlobalOpts
2424
, globalTimeInLog :: !Bool -- ^ Whether to include timings in logs.
2525
, globalRSLInLog :: !Bool
2626
-- ^ Whether to include raw snapshot layer (RSL) in logs.
27+
, globalPlanInLog :: !Bool
28+
-- ^ Whether to include debug information about the construction of the
29+
-- build plan in logs.
2730
, globalConfigMonoid :: !ConfigMonoid
2831
-- ^ Config monoid, for passing into 'loadConfig'
2932
, globalResolver :: !(Maybe AbstractResolver) -- ^ Resolver override

src/Stack/Types/GlobalOptsMonoid.hs

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ data GlobalOptsMonoid = GlobalOptsMonoid
2424
-- ^ Whether to include timings in logs.
2525
, globalMonoidRSLInLog :: !FirstFalse
2626
-- ^ Whether to include raw snapshot layer (RSL) in logs.
27+
, globalMonoidPlanInLog :: !FirstFalse
28+
-- ^ Whether to include debug information about the construction of the
29+
-- build plan in logs.
2730
, globalMonoidConfigMonoid :: !ConfigMonoid
2831
-- ^ Config monoid, for passing into 'loadConfig'
2932
, globalMonoidResolver :: !(First (Unresolved AbstractResolver))

0 commit comments

Comments
 (0)