Skip to content

Commit 540dd1f

Browse files
authored
Merge pull request #6040 from commercialhaskell/pretty-2476
Prettier errors S-2476 and S-4764
2 parents 2574717 + c023240 commit 540dd1f

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

doc/maintainers/stack_errors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,10 @@ to take stock of the errors that Stack itself can raise, by reference to the
264264
[S-9561] | SetupInfoMissingSevenz
265265
[S-1457] | DockerStackExeNotFound Version Text
266266
[S-7748] | UnsupportedSetupConfiguration
267-
[S-2476] | InvalidGhcAt (Path Abs File) SomeException
268267
[S-5308] | MSYS2NotFound Text
269268
[S-5127] | UnwantedCompilerVersion
270269
[S-1540] | UnwantedArchitecture
271270
[S-9953] | SandboxedCompilerNotFound
272-
[S-4764] | CompilerNotFound [String]
273271
[S-8668] | GHCInfoNotValidUTF8 UnicodeException
274272
[S-4878] | GHCInfoNotListOfPairs
275273
[S-2965] | GHCInfoMissingGlobalPackageDB
@@ -300,7 +298,9 @@ to take stock of the errors that Stack itself can raise, by reference to the
300298
- `Stack.Setup.SetupPrettyException`
301299

302300
~~~haskell
303-
[S-7441]= GHCInstallFailed SomeException StyleDoc String [String] (Path Abs Dir) (Path Abs Dir) (Path Abs Dir)
301+
[S-7441] = GHCInstallFailed SomeException StyleDoc String [String] (Path Abs Dir) (Path Abs Dir) (Path Abs Dir)
302+
[S-2476] | InvalidGhcAt (Path Abs File) SomeException
303+
[S-4764] | ExecutableNotFound [Path Abs File]
304304
~~~
305305

306306
- `Stack.Storage.User.StorageUserException`

