Skip to content

Commit e14a2b7

Browse files
authored
Merge pull request #5781 from mpilgrem/fix5780
Fix #5780 Accumulate missing keys before logging
2 parents 8406457 + e432da9 commit e14a2b7

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Bug fixes:
3636
[#5714](https://github.com/commercialhaskell/stack/issues/5714)
3737
* Fix an inconsistency in the pretty formatting of the output of
3838
`stack build --coverage`
39+
* Fix repeated warning about missing parameters when using `stack new`
3940

4041
## v2.7.5
4142

src/Stack/New.hs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ applyTemplate project template nonceParams dir templateText = do
271271
throwM (InvalidTemplate template
272272
"Template does not contain a .cabal or package.yaml file")
273273

274-
-- Apply Mustache templating to a single file within the project
275-
-- template.
274+
-- Apply Mustache templating to a single file within the project template.
276275
let applyMustache bytes
277276
-- Workaround for performance problems with mustache and
278277
-- large files, applies to Yesod templates with large
@@ -286,29 +285,40 @@ applyTemplate project template nonceParams dir templateText = do
286285
Right t -> return t
287286
let (substitutionErrors, applied) = Mustache.checkedSubstitute templateCompiled context
288287
missingKeys = S.fromList $ concatMap onlyMissingKeys substitutionErrors
289-
unless (S.null missingKeys)
290-
(logInfo ("\n" <> displayShow (MissingParameters project template missingKeys (configUserConfigPath config)) <> "\n"))
291-
pure $ LB.fromStrict $ encodeUtf8 applied
288+
pure (LB.fromStrict $ encodeUtf8 applied, missingKeys)
292289

293290
-- Too large or too binary
294-
| otherwise = pure bytes
295-
296-
liftM
297-
M.fromList
298-
(mapM
299-
(\(fpOrig,bytes) ->
300-
do -- Apply the mustache template to the filenames
301-
-- as well, so that we can have file names
302-
-- depend on the project name.
303-
fp <- applyMustache $ TLE.encodeUtf8 $ TL.pack fpOrig
304-
path <- parseRelFile $ TL.unpack $ TLE.decodeUtf8 fp
305-
bytes' <- applyMustache bytes
306-
return (dir </> path, bytes'))
307-
(M.toList files))
291+
| otherwise = pure (bytes, S.empty)
292+
293+
-- Accumulate any missing keys as the file is processed
294+
processFile mks (fpOrig, bytes) = do
295+
-- Apply the mustache template to the filenames as well, so that we
296+
-- can have file names depend on the project name.
297+
(fp, mks1) <- applyMustache $ TLE.encodeUtf8 $ TL.pack fpOrig
298+
path <- parseRelFile $ TL.unpack $ TLE.decodeUtf8 fp
299+
(bytes', mks2) <- applyMustache bytes
300+
return (mks <> mks1 <> mks2, (dir </> path, bytes'))
301+
302+
(missingKeys, results) <- mapAccumLM processFile S.empty (M.toList files)
303+
unless (S.null missingKeys) $ do
304+
let missingParamters = MissingParameters
305+
project
306+
template
307+
missingKeys
308+
(configUserConfigPath config)
309+
logInfo ("\n" <> displayShow missingParamters <> "\n")
310+
return $ M.fromList results
308311
where
309312
onlyMissingKeys (Mustache.VariableNotFound ks) = map T.unpack ks
310313
onlyMissingKeys _ = []
311314

315+
mapAccumLM :: Monad m => (a -> b -> m(a, c)) -> a -> [b] -> m(a, [c])
316+
mapAccumLM _ a [] = return (a, [])
317+
mapAccumLM f a (x:xs) = do
318+
(a', c) <- f a x
319+
(a'', cs) <- mapAccumLM f a' xs
320+
return (a'', c:cs)
321+
312322
-- | Check if we're going to overwrite any existing files.
313323
checkForOverwrite :: (MonadIO m, MonadThrow m) => [Path Abs File] -> m ()
314324
checkForOverwrite files = do

0 commit comments

Comments
 (0)