Skip to content

Commit

Permalink
Remove GHCi specific hints from GHC error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Jan 25, 2025
1 parent 8dc88eb commit 6a0d7b2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/GHC/Diagnostic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ data Severity = Warning | Error
deriving (Eq, Show, Generic, ToJSON, FromJSON)

parse :: ByteString -> Maybe Diagnostic
parse = decode . fromStrict
parse = fmap removeGhciSpecificHints . decode . fromStrict

format :: Diagnostic -> ByteString
format diagnostic = encodeUtf8 . render $ unlines [
Expand Down Expand Up @@ -90,3 +90,11 @@ format diagnostic = encodeUtf8 . render $ unlines [

unlines :: [Doc] -> Doc
unlines = foldr ($+$) empty

removeGhciSpecificHints :: Diagnostic -> Diagnostic
removeGhciSpecificHints diagnostic = diagnostic { hints = map f diagnostic.hints }
where
f :: String -> String
f input = case lines input of
[hint, "You may enable this language extension in GHCi with:", ghciHint] | " :set -X" `isPrefixOf` ghciHint -> hint
_ -> input
2 changes: 1 addition & 1 deletion test/GHC/DiagnosticSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import GHC.Diagnostic
test :: HasCallStack => FilePath -> Spec
test name = it name $ do
err <- translate <$> ghc ["-fno-diagnostics-show-caret"]
json <- encodeUtf8 <$> ghc ["-fdiagnostics-as-json"]
json <- encodeUtf8 <$> ghc ["-fdiagnostics-as-json", "--interactive", "-ignore-dot-ghci"]
ensureFile (dir </> "err.out") (encodeUtf8 err)
ensureFile (dir </> "err.json") json
Just diagnostic <- return $ parse json
Expand Down
2 changes: 1 addition & 1 deletion test/assets/use-BlockArguments/err.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"1.0","ghcVersion":"ghc-9.10.1","span":{"file":"test/assets/use-BlockArguments/Foo.hs","start":{"line":5,"column":10},"end":{"line":5,"column":22}},"severity":"Error","code":52095,"message":["Unexpected do block in function application:\n do return ()"],"hints":["Use parentheses.","Perhaps you intended to use BlockArguments"]}
{"version":"1.0","ghcVersion":"ghc-9.10.1","span":{"file":"test/assets/use-BlockArguments/Foo.hs","start":{"line":5,"column":10},"end":{"line":5,"column":22}},"severity":"Error","code":52095,"message":["Unexpected do block in function application:\n do return ()"],"hints":["Use parentheses.","Perhaps you intended to use BlockArguments\nYou may enable this language extension in GHCi with:\n :set -XBlockArguments"]}

0 comments on commit 6a0d7b2

Please sign in to comment.