src/Stack/Prelude.hs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ module Stack.Prelude
5454
, mkNarrativeList
5555
, parens
5656
, parseStylesUpdateFromString
57+
, ppException
58+
, prettyDebug
5759
, prettyDebugL
5860
, prettyError
5961
, prettyErrorL
@@ -92,12 +94,12 @@ import RIO.PrettyPrint
9294
( HasStylesUpdate (..), HasTerm (..), Pretty (..), Style (..)
9395
, StyleDoc, (<+>), align, bulletedList, debugBracket
9496
, encloseSep, fill, fillSep, flow, hang, hcat, hsep, indent
95-
, line, logLevelToStyle, mkNarrativeList, parens
97+
, line, logLevelToStyle, mkNarrativeList, parens, prettyDebug
9698
, prettyDebugL, prettyError, prettyErrorL, prettyInfo
9799
, prettyInfoL, prettyInfoS, prettyNote, prettyWarn
98100
, prettyWarnL, prettyWarnNoIndent, prettyWarnS, punctuate
99101
, sep, softbreak, softline, string, style, stylesUpdateL
100-
, useColorL, vsep
102+
, useColorL, vsep, prettyDebug
101103
)
102104
import RIO.PrettyPrint.DefaultStyles (defaultStyles)
103105
import RIO.PrettyPrint.PrettyException ( PrettyException (..) )
@@ -343,3 +345,9 @@ bugRequest = "Please report this bug at Stack's repository."
343345
-- | A \'pretty\' blank line.
344346
blankLine :: StyleDoc
345347
blankLine = line <> line
348+
349+
-- | Provide the prettiest available information about an exception.
350+
ppException :: SomeException -> StyleDoc
351+
ppException e = case fromException e of
352+
Just (PrettyException e') -> pretty e'
353+
Nothing -> (string . displayException) e

src/Stack/Setup.hs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,10 @@ data SetupException
159159
| SetupInfoMissingSevenz
160160
| DockerStackExeNotFound Version Text
161161
| UnsupportedSetupConfiguration
162-
| InvalidGhcAt (Path Abs File) SomeException
163162
| MSYS2NotFound Text
164163
| UnwantedCompilerVersion
165164
| UnwantedArchitecture
166165
| SandboxedCompilerNotFound
167-
| CompilerNotFound [String]
168166
| GHCInfoNotValidUTF8 UnicodeException
169167
| GHCInfoNotListOfPairs
170168
| GHCInfoMissingGlobalPackageDB
@@ -256,13 +254,6 @@ instance Exception SetupException where
256254
"Error: [S-7748]\n"
257255
++ "Stack does not know how to install GHC on your system \
258256
\configuration, please install manually."
259-
displayException (InvalidGhcAt compiler e) = concat
260-
[ "Error: [S-2476]\n"
261-
, "Found an invalid compiler at "
262-
, show (toFilePath compiler)
263-
, ": "
264-
, displayException e
265-
]
266257
displayException (MSYS2NotFound osKey) = concat
267258
[ "Error: [S-5308]\n"
268259
, "MSYS2 not found for "
@@ -277,11 +268,6 @@ instance Exception SetupException where
277268
displayException SandboxedCompilerNotFound =
278269
"Error: [S-9953]\n"
279270
++ "Could not find sandboxed compiler."
280-
displayException (CompilerNotFound toTry) = concat
281-
[ "Error: [S-4764]\n"
282-
, "Could not find any of: "
283-
, show toTry
284-
]
285271
displayException (GHCInfoNotValidUTF8 e) = concat
286272
[ "Error: [S-8668]\n"
287273
, "GHC info is not valid UTF-8: "
@@ -407,11 +393,13 @@ data SetupPrettyException
407393
(Path Abs Dir)
408394
(Path Abs Dir)
409395
(Path Abs Dir)
396+
| InvalidGhcAt (Path Abs File) SomeException
397+
| ExecutableNotFound [Path Abs File]
410398
deriving (Show, Typeable)
411399

412400
instance Pretty SetupPrettyException where
413401
pretty (GHCInstallFailed ex step cmd args wd tempDir destDir) =
414-
"[S-7441]"
402+
"[S-7441]"
415403
<> line
416404
<> string (displayException ex)
417405
<> line
@@ -442,6 +430,24 @@ instance Pretty SetupPrettyException where
442430
, "flag."
443431
]
444432
<> line
433+
pretty (InvalidGhcAt compiler e) =
434+
"[S-2476]"
435+
<> line
436+
<> fillSep
437+
[ flow "Stack considers the compiler at"
438+
, pretty compiler
439+
, flow "to be invalid."
440+
]
441+
<> blankLine
442+
<> flow "While assessing that compiler, Stack encountered the error:"
443+
<> blankLine
444+
<> ppException e
445+
pretty (ExecutableNotFound toTry) =
446+
"[S-4764]"
447+
<> line
448+
<> flow "Stack could not find any of the following executables:"
449+
<> line
450+
<> bulletedList (map pretty toTry)
445451

446452
instance Exception SetupPrettyException
447453

@@ -1177,17 +1183,21 @@ pathsFromCompiler wc compilerBuild isSandboxed compiler =
11771183
suffixes = maybe id (:) msuffixWithVersion [suffixNoVersion]
11781184
findHelper :: (WhichCompiler -> [String]) -> RIO env (Path Abs File)
11791185
findHelper getNames = do
1180-
let toTry = [ dir ++ name ++ suffix
1181-
| suffix <- suffixes, name <- getNames wc
1182-
]
1183-
loop [] = throwIO $ CompilerNotFound toTry
1184-
loop (guessedPath':rest) = do
1185-
guessedPath <- parseAbsFile guessedPath'
1186+
toTry <- mapM
1187+
parseAbsFile
1188+
[ dir ++ name ++ suffix
1189+
| suffix <- suffixes, name <- getNames wc
1190+
]
1191+
let loop [] = throwIO $ PrettyException $ ExecutableNotFound toTry
1192+
loop (guessedPath:rest) = do
11861193
exists <- doesFileExist guessedPath
11871194
if exists
11881195
then pure guessedPath
11891196
else loop rest
1190-
logDebug $ "Looking for executable(s): " <> displayShow toTry
1197+
prettyDebug $
1198+
flow "Looking for executable(s):"
1199+
<> line
1200+
<> bulletedList (map pretty toTry)
11911201
loop toTry
11921202
pkg <- fmap GhcPkgExe $ findHelper $ \case
11931203
Ghc -> ["ghc-pkg"]
@@ -1268,7 +1278,7 @@ pathsFromCompiler wc compilerBuild isSandboxed compiler =
12681278
, cpGlobalDump = globalDump
12691279
}
12701280
where
1271-
onErr = throwIO . InvalidGhcAt compiler
1281+
onErr = throwIO . PrettyException . InvalidGhcAt compiler
12721282

12731283
withCache inner = do
12741284
eres <- tryAny $ loadCompilerPaths compiler compilerBuild isSandboxed

src/Stack/Types/Build.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,14 @@ instance Pretty BuildPrettyException where
395395
<> line
396396
<> flow "Stack failed to execute the build plan."
397397
<> blankLine
398-
<> flow "While executing the build plan, Stack encountered the \
399-
\following errors:"
398+
<> fillSep
399+
[ flow "While executing the build plan, Stack encountered the"
400+
, case es of
401+
[_] -> "error:"
402+
_ -> flow "following errors:"
403+
]
400404
<> blankLine
401-
<> hcat (L.intersperse blankLine (map ppExceptions es))
402-
where
403-
ppExceptions :: SomeException -> StyleDoc
404-
ppExceptions e = case fromException e of
405-
Just (PrettyException e') -> pretty e'
406-
Nothing -> (string . show) e
405+
<> hcat (L.intersperse blankLine (map ppException es))
407406
pretty (CabalExitedUnsuccessfully exitCode taskProvides' execName fullArgs logFiles bss) =
408407
showBuildError "[S-7011]"
409408
False exitCode (Just taskProvides') execName fullArgs logFiles bss

0 commit comments

Comments
 (0